ptolemy.kernel.attributes
Class FileAttribute

java.lang.Object
  extended by ptolemy.kernel.util.NamedObj
      extended by ptolemy.kernel.util.Attribute
          extended by ptolemy.kernel.util.AbstractSettableAttribute
              extended by ptolemy.kernel.util.StringAttribute
                  extended by ptolemy.kernel.attributes.FileAttribute
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, FileOrURLAccessor, Changeable, Debuggable, DebugListener, Derivable, ModelErrorHandler, MoMLExportable, Moveable, Nameable, Settable

Deprecated. Use FileParameter instead.

public class FileAttribute
extends StringAttribute
implements FileOrURLAccessor

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. 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:

Note, however, that these file names cannot be converted to URLs using the asURL() method.

A file name can also contain the following strings that start with "$", which get substituted with the appropriate values

$CWDThe current working directoryuser.dir$HOMEThe user's home directoryuser.home$PTIIThe home directory of the Ptolemy II installationptolemy.ptII.dir$TMPDIRThe 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 string "$CLASSPATH", followed by either "/" or "\", 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).

Since:
Ptolemy II 3.0
Version:
$Id: FileAttribute.java 57040 2010-01-27 20:52:32Z cxh $
Author:
Edward A. Lee
See Also:
URIAttribute, Serialized Form
Accepted Rating:
Yellow (cxh)
Proposed Rating:
Green (eal)

Nested Class Summary
 
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
          Deprecated. The base directory to use for relative file names.
private  java.io.BufferedReader _reader
          Deprecated. The current reader for the input file.
private  java.io.Writer _writer
          Deprecated. The current writer for the output file.
 
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
FileAttribute(NamedObj container, java.lang.String name)
          Deprecated. Construct an attribute with the given name contained by the specified container.
 
Method Summary
private static java.lang.String _substituteSpecialStrings(java.lang.String string)
          Deprecated. Return a string that is the current value of this attribute with the strings "$CWD, "$HOME", "$PTII" and "$TMPDIR" replaced by their respective values.
 java.io.File asFile()
          Deprecated. Return the file as a File object.
 java.net.URL asURL()
          Deprecated. Return the file as a URL.
 java.lang.Object clone(Workspace workspace)
          Deprecated. Clone the attribute into the specified workspace.
 void close()
          Deprecated. Close the file.
 java.net.URI getBaseDirectory()
          Deprecated. Return the directory to use as the base for relative file or URL names.
 java.io.BufferedReader openForReading()
          Deprecated. Open the file or URL for reading.
 java.io.Writer openForWriting()
          Deprecated. Open the file for writing.
 java.io.Writer openForWriting(boolean append)
          Deprecated. Open the file for writing or appending.
 void setBaseDirectory(java.net.URI directory)
          Deprecated. Set the directory to use as the base for relative file or URL names.
 
Methods inherited from class ptolemy.kernel.util.StringAttribute
_propagateValue, addValueListener, exportMoML, getExpression, getVisibility, removeValueListener, setExpression, setVisibility, validate
 
Methods inherited from class ptolemy.kernel.util.AbstractSettableAttribute
getDefaultExpression, getValueAsString
 
Methods inherited from class ptolemy.kernel.util.Attribute
_checkContainer, _getContainedObject, _propagateExistence, getContainer, moveDown, moveToFirst, moveToIndex, moveToLast, moveUp, setContainer, setName, updateContent
 
Methods inherited from class ptolemy.kernel.util.NamedObj
_addAttribute, _adjustOverride, _attachText, _cloneFixAttributeFields, _debug, _debug, _debug, _debug, _debug, _description, _exportMoMLContents, _getIndentPrefix, _isMoMLSuppressed, _markContentsDerived, _recordDecoratedAttributes, _removeAttribute, _splitName, _stripNumericSuffix, _validateSettables, addChangeListener, addDebugListener, attributeChanged, attributeList, attributeList, attributeTypeChanged, clone, containedObjectsIterator, deepContains, depthInHierarchy, description, description, event, executeChangeRequests, exportMoML, exportMoML, exportMoML, exportMoML, exportMoMLPlain, getAttribute, getAttribute, getAttributes, getChangeListeners, getClassName, getDecoratorAttribute, getDecoratorAttributes, getDerivedLevel, getDerivedList, getDisplayName, getElementName, getFullName, getModelErrorHandler, getName, getName, getPrototypeList, getSource, handleModelError, isDeferringChangeRequests, isOverridden, isPersistent, lazyContainedObjectsIterator, message, propagateExistence, propagateValue, propagateValues, removeChangeListener, removeDebugListener, requestChange, setClassName, setDeferringChangeRequests, setDerivedLevel, setDisplayName, setModelErrorHandler, setPersistent, setSource, sortContainedObjects, toplevel, toString, uniqueName, validateSettables, workspace
 
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

_baseDirectory

private java.net.URI _baseDirectory
Deprecated. 
The base directory to use for relative file names.


_reader

private java.io.BufferedReader _reader
Deprecated. 
The current reader for the input file.


_writer

private java.io.Writer _writer
Deprecated. 
The current writer for the output file.

Constructor Detail

FileAttribute

public FileAttribute(NamedObj container,
                     java.lang.String name)
              throws IllegalActionException,
                     NameDuplicationException
Deprecated. 
Construct an attribute with the given name contained by the specified container. The container argument must not be null, or a NullPointerException will be thrown. This attribute will use the workspace of the container for synchronization and version counts. If the name argument is null, then the name is set to the empty string. Increment the version of the workspace.

Parameters:
container - The container.
name - The name of this attribute.
Throws:
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

asFile

public java.io.File asFile()
                    throws IllegalActionException
Deprecated. 
Return the file as a File object. This method first attempts to directly use the file name to construct the File. If the resulting File is not absolute, then it attempts to resolve it relative to the base directory returned by getBaseDirectory(). If there is no such base URI, then it simply returns the relative File object.

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:

      FileAttribute fileAttribute;
      ...
      File file = fileAttribute.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(fileAttribute.openForWriting());
  

Specified by:
asFile in interface FileOrURLAccessor
Returns:
A File, or null if no file name has been specified.
Throws:
IllegalActionException - If a parse error occurs reading the file name.
See Also:
getBaseDirectory()

asURL

public java.net.URL asURL()
                   throws IllegalActionException
Deprecated. 
Return the file as a URL. If the file name is relative, then it is interpreted as being relative to the directory returned by getBaseDirectory(). If the name begins with "$CLASSPATH", then search for the file relative to the classpath. If no file is found, then it throws an exception.

Specified by:
asURL in interface FileOrURLAccessor
Returns:
A URL, or null if no file name or URL has been specified.
Throws:
IllegalActionException - If the file cannot be read, or if the file cannot be represented as a URL (e.g. System.in).

clone

public java.lang.Object clone(Workspace workspace)
                       throws java.lang.CloneNotSupportedException
Deprecated. 
Clone the attribute into the specified workspace. The resulting object has no base directory name nor any reference to any open stream.

Overrides:
clone in class Attribute
Parameters:
workspace - The workspace for the cloned object.
Returns:
A new attribute.
Throws:
java.lang.CloneNotSupportedException - If a derived class contains an attribute that cannot be cloned.
See Also:
NamedObj.exportMoML(Writer, int, String), NamedObj.setDeferringChangeRequests(boolean)

close

public void close()
           throws IllegalActionException
Deprecated. 
Close the file. If it has not been opened using openForReading() or openForWriting(), then do nothing. Also, if the file is System.in or System.out, then do not close it (it does not make sense to close these files).

Specified by:
close in interface FileOrURLAccessor
Throws:
IllegalActionException - If the file or URL cannot be closed.

getBaseDirectory

public java.net.URI getBaseDirectory()
Deprecated. 
Return the directory to use as the base for relative file or URL names. If setBaseDirectory() has been called, then that directory is returned. Otherwise, the directory containing the file returned by URIAttribute.getModelURI() is returned, which is the URI of the first container above this attribute in the hierarchy that has a URIAttribute, or null if there none.

Specified by:
getBaseDirectory in interface FileOrURLAccessor
Returns:
A directory name, or null if there is none.
See Also:
setBaseDirectory(URI), URIAttribute.getModelURI(NamedObj)

openForReading

public java.io.BufferedReader openForReading()
                                      throws IllegalActionException
Deprecated. 
Open the file or URL for reading. If the name begins with "$CLASSPATH", then search for the file relative to the classpath. If the name is relative, then it is relative to the directory returned by getBaseDirectory().

Specified by:
openForReading in interface FileOrURLAccessor
Returns:
A buffered reader.
Throws:
IllegalActionException - If the file or URL cannot be opened.
See Also:
getBaseDirectory()

openForWriting

public java.io.Writer openForWriting()
                              throws IllegalActionException
Deprecated. 
Open the file for writing. If the file does not exist, then create it. If the file name is not absolute, the it is assumed to be relative to the base directory returned by getBaseDirectory(). If permitted, this method will return a Writer that will simply overwrite the contents of the file. It is up to the user of this method to check whether this is OK (by first calling asFile() and calling exists() on the returned value).

Specified by:
openForWriting in interface FileOrURLAccessor
Returns:
A writer, or null if no file name has been specified.
Throws:
IllegalActionException - If the file cannot be opened or created.
See Also:
getBaseDirectory(), asFile()

openForWriting

public java.io.Writer openForWriting(boolean append)
                              throws IllegalActionException
Deprecated. 
Open the file for writing or appending. If the file does not exist, then create it. If the file name is not absolute, the it is assumed to be relative to the base directory returned by getBaseDirectory(). If permitted, this method will return a Writer that will simply overwrite the contents of the file. It is up to the user of this method to check whether this is OK (by first calling asFile() and calling exists() on the returned value).

Specified by:
openForWriting in interface FileOrURLAccessor
Parameters:
append - If true, then append to the file rather than overwriting.
Returns:
A writer, or null if no file name has been specified.
Throws:
IllegalActionException - If the file cannot be opened or created.
See Also:
getBaseDirectory(), asFile()

setBaseDirectory

public void setBaseDirectory(java.net.URI directory)
Deprecated. 
Set the directory to use as the base for relative file or URL names. If this is not called, then the default is the directory containing the file returned by URIAttribute.getModelURI().

Specified by:
setBaseDirectory in interface FileOrURLAccessor
Parameters:
directory - The base directory.
See Also:
getBaseDirectory(), URIAttribute.getModelURI(NamedObj)

_substituteSpecialStrings

private static java.lang.String _substituteSpecialStrings(java.lang.String string)
Deprecated. 
Return a string that is the current value of this attribute with the strings "$CWD, "$HOME", "$PTII" and "$TMPDIR" replaced by their respective values.

Parameters:
string - The string in which to do the substitution.
Returns:
A new string.