Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libIverUtiles / src / org / gvsig / tools / file / PathGenerator.java @ 34282

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

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

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

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

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

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