KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > tool > common > PathHandle


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  * Paul Mahar
22  *
23  */

24 package org.enhydra.tool.common;
25
26 // Standard imports
27
import java.io.File JavaDoc;
28
29 //
30
public class PathHandle {
31     private String JavaDoc path = null;
32
33     public static PathHandle createPathHandle(File JavaDoc inFile) {
34         PathHandle handle = null;
35
36         if (inFile == null) {
37             handle = new PathHandle();
38         } else {
39             handle = PathHandle.createPathHandle(inFile.getAbsolutePath());
40         }
41         return handle;
42     }
43
44     public static PathHandle createPathHandle(String JavaDoc inPath) {
45         PathHandle handle = null;
46
47         handle = new PathHandle();
48         handle.setPath(inPath);
49         return handle;
50     }
51
52     public static String JavaDoc createPathString(String JavaDoc inPath) {
53         PathHandle handle = null;
54
55         handle = PathHandle.createPathHandle(inPath);
56         return handle.getPath();
57     }
58
59     public static String JavaDoc createPathString(File JavaDoc inFile) {
60         PathHandle handle = null;
61
62         handle = PathHandle.createPathHandle(inFile);
63         return handle.getPath();
64     }
65
66     public PathHandle() {}
67
68     public boolean isFile() {
69         return FileUtil.isFile(getPath());
70     }
71
72     public boolean isDirectory() {
73         return FileUtil.isDirectory(getPath());
74     }
75
76     public PathHandle getParent() {
77         PathHandle parent = null;
78         File JavaDoc parentFile = null;
79
80         if (getFile() == null) {
81             parent = null;
82         } else {
83             parentFile = getFile().getParentFile();
84             if (parentFile == null) {
85                 parent = null;
86             } else {
87                 parent = PathHandle.createPathHandle(parentFile);
88             }
89         }
90         return parent;
91     }
92
93     public File JavaDoc getFile() {
94         File JavaDoc file = null;
95
96         if (getPath() == null) {
97
98             // done
99
} else {
100             file = new File JavaDoc(getPath());
101         }
102         return file;
103     }
104
105     public void setPath(final String JavaDoc inPath) {
106         if (inPath == null) {
107
108             // done
109
} else if (inPath.trim().length() == 0) {
110             path = new String JavaDoc();
111         } else {
112             path = new String JavaDoc(inPath);
113             if ((path.indexOf('\\') == -1) && (path.indexOf('/') == -1)) {
114                 path = "./" + path;
115             }
116             path = FileUtil.toCanonicalPath(path);
117             path = path.replace('\\', '/').trim();
118         }
119     }
120
121     public String JavaDoc getPath() {
122         return path;
123     }
124
125     public String JavaDoc toString() {
126         return getPath();
127     }
128
129     public boolean parentOf(File JavaDoc childFile) {
130         PathHandle childHandle = null;
131
132         childHandle = PathHandle.createPathHandle(childFile);
133         return parentOf(childHandle);
134     }
135
136     public boolean parentOf(String JavaDoc childPath) {
137         PathHandle childHandle = null;
138
139         childHandle = PathHandle.createPathHandle(childPath);
140         return parentOf(childHandle);
141     }
142
143     public boolean parentOf(PathHandle childHandle) {
144         boolean isParent = false;
145
146         if (getPath() == null || childHandle.getPath() == null) {
147
148             // Done
149
} else {
150             File JavaDoc parentFile = null;
151
152             parentFile = new File JavaDoc(getPath());
153             if (parentFile.isDirectory()) {
154                 isParent =
155                     childHandle.getComparePath().startsWith(getComparePath()
156                                                             + '/');
157             }
158         }
159         return isParent;
160     }
161
162     public boolean equals(String JavaDoc comparePath) {
163         PathHandle compareHandle = null;
164
165         compareHandle = PathHandle.createPathHandle(comparePath);
166         return equals(compareHandle);
167     }
168
169     public boolean equals(PathHandle compare) {
170         boolean equal = false;
171
172         if (getPath() == null || compare == null
173                 || compare.getPath() == null) {
174
175             // done.
176
} else {
177             equal = getComparePath().equals(compare.getComparePath());
178         }
179         return equal;
180     }
181
182     public boolean hasExtension(String JavaDoc[] exts) {
183         boolean has = false;
184
185         for (int i = 0; i < exts.length; i++) {
186             if (hasExtension(exts[i])) {
187                 has = true;
188                 break;
189             }
190         }
191         return has;
192     }
193
194     public boolean hasExtension(String JavaDoc ext) {
195         boolean has = false;
196
197         if (getPath() == null || ext == null) {
198
199             // done
200
} else {
201             if (isWindows()) {
202                 has = ext.equalsIgnoreCase(getExtension());
203             } else {
204                 has = ext.equals(getExtension());
205             }
206         }
207         return has;
208     }
209
210     public boolean endsWith(String JavaDoc[] inPaths) {
211         boolean endsWith = false;
212
213         if (inPaths == null) {
214
215             // done
216
} else {
217             for (int i = 0; i < inPaths.length; i++) {
218                 if (endsWith(inPaths[i])) {
219                     endsWith = true;
220                     break;
221                 }
222             }
223         }
224         return endsWith;
225     }
226
227     public boolean endsWith(String JavaDoc inPath) {
228         boolean endsWith = false;
229
230         if (inPath == null || getPath() == null) {
231
232             // done
233
} else {
234             inPath = inPath.replace('\\', '/');
235             if (isWindows()) {
236                 endsWith = getComparePath().endsWith(inPath.toLowerCase());
237             } else {
238                 endsWith = getComparePath().endsWith(inPath);
239             }
240         }
241         return endsWith;
242     }
243
244     public boolean isEmpty() {
245         boolean empty = false;
246
247         if (getPath() == null) {
248             empty = true;
249         } else {
250             empty = (getPath().trim().length() == 0);
251         }
252         return empty;
253     }
254
255     public String JavaDoc getExtension() {
256         String JavaDoc ext = new String JavaDoc();
257         int index = -1;
258
259         if (getPath() == null) {
260
261             // done
262
} else {
263             index = getPath().lastIndexOf('.');
264             if (index > getPath().lastIndexOf('/')) {
265                 if ((index > -1) && (getPath().length() > index)) {
266                     ext = getPath().substring(index + 1);
267                 }
268             }
269         }
270         return ext;
271     }
272
273     public void setExtension(String JavaDoc newExt) {
274         String JavaDoc ext = new String JavaDoc();
275
276         if (getPath() == null) {
277
278             // ignore
279
} else {
280             ext = getExtension();
281             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
282
283             if (ext.length() > 0) {
284                 buf.append(path.substring(0, path.length() - ext.length()));
285             } else {
286                 buf.append(path);
287                 buf.append('.');
288             }
289             buf.append(newExt);
290             setPath(buf.toString());
291         }
292     }
293
294     public PathHandle expandRelativePath(String JavaDoc inPath) {
295         PathHandle out = null;
296         PathHandle dir = null;
297
298         if (isFile()) {
299             dir = getParent();
300         } else {
301             dir = this;
302         }
303         if (inPath == null) {
304             out = null;
305         } else if (dir.getPath() == null) {
306             out = PathHandle.createPathHandle(inPath);
307         } else {
308             StringBuffer JavaDoc outPath = new StringBuffer JavaDoc();
309
310             inPath = inPath.replace('\\', '/');
311             if ((inPath.length() == 1) && (inPath.charAt(0) == '.')) {
312                 outPath.append(dir.getPath());
313             } else if ((inPath.length() > 0) && (inPath.charAt(0) == '.')) {
314
315                 // expand path relative to the project.
316
outPath.append(dir.getPath());
317                 outPath.append('/');
318                 if ((inPath.length() > 1) && (inPath.charAt(1) == '.')) {
319                     outPath.append(inPath);
320                 } else {
321                     outPath.append(inPath.substring(2));
322                 }
323             } else {
324                 outPath.append(inPath);
325             }
326             out = PathHandle.createPathHandle(outPath.toString());
327         }
328         return out;
329     }
330
331     public String JavaDoc getRelativePath(String JavaDoc in) {
332         String JavaDoc relPath = new String JavaDoc();
333         if ((in.length() > 0) && (in.charAt(0) == '.')) {
334            relPath = in.replace('\\','/');
335         } else {
336            relPath = getRelativePath(PathHandle.createPathHandle(in));
337         }
338         return relPath;
339     }
340
341     public String JavaDoc getRelativePath(PathHandle inPath) {
342         String JavaDoc relPath = new String JavaDoc();
343         PathHandle dir = null;
344
345         if (isFile()) {
346             dir = getParent();
347         } else {
348             dir = this;
349         }
350         if (dir.getPath() == null) {
351             relPath = inPath.getPath();
352         } else {
353             StringBuffer JavaDoc relBuf = new StringBuffer JavaDoc();
354
355             if (dir.equals(inPath)) {
356                 relBuf.append('.');
357                 relBuf.append('/');
358             } else if (dir.parentOf(inPath)) {
359                 relBuf.append('.');
360                 relBuf.append('/');
361                 relBuf.append(inPath.getPath().substring(dir.getPath().length()
362                                                          + 1));
363             } else {
364                 relBuf.append(inPath);
365             }
366             relPath = relBuf.toString();
367         }
368
369         // Return with separators set to / for any OS but don't do
370
// path handle to avoid decompress.
371
return relPath.replace('\\', '/');
372     }
373
374     private String JavaDoc getComparePath() {
375         String JavaDoc thisPath = getPath();
376
377         if (getPath() == null) {
378
379             // done
380
} else if (isWindows()) {
381             thisPath = getPath().toLowerCase();
382         } else {
383             thisPath = new String JavaDoc(getPath());
384         }
385         return thisPath;
386     }
387
388     private boolean isWindows() {
389         return File.separatorChar == '\\';
390     }
391
392 }
393
Popular Tags