KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > api > persistence > enhancer > util > FilePath


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24
25 package com.sun.jdo.api.persistence.enhancer.util;
26
27 import java.util.StringTokenizer JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.NoSuchElementException JavaDoc;
30 import java.util.Vector JavaDoc;
31 import java.io.File JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.FilenameFilter JavaDoc;
34
35
36 /**
37  * FilePath provides general file path manipulation utilities.
38  */

39
40 public class FilePath {
41   private static String JavaDoc cwdAbsolute;
42
43   /**
44    * Return the absolute path for a file. All directory separators are
45    * converted to be File.separatorChar
46    */

47   public static String JavaDoc getAbsolutePath(File JavaDoc file) {
48     /* VJ++ blows it here and doesn't use File.separatorChar when making
49        a relative path absolute. It uses '/' instead. */

50
51     String JavaDoc basicAbsolute = file.getAbsolutePath();
52     if (file.separatorChar == '/')
53       return basicAbsolute.replace('\\', '/');
54     else
55       return basicAbsolute.replace('/', '\\');
56   }
57
58   private static String JavaDoc getCwdAbsolute() {
59     if (cwdAbsolute == null)
60         cwdAbsolute = getAbsolutePath(new File JavaDoc("."));//NOI18N
61
return cwdAbsolute;
62   }
63
64   /**
65    * Attempt to produce a canonical path name for the specified file
66    */

67   public static String JavaDoc canonicalize(File JavaDoc file) {
68     try {
69       return file.getCanonicalPath();
70     } catch (IOException JavaDoc ioe) {
71       /* JDK 1.1.4 gets an IOException if you pass it a UNC path name. */
72     }
73
74     /* Do it the hard way if getCanonicalPath fails.
75      * This is far from perfect.
76      * - It doesn't know about multiple mount points
77      * - Doesn't deal with case differences.
78      */

79     String JavaDoc absolutePath = getAbsolutePath(file);
80     Vector JavaDoc components = new Vector JavaDoc();
81     StringTokenizer JavaDoc parser =
82       new StringTokenizer JavaDoc(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 JavaDoc s = (String JavaDoc)components.elementAt(i);
91     if (s.equals(".")) {//NOI18N
92
components.removeElementAt(i);
93       components.removeElementAt(i-1);
94       editted = true;
95     } else if (s.equals("..")) {//NOI18N
96
components.removeElementAt(i);
97       components.removeElementAt(i-1);
98       if (i > 2) {
99         if (!((String JavaDoc)components.elementAt(i-2)).equals(File.separator) &&
100         ((String JavaDoc)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     /* Special case for Windows */
111     String JavaDoc cwd = getCwdAbsolute();
112     if (cwd.length() > 2 &&
113     cwd.charAt(0) != File.separatorChar &&
114     cwd.charAt(1) == ':') {
115       /* probably a drive letter */
116       if (((String JavaDoc)components.elementAt(0)).equals(File.separator) &&
117       (components.size() == 1 ||
118        !((String JavaDoc)components.elementAt(1)).equals(File.separator))) {
119     String JavaDoc drive = cwd.substring(0,2);
120     components.insertElementAt(drive, 0);
121       }
122     }
123
124     /* Remove a trailing File.separatorChar */
125     if (components.size() > 0 &&
126     ((String JavaDoc)components.elementAt(components.size()-1)).equals(
127         File.separator))
128       components.removeElementAt(components.size()-1);
129
130     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
131     for (int j=0; j<components.size(); j++)
132       result.append((String JavaDoc)components.elementAt(j));
133
134     return result.toString();
135   }
136
137   /**
138    * Compare two "canonical" file names for equivalence
139    */

140   public static boolean canonicalNamesEqual(String JavaDoc f1, String JavaDoc f2) {
141     boolean equal;
142     String JavaDoc 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