|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectptolemy.kernel.util.NamedObj
ptolemy.kernel.util.Attribute
ptolemy.kernel.util.AbstractSettableAttribute
ptolemy.data.expr.Variable
ptolemy.data.expr.Parameter
ptolemy.data.expr.StringParameter
ptolemy.data.expr.FileParameter
public class FileParameter
This is an attribute that specifies a file or URL. The value of this attribute, accessed by getExpression(), is a string that names a file or URL, possibly containing references to variables defined in scope using the syntax $ID, ${ID} or $(ID). The value returned by getToken() is name of the file with such references resolved.
If this attribute contains a parameter named allowFiles with value false, then when a file browser is used to select a file, that file browser will be set to not show files (only directories will be shown). If this attribute contains a parameter named allowDirectories with value true, then the file browser will permit the user to select directories (the default behavior is that when a directory is selected, that directory is opened and its contained files and directories are listed).
If the model containing this attribute has been saved to a MoML file, then the file name can be given relative to the directory containing that MoML file. If the model has not been saved to a file, then the classpath is used for identifying relative file names.
Files can be given relative to a base, where the base is the URI of the first container above this one that has a URIAttribute. Normally, this URI specifies the file or URL containing the model definition. Thus, files that are referred to here can be kept in the same directory as the model, or in a related directory, and can moved together with the model.
The following special file names are understood:
$CWD
The current working directoryuser.dir
$HOME
The user's home directoryuser.home
$PTII
The home directory of the Ptolemy II installationptolemy.ptII.dir
$TMPDIR
The temporary directoryjava.io.tmpdir
String | Description | Property |
---|---|---|
The above properties are normally set when a Ptolemy II application starts.
If a file name begins with the reference "$CLASSPATH", then when the file is opened for reading, the openForReading() method will search for the file relative to the classpath (using the getResource() method of the current class loader). This will only work for a file that exists, and thus the openForWriting() method will not understand the "$CLASSPATH" string; this makes sense since the classpath typically has several directories, and it would not be obvious where to create the file. The asURL() method also recognizes the "$CLASSPATH" string, but not the asFile() method (which is typically used when accessing a file for writing). NOTE: If the container of this parameter also contains a variable named CLASSPATH, then the value of that variable is used instead of the Java classpath.
URIAttribute
,
Serialized Form
Yellow (cxh) |
Green (eal) |
Nested Class Summary |
---|
Nested classes/interfaces inherited from class ptolemy.data.expr.Variable |
---|
Variable.VariableScope |
Nested classes/interfaces inherited from class ptolemy.kernel.util.NamedObj |
---|
NamedObj.ContainedObjectsIterator |
Nested classes/interfaces inherited from interface ptolemy.kernel.util.Settable |
---|
Settable.Visibility |
Field Summary | |
---|---|
private java.net.URI |
_baseDirectory
The base directory to use for relative file names. |
private boolean |
_isOutput
Whether the file is to be written to. |
private java.io.BufferedReader |
_reader
The current reader for the input file. |
private java.io.Writer |
_writer
The current writer for the output file. |
Fields inherited from class ptolemy.data.expr.Variable |
---|
_currentExpression, _needsEvaluation, _parserScope, _parseTreeValid, _valueListeners |
Fields inherited from class ptolemy.kernel.util.NamedObj |
---|
_changeListeners, _changeLock, _changeRequests, _debugging, _debugListeners, _elementName, _isPersistent, _verbose, _workspace, ATTRIBUTES, CLASSNAME, COMPLETE, CONTENTS, DEEP, FULLNAME, LINKS |
Fields inherited from interface ptolemy.kernel.util.Settable |
---|
EXPERT, FULL, NONE, NOT_EDITABLE |
Constructor Summary | |
---|---|
FileParameter(NamedObj container,
java.lang.String name)
Construct an attribute with the given name contained by the specified container. |
|
FileParameter(NamedObj container,
java.lang.String name,
boolean isOutput)
Construct an attribute with the given name contained by the specified container. |
Method Summary | |
---|---|
java.io.File |
asFile()
Return the file as a File object. |
java.net.URL |
asURL()
Return the file as a URL. |
java.lang.Object |
clone(Workspace workspace)
Clone the attribute into the specified workspace. |
void |
close()
Close the file. |
java.net.URI |
getBaseDirectory()
Return the directory to use as the base for relative file or URL names. |
boolean |
isOutput()
Return whether the file is to be written to. |
java.io.BufferedReader |
openForReading()
Open the file or URL for reading. |
java.io.Writer |
openForWriting()
Open the file for writing. |
java.io.Writer |
openForWriting(boolean append)
Open the file for writing or appending. |
void |
setBaseDirectory(java.net.URI directory)
Set the directory to use as the base for relative file or URL names. |
Methods inherited from class ptolemy.data.expr.StringParameter |
---|
stringValue |
Methods inherited from class ptolemy.data.expr.Parameter |
---|
addChoice, exportMoML, getChoices, removeAllChoices, removeChoice |
Methods inherited from class ptolemy.kernel.util.AbstractSettableAttribute |
---|
getDefaultExpression |
Methods inherited from class ptolemy.kernel.util.Attribute |
---|
_checkContainer, _getContainedObject, _propagateExistence, getContainer, moveDown, moveToFirst, moveToIndex, moveToLast, moveUp, updateContent |
Methods inherited from class java.lang.Object |
---|
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Methods inherited from interface ptolemy.kernel.util.Settable |
---|
addValueListener, getDefaultExpression, getDisplayName, getExpression, getValueAsString, getVisibility, removeValueListener, setExpression, setVisibility, validate |
Methods inherited from interface ptolemy.kernel.util.Nameable |
---|
description, getContainer, getFullName, getName, getName, setName |
Field Detail |
---|
private java.net.URI _baseDirectory
private java.io.BufferedReader _reader
private java.io.Writer _writer
private boolean _isOutput
Constructor Detail |
---|
public FileParameter(NamedObj container, java.lang.String name) throws IllegalActionException, NameDuplicationException
container
- The container.name
- The name of this attribute.
IllegalActionException
- If the attribute is not of an
acceptable class for the container, or if the name contains a period.
NameDuplicationException
- If the name coincides with
an attribute already in the container.public FileParameter(NamedObj container, java.lang.String name, boolean isOutput) throws IllegalActionException, NameDuplicationException
container
- The container.name
- The name of this attribute.isOutput
- Whether the file is to be written to.
IllegalActionException
- If the attribute is not of an
acceptable class for the container, or if the name contains a period.
NameDuplicationException
- If the name coincides with
an attribute already in the container.Method Detail |
---|
public java.io.File asFile() throws IllegalActionException
The file need not exist for this method to succeed. Thus, this method can be used to determine whether a file with a given name exists, prior to calling openForWriting(). A typical usage looks like this:
FileParameter fileParameter; ... File file = fileParameter.asFile(); if (file.exists()) { ... Ask the user if it's OK to overwrite... ... Throw an exception if not... } // The following will overwrite an existing file. Writer writer = new PrintWriter(fileParameter.openForWriting());
If the name begins with "$CLASSPATH", then search for the file relative to the classpath. If the name begins with $CLASSPATH and a file is not found in the CLASSPATH, then the value of $PTII is substituted in a returned. If this is not done, then the File that is created would have $CLASSPATH or xxxxxxCLASSPATHxxxxxx in the file pathname, which is unlikely to be useful.
asFile
in interface FileOrURLAccessor
IllegalActionException
- If a parse error occurs
reading the file name.getBaseDirectory()
public java.net.URL asURL() throws IllegalActionException
asURL
in interface FileOrURLAccessor
IllegalActionException
- If the file cannot be read, or
if the file cannot be represented as a URL (e.g. System.in), or
the name specification cannot be parsed.public java.lang.Object clone(Workspace workspace) throws java.lang.CloneNotSupportedException
clone
in class Variable
workspace
- The workspace for the new object.
java.lang.CloneNotSupportedException
- If a derived class contains
an attribute that cannot be cloned.Object.clone()
public void close() throws IllegalActionException
close
in interface FileOrURLAccessor
IllegalActionException
- If the file or URL cannot be
closed.public java.net.URI getBaseDirectory()
getBaseDirectory
in interface FileOrURLAccessor
setBaseDirectory(URI)
,
URIAttribute.getModelURI(NamedObj)
public boolean isOutput()
public java.io.BufferedReader openForReading() throws IllegalActionException
openForReading
in interface FileOrURLAccessor
IllegalActionException
- If the file or URL cannot be
opened.getBaseDirectory()
public java.io.Writer openForWriting() throws IllegalActionException
openForWriting
in interface FileOrURLAccessor
IllegalActionException
- If the file cannot be opened
or created.getBaseDirectory()
,
asFile()
public java.io.Writer openForWriting(boolean append) throws IllegalActionException
openForWriting
in interface FileOrURLAccessor
append
- If true, then append to the file rather than
overwriting.
IllegalActionException
- If the file cannot be opened
or created.getBaseDirectory()
,
asFile()
public void setBaseDirectory(java.net.URI directory)
setBaseDirectory
in interface FileOrURLAccessor
directory
- The base directory.getBaseDirectory()
,
URIAttribute.getModelURI(NamedObj)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |