root / trunk / libraries / libIverUtiles / src / org / gvsig / tools / file / PathGenerator.java @ 34263
History | View | Annotate | Download (5.72 KB)
1 |
package org.gvsig.tools.file; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.net.MalformedURLException; |
5 |
import java.net.URI; |
6 |
import java.net.URISyntaxException; |
7 |
import java.net.URL; |
8 |
import java.util.regex.Pattern; |
9 |
|
10 |
/**
|
11 |
* Generator of path absolute or relative.
|
12 |
*
|
13 |
* Vicente Caballero Navarro
|
14 |
*/
|
15 |
public class PathGenerator { |
16 |
private String basePath = null; |
17 |
private boolean isAbsolutePath = true; |
18 |
private static PathGenerator instance = null; |
19 |
private static String pathSeparator=File.separator; |
20 |
/**
|
21 |
* Return the singleton instance of this object.
|
22 |
*/
|
23 |
public static PathGenerator getInstance(){ |
24 |
if (instance == null){ |
25 |
instance=new PathGenerator();
|
26 |
} |
27 |
return instance;
|
28 |
} |
29 |
|
30 |
/**
|
31 |
* Return the path relative or absolute depends if the option
|
32 |
* isAbsolutePath is true or false.
|
33 |
* @param targetPath Absolute path of file
|
34 |
* @param pathSeparator separator path of system
|
35 |
* @return the path of file.
|
36 |
*/
|
37 |
public String getURLPath(String targetPath) { |
38 |
try {
|
39 |
URL url=new URL(targetPath); |
40 |
File fileIn = new File(url.toURI()); |
41 |
File file = getFile(getPath(fileIn.getAbsolutePath()));
|
42 |
return file.toURI().toURL().toString();
|
43 |
} catch (MalformedURLException e) { |
44 |
e.printStackTrace(); |
45 |
} catch (URISyntaxException e) { |
46 |
e.printStackTrace(); |
47 |
} |
48 |
return targetPath;
|
49 |
|
50 |
} |
51 |
|
52 |
/**
|
53 |
* Return the path relative or absolute depends if the option
|
54 |
* isAbsolutePath is true or false.
|
55 |
* @param targetPath Absolute path of file
|
56 |
* @param pathSeparator separator path of system
|
57 |
* @return the path of file.
|
58 |
*/
|
59 |
public String getPath(String targetPath) { |
60 |
if (isAbsolutePath || basePath==null){ |
61 |
return targetPath;
|
62 |
} |
63 |
if (targetPath == null) { |
64 |
return null; |
65 |
} |
66 |
boolean isDir = false; |
67 |
{ |
68 |
File f = getFile(targetPath);
|
69 |
isDir = f.isDirectory(); |
70 |
} |
71 |
// We need the -1 argument to split to make sure we get a trailing
|
72 |
// "" token if the base ends in the path separator and is therefore
|
73 |
// a directory. We require directory paths to end in the path
|
74 |
// separator -- otherwise they are indistinguishable from files.
|
75 |
String[] base = basePath.split(Pattern.quote(pathSeparator), -1); |
76 |
String[] target = targetPath.split(Pattern.quote(pathSeparator), 0); |
77 |
|
78 |
// First get all the common elements. Store them as a string,
|
79 |
// and also count how many of them there are.
|
80 |
String common = ""; |
81 |
int commonIndex = 0; |
82 |
for (int i = 0; i < target.length && i < base.length; i++) { |
83 |
if (target[i].equals(base[i])) {
|
84 |
common += target[i] + pathSeparator; |
85 |
commonIndex++; |
86 |
} |
87 |
else break; |
88 |
} |
89 |
|
90 |
if (commonIndex == 0) |
91 |
{ |
92 |
// Whoops -- not even a single common path element. This most
|
93 |
// likely indicates differing drive letters, like C: and D:.
|
94 |
// These paths cannot be relativized. Return the target path.
|
95 |
return targetPath;
|
96 |
// This should never happen when all absolute paths
|
97 |
// begin with / as in *nix.
|
98 |
} |
99 |
|
100 |
String relative = ""; |
101 |
if (base.length == commonIndex) {
|
102 |
// Comment this out if you prefer that a relative path not start with ./
|
103 |
relative = "." + pathSeparator;
|
104 |
} |
105 |
else {
|
106 |
int numDirsUp = base.length - commonIndex - (isDir?0:1); /* only subtract 1 if it is a file. */ |
107 |
// The number of directories we have to backtrack is the length of
|
108 |
// the base path MINUS the number of common path elements, minus
|
109 |
// one because the last element in the path isn't a directory.
|
110 |
for (int i = 1; i <= (numDirsUp); i++) { |
111 |
relative += ".." + pathSeparator;
|
112 |
} |
113 |
} |
114 |
//if we are comparing directories then we
|
115 |
if (targetPath.length() > common.length()) {
|
116 |
//it's OK, it isn't a directory
|
117 |
relative += targetPath.substring(common.length()); |
118 |
} |
119 |
|
120 |
return relative;
|
121 |
} |
122 |
|
123 |
/**
|
124 |
* Set the base path of project (.gvp)
|
125 |
* @param path of .GVP
|
126 |
*/
|
127 |
public void setBasePath(String path){ |
128 |
if(path!=null){ |
129 |
basePath = getFile(path).getAbsolutePath(); |
130 |
} else {
|
131 |
basePath = null;
|
132 |
} |
133 |
} |
134 |
|
135 |
/**
|
136 |
* Returns a file from a path.
|
137 |
* @param path relative path.
|
138 |
* @return
|
139 |
*/
|
140 |
private File getFile(String path){ |
141 |
if (path==null) |
142 |
return null; |
143 |
File filePath;
|
144 |
try {
|
145 |
URI uri = new URI(path.replace(" ", "%20")); |
146 |
filePath = new File(uri); |
147 |
} catch (Exception e) { |
148 |
filePath=new File(path); |
149 |
} |
150 |
return filePath;
|
151 |
} |
152 |
/**
|
153 |
* Returns absolute path from a relative.
|
154 |
* @param path relative path.
|
155 |
* @return
|
156 |
*/
|
157 |
public String getAbsoluteURLPath(String path){ |
158 |
try {
|
159 |
URL url=new URL(path); |
160 |
File fileIn=new File(url.toURI()); |
161 |
File file = getFile(getAbsolutePath(fileIn.getAbsolutePath()));
|
162 |
return file.toURI().toURL().toString();
|
163 |
} catch (MalformedURLException e) { |
164 |
e.printStackTrace(); |
165 |
} catch (URISyntaxException e) { |
166 |
e.printStackTrace(); |
167 |
} |
168 |
return path;
|
169 |
|
170 |
} |
171 |
|
172 |
/**
|
173 |
* Returns absolute path from a relative.
|
174 |
* @param path relative path.
|
175 |
* @return
|
176 |
*/
|
177 |
public String getAbsolutePath(String path){ |
178 |
if (path==null) |
179 |
return null; |
180 |
File filePath = getFile(path);
|
181 |
if (isAbsolutePath && filePath.exists())
|
182 |
return path;
|
183 |
filePath=new File(basePath, path); |
184 |
if (filePath.exists())
|
185 |
return filePath.getAbsolutePath();
|
186 |
return path;
|
187 |
} |
188 |
|
189 |
/**
|
190 |
* Set if the path of project works in absolute path or relative path.
|
191 |
* @param b true if is absolute path.
|
192 |
*/
|
193 |
public void setIsAbsolutePath(boolean b){ |
194 |
isAbsolutePath=b; |
195 |
} |
196 |
|
197 |
|
198 |
public static void main(String[] args) { |
199 |
getInstance().setBasePath("C:\\Documents and Settings\\vcn\\Escritorio\\kk.gvp");
|
200 |
String s=getInstance().getPath("C:\\CONSTRU.SHP"); |
201 |
System.err.println("ruta resultado: "+ s); |
202 |
} |
203 |
|
204 |
} |