org.csstudio.opibuilder.scriptUtil

## Class FileUtil

- java.lang.Object
- - org.csstudio.opibuilder.scriptUtil.FileUtil

-     public class FileUtil
      extends java.lang.Object

  The Utility class to help file operating.
  Author:
    Xihui Chen

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `FileUtil()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `static java.io.InputStream` | `getInputStreamFromFile(java.lang.String filePath, org.csstudio.opibuilder.editparts.AbstractBaseEditPart widget)` Return an `InputStream` of the file on the specified path.
    `static org.jdom.Element` | `loadXMLFile(java.lang.String filePath)` Load the root element of an XML file.
    `static org.jdom.Element` | `loadXMLFile(java.lang.String filePath, org.csstudio.opibuilder.editparts.AbstractBaseEditPart widget)` Load the root element of an XML file.
    `static void` | `openFile(java.lang.String filePath, org.csstudio.opibuilder.editparts.AbstractBaseEditPart widget)` Open a file in default editor.
    `static java.lang.String` | `openFileDialog(boolean inWorkspace)` Open a file select dialog.
    `static void` | `openWebPage(java.lang.String link)` Open a web page.
    `static void` | `playWavFile(java.lang.String filePath, org.csstudio.opibuilder.editparts.AbstractBaseEditPart widget)` Play a .wav file.
    `static java.lang.String` | `readTextFile(java.lang.String filePath)` Read a text file.
    `static java.lang.String` | `readTextFile(java.lang.String filePath, org.csstudio.opibuilder.editparts.AbstractBaseEditPart widget)` Read a text file.
    `static java.lang.String` | `workspacePathToSysPath(java.lang.String workspacePath)` Convert an exist workspace path to system path.
    `static void` | `writeTextFile(java.lang.String filePath, boolean inWorkspace, org.csstudio.opibuilder.editparts.AbstractBaseEditPart widget, java.lang.String text, boolean append)` Write a text file.
    `static void` | `writeTextFile(java.lang.String filePath, boolean inWorkspace, java.lang.String text, boolean append)` Write a text file.

    - ### Methods inherited from class java.lang.Object

      `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### FileUtil

          public FileUtil()

  - ### Method Detail

    - #### loadXMLFile

          public static org.jdom.Element loadXMLFile(java.lang.String filePath)
                                              throws java.lang.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 `/BOY 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:
        `java.lang.Exception` - if the file does not exist or is not a correct XML file.

    - #### loadXMLFile

          public static org.jdom.Element loadXMLFile(java.lang.String filePath,
                                     org.csstudio.opibuilder.editparts.AbstractBaseEditPart widget)
                                              throws java.lang.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 `/BOY 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:
        `java.lang.Exception` - if the file does not exist or is not a correct XML file.

    - #### getInputStreamFromFile

          public static java.io.InputStream getInputStreamFromFile(java.lang.String filePath,
                                                   org.csstudio.opibuilder.editparts.AbstractBaseEditPart widget)
                                                            throws java.lang.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 `/BOY 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:
        `java.lang.Exception` - if the file does not exist.

    - #### readTextFile

          public static java.lang.String readTextFile(java.lang.String filePath)
                                               throws java.lang.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 `/BOY 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:
        `java.lang.Exception` - if the file does not exist or is not a correct text file.

    - #### readTextFile

          public static java.lang.String readTextFile(java.lang.String filePath,
                                      org.csstudio.opibuilder.editparts.AbstractBaseEditPart widget)
                                               throws java.lang.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 `/BOY 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:
        `java.lang.Exception` - if the file does not exist or is not a correct text file.

    - #### writeTextFile

          public static void writeTextFile(java.lang.String filePath,
                           boolean inWorkspace,
                           java.lang.String text,
                           boolean append)
                                    throws java.lang.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 `/BOY 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:
        `java.lang.Exception` - if error happens.

    - #### writeTextFile

          public static void writeTextFile(java.lang.String filePath,
                           boolean inWorkspace,
                           org.csstudio.opibuilder.editparts.AbstractBaseEditPart widget,
                           java.lang.String text,
                           boolean append)
                                    throws java.lang.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 `/BOY 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:
        `java.lang.Exception` - if error happens.

    - #### openFile

          public static void openFile(java.lang.String filePath,
                      org.csstudio.opibuilder.editparts.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
        `/BOY 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.

    - #### openWebPage

          public static void openWebPage(java.lang.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(java.lang.String filePath,
                         org.csstudio.opibuilder.editparts.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 `/BOY 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.

    - #### openFileDialog

          public static java.lang.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 canceled.

    - #### workspacePathToSysPath

          public static java.lang.String workspacePathToSysPath(java.lang.String workspacePath)
                                                         throws java.lang.RuntimeException

      Convert an exist workspace path to system path.
      Parameters:
        `workspacePath` - path in workspace.
      Returns:
        the system path on OS.
      Throws:
        `java.lang.RuntimeException` - if the workspace path doesn't exist.