svn-gvsig-desktop / trunk / libraries / libIverUtiles / src / org / gvsig / tools / file / PathGenerator.java @ 33329
History | View | Annotate | Download (3.98 KB)
1 |
package org.gvsig.tools.file; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.util.regex.Pattern; |
5 |
|
6 |
/**
|
7 |
* Generator of path absolute or relative.
|
8 |
*
|
9 |
* Vicente Caballero Navarro
|
10 |
*/
|
11 |
public class PathGenerator { |
12 |
private String basePath = null; |
13 |
private boolean isAbsolutePath = true; |
14 |
private static PathGenerator instance = null; |
15 |
private static String pathSeparator=File.separator; |
16 |
/**
|
17 |
* Return the singleton instance of this object.
|
18 |
*/
|
19 |
public static PathGenerator getInstance(){ |
20 |
if (instance == null){ |
21 |
instance=new PathGenerator();
|
22 |
} |
23 |
return instance;
|
24 |
} |
25 |
|
26 |
/**
|
27 |
* Return the path relative or absolute depends if the option
|
28 |
* isAbsolutePath is true or false.
|
29 |
* @param targetPath Absolute path of file
|
30 |
* @param pathSeparator separator path of system
|
31 |
* @return the path of file.
|
32 |
*/
|
33 |
public String getPath(String targetPath) { |
34 |
if (isAbsolutePath || basePath==null){ |
35 |
return targetPath;
|
36 |
} |
37 |
|
38 |
boolean isDir = false; |
39 |
{ |
40 |
File f = new File(targetPath); |
41 |
isDir = f.isDirectory(); |
42 |
} |
43 |
// We need the -1 argument to split to make sure we get a trailing
|
44 |
// "" token if the base ends in the path separator and is therefore
|
45 |
// a directory. We require directory paths to end in the path
|
46 |
// separator -- otherwise they are indistinguishable from files.
|
47 |
String[] base = basePath.split(Pattern.quote(pathSeparator), -1); |
48 |
String[] target = targetPath.split(Pattern.quote(pathSeparator), 0); |
49 |
|
50 |
// First get all the common elements. Store them as a string,
|
51 |
// and also count how many of them there are.
|
52 |
String common = ""; |
53 |
int commonIndex = 0; |
54 |
for (int i = 0; i < target.length && i < base.length; i++) { |
55 |
if (target[i].equals(base[i])) {
|
56 |
common += target[i] + pathSeparator; |
57 |
commonIndex++; |
58 |
} |
59 |
else break; |
60 |
} |
61 |
|
62 |
if (commonIndex == 0) |
63 |
{ |
64 |
// Whoops -- not even a single common path element. This most
|
65 |
// likely indicates differing drive letters, like C: and D:.
|
66 |
// These paths cannot be relativized. Return the target path.
|
67 |
return targetPath;
|
68 |
// This should never happen when all absolute paths
|
69 |
// begin with / as in *nix.
|
70 |
} |
71 |
|
72 |
String relative = ""; |
73 |
if (base.length == commonIndex) {
|
74 |
// Comment this out if you prefer that a relative path not start with ./
|
75 |
relative = "." + pathSeparator;
|
76 |
} |
77 |
else {
|
78 |
int numDirsUp = base.length - commonIndex - (isDir?0:1); /* only subtract 1 if it is a file. */ |
79 |
// The number of directories we have to backtrack is the length of
|
80 |
// the base path MINUS the number of common path elements, minus
|
81 |
// one because the last element in the path isn't a directory.
|
82 |
for (int i = 1; i <= (numDirsUp); i++) { |
83 |
relative += ".." + pathSeparator;
|
84 |
} |
85 |
} |
86 |
//if we are comparing directories then we
|
87 |
if (targetPath.length() > common.length()) {
|
88 |
//it's OK, it isn't a directory
|
89 |
relative += targetPath.substring(common.length()); |
90 |
} |
91 |
|
92 |
return relative;
|
93 |
} |
94 |
|
95 |
/**
|
96 |
* Set the base path of project (.gvp)
|
97 |
* @param path of .GVP
|
98 |
*/
|
99 |
public void setBasePath(String path){ |
100 |
basePath=path; |
101 |
} |
102 |
|
103 |
/**
|
104 |
* Returns absolute path from a relative.
|
105 |
* @param path relative path.
|
106 |
* @return
|
107 |
*/
|
108 |
public String getAbsolutePath(String path){ |
109 |
File filePath=new File(path); |
110 |
if (isAbsolutePath && filePath.exists())
|
111 |
return path;
|
112 |
filePath=new File(basePath, path); |
113 |
if (filePath.exists())
|
114 |
return filePath.getAbsolutePath();
|
115 |
return null; |
116 |
} |
117 |
|
118 |
/**
|
119 |
* Set if the path of project works in absolute path or relative path.
|
120 |
* @param b true if is absolute path.
|
121 |
*/
|
122 |
public void setIsAbsolutePath(boolean b){ |
123 |
isAbsolutePath=b; |
124 |
} |
125 |
|
126 |
|
127 |
public static void main(String[] args) { |
128 |
getInstance().setBasePath("C:\\Documents and Settings\\vcn\\Escritorio\\kk.gvp");
|
129 |
String s=getInstance().getPath("C:\\CONSTRU.SHP"); |
130 |
System.err.println("ruta resultado: "+ s); |
131 |
} |
132 |
|
133 |
} |