KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > types > URI


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.axis.types;
18
19 import java.io.IOException JavaDoc;
20 import java.io.Serializable JavaDoc;
21
22 /**********************************************************************
23 * <i>Axis Note: This class was 'borrowed' from Xerces 2:
24  * org.apache.xerces.util.URI.java, version 1.22 </i>
25 * <p>
26 * A class to represent a Uniform Resource Identifier (URI). This class
27 * is designed to handle the parsing of URIs and provide access to
28 * the various components (scheme, host, port, userinfo, path, query
29 * string and fragment) that may constitute a URI.
30 * <p>
31 * Parsing of a URI specification is done according to the URI
32 * syntax described in
33 * <a HREF="http://www.ietf.org/rfc/rfc2396.txt?number=2396">RFC 2396</a>,
34 * and amended by
35 * <a HREF="http://www.ietf.org/rfc/rfc2732.txt?number=2732">RFC 2732</a>.
36 * <p>
37 * Every absolute URI consists of a scheme, followed by a colon (':'),
38 * followed by a scheme-specific part. For URIs that follow the
39 * "generic URI" syntax, the scheme-specific part begins with two
40 * slashes ("//") and may be followed by an authority segment (comprised
41 * of user information, host, and port), path segment, query segment
42 * and fragment. Note that RFC 2396 no longer specifies the use of the
43 * parameters segment and excludes the "user:password" syntax as part of
44 * the authority segment. If "user:password" appears in a URI, the entire
45 * user/password string is stored as userinfo.
46 * <p>
47 * For URIs that do not follow the "generic URI" syntax (e.g. mailto),
48 * the entire scheme-specific part is treated as the "path" portion
49 * of the URI.
50 * <p>
51 * Note that, unlike the java.net.URL class, this class does not provide
52 * any built-in network access functionality nor does it provide any
53 * scheme-specific functionality (for example, it does not know a
54 * default port for a specific scheme). Rather, it only knows the
55 * grammar and basic set of operations that can be applied to a URI.
56 *
57 **********************************************************************/

