/** * Given a file name or URL, construct a java.io.File object that refers to the file name or URL. * This method first attempts to directly use the file name to construct the File. If the * resulting File is a relative pathname, then it is resolved relative to the specified base URI, * if there is one. If there is no such base URI, then it simply returns the relative File object. * See the java.io.File documentation for a details about relative and absolute pathnames. * * <p>If the name begins with "xxxxxxCLASSPATHxxxxxx" or "$CLASSPATH" then search for the file * relative to the classpath. * * <p>Note that "xxxxxxCLASSPATHxxxxxx" is the value of the globally defined constant $CLASSPATH * available in the Ptolemy II expression language. * * <p>If the name begins with $CLASSPATH or "xxxxxxCLASSPATHxxxxxx" but that name cannot be found * in the classpath, the value of the ptolemy.ptII.dir property is substituted in. * * <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. * * <p>This method is similar to {@link #nameToURL(String, URI, ClassLoader)} except that in this * method, the file or URL must be readable. Usually, this method is use for write a file and * {@link #nameToURL(String, URI, ClassLoader)} is used for reading. * * @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. * @see #nameToURL(String, URI, ClassLoader) */ public static File nameToFile(String name, URI base) { if ((name == null) || name.trim().equals("")) { return null; } if (name.startsWith(_CLASSPATH_VALUE) || name.startsWith("$CLASSPATH")) { URL result = null; try { result = _searchClassPath(name, null); } catch (IOException ex) { // Ignore. In nameToFile(), it is ok if we don't find the variable } if (result != null) { return new File(result.getPath()); } else { String ptII = StringUtilities.getProperty("ptolemy.ptII.dir"); if (ptII != null && ptII.length() > 0) { return new File(ptII, _trimClassPath(name)); } } } File file = new File(name); if (!file.isAbsolute()) { // Try to resolve the base directory. if (base != null) { // Need to replace \ with /, otherwise resolve would fail even // if invoked in a windows OS. -- tfeng (02/27/2009) URI newURI = base.resolve(StringUtilities.substitute(name, " ", "%20").replace('\\', '/')); // file = new File(newURI); String urlString = newURI.getPath(); file = new File(StringUtilities.substitute(urlString, "%20", " ")); } } return file; }