Package org.yamcs.studio.script
Class FileUtil
java.lang.Object
org.yamcs.studio.script.FileUtil
- Direct Known Subclasses:
FileUtil
The Utility class to help file operating.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected static IPath
buildAbsolutePath
(String filePath, AbstractBaseEditPart widget) static InputStream
getInputStreamFromFile
(String filePath) static InputStream
getInputStreamFromFile
(String filePath, AbstractBaseEditPart widget) Return anInputStream
of the file on the specified path.static org.jdom2.Element
loadXMLFile
(String filePath) Load the root element of an XML file.static org.jdom2.Element
loadXMLFile
(String filePath, AbstractBaseEditPart widget) Load the root element of an XML file.static String
static String
openDirectoryDialog
(String startingFolder) static void
static void
openFile
(String filePath, AbstractBaseEditPart widget) Open a file in default editor.static String
openFileDialog
(boolean inWorkspace) Open a file select dialog.static String
openFileDialog
(String startingFolder) Open a file select dialog.static void
openWebPage
(String link) Open a web page.static void
playWavFile
(String filePath) static void
playWavFile
(String filePath, AbstractBaseEditPart widget) Play a .wav file.static String
readTextFile
(String filePath) Read a text file.static String
readTextFile
(String filePath, AbstractBaseEditPart widget) Read a text file.static String
saveFileDialog
(boolean inWorkspace) Open a file save dialog.static String
saveFileDialog
(String startingFolder) Open a file save dialog.static String
workspacePathToSysPath
(String workspacePath) Convert a workspace path to system path.static void
writeTextFile
(String filePath, boolean inWorkspace, String text, boolean append) Write a text file.static void
writeTextFile
(String filePath, boolean inWorkspace, AbstractBaseEditPart widget, String text, boolean append) Write a text file.
-
Constructor Details
-
FileUtil
public FileUtil()
-
-
Method Details
-
loadXMLFile
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 asC:\myfile.xml
or an URL path such ashttp://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 asC:\myfile.xml
or an URL path such ashttp://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 anInputStream
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 asC:\myfile.xml
or an URL path such ashttp://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
- Throws:
Exception
-
readTextFile
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 asC:\myfile.xml
or an URL path such ashttp://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
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 asC:\myfile.xml
or an URL path such ashttp://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 asC:\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 asC:\myfile.xml
or an URL path such ashttp://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
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 asC:\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
-
openWebPage
Open a web page.- Parameters:
link
- link to the web page. It can be a link started with http://, https:// or file://.
-
playWavFile
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 asC:\myfile.xml
or an URL path such ashttp://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
-
openDirectoryDialog
-
openDirectoryDialog
-
openFileDialog
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
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
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
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
-
workspacePathToSysPath
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.
-