Statistics
| Revision:

root / trunk / libraries / libIverUtiles / src / org / gvsig / tools / file / PathGenerator.java @ 34711

History | View | Annotate | Download (6.11 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
                        file=new File(file.getPath().replace(".\\.\\", ".\\").replace("././", "./"));
43
                        return file.getPath();
44
                 } catch (MalformedURLException e) {
45
                        e.printStackTrace();
46
                } catch (IllegalArgumentException e) {
47
                        return targetPath;
48
                } catch (URISyntaxException e) {
49
                        e.printStackTrace();
50
                }
51
                 return targetPath;
52
                 
53
         }
54
        
55
        /**
56
         * Return the path relative or absolute depends if the option 
57
         * isAbsolutePath is true or false. 
58
         * @param targetPath Absolute path of file
59
         * @param pathSeparator separator path of system
60
         * @return the path of file.
61
         */
62
         public String getPath(String targetPath) {
63
                 if (isAbsolutePath || basePath==null){
64
                         return targetPath;
65
                 }
66
                 if (targetPath == null) {
67
                         return null;
68
                 }
69
                 boolean isDir = false;
70
                 {
71
                   File f = getFile(targetPath);
72
                   isDir = f.isDirectory();
73
                 }
74
                 //  We need the -1 argument to split to make sure we get a trailing 
75
                 //  "" token if the base ends in the path separator and is therefore
76
                 //  a directory. We require directory paths to end in the path
77
                 //  separator -- otherwise they are indistinguishable from files.
78
                 String[] base = basePath.split(Pattern.quote(pathSeparator), -1);
79
                 String[] target = targetPath.split(Pattern.quote(pathSeparator), 0);
80

    
81
                 //  First get all the common elements. Store them as a string,
82
                 //  and also count how many of them there are. 
83
                 String common = "";
84
                 int commonIndex = 0;
85
                 for (int i = 0; i < target.length && i < base.length; i++) {
86
                     if (target[i].equals(base[i])) {
87
                         common += target[i] + pathSeparator;
88
                         commonIndex++;
89
                     }
90
                     else break;
91
                 }
92

    
93
                 if (commonIndex == 0)
94
                 {
95
                     //  Whoops -- not even a single common path element. This most
96
                     //  likely indicates differing drive letters, like C: and D:. 
97
                     //  These paths cannot be relativized. Return the target path.
98
                     return targetPath;
99
                     //  This should never happen when all absolute paths
100
                     //  begin with / as in *nix. 
101
                 }
102

    
103
                 String relative = "";
104
                 if (base.length == commonIndex) {
105
                     //  Comment this out if you prefer that a relative path not start with ./
106
                     relative = "." + pathSeparator;
107
                 }
108
                 else {
109
                     int numDirsUp = base.length - commonIndex - (isDir?0:1); /* only subtract 1 if it  is a file. */
110
                     //  The number of directories we have to backtrack is the length of 
111
                     //  the base path MINUS the number of common path elements, minus
112
                     //  one because the last element in the path isn't a directory.
113
                     for (int i = 1; i <= (numDirsUp); i++) {
114
                         relative += ".." + pathSeparator;
115
                     }
116
                 }
117
                 //if we are comparing directories then we 
118
                 if (targetPath.length() > common.length()) {
119
                  //it's OK, it isn't a directory
120
                  relative += targetPath.substring(common.length());
121
                 }
122

    
123
                 return relative;
124
         }
125
        
126
         /**
127
          * Set the base path of project (.gvp)
128
          * @param path of .GVP
129
          */
130
        public void setBasePath(String path){
131
                if(path!=null){
132
                        basePath = getFile(path).getAbsolutePath();
133
                } else {
134
                        basePath = null;
135
                }
136
        }
137

    
138
        /**
139
         * Returns a file from a path.
140
         * @param path relative path.
141
         * @return
142
         */
143
        private File getFile(String path){
144
                if (path==null)
145
                        return null;
146
                File filePath;
147
                try {
148
                        URI uri = new URI(path.replace(" ", "%20"));
149
                        filePath = new File(uri);
150
                } catch (Exception e) {
151
                        filePath=new File(path);
152
                }
153
                return filePath;
154
        }
155
        /**
156
         * Returns absolute path from a relative.
157
         * @param path relative path.
158
         * @return
159
         */
160
        public String getAbsoluteURLPath(String path){
161
                try {
162
                        URL url=new URL(path);
163
                        File fileIn=new File(url.toURI());
164
                        File file = getFile(getAbsolutePath(fileIn.getAbsolutePath()));
165
                        return file.toURI().toURL().toString();
166
                } catch (MalformedURLException e) {
167
                        try {
168
                                File filePath = new File(path);
169
                                File file = new File(getAbsolutePath(filePath.getPath()));
170
                                return file.toURI().toURL().toString();
171
                        } catch (MalformedURLException e1) {
172
                                e1.printStackTrace();
173
                        }
174
                } catch (IllegalArgumentException e) {
175
                        return path;
176
                }catch (URISyntaxException e) {
177
                        e.printStackTrace();
178
                }
179
                return path;
180
                
181
        }
182
        
183
        /**
184
         * Returns absolute path from a relative.
185
         * @param path relative path.
186
         * @return
187
         */
188
        public String getAbsolutePath(String path){
189
                if (path==null)
190
                        return null;
191
                File filePath = getFile(path);
192
                if (isAbsolutePath && filePath.exists())
193
                        return path;
194
                filePath=new File(basePath, path);
195
                if (filePath.exists())
196
                        return filePath.getAbsolutePath();
197
                return path;
198
        }
199
        
200
        /**
201
         * Set if the path of project works in absolute path or relative path.
202
         * @param b true if is absolute path.
203
         */
204
        public void setIsAbsolutePath(boolean b){
205
                isAbsolutePath=b;
206
        }
207
        
208
        
209
        public static void main(String[] args) {
210
                getInstance().setBasePath("C:\\Documents and Settings\\vcn\\Escritorio\\kk.gvp");
211
                String s=getInstance().getPath("C:\\CONSTRU.SHP");
212
                System.err.println("ruta resultado: "+ s);
213
        }
214
        
215
}