1 23 24 25 package com.sun.jdo.api.persistence.enhancer.util; 26 27 import java.util.StringTokenizer ; 28 import java.util.Enumeration ; 29 import java.util.NoSuchElementException ; 30 import java.util.Vector ; 31 import java.io.File ; 32 import java.io.IOException ; 33 import java.io.FilenameFilter ; 34 35 36 39 40 public class FilePath { 41 private static String cwdAbsolute; 42 43 47 public static String getAbsolutePath(File file) { 48 50 51 String basicAbsolute = file.getAbsolutePath(); 52 if (file.separatorChar == '/') 53 return basicAbsolute.replace('\\', '/'); 54 else 55 return basicAbsolute.replace('/', '\\'); 56 } 57 58 private static String getCwdAbsolute() { 59 if (cwdAbsolute == null) 60 cwdAbsolute = getAbsolutePath(new File (".")); return cwdAbsolute; 62 } 63 64 67 public static String canonicalize(File file) { 68 try { 69 return file.getCanonicalPath(); 70 } catch (IOException ioe) { 71 72 } 73 74 79 String absolutePath = getAbsolutePath(file); 80 Vector components = new Vector (); 81 StringTokenizer parser = 82 new StringTokenizer (absolutePath, File.separator, true); 83 while (parser.hasMoreElements()) 84 components.addElement(parser.nextToken()); 85 86 boolean editted = true; 87 while (editted) { 88 editted = false; 89 for (int i=1; i<components.size() && !editted; i++) { 90 String s = (String )components.elementAt(i); 91 if (s.equals(".")) { components.removeElementAt(i); 93 components.removeElementAt(i-1); 94 editted = true; 95 } else if (s.equals("..")) { components.removeElementAt(i); 97 components.removeElementAt(i-1); 98 if (i > 2) { 99 if (!((String )components.elementAt(i-2)).equals(File.separator) && 100 ((String )components.elementAt(i-3)).equals(File.separator)) { 101 components.removeElementAt(i-2); 102 components.removeElementAt(i-3); 103 } 104 } 105 editted = true; 106 } 107 } 108 } 109 110 111 String cwd = getCwdAbsolute(); 112 if (cwd.length() > 2 && 113 cwd.charAt(0) != File.separatorChar && 114 cwd.charAt(1) == ':') { 115 116 if (((String )components.elementAt(0)).equals(File.separator) && 117 (components.size() == 1 || 118 !((String )components.elementAt(1)).equals(File.separator))) { 119 String drive = cwd.substring(0,2); 120 components.insertElementAt(drive, 0); 121 } 122 } 123 124 125 if (components.size() > 0 && 126 ((String )components.elementAt(components.size()-1)).equals( 127 File.separator)) 128 components.removeElementAt(components.size()-1); 129 130 StringBuffer result = new StringBuffer (); 131 for (int j=0; j<components.size(); j++) 132 result.append((String )components.elementAt(j)); 133 134 return result.toString(); 135 } 136 137 140 public static boolean canonicalNamesEqual(String f1, String f2) { 141 boolean equal; 142 String cwd = getCwdAbsolute(); 143 if (cwd.length() > 2 && 144 cwd.charAt(0) != File.separatorChar && 145 cwd.charAt(1) == ':') { 146 equal = f1.equalsIgnoreCase(f2); 147 } 148 else 149 equal = f1.equals(f2); 150 return equal; 151 } 152 153 } 154 155 | Popular Tags |