58  public class URI implements Serializable JavaDoc {
59
60   /*******************************************************************
61   * MalformedURIExceptions are thrown in the process of building a URI
62   * or setting fields on a URI when an operation would result in an
63   * invalid URI specification.
64   *
65   ********************************************************************/

66   public static class MalformedURIException extends IOException JavaDoc {
67
68    /** Serialization version. */
69    static final long serialVersionUID = -6695054834342951930L;
70    
71    /******************************************************************
72     * Constructs a <code>MalformedURIException</code> with no specified
73     * detail message.
74     ******************************************************************/

75     public MalformedURIException() {
76       super();
77     }
78
79     /*****************************************************************
80     * Constructs a <code>MalformedURIException</code> with the
81     * specified detail message.
82     *
83     * @param p_msg the detail message.
84     ******************************************************************/

85     public MalformedURIException(String JavaDoc p_msg) {
86       super(p_msg);
87     }
88   }
89
90   /** Serialization version. */
91   static final long serialVersionUID = 1601921774685357214L;
92
93   private static final byte [] fgLookupTable = new byte[128];
94   
95   /**
96    * Character Classes
97    */

98   
99   /** reserved characters ;/?:@&=+$,[] */
100   //RFC 2732 added '[' and ']' as reserved characters
101
private static final int RESERVED_CHARACTERS = 0x01;
102   
103   /** URI punctuation mark characters: -_.!~*'() - these, combined with
104       alphanumerics, constitute the "unreserved" characters */

105   private static final int MARK_CHARACTERS = 0x02;
106   
107   /** scheme can be composed of alphanumerics and these characters: +-. */
108   private static final int SCHEME_CHARACTERS = 0x04;
109   
110   /** userinfo can be composed of unreserved, escaped and these
111       characters: ;:&=+$, */

112   private static final int USERINFO_CHARACTERS = 0x08;
113   
114   /** ASCII letter characters */
115   private static final int ASCII_ALPHA_CHARACTERS = 0x10;
116   
117   /** ASCII digit characters */
118   private static final int ASCII_DIGIT_CHARACTERS = 0x20;
119   
120   /** ASCII hex characters */
121   private static final int ASCII_HEX_CHARACTERS = 0x40;
122   
123   /** Path characters */
124   private static final int PATH_CHARACTERS = 0x80;
125
126   /** Mask for alpha-numeric characters */
127   private static final int MASK_ALPHA_NUMERIC = ASCII_ALPHA_CHARACTERS | ASCII_DIGIT_CHARACTERS;
128   
129   /** Mask for unreserved characters */
130   private static final int MASK_UNRESERVED_MASK = MASK_ALPHA_NUMERIC | MARK_CHARACTERS;
131   
132   /** Mask for URI allowable characters except for % */
133   private static final int MASK_URI_CHARACTER = MASK_UNRESERVED_MASK | RESERVED_CHARACTERS;
134   
135   /** Mask for scheme characters */
136   private static final int MASK_SCHEME_CHARACTER = MASK_ALPHA_NUMERIC | SCHEME_CHARACTERS;
137   
138   /** Mask for userinfo characters */
139   private static final int MASK_USERINFO_CHARACTER = MASK_UNRESERVED_MASK | USERINFO_CHARACTERS;
140   
141   /** Mask for path characters */
142   private static final int MASK_PATH_CHARACTER = MASK_UNRESERVED_MASK | PATH_CHARACTERS;
143
144   static {
145       // Add ASCII Digits and ASCII Hex Numbers
146
for (int i = '0'; i <= '9'; ++i) {
147           fgLookupTable[i] |= ASCII_DIGIT_CHARACTERS | ASCII_HEX_CHARACTERS;
148       }
149
150       // Add ASCII Letters and ASCII Hex Numbers
151
for (int i = 'A'; i <= 'F'; ++i) {
152           fgLookupTable[i] |= ASCII_ALPHA_CHARACTERS | ASCII_HEX_CHARACTERS;
153           fgLookupTable[i+0x00000020] |= ASCII_ALPHA_CHARACTERS | ASCII_HEX_CHARACTERS;
154       }
155
156       // Add ASCII Letters
157
for (int i = 'G'; i <= 'Z'; ++i) {
158           fgLookupTable[i] |= ASCII_ALPHA_CHARACTERS;
159           fgLookupTable[i+0x00000020] |= ASCII_ALPHA_CHARACTERS;
160       }
161
162       // Add Reserved Characters
163
fgLookupTable[';'] |= RESERVED_CHARACTERS;
164       fgLookupTable['/'] |= RESERVED_CHARACTERS;
165       fgLookupTable['?'] |= RESERVED_CHARACTERS;
166       fgLookupTable[':'] |= RESERVED_CHARACTERS;
167       fgLookupTable['@'] |= RESERVED_CHARACTERS;
168       fgLookupTable['&'] |= RESERVED_CHARACTERS;
169       fgLookupTable['='] |= RESERVED_CHARACTERS;
170       fgLookupTable['+'] |= RESERVED_CHARACTERS;
171       fgLookupTable['$'] |= RESERVED_CHARACTERS;
172       fgLookupTable[','] |= RESERVED_CHARACTERS;
173       fgLookupTable['['] |= RESERVED_CHARACTERS;
174       fgLookupTable[']'] |= RESERVED_CHARACTERS;
175
176       // Add Mark Characters
177
fgLookupTable['-'] |= MARK_CHARACTERS;
178       fgLookupTable['_'] |= MARK_CHARACTERS;
179       fgLookupTable['.'] |= MARK_CHARACTERS;
180       fgLookupTable['!'] |= MARK_CHARACTERS;
181       fgLookupTable['~'] |= MARK_CHARACTERS;
182       fgLookupTable['*'] |= MARK_CHARACTERS;
183       fgLookupTable['\''] |= MARK_CHARACTERS;
184       fgLookupTable['('] |= MARK_CHARACTERS;
185       fgLookupTable[')'] |= MARK_CHARACTERS;
186
187       // Add Scheme Characters
188
fgLookupTable['+'] |= SCHEME_CHARACTERS;
189       fgLookupTable['-'] |= SCHEME_CHARACTERS;
190       fgLookupTable['.'] |= SCHEME_CHARACTERS;
191
192       // Add Userinfo Characters
193
fgLookupTable[';'] |= USERINFO_CHARACTERS;
194       fgLookupTable[':'] |= USERINFO_CHARACTERS;
195       fgLookupTable['&'] |= USERINFO_CHARACTERS;
196       fgLookupTable['='] |= USERINFO_CHARACTERS;
197       fgLookupTable['+'] |= USERINFO_CHARACTERS;
198       fgLookupTable['$'] |= USERINFO_CHARACTERS;
199       fgLookupTable[','] |= USERINFO_CHARACTERS;
200       
201       // Add Path Characters
202
fgLookupTable[';'] |= PATH_CHARACTERS;
203       fgLookupTable['/'] |= PATH_CHARACTERS;
204       fgLookupTable[':'] |= PATH_CHARACTERS;
205       fgLookupTable['@'] |= PATH_CHARACTERS;
206       fgLookupTable['&'] |= PATH_CHARACTERS;
207       fgLookupTable['='] |= PATH_CHARACTERS;
208       fgLookupTable['+'] |= PATH_CHARACTERS;
209       fgLookupTable['$'] |= PATH_CHARACTERS;
210       fgLookupTable[','] |= PATH_CHARACTERS;
211   }
212
213   /** Stores the scheme (usually the protocol) for this URI. */
214   private String JavaDoc m_scheme = null;
215
216   /** If specified, stores the userinfo for this URI; otherwise null */
217   private String JavaDoc m_userinfo = null;
218
219   /** If specified, stores the host for this URI; otherwise null */
220   private String JavaDoc m_host = null;
221
222   /** If specified, stores the port for this URI; otherwise -1 */
223   private int m_port = -1;
224   
225   /** If specified, stores the registry based authority for this URI; otherwise -1 */
226   private String JavaDoc m_regAuthority = null;
227
228   /** If specified, stores the path for this URI; otherwise null */
229   private String JavaDoc m_path = null;
230
231   /** If specified, stores the query string for this URI; otherwise
232       null. */

233   private String JavaDoc m_queryString = null;
234
235   /** If specified, stores the fragment for this URI; otherwise null */
236   private String JavaDoc m_fragment = null;
237
238   private static boolean DEBUG = false;
239
240   /**
241   * Construct a new and uninitialized URI.
242   */

243   public URI() {
244   }
245
246  /**
247   * Construct a new URI from another URI. All fields for this URI are
248   * set equal to the fields of the URI passed in.
249   *
250   * @param p_other the URI to copy (cannot be null)
251   */

252   public URI(URI p_other) {
253     initialize(p_other);
254   }
255
256  /**
257   * Construct a new URI from a URI specification string. If the
258   * specification follows the "generic URI" syntax, (two slashes
259   * following the first colon), the specification will be parsed
260   * accordingly - setting the scheme, userinfo, host,port, path, query
261   * string and fragment fields as necessary. If the specification does
262   * not follow the "generic URI" syntax, the specification is parsed
263   * into a scheme and scheme-specific part (stored as the path) only.
264   *
265   * @param p_uriSpec the URI specification string (cannot be null or
266   * empty)
267   *
268   * @exception MalformedURIException if p_uriSpec violates any syntax
269   * rules
270   */

271   public URI(String JavaDoc p_uriSpec) throws MalformedURIException {
272     this((URI)null, p_uriSpec);
273   }
274
275  /**
276    * Construct a new URI from a URI specification string. If the
277    * specification follows the "generic URI" syntax, (two slashes
278    * following the first colon), the specification will be parsed
279    * accordingly - setting the scheme, userinfo, host,port, path, query
280    * string and fragment fields as necessary. If the specification does
281    * not follow the "generic URI" syntax, the specification is parsed
282    * into a scheme and scheme-specific part (stored as the path) only.
283    * Construct a relative URI if boolean is assigned to "true"
284    * and p_uriSpec is not valid absolute URI, instead of throwing an exception.
285    *
286    * @param p_uriSpec the URI specification string (cannot be null or
287    * empty)
288    * @param allowNonAbsoluteURI true to permit non-absolute URIs,
289    * false otherwise.
290    *
291    * @exception MalformedURIException if p_uriSpec violates any syntax
292    * rules
293    */

294   public URI(String JavaDoc p_uriSpec, boolean allowNonAbsoluteURI) throws MalformedURIException {
295       this((URI)null, p_uriSpec, allowNonAbsoluteURI);
296   }
297   
298  /**
299   * Construct a new URI from a base URI and a URI specification string.
300   * The URI specification string may be a relative URI.
301   *
302   * @param p_base the base URI (cannot be null if p_uriSpec is null or
303   * empty)
304   * @param p_uriSpec the URI specification string (cannot be null or
305   * empty if p_base is null)
306   *
307   * @exception MalformedURIException if p_uriSpec violates any syntax
308   * rules
309   */

310   public URI(URI p_base, String JavaDoc p_uriSpec) throws MalformedURIException {
311     initialize(p_base, p_uriSpec);
312   }
313
314  /**
315    * Construct a new URI from a base URI and a URI specification string.
316    * The URI specification string may be a relative URI.
317    * Construct a relative URI if boolean is assigned to "true"
318    * and p_uriSpec is not valid absolute URI and p_base is null
319    * instead of throwing an exception.
320    *
321    * @param p_base the base URI (cannot be null if p_uriSpec is null or
322    * empty)
323    * @param p_uriSpec the URI specification string (cannot be null or
324    * empty if p_base is null)
325    * @param allowNonAbsoluteURI true to permit non-absolute URIs,
326    * false otherwise.
327    *
328    * @exception MalformedURIException if p_uriSpec violates any syntax
329    * rules
330    */

331   public URI(URI p_base, String JavaDoc p_uriSpec, boolean allowNonAbsoluteURI) throws MalformedURIException {
332       initialize(p_base, p_uriSpec, allowNonAbsoluteURI);
333   }
334
335  /**
336   * Construct a new URI that does not follow the generic URI syntax.
337   * Only the scheme and scheme-specific part (stored as the path) are
338   * initialized.
339   *
340   * @param p_scheme the URI scheme (cannot be null or empty)
341   * @param p_schemeSpecificPart the scheme-specific part (cannot be
342   * null or empty)
343   *
344   * @exception MalformedURIException if p_scheme violates any
345   * syntax rules
346   */

347   public URI(String JavaDoc p_scheme, String JavaDoc p_schemeSpecificPart)
348              throws MalformedURIException {
349     if (p_scheme == null || p_scheme.trim().length() == 0) {
350       throw new MalformedURIException(
351             "Cannot construct URI with null/empty scheme!");
352     }
353     if (p_schemeSpecificPart == null ||
354         p_schemeSpecificPart.trim().length() == 0) {
355       throw new MalformedURIException(
356           "Cannot construct URI with null/empty scheme-specific part!");
357     }
358     setScheme(p_scheme);
359     setPath(p_schemeSpecificPart);
360   }
361
362  /**
363   * Construct a new URI that follows the generic URI syntax from its
364   * component parts. Each component is validated for syntax and some
365   * basic semantic checks are performed as well. See the individual
366   * setter methods for specifics.
367   *
368   * @param p_scheme the URI scheme (cannot be null or empty)
369   * @param p_host the hostname, IPv4 address or IPv6 reference for the URI
370   * @param p_path the URI path - if the path contains '?' or '#',
371   * then the query string and/or fragment will be
372   * set from the path; however, if the query and
373   * fragment are specified both in the path and as
374   * separate parameters, an exception is thrown
375   * @param p_queryString the URI query string (cannot be specified
376   * if path is null)
377   * @param p_fragment the URI fragment (cannot be specified if path
378   * is null)
379   *
380   * @exception MalformedURIException if any of the parameters violates
381   * syntax rules or semantic rules
382   */

383   public URI(String JavaDoc p_scheme, String JavaDoc p_host, String JavaDoc p_path,
384              String JavaDoc p_queryString, String JavaDoc p_fragment)
385          throws MalformedURIException {
386     this(p_scheme, null, p_host, -1, p_path, p_queryString, p_fragment);
387   }
388
389  /**
390   * Construct a new URI that follows the generic URI syntax from its
391   * component parts. Each component is validated for syntax and some
392   * basic semantic checks are performed as well. See the individual
393   * setter methods for specifics.
394   *
395   * @param p_scheme the URI scheme (cannot be null or empty)
396   * @param p_userinfo the URI userinfo (cannot be specified if host
397   * is null)
398   * @param p_host the hostname, IPv4 address or IPv6 reference for the URI
399   * @param p_port the URI port (may be -1 for "unspecified"; cannot
400   * be specified if host is null)
401   * @param p_path the URI path - if the path contains '?' or '#',
402   * then the query string and/or fragment will be
403   * set from the path; however, if the query and
404   * fragment are specified both in the path and as
405   * separate parameters, an exception is thrown
406   * @param p_queryString the URI query string (cannot be specified
407   * if path is null)
408   * @param p_fragment the URI fragment (cannot be specified if path
409   * is null)
410   *
411   * @exception MalformedURIException if any of the parameters violates
412   * syntax rules or semantic rules
413   */

414   public URI(String JavaDoc p_scheme, String JavaDoc p_userinfo,
415              String JavaDoc p_host, int p_port, String JavaDoc p_path,
416              String JavaDoc p_queryString, String JavaDoc p_fragment)
417          throws MalformedURIException {
418     if (p_scheme == null || p_scheme.trim().length() == 0) {
419       throw new MalformedURIException("Scheme is required!");
420     }
421
422     if (p_host == null) {
423       if (p_userinfo != null) {
424         throw new MalformedURIException(
425              "Userinfo may not be specified if host is not specified!");
426       }
427       if (p_port != -1) {
428         throw new MalformedURIException(
429              "Port may not be specified if host is not specified!");
430       }
431     }
432
433     if (p_path != null) {
434       if (p_path.indexOf('?') != -1 && p_queryString != null) {
435         throw new MalformedURIException(
436           "Query string cannot be specified in path and query string!");
437       }
438
439       if (p_path.indexOf('#') != -1 && p_fragment != null) {
440         throw new MalformedURIException(
441           "Fragment cannot be specified in both the path and fragment!");
442       }
443     }
444
445     setScheme(p_scheme);
446     setHost(p_host);
447     setPort(p_port);
448     setUserinfo(p_userinfo);
449     setPath(p_path);
450     setQueryString(p_queryString);
451     setFragment(p_fragment);
452   }
453
454  /**
455   * Initialize all fields of this URI from another URI.
456   *
457   * @param p_other the URI to copy (cannot be null)
458   */

459   private void initialize(URI p_other) {
460     m_scheme = p_other.getScheme();
461     m_userinfo = p_other.getUserinfo();
462     m_host = p_other.getHost();
463     m_port = p_other.getPort();
464     m_regAuthority = p_other.getRegBasedAuthority();
465     m_path = p_other.getPath();
466     m_queryString = p_other.getQueryString();
467     m_fragment = p_other.getFragment();
468   }
469
470  /**
471   * Initializes this URI from a base URI and a URI specification string.
472   * See RFC 2396 Section 4 and Appendix B for specifications on parsing
473   * the URI and Section 5 for specifications on resolving relative URIs
474   * and relative paths.
475   *
476   * @param p_base the base URI (may be null if p_uriSpec is an absolute
477   * URI)
478   * @param p_uriSpec the URI spec string which may be an absolute or
479   * relative URI (can only be null/empty if p_base
480   * is not null)
481    * @param allowNonAbsoluteURI true to permit non-absolute URIs,
482    * in case of relative URI, false otherwise.
483    *
484    * @exception MalformedURIException if p_base is null and p_uriSpec
485    * is not an absolute URI or if
486    * p_uriSpec violates syntax rules
487    */

488   private void initialize(URI p_base, String JavaDoc p_uriSpec, boolean allowNonAbsoluteURI)
489       throws MalformedURIException {
490       
491       String JavaDoc uriSpec = p_uriSpec;
492       int uriSpecLen = (uriSpec != null) ? uriSpec.length() : 0;
493       
494       if (p_base == null && uriSpecLen == 0) {
495           if (allowNonAbsoluteURI) {
496               m_path = "";
497               return;
498           }
499           throw new MalformedURIException("Cannot initialize URI with empty parameters.");
500       }
501       
502       // just make a copy of the base if spec is empty
503
if (uriSpecLen == 0) {
504           initialize(p_base);
505           return;
506       }
507       
508       int index = 0;
509       
510       // Check for scheme, which must be before '/', '?' or '#'.
511
int colonIdx = uriSpec.indexOf(':');
512       if (colonIdx != -1) {
513           final int searchFrom = colonIdx - 1;
514           // search backwards starting from character before ':'.
515
int slashIdx = uriSpec.lastIndexOf('/', searchFrom);
516           int queryIdx = uriSpec.lastIndexOf('?', searchFrom);
517           int fragmentIdx = uriSpec.lastIndexOf('#', searchFrom);
518           
519           if (colonIdx == 0 || slashIdx != -1 ||
520               queryIdx != -1 || fragmentIdx != -1) {
521               // A standalone base is a valid URI according to spec
522
if (colonIdx == 0 || (p_base == null && fragmentIdx != 0 && !allowNonAbsoluteURI)) {
523                   throw new MalformedURIException("No scheme found in URI.");
524               }
525           }
526           else {
527               initializeScheme(uriSpec);
528               index = m_scheme.length()+1;
529               
530               // Neither 'scheme:' or 'scheme:#fragment' are valid URIs.
531
if (colonIdx == uriSpecLen - 1 || uriSpec.charAt(colonIdx+1) == '#') {
532                   throw new MalformedURIException("Scheme specific part cannot be empty.");
533               }
534           }
535       }
536       else if (p_base == null && uriSpec.indexOf('#') != 0 && !allowNonAbsoluteURI) {
537           throw new MalformedURIException("No scheme found in URI.");
538       }
539       
540       // Two slashes means we may have authority, but definitely means we're either
541
// matching net_path or abs_path. These two productions are ambiguous in that
542
// every net_path (except those containing an IPv6Reference) is an abs_path.
543
// RFC 2396 resolves this ambiguity by applying a greedy left most matching rule.
544
// Try matching net_path first, and if that fails we don't have authority so
545
// then attempt to match abs_path.
546
//
547
// net_path = "//" authority [ abs_path ]
548
// abs_path = "/" path_segments
549
if (((index+1) < uriSpecLen) &&
550           (uriSpec.charAt(index) == '/' && uriSpec.charAt(index+1) == '/')) {
551           index += 2;
552           int startPos = index;
553           
554           // Authority will be everything up to path, query or fragment
555
char testChar = '\0';
556           while (index < uriSpecLen) {
557               testChar = uriSpec.charAt(index);
558               if (testChar == '/' || testChar == '?' || testChar == '#') {
559                   break;
560               }
561               index++;
562           }
563           
564           // Attempt to parse authority. If the section is an empty string
565
// this is a valid server based authority, so set the host to this
566
// value.
567
if (index > startPos) {
568               // If we didn't find authority we need to back up. Attempt to
569
// match against abs_path next.
570
if (!initializeAuthority(uriSpec.substring(startPos, index))) {
571                   index = startPos - 2;
572               }
573           }
574           else {
575               m_host = "";
576           }
577       }
578       
579       initializePath(uriSpec, index);
580       
581       // Resolve relative URI to base URI - see RFC 2396 Section 5.2
582
// In some cases, it might make more sense to throw an exception
583
// (when scheme is specified is the string spec and the base URI
584
// is also specified, for example), but we're just following the
585
// RFC specifications
586
if (p_base != null) {
587           absolutize(p_base);
588       }
589   }
590
591  /**
592   * Initializes this URI from a base URI and a URI specification string.
593   * See RFC 2396 Section 4 and Appendix B for specifications on parsing
594   * the URI and Section 5 for specifications on resolving relative URIs
595   * and relative paths.
596   *
597   * @param p_base the base URI (may be null if p_uriSpec is an absolute
598   * URI)
599   * @param p_uriSpec the URI spec string which may be an absolute or
600   * relative URI (can only be null/empty if p_base
601   * is not null)
602   *
603   * @exception MalformedURIException if p_base is null and p_uriSpec
604   * is not an absolute URI or if
605   * p_uriSpec violates syntax rules
606   */

607   private void initialize(URI p_base, String JavaDoc p_uriSpec)
608                          throws MalformedURIException {
609       
610     String JavaDoc uriSpec = p_uriSpec;
611     int uriSpecLen = (uriSpec != null) ? uriSpec.length() : 0;
612     
613     if (p_base == null && uriSpecLen == 0) {
614       throw new MalformedURIException(
615                   "Cannot initialize URI with empty parameters.");
616     }
617
618     // just make a copy of the base if spec is empty
619
if (uriSpecLen == 0) {
620       initialize(p_base);
621       return;
622     }
623
624     int index = 0;
625
626     // Check for scheme, which must be before '/', '?' or '#'.
627
int colonIdx = uriSpec.indexOf(':');
628     if (colonIdx != -1) {
629         final int searchFrom = colonIdx - 1;
630         // search backwards starting from character before ':'.
631
int slashIdx = uriSpec.lastIndexOf('/', searchFrom);
632         int queryIdx = uriSpec.lastIndexOf('?', searchFrom);
633         int fragmentIdx = uriSpec.lastIndexOf('#', searchFrom);
634        
635         if (colonIdx == 0 || slashIdx != -1 ||
636             queryIdx != -1 || fragmentIdx != -1) {
637             // A standalone base is a valid URI according to spec
638
if (colonIdx == 0 || (p_base == null && fragmentIdx != 0)) {
639                 throw new MalformedURIException("No scheme found in URI.");
640             }
641         }
642         else {
643             initializeScheme(uriSpec);
644             index = m_scheme.length()+1;
645             
646             // Neither 'scheme:' or 'scheme:#fragment' are valid URIs.
647
if (colonIdx == uriSpecLen - 1 || uriSpec.charAt(colonIdx+1) == '#') {
648                 throw new MalformedURIException("Scheme specific part cannot be empty.");
649             }
650         }
651     }
652     else if (p_base == null && uriSpec.indexOf('#') != 0) {
653         throw new MalformedURIException("No scheme found in URI.");
654     }
655
656     // Two slashes means we may have authority, but definitely means we're either
657
// matching net_path or abs_path. These two productions are ambiguous in that
658
// every net_path (except those containing an IPv6Reference) is an abs_path.
659
// RFC 2396 resolves this ambiguity by applying a greedy left most matching rule.
660
// Try matching net_path first, and if that fails we don't have authority so
661
// then attempt to match abs_path.
662
//
663
// net_path = "//" authority [ abs_path ]
664
// abs_path = "/" path_segments
665
if (((index+1) < uriSpecLen) &&
666         (uriSpec.charAt(index) == '/' && uriSpec.charAt(index+1) == '/')) {
667       index += 2;
668       int startPos = index;
669
670       // Authority will be everything up to path, query or fragment
671
char testChar = '\0';
672       while (index < uriSpecLen) {
673         testChar = uriSpec.charAt(index);
674         if (testChar == '/' || testChar == '?' || testChar == '#') {
675           break;
676         }
677         index++;
678       }
679
680       // Attempt to parse authority. If the section is an empty string
681
// this is a valid server based authority, so set the host to this
682
// value.
683
if (index > startPos) {
684         // If we didn't find authority we need to back up. Attempt to
685
// match against abs_path next.
686
if (!initializeAuthority(uriSpec.substring(startPos, index))) {
687           index = startPos - 2;
688         }
689       }
690       else {
691         m_host = "";
692       }
693     }
694
695     initializePath(uriSpec, index);
696
697     // Resolve relative URI to base URI - see RFC 2396 Section 5.2
698
// In some cases, it might make more sense to throw an exception
699
// (when scheme is specified is the string spec and the base URI
700
// is also specified, for example), but we're just following the
701
// RFC specifications
702
if (p_base != null) {
703         absolutize(p_base);
704     }
705   }
706
707   /**
708    * Absolutize URI with given base URI.
709    *
710    * @param p_base base URI for absolutization
711    */

712   public void absolutize(URI p_base) {
713
714       // check to see if this is the current doc - RFC 2396 5.2 #2
715
// note that this is slightly different from the RFC spec in that
716
// we don't include the check for query string being null
717
// - this handles cases where the urispec is just a query
718
// string or a fragment (e.g. "?y" or "#s") -
719
// see <http://www.ics.uci.edu/~fielding/url/test1.html> which
720
// identified this as a bug in the RFC
721
if (m_path.length() == 0 && m_scheme == null &&
722           m_host == null && m_regAuthority == null) {
723         m_scheme = p_base.getScheme();
724         m_userinfo = p_base.getUserinfo();
725         m_host = p_base.getHost();
726         m_port = p_base.getPort();
727         m_regAuthority = p_base.getRegBasedAuthority();
728         m_path = p_base.getPath();
729
730         if (m_queryString == null) {
731           m_queryString = p_base.getQueryString();
732               
733               if (m_fragment == null) {
734                   m_fragment = p_base.getFragment();
735               }
736         }
737         return;
738       }
739
740       // check for scheme - RFC 2396 5.2 #3
741
// if we found a scheme, it means absolute URI, so we're done
742
if (m_scheme == null) {
743         m_scheme = p_base.getScheme();
744       }
745       else {
746         return;
747       }
748
749       // check for authority - RFC 2396 5.2 #4
750
// if we found a host, then we've got a network path, so we're done
751
if (m_host == null && m_regAuthority == null) {
752         m_userinfo = p_base.getUserinfo();
753         m_host = p_base.getHost();
754         m_port = p_base.getPort();
755         m_regAuthority = p_base.getRegBasedAuthority();
756       }
757       else {
758         return;
759       }
760
761       // check for absolute path - RFC 2396 5.2 #5
762
if (m_path.length() > 0 &&
763           m_path.startsWith("/")) {
764         return;
765       }
766
767       // if we get to this point, we need to resolve relative path
768
// RFC 2396 5.2 #6
769
String JavaDoc path = "";
770       String JavaDoc basePath = p_base.getPath();
771
772       // 6a - get all but the last segment of the base URI path
773
if (basePath != null && basePath.length() > 0) {
774         int lastSlash = basePath.lastIndexOf('/');
775         if (lastSlash != -1) {
776           path = basePath.substring(0, lastSlash+1);
777         }
778       }
779       else if (m_path.length() > 0) {
780           path = "/";
781       }
782
783       // 6b - append the relative URI path
784
path = path.concat(m_path);
785
786       // 6c - remove all "./" where "." is a complete path segment
787
int index = -1;
788       while ((index = path.indexOf("/./")) != -1) {
789         path = path.substring(0, index+1).concat(path.substring(index+3));
790       }
791
792       // 6d - remove "." if path ends with "." as a complete path segment
793
if (path.endsWith("/.")) {
794         path = path.substring(0, path.length()-1);
795       }
796
797       // 6e - remove all "<segment>/../" where "<segment>" is a complete
798
// path segment not equal to ".."
799
index = 1;
800       int segIndex = -1;
801       String JavaDoc tempString = null;
802
803       while ((index = path.indexOf("/../", index)) > 0) {
804         tempString = path.substring(0, path.indexOf("/../"));
805         segIndex = tempString.lastIndexOf('/');
806         if (segIndex != -1) {
807           if (!tempString.substring(segIndex).equals("..")) {
808             path = path.substring(0, segIndex+1).concat(path.substring(index+4));
809             index = segIndex;
810           }
811               else {
812             index += 4;
813         }
814           }
815           else {
816           index += 4;
817       }
818       }
819
820       // 6f - remove ending "<segment>/.." where "<segment>" is a
821
// complete path segment
822
if (path.endsWith("/..")) {
823         tempString = path.substring(0, path.length()-3);
824         segIndex = tempString.lastIndexOf('/');
825         if (segIndex != -1) {
826           path = path.substring(0, segIndex+1);
827         }
828       }
829       m_path = path;
830     }
831
832  /**
833   * Initialize the scheme for this URI from a URI string spec.
834   *
835   * @param p_uriSpec the URI specification (cannot be null)
836   *
837   * @exception MalformedURIException if URI does not have a conformant
838   * scheme
839   */

840   private void initializeScheme(String JavaDoc p_uriSpec)
841                  throws MalformedURIException {
842     int uriSpecLen = p_uriSpec.length();
843     int index = 0;
844     String JavaDoc scheme = null;
845     char testChar = '\0';
846
847     while (index < uriSpecLen) {
848       testChar = p_uriSpec.charAt(index);
849       if (testChar == ':' || testChar == '/' ||
850           testChar == '?' || testChar == '#') {
851         break;
852       }
853       index++;
854     }
855     scheme = p_uriSpec.substring(0, index);
856
857     if (scheme.length() == 0) {
858       throw new MalformedURIException("No scheme found in URI.");
859     }
860     else {
861       setScheme(scheme);
862     }
863   }
864
865  /**
866   * Initialize the authority (either server or registry based)
867   * for this URI from a URI string spec.
868   *
869   * @param p_uriSpec the URI specification (cannot be null)
870   *
871   * @return true if the given string matched server or registry
872   * based authority
873   */

874   private boolean initializeAuthority(String JavaDoc p_uriSpec) {
875     
876     int index = 0;
877     int start = 0;
878     int end = p_uriSpec.length();
879
880     char testChar = '\0';
881     String JavaDoc userinfo = null;
882
883     // userinfo is everything up to @
884
if (p_uriSpec.indexOf('@', start) != -1) {
885       while (index < end) {
886         testChar = p_uriSpec.charAt(index);
887         if (testChar == '@') {
888           break;
889         }
890         index++;
891       }
892       userinfo = p_uriSpec.substring(start, index);
893       index++;
894     }
895
896     // host is everything up to last ':', or up to
897
// and including ']' if followed by ':'.
898
String JavaDoc host = null;
899     start = index;
900     boolean hasPort = false;
901     if (index < end) {
902       if (p_uriSpec.charAt(start) == '[') {
903           int bracketIndex = p_uriSpec.indexOf(']', start);
904           index = (bracketIndex != -1) ? bracketIndex : end;
905           if (index+1 < end && p_uriSpec.charAt(index+1) == ':') {
906             ++index;
907             hasPort = true;
908           }
909           else {
910             index = end;
911           }
912       }
913       else {
914           int colonIndex = p_uriSpec.lastIndexOf(':', end);
915           index = (colonIndex > start) ? colonIndex : end;
916           hasPort = (index != end);
917       }
918     }
919     host = p_uriSpec.substring(start, index);
920     int port = -1;
921     if (host.length() > 0) {
922       // port
923
if (hasPort) {
924         index++;
925         start = index;
926         while (index < end) {
927           index++;
928         }
929         String JavaDoc portStr = p_uriSpec.substring(start, index);
930         if (portStr.length() > 0) {
931           // REVISIT: Remove this code.
932
/** for (int i = 0; i < portStr.length(); i++) {
933             if (!isDigit(portStr.charAt(i))) {
934               throw new MalformedURIException(
935                    portStr +
936                    " is invalid. Port should only contain digits!");
937             }
938           }**/

939           // REVISIT: Remove this code.
940
// Store port value as string instead of integer.
941
try {
942             port = Integer.parseInt(portStr);
943             if (port == -1) --port;
944           }
945           catch (NumberFormatException JavaDoc nfe) {
946             port = -2;
947           }
948         }
949       }
950     }
951     
952     if (isValidServerBasedAuthority(host, port, userinfo)) {
953       m_host = host;
954       m_port = port;
955       m_userinfo = userinfo;
956 &n