FileDocCategorySizeDatePackage
FilePath.javaAPI DocGlassfish v2 API5686Fri May 04 22:34:38 BST 2007com.sun.jdo.api.persistence.enhancer.util

FilePath

public class FilePath extends Object
FilePath provides general file path manipulation utilities.

Fields Summary
private static String
cwdAbsolute
Constructors Summary
Methods Summary
public static booleancanonicalNamesEqual(java.lang.String f1, java.lang.String f2)
Compare two "canonical" file names for equivalence

    boolean equal;
    String cwd = getCwdAbsolute();
    if (cwd.length() > 2 &&
	cwd.charAt(0) != File.separatorChar &&
	cwd.charAt(1) == ':") {
      equal = f1.equalsIgnoreCase(f2);
    }
    else
      equal = f1.equals(f2);
    return equal;
  
public static java.lang.Stringcanonicalize(java.io.File file)
Attempt to produce a canonical path name for the specified file

    try {
      return file.getCanonicalPath();
    } catch (IOException ioe) {
      /* JDK 1.1.4 gets an IOException if you pass it a UNC path name. */
    }

    /* Do it the hard way if getCanonicalPath fails.
     * This is far from perfect.
     * - It doesn't know about multiple mount points
     * - Doesn't deal with case differences.
     */
    String absolutePath = getAbsolutePath(file);
    Vector components = new Vector();
    StringTokenizer parser = 
      new StringTokenizer(absolutePath, File.separator, true);
    while (parser.hasMoreElements())
      components.addElement(parser.nextToken());

    boolean editted = true;
    while (editted) {
      editted = false;
      for (int i=1; i<components.size() && !editted; i++) {
	String s = (String)components.elementAt(i);
	if (s.equals(".")) {//NOI18N
	  components.removeElementAt(i);
	  components.removeElementAt(i-1);
	  editted = true;
	} else if (s.equals("..")) {//NOI18N
	  components.removeElementAt(i);
	  components.removeElementAt(i-1);
	  if (i > 2) {
	    if (!((String)components.elementAt(i-2)).equals(File.separator) &&
		((String)components.elementAt(i-3)).equals(File.separator)) {
	      components.removeElementAt(i-2);
	      components.removeElementAt(i-3);
	    }
	  }
	  editted = true;
	}
      }
    }

    /* Special case for Windows */
    String cwd = getCwdAbsolute();
    if (cwd.length() > 2 &&
	cwd.charAt(0) != File.separatorChar &&
	cwd.charAt(1) == ':") {
      /* probably a drive letter */
      if (((String)components.elementAt(0)).equals(File.separator) &&
	  (components.size() == 1 ||
	   !((String)components.elementAt(1)).equals(File.separator))) {
	String drive = cwd.substring(0,2);
	components.insertElementAt(drive, 0);
      }
    }

    /* Remove a trailing File.separatorChar */
    if (components.size() > 0 &&
	((String)components.elementAt(components.size()-1)).equals(
		File.separator))
      components.removeElementAt(components.size()-1);

    StringBuffer result = new StringBuffer();
    for (int j=0; j<components.size(); j++)
      result.append((String)components.elementAt(j));

    return result.toString();
  
public static java.lang.StringgetAbsolutePath(java.io.File file)
Return the absolute path for a file. All directory separators are converted to be File.separatorChar

    /* VJ++ blows it here and doesn't use File.separatorChar when making
       a relative path absolute.  It uses '/' instead.  */

    String basicAbsolute = file.getAbsolutePath();
    if (file.separatorChar == '/")
      return basicAbsolute.replace('\\", '/");
    else
      return basicAbsolute.replace('/", '\\");
  
private static java.lang.StringgetCwdAbsolute()

    if (cwdAbsolute == null)
        cwdAbsolute = getAbsolutePath(new File("."));//NOI18N
    return cwdAbsolute;