Class Script

java.lang.Object
weka.gui.scripting.Script
All Implemented Interfaces:
Serializable, OptionHandler
Direct Known Subclasses:
GroovyScript, JythonScript

public abstract class Script extends Object implements OptionHandler, Serializable
A simple helper class for loading, saving scripts.
Version:
$Revision: 10222 $
Author:
fracpete (fracpete at waikato dot ac dot nz)
See Also:
  • Field Details

  • Constructor Details

    • Script

      public Script()
      Initializes the script.
    • Script

      public Script(Document doc)
      Initializes the script.
      Parameters:
      doc - the document to use as basis
    • Script

      public Script(Document doc, File file)
      Initializes the script. Automatically loads the specified file, if not null.
      Parameters:
      doc - the document to use as basis
      file - the file to load (if not null)
  • Method Details

    • listOptions

      public Enumeration<Option> listOptions()
      Returns an enumeration describing the available options.
      Specified by:
      listOptions in interface OptionHandler
      Returns:
      an enumeration of all the available options
    • setOptions

      public void setOptions(String[] options) throws Exception
      Parses a given list of options.
      Specified by:
      setOptions in interface OptionHandler
      Parameters:
      options - the list of options as an array of strings
      Throws:
      Exception - if an option is not supported
    • getOptions

      public String[] getOptions()
      Gets the current settings of the script.
      Specified by:
      getOptions in interface OptionHandler
      Returns:
      an array of strings suitable for passing to setOptions
    • getFilters

      public abstract ExtensionFileFilter[] getFilters()
      Returns the extension filters for this type of script.
      Returns:
      the filters
    • getDefaultExtension

      public abstract String getDefaultExtension()
      Returns the default extension. Gets automatically added to files if their name doesn't end with this.
      Returns:
      the default extension (incl. the dot)
      See Also:
    • getFilename

      public File getFilename()
      Returns the current filename.
      Returns:
      the filename, null if no file loaded/saved
    • getNewLine

      public String getNewLine()
      Returns the new line string in use.
      Returns:
      the new line string
    • isModified

      public boolean isModified()
      Returns whether the script is modified.
      Returns:
      true if the script is modified
    • getContent

      public String getContent()
      Returns the content.
      Returns:
      the content or null in case of an error
    • setContent

      public void setContent(String value)
      Sets the content.
      Parameters:
      value - the new content
    • empty

      public void empty()
      Empties the document.
    • open

      public boolean open(File file)
      Tries to open the file.
      Parameters:
      file - the file to open
      Returns:
      true if successfully read
    • save

      public boolean save()
      Saves the file under with the current filename.
      Returns:
      true if successfully written
    • saveAs

      public boolean saveAs(File file)
      Saves the file under with the given filename (and updates the internal filename).
      Parameters:
      file - the filename to write the content to
      Returns:
      true if successfully written
    • newThread

      public abstract Script.ScriptThread newThread(String[] args)
      Returns a new thread to execute.
      Parameters:
      args - optional commandline arguments
      Returns:
      the new thread object
    • start

      public void start(String[] args) throws Exception
      Executes the script.
      Parameters:
      args - optional commandline arguments, can be null
      Throws:
      Exception - if checks or execution fail
    • stop

      public void stop()
      Stops the execution of the script.
    • run

      public void run(File file, String[] args)
      Executes the script without loading it first.
      Parameters:
      file - the script to execute
      args - the commandline parameters for the script
    • isRunning

      public boolean isRunning()
      Returns whether the script is still running.
      Returns:
      true if the script is still running
    • addScriptFinishedListener

      public void addScriptFinishedListener(ScriptExecutionListener l)
      Adds the given listener to its internal list.
      Parameters:
      l - the listener to add
    • removeScriptFinishedListener

      public void removeScriptFinishedListener(ScriptExecutionListener l)
      Removes the given listener from its internal list.
      Parameters:
      l - the listener to remove
    • toString

      public String toString()
      Returns the content as string.
      Overrides:
      toString in class Object
      Returns:
      the current content
    • runScript

      public static void runScript(Script script, String[] args) throws Exception
      Runs the specified script. All options that weren't "consumed" (like "-s" for the script filename), will be used as commandline arguments for the actual script.
      Parameters:
      script - the script object to use
      args - the commandline arguments
      Throws:
      Exception - if execution fails