KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > server > util > PathUtil


1 package org.enhydra.server.util;
2
3 import java.io.File JavaDoc;
4
5 /**
6  * <p>Title: </p>
7  * <p>Description: </p>
8  * <p>Copyright: Copyright (c) 2002</p>
9  * <p>Company: </p>
10  * @author Damir Milovic
11  * @version 1.0
12  */

13
14 public class PathUtil {
15
16     private PathUtil() {
17     }
18
19     /**
20      * Creates relative path from absolute. Resolve relative pathnames against
21      * parent directory.
22      * @param parentPath parent directory path.
23      * @param absolutePath absolute file path (directory or file).
24      * @return relative path if parent directory is included in absolutePath,
25      * otherwise return absolute path.
26      */

27     public static String JavaDoc makeRelativePath(String JavaDoc parentPath, String JavaDoc absolutePath){
28
29             String JavaDoc relativePath = null;
30
31             if(parentPath!=null && absolutePath!=null && (parentPath.length()<absolutePath.length())){
32                 File JavaDoc absoluteFile = new File JavaDoc(absolutePath);
33                 if(absoluteFile.isAbsolute()){
34                     File JavaDoc parentFile = new File JavaDoc(parentPath);
35                     // parentFile must be absolute
36
if(parentFile.isAbsolute()){
37                         String JavaDoc dirPath = parentFile.getAbsolutePath().replace('\\','/');
38                         absolutePath = absolutePath.replace('\\','/');
39                         int dirLength = dirPath.length();
40                         if(absolutePath.substring(0,dirLength).equalsIgnoreCase(dirPath)){
41                             //Cut parent path
42
relativePath = absolutePath.substring(dirLength);
43                             if(relativePath.startsWith("/")){
44                                 relativePath = relativePath.substring(1);
45                             }
46                         }
47                     }else{
48                         relativePath = absoluteFile.getPath().replace('\\','/');
49                     }
50                 }else{
51                     relativePath = absoluteFile.getPath().replace('\\','/');
52                 }
53             }else{
54                 if(absolutePath != null){
55                     relativePath = absolutePath.replace('\\','/');
56                 }
57
58             }
59             return relativePath;
60         }
61
62         /**
63          * Creates absolute path from relative.
64          * @param parentPath root path (directory).
65          * @param relativePath relative path (directory or file).
66          * @return absolute path if <code>parentPath</code> is not <code>null</code> and it is absolute.
67          */

68         public static String JavaDoc makeAbsolutePath(String JavaDoc parentPath, String JavaDoc relativePath){
69             String JavaDoc absolutePath = relativePath;
70
71             if(parentPath != null){
72                 parentPath = parentPath.replace('\\','/');
73
74                 File JavaDoc parentFile = new File JavaDoc(parentPath);
75                 File JavaDoc relativeFile = new File JavaDoc(relativePath);
76                 File JavaDoc dir = null;
77                 //Must create dirs if don't exist
78
parentFile.mkdirs();
79                 if(parentFile.isAbsolute() && (!relativeFile.isAbsolute())){
80                     //Check dir
81
if(parentFile.isDirectory()){
82                         dir = parentFile;
83                     }else{
84                         dir = parentFile.getParentFile();
85                     }
86                     File JavaDoc absoluteFile = new File JavaDoc(dir,relativeFile.getPath());
87                     absolutePath = absoluteFile.getAbsolutePath();
88                     absolutePath = absolutePath.replace('\\','/');
89                 }
90             }
91             return absolutePath;
92         }
93     }
Popular Tags