KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jicengine > io > PathResolver


1 package org.jicengine.io;
2 import java.io.File JavaDoc;
3 import java.util.StringTokenizer JavaDoc;
4
5 /**
6  *
7  * <p>
8  * Copyright (C) 2004 Timo Laitinen
9  * </p>
10  * @author Timo Laitinen
11  * @created 2004-09-20
12  * @since JICE-0.10
13  */

14 public class PathResolver {
15
16     private static final String JavaDoc DIRECTORY_SEPARATOR = "/";
17
18     /**
19      * <p>
20      * Forms a real path from a base-path and a relative path.
21      * </p>
22      *
23      * <p>
24      * For example, <br>
25      * <code>'/directory/file1.txt' + 'file2.txt' -> '/directory/file2.txt'</code><br>
26      * <code>'/directory/file1.txt' + '../file2.txt' -> '/file2.txt'</code><br>
27      * <code>'/directory/subdir/' + 'file2.txt' -> '/directory/subdir/file2.txt'</code><br>
28      * <code>'/directory/file1.txt' + 'subdir/' -> '/directory/subdir/'</code><br>
29      * </p>
30      *
31      * @param contextPath
32      * @param relativePath
33      */

34     public static String JavaDoc getRealPath(String JavaDoc contextPath, String JavaDoc relativePath)
35     {
36         File JavaDoc baseFile;
37         if( contextPath.endsWith(DIRECTORY_SEPARATOR) || contextPath.endsWith(File.separator) ){
38             // a directory
39
baseFile = new File JavaDoc(contextPath);
40         }
41         else {
42             // not a directory, so use the parent-file as the base-file.
43
// the parent-file should be a directory..
44
baseFile = new File JavaDoc(contextPath).getParentFile();
45         }
46
47         File JavaDoc newFile = new File JavaDoc(baseFile, relativePath);
48
49         String JavaDoc realPath = getPathOf(newFile);
50
51         // directories must end in '/'.
52
// unfortunately the java.io.File discard the trailing '/'.
53
// we'll add it manually.
54
if( relativePath.endsWith(DIRECTORY_SEPARATOR) && !realPath.endsWith(DIRECTORY_SEPARATOR)){
55             realPath = realPath + DIRECTORY_SEPARATOR;
56         }
57
58         return realPath;
59     }
60
61     public static String JavaDoc getPathOf(File JavaDoc file)
62     {
63         String JavaDoc path;
64         if( File.separator.equals(DIRECTORY_SEPARATOR) ){
65             // no need to do any conversions
66
path = file.getPath();
67         }
68         else {
69             // convert the windows file-separator to the unix-style that we are using.
70
StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(file.getPath(), File.separator, true);
71             String JavaDoc token;
72             StringBuffer JavaDoc pathBuffer = new StringBuffer JavaDoc();
73             while(tokenizer.hasMoreElements() ){
74                 token = tokenizer.nextToken();
75                 if( token.equals(File.separator) ){
76                     pathBuffer.append(DIRECTORY_SEPARATOR);
77                 }
78                 else {
79                     pathBuffer.append(token);
80                 }
81             }
82             path = pathBuffer.toString();
83         }
84
85         return path;
86     }
87
88 }
89
Popular Tags