[kepler-dev] [kepler-cvs] kepler/src/exp/ptolemy/utilFileUtilities.java
Christopher Brooks
cxh at eecs.berkeley.edu
Mon Apr 25 18:37:29 PDT 2005
Hi Efrat,
I folded in your changes.
You wrote:
> The first modification was due to a URI authentication exception when
> trying to create a file object from a URI on the remote side.
That makes sense, the modified nameToFile() is below:
/** Given a file name or URL, return the file. 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 specified base directory, if there is one.
* If there is no such base URI, then it simply returns the
* relative File object.
* <p>
* 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(), for example.
* @param name The file name or URL.
* @param base The base for relative URLs.
* @return A File, or null if the filename argument is null or
* an empty string.
*/
public static File nameToFile(String name, URI base) {
if ((name == null) || name.trim().equals("")) {
return null;
}
File file = new File(name);
if (!file.isAbsolute()) {
// Try to resolve the base directory.
if (base != null) {
URI newURI = base.resolve(name);
String urlString = newURI.toString();
if (urlString.indexOf("file:/") != -1) {
urlString = urlString.substring(6); //Removing 'file:/'
}
file = new File(StringUtilities.substitute(urlString,
"%20", " "));
}
}
return file;
}
> The second modification was due to the file protocol requirements
> to use 3 slashes, 'file:///' on the remote side, although it
> would be probably be a good idea to also make sure first that the
> url string actually represents the file protocol.
I'm not so sure about this change to nameToURL().
In the code below, I marked your change with /* Efrat */.
--start--
/** Given a file or URL name, return as a URL. If the file name
* is relative, then it is interpreted as being relative to the
* specified base directory. If the name begins with
* "xxxxxxCLASSPATHxxxxxx" or "$CLASSPATH" then search for the
* file relative to the classpath.
*
* <p>Note that "xxxxxxCLASSPATHxxxxxx" the value of the globally
* defined constant $CLASSPATH available in the Ptolemy II
* expression language. If no file is found, then throw an
* exception.
*
* @param name The name of a file or URL.
* @param baseDirectory The base directory for relative file names,
* or null to specify none.
* @param classLoader The class loader to use to locate system
* resources, or null to use the system class loader.
* @return A URL, or null if no file name or URL has been specified.
* @exception IOException 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.
* @exception MalformedURLException If the
*/
public static URL nameToURL(String name, URI baseDirectory,
ClassLoader classLoader) throws IOException {
if ((name == null) || name.trim().equals("")) {
return null;
}
// If the name begins with "$CLASSPATH", then attempt to
// open the file relative to the classpath.
// NOTE: Use the dummy variable constant set up in the constructor.
if (name.startsWith(_CLASSPATH_VALUE) || name.startsWith("$CLASSPATH")) {
// Try relative to classpath.
String classpathKey;
if (name.startsWith(_CLASSPATH_VALUE)) {
classpathKey = _CLASSPATH_VALUE;
} else {
classpathKey = "$CLASSPATH";
}
String trimmedName = name.substring(classpathKey.length() + 1);
if (classLoader == null) {
try {
// WebStart: We might be in the Swing Event thread, so
// Thread.currentThread().getContextClassLoader()
// .getResource(entry) probably will not work so we
// use a marker class.
Class refClass = Class.forName(
"ptolemy.kernel.util.NamedObj");
classLoader = refClass.getClassLoader();
} catch (Exception ex) {
// IOException constructor does not take a cause
IOException ioException = new IOException(
"Cannot find file '" + trimmedName
+ "' in classpath");
ioException.initCause(ex);
throw ioException;
}
}
// Use Thread.currentThread()... for Web Start.
URL result = classLoader.getResource(trimmedName);
if (result == null) {
new IOException("Cannot find file '" + trimmedName
+ "' in classpath");
}
return result;
}
File file = new File(name);
if (file.isAbsolute()) {
if (!file.canRead()) {
// FIXME: This is a hack.
// Expanding the configuration with Ptolemy II installed
// in a directory with spaces in the name fails on
// JAIImageReader because PtolemyII.jpg is passed in
// to this method as C:\Program%20Files\Ptolemy\...
file = new File(StringUtilities.substitute(name, "%20", " "));
if (!file.canRead()) {
throw new IOException("Cannot read file '" + name
+ "' or '"
+ StringUtilities.substitute(name, "%20", " ") + "'");
}
}
return file.toURL();
} else {
// Try relative to the base directory.
if (baseDirectory != null) {
// Try to resolve the URI.
URI newURI;
try {
newURI = baseDirectory.resolve(name);
} catch (Exception ex) {
// FIXME: Another hack
// This time, if we try to open some of the JAI
// demos that have actors that have defaults FileParameters
// like "$PTII/doc/img/PtolemyII.jpg", then resolve()
// bombs.
String name2 = StringUtilities.substitute(name, "%20", " ");
try {
newURI = baseDirectory.resolve(name2);
name = name2;
} catch (Exception ex2) {
IOException io = new IOException(
"Problem with URI format in '" + name + "'. "
+ "and '" + name2 + "'"
+ "This can happen if the file name "
+ " is not absolute"
+ " and is not present relative to the directory"
+ " in which the specified model was read"
+ " (which was '" + baseDirectory + "')");
io.initCause(ex2);
throw io;
}
}
String urlString = newURI.toString();
try {
/* Efrat */
// Adding another '/' for remote execution.
urlString = urlString.substring(0,6) + "/"
+ urlString.substring(6);
return new URL(urlString);
} catch (Exception ex3) {
IOException io = new IOException(
"Problem with URI format in '" + urlString + "'. "
+ "This can happen if the '" + urlString
+ "' is not absolute"
+ " and is not present relative to the directory"
+ " in which the specified model was read"
+ " (which was '" + baseDirectory + "')");
io.initCause(ex3);
throw io;
}
}
// As a last resort, try an absolute URL.
return new URL(name);
}
}
--end--
The problem is that if I call this method with something like
nameToURL("doesnotexist", new URI("."), null);
then I get
java.io.IOException: Problem with URI format in 'doesno/texist'. This can happen if the 'doesno/texist' is not absolute and is not present relative to the directory in which the specified model was read (which was '.')
>From the comment, I would expect to get an exception because the
file cannot be found.
It seems like the code does not work as advertised.
nameToURL("file://./doesnotexist", null, null);
does not thrown an exception either.
BTW - nameToURL is called by actor/parameters/FilePortParameter.java,
data/expr/FileParameter.java and vergil/basiclGraphController.
_Christopher
--------
Hi Christopher,
I made these change to allow remote execution of a workflow from within =
a web service.=20
The first modification was due to a URI authentication exception when =
trying to create a file object from a URI on the remote side. The second =
modification was due to the file protocol requirements to use 3 slashes, =
'file:///' on the remote side, although it would be probably be a good =
idea to also make sure first that the url string actually represents the =
file protocol.=20
I tested these changes with several io actors. Please let me know what =
you think, and It would be great if these changes could be checked to =
the CVS tree.
BTW, as of the beginning of this week, we started synchronyzing with the =
Ptolemy CVS tree. So, from now on any changes to ptolemy files will be =
directly on most recent versions.
Thanks a lot,
Efrat
----- Original Message -----=20
From: Christopher Brooks=20
To: Efrat Jaeger=20
Cc: kepler-dev at ecoinformatics.org=20
Sent: Friday, April 22, 2005 4:41 PM
Subject: Re: [kepler-dev] [kepler-cvs] =
kepler/src/exp/ptolemy/utilFileUtilities.java
Hi Efrat,
I saw your checkin of FileUtilities.java into the Kepler tree.
Is there a specific modification you would like to see made to the
Ptolemy version? =20
It looks like you are adding modify nameToFile() and removing
file:/ in one location and then adding a second slash in another
location? I'm not totally sure what version of FileUtilities
you based your changes on.
The reason I ask is because I think the version of FileUtilities.java
that you checked in does not have my latest changes. Also, if you
start keeping a private copy of the file, then you won't see further=20
changes. This sort of thing ends up causing support problems.=20
If you would like, I could make changes to FileUtilties.java for you,
or else give you read/write cvs permission to the ptII tree if you do
not have it.
I think your changes are:
***************
*** 160,168 ****
// Try to resolve the base directory.
if (base !=3D null) {
URI newURI =3D base.resolve(name);
! String urlStr =3D newURI.toString();
! urlStr =3D urlStr.substring(6); //Removing =
'file:/'
! file =3D new =
File(StringUtilities.substitute(urlStr, "%20", " "));
}
}
=20
--- 159,165 ----
// Try to resolve the base directory.
if (base !=3D null) {
URI newURI =3D base.resolve(name);
! file =3D new File(newURI);
}
}
=20
***************
*** 298,306 ****
}
=20
try {
! String urlStr =3D newURI.toString();
! urlStr =3D urlStr.substring(0,6) + "/" + =
urlStr.substring(6); // adding another '/' for remote execution.
! return new URL(urlStr);
} catch (IllegalArgumentException ex3) {
IOException io =3D new IOException(
"Problem with URI format in '" + name =
+ "'. "
--- 295,301 ----
}
=20
try {
! return newURI.toURL();
} catch (IllegalArgumentException ex3) {
IOException io =3D new IOException(
"Problem with URI format in '" + name + =
"'. "
cxh at maury 167%=20
There is this larger issue of when should the Kepler source tree
start using the latest ptII devel tree. Since we are moving towards
a release, now might be a good time to start synchronizing.
Anyway, just curious about this.
_Christopher
--------
jaeger 05/04/22 15:52:04
=20
Added: src/exp/ptolemy/util FileUtilities.java
Log:
modified file paths to match both local and remote workflow =
execcution
=20
Revision Changes Path
1.1 =
kepler/src/exp/ptolemy/util/FileUtilities.java
=20
Index: FileUtilities.java
=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
/* Utilities used to manipulate files
=20
Copyright (c) 2004-2005 The Regents of the University of =
California.
All rights reserved.
Permission is hereby granted, without written agreement and =
without
license or royalty fees, to use, copy, modify, and distribute =
this
software and its documentation for any purpose, provided that =
the above
copyright notice and the following two paragraphs appear in all =
copies
of this software.
=20
IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY =
PARTY
FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL =
DAMAGES
ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, =
EVEN IF
THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY =
OF
SUCH DAMAGE.
=20
THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY =
WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE =
SOFTWARE
PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, =
UPDATES,
ENHANCEMENTS, OR MODIFICATIONS.
=20
PT_COPYRIGHT_VERSION_2
COPYRIGHTENDKEY
=20
*/
package ptolemy.util;
=20
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLDecoder;
=20
import ptolemy.kernel.util.IllegalActionException;
=20
=20
=
/////////////////////////////////////////////////////////////////////////=
/
//// FileUtilities
=20
/**
A collection of utilities for manipulating files
These utilities do not depend on any other ptolemy.* =
packages.
=20
@author Christopher Hylands Brooks
@version $Id: FileUtilities.java,v 1.1 2005/04/22 22:52:04 =
jaeger Exp=20
$
@since Ptolemy II 4.0
@Pt.ProposedRating Yellow (eal)
@Pt.AcceptedRating Red (cxh)
*/
public class FileUtilities {
/** Instances of this class cannot be created.
*/
private FileUtilities() {
}
=20
=
///////////////////////////////////////////////////////////////////
//// public methods =
////
=20
/** Copy sourceURL to destinationFile without doing any byte =
conversi
on.
* @param sourceURL The source URL
* @param destinationFile The destination File.
* @return true if the file was copied, false if the file =
was not
* copied because the sourceURL and the destinationFile =
refer to the
* same file.
* @exception IOException If the source file is not the =
same as the
* destination file and the destination file does not =
exist.
*/
public static boolean binaryCopyURLToFile(URL sourceURL,
File destinationFile) throws IOException {
URL destinationURL =3D =
destinationFile.getCanonicalFile().toURL();
=20
if (sourceURL.sameFile(destinationURL)) {
return false;
}
=20
// If sourceURL is of the form file:./foo, then we need =
to try ag
ain.
File sourceFile =3D new File(sourceURL.getFile());
=20
if =
(sourceFile.getCanonicalFile().toURL().sameFile(destinationURL
)) {
return false;
}
=20
BufferedInputStream input =3D null;
try {
input =3D new =
BufferedInputStream(sourceURL.openStream());
BufferedOutputStream output =3D null;
try {
output =3D new BufferedOutputStream(
new FileOutputStream(destinationFile));
=20
int c;
=20
while ((c =3D input.read()) !=3D -1) {
output.write(c);
}
} finally {
if (output !=3D null) {
try {
output.close();
} catch (Throwable throwable) {
System.out.println("Ignoring failure to
close stream "
+ "on " + destinationFi
le);
throwable.printStackTrace();
}
} =20
} =20
} finally {
if (input !=3D null) {
try {
input.close();
} catch (Throwable throwable) {
System.out.println("Ignoring failure to close s
tream "
+ "on " + sourceURL);
throwable.printStackTrace();
}
}
=20
=20
}
=20
return true;
}
=20
/** Given a file name or URL, return the file. This method =
first atte
mpts
* 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 specified base directory, if there is =
one.
* If there is no such base URI, then it simply returns the
* relative File object.
* <p>
* 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(), for =
example.
* @param name The file name or URL.
* @param base The base for relative URLs.
* @return A File, or null if the filename argument is null =
or
* an empty string.
* @exception IllegalActionException If a parse error =
occurs
* reading the file name.
*/
public static File nameToFile(String name, URI base) {
if ((name =3D=3D null) || name.trim().equals("")) {
return null;
}
=20
File file =3D new File(name);
=20
if (!file.isAbsolute()) {
// Try to resolve the base directory.
if (base !=3D null) {
URI newURI =3D base.resolve(name);
String urlStr =3D newURI.toString();
urlStr =3D urlStr.substring(6); //Removing =
'file:/'
file =3D new =
File(StringUtilities.substitute(urlStr, "%20",
" "));
}
}
=20
return file;
}
=20
/** Given a file or URL name, return as a URL. If the file =
name
* is relative, then it is interpreted as being relative to =
the
* specified base directory. If the name begins with
* "xxxxxxCLASSPATHxxxxxx" or "$CLASSPATH" then search for =
the
* file relative to the classpath.
*
* <p>Note that "xxxxxxCLASSPATHxxxxxx" the value of the =
globally
* defined constant $CLASSPATH available in the Ptolemy II
* expression language. If no file is found, then throw an
* exception.
*
* @param name The name of a file or URL.
* @param baseDirectory The base directory for relative =
file names,
* or null to specify none.
* @param classLoader The class loader to use to locate =
system
* resources, or null to use the system class loader.
* @return A URL, or null if no file name or URL has been =
specified.
* @exception IOException 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.
* @exception MalformedURLException If the
*/
public static URL nameToURL(String name, URI baseDirectory,
ClassLoader classLoader) throws IOException {
if ((name =3D=3D null) || name.trim().equals("")) {
return null;
}
=20
// If the name begins with "$CLASSPATH", then attempt to
// open the file relative to the classpath.
// NOTE: Use the dummy variable constant set up in the =
constructo
r.
if (name.startsWith(_CLASSPATH_VALUE)
|| name.startsWith("$CLASSPATH") ) {
// Try relative to classpath.
=20
String classpathKey;
if (name.startsWith(_CLASSPATH_VALUE)) {
classpathKey =3D _CLASSPATH_VALUE;
} else {=20
classpathKey =3D "$CLASSPATH";
}
=20
String trimmedName=3D =
name.substring(classpathKey.length() + 1)
;
=20
if (classLoader =3D=3D null) {
try {
// WebStart: We might be in the Swing Event =
thread, s
o
// =
Thread.currentThread().getContextClassLoader()
// .getResource(entry) probably will not =
work so we
// use a marker class.
Class refClass =3D Class.forName(
"ptolemy.kernel.util.NamedObj");
classLoader =3D refClass.getClassLoader();
} catch (Exception ex) {
// IOException constructor does not take a =
cause
IOException ioException =3D new IOException(
"Cannot find file '" + trimmedName
+ "' in classpath");
ioException.initCause(ex);
throw ioException;
}
}
=20
// Use Thread.currentThread()... for Web Start.
URL result =3D classLoader.getResource(trimmedName);
=20
if (result =3D=3D null) {
new IOException("Cannot find file '" + =
trimmedName
+ "' in classpath");
}
=20
return result;
}
=20
File file =3D new File(name);
=20
if (file.isAbsolute()) {
if (!file.canRead()) {
// FIXME: This is a hack.
// Expanding the configuration with Ptolemy II =
installed
// in a directory with spaces in the name fails =
on
// JAIImageReader because PtolemyII.jpg is =
passed in
// to this method as =
C:\Program%20Files\Ptolemy\...
file =3D new =
File(StringUtilities.substitute(name, "%20", "
"));
=20
if (!file.canRead()) {
throw new IOException("Cannot read file '" + =
name
+ "' or '"
+ StringUtilities.substitute(name, =
"%20", " "
)
+ "'");
}
}
=20
return file.toURL();
} else {
// Try relative to the base directory.
if (baseDirectory !=3D null) {
// Try to resolve the URI.
URI newURI;
=20
try {
newURI =3D baseDirectory.resolve(name);
} catch (IllegalArgumentException ex) {
// FIXME: Another hack
// This time, if we try to open some of the =
JAI
// demos that have actors that have defaults =
FilePara
meters
// like "$PTII/doc/img/PtolemyII.jpg", then =
resolve()
// bombs.
String name2 =3D =
StringUtilities.substitute(name,
"%20", " ");
=20
try {
newURI =3D baseDirectory.resolve(name2);
name =3D name2;
} catch (IllegalArgumentException ex2) {
IOException io =3D new IOException(
"Problem with URI format in '" + =
name + "
'. "
+ "and '" + name2 + "'"
+ "This can happen if the file =
name "
+ " is not absolute"
+ " and is not present relative =
to the di
rectory"
+ " in which the specified model =
was read
"
+ " (which was '" + =
baseDirectory + "')")
;
io.initCause(ex2);
throw io;
}
}
=20
try {
String urlStr =3D newURI.toString();
urlStr =3D urlStr.substring(0,6) + "/" + =
urlStr.substring
(6); // adding another '/' for remote execution.
return new URL(urlStr);
} catch (IllegalArgumentException ex3) {
IOException io =3D new IOException(
"Problem with URI format in '" + =
name + "'. "
+ "This can happen if the '" + name
+ "' is not absolute"
+ " and is not present relative to =
the direct
ory"
+ " in which the specified model was =
read"
+ " (which was '" + baseDirectory + =
"')");
io.initCause(ex3);
throw io;
}
}
=20
// As a last resort, try an absolute URL.
return new URL(name);
}
}
=20
/** Open the specified file for reading. If the
* specified name is "System.in", then a reader from =
standard
* in is returned. If the name begins with
* "$CLASSPATH", then search for the file relative to the =
classpath.
* If the file name is not absolute, the it is assumed
* to be relative to the specified base directory.
* @see #nameToFile(String, URI)
* @param name File name.
* @param base The base URI for relative references.
* @param classLoader The class loader to use for opening =
files
* relative to the classpath.
* @return A buffered reader.
* @exception IOException If the file cannot be opened.
*/
public static BufferedReader openForReading(String name, URI =
base,
ClassLoader classLoader) throws IOException {
if (name.trim().equals("System.in")) {
if (STD_IN =3D=3D null) {
STD_IN =3D new BufferedReader(new =
InputStreamReader(System.
in));
}
=20
return STD_IN;
}
=20
// Not standard input. Try URL mechanism.
URL url =3D nameToURL(name, base, classLoader);
=20
if (url =3D=3D null) {
throw new IOException("No file name has been =
specified.");
}
=20
return new BufferedReader(new =
InputStreamReader(url.openStream())
);
}
=20
/** Open the specified file for writing or appending. If the
* specified name is "System.out", then a writer to =
standard
* out is returned. 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 specified base directory.
* 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
* {@link #nameToFile(String, URI)}
* and calling exists() on the returned value).
* @param name File name.
* @param base The base URI for relative references.
* @param append If true, then append to the file rather =
than
* overwriting.
* @return A writer, or null if no file name is specified.
* @exception IOException If the file cannot be opened
* or created.
*/
public static Writer openForWriting(String name, URI base, =
boolean ap
pend)
throws IOException {
if (name =3D=3D null) {
return null; =20
}
if (name.trim().equals("System.out")) {
if (STD_OUT =3D=3D null) {
STD_OUT =3D new PrintWriter(System.out);
}
=20
return STD_OUT;
}
=20
if (name.trim().equals("")) {
return null;
}
=20
File file =3D nameToFile(name, base);
return new FileWriter(file, append);
}
=20
=
///////////////////////////////////////////////////////////////////
//// public members =
////
=20
/** Standard in as a reader, which will be non-null
* only after a call to openForReading("System.in").
*/
public static BufferedReader STD_IN =3D null;
=20
/** Standard out as a writer, which will be non-null
* only after a call to openForWriting("System.out").
*/
public static PrintWriter STD_OUT =3D null;
=20
=
///////////////////////////////////////////////////////////////////
//// private members =
////
=20
/** Tag value used by this class and registered as a parser
* constant for the identifier "CLASSPATH" to indicate =
searching
* in the classpath. This is a hack, but it deals with the =
fact
* that Java is not symmetric in how it deals with getting =
files
* from the classpath (using getResource) and getting files =
from
* the file system.
*/
private static String _CLASSPATH_VALUE =3D =
"xxxxxxCLASSPATHxxxxxx";
}
=20
=20
=20
_______________________________________________
Kepler-cvs mailing list
Kepler-cvs at ecoinformatics.org
=
http://mercury.nceas.ucsb.edu/ecoinformatics/mailman/listinfo/kepler-cvs
--------
_______________________________________________
Kepler-dev mailing list
Kepler-dev at ecoinformatics.org
=
http://mercury.nceas.ucsb.edu/ecoinformatics/mailman/listinfo/kepler-dev
------=_NextPart_000_00B5_01C5479A.0DB664C0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2800.1458" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Hi Christopher,</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>I made these change to allow remote =
execution of=20
a workflow from within a web service. </FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>The first modification was due to a URI =
authentication exception when trying to create a file object from a URI =
on the=20
remote side. The second modification was due to the file protocol =
requirements=20
to use 3 slashes, 'file:///' on the remote side, although it =
would be=20
probably be a good idea to also make sure first that the url string=20
actually represents the file protocol. </FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>I tested these changes with several io =
actors.=20
Please let me know what you think, and It would be great if these =
changes could=20
be checked to the CVS tree.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>BTW, as of the beginning of this week,=20
we started synchronyzing with the Ptolemy CVS tree. So, from now on =
any=20
changes to ptolemy files will be directly on most recent =
versions.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Thanks a lot,</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Efrat</FONT></DIV>
<BLOCKQUOTE=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV style=3D"FONT: 10pt arial">----- Original Message ----- </DIV>
<DIV=20
style=3D"BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: =
black"><B>From:</B>=20
<A title=3Dcxh at eecs.berkeley.edu =
href=3D"mailto:cxh at eecs.berkeley.edu">Christopher=20
Brooks</A> </DIV>
<DIV style=3D"FONT: 10pt arial"><B>To:</B> <A =
title=3Djaeger at ecoinformatics.org=20
href=3D"mailto:jaeger at ecoinformatics.org">Efrat Jaeger</A> </DIV>
<DIV style=3D"FONT: 10pt arial"><B>Cc:</B> <A=20
title=3Dkepler-dev at ecoinformatics.org=20
=
href=3D"mailto:kepler-dev at ecoinformatics.org">kepler-dev at ecoinformatics.o=
rg</A>=20
</DIV>
<DIV style=3D"FONT: 10pt arial"><B>Sent:</B> Friday, April 22, 2005 =
4:41=20
PM</DIV>
<DIV style=3D"FONT: 10pt arial"><B>Subject:</B> Re: [kepler-dev] =
[kepler-cvs]=20
kepler/src/exp/ptolemy/utilFileUtilities.java</DIV>
<DIV><FONT face=3DArial size=3D2></FONT><FONT face=3DArial=20
size=3D2></FONT><BR></DIV>Hi Efrat,<BR><BR>I saw your checkin of=20
FileUtilities.java into the Kepler tree.<BR><BR>Is there a specific=20
modification you would like to see made to the<BR>Ptolemy =
version? =20
<BR><BR>It looks like you are adding modify nameToFile() and=20
removing<BR>file:/ in one location and then adding a second slash in=20
another<BR>location? I'm not totally sure what =
version of=20
FileUtilities<BR>you based your changes on.<BR><BR>The reason I ask is =
because=20
I think the version of FileUtilities.java<BR>that you checked in does =
not have=20
my latest changes. Also, if you<BR>start keeping a private copy =
of the=20
file, then you won't see further <BR>changes. This sort of thing =
ends up=20
causing support problems. <BR><BR>If you would like, I could make =
changes to=20
FileUtilties.java for you,<BR>or else give you read/write cvs =
permission to=20
the ptII tree if you do<BR>not have it.<BR><BR>I think your changes=20
are:<BR><BR>***************<BR>*** 160,168=20
=
****<BR>  =
; =20
// Try to resolve the base=20
=
directory.<BR>  =
; =20
if (base !=3D null)=20
=
{<BR> &n=
bsp; =20
URI newURI =3D=20
=
base.resolve(name);<BR>! &=
nbsp; =20
String urlStr =3D=20
=
newURI.toString();<BR>! &n=
bsp; =20
urlStr =3D urlStr.substring(6); //Removing=20
=
'file:/'<BR>! =
=20
file =3D new File(StringUtilities.substitute(urlStr, "%20", "=20
=
"));<BR>  =
; =20
=
}<BR> =20
}<BR> <BR>--- 159,165=20
=
----<BR>  =
; =20
// Try to resolve the base=20
=
directory.<BR>  =
; =20
if (base !=3D null)=20
=
{<BR> &n=
bsp; =20
URI newURI =3D=20
=
base.resolve(name);<BR>! &=
nbsp; =20
file =3D new=20
=
File(newURI);<BR> &n=
bsp; =20
}<BR> =
}<BR> =20
<BR>***************<BR>*** 298,306=20
=
****<BR>  =
; =20
}<BR> =20
=
<BR> &nb=
sp; =20
try=20
=
{<BR>! &=
nbsp; =20
String urlStr =3D=20
=
newURI.toString();<BR>! &n=
bsp; =20
urlStr =3D urlStr.substring(0,6) + "/" + urlStr.substring(6); // =
adding another=20
'/' for remote=20
=
execution.<BR>! &nbs=
p;  =
;=20
return new=20
=
URL(urlStr);<BR> &nb=
sp; =20
} catch (IllegalArgumentException ex3)=20
=
{<BR> &n=
bsp; =20
IOException io =3D new=20
=
IOException(<BR> &nb=
sp; &nbs=
p; =20
"Problem with URI format in '" + name + "'. "<BR>--- 295,301=20
=
----<BR>  =
; =20
}<BR> =20
=
<BR> &nb=
sp; =20
try=20
=
{<BR>! &=
nbsp; =20
return=20
=
newURI.toURL();<BR> =
=20
} catch (IllegalArgumentException ex3)=20
=
{<BR> &n=
bsp; =20
IOException io =3D new=20
=
IOException(<BR> &nb=
sp; &nbs=
p; =20
"Problem with URI format in '" + name + "'. "<BR><A=20
href=3D"mailto:cxh at maury">cxh at maury</A> 167% <BR><BR><BR>There is this =
larger=20
issue of when should the Kepler source tree<BR>start using the latest =
ptII=20
devel tree. Since we are moving towards<BR>a release, now might =
be a=20
good time to start synchronizing.<BR><BR>Anyway, just curious about=20
this.<BR><BR>_Christopher<BR><BR>--------<BR><BR> =20
jaeger 05/04/22 =
15:52:04<BR> =20
<BR> =
Added: =20
src/exp/ptolemy/util =
FileUtilities.java<BR> =20
Log:<BR> modified file paths to match =
both local=20
and remote workflow execcution<BR> =20
<BR> Revision =
Changes =20
Path<BR> =20
=
1.1 &nbs=
p; =20
=
kepler/src/exp/ptolemy/util/FileUtilities.java<BR>  =
; =20
<BR> Index:=20
FileUtilities.java<BR> =20
=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D<BR> =
=20
/* Utilities used to manipulate =
files<BR> =20
<BR> Copyright (c) 2004-2005 The Regents =
of the=20
University of California.<BR> All rights =
reserved.<BR> Permission is hereby =
granted,=20
without written agreement and =
without<BR> =20
license or royalty fees, to use, copy, modify, and distribute=20
this<BR> software and its documentation =
for any=20
purpose, provided that the above<BR> =
copyright=20
notice and the following two paragraphs appear in all=20
copies<BR> of this=20
software.<BR> =
<BR> =20
IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY=20
PARTY<BR> FOR DIRECT, INDIRECT, SPECIAL, =
INCIDENTAL, OR CONSEQUENTIAL DAMAGES<BR> =
ARISING=20
OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN=20
IF<BR> THE UNIVERSITY OF CALIFORNIA HAS =
BEEN=20
ADVISED OF THE POSSIBILITY OF<BR> SUCH=20
DAMAGE.<BR> =
<BR> =20
THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY=20
WARRANTIES,<BR> INCLUDING, BUT NOT =
LIMITED TO,=20
THE IMPLIED WARRANTIES OF<BR> =
MERCHANTABILITY=20
AND FITNESS FOR A PARTICULAR PURPOSE. THE=20
SOFTWARE<BR> PROVIDED HEREUNDER IS ON AN =
"AS IS"=20
BASIS, AND THE UNIVERSITY OF<BR> =
CALIFORNIA HAS=20
NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,=20
UPDATES,<BR> ENHANCEMENTS, OR=20
MODIFICATIONS.<BR> =20
<BR> =20
PT_COPYRIGHT_VERSION_2<BR> =20
COPYRIGHTENDKEY<BR> =20
<BR> =
*/<BR> =20
package ptolemy.util;<BR> =20
<BR> import=20
java.io.BufferedInputStream;<BR> import=20
java.io.BufferedOutputStream;<BR> import =
java.io.BufferedReader;<BR> import=20
java.io.File;<BR> import=20
java.io.FileOutputStream;<BR> import=20
java.io.FileWriter;<BR> import=20
java.io.IOException;<BR> import=20
java.io.InputStreamReader;<BR> import=20
java.io.PrintWriter;<BR> import=20
java.io.Writer;<BR> import=20
java.net.MalformedURLException;<BR> =
import=20
java.net.URI;<BR> import=20
java.net.URL;<BR> import=20
java.net.URLDecoder;<BR> =20
<BR> import=20
=
ptolemy.kernel.util.IllegalActionException;<BR> &n=
bsp;=20
<BR> <BR> =20
=
/////////////////////////////////////////////////////////////////////////=
<BR> =20
/<BR> ////=20
FileUtilities<BR> =20
<BR> =20
/**<BR> A collection =
of=20
utilities for manipulating=20
files<BR> These =
utilities do=20
not depend on any other ptolemy.* =
packages.<BR> =20
<BR> @author =
Christopher=20
Hylands Brooks<BR> =
@version=20
$Id: FileUtilities.java,v 1.1 2005/04/22 22:52:04 jaeger Exp =
<BR> =20
$<BR> @since Ptolemy =
II=20
4.0<BR> =
@Pt.ProposedRating=20
Yellow (eal)<BR> =20
@Pt.AcceptedRating Red (cxh)<BR> =20
*/<BR> public class FileUtilities=20
{<BR> /** =
Instances of=20
this class cannot be=20
=
created.<BR> =
*/<BR> private=20
FileUtilities() =
{<BR> =20
}<BR> =20
<BR> =20
=
///////////////////////////////////////////////////////////////////<BR>&n=
bsp; =20
=
//// &nb=
sp; &nbs=
p;=20
public=20
=
methods =
=20
////<BR> =20
<BR> /** Copy =
sourceURL=20
to destinationFile without doing any byte conversi<BR> =20
on.<BR> =
* =20
@param sourceURL The source=20
URL<BR> =
* =20
@param destinationFile The destination=20
File.<BR> =
* =20
@return true if the file was copied, false if the file was=20
not<BR> =
* =20
copied because the sourceURL and the destinationFile refer to=20
the<BR> =
* =20
same =
file.<BR> =20
* @exception IOException If the source file is not the same as=20
the<BR> =
* =20
destination file and the destination file does not=20
exist.<BR> =
*/<BR> public =
static=20
boolean binaryCopyURLToFile(URL=20
=
sourceURL,<BR>  =
; =20
File destinationFile) throws IOException=20
=
{<BR> &n=
bsp; =20
URL destinationURL =3D=20
=
destinationFile.getCanonicalFile().toURL();<BR> &n=
bsp;=20
=
<BR> &nb=
sp; =20
if (sourceURL.sameFile(destinationURL))=20
=
{<BR> &n=
bsp; =20
return=20
=
false;<BR> &nb=
sp; =20
}<BR> =20
=
<BR> &nb=
sp; =20
// If sourceURL is of the form file:./foo, then we need to try=20
ag<BR> =20
=
ain.<BR>  =
; =20
File sourceFile =3D new=20
File(sourceURL.getFile());<BR> =20
=
<BR> &nb=
sp; =20
if=20
=
(sourceFile.getCanonicalFile().toURL().sameFile(destinationURL<BR> &=
nbsp;=20
))=20
=
{<BR> &n=
bsp; =20
return=20
=
false;<BR> &nb=
sp; =20
}<BR> =20
=
<BR> &nb=
sp; =20
BufferedInputStream input =3D=20
=
null;<BR> &nbs=
p; =20
try=20
=
{<BR> &n=
bsp; =20
input =3D new=20
=
BufferedInputStream(sourceURL.openStream());<BR> &=
nbsp; =20
BufferedOutputStream output =3D=20
=
null;<BR> &nbs=
p; =20
try=20
=
{<BR> &n=
bsp; =20
output =3D new=20
=
BufferedOutputStream(<BR> =
=20
new=20
=
FileOutputStream(destinationFile));<BR> &nbs=
p; =20
=
<BR> &nb=
sp; =20
int=20
=
c;<BR> &=
nbsp; =20
=
<BR> &nb=
sp; =20
while ((c =3D input.read()) !=3D -1)=20
=
{<BR> &n=
bsp; =20
=
output.write(c);<BR>  =
; =20
=
}<BR> &n=
bsp; =20
} finally=20
=
{<BR> &n=
bsp; =20
if (output !=3D null)=20
=
{<BR> &n=
bsp; =20
try=20
=
{<BR> &n=
bsp; =20
=
output.close();<BR> =
=20
} catch (Throwable throwable)=20
=
{<BR> &n=
bsp; =20
System.out.println("Ignoring failure to<BR> close =
stream=20
=
"<BR> &n=
bsp; =20
+ "on " + destinationFi<BR> =20
=
le);<BR>  =
; =20
=
throwable.printStackTrace();<BR>  =
; =20
=
}<BR> &n=
bsp; =20
} =20
=
<BR> &nb=
sp; =20
} =20
=
<BR> &nb=
sp; =20
} finally=20
=
{<BR> &n=
bsp; =20
if (input !=3D null)=20
=
{<BR> &n=
bsp; =20
try=20
=
{<BR> &n=
bsp; =20
=
input.close();<BR> &=
nbsp; =20
} catch (Throwable throwable)=20
=
{<BR> &n=
bsp; =20
System.out.println("Ignoring failure to close s<BR> tream=20
=
"<BR> &n=
bsp; =20
+ "on " +=20
=
sourceURL);<BR> &nbs=
p; =20
=
throwable.printStackTrace();<BR>  =
; =20
=
}<BR> &n=
bsp; =20
=
}<BR> &n=
bsp; =20
=
<BR> &nb=
sp; =20
=
<BR> &nb=
sp; =20
=
}<BR> &n=
bsp; =20
=
<BR> &nb=
sp; =20
return true;<BR> =
}<BR> =20
<BR> /** Given a =
file=20
name or URL, return the file. This method first atte<BR> =20
mpts<BR> =
* =20
to directly use the file name to construct the File. If=20
the<BR> =
* =20
resulting File is not absolute, then it attempts to resolve=20
it<BR> =
* =20
relative to the specified base directory, if there is=20
one.<BR> =
* =20
If there is no such base URI, then it simply returns=20
the<BR> =
* =20
relative File=20
=
object.<BR> =20
* =20
=
<p><BR> =
=20
* The file need not exist for this method to succeed. =20
Thus,<BR> =
* =20
this method can be used to determine whether a file with a=20
given<BR> =
* =20
name exists, prior to calling openForWriting(), for=20
=
example.<BR> =
* @param name The file name or=20
URL.<BR> =
* =20
@param base The base for relative=20
URLs.<BR> =
* =20
@return A File, or null if the filename argument is null=20
or<BR> =20
* an empty=20
=
string.<BR> =20
* @exception IllegalActionException If a parse error=20
occurs<BR> =
* reading the file=20
name.<BR> =20
*/<BR> public =
static=20
File nameToFile(String name, URI base)=20
=
{<BR> &n=
bsp; =20
if ((name =3D=3D null) || name.trim().equals(""))=20
=
{<BR> &n=
bsp; =20
return=20
=
null;<BR> &nbs=
p; =20
}<BR> =20
=
<BR> &nb=
sp; =20
File file =3D new File(name);<BR> =20
=
<BR> &nb=
sp; =20
if (!file.isAbsolute())=20
=
{<BR> &n=
bsp; =20
// Try to resolve the base=20
=
directory.<BR>  =
; =20
if (base !=3D null)=20
=
{<BR> &n=
bsp; =20
URI newURI =3D=20
=
base.resolve(name);<BR> &n=
bsp; &nb=
sp;=20
String urlStr =3D=20
=
newURI.toString();<BR> &nb=
sp; &nbs=
p;=20
urlStr =3D urlStr.substring(6); //Removing=20
=
'file:/'<BR> &=
nbsp; =20
file =3D new File(StringUtilities.substitute(urlStr,=20
"%20",<BR> "=20
=
"));<BR>  =
; =20
=
}<BR> &n=
bsp; =20
}<BR> =20
=
<BR> &nb=
sp; =20
return file;<BR> =
}<BR> =20
<BR> /** Given a =
file or=20
URL name, return as a URL. If the file=20
name<BR> =
* =20
is relative, then it is interpreted as being relative to=20
the<BR> =
* =20
specified base directory. If the name begins=20
with<BR> =
* =20
"xxxxxxCLASSPATHxxxxxx" or "$CLASSPATH" then search for=20
the<BR> =
* =20
file relative to the=20
=
classpath.<BR>  =
;=20
*<BR> =
* =20
<p>Note that "xxxxxxCLASSPATHxxxxxx" the value of the=20
=
globally<BR> =
* defined constant $CLASSPATH available in the Ptolemy=20
II<BR> =
* =20
expression language. If no file is found, then throw=20
an<BR> =
* =20
=
exception.<BR>  =
;=20
*<BR> =
* =20
@param name The name of a file or=20
URL.<BR> =
* =20
@param baseDirectory The base directory for relative file=20
names,<BR> =
* or null to specify=20
none.<BR> =
* =20
@param classLoader The class loader to use to locate=20
system<BR> =
* resources, or null to use the system class=20
=
loader.<BR> =20
* @return A URL, or null if no file name or URL has been=20
=
specified.<BR>  =
;=20
* @exception IOException If the file cannot be read,=20
or<BR> =20
* if the file cannot be represented as a URL (e.g. =
System.in),=20
or<BR> =20
* the name specification cannot be=20
=
parsed.<BR> =20
* @exception MalformedURLException If=20
the<BR> =20
*/<BR> public =
static URL=20
nameToURL(String name, URI=20
=
baseDirectory,<BR> &=
nbsp; =20
ClassLoader classLoader) throws IOException=20
=
{<BR> &n=
bsp; =20
if ((name =3D=3D null) || name.trim().equals(""))=20
=
{<BR> &n=
bsp; =20
return=20
=
null;<BR> &nbs=
p; =20
}<BR> =20
=
<BR> &nb=
sp; =20
// If the name begins with "$CLASSPATH", then attempt=20
=
to<BR> &=
nbsp; =20
// open the file relative to the=20
=
classpath.<BR>  =
; =20
// NOTE: Use the dummy variable constant set up in the=20
constructo<BR> =20
=
r.<BR> &=
nbsp; =20
if=20
=
(name.startsWith(_CLASSPATH_VALUE)<BR>  =
; =
=20
|| name.startsWith("$CLASSPATH") )=20
=
{<BR> &n=
bsp; =20
// Try relative to classpath.<BR> =20
=
<BR> &nb=
sp; =20
String=20
=
classpathKey;<BR> &n=
bsp; =20
if (name.startsWith(_CLASSPATH_VALUE))=20
=
{<BR> &n=
bsp; =20
classpathKey =3D=20
=
_CLASSPATH_VALUE;<BR> &nbs=
p; =20
} else {=20
=
<BR> &nb=
sp; =20
classpathKey =3D=20
=
"$CLASSPATH";<BR> &n=
bsp; =20
}<BR> =20
=
<BR> &nb=
sp; =20
String trimmedName=3D name.substring(classpathKey.length() + =
1)<BR> =20
;<BR> =20
=
<BR> &nb=
sp; =20
if (classLoader =3D=3D null)=20
=
{<BR> &n=
bsp; =20
try=20
=
{<BR> &n=
bsp; &nb=
sp; =20
// WebStart: We might be in the Swing Event thread, s<BR> =20
=
o<BR> &n=
bsp; &nb=
sp; =20
//=20
=
Thread.currentThread().getContextClassLoader()<BR>  =
; =
=20
// .getResource(entry) probably will not work so=20
=
we<BR> &=
nbsp; &n=
bsp; =20
// use a marker=20
=
class.<BR> &nb=
sp; &nbs=
p; =20
Class refClass =3D=20
=
Class.forName(<BR> &=
nbsp; &n=
bsp; =20
=
"ptolemy.kernel.util.NamedObj");<BR> &=
nbsp; &n=
bsp; =20
classLoader =3D=20
=
refClass.getClassLoader();<BR> &=
nbsp; &n=
bsp; =20
} catch (Exception ex)=20
=
{<BR> &n=
bsp; &nb=
sp; =20
// IOException constructor does not take a=20
=
cause<BR> &nbs=
p;  =
; =20
IOException ioException =3D new=20
=
IOException(<BR> &nb=
sp; &nbs=
p; =20
"Cannot find file '" +=20
=
trimmedName<BR> &nbs=
p;  =
; =20
+ "' in=20
=
classpath");<BR> &nb=
sp; &nbs=
p; =20
=
ioException.initCause(ex);<BR> &=
nbsp; &n=
bsp; =20
throw=20
=
ioException;<BR> &nb=
sp; =20
=
}<BR> &n=
bsp; =20
}<BR> =20
=
<BR> &nb=
sp; =20
// Use Thread.currentThread()... for Web=20
=
Start.<BR> &nb=
sp; =20
URL result =3D=20
=
classLoader.getResource(trimmedName);<BR> =20
=
<BR> &nb=
sp; =20
if (result =3D=3D null)=20
=
{<BR> &n=
bsp; =20
new IOException("Cannot find file '" +=20
=
trimmedName<BR> &nbs=
p;  =
; =20
+ "' in=20
=
classpath");<BR> &nb=
sp; =20
}<BR> =20
=
<BR> &nb=
sp; =20
return=20
=
result;<BR> &n=
bsp; =20
}<BR> =20
=
<BR> &nb=
sp; =20
File file =3D new File(name);<BR> =20
=
<BR> &nb=
sp; =20
if (file.isAbsolute())=20
=
{<BR> &n=
bsp; =20
if (!file.canRead())=20
=
{<BR> &n=
bsp; =20
// FIXME: This is a=20
=
hack.<BR> &nbs=
p; =20
// Expanding the configuration with Ptolemy II=20
=
installed<BR> =
=20
// in a directory with spaces in the name fails=20
=
on<BR> &=
nbsp; =20
// JAIImageReader because PtolemyII.jpg is passed=20
=
in<BR> &=
nbsp; =20
// to this method as=20
=
C:\Program%20Files\Ptolemy\...<BR> &nb=
sp; &nbs=
p; =20
file =3D new File(StringUtilities.substitute(name, "%20",=20
"<BR> "));<BR> =20
=
<BR> &nb=
sp; =20
if (!file.canRead())=20
=
{<BR> &n=
bsp; &nb=
sp; =20
throw new IOException("Cannot read file '" +=20
=
name<BR>  =
; =
=20
+ "' or=20
=
'"<BR> &=
nbsp; &n=
bsp; =20
+ StringUtilities.substitute(name, "%20", " "<BR> =20
=
)<BR> &n=
bsp; &nb=
sp; =20
+=20
=
"'");<BR> &nbs=
p; =20
=
}<BR> &n=
bsp; =20
}<BR> =20
=
<BR> &nb=
sp; =20
return=20
=
file.toURL();<BR> &n=
bsp; =20
} else=20
=
{<BR> &n=
bsp; =20
// Try relative to the base=20
=
directory.<BR>  =
; =20
if (baseDirectory !=3D null)=20
=
{<BR> &n=
bsp; =20
// Try to resolve the=20
=
URI.<BR>  =
; =20
URI newURI;<BR> =20
=
<BR> &nb=
sp; =20
try=20
=
{<BR> &n=
bsp; &nb=
sp; =20
newURI =3D=20
=
baseDirectory.resolve(name);<BR>  =
; =
=20
} catch (IllegalArgumentException ex)=20
=
{<BR> &n=
bsp; &nb=
sp; =20
// FIXME: Another=20
=
hack<BR>  =
; =
=20
// This time, if we try to open some of the=20
=
JAI<BR> =
&=
nbsp; =20
// demos that have actors that have defaults FilePara<BR> =20
=
meters<BR> &nb=
sp; &nbs=
p; =20
// like "$PTII/doc/img/PtolemyII.jpg", then=20
=
resolve()<BR> =
&=
nbsp; =20
//=20
=
bombs.<BR> &nb=
sp; &nbs=
p; =20
String name2 =3D=20
=
StringUtilities.substitute(name,<BR> &=
nbsp; &n=
bsp; &nb=
sp; =20
"%20", " ");<BR> =20
=
<BR> &nb=
sp; &nbs=
p; =20
try=20
=
{<BR> &n=
bsp; &nb=
sp; =20
newURI =3D=20
=
baseDirectory.resolve(name2);<BR> &nbs=
p;  =
; =20
name =3D=20
=
name2;<BR> &nb=
sp; &nbs=
p; =20
} catch (IllegalArgumentException ex2)=20
=
{<BR> &n=
bsp; &nb=
sp; =20
IOException io =3D new=20
=
IOException(<BR> &nb=
sp; &nbs=
p;  =
; =20
"Problem with URI format in '" + name + "<BR> '.=20
=
"<BR> &n=
bsp; &nb=
sp; &nbs=
p; =20
+ "and '" + name2 +=20
=
"'"<BR> =
&=
nbsp; &n=
bsp; =20
+ "This can happen if the file name=20
=
"<BR> &n=
bsp; &nb=
sp; &nbs=
p; =20
+ " is not=20
=
absolute"<BR> =
&=
nbsp; &n=
bsp; =20
+ " and is not present relative to the di<BR> =20
=
rectory"<BR> &=
nbsp; &n=
bsp; &nb=
sp; =20
+ " in which the specified model was read<BR> =20
=
"<BR> &n=
bsp; &nb=
sp; &nbs=
p; =20
+ " (which was '" + baseDirectory + "')")<BR> =20
=
;<BR> &n=
bsp; &nb=
sp; =20
=
io.initCause(ex2);<BR> &nb=
sp; &nbs=
p; =20
throw=20
=
io;<BR> =
&=
nbsp; =20
=
}<BR> &n=
bsp; =20
}<BR> =20
=
<BR> &nb=
sp; =20
try=20
=
{<BR> &n=
bsp; =20
String urlStr =3D=20
=
newURI.toString();<BR> &nb=
sp; &nbs=
p;=20
urlStr =3D urlStr.substring(0,6) + "/" + =
urlStr.substring<BR> (6);=20
// adding another '/' for remote=20
=
execution.<BR>  =
; =
=20
return new=20
=
URL(urlStr);<BR> &nb=
sp; =20
} catch (IllegalArgumentException ex3)=20
=
{<BR> &n=
bsp; &nb=
sp; =20
IOException io =3D new=20
=
IOException(<BR> &nb=
sp; &nbs=
p; =20
"Problem with URI format in '" + name + "'.=20
=
"<BR> &n=
bsp; &nb=
sp; =20
+ "This can happen if the '" +=20
=
name<BR>  =
; =
=20
+ "' is not=20
=
absolute"<BR> =
&=
nbsp; =20
+ " and is not present relative to the direct<BR> =20
=
ory"<BR>  =
; =
=20
+ " in which the specified model was=20
=
read"<BR> &nbs=
p;  =
; =20
+ " (which was '" + baseDirectory +=20
=
"')");<BR> &nb=
sp; &nbs=
p; =20
=
io.initCause(ex3);<BR> &nb=
sp; &nbs=
p; =20
throw=20
=
io;<BR> =
=20
=
}<BR> &n=
bsp; =20
}<BR> =20
=
<BR> &nb=
sp; =20
// As a last resort, try an absolute=20
=
URL.<BR>  =
; =20
return new=20
=
URL(name);<BR>  =
; =20
}<BR> =20
}<BR> =20
<BR> /** Open =
the=20
specified file for reading. If=20
the<BR> =
* =20
specified name is "System.in", then a reader from=20
=
standard<BR> =
* in is returned. If the name begins=20
with<BR> =
* =20
"$CLASSPATH", then search for the file relative to the=20
=
classpath.<BR>  =
;=20
* If the file name is not absolute, the it is=20
=
assumed<BR> =20
* to be relative to the specified base=20
=
directory.<BR>  =
;=20
* @see #nameToFile(String,=20
URI)<BR> =
* =20
@param name File=20
name.<BR> =
* =20
@param base The base URI for relative=20
=
references.<BR> &nbs=
p;=20
* @param classLoader The class loader to use for opening=20
files<BR> =20
* relative to the=20
=
classpath.<BR>  =
;=20
* @return A buffered=20
=
reader.<BR> =20
* @exception IOException If the file cannot be=20
=
opened.<BR> =20
*/<BR> public =
static=20
BufferedReader openForReading(String name, URI=20
=
base,<BR> &nbs=
p; =20
ClassLoader classLoader) throws IOException=20
=
{<BR> &n=
bsp; =20
if (name.trim().equals("System.in"))=20
=
{<BR> &n=
bsp; =20
if (STD_IN =3D=3D null)=20
=
{<BR> &n=
bsp; =20
STD_IN =3D new BufferedReader(new =
InputStreamReader(System.<BR> =20
=
in));<BR> &nbs=
p; =20
}<BR> =20
=
<BR> &nb=
sp; =20
return=20
=
STD_IN;<BR> &n=
bsp; =20
}<BR> =20
=
<BR> &nb=
sp; =20
// Not standard input. Try URL=20
=
mechanism.<BR>  =
; =20
URL url =3D nameToURL(name, base,=20
classLoader);<BR> =20
=
<BR> &nb=
sp; =20
if (url =3D=3D null)=20
=
{<BR> &n=
bsp; =20
throw new IOException("No file name has been=20
=
specified.");<BR> &n=
bsp; =20
}<BR> =20
=
<BR> &nb=
sp; =20
return new BufferedReader(new=20
InputStreamReader(url.openStream())<BR> =20
);<BR> =20
}<BR> =20
<BR> /** Open =
the=20
specified file for writing or appending. If=20
the<BR> =
* =20
specified name is "System.out", then a writer to=20
=
standard<BR> =
* out is returned. If the file does not exist,=20
then<BR> =
* =20
create it. If the file name is not absolute, the it is=20
=
assumed<BR> =20
* to be relative to the specified base=20
=
directory.<BR>  =
;=20
* If permitted, this method will return a Writer that will=20
simply<BR> =
* =20
overwrite the contents of the file. It is up to the user of=20
this<BR> =
* =20
method to check whether this is OK (by first=20
=
calling<BR> =20
* <A href=3D"mailto:{@link">{@link</A> #nameToFile(String,=20
URI)}<BR> =
* =20
and calling exists() on the returned=20
=
value).<BR> =20
* @param name File=20
name.<BR> =
* =20
@param base The base URI for relative=20
=
references.<BR> &nbs=
p;=20
* @param append If true, then append to the file rather=20
than<BR> =20
* =20
=
overwriting.<BR> &nb=
sp;=20
* @return A writer, or null if no file name is=20
=
specified.<BR>  =
;=20
* @exception IOException If the file cannot be=20
opened<BR> =
* or=20
=
created.<BR> =
*/<BR> public =
static=20
Writer openForWriting(String name, URI base, boolean =
ap<BR> =20
=
pend)<BR> &nbs=
p; =20
throws IOException=20
=
{<BR> &n=
bsp; =20
if (name =3D=3D null)=20
=
{<BR> &n=
bsp; =20
return null; =20
=
<BR> &nb=
sp; =20
=
}<BR> &n=
bsp; =20
if (name.trim().equals("System.out"))=20
=
{<BR> &n=
bsp; =20
if (STD_OUT =3D=3D null)=20
=
{<BR> &n=
bsp; =20
STD_OUT =3D new=20
=
PrintWriter(System.out);<BR> &nb=
sp; =20
}<BR> =20
=
<BR> &nb=
sp; =20
return=20
=
STD_OUT;<BR> &=
nbsp; =20
}<BR> =20
=
<BR> &nb=
sp; =20
if (name.trim().equals(""))=20
=
{<BR> &n=
bsp; =20
return=20
=
null;<BR> &nbs=
p; =20
}<BR> =20
=
<BR> &nb=
sp; =20
File file =3D nameToFile(name,=20
=
base);<BR> &nb=
sp; =20
return new FileWriter(file,=20
append);<BR> =20
}<BR> =20
<BR> =20
=
///////////////////////////////////////////////////////////////////<BR>&n=
bsp; =20
=
//// &nb=
sp; &nbs=
p;=20
public=20
=
members =
=20
////<BR> =20
<BR> /** =
Standard in as=20
a reader, which will be=20
=
non-null<BR> =
* only after a call to=20
=
openForReading("System.in").<BR>  =
; =20
*/<BR> public =
static=20
BufferedReader STD_IN =3D null;<BR> =20
<BR> /** =
Standard out as=20
a writer, which will be=20
=
non-null<BR> =
* only after a call to=20
=
openForWriting("System.out").<BR> &nbs=
p; =20
*/<BR> public =
static=20
PrintWriter STD_OUT =3D null;<BR> =20
<BR> =20
=
///////////////////////////////////////////////////////////////////<BR>&n=
bsp; =20
=
//// &nb=
sp; &nbs=
p;=20
private=20
=
members =
=20
////<BR> =20
<BR> /** Tag =
value used=20
by this class and registered as a=20
parser<BR> =
* =20
constant for the identifier "CLASSPATH" to indicate=20
=
searching<BR> =
=20
* in the classpath. This is a hack, but it deals with the=20
fact<BR> =
* =20
that Java is not symmetric in how it deals with getting=20
files<BR> =
* =20
from the classpath (using getResource) and getting files=20
from<BR> =
* =20
the file=20
=
system.<BR> =20
*/<BR> private =
static=20
String _CLASSPATH_VALUE =3D=20
"xxxxxxCLASSPATHxxxxxx";<BR> =20
}<BR> <BR> =
<BR> <BR> =20
_______________________________________________<BR> =20
Kepler-cvs mailing list<BR> <A=20
=
href=3D"mailto:Kepler-cvs at ecoinformatics.org">Kepler-cvs at ecoinformatics.o=
rg</A><BR> =20
<A=20
=
href=3D"http://mercury.nceas.ucsb.edu/ecoinformatics/mailman/listinfo/kep=
ler-cvs">http://mercury.nceas.ucsb.edu/ecoinformatics/mailman/listinfo/ke=
pler-cvs</A><BR>--------<BR>_____________________________________________=
__<BR>Kepler-dev=20
mailing list<BR><A=20
=
href=3D"mailto:Kepler-dev at ecoinformatics.org">Kepler-dev at ecoinformatics.o=
rg</A><BR><A=20
=
href=3D"http://mercury.nceas.ucsb.edu/ecoinformatics/mailman/listinfo/kep=
ler-dev">http://mercury.nceas.ucsb.edu/ecoinformatics/mailman/listinfo/ke=
pler-dev</A><BR></BLOCKQUOTE></BODY></HTML>
------=_NextPart_000_00B5_01C5479A.0DB664C0--
--------
More information about the Kepler-dev
mailing list