Class ResourcesModule

java.lang.Object
org.eclipse.ease.modules.AbstractScriptModule
org.eclipse.ease.modules.platform.resources.ResourcesModule
All Implemented Interfaces:
IExecutionListener, IScriptModule

public class ResourcesModule extends AbstractScriptModule implements IExecutionListener
Provides file access for workspace and file system resources. Methods accepting location objects can deal with String, URI, IFile and File instances.
  • Field Details

  • Constructor Details

    • ResourcesModule

      public ResourcesModule()
  • Method Details

    • getWorkspace

      public static org.eclipse.core.resources.IWorkspaceRoot getWorkspace()
      Get the workspace root.
      Returns:
      workspace root
    • getProject

      public static org.eclipse.core.resources.IProject getProject(String name)
      Get a project instance.
      Parameters:
      name - project name
      Returns:
      project or null
    • getFile

      public Object getFile(String location, @ScriptParameter(defaultValue="true") boolean exists) throws FileNotFoundException
      Get a workspace or file system file. Resolves relative and absolute file locations. Relative files are resolved against the current script file. If exists is false this method also returns files that do not exist yet. If true only existing instances are returned.
      Parameters:
      location - file location/path to resolve
      exists - whether the resolved file needs to exist
      Returns:
      resolved IFile or File instance
      Throws:
      FileNotFoundException - when file does not exist and exists is true
    • createProject

      public static org.eclipse.core.resources.IProject createProject(String name) throws IOException
      Create a new workspace project. Will create a new project if it not already exists. In case the project already exists, the existing instance will be returned.
      Parameters:
      name - name of project to create
      Returns:
      project instance
      Throws:
      IOException - when project creation fails
    • createFolder

      public Object createFolder(Object location) throws IOException
      Create a new folder in the workspace or the file system.
      Parameters:
      location - folder location
      Returns:
      IFolder or File instance
      Throws:
      IOException - if this method fails. Reasons include:
      • This resource already exists in the workspace.
      • The workspace contains a resource of a different type at the same path as this resource.
      • The parent of this resource does not exist.
      • The parent of this resource is a project that is not open.
      • The parent contains a resource of a different type at the same path as this resource.
      • The parent of this resource is virtual, but this resource is not.
      • The name of this resource is not valid (according to IWorkspace.validateName).
      • The corresponding location in the local file system is occupied by a file (as opposed to a directory).
      • The corresponding location in the local file system is occupied by a folder and force is false.
      • Resource changes are disallowed during certain types of resource change event notification. See IResourceChangeEvent for more details.
    • createFile

      public Object createFile(Object location) throws IOException
      Create a new file in the workspace or the file system.
      Parameters:
      location - file location
      Returns:
      IFile, File or null in case of error
      Throws:
      IOException - problems on file access
    • openFile

      public IFileHandle openFile(Object location, @ScriptParameter(defaultValue="1") int mode, @ScriptParameter(defaultValue="true") boolean autoClose) throws IOException
      Opens a file from the workspace or the file system. If the file does not exist and we open it for writing, the file is created automatically.
      Parameters:
      location - file location
      mode - one of , ,
      autoClose - automatically close resource when script engine is terminated
      Returns:
      file handle instance to be used for file modification commands
      Throws:
      IOException - problems on file access
    • fileExists

      public boolean fileExists(Object location)
      Verifies that a specific file exists.
      Parameters:
      location - file location to verify
      Returns:
      true if file exists
    • closeFile

      public void closeFile(IFileHandle handle)
      Close a file. Releases system resources bound by an open file.
      Parameters:
      handle - handle to be closed
    • readFile

      public String readFile(Object location, @ScriptParameter(defaultValue="-1") int bytes) throws IOException
      Read data from a file. To repeatedly read from a file retrieve a IFileHandle first using and use the handle for location.
      Parameters:
      location - file location, file handle or file instance
      bytes - amount of bytes to read (-1 for whole file)
      Returns:
      file data or null if EOF is reached
      Throws:
      IOException - problems on file access
    • copyFile

      public void copyFile(Object sourceLocation, Object targetLocation) throws IOException
      Copies a file from location to targetLocation.
      Parameters:
      sourceLocation - file location, file handle or file instance for the source object
      targetLocation - file location, file handle or file instance for the target object
      Throws:
      IOException - problems on file access
    • deleteFile

      public void deleteFile(Object location) throws IOException
      Delete a file. Does nothing if location cannot be resolved to an existing file.
      Parameters:
      location - file to be deleted
      Throws:
      IOException - on deletion errors
    • deleteFolder

      public void deleteFolder(Object location) throws IOException
      Delete a folder recursively.
      Parameters:
      location - folder to be deleted
      Throws:
      IOException - on deletion errors
    • deleteProject

      public void deleteProject(Object source) throws IOException
      Delete a project from the workspace.
      Parameters:
      source - location or name of project to be deleted
      Throws:
      IOException - on deletion errors
    • readLine

      public String readLine(Object location) throws IOException
      Read a single line from a file. To repeatedly read from a file retrieve a IFileHandle first using and use the handle for location.
      Parameters:
      location - file location, file handle or file instance
      Returns:
      line of text or null if EOF is reached
      Throws:
      IOException - problems on file access
    • writeFile

      public IFileHandle writeFile(Object location, Object data, @ScriptParameter(defaultValue="2") int mode, @ScriptParameter(defaultValue="true") boolean autoClose) throws IOException
      Write data to a file. Files that do not exist yet will be created automatically. After the write operation the file remains open. It needs to be closed explicitly using the command.
      Parameters:
      location - file location
      data - data to be written
      mode - write mode (/)
      autoClose - automatically close resource when script engine is terminated
      Returns:
      file handle to continue write operations
      Throws:
      IOException - problems on file access
    • writeLine

      public IFileHandle writeLine(Object location, String data, @ScriptParameter(defaultValue="2") int mode, @ScriptParameter(defaultValue="true") boolean autoClose) throws IOException
      Write a line of data to a file. Files that do not exist yet will be created automatically. After the write operation the file remains open. It needs to be closed explicitly using the command.
      Parameters:
      location - file location
      data - data to be written
      mode - write mode (/)
      autoClose - automatically close resource when script engine is terminated
      Returns:
      file handle to continue write operations
      Throws:
      IOException - problems on file access
    • showFileSelectionDialog

      public String showFileSelectionDialog(@ScriptParameter(defaultValue="org.eclipse.ease.modules.ScriptParameter.null") Object rootFolder, @ScriptParameter(defaultValue="0") int type, @ScriptParameter(defaultValue="org.eclipse.ease.modules.ScriptParameter.null") String title, @ScriptParameter(defaultValue="org.eclipse.ease.modules.ScriptParameter.null") String message) throws IOException
      Opens a file dialog. Depending on the rootFolder a workspace dialog or a file system dialog will be used. If the folder cannot be located, the workspace root folder is used by default. When type is set to or a save dialog will be shown instead of the default open dialog.
      Parameters:
      rootFolder - root folder path to use
      type - dialog type to use (/ for save dialog, for open dialog)
      title - dialog title
      message - dialog message
      Returns:
      full path to selected file either as absolute file system path or as absolute workspace URI. Returns null when the dialog is cancelled by the user.
      Throws:
      IOException - when root folder cannot be found
    • showFolderSelectionDialog

      public String showFolderSelectionDialog(@ScriptParameter(defaultValue="org.eclipse.ease.modules.ScriptParameter.null") Object rootFolder, @ScriptParameter(defaultValue="org.eclipse.ease.modules.ScriptParameter.null") String title, @ScriptParameter(defaultValue="org.eclipse.ease.modules.ScriptParameter.null") String message) throws IOException
      Opens a dialog box which allows the user to select a container (project or folder). Workspace paths will always display the workspace as root object.
      Parameters:
      rootFolder - root folder to display: for workspace paths this will set the default selection
      title - dialog title
      message - dialog message
      Returns:
      full path to selected folder either as absolute file system path or as absolute workspace URI. Returns null when the dialog is cancelled by the user.
      Throws:
      IOException - when root folder cannot be found
    • findFiles

      public String[] findFiles(String pattern, @ScriptParameter(defaultValue="org.eclipse.ease.modules.ScriptParameter.null") Object rootFolder, @ScriptParameter(defaultValue="true") boolean recursive) throws IOException
      Return files matching a certain pattern.
      Parameters:
      pattern - search pattern: use * and ? as wildcards. If the pattern starts with '^' then a regular expression can be used.
      rootFolder - root folder to start your search from. null for workspace root
      recursive - searches subfolders when set to true
      Returns:
      An array of all matching file locations
      Throws:
      IOException - when search fails
    • linkProject

      public org.eclipse.core.resources.IProject linkProject(Object location) throws IOException
      Links a project into the current workspace. Does not copy resources to the workspace.
      Parameters:
      location - location of project folder (needs to contain .project file)
      Returns:
      linked project, throws otherwise
      Throws:
      IOException - when project location cannot be read/linked
    • importProject

      public org.eclipse.core.resources.IProject importProject(Object location) throws IOException
      Imports a project into the current workspace. The project needs to be available on the file system and needs to be unpacked. Zipped projects cannot be imported.
      Parameters:
      location - location of project folder (needs to contain .project file)
      Returns:
      project instance, throws otherwise
      Throws:
      IOException - on any import errors
    • refreshResource

      public void refreshResource(Object resource) throws IOException
      Refresh a given resource and all its child elements.
      Parameters:
      resource - IFile, IFolder, IProject or workspace root to update
      Throws:
      IOException - if this method fails. Reasons include:
      • Resource changes are disallowed during certain types of resource change event notification. See IResourceChangeEvent for more details.
    • readStream

      public String readStream(InputStream input) throws IOException
      Read from an InputStream into a string. Consumes an InputStream and stores all available data in a string. Usually a stream is only readable once.
      Parameters:
      input - input stream to read from
      Returns:
      string content of stream.
      Throws:
      IOException - on read error on the stream
    • readUrl

      public String readUrl(String address) throws IOException
      Read full content from a given URL.
      Parameters:
      address - address to read from
      Returns:
      read data
      Throws:
      IOException - on read error on the stream
    • createProblemMarker

      public void createProblemMarker(String severity, Object location, int lineNumber, String message, @ScriptParameter(defaultValue="org.eclipse.core.resources.problemmarker") String type, @ScriptParameter(defaultValue="true") boolean permanent) throws IOException
      Create a problem marker on a file resource.
      Parameters:
      severity - one of error/warning/info
      location - file resource to create marker for
      lineNumber - line number to set marker on
      message - message to be added to the marker
      type - marker type to create, needs to match an existing type
      permanent - true for permanent markers, false for temporary markers
      Throws:
      IOException - when marker cannot be created
    • notify

      public void notify(IScriptEngine engine, Script script, int status)
      Description copied from interface: IExecutionListener
      Notifies the listeners when script execution started/ended or when the engine itself is started/ended.
      Specified by:
      notify in interface IExecutionListener
      Parameters:
      engine - Script Engine
      script - Script or null
      status - Event Status
    • zip

      public Object zip(Object sourceLocation, Object zipLocation) throws IOException
      Create or update a zip file with given resources.
      Parameters:
      sourceLocation - source folder/file to zip
      zipLocation - zip file to use
      Returns:
      zip file reference
      Throws:
      IOException - when zip file cannot be created/appended
    • unzip

      public Object unzip(Object zipLocation, Object targetLocation) throws IOException
      Unzip a zip file to the provided target location.
      Parameters:
      zipLocation - zip file to unzip
      targetLocation - folder to unzip to
      Returns:
      target location reference
      Throws:
      IOException - when zip file cannot be unzipped
    • getChecksum

      public String getChecksum(Object location) throws IOException
      Get an MD5 checksum over a readable resource.
      Parameters:
      location - location of resource to create checksum for
      Returns:
      file hash as hex encoded string
      Throws:
      IOException - when resource cannot be read