1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 package org.exolab.castor.net.util;
46
47
48 import java.io.*;
49 import java.net.URL;
50 import java.net.MalformedURLException;
51 import java.util.Stack;
52 import java.util.StringTokenizer;
53
54
55
56
57
58
59 public class URIUtils {
60
61
62
63
64 private static final String FILE_PROTOCOL_PREFIX = "file:///";
65
66
67
68
69 private static final char HREF_PATH_SEP = '/';
70
71
72
73
74 private static final String URL_PATH_SEP_STR = "/";
75
76
77
78
79 private static final String CURRENT_DIR_OP = ".";
80
81
82
83
84 private static final String PARENT_DIR_OP = "..";
85
86
87
88
89
90
91
92
93
94
95
96
97
98 public static InputStream getInputStream(String href, String documentBase)
99 throws java.io.FileNotFoundException, java.io.IOException
100 {
101
102
103 URL url = null;
104 try {
105 url = new URL(href);
106 return url.openStream();
107 }
108 catch (MalformedURLException muex) {}
109
110
111 String xHref = null;
112 if ((documentBase != null) && (documentBase.length() > 0)) {
113 int idx = documentBase.lastIndexOf(HREF_PATH_SEP);
114 if (idx == (documentBase.length()-1))
115 xHref = documentBase+href;
116 else
117 xHref = documentBase+HREF_PATH_SEP+href;
118 }
119 else xHref = href;
120
121
122 try {
123 url = new URL(xHref);
124 return url.openStream();
125 }
126 catch(MalformedURLException muex) {}
127
128
129 File iFile = new File(href);
130 if (iFile.isAbsolute()) return new FileInputStream(iFile);
131
132 iFile = new File(xHref);
133 return new FileInputStream(iFile);
134
135 }
136
137
138
139
140
141
142
143
144
145
146
147
148 public static Reader getReader(String href, String documentBase)
149 throws java.io.FileNotFoundException, java.io.IOException
150 {
151 InputStream is = getInputStream(href, documentBase);
152 return new InputStreamReader(is);
153 }
154
155
156
157
158
159 public static String getDocumentBase(String href) {
160
161 String docBase = "";
162
163 if (href == null) return docBase;
164
165 int idx = -1;
166
167 try {
168
169
170 new URL(href);
171 idx = href.lastIndexOf(HREF_PATH_SEP);
172 }
173 catch(MalformedURLException muex) {
174
175
176 int idx2 = href.lastIndexOf(HREF_PATH_SEP);
177 idx = href.lastIndexOf(File.separator);
178 if (idx2 > idx) idx = idx2;
179 }
180
181 if (idx >= 0) docBase = href.substring(0,idx);
182
183 return docBase;
184 }
185
186
187
188
189
190
191 public static String getRelativeURI(String href) {
192
193 if (href == null) return href;
194
195 int idx = -1;
196
197 try {
198
199
200 new URL(href);
201 idx = href.lastIndexOf(HREF_PATH_SEP);
202 }
203 catch(MalformedURLException muex) {
204
205
206 int idx2 = href.lastIndexOf(HREF_PATH_SEP);
207 idx = href.lastIndexOf(File.separator);
208 if (idx2 > idx) idx = idx2;
209 }
210
211 if (idx >= 0)
212 return href.substring(idx+1);
213
214 return href;
215 }
216
217
218
219
220
221
222
223
224 public static String normalize(String absoluteURL)
225 throws MalformedURLException
226 {
227 if (absoluteURL == null) return absoluteURL;
228 if (absoluteURL.indexOf('.') < 0) return absoluteURL;
229
230
231
232
233 Stack tokens = new Stack();
234 StringTokenizer st = new StringTokenizer(absoluteURL, URL_PATH_SEP_STR, true);
235 String last = null;
236 while (st.hasMoreTokens()) {
237 String token = st.nextToken();
238 if (URL_PATH_SEP_STR.equals(token)) {
239 if (URL_PATH_SEP_STR.equals(last)) {
240 tokens.push("");
241 }
242 }
243 else if (PARENT_DIR_OP.equals(token)) {
244 if (tokens.empty()) {
245
246 throw new MalformedURLException("invalid absolute URL: " + absoluteURL);
247 }
248 tokens.pop();
249 }
250 else {
251 if (!CURRENT_DIR_OP.equals(token)) {
252 tokens.push(token);
253 }
254 }
255 last = token;
256 }
257
258
259 StringBuffer buffer = new StringBuffer(absoluteURL.length());
260 for (int i = 0; i < tokens.size(); i++) {
261 if (i > 0) buffer.append(HREF_PATH_SEP);
262 buffer.append(tokens.elementAt(i).toString());
263 }
264 return buffer.toString();
265 }
266
267
268
269
270
271 public static String resolveAsString(String href, String documentBase) {
272
273 try {
274
275
276 new URL(href);
277 return href;
278 }
279 catch(MalformedURLException muex) {}
280
281
282
283 String absolute = null;
284 if ((documentBase != null) && (documentBase.length() > 0)) {
285 int idx = documentBase.lastIndexOf(HREF_PATH_SEP);
286 if (idx == (documentBase.length()-1))
287 absolute = documentBase+href;
288 else
289 absolute = documentBase+HREF_PATH_SEP+href;
290
291
292 }
293 else absolute = href;
294
295
296 try {
297
298
299
300 if (absolute.indexOf("./") >= 0) {
301
302 absolute = normalize(absolute);
303 }
304 new URL(absolute);
305 return absolute;
306 }
307 catch(MalformedURLException muex) {
308
309 int idx = absolute.indexOf(':');
310 if (idx >= 0) {
311 String scheme = absolute.substring(0, idx);
312
313 String error = "unknown protocol: " + scheme;
314 if (error.equals(muex.getMessage())) {
315 return absolute;
316 }
317 }
318
319 }
320
321
322
323 String fileURL = absolute;
324 File iFile = new File(href);
325 boolean exists = iFile.exists();
326 fileURL = createFileURL(iFile.getAbsolutePath());
327 if (!iFile.isAbsolute()) {
328 iFile = new File(absolute);
329 if (iFile.exists() || (!exists)) {
330 fileURL = createFileURL(iFile.getAbsolutePath());
331 }
332 }
333
334
335 try {
336
337
338 new URL(fileURL);
339 return fileURL;
340 }
341 catch(MalformedURLException muex) {}
342
343
344
345
346
347 return absolute;
348 }
349
350
351
352
353
354
355
356 private static String createFileURL(String filename) {
357
358 if (filename == null) return FILE_PROTOCOL_PREFIX;
359 int size = filename.length() + FILE_PROTOCOL_PREFIX.length();
360 StringBuffer sb = new StringBuffer(size);
361 sb.append(FILE_PROTOCOL_PREFIX);
362 char[] chars = filename.toCharArray();
363 for (int i = 0; i < chars.length; i++) {
364 char ch = chars[i];
365 switch (ch) {
366 case '\\':
367 sb.append(HREF_PATH_SEP);
368 break;
369 default:
370 sb.append(ch);
371 break;
372
373 }
374 }
375 return sb.toString();
376 }
377
378
379 }