author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 44797 | jaxp/src/java.xml/share/classes/com/sun/org/apache/xml/internal/utils/URI.java@8b3b3b911b8a |
child 47443 | 711ef438b6ad |
permissions | -rw-r--r-- |
6 | 1 |
/* |
2 |
* reserved comment block |
|
3 |
* DO NOT REMOVE OR ALTER! |
|
4 |
*/ |
|
5 |
/* |
|
44797
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
6 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
7 |
* contributor license agreements. See the NOTICE file distributed with |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
8 |
* this work for additional information regarding copyright ownership. |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
9 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
10 |
* (the "License"); you may not use this file except in compliance with |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
11 |
* the License. You may obtain a copy of the License at |
6 | 12 |
* |
44797
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
13 |
* http://www.apache.org/licenses/LICENSE-2.0 |
6 | 14 |
* |
15 |
* Unless required by applicable law or agreed to in writing, software |
|
16 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
17 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
18 |
* See the License for the specific language governing permissions and |
|
19 |
* limitations under the License. |
|
20 |
*/ |
|
44797
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
21 |
|
6 | 22 |
package com.sun.org.apache.xml.internal.utils; |
23 |
||
24 |
import java.io.IOException; |
|
25 |
import java.io.Serializable; |
|
26 |
||
27 |
import com.sun.org.apache.xml.internal.res.XMLErrorResources; |
|
28 |
import com.sun.org.apache.xml.internal.res.XMLMessages; |
|
17538 | 29 |
import java.util.Objects; |
6 | 30 |
|
31 |
/** |
|
32 |
* A class to represent a Uniform Resource Identifier (URI). This class |
|
33 |
* is designed to handle the parsing of URIs and provide access to |
|
34 |
* the various components (scheme, host, port, userinfo, path, query |
|
35 |
* string and fragment) that may constitute a URI. |
|
36 |
* <p> |
|
37 |
* Parsing of a URI specification is done according to the URI |
|
38 |
* syntax described in RFC 2396 |
|
39 |
* <http://www.ietf.org/rfc/rfc2396.txt?number=2396>. Every URI consists |
|
40 |
* of a scheme, followed by a colon (':'), followed by a scheme-specific |
|
41 |
* part. For URIs that follow the "generic URI" syntax, the scheme- |
|
42 |
* specific part begins with two slashes ("//") and may be followed |
|
43 |
* by an authority segment (comprised of user information, host, and |
|
44 |
* port), path segment, query segment and fragment. Note that RFC 2396 |
|
45 |
* no longer specifies the use of the parameters segment and excludes |
|
46 |
* the "user:password" syntax as part of the authority segment. If |
|
47 |
* "user:password" appears in a URI, the entire user/password string |
|
48 |
* is stored as userinfo. |
|
49 |
* <p> |
|
50 |
* For URIs that do not follow the "generic URI" syntax (e.g. mailto), |
|
51 |
* the entire scheme-specific part is treated as the "path" portion |
|
52 |
* of the URI. |
|
53 |
* <p> |
|
54 |
* Note that, unlike the java.net.URL class, this class does not provide |
|
55 |
* any built-in network access functionality nor does it provide any |
|
56 |
* scheme-specific functionality (for example, it does not know a |
|
57 |
* default port for a specific scheme). Rather, it only knows the |
|
58 |
* grammar and basic set of operations that can be applied to a URI. |
|
59 |
* |
|
60 |
* |
|
61 |
*/ |
|
62 |
public class URI implements Serializable |
|
63 |
{ |
|
64 |
static final long serialVersionUID = 7096266377907081897L; |
|
65 |
||
66 |
/** |
|
67 |
* MalformedURIExceptions are thrown in the process of building a URI |
|
68 |
* or setting fields on a URI when an operation would result in an |
|
69 |
* invalid URI specification. |
|
70 |
* |
|
71 |
*/ |
|
72 |
public static class MalformedURIException extends IOException |
|
73 |
{ |
|
74 |
||
75 |
/** |
|
76 |
* Constructs a <code>MalformedURIException</code> with no specified |
|
77 |
* detail message. |
|
78 |
*/ |
|
79 |
public MalformedURIException() |
|
80 |
{ |
|
81 |
super(); |
|
82 |
} |
|
83 |
||
84 |
/** |
|
85 |
* Constructs a <code>MalformedURIException</code> with the |
|
86 |
* specified detail message. |
|
87 |
* |
|
88 |
* @param p_msg the detail message. |
|
89 |
*/ |
|
90 |
public MalformedURIException(String p_msg) |
|
91 |
{ |
|
92 |
super(p_msg); |
|
93 |
} |
|
94 |
} |
|
95 |
||
96 |
/** reserved characters */ |
|
97 |
private static final String RESERVED_CHARACTERS = ";/?:@&=+$,"; |
|
98 |
||
99 |
/** |
|
100 |
* URI punctuation mark characters - these, combined with |
|
101 |
* alphanumerics, constitute the "unreserved" characters |
|
102 |
*/ |
|
103 |
private static final String MARK_CHARACTERS = "-_.!~*'() "; |
|
104 |
||
105 |
/** scheme can be composed of alphanumerics and these characters */ |
|
106 |
private static final String SCHEME_CHARACTERS = "+-."; |
|
107 |
||
108 |
/** |
|
109 |
* userinfo can be composed of unreserved, escaped and these |
|
110 |
* characters |
|
111 |
*/ |
|
112 |
private static final String USERINFO_CHARACTERS = ";:&=+$,"; |
|
113 |
||
114 |
/** Stores the scheme (usually the protocol) for this URI. |
|
115 |
* @serial */ |
|
116 |
private String m_scheme = null; |
|
117 |
||
118 |
/** If specified, stores the userinfo for this URI; otherwise null. |
|
119 |
* @serial */ |
|
120 |
private String m_userinfo = null; |
|
121 |
||
122 |
/** If specified, stores the host for this URI; otherwise null. |
|
123 |
* @serial */ |
|
124 |
private String m_host = null; |
|
125 |
||
126 |
/** If specified, stores the port for this URI; otherwise -1. |
|
127 |
* @serial */ |
|
128 |
private int m_port = -1; |
|
129 |
||
130 |
/** If specified, stores the path for this URI; otherwise null. |
|
131 |
* @serial */ |
|
132 |
private String m_path = null; |
|
133 |
||
134 |
/** |
|
135 |
* If specified, stores the query string for this URI; otherwise |
|
136 |
* null. |
|
137 |
* @serial |
|
138 |
*/ |
|
139 |
private String m_queryString = null; |
|
140 |
||
141 |
/** If specified, stores the fragment for this URI; otherwise null. |
|
142 |
* @serial */ |
|
143 |
private String m_fragment = null; |
|
144 |
||
145 |
/** Indicate whether in DEBUG mode */ |
|
146 |
private static boolean DEBUG = false; |
|
147 |
||
148 |
/** |
|
149 |
* Construct a new and uninitialized URI. |
|
150 |
*/ |
|
151 |
public URI(){} |
|
152 |
||
153 |
/** |
|
154 |
* Construct a new URI from another URI. All fields for this URI are |
|
155 |
* set equal to the fields of the URI passed in. |
|
156 |
* |
|
157 |
* @param p_other the URI to copy (cannot be null) |
|
158 |
*/ |
|
159 |
public URI(URI p_other) |
|
160 |
{ |
|
161 |
initialize(p_other); |
|
162 |
} |
|
163 |
||
164 |
/** |
|
165 |
* Construct a new URI from a URI specification string. If the |
|
166 |
* specification follows the "generic URI" syntax, (two slashes |
|
167 |
* following the first colon), the specification will be parsed |
|
168 |
* accordingly - setting the scheme, userinfo, host,port, path, query |
|
169 |
* string and fragment fields as necessary. If the specification does |
|
170 |
* not follow the "generic URI" syntax, the specification is parsed |
|
171 |
* into a scheme and scheme-specific part (stored as the path) only. |
|
172 |
* |
|
173 |
* @param p_uriSpec the URI specification string (cannot be null or |
|
174 |
* empty) |
|
175 |
* |
|
176 |
* @throws MalformedURIException if p_uriSpec violates any syntax |
|
177 |
* rules |
|
178 |
*/ |
|
179 |
public URI(String p_uriSpec) throws MalformedURIException |
|
180 |
{ |
|
181 |
this((URI) null, p_uriSpec); |
|
182 |
} |
|
183 |
||
184 |
/** |
|
185 |
* Construct a new URI from a base URI and a URI specification string. |
|
186 |
* The URI specification string may be a relative URI. |
|
187 |
* |
|
188 |
* @param p_base the base URI (cannot be null if p_uriSpec is null or |
|
189 |
* empty) |
|
190 |
* @param p_uriSpec the URI specification string (cannot be null or |
|
191 |
* empty if p_base is null) |
|
192 |
* |
|
193 |
* @throws MalformedURIException if p_uriSpec violates any syntax |
|
194 |
* rules |
|
195 |
*/ |
|
196 |
public URI(URI p_base, String p_uriSpec) throws MalformedURIException |
|
197 |
{ |
|
198 |
initialize(p_base, p_uriSpec); |
|
199 |
} |
|
200 |
||
201 |
/** |
|
202 |
* Construct a new URI that does not follow the generic URI syntax. |
|
203 |
* Only the scheme and scheme-specific part (stored as the path) are |
|
204 |
* initialized. |
|
205 |
* |
|
206 |
* @param p_scheme the URI scheme (cannot be null or empty) |
|
207 |
* @param p_schemeSpecificPart the scheme-specific part (cannot be |
|
208 |
* null or empty) |
|
209 |
* |
|
210 |
* @throws MalformedURIException if p_scheme violates any |
|
211 |
* syntax rules |
|
212 |
*/ |
|
213 |
public URI(String p_scheme, String p_schemeSpecificPart) |
|
214 |
throws MalformedURIException |
|
215 |
{ |
|
216 |
||
217 |
if (p_scheme == null || p_scheme.trim().length() == 0) |
|
218 |
{ |
|
219 |
throw new MalformedURIException( |
|
220 |
"Cannot construct URI with null/empty scheme!"); |
|
221 |
} |
|
222 |
||
223 |
if (p_schemeSpecificPart == null |
|
224 |
|| p_schemeSpecificPart.trim().length() == 0) |
|
225 |
{ |
|
226 |
throw new MalformedURIException( |
|
227 |
"Cannot construct URI with null/empty scheme-specific part!"); |
|
228 |
} |
|
229 |
||
230 |
setScheme(p_scheme); |
|
231 |
setPath(p_schemeSpecificPart); |
|
232 |
} |
|
233 |
||
234 |
/** |
|
235 |
* Construct a new URI that follows the generic URI syntax from its |
|
236 |
* component parts. Each component is validated for syntax and some |
|
237 |
* basic semantic checks are performed as well. See the individual |
|
238 |
* setter methods for specifics. |
|
239 |
* |
|
240 |
* @param p_scheme the URI scheme (cannot be null or empty) |
|
241 |
* @param p_host the hostname or IPv4 address for the URI |
|
242 |
* @param p_path the URI path - if the path contains '?' or '#', |
|
243 |
* then the query string and/or fragment will be |
|
244 |
* set from the path; however, if the query and |
|
245 |
* fragment are specified both in the path and as |
|
246 |
* separate parameters, an exception is thrown |
|
247 |
* @param p_queryString the URI query string (cannot be specified |
|
248 |
* if path is null) |
|
249 |
* @param p_fragment the URI fragment (cannot be specified if path |
|
250 |
* is null) |
|
251 |
* |
|
252 |
* @throws MalformedURIException if any of the parameters violates |
|
253 |
* syntax rules or semantic rules |
|
254 |
*/ |
|
255 |
public URI(String p_scheme, String p_host, String p_path, String p_queryString, String p_fragment) |
|
256 |
throws MalformedURIException |
|
257 |
{ |
|
258 |
this(p_scheme, null, p_host, -1, p_path, p_queryString, p_fragment); |
|
259 |
} |
|
260 |
||
261 |
/** |
|
262 |
* Construct a new URI that follows the generic URI syntax from its |
|
263 |
* component parts. Each component is validated for syntax and some |
|
264 |
* basic semantic checks are performed as well. See the individual |
|
265 |
* setter methods for specifics. |
|
266 |
* |
|
267 |
* @param p_scheme the URI scheme (cannot be null or empty) |
|
268 |
* @param p_userinfo the URI userinfo (cannot be specified if host |
|
269 |
* is null) |
|
270 |
* @param p_host the hostname or IPv4 address for the URI |
|
271 |
* @param p_port the URI port (may be -1 for "unspecified"; cannot |
|
272 |
* be specified if host is null) |
|
273 |
* @param p_path the URI path - if the path contains '?' or '#', |
|
274 |
* then the query string and/or fragment will be |
|
275 |
* set from the path; however, if the query and |
|
276 |
* fragment are specified both in the path and as |
|
277 |
* separate parameters, an exception is thrown |
|
278 |
* @param p_queryString the URI query string (cannot be specified |
|
279 |
* if path is null) |
|
280 |
* @param p_fragment the URI fragment (cannot be specified if path |
|
281 |
* is null) |
|
282 |
* |
|
283 |
* @throws MalformedURIException if any of the parameters violates |
|
284 |
* syntax rules or semantic rules |
|
285 |
*/ |
|
286 |
public URI(String p_scheme, String p_userinfo, String p_host, int p_port, String p_path, String p_queryString, String p_fragment) |
|
287 |
throws MalformedURIException |
|
288 |
{ |
|
289 |
||
290 |
if (p_scheme == null || p_scheme.trim().length() == 0) |
|
291 |
{ |
|
292 |
throw new MalformedURIException(XMLMessages.createXMLMessage(XMLErrorResources.ER_SCHEME_REQUIRED, null)); //"Scheme is required!"); |
|
293 |
} |
|
294 |
||
295 |
if (p_host == null) |
|
296 |
{ |
|
297 |
if (p_userinfo != null) |
|
298 |
{ |
|
299 |
throw new MalformedURIException( |
|
300 |
XMLMessages.createXMLMessage(XMLErrorResources.ER_NO_USERINFO_IF_NO_HOST, null)); //"Userinfo may not be specified if host is not specified!"); |
|
301 |
} |
|
302 |
||
303 |
if (p_port != -1) |
|
304 |
{ |
|
305 |
throw new MalformedURIException( |
|
306 |
XMLMessages.createXMLMessage(XMLErrorResources.ER_NO_PORT_IF_NO_HOST, null)); //"Port may not be specified if host is not specified!"); |
|
307 |
} |
|
308 |
} |
|
309 |
||
310 |
if (p_path != null) |
|
311 |
{ |
|
312 |
if (p_path.indexOf('?') != -1 && p_queryString != null) |
|
313 |
{ |
|
314 |
throw new MalformedURIException( |
|
315 |
XMLMessages.createXMLMessage(XMLErrorResources.ER_NO_QUERY_STRING_IN_PATH, null)); //"Query string cannot be specified in path and query string!"); |
|
316 |
} |
|
317 |
||
318 |
if (p_path.indexOf('#') != -1 && p_fragment != null) |
|
319 |
{ |
|
320 |
throw new MalformedURIException( |
|
321 |
XMLMessages.createXMLMessage(XMLErrorResources.ER_NO_FRAGMENT_STRING_IN_PATH, null)); //"Fragment cannot be specified in both the path and fragment!"); |
|
322 |
} |
|
323 |
} |
|
324 |
||
325 |
setScheme(p_scheme); |
|
326 |
setHost(p_host); |
|
327 |
setPort(p_port); |
|
328 |
setUserinfo(p_userinfo); |
|
329 |
setPath(p_path); |
|
330 |
setQueryString(p_queryString); |
|
331 |
setFragment(p_fragment); |
|
332 |
} |
|
333 |
||
334 |
/** |
|
335 |
* Initialize all fields of this URI from another URI. |
|
336 |
* |
|
337 |
* @param p_other the URI to copy (cannot be null) |
|
338 |
*/ |
|
339 |
private void initialize(URI p_other) |
|
340 |
{ |
|
341 |
||
342 |
m_scheme = p_other.getScheme(); |
|
343 |
m_userinfo = p_other.getUserinfo(); |
|
344 |
m_host = p_other.getHost(); |
|
345 |
m_port = p_other.getPort(); |
|
346 |
m_path = p_other.getPath(); |
|
347 |
m_queryString = p_other.getQueryString(); |
|
348 |
m_fragment = p_other.getFragment(); |
|
349 |
} |
|
350 |
||
351 |
/** |
|
352 |
* Initializes this URI from a base URI and a URI specification string. |
|
353 |
* See RFC 2396 Section 4 and Appendix B for specifications on parsing |
|
354 |
* the URI and Section 5 for specifications on resolving relative URIs |
|
355 |
* and relative paths. |
|
356 |
* |
|
357 |
* @param p_base the base URI (may be null if p_uriSpec is an absolute |
|
358 |
* URI) |
|
359 |
* @param p_uriSpec the URI spec string which may be an absolute or |
|
360 |
* relative URI (can only be null/empty if p_base |
|
361 |
* is not null) |
|
362 |
* |
|
363 |
* @throws MalformedURIException if p_base is null and p_uriSpec |
|
364 |
* is not an absolute URI or if |
|
365 |
* p_uriSpec violates syntax rules |
|
366 |
*/ |
|
367 |
private void initialize(URI p_base, String p_uriSpec) |
|
368 |
throws MalformedURIException |
|
369 |
{ |
|
370 |
||
371 |
if (p_base == null |
|
372 |
&& (p_uriSpec == null || p_uriSpec.trim().length() == 0)) |
|
373 |
{ |
|
374 |
throw new MalformedURIException( |
|
375 |
XMLMessages.createXMLMessage(XMLErrorResources.ER_CANNOT_INIT_URI_EMPTY_PARMS, null)); //"Cannot initialize URI with empty parameters."); |
|
376 |
} |
|
377 |
||
378 |
// just make a copy of the base if spec is empty |
|
379 |
if (p_uriSpec == null || p_uriSpec.trim().length() == 0) |
|
380 |
{ |
|
381 |
initialize(p_base); |
|
382 |
||
383 |
return; |
|
384 |
} |
|
385 |
||
386 |
String uriSpec = p_uriSpec.trim(); |
|
387 |
int uriSpecLen = uriSpec.length(); |
|
388 |
int index = 0; |
|
389 |
||
390 |
// check for scheme |
|
391 |
int colonIndex = uriSpec.indexOf(':'); |
|
392 |
if (colonIndex < 0) |
|
393 |
{ |
|
394 |
if (p_base == null) |
|
395 |
{ |
|
396 |
throw new MalformedURIException(XMLMessages.createXMLMessage(XMLErrorResources.ER_NO_SCHEME_IN_URI, new Object[]{uriSpec})); //"No scheme found in URI: "+uriSpec); |
|
397 |
} |
|
398 |
} |
|
399 |
else |
|
400 |
{ |
|
401 |
initializeScheme(uriSpec); |
|
402 |
uriSpec = uriSpec.substring(colonIndex+1); |
|
403 |
// This is a fix for XALANJ-2059. |
|
404 |
if(m_scheme != null && p_base != null) |
|
405 |
{ |
|
406 |
// a) If <uriSpec> starts with a slash (/), it means <uriSpec> is absolute |
|
407 |
// and p_base can be ignored. |
|
408 |
// For example, |
|
409 |
// uriSpec = file:/myDIR/myXSLFile.xsl |
|
410 |
// p_base = file:/myWork/ |
|
411 |
// |
|
412 |
// Here, uriSpec has absolute path after scheme file and : |
|
413 |
// Hence p_base can be ignored. |
|
414 |
// |
|
415 |
// b) Similarily, according to RFC 2396, uri is resolved for <uriSpec> relative to <p_base> |
|
416 |
// if scheme in <uriSpec> is same as scheme in <p_base>, else p_base can be ignored. |
|
417 |
// |
|
418 |
// c) if <p_base> is not hierarchical, it can be ignored. |
|
419 |
// |
|
420 |
if(uriSpec.startsWith("/") || !m_scheme.equals(p_base.m_scheme) || !p_base.getSchemeSpecificPart().startsWith("/")) |
|
421 |
{ |
|
422 |
p_base = null; |
|
423 |
} |
|
424 |
} |
|
425 |
// Fix for XALANJ-2059 |
|
426 |
uriSpecLen = uriSpec.length(); |
|
427 |
} |
|
428 |
||
429 |
// two slashes means generic URI syntax, so we get the authority |
|
430 |
if (((index + 1) < uriSpecLen) |
|
431 |
&& (uriSpec.substring(index).startsWith("//"))) |
|
432 |
{ |
|
433 |
index += 2; |
|
434 |
||
435 |
int startPos = index; |
|
436 |
||
437 |
// get authority - everything up to path, query or fragment |
|
438 |
char testChar = '\0'; |
|
439 |
||
440 |
while (index < uriSpecLen) |
|
441 |
{ |
|
442 |
testChar = uriSpec.charAt(index); |
|
443 |
||
444 |
if (testChar == '/' || testChar == '?' || testChar == '#') |
|
445 |
{ |
|
446 |
break; |
|
447 |
} |
|
448 |
||
449 |
index++; |
|
450 |
} |
|
451 |
||
452 |
// if we found authority, parse it out, otherwise we set the |
|
453 |
// host to empty string |
|
454 |
if (index > startPos) |
|
455 |
{ |
|
456 |
initializeAuthority(uriSpec.substring(startPos, index)); |
|
457 |
} |
|
458 |
else |
|
459 |
{ |
|
460 |
m_host = ""; |
|
461 |
} |
|
462 |
} |
|
463 |
||
464 |
initializePath(uriSpec.substring(index)); |
|
465 |
||
466 |
// Resolve relative URI to base URI - see RFC 2396 Section 5.2 |
|
467 |
// In some cases, it might make more sense to throw an exception |
|
468 |
// (when scheme is specified is the string spec and the base URI |
|
469 |
// is also specified, for example), but we're just following the |
|
470 |
// RFC specifications |
|
471 |
if (p_base != null) |
|
472 |
{ |
|
473 |
||
474 |
// check to see if this is the current doc - RFC 2396 5.2 #2 |
|
475 |
// note that this is slightly different from the RFC spec in that |
|
476 |
// we don't include the check for query string being null |
|
477 |
// - this handles cases where the urispec is just a query |
|
478 |
// string or a fragment (e.g. "?y" or "#s") - |
|
479 |
// see <http://www.ics.uci.edu/~fielding/url/test1.html> which |
|
480 |
// identified this as a bug in the RFC |
|
481 |
if (m_path.length() == 0 && m_scheme == null && m_host == null) |
|
482 |
{ |
|
483 |
m_scheme = p_base.getScheme(); |
|
484 |
m_userinfo = p_base.getUserinfo(); |
|
485 |
m_host = p_base.getHost(); |
|
486 |
m_port = p_base.getPort(); |
|
487 |
m_path = p_base.getPath(); |
|
488 |
||
489 |
if (m_queryString == null) |
|
490 |
{ |
|
491 |
m_queryString = p_base.getQueryString(); |
|
492 |
} |
|
493 |
||
494 |
return; |
|
495 |
} |
|
496 |
||
497 |
// check for scheme - RFC 2396 5.2 #3 |
|
498 |
// if we found a scheme, it means absolute URI, so we're done |
|
499 |
if (m_scheme == null) |
|
500 |
{ |
|
501 |
m_scheme = p_base.getScheme(); |
|
502 |
} |
|
503 |
||
504 |
// check for authority - RFC 2396 5.2 #4 |
|
505 |
// if we found a host, then we've got a network path, so we're done |
|
506 |
if (m_host == null) |
|
507 |
{ |
|
508 |
m_userinfo = p_base.getUserinfo(); |
|
509 |
m_host = p_base.getHost(); |
|
510 |
m_port = p_base.getPort(); |
|
511 |
} |
|
512 |
else |
|
513 |
{ |
|
514 |
return; |
|
515 |
} |
|
516 |
||
517 |
// check for absolute path - RFC 2396 5.2 #5 |
|
518 |
if (m_path.length() > 0 && m_path.startsWith("/")) |
|
519 |
{ |
|
520 |
return; |
|
521 |
} |
|
522 |
||
523 |
// if we get to this point, we need to resolve relative path |
|
524 |
// RFC 2396 5.2 #6 |
|
12458 | 525 |
String path = ""; |
6 | 526 |
String basePath = p_base.getPath(); |
527 |
||
528 |
// 6a - get all but the last segment of the base URI path |
|
529 |
if (basePath != null) |
|
530 |
{ |
|
531 |
int lastSlash = basePath.lastIndexOf('/'); |
|
532 |
||
533 |
if (lastSlash != -1) |
|
534 |
{ |
|
535 |
path = basePath.substring(0, lastSlash + 1); |
|
536 |
} |
|
537 |
} |
|
538 |
||
539 |
// 6b - append the relative URI path |
|
540 |
path = path.concat(m_path); |
|
541 |
||
542 |
// 6c - remove all "./" where "." is a complete path segment |
|
543 |
index = -1; |
|
544 |
||
545 |
while ((index = path.indexOf("/./")) != -1) |
|
546 |
{ |
|
547 |
path = path.substring(0, index + 1).concat(path.substring(index + 3)); |
|
548 |
} |
|
549 |
||
550 |
// 6d - remove "." if path ends with "." as a complete path segment |
|
551 |
if (path.endsWith("/.")) |
|
552 |
{ |
|
553 |
path = path.substring(0, path.length() - 1); |
|
554 |
} |
|
555 |
||
556 |
// 6e - remove all "<segment>/../" where "<segment>" is a complete |
|
557 |
// path segment not equal to ".." |
|
558 |
index = -1; |
|
559 |
||
560 |
int segIndex = -1; |
|
561 |
String tempString = null; |
|
562 |
||
563 |
while ((index = path.indexOf("/../")) > 0) |
|
564 |
{ |
|
565 |
tempString = path.substring(0, path.indexOf("/../")); |
|
566 |
segIndex = tempString.lastIndexOf('/'); |
|
567 |
||
568 |
if (segIndex != -1) |
|
569 |
{ |
|
570 |
if (!tempString.substring(segIndex++).equals("..")) |
|
571 |
{ |
|
572 |
path = path.substring(0, segIndex).concat(path.substring(index |
|
573 |
+ 4)); |
|
574 |
} |
|
575 |
} |
|
576 |
} |
|
577 |
||
578 |
// 6f - remove ending "<segment>/.." where "<segment>" is a |
|
579 |
// complete path segment |
|
580 |
if (path.endsWith("/..")) |
|
581 |
{ |
|
582 |
tempString = path.substring(0, path.length() - 3); |
|
583 |
segIndex = tempString.lastIndexOf('/'); |
|
584 |
||
585 |
if (segIndex != -1) |
|
586 |
{ |
|
587 |
path = path.substring(0, segIndex + 1); |
|
588 |
} |
|
589 |
} |
|
590 |
||
591 |
m_path = path; |
|
592 |
} |
|
593 |
} |
|
594 |
||
595 |
/** |
|
596 |
* Initialize the scheme for this URI from a URI string spec. |
|
597 |
* |
|
598 |
* @param p_uriSpec the URI specification (cannot be null) |
|
599 |
* |
|
600 |
* @throws MalformedURIException if URI does not have a conformant |
|
601 |
* scheme |
|
602 |
*/ |
|
603 |
private void initializeScheme(String p_uriSpec) throws MalformedURIException |
|
604 |
{ |
|
605 |
||
606 |
int uriSpecLen = p_uriSpec.length(); |
|
607 |
int index = 0; |
|
608 |
String scheme = null; |
|
609 |
char testChar = '\0'; |
|
610 |
||
611 |
while (index < uriSpecLen) |
|
612 |
{ |
|
613 |
testChar = p_uriSpec.charAt(index); |
|
614 |
||
615 |
if (testChar == ':' || testChar == '/' || testChar == '?' |
|
616 |
|| testChar == '#') |
|
617 |
{ |
|
618 |
break; |
|
619 |
} |
|
620 |
||
621 |
index++; |
|
622 |
} |
|
623 |
||
624 |
scheme = p_uriSpec.substring(0, index); |
|
625 |
||
626 |
if (scheme.length() == 0) |
|
627 |
{ |
|
628 |
throw new MalformedURIException(XMLMessages.createXMLMessage(XMLErrorResources.ER_NO_SCHEME_INURI, null)); //"No scheme found in URI."); |
|
629 |
} |
|
630 |
else |
|
631 |
{ |
|
632 |
setScheme(scheme); |
|
633 |
} |
|
634 |
} |
|
635 |
||
636 |
/** |
|
637 |
* Initialize the authority (userinfo, host and port) for this |
|
638 |
* URI from a URI string spec. |
|
639 |
* |
|
640 |
* @param p_uriSpec the URI specification (cannot be null) |
|
641 |
* |
|
642 |
* @throws MalformedURIException if p_uriSpec violates syntax rules |
|
643 |
*/ |
|
644 |
private void initializeAuthority(String p_uriSpec) |
|
645 |
throws MalformedURIException |
|
646 |
{ |
|
647 |
||
648 |
int index = 0; |
|
649 |
int start = 0; |
|
650 |
int end = p_uriSpec.length(); |
|
651 |
char testChar = '\0'; |
|
652 |
String userinfo = null; |
|
653 |
||
654 |
// userinfo is everything up @ |
|
655 |
if (p_uriSpec.indexOf('@', start) != -1) |
|
656 |
{ |
|
657 |
while (index < end) |
|
658 |
{ |
|
659 |
testChar = p_uriSpec.charAt(index); |
|
660 |
||
661 |
if (testChar == '@') |
|
662 |
{ |
|
663 |
break; |
|
664 |
} |
|
665 |
||
666 |
index++; |
|
667 |
} |
|
668 |
||
669 |
userinfo = p_uriSpec.substring(start, index); |
|
670 |
||
671 |
index++; |
|
672 |
} |
|
673 |
||
674 |
// host is everything up to ':' |
|
675 |
String host = null; |
|
676 |
||
677 |
start = index; |
|
678 |
||
679 |
while (index < end) |
|
680 |
{ |
|
681 |
testChar = p_uriSpec.charAt(index); |
|
682 |
||
683 |
if (testChar == ':') |
|
684 |
{ |
|
685 |
break; |
|
686 |
} |
|
687 |
||
688 |
index++; |
|
689 |
} |
|
690 |
||
691 |
host = p_uriSpec.substring(start, index); |
|
692 |
||
693 |
int port = -1; |
|
694 |
||
695 |
if (host.length() > 0) |
|
696 |
{ |
|
697 |
||
698 |
// port |
|
699 |
if (testChar == ':') |
|
700 |
{ |
|
701 |
index++; |
|
702 |
||
703 |
start = index; |
|
704 |
||
705 |
while (index < end) |
|
706 |
{ |
|
707 |
index++; |
|
708 |
} |
|
709 |
||
710 |
String portStr = p_uriSpec.substring(start, index); |
|
711 |
||
712 |
if (portStr.length() > 0) |
|
713 |
{ |
|
714 |
for (int i = 0; i < portStr.length(); i++) |
|
715 |
{ |
|
716 |
if (!isDigit(portStr.charAt(i))) |
|
717 |
{ |
|
718 |
throw new MalformedURIException( |
|
719 |
portStr + " is invalid. Port should only contain digits!"); |
|
720 |
} |
|
721 |
} |
|
722 |
||
723 |
try |
|
724 |
{ |
|
725 |
port = Integer.parseInt(portStr); |
|
726 |
} |
|
727 |
catch (NumberFormatException nfe) |
|
728 |
{ |
|
729 |
||
730 |
// can't happen |
|
731 |
} |
|
732 |
} |
|
733 |
} |
|
734 |
} |
|
735 |
||
736 |
setHost(host); |
|
737 |
setPort(port); |
|
738 |
setUserinfo(userinfo); |
|
739 |
} |
|
740 |
||
741 |
/** |
|
742 |
* Initialize the path for this URI from a URI string spec. |
|
743 |
* |
|
744 |
* @param p_uriSpec the URI specification (cannot be null) |
|
745 |
* |
|
746 |
* @throws MalformedURIException if p_uriSpec violates syntax rules |
|
747 |
*/ |
|
748 |
private void initializePath(String p_uriSpec) throws MalformedURIException |
|
749 |
{ |
|
750 |
||
751 |
if (p_uriSpec == null) |
|
752 |
{ |
|
753 |
throw new MalformedURIException( |
|
754 |
"Cannot initialize path from null string!"); |
|
755 |
} |
|
756 |
||
757 |
int index = 0; |
|
758 |
int start = 0; |
|
759 |
int end = p_uriSpec.length(); |
|
760 |
char testChar = '\0'; |
|
761 |
||
762 |
// path - everything up to query string or fragment |
|
763 |
while (index < end) |
|
764 |
{ |
|
765 |
testChar = p_uriSpec.charAt(index); |
|
766 |
||
767 |
if (testChar == '?' || testChar == '#') |
|
768 |
{ |
|
769 |
break; |
|
770 |
} |
|
771 |
||
772 |
// check for valid escape sequence |
|
773 |
if (testChar == '%') |
|
774 |
{ |
|
775 |
if (index + 2 >= end ||!isHex(p_uriSpec.charAt(index + 1)) |
|
776 |
||!isHex(p_uriSpec.charAt(index + 2))) |
|
777 |
{ |
|
778 |
throw new MalformedURIException( |
|
779 |
XMLMessages.createXMLMessage(XMLErrorResources.ER_PATH_CONTAINS_INVALID_ESCAPE_SEQUENCE, null)); //"Path contains invalid escape sequence!"); |
|
780 |
} |
|
781 |
} |
|
782 |
else if (!isReservedCharacter(testChar) |
|
783 |
&&!isUnreservedCharacter(testChar)) |
|
784 |
{ |
|
785 |
if ('\\' != testChar) |
|
786 |
throw new MalformedURIException(XMLMessages.createXMLMessage(XMLErrorResources.ER_PATH_INVALID_CHAR, new Object[]{String.valueOf(testChar)})); //"Path contains invalid character: " |
|
787 |
//+ testChar); |
|
788 |
} |
|
789 |
||
790 |
index++; |
|
791 |
} |
|
792 |
||
793 |
m_path = p_uriSpec.substring(start, index); |
|
794 |
||
795 |
// query - starts with ? and up to fragment or end |
|
796 |
if (testChar == '?') |
|
797 |
{ |
|
798 |
index++; |
|
799 |
||
800 |
start = index; |
|
801 |
||
802 |
while (index < end) |
|
803 |
{ |
|
804 |
testChar = p_uriSpec.charAt(index); |
|
805 |
||
806 |
if (testChar == '#') |
|
807 |
{ |
|
808 |
break; |
|
809 |
} |
|
810 |
||
811 |
if (testChar == '%') |
|
812 |
{ |
|
813 |
if (index + 2 >= end ||!isHex(p_uriSpec.charAt(index + 1)) |
|
814 |
||!isHex(p_uriSpec.charAt(index + 2))) |
|
815 |
{ |
|
816 |
throw new MalformedURIException( |
|
817 |
"Query string contains invalid escape sequence!"); |
|
818 |
} |
|
819 |
} |
|
820 |
else if (!isReservedCharacter(testChar) |
|
821 |
&&!isUnreservedCharacter(testChar)) |
|
822 |
{ |
|
823 |
throw new MalformedURIException( |
|
824 |
"Query string contains invalid character:" + testChar); |
|
825 |
} |
|
826 |
||
827 |
index++; |
|
828 |
} |
|
829 |
||
830 |
m_queryString = p_uriSpec.substring(start, index); |
|
831 |
} |
|
832 |
||
833 |
// fragment - starts with # |
|
834 |
if (testChar == '#') |
|
835 |
{ |
|
836 |
index++; |
|
837 |
||
838 |
start = index; |
|
839 |
||
840 |
while (index < end) |
|
841 |
{ |
|
842 |
testChar = p_uriSpec.charAt(index); |
|
843 |
||
844 |
if (testChar == '%') |
|
845 |
{ |
|
846 |
if (index + 2 >= end ||!isHex(p_uriSpec.charAt(index + 1)) |
|
847 |
||!isHex(p_uriSpec.charAt(index + 2))) |
|
848 |
{ |
|
849 |
throw new MalformedURIException( |
|
850 |
"Fragment contains invalid escape sequence!"); |
|
851 |
} |
|
852 |
} |
|
853 |
else if (!isReservedCharacter(testChar) |
|
854 |
&&!isUnreservedCharacter(testChar)) |
|
855 |
{ |
|
856 |
throw new MalformedURIException( |
|
857 |
"Fragment contains invalid character:" + testChar); |
|
858 |
} |
|
859 |
||
860 |
index++; |
|
861 |
} |
|
862 |
||
863 |
m_fragment = p_uriSpec.substring(start, index); |
|
864 |
} |
|
865 |
} |
|
866 |
||
867 |
/** |
|
868 |
* Get the scheme for this URI. |
|
869 |
* |
|
870 |
* @return the scheme for this URI |
|
871 |
*/ |
|
872 |
public String getScheme() |
|
873 |
{ |
|
874 |
return m_scheme; |
|
875 |
} |
|
876 |
||
877 |
/** |
|
878 |
* Get the scheme-specific part for this URI (everything following the |
|
879 |
* scheme and the first colon). See RFC 2396 Section 5.2 for spec. |
|
880 |
* |
|
881 |
* @return the scheme-specific part for this URI |
|
882 |
*/ |
|
883 |
public String getSchemeSpecificPart() |
|
884 |
{ |
|
885 |
||
17538 | 886 |
final StringBuilder schemespec = new StringBuilder(); |
6 | 887 |
|
888 |
if (m_userinfo != null || m_host != null || m_port != -1) |
|
889 |
{ |
|
890 |
schemespec.append("//"); |
|
891 |
} |
|
892 |
||
893 |
if (m_userinfo != null) |
|
894 |
{ |
|
895 |
schemespec.append(m_userinfo); |
|
896 |
schemespec.append('@'); |
|
897 |
} |
|
898 |
||
899 |
if (m_host != null) |
|
900 |
{ |
|
901 |
schemespec.append(m_host); |
|
902 |
} |
|
903 |
||
904 |
if (m_port != -1) |
|
905 |
{ |
|
906 |
schemespec.append(':'); |
|
907 |
schemespec.append(m_port); |
|
908 |
} |
|
909 |
||
910 |
if (m_path != null) |
|
911 |
{ |
|
912 |
schemespec.append((m_path)); |
|
913 |
} |
|
914 |
||
915 |
if (m_queryString != null) |
|
916 |
{ |
|
917 |
schemespec.append('?'); |
|
918 |
schemespec.append(m_queryString); |
|
919 |
} |
|
920 |
||
921 |
if (m_fragment != null) |
|
922 |
{ |
|
923 |
schemespec.append('#'); |
|
924 |
schemespec.append(m_fragment); |
|
925 |
} |
|
926 |
||
927 |
return schemespec.toString(); |
|
928 |
} |
|
929 |
||
930 |
/** |
|
931 |
* Get the userinfo for this URI. |
|
932 |
* |
|
933 |
* @return the userinfo for this URI (null if not specified). |
|
934 |
*/ |
|
935 |
public String getUserinfo() |
|
936 |
{ |
|
937 |
return m_userinfo; |
|
938 |
} |
|
939 |
||
940 |
/** |
|
941 |
* Get the host for this URI. |
|
942 |
* |
|
943 |
* @return the host for this URI (null if not specified). |
|
944 |
*/ |
|
945 |
public String getHost() |
|
946 |
{ |
|
947 |
return m_host; |
|
948 |
} |
|
949 |
||
950 |
/** |
|
951 |
* Get the port for this URI. |
|
952 |
* |
|
953 |
* @return the port for this URI (-1 if not specified). |
|
954 |
*/ |
|
955 |
public int getPort() |
|
956 |
{ |
|
957 |
return m_port; |
|
958 |
} |
|
959 |
||
960 |
/** |
|
961 |
* Get the path for this URI (optionally with the query string and |
|
962 |
* fragment). |
|
963 |
* |
|
964 |
* @param p_includeQueryString if true (and query string is not null), |
|
965 |
* then a "?" followed by the query string |
|
966 |
* will be appended |
|
967 |
* @param p_includeFragment if true (and fragment is not null), |
|
968 |
* then a "#" followed by the fragment |
|
969 |
* will be appended |
|
970 |
* |
|
971 |
* @return the path for this URI possibly including the query string |
|
972 |
* and fragment |
|
973 |
*/ |
|
974 |
public String getPath(boolean p_includeQueryString, |
|
975 |
boolean p_includeFragment) |
|
976 |
{ |
|
977 |
||
17538 | 978 |
final StringBuilder pathString = new StringBuilder(m_path); |
6 | 979 |
|
980 |
if (p_includeQueryString && m_queryString != null) |
|
981 |
{ |
|
982 |
pathString.append('?'); |
|
983 |
pathString.append(m_queryString); |
|
984 |
} |
|
985 |
||
986 |
if (p_includeFragment && m_fragment != null) |
|
987 |
{ |
|
988 |
pathString.append('#'); |
|
989 |
pathString.append(m_fragment); |
|
990 |
} |
|
991 |
||
992 |
return pathString.toString(); |
|
993 |
} |
|
994 |
||
995 |
/** |
|
996 |
* Get the path for this URI. Note that the value returned is the path |
|
997 |
* only and does not include the query string or fragment. |
|
998 |
* |
|
999 |
* @return the path for this URI. |
|
1000 |
*/ |
|
1001 |
public String getPath() |
|
1002 |
{ |
|
1003 |
return m_path; |
|
1004 |
} |
|
1005 |
||
1006 |
/** |
|
1007 |
* Get the query string for this URI. |
|
1008 |
* |
|
1009 |
* @return the query string for this URI. Null is returned if there |
|
1010 |
* was no "?" in the URI spec, empty string if there was a |
|
1011 |
* "?" but no query string following it. |
|
1012 |
*/ |
|
1013 |
public String getQueryString() |
|
1014 |
{ |
|
1015 |
return m_queryString; |
|
1016 |
} |
|
1017 |
||
1018 |
/** |
|
1019 |
* Get the fragment for this URI. |
|
1020 |
* |
|
1021 |
* @return the fragment for this URI. Null is returned if there |
|
1022 |
* was no "#" in the URI spec, empty string if there was a |
|
1023 |
* "#" but no fragment following it. |
|
1024 |
*/ |
|
1025 |
public String getFragment() |
|
1026 |
{ |
|
1027 |
return m_fragment; |
|
1028 |
} |
|
1029 |
||
1030 |
/** |
|
1031 |
* Set the scheme for this URI. The scheme is converted to lowercase |
|
1032 |
* before it is set. |
|
1033 |
* |
|
1034 |
* @param p_scheme the scheme for this URI (cannot be null) |
|
1035 |
* |
|
1036 |
* @throws MalformedURIException if p_scheme is not a conformant |
|
1037 |
* scheme name |
|
1038 |
*/ |
|
1039 |
public void setScheme(String p_scheme) throws MalformedURIException |
|
1040 |
{ |
|
1041 |
||
1042 |
if (p_scheme == null) |
|
1043 |
{ |
|
1044 |
throw new MalformedURIException(XMLMessages.createXMLMessage(XMLErrorResources.ER_SCHEME_FROM_NULL_STRING, null)); //"Cannot set scheme from null string!"); |
|
1045 |
} |
|
1046 |
||
1047 |
if (!isConformantSchemeName(p_scheme)) |
|
1048 |
{ |
|
1049 |
throw new MalformedURIException(XMLMessages.createXMLMessage(XMLErrorResources.ER_SCHEME_NOT_CONFORMANT, null)); //"The scheme is not conformant."); |
|
1050 |
} |
|
1051 |
||
1052 |
m_scheme = p_scheme.toLowerCase(); |
|
1053 |
} |
|
1054 |
||
1055 |
/** |
|
1056 |
* Set the userinfo for this URI. If a non-null value is passed in and |
|
1057 |
* the host value is null, then an exception is thrown. |
|
1058 |
* |
|
1059 |
* @param p_userinfo the userinfo for this URI |
|
1060 |
* |
|
1061 |
* @throws MalformedURIException if p_userinfo contains invalid |
|
1062 |
* characters |
|
1063 |
*/ |
|
1064 |
public void setUserinfo(String p_userinfo) throws MalformedURIException |
|
1065 |
{ |
|
1066 |
||
1067 |
if (p_userinfo == null) |
|
1068 |
{ |
|
1069 |
m_userinfo = null; |
|
1070 |
} |
|
1071 |
else |
|
1072 |
{ |
|
1073 |
if (m_host == null) |
|
1074 |
{ |
|
1075 |
throw new MalformedURIException( |
|
1076 |
"Userinfo cannot be set when host is null!"); |
|
1077 |
} |
|
1078 |
||
1079 |
// userinfo can contain alphanumerics, mark characters, escaped |
|
1080 |
// and ';',':','&','=','+','$',',' |
|
1081 |
int index = 0; |
|
1082 |
int end = p_userinfo.length(); |
|
1083 |
char testChar = '\0'; |
|
1084 |
||
1085 |
while (index < end) |
|
1086 |
{ |
|
1087 |
testChar = p_userinfo.charAt(index); |
|
1088 |
||
1089 |
if (testChar == '%') |
|
1090 |
{ |
|
1091 |
if (index + 2 >= end ||!isHex(p_userinfo.charAt(index + 1)) |
|
1092 |
||!isHex(p_userinfo.charAt(index + 2))) |
|
1093 |
{ |
|
1094 |
throw new MalformedURIException( |
|
1095 |
"Userinfo contains invalid escape sequence!"); |
|
1096 |
} |
|
1097 |
} |
|
1098 |
else if (!isUnreservedCharacter(testChar) |
|
1099 |
&& USERINFO_CHARACTERS.indexOf(testChar) == -1) |
|
1100 |
{ |
|
1101 |
throw new MalformedURIException( |
|
1102 |
"Userinfo contains invalid character:" + testChar); |
|
1103 |
} |
|
1104 |
||
1105 |
index++; |
|
1106 |
} |
|
1107 |
} |
|
1108 |
||
1109 |
m_userinfo = p_userinfo; |
|
1110 |
} |
|
1111 |
||
1112 |
/** |
|
1113 |
* Set the host for this URI. If null is passed in, the userinfo |
|
1114 |
* field is also set to null and the port is set to -1. |
|
1115 |
* |
|
1116 |
* @param p_host the host for this URI |
|
1117 |
* |
|
1118 |
* @throws MalformedURIException if p_host is not a valid IP |
|
1119 |
* address or DNS hostname. |
|
1120 |
*/ |
|
1121 |
public void setHost(String p_host) throws MalformedURIException |
|
1122 |
{ |
|
1123 |
||
1124 |
if (p_host == null || p_host.trim().length() == 0) |
|
1125 |
{ |
|
1126 |
m_host = p_host; |
|
1127 |
m_userinfo = null; |
|
1128 |
m_port = -1; |
|
1129 |
} |
|
1130 |
else if (!isWellFormedAddress(p_host)) |
|
1131 |
{ |
|
1132 |
throw new MalformedURIException(XMLMessages.createXMLMessage(XMLErrorResources.ER_HOST_ADDRESS_NOT_WELLFORMED, null)); //"Host is not a well formed address!"); |
|
1133 |
} |
|
1134 |
||
1135 |
m_host = p_host; |
|
1136 |
} |
|
1137 |
||
1138 |
/** |
|
1139 |
* Set the port for this URI. -1 is used to indicate that the port is |
|
1140 |
* not specified, otherwise valid port numbers are between 0 and 65535. |
|
1141 |
* If a valid port number is passed in and the host field is null, |
|
1142 |
* an exception is thrown. |
|
1143 |
* |
|
1144 |
* @param p_port the port number for this URI |
|
1145 |
* |
|
1146 |
* @throws MalformedURIException if p_port is not -1 and not a |
|
1147 |
* valid port number |
|
1148 |
*/ |
|
1149 |
public void setPort(int p_port) throws MalformedURIException |
|
1150 |
{ |
|
1151 |
||
1152 |
if (p_port >= 0 && p_port <= 65535) |
|
1153 |
{ |
|
1154 |
if (m_host == null) |
|
1155 |
{ |
|
1156 |
throw new MalformedURIException( |
|
1157 |
XMLMessages.createXMLMessage(XMLErrorResources.ER_PORT_WHEN_HOST_NULL, null)); //"Port cannot be set when host is null!"); |
|
1158 |
} |
|
1159 |
} |
|
1160 |
else if (p_port != -1) |
|
1161 |
{ |
|
1162 |
throw new MalformedURIException(XMLMessages.createXMLMessage(XMLErrorResources.ER_INVALID_PORT, null)); //"Invalid port number!"); |
|
1163 |
} |
|
1164 |
||
1165 |
m_port = p_port; |
|
1166 |
} |
|
1167 |
||
1168 |
/** |
|
1169 |
* Set the path for this URI. If the supplied path is null, then the |
|
1170 |
* query string and fragment are set to null as well. If the supplied |
|
1171 |
* path includes a query string and/or fragment, these fields will be |
|
1172 |
* parsed and set as well. Note that, for URIs following the "generic |
|
1173 |
* URI" syntax, the path specified should start with a slash. |
|
1174 |
* For URIs that do not follow the generic URI syntax, this method |
|
1175 |
* sets the scheme-specific part. |
|
1176 |
* |
|
1177 |
* @param p_path the path for this URI (may be null) |
|
1178 |
* |
|
1179 |
* @throws MalformedURIException if p_path contains invalid |
|
1180 |
* characters |
|
1181 |
*/ |
|
1182 |
public void setPath(String p_path) throws MalformedURIException |
|
1183 |
{ |
|
1184 |
||
1185 |
if (p_path == null) |
|
1186 |
{ |
|
1187 |
m_path = null; |
|
1188 |
m_queryString = null; |
|
1189 |
m_fragment = null; |
|
1190 |
} |
|
1191 |
else |
|
1192 |
{ |
|
1193 |
initializePath(p_path); |
|
1194 |
} |
|
1195 |
} |
|
1196 |
||
1197 |
/** |
|
1198 |
* Append to the end of the path of this URI. If the current path does |
|
1199 |
* not end in a slash and the path to be appended does not begin with |
|
1200 |
* a slash, a slash will be appended to the current path before the |
|
1201 |
* new segment is added. Also, if the current path ends in a slash |
|
1202 |
* and the new segment begins with a slash, the extra slash will be |
|
1203 |
* removed before the new segment is appended. |
|
1204 |
* |
|
1205 |
* @param p_addToPath the new segment to be added to the current path |
|
1206 |
* |
|
1207 |
* @throws MalformedURIException if p_addToPath contains syntax |
|
1208 |
* errors |
|
1209 |
*/ |
|
1210 |
public void appendPath(String p_addToPath) throws MalformedURIException |
|
1211 |
{ |
|
1212 |
||
1213 |
if (p_addToPath == null || p_addToPath.trim().length() == 0) |
|
1214 |
{ |
|
1215 |
return; |
|
1216 |
} |
|
1217 |
||
1218 |
if (!isURIString(p_addToPath)) |
|
1219 |
{ |
|
1220 |
throw new MalformedURIException(XMLMessages.createXMLMessage(XMLErrorResources.ER_PATH_INVALID_CHAR, new Object[]{p_addToPath})); //"Path contains invalid character!"); |
|
1221 |
} |
|
1222 |
||
1223 |
if (m_path == null || m_path.trim().length() == 0) |
|
1224 |
{ |
|
1225 |
if (p_addToPath.startsWith("/")) |
|
1226 |
{ |
|
1227 |
m_path = p_addToPath; |
|
1228 |
} |
|
1229 |
else |
|
1230 |
{ |
|
1231 |
m_path = "/" + p_addToPath; |
|
1232 |
} |
|
1233 |
} |
|
1234 |
else if (m_path.endsWith("/")) |
|
1235 |
{ |
|
1236 |
if (p_addToPath.startsWith("/")) |
|
1237 |
{ |
|
1238 |
m_path = m_path.concat(p_addToPath.substring(1)); |
|
1239 |
} |
|
1240 |
else |
|
1241 |
{ |
|
1242 |
m_path = m_path.concat(p_addToPath); |
|
1243 |
} |
|
1244 |
} |
|
1245 |
else |
|
1246 |
{ |
|
1247 |
if (p_addToPath.startsWith("/")) |
|
1248 |
{ |
|
1249 |
m_path = m_path.concat(p_addToPath); |
|
1250 |
} |
|
1251 |
else |
|
1252 |
{ |
|
1253 |
m_path = m_path.concat("/" + p_addToPath); |
|
1254 |
} |
|
1255 |
} |
|
1256 |
} |
|
1257 |
||
1258 |
/** |
|
1259 |
* Set the query string for this URI. A non-null value is valid only |
|
1260 |
* if this is an URI conforming to the generic URI syntax and |
|
1261 |
* the path value is not null. |
|
1262 |
* |
|
1263 |
* @param p_queryString the query string for this URI |
|
1264 |
* |
|
1265 |
* @throws MalformedURIException if p_queryString is not null and this |
|
1266 |
* URI does not conform to the generic |
|
1267 |
* URI syntax or if the path is null |
|
1268 |
*/ |
|
1269 |
public void setQueryString(String p_queryString) |
|
1270 |
throws MalformedURIException |
|
1271 |
{ |
|
1272 |
||
1273 |
if (p_queryString == null) |
|
1274 |
{ |
|
1275 |
m_queryString = null; |
|
1276 |
} |
|
1277 |
else if (!isGenericURI()) |
|
1278 |
{ |
|
1279 |
throw new MalformedURIException( |
|
1280 |
"Query string can only be set for a generic URI!"); |
|
1281 |
} |
|
1282 |
else if (getPath() == null) |
|
1283 |
{ |
|
1284 |
throw new MalformedURIException( |
|
1285 |
"Query string cannot be set when path is null!"); |
|
1286 |
} |
|
1287 |
else if (!isURIString(p_queryString)) |
|
1288 |
{ |
|
1289 |
throw new MalformedURIException( |
|
1290 |
"Query string contains invalid character!"); |
|
1291 |
} |
|
1292 |
else |
|
1293 |
{ |
|
1294 |
m_queryString = p_queryString; |
|
1295 |
} |
|
1296 |
} |
|
1297 |
||
1298 |
/** |
|
1299 |
* Set the fragment for this URI. A non-null value is valid only |
|
1300 |
* if this is a URI conforming to the generic URI syntax and |
|
1301 |
* the path value is not null. |
|
1302 |
* |
|
1303 |
* @param p_fragment the fragment for this URI |
|
1304 |
* |
|
1305 |
* @throws MalformedURIException if p_fragment is not null and this |
|
1306 |
* URI does not conform to the generic |
|
1307 |
* URI syntax or if the path is null |
|
1308 |
*/ |
|
1309 |
public void setFragment(String p_fragment) throws MalformedURIException |
|
1310 |
{ |
|
1311 |
||
1312 |
if (p_fragment == null) |
|
1313 |
{ |
|
1314 |
m_fragment = null; |
|
1315 |
} |
|
1316 |
else if (!isGenericURI()) |
|
1317 |
{ |
|
1318 |
throw new MalformedURIException( |
|
1319 |
XMLMessages.createXMLMessage(XMLErrorResources.ER_FRAG_FOR_GENERIC_URI, null)); //"Fragment can only be set for a generic URI!"); |
|
1320 |
} |
|
1321 |
else if (getPath() == null) |
|
1322 |
{ |
|
1323 |
throw new MalformedURIException( |
|
1324 |
XMLMessages.createXMLMessage(XMLErrorResources.ER_FRAG_WHEN_PATH_NULL, null)); //"Fragment cannot be set when path is null!"); |
|
1325 |
} |
|
1326 |
else if (!isURIString(p_fragment)) |
|
1327 |
{ |
|
1328 |
throw new MalformedURIException(XMLMessages.createXMLMessage(XMLErrorResources.ER_FRAG_INVALID_CHAR, null)); //"Fragment contains invalid character!"); |
|
1329 |
} |
|
1330 |
else |
|
1331 |
{ |
|
1332 |
m_fragment = p_fragment; |
|
1333 |
} |
|
1334 |
} |
|
1335 |
||
1336 |
/** |
|
1337 |
* Determines if the passed-in Object is equivalent to this URI. |
|
1338 |
* |
|
1339 |
* @param p_test the Object to test for equality. |
|
1340 |
* |
|
1341 |
* @return true if p_test is a URI with all values equal to this |
|
1342 |
* URI, false otherwise |
|
1343 |
*/ |
|
17538 | 1344 |
@Override |
6 | 1345 |
public boolean equals(Object p_test) |
1346 |
{ |
|
1347 |
||
1348 |
if (p_test instanceof URI) |
|
1349 |
{ |
|
1350 |
URI testURI = (URI) p_test; |
|
1351 |
||
1352 |
if (((m_scheme == null && testURI.m_scheme == null) || (m_scheme != null && testURI.m_scheme != null && m_scheme.equals( |
|
1353 |
testURI.m_scheme))) && ((m_userinfo == null && testURI.m_userinfo == null) || (m_userinfo != null && testURI.m_userinfo != null && m_userinfo.equals( |
|
1354 |
testURI.m_userinfo))) && ((m_host == null && testURI.m_host == null) || (m_host != null && testURI.m_host != null && m_host.equals( |
|
1355 |
testURI.m_host))) && m_port == testURI.m_port && ((m_path == null && testURI.m_path == null) || (m_path != null && testURI.m_path != null && m_path.equals( |
|
1356 |
testURI.m_path))) && ((m_queryString == null && testURI.m_queryString == null) || (m_queryString != null && testURI.m_queryString != null && m_queryString.equals( |
|
1357 |
testURI.m_queryString))) && ((m_fragment == null && testURI.m_fragment == null) || (m_fragment != null && testURI.m_fragment != null && m_fragment.equals( |
|
1358 |
testURI.m_fragment)))) |
|
1359 |
{ |
|
1360 |
return true; |
|
1361 |
} |
|
1362 |
} |
|
1363 |
||
1364 |
return false; |
|
1365 |
} |
|
1366 |
||
17538 | 1367 |
@Override |
1368 |
public int hashCode() { |
|
1369 |
int hash = 7; |
|
1370 |
hash = 59 * hash + Objects.hashCode(this.m_scheme); |
|
1371 |
hash = 59 * hash + Objects.hashCode(this.m_userinfo); |
|
1372 |
hash = 59 * hash + Objects.hashCode(this.m_host); |
|
1373 |
hash = 59 * hash + this.m_port; |
|
1374 |
hash = 59 * hash + Objects.hashCode(this.m_path); |
|
1375 |
hash = 59 * hash + Objects.hashCode(this.m_queryString); |
|
1376 |
hash = 59 * hash + Objects.hashCode(this.m_fragment); |
|
1377 |
return hash; |
|
1378 |
} |
|
1379 |
||
6 | 1380 |
/** |
1381 |
* Get the URI as a string specification. See RFC 2396 Section 5.2. |
|
1382 |
* |
|
1383 |
* @return the URI string specification |
|
1384 |
*/ |
|
17538 | 1385 |
@Override |
6 | 1386 |
public String toString() |
1387 |
{ |
|
1388 |
||
17538 | 1389 |
final StringBuilder uriSpecString = new StringBuilder(); |
6 | 1390 |
|
1391 |
if (m_scheme != null) |
|
1392 |
{ |
|
1393 |
uriSpecString.append(m_scheme); |
|
1394 |
uriSpecString.append(':'); |
|
1395 |
} |
|
1396 |
||
1397 |
uriSpecString.append(getSchemeSpecificPart()); |
|
1398 |
||
1399 |
return uriSpecString.toString(); |
|
1400 |
} |
|
1401 |
||
1402 |
/** |
|
1403 |
* Get the indicator as to whether this URI uses the "generic URI" |
|
1404 |
* syntax. |
|
1405 |
* |
|
1406 |
* @return true if this URI uses the "generic URI" syntax, false |
|
1407 |
* otherwise |
|
1408 |
*/ |
|
1409 |
public boolean isGenericURI() |
|
1410 |
{ |
|
1411 |
||
1412 |
// presence of the host (whether valid or empty) means |
|
1413 |
// double-slashes which means generic uri |
|
1414 |
return (m_host != null); |
|
1415 |
} |
|
1416 |
||
1417 |
/** |
|
1418 |
* Determine whether a scheme conforms to the rules for a scheme name. |
|
1419 |
* A scheme is conformant if it starts with an alphanumeric, and |
|
1420 |
* contains only alphanumerics, '+','-' and '.'. |
|
1421 |
* |
|
1422 |
* |
|
1423 |
* @param p_scheme The sheme name to check |
|
1424 |
* @return true if the scheme is conformant, false otherwise |
|
1425 |
*/ |
|
1426 |
public static boolean isConformantSchemeName(String p_scheme) |
|
1427 |
{ |
|
1428 |
||
1429 |
if (p_scheme == null || p_scheme.trim().length() == 0) |
|
1430 |
{ |
|
1431 |
return false; |
|
1432 |
} |
|
1433 |
||
1434 |
if (!isAlpha(p_scheme.charAt(0))) |
|
1435 |
{ |
|
1436 |
return false; |
|
1437 |
} |
|
1438 |
||
1439 |
char testChar; |
|
1440 |
||
1441 |
for (int i = 1; i < p_scheme.length(); i++) |
|
1442 |
{ |
|
1443 |
testChar = p_scheme.charAt(i); |
|
1444 |
||
1445 |
if (!isAlphanum(testChar) && SCHEME_CHARACTERS.indexOf(testChar) == -1) |
|
1446 |
{ |
|
1447 |
return false; |
|
1448 |
} |
|
1449 |
} |
|
1450 |
||
1451 |
return true; |
|
1452 |
} |
|
1453 |
||
1454 |
/** |
|
1455 |
* Determine whether a string is syntactically capable of representing |
|
1456 |
* a valid IPv4 address or the domain name of a network host. A valid |
|
1457 |
* IPv4 address consists of four decimal digit groups separated by a |
|
1458 |
* '.'. A hostname consists of domain labels (each of which must |
|
1459 |
* begin and end with an alphanumeric but may contain '-') separated |
|
1460 |
* & by a '.'. See RFC 2396 Section 3.2.2. |
|
1461 |
* |
|
1462 |
* |
|
1463 |
* @param p_address The address string to check |
|
1464 |
* @return true if the string is a syntactically valid IPv4 address |
|
1465 |
* or hostname |
|
1466 |
*/ |
|
1467 |
public static boolean isWellFormedAddress(String p_address) |
|
1468 |
{ |
|
1469 |
||
1470 |
if (p_address == null) |
|
1471 |
{ |
|
1472 |
return false; |
|
1473 |
} |
|
1474 |
||
1475 |
String address = p_address.trim(); |
|
1476 |
int addrLength = address.length(); |
|
1477 |
||
1478 |
if (addrLength == 0 || addrLength > 255) |
|
1479 |
{ |
|
1480 |
return false; |
|
1481 |
} |
|
1482 |
||
1483 |
if (address.startsWith(".") || address.startsWith("-")) |
|
1484 |
{ |
|
1485 |
return false; |
|
1486 |
} |
|
1487 |
||
1488 |
// rightmost domain label starting with digit indicates IP address |
|
1489 |
// since top level domain label can only start with an alpha |
|
1490 |
// see RFC 2396 Section 3.2.2 |
|
1491 |
int index = address.lastIndexOf('.'); |
|
1492 |
||
1493 |
if (address.endsWith(".")) |
|
1494 |
{ |
|
1495 |
index = address.substring(0, index).lastIndexOf('.'); |
|
1496 |
} |
|
1497 |
||
1498 |
if (index + 1 < addrLength && isDigit(p_address.charAt(index + 1))) |
|
1499 |
{ |
|
1500 |
char testChar; |
|
1501 |
int numDots = 0; |
|
1502 |
||
1503 |
// make sure that 1) we see only digits and dot separators, 2) that |
|
1504 |
// any dot separator is preceded and followed by a digit and |
|
1505 |
// 3) that we find 3 dots |
|
1506 |
for (int i = 0; i < addrLength; i++) |
|
1507 |
{ |
|
1508 |
testChar = address.charAt(i); |
|
1509 |
||
1510 |
if (testChar == '.') |
|
1511 |
{ |
|
1512 |
if (!isDigit(address.charAt(i - 1)) |
|
1513 |
|| (i + 1 < addrLength &&!isDigit(address.charAt(i + 1)))) |
|
1514 |
{ |
|
1515 |
return false; |
|
1516 |
} |
|
1517 |
||
1518 |
numDots++; |
|
1519 |
} |
|
1520 |
else if (!isDigit(testChar)) |
|
1521 |
{ |
|
1522 |
return false; |
|
1523 |
} |
|
1524 |
} |
|
1525 |
||
1526 |
if (numDots != 3) |
|
1527 |
{ |
|
1528 |
return false; |
|
1529 |
} |
|
1530 |
} |
|
1531 |
else |
|
1532 |
{ |
|
1533 |
||
1534 |
// domain labels can contain alphanumerics and '-" |
|
1535 |
// but must start and end with an alphanumeric |
|
1536 |
char testChar; |
|
1537 |
||
1538 |
for (int i = 0; i < addrLength; i++) |
|
1539 |
{ |
|
1540 |
testChar = address.charAt(i); |
|
1541 |
||
1542 |
if (testChar == '.') |
|
1543 |
{ |
|
1544 |
if (!isAlphanum(address.charAt(i - 1))) |
|
1545 |
{ |
|
1546 |
return false; |
|
1547 |
} |
|
1548 |
||
1549 |
if (i + 1 < addrLength &&!isAlphanum(address.charAt(i + 1))) |
|
1550 |
{ |
|
1551 |
return false; |
|
1552 |
} |
|
1553 |
} |
|
1554 |
else if (!isAlphanum(testChar) && testChar != '-') |
|
1555 |
{ |
|
1556 |
return false; |
|
1557 |
} |
|
1558 |
} |
|
1559 |
} |
|
1560 |
||
1561 |
return true; |
|
1562 |
} |
|
1563 |
||
1564 |
/** |
|
1565 |
* Determine whether a char is a digit. |
|
1566 |
* |
|
1567 |
* |
|
1568 |
* @param p_char the character to check |
|
1569 |
* @return true if the char is betweeen '0' and '9', false otherwise |
|
1570 |
*/ |
|
1571 |
private static boolean isDigit(char p_char) |
|
1572 |
{ |
|
1573 |
return p_char >= '0' && p_char <= '9'; |
|
1574 |
} |
|
1575 |
||
1576 |
/** |
|
1577 |
* Determine whether a character is a hexadecimal character. |
|
1578 |
* |
|
1579 |
* |
|
1580 |
* @param p_char the character to check |
|
1581 |
* @return true if the char is betweeen '0' and '9', 'a' and 'f' |
|
1582 |
* or 'A' and 'F', false otherwise |
|
1583 |
*/ |
|
1584 |
private static boolean isHex(char p_char) |
|
1585 |
{ |
|
1586 |
return (isDigit(p_char) || (p_char >= 'a' && p_char <= 'f') |
|
1587 |
|| (p_char >= 'A' && p_char <= 'F')); |
|
1588 |
} |
|
1589 |
||
1590 |
/** |
|
1591 |
* Determine whether a char is an alphabetic character: a-z or A-Z |
|
1592 |
* |
|
1593 |
* |
|
1594 |
* @param p_char the character to check |
|
1595 |
* @return true if the char is alphabetic, false otherwise |
|
1596 |
*/ |
|
1597 |
private static boolean isAlpha(char p_char) |
|
1598 |
{ |
|
1599 |
return ((p_char >= 'a' && p_char <= 'z') |
|
1600 |
|| (p_char >= 'A' && p_char <= 'Z')); |
|
1601 |
} |
|
1602 |
||
1603 |
/** |
|
1604 |
* Determine whether a char is an alphanumeric: 0-9, a-z or A-Z |
|
1605 |
* |
|
1606 |
* |
|
1607 |
* @param p_char the character to check |
|
1608 |
* @return true if the char is alphanumeric, false otherwise |
|
1609 |
*/ |
|
1610 |
private static boolean isAlphanum(char p_char) |
|
1611 |
{ |
|
1612 |
return (isAlpha(p_char) || isDigit(p_char)); |
|
1613 |
} |
|
1614 |
||
1615 |
/** |
|
1616 |
* Determine whether a character is a reserved character: |
|
1617 |
* ';', '/', '?', ':', '@', '&', '=', '+', '$' or ',' |
|
1618 |
* |
|
1619 |
* |
|
1620 |
* @param p_char the character to check |
|
1621 |
* @return true if the string contains any reserved characters |
|
1622 |
*/ |
|
1623 |
private static boolean isReservedCharacter(char p_char) |
|
1624 |
{ |
|
1625 |
return RESERVED_CHARACTERS.indexOf(p_char) != -1; |
|
1626 |
} |
|
1627 |
||
1628 |
/** |
|
1629 |
* Determine whether a char is an unreserved character. |
|
1630 |
* |
|
1631 |
* |
|
1632 |
* @param p_char the character to check |
|
1633 |
* @return true if the char is unreserved, false otherwise |
|
1634 |
*/ |
|
1635 |
private static boolean isUnreservedCharacter(char p_char) |
|
1636 |
{ |
|
1637 |
return (isAlphanum(p_char) || MARK_CHARACTERS.indexOf(p_char) != -1); |
|
1638 |
} |
|
1639 |
||
1640 |
/** |
|
1641 |
* Determine whether a given string contains only URI characters (also |
|
1642 |
* called "uric" in RFC 2396). uric consist of all reserved |
|
1643 |
* characters, unreserved characters and escaped characters. |
|
1644 |
* |
|
1645 |
* |
|
1646 |
* @param p_uric URI string |
|
1647 |
* @return true if the string is comprised of uric, false otherwise |
|
1648 |
*/ |
|
1649 |
private static boolean isURIString(String p_uric) |
|
1650 |
{ |
|
1651 |
||
1652 |
if (p_uric == null) |
|
1653 |
{ |
|
1654 |
return false; |
|
1655 |
} |
|
1656 |
||
1657 |
int end = p_uric.length(); |
|
1658 |
char testChar = '\0'; |
|
1659 |
||
1660 |
for (int i = 0; i < end; i++) |
|
1661 |
{ |
|
1662 |
testChar = p_uric.charAt(i); |
|
1663 |
||
1664 |
if (testChar == '%') |
|
1665 |
{ |
|
1666 |
if (i + 2 >= end ||!isHex(p_uric.charAt(i + 1)) |
|
1667 |
||!isHex(p_uric.charAt(i + 2))) |
|
1668 |
{ |
|
1669 |
return false; |
|
1670 |
} |
|
1671 |
else |
|
1672 |
{ |
|
1673 |
i += 2; |
|
1674 |
||
1675 |
continue; |
|
1676 |
} |
|
1677 |
} |
|
1678 |
||
1679 |
if (isReservedCharacter(testChar) || isUnreservedCharacter(testChar)) |
|
1680 |
{ |
|
1681 |
continue; |
|
1682 |
} |
|
1683 |
else |
|
1684 |
{ |
|
1685 |
return false; |
|
1686 |
} |
|
1687 |
} |
|
1688 |
||
1689 |
return true; |
|
1690 |
} |
|
1691 |
} |