Class FileUtil

java.lang.Object
org.yamcs.studio.script.FileUtil
Direct Known Subclasses:
FileUtil

public class FileUtil extends Object
The Utility class to help file operating.
  • Constructor Details

    • FileUtil

      public FileUtil()
  • Method Details

    • loadXMLFile

      public static org.jdom2.Element loadXMLFile(String filePath) throws Exception
      Load the root element of an XML file. The element is a JDOM Element.
      Parameters:
      filePath - path of the file. It must be an absolute path which can be either
      a workspace path such as /OPI Examples/Scripts/myfile.xml
      a local file system path such as C:\myfile.xml
      or an URL path such as http://mysite.com/myfile.xml. *
      Returns:
      root element of the XML file.
      Throws:
      Exception - if the file does not exist or is not a correct XML file.
    • loadXMLFile

      public static org.jdom2.Element loadXMLFile(String filePath, AbstractBaseEditPart widget) throws Exception
      Load the root element of an XML file. The element is a JDOM Element.
      Parameters:
      filePath - path of the file. It can be an absolute path or a relative path to the OPI that contains the specified widget. If it is an absolute path, it can be either
      a workspace path such as /OPI Examples/Scripts/myfile.xml
      a local file system path such as C:\myfile.xml
      or an URL path such as http://mysite.com/myfile.xml.
      widget - a widget in the OPI, which is used to provide relative path reference. It can be null if the path is an absolute path.
      Returns:
      root element of the XML file.
      Throws:
      Exception - if the file does not exist or is not a correct XML file.
    • getInputStreamFromFile

      public static InputStream getInputStreamFromFile(String filePath, AbstractBaseEditPart widget) throws Exception
      Return an InputStream of the file on the specified path. The client is responsible for closing the stream when finished.
      Parameters:
      filePath - path of the file. It can be an absolute path or a relative path to the OPI that contains the specified widget. If it is an absolute path, it can be either
      a workspace path such as /OPI Examples/Scripts/myfile.xml
      a local file system path such as C:\myfile.xml
      or an URL path such as http://mysite.com/myfile.xml.
      widget - a widget in the OPI, which is used to provide relative path reference. It can be null if the path is an absolute path.
      Returns:
      java.io.InputStream of the file.
      Throws:
      Exception - if the file does not exist.
    • getInputStreamFromFile

      public static InputStream getInputStreamFromFile(String filePath) throws Exception
      Throws:
      Exception
    • readTextFile

      public static String readTextFile(String filePath) throws Exception
      Read a text file.
      Parameters:
      filePath - path of the file. It must be an absolute path which can be either
      a workspace path such as /OPI Examples/Scripts/myfile.xml
      a local file system path such as C:\myfile.xml
      or an URL path such as http://mysite.com/myfile.xml.
      Returns:
      a string of the text.
      Throws:
      Exception - if the file does not exist or is not a correct text file.
    • readTextFile

      public static String readTextFile(String filePath, AbstractBaseEditPart widget) throws Exception
      Read a text file.
      Parameters:
      filePath - path of the file. It can be an absolute path or a relative path to the OPI that contains the specified widget. If it is an absolute path, it can be either
      a workspace path such as /OPI Examples/Scripts/myfile.xml
      a local file system path such as C:\myfile.xml
      or an URL path such as http://mysite.com/myfile.xml.
      widget - a widget in the OPI, which is used to provide relative path reference. It can be null if the path is an absolute path.
      Returns:
      a string of the text.
      Throws:
      Exception - if the file does not exist or is not a correct text file.
    • writeTextFile

      public static void writeTextFile(String filePath, boolean inWorkspace, String text, boolean append) throws Exception
      Write a text file.
      Parameters:
      filePath - path of the file. It must be an absolute path which can be either
      a workspace path such as /OPI Examples/Scripts/myfile.xml
      or a local file system path such as C:\myfile.xml.
      inWorkspace - true if the file path is a workspace file path. Otherwise, it will be recognized as a local file system file.
      text - the text to be written to the file.
      append - true if the text should be appended to the end of the file.
      Throws:
      Exception - if error happens.
    • writeTextFile

      public static void writeTextFile(String filePath, boolean inWorkspace, AbstractBaseEditPart widget, String text, boolean append) throws Exception
      Write a text file.
      Parameters:
      filePath - path of the file. It can be an absolute path or a relative path to the OPI that contains the specified widget. If it is an absolute path, it can be either
      a workspace path such as /OPI Examples/Scripts/myfile.xml
      a local file system path such as C:\myfile.xml
      or an URL path such as http://mysite.com/myfile.xml.
      inWorkspace - true if the file path is a workspace file path. Otherwise, it will be recognized as a local file system file.
      widget - a widget in the OPI, which is used to provide relative path reference. It can be null if the path is an absolute path.
      text - the text to be written to the file.
      append - true if the text should be appended to the end of the file.
      Throws:
      Exception - if error happens.
    • openFile

      public static void openFile(String filePath, AbstractBaseEditPart widget)
      Open a file in default editor. If no such an editor for the type of file, OS default program will be called to open this file.
      Parameters:
      filePath - path of the file. It can be an absolute path or a relative path to the OPI that contains the specified widget. If it is an absolute path, it can be either a workspace path such as
      /OPI Examples/Scripts/myfile.txt
      or a local file system path such as C:\myfile.txt.
      widget - a widget in the OPI, which is used to provide relative path reference. It can be null if the path is an absolute path.
    • openFile

      public static void openFile(String filePath)
    • openWebPage

      public static void openWebPage(String link)
      Open a web page.
      Parameters:
      link - link to the web page. It can be a link started with http://, https:// or file://.
    • playWavFile

      public static void playWavFile(String filePath, AbstractBaseEditPart widget)
      Play a .wav file.
      Parameters:
      filePath - path of the file. It can be an absolute path or a relative path to the OPI that contains the specified widget. If it is an absolute path, it can be either
      a workspace path such as /OPI Examples/Scripts/myfile.xml
      a local file system path such as C:\myfile.xml
      or an URL path such as http://mysite.com/myfile.xml.
      widget - a widget in the OPI, which is used to provide relative path reference. It can be null if the path is an absolute path.
    • playWavFile

      public static void playWavFile(String filePath)
    • openDirectoryDialog

      public static String openDirectoryDialog()
    • openDirectoryDialog

      public static String openDirectoryDialog(String startingFolder)
    • openFileDialog

      public static String openFileDialog(boolean inWorkspace)
      Open a file select dialog.
      Parameters:
      inWorkspace - true if it is a workspace file dialog; Otherwise, it is a local file system file dialog.
      Returns:
      the full file path. Or null if it is cancelled.
    • openFileDialog

      public static String openFileDialog(String startingFolder)
      Open a file select dialog.
      Parameters:
      startingFolder - the folder where the file dialog starts.
      Returns:
      the full file path. Or null if it is cancelled.
    • saveFileDialog

      public static String saveFileDialog(boolean inWorkspace)
      Open a file save dialog.
      Parameters:
      inWorkspace - true if it is a workspace file dialog; Otherwise, it is a local file system file dialog.
      Returns:
      the full file path. Or null if it is cancelled.
    • saveFileDialog

      public static String saveFileDialog(String startingFolder)
      Open a file save dialog.
      Parameters:
      startingFolder - the folder where the file dialog starts.
      Returns:
      the full file path. Or null if it is cancelled.
    • buildAbsolutePath

      protected static IPath buildAbsolutePath(String filePath, AbstractBaseEditPart widget)
    • workspacePathToSysPath

      public static String workspacePathToSysPath(String workspacePath)
      Convert a workspace path to system path. If this resource is a project that does not exist in the workspace, or a file or folder below such a project, this method returns null.
      Parameters:
      workspacePath - path in workspace.
      Returns:
      the system path on OS. Return an empty string if the path doesn't exist.