Class ResourcesModule

java.lang.Object
org.eclipse.ease.modules.AbstractScriptModule
org.eclipse.ease.modules.platform.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)
      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
    • createProject

      public static org.eclipse.core.resources.IProject createProject​(String name)
      Create a new workspace project. Will create a new project if it not already exists. If creation fails, null is returned.
      Parameters:
      name - name or project to create
      Returns:
      null or project
    • createFolder

      public Object createFolder​(Object location) throws org.eclipse.core.runtime.CoreException
      Create a new folder in the workspace or the file system.
      Parameters:
      location - folder location
      Returns:
      IFolder, File or null in case of error
      Throws:
      org.eclipse.core.runtime.CoreException - 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 Exception
      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:
      Exception - problems on file access
    • openFile

      public IFileHandle openFile​(Object location, @ScriptParameter(defaultValue="1") int mode, @ScriptParameter(defaultValue="true") boolean autoClose) throws Exception
      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:
      Exception - 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 Exception
      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:
      Exception - problems on file access
    • copyFile

      public void copyFile​(Object sourceLocation, Object targetLocation) throws Exception
      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:
      Exception - problems on file access
    • deleteFile

      public void deleteFile​(Object source) throws org.eclipse.core.runtime.CoreException
      Delete a file from the workspace or local file system.
      Parameters:
      source - file to be deleted
      Throws:
      org.eclipse.core.runtime.CoreException - on deletion errors
    • deleteFolder

      public void deleteFolder​(Object source) throws org.eclipse.core.runtime.CoreException
      Delete a folder from the workspace or local file system.
      Parameters:
      source - folder to be deleted
      Throws:
      org.eclipse.core.runtime.CoreException - on deletion errors
    • deleteProject

      public void deleteProject​(Object source) throws org.eclipse.core.runtime.CoreException
      Delete a project from the workspace.
      Parameters:
      source - project to be deleted
      Throws:
      org.eclipse.core.runtime.CoreException - on deletion errors
    • readLine

      public String readLine​(Object location) throws Exception
      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:
      Exception - problems on file access
    • writeFile

      public IFileHandle writeFile​(Object location, Object data, @ScriptParameter(defaultValue="2") int mode, @ScriptParameter(defaultValue="true") boolean autoClose) throws Exception
      Write data to a file. When not using an IFileHandle, previous file content will be overridden. Files that do not exist yet will be automatically created. 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:
      Exception - problems on file access
    • writeLine

      public IFileHandle writeLine​(Object location, String data, @ScriptParameter(defaultValue="2") int mode, @ScriptParameter(defaultValue="true") boolean autoClose) throws Exception
      Write a line of data to a file. When not using an IFileHandle, previous file content will be overridden. Files that do not exist yet will be automatically created.
      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:
      Exception - 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)
      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, other for open dialog)
      title - dialog title
      message - dialog message
      Returns:
      full path to selected file
    • 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)
      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:
      path to selected folder
    • findFiles

      public Object[] findFiles​(String pattern, @ScriptParameter(defaultValue="org.eclipse.ease.modules.ScriptParameter.null") Object rootFolder, @ScriptParameter(defaultValue="true") boolean recursive)
      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 files
    • linkProject

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

      public org.eclipse.core.resources.IProject importProject​(Object location) throws InvocationTargetException, InterruptedException, IOException, org.eclipse.core.runtime.CoreException
      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 (needs to contain .project file)
      Returns:
      project instance, throws otherwise
      Throws:
      InvocationTargetException - on errors during the import
      InterruptedException - when import task got interrupted
      IOException - when project cannot be located on disk
      org.eclipse.core.runtime.CoreException - when project description cannot be loaded from .project file
    • refreshResource

      public void refreshResource​(Object resource) throws org.eclipse.core.runtime.CoreException
      Refresh a given resource and all its child elements.
      Parameters:
      resource - IFile, IFolder, IProject or workspace root to update
      Throws:
      org.eclipse.core.runtime.CoreException - 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
    • 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 org.eclipse.core.runtime.CoreException
      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:
      org.eclipse.core.runtime.CoreException - when marker cannot be created
    • notify

      public void notify​(IScriptEngine engine, Script script, int status)
      Specified by:
      notify in interface IExecutionListener
    • 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
    • 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 NoSuchAlgorithmException, 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:
      NoSuchAlgorithmException - when MD5 digest cannot be found
      IOException - when resource cannot be read