jaxp/src/com/sun/org/apache/xerces/internal/util/URI.java
author dfuchs
Fri, 17 May 2013 10:40:21 +0200
changeset 17538 d8d911c4e5d4
parent 12457 c348e06f0e82
child 20977 0c63befdae8a
permissions -rw-r--r--
8013900: More warnings compiling jaxp. Summary: Some internal implementation classes in Jaxp were redefining equals() without redefining hashCode(). This patch adds hashCode() methods that are consistent with equals(). Reviewed-by: chegar, joehw
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     6
 * Copyright 1999-2005 The Apache Software Foundation.
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 * Licensed under the Apache License, Version 2.0 (the "License");
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 * you may not use this file except in compliance with the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 * You may obtain a copy of the License at
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *      http://www.apache.org/licenses/LICENSE-2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
package com.sun.org.apache.xerces.internal.util;
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
import java.io.IOException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
import java.io.Serializable;
17538
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
    25
import java.util.Objects;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
/**********************************************************************
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
* A class to represent a Uniform Resource Identifier (URI). This class
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
* is designed to handle the parsing of URIs and provide access to
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
* the various components (scheme, host, port, userinfo, path, query
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
* string and fragment) that may constitute a URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
* <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
* Parsing of a URI specification is done according to the URI
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
* syntax described in
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
* <a href="http://www.ietf.org/rfc/rfc2396.txt?number=2396">RFC 2396</a>,
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
* and amended by
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
* <a href="http://www.ietf.org/rfc/rfc2732.txt?number=2732">RFC 2732</a>.
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
* <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
* Every absolute URI consists of a scheme, followed by a colon (':'),
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
* followed by a scheme-specific part. For URIs that follow the
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
* "generic URI" syntax, the scheme-specific part begins with two
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
* slashes ("//") and may be followed by an authority segment (comprised
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
* of user information, host, and port), path segment, query segment
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
* and fragment. Note that RFC 2396 no longer specifies the use of the
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
* parameters segment and excludes the "user:password" syntax as part of
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
* the authority segment. If "user:password" appears in a URI, the entire
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
* user/password string is stored as userinfo.
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
* <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
* For URIs that do not follow the "generic URI" syntax (e.g. mailto),
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
* the entire scheme-specific part is treated as the "path" portion
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
* of the URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
* <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
* Note that, unlike the java.net.URL class, this class does not provide
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
* any built-in network access functionality nor does it provide any
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
* scheme-specific functionality (for example, it does not know a
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
* default port for a specific scheme). Rather, it only knows the
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
* grammar and basic set of operations that can be applied to a URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
*
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
*
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
**********************************************************************/
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
 public class URI implements Serializable {
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
  /*******************************************************************
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
  * MalformedURIExceptions are thrown in the process of building a URI
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
  * or setting fields on a URI when an operation would result in an
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
  * invalid URI specification.
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
  ********************************************************************/
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
  public static class MalformedURIException extends IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
   /** Serialization version. */
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
   static final long serialVersionUID = -6695054834342951930L;
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
   /******************************************************************
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
    * Constructs a <code>MalformedURIException</code> with no specified
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
    * detail message.
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
    ******************************************************************/
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
    public MalformedURIException() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
      super();
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
    /*****************************************************************
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
    * Constructs a <code>MalformedURIException</code> with the
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
    * specified detail message.
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
    *
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
    * @param p_msg the detail message.
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
    ******************************************************************/
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
    public MalformedURIException(String p_msg) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
      super(p_msg);
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
  /** Serialization version. */
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
  static final long serialVersionUID = 1601921774685357214L;
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
  private static final byte [] fgLookupTable = new byte[128];
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
   * Character Classes
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
  /** reserved characters ;/?:@&=+$,[] */
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
  //RFC 2732 added '[' and ']' as reserved characters
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
  private static final int RESERVED_CHARACTERS = 0x01;
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
  /** URI punctuation mark characters: -_.!~*'() - these, combined with
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
      alphanumerics, constitute the "unreserved" characters */
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
  private static final int MARK_CHARACTERS = 0x02;
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
  /** scheme can be composed of alphanumerics and these characters: +-. */
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
  private static final int SCHEME_CHARACTERS = 0x04;
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
  /** userinfo can be composed of unreserved, escaped and these
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
      characters: ;:&=+$, */
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
  private static final int USERINFO_CHARACTERS = 0x08;
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
  /** ASCII letter characters */
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
  private static final int ASCII_ALPHA_CHARACTERS = 0x10;
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
  /** ASCII digit characters */
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
  private static final int ASCII_DIGIT_CHARACTERS = 0x20;
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
  /** ASCII hex characters */
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
  private static final int ASCII_HEX_CHARACTERS = 0x40;
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
  /** Path characters */
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
  private static final int PATH_CHARACTERS = 0x80;
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
  /** Mask for alpha-numeric characters */
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
  private static final int MASK_ALPHA_NUMERIC = ASCII_ALPHA_CHARACTERS | ASCII_DIGIT_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
  /** Mask for unreserved characters */
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
  private static final int MASK_UNRESERVED_MASK = MASK_ALPHA_NUMERIC | MARK_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
  /** Mask for URI allowable characters except for % */
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
  private static final int MASK_URI_CHARACTER = MASK_UNRESERVED_MASK | RESERVED_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
  /** Mask for scheme characters */
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
  private static final int MASK_SCHEME_CHARACTER = MASK_ALPHA_NUMERIC | SCHEME_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
  /** Mask for userinfo characters */
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
  private static final int MASK_USERINFO_CHARACTER = MASK_UNRESERVED_MASK | USERINFO_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
  /** Mask for path characters */
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
  private static final int MASK_PATH_CHARACTER = MASK_UNRESERVED_MASK | PATH_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
  static {
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
      // Add ASCII Digits and ASCII Hex Numbers
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
      for (int i = '0'; i <= '9'; ++i) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
          fgLookupTable[i] |= ASCII_DIGIT_CHARACTERS | ASCII_HEX_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
      // Add ASCII Letters and ASCII Hex Numbers
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
      for (int i = 'A'; i <= 'F'; ++i) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
          fgLookupTable[i] |= ASCII_ALPHA_CHARACTERS | ASCII_HEX_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
          fgLookupTable[i+0x00000020] |= ASCII_ALPHA_CHARACTERS | ASCII_HEX_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
      // Add ASCII Letters
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
      for (int i = 'G'; i <= 'Z'; ++i) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
          fgLookupTable[i] |= ASCII_ALPHA_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
          fgLookupTable[i+0x00000020] |= ASCII_ALPHA_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
      // Add Reserved Characters
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
      fgLookupTable[';'] |= RESERVED_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
      fgLookupTable['/'] |= RESERVED_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
      fgLookupTable['?'] |= RESERVED_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
      fgLookupTable[':'] |= RESERVED_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
      fgLookupTable['@'] |= RESERVED_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
      fgLookupTable['&'] |= RESERVED_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
      fgLookupTable['='] |= RESERVED_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
      fgLookupTable['+'] |= RESERVED_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
      fgLookupTable['$'] |= RESERVED_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
      fgLookupTable[','] |= RESERVED_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
      fgLookupTable['['] |= RESERVED_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
      fgLookupTable[']'] |= RESERVED_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
      // Add Mark Characters
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
      fgLookupTable['-'] |= MARK_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
      fgLookupTable['_'] |= MARK_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
      fgLookupTable['.'] |= MARK_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
      fgLookupTable['!'] |= MARK_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
      fgLookupTable['~'] |= MARK_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
      fgLookupTable['*'] |= MARK_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
      fgLookupTable['\''] |= MARK_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
      fgLookupTable['('] |= MARK_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
      fgLookupTable[')'] |= MARK_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
      // Add Scheme Characters
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
      fgLookupTable['+'] |= SCHEME_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
      fgLookupTable['-'] |= SCHEME_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
      fgLookupTable['.'] |= SCHEME_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
      // Add Userinfo Characters
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
      fgLookupTable[';'] |= USERINFO_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
      fgLookupTable[':'] |= USERINFO_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
      fgLookupTable['&'] |= USERINFO_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
      fgLookupTable['='] |= USERINFO_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
      fgLookupTable['+'] |= USERINFO_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
      fgLookupTable['$'] |= USERINFO_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
      fgLookupTable[','] |= USERINFO_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
      // Add Path Characters
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
      fgLookupTable[';'] |= PATH_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
      fgLookupTable['/'] |= PATH_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
      fgLookupTable[':'] |= PATH_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
      fgLookupTable['@'] |= PATH_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
      fgLookupTable['&'] |= PATH_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
      fgLookupTable['='] |= PATH_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
      fgLookupTable['+'] |= PATH_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
      fgLookupTable['$'] |= PATH_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
      fgLookupTable[','] |= PATH_CHARACTERS;
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
  /** Stores the scheme (usually the protocol) for this URI. */
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
  private String m_scheme = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
  /** If specified, stores the userinfo for this URI; otherwise null */
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
  private String m_userinfo = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
  /** If specified, stores the host for this URI; otherwise null */
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
  private String m_host = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
  /** If specified, stores the port for this URI; otherwise -1 */
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
  private int m_port = -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
  /** If specified, stores the registry based authority for this URI; otherwise -1 */
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
  private String m_regAuthority = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
  /** If specified, stores the path for this URI; otherwise null */
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
  private String m_path = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
  /** If specified, stores the query string for this URI; otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
      null.  */
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
  private String m_queryString = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
  /** If specified, stores the fragment for this URI; otherwise null */
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
  private String m_fragment = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
  private static boolean DEBUG = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
  * Construct a new and uninitialized URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
  public URI() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
  * Construct a new URI from another URI. All fields for this URI are
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
  * set equal to the fields of the URI passed in.
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
  * @param p_other the URI to copy (cannot be null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
  public URI(URI p_other) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
    initialize(p_other);
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
  * Construct a new URI from a URI specification string. If the
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
  * specification follows the "generic URI" syntax, (two slashes
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
  * following the first colon), the specification will be parsed
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
  * accordingly - setting the scheme, userinfo, host,port, path, query
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
  * string and fragment fields as necessary. If the specification does
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
  * not follow the "generic URI" syntax, the specification is parsed
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
  * into a scheme and scheme-specific part (stored as the path) only.
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
  * @param p_uriSpec the URI specification string (cannot be null or
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
  *                  empty)
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
  * @exception MalformedURIException if p_uriSpec violates any syntax
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
  *                                   rules
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
  public URI(String p_uriSpec) throws MalformedURIException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
    this((URI)null, p_uriSpec);
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
   * Construct a new URI from a URI specification string. If the
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
   * specification follows the "generic URI" syntax, (two slashes
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
   * following the first colon), the specification will be parsed
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
   * accordingly - setting the scheme, userinfo, host,port, path, query
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
   * string and fragment fields as necessary. If the specification does
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
   * not follow the "generic URI" syntax, the specification is parsed
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
   * into a scheme and scheme-specific part (stored as the path) only.
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
   * Construct a relative URI if boolean is assigned to "true"
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
   * and p_uriSpec is not valid absolute URI, instead of throwing an exception.
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
   * @param p_uriSpec the URI specification string (cannot be null or
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
   *                  empty)
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
   * @param allowNonAbsoluteURI true to permit non-absolute URIs,
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
   *                            false otherwise.
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
   * @exception MalformedURIException if p_uriSpec violates any syntax
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
   *                                   rules
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
  public URI(String p_uriSpec, boolean allowNonAbsoluteURI) throws MalformedURIException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
      this((URI)null, p_uriSpec, allowNonAbsoluteURI);
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
  * Construct a new URI from a base URI and a URI specification string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
  * The URI specification string may be a relative URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
  * @param p_base the base URI (cannot be null if p_uriSpec is null or
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
  *               empty)
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
  * @param p_uriSpec the URI specification string (cannot be null or
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
  *                  empty if p_base is null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
  * @exception MalformedURIException if p_uriSpec violates any syntax
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
  *                                  rules
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
  public URI(URI p_base, String p_uriSpec) throws MalformedURIException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
    initialize(p_base, p_uriSpec);
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
   * Construct a new URI from a base URI and a URI specification string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
   * The URI specification string may be a relative URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
   * Construct a relative URI if boolean is assigned to "true"
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
   * and p_uriSpec is not valid absolute URI and p_base is null
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
   * instead of throwing an exception.
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
   * @param p_base the base URI (cannot be null if p_uriSpec is null or
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
   *               empty)
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
   * @param p_uriSpec the URI specification string (cannot be null or
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
   *                  empty if p_base is null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
   * @param allowNonAbsoluteURI true to permit non-absolute URIs,
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
   *                            false otherwise.
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
   * @exception MalformedURIException if p_uriSpec violates any syntax
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
   *                                  rules
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   334
  public URI(URI p_base, String p_uriSpec, boolean allowNonAbsoluteURI) throws MalformedURIException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   335
      initialize(p_base, p_uriSpec, allowNonAbsoluteURI);
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   337
7f561c08de6b Initial load
duke
parents:
diff changeset
   338
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   339
  * Construct a new URI that does not follow the generic URI syntax.
7f561c08de6b Initial load
duke
parents:
diff changeset
   340
  * Only the scheme and scheme-specific part (stored as the path) are
7f561c08de6b Initial load
duke
parents:
diff changeset
   341
  * initialized.
7f561c08de6b Initial load
duke
parents:
diff changeset
   342
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
   343
  * @param p_scheme the URI scheme (cannot be null or empty)
7f561c08de6b Initial load
duke
parents:
diff changeset
   344
  * @param p_schemeSpecificPart the scheme-specific part (cannot be
7f561c08de6b Initial load
duke
parents:
diff changeset
   345
  *                             null or empty)
7f561c08de6b Initial load
duke
parents:
diff changeset
   346
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
   347
  * @exception MalformedURIException if p_scheme violates any
7f561c08de6b Initial load
duke
parents:
diff changeset
   348
  *                                  syntax rules
7f561c08de6b Initial load
duke
parents:
diff changeset
   349
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
   350
  public URI(String p_scheme, String p_schemeSpecificPart)
7f561c08de6b Initial load
duke
parents:
diff changeset
   351
             throws MalformedURIException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   352
    if (p_scheme == null || p_scheme.trim().length() == 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   353
      throw new MalformedURIException(
7f561c08de6b Initial load
duke
parents:
diff changeset
   354
            "Cannot construct URI with null/empty scheme!");
7f561c08de6b Initial load
duke
parents:
diff changeset
   355
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   356
    if (p_schemeSpecificPart == null ||
7f561c08de6b Initial load
duke
parents:
diff changeset
   357
        p_schemeSpecificPart.trim().length() == 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   358
      throw new MalformedURIException(
7f561c08de6b Initial load
duke
parents:
diff changeset
   359
          "Cannot construct URI with null/empty scheme-specific part!");
7f561c08de6b Initial load
duke
parents:
diff changeset
   360
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   361
    setScheme(p_scheme);
7f561c08de6b Initial load
duke
parents:
diff changeset
   362
    setPath(p_schemeSpecificPart);
7f561c08de6b Initial load
duke
parents:
diff changeset
   363
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   364
7f561c08de6b Initial load
duke
parents:
diff changeset
   365
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   366
  * Construct a new URI that follows the generic URI syntax from its
7f561c08de6b Initial load
duke
parents:
diff changeset
   367
  * component parts. Each component is validated for syntax and some
7f561c08de6b Initial load
duke
parents:
diff changeset
   368
  * basic semantic checks are performed as well.  See the individual
7f561c08de6b Initial load
duke
parents:
diff changeset
   369
  * setter methods for specifics.
7f561c08de6b Initial load
duke
parents:
diff changeset
   370
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
   371
  * @param p_scheme the URI scheme (cannot be null or empty)
7f561c08de6b Initial load
duke
parents:
diff changeset
   372
  * @param p_host the hostname, IPv4 address or IPv6 reference for the URI
7f561c08de6b Initial load
duke
parents:
diff changeset
   373
  * @param p_path the URI path - if the path contains '?' or '#',
7f561c08de6b Initial load
duke
parents:
diff changeset
   374
  *               then the query string and/or fragment will be
7f561c08de6b Initial load
duke
parents:
diff changeset
   375
  *               set from the path; however, if the query and
7f561c08de6b Initial load
duke
parents:
diff changeset
   376
  *               fragment are specified both in the path and as
7f561c08de6b Initial load
duke
parents:
diff changeset
   377
  *               separate parameters, an exception is thrown
7f561c08de6b Initial load
duke
parents:
diff changeset
   378
  * @param p_queryString the URI query string (cannot be specified
7f561c08de6b Initial load
duke
parents:
diff changeset
   379
  *                      if path is null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   380
  * @param p_fragment the URI fragment (cannot be specified if path
7f561c08de6b Initial load
duke
parents:
diff changeset
   381
  *                   is null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   382
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
   383
  * @exception MalformedURIException if any of the parameters violates
7f561c08de6b Initial load
duke
parents:
diff changeset
   384
  *                                  syntax rules or semantic rules
7f561c08de6b Initial load
duke
parents:
diff changeset
   385
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
   386
  public URI(String p_scheme, String p_host, String p_path,
7f561c08de6b Initial load
duke
parents:
diff changeset
   387
             String p_queryString, String p_fragment)
7f561c08de6b Initial load
duke
parents:
diff changeset
   388
         throws MalformedURIException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   389
    this(p_scheme, null, p_host, -1, p_path, p_queryString, p_fragment);
7f561c08de6b Initial load
duke
parents:
diff changeset
   390
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   391
7f561c08de6b Initial load
duke
parents:
diff changeset
   392
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   393
  * Construct a new URI that follows the generic URI syntax from its
7f561c08de6b Initial load
duke
parents:
diff changeset
   394
  * component parts. Each component is validated for syntax and some
7f561c08de6b Initial load
duke
parents:
diff changeset
   395
  * basic semantic checks are performed as well.  See the individual
7f561c08de6b Initial load
duke
parents:
diff changeset
   396
  * setter methods for specifics.
7f561c08de6b Initial load
duke
parents:
diff changeset
   397
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
   398
  * @param p_scheme the URI scheme (cannot be null or empty)
7f561c08de6b Initial load
duke
parents:
diff changeset
   399
  * @param p_userinfo the URI userinfo (cannot be specified if host
7f561c08de6b Initial load
duke
parents:
diff changeset
   400
  *                   is null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   401
  * @param p_host the hostname, IPv4 address or IPv6 reference for the URI
7f561c08de6b Initial load
duke
parents:
diff changeset
   402
  * @param p_port the URI port (may be -1 for "unspecified"; cannot
7f561c08de6b Initial load
duke
parents:
diff changeset
   403
  *               be specified if host is null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   404
  * @param p_path the URI path - if the path contains '?' or '#',
7f561c08de6b Initial load
duke
parents:
diff changeset
   405
  *               then the query string and/or fragment will be
7f561c08de6b Initial load
duke
parents:
diff changeset
   406
  *               set from the path; however, if the query and
7f561c08de6b Initial load
duke
parents:
diff changeset
   407
  *               fragment are specified both in the path and as
7f561c08de6b Initial load
duke
parents:
diff changeset
   408
  *               separate parameters, an exception is thrown
7f561c08de6b Initial load
duke
parents:
diff changeset
   409
  * @param p_queryString the URI query string (cannot be specified
7f561c08de6b Initial load
duke
parents:
diff changeset
   410
  *                      if path is null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   411
  * @param p_fragment the URI fragment (cannot be specified if path
7f561c08de6b Initial load
duke
parents:
diff changeset
   412
  *                   is null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   413
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
   414
  * @exception MalformedURIException if any of the parameters violates
7f561c08de6b Initial load
duke
parents:
diff changeset
   415
  *                                  syntax rules or semantic rules
7f561c08de6b Initial load
duke
parents:
diff changeset
   416
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
   417
  public URI(String p_scheme, String p_userinfo,
7f561c08de6b Initial load
duke
parents:
diff changeset
   418
             String p_host, int p_port, String p_path,
7f561c08de6b Initial load
duke
parents:
diff changeset
   419
             String p_queryString, String p_fragment)
7f561c08de6b Initial load
duke
parents:
diff changeset
   420
         throws MalformedURIException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   421
    if (p_scheme == null || p_scheme.trim().length() == 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   422
      throw new MalformedURIException("Scheme is required!");
7f561c08de6b Initial load
duke
parents:
diff changeset
   423
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   424
7f561c08de6b Initial load
duke
parents:
diff changeset
   425
    if (p_host == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   426
      if (p_userinfo != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   427
        throw new MalformedURIException(
7f561c08de6b Initial load
duke
parents:
diff changeset
   428
             "Userinfo may not be specified if host is not specified!");
7f561c08de6b Initial load
duke
parents:
diff changeset
   429
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   430
      if (p_port != -1) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   431
        throw new MalformedURIException(
7f561c08de6b Initial load
duke
parents:
diff changeset
   432
             "Port may not be specified if host is not specified!");
7f561c08de6b Initial load
duke
parents:
diff changeset
   433
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   434
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   435
7f561c08de6b Initial load
duke
parents:
diff changeset
   436
    if (p_path != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   437
      if (p_path.indexOf('?') != -1 && p_queryString != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   438
        throw new MalformedURIException(
7f561c08de6b Initial load
duke
parents:
diff changeset
   439
          "Query string cannot be specified in path and query string!");
7f561c08de6b Initial load
duke
parents:
diff changeset
   440
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   441
7f561c08de6b Initial load
duke
parents:
diff changeset
   442
      if (p_path.indexOf('#') != -1 && p_fragment != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   443
        throw new MalformedURIException(
7f561c08de6b Initial load
duke
parents:
diff changeset
   444
          "Fragment cannot be specified in both the path and fragment!");
7f561c08de6b Initial load
duke
parents:
diff changeset
   445
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   446
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   447
7f561c08de6b Initial load
duke
parents:
diff changeset
   448
    setScheme(p_scheme);
7f561c08de6b Initial load
duke
parents:
diff changeset
   449
    setHost(p_host);
7f561c08de6b Initial load
duke
parents:
diff changeset
   450
    setPort(p_port);
7f561c08de6b Initial load
duke
parents:
diff changeset
   451
    setUserinfo(p_userinfo);
7f561c08de6b Initial load
duke
parents:
diff changeset
   452
    setPath(p_path);
7f561c08de6b Initial load
duke
parents:
diff changeset
   453
    setQueryString(p_queryString);
7f561c08de6b Initial load
duke
parents:
diff changeset
   454
    setFragment(p_fragment);
7f561c08de6b Initial load
duke
parents:
diff changeset
   455
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   456
7f561c08de6b Initial load
duke
parents:
diff changeset
   457
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   458
  * Initialize all fields of this URI from another URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
   459
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
   460
  * @param p_other the URI to copy (cannot be null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   461
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
   462
  private void initialize(URI p_other) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   463
    m_scheme = p_other.getScheme();
7f561c08de6b Initial load
duke
parents:
diff changeset
   464
    m_userinfo = p_other.getUserinfo();
7f561c08de6b Initial load
duke
parents:
diff changeset
   465
    m_host = p_other.getHost();
7f561c08de6b Initial load
duke
parents:
diff changeset
   466
    m_port = p_other.getPort();
7f561c08de6b Initial load
duke
parents:
diff changeset
   467
    m_regAuthority = p_other.getRegBasedAuthority();
7f561c08de6b Initial load
duke
parents:
diff changeset
   468
    m_path = p_other.getPath();
7f561c08de6b Initial load
duke
parents:
diff changeset
   469
    m_queryString = p_other.getQueryString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   470
    m_fragment = p_other.getFragment();
7f561c08de6b Initial load
duke
parents:
diff changeset
   471
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   472
7f561c08de6b Initial load
duke
parents:
diff changeset
   473
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   474
   * Initializes this URI from a base URI and a URI specification string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   475
   * See RFC 2396 Section 4 and Appendix B for specifications on parsing
7f561c08de6b Initial load
duke
parents:
diff changeset
   476
   * the URI and Section 5 for specifications on resolving relative URIs
7f561c08de6b Initial load
duke
parents:
diff changeset
   477
   * and relative paths.
7f561c08de6b Initial load
duke
parents:
diff changeset
   478
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   479
   * @param p_base the base URI (may be null if p_uriSpec is an absolute
7f561c08de6b Initial load
duke
parents:
diff changeset
   480
   *               URI)
7f561c08de6b Initial load
duke
parents:
diff changeset
   481
   * @param p_uriSpec the URI spec string which may be an absolute or
7f561c08de6b Initial load
duke
parents:
diff changeset
   482
   *                  relative URI (can only be null/empty if p_base
7f561c08de6b Initial load
duke
parents:
diff changeset
   483
   *                  is not null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   484
   * @param allowNonAbsoluteURI true to permit non-absolute URIs,
7f561c08de6b Initial load
duke
parents:
diff changeset
   485
   *                         in case of relative URI, false otherwise.
7f561c08de6b Initial load
duke
parents:
diff changeset
   486
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   487
   * @exception MalformedURIException if p_base is null and p_uriSpec
7f561c08de6b Initial load
duke
parents:
diff changeset
   488
   *                                  is not an absolute URI or if
7f561c08de6b Initial load
duke
parents:
diff changeset
   489
   *                                  p_uriSpec violates syntax rules
7f561c08de6b Initial load
duke
parents:
diff changeset
   490
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   491
  private void initialize(URI p_base, String p_uriSpec, boolean allowNonAbsoluteURI)
7f561c08de6b Initial load
duke
parents:
diff changeset
   492
      throws MalformedURIException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   493
7f561c08de6b Initial load
duke
parents:
diff changeset
   494
      String uriSpec = p_uriSpec;
7f561c08de6b Initial load
duke
parents:
diff changeset
   495
      int uriSpecLen = (uriSpec != null) ? uriSpec.length() : 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   496
7f561c08de6b Initial load
duke
parents:
diff changeset
   497
      if (p_base == null && uriSpecLen == 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   498
          if (allowNonAbsoluteURI) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   499
              m_path = "";
7f561c08de6b Initial load
duke
parents:
diff changeset
   500
              return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   501
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
   502
          throw new MalformedURIException("Cannot initialize URI with empty parameters.");
7f561c08de6b Initial load
duke
parents:
diff changeset
   503
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   504
7f561c08de6b Initial load
duke
parents:
diff changeset
   505
      // just make a copy of the base if spec is empty
7f561c08de6b Initial load
duke
parents:
diff changeset
   506
      if (uriSpecLen == 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   507
          initialize(p_base);
7f561c08de6b Initial load
duke
parents:
diff changeset
   508
          return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   509
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   510
7f561c08de6b Initial load
duke
parents:
diff changeset
   511
      int index = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   512
7f561c08de6b Initial load
duke
parents:
diff changeset
   513
      // Check for scheme, which must be before '/', '?' or '#'.
7f561c08de6b Initial load
duke
parents:
diff changeset
   514
      int colonIdx = uriSpec.indexOf(':');
7f561c08de6b Initial load
duke
parents:
diff changeset
   515
      if (colonIdx != -1) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   516
          final int searchFrom = colonIdx - 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   517
          // search backwards starting from character before ':'.
7f561c08de6b Initial load
duke
parents:
diff changeset
   518
          int slashIdx = uriSpec.lastIndexOf('/', searchFrom);
7f561c08de6b Initial load
duke
parents:
diff changeset
   519
          int queryIdx = uriSpec.lastIndexOf('?', searchFrom);
7f561c08de6b Initial load
duke
parents:
diff changeset
   520
          int fragmentIdx = uriSpec.lastIndexOf('#', searchFrom);
7f561c08de6b Initial load
duke
parents:
diff changeset
   521
7f561c08de6b Initial load
duke
parents:
diff changeset
   522
          if (colonIdx == 0 || slashIdx != -1 ||
7f561c08de6b Initial load
duke
parents:
diff changeset
   523
              queryIdx != -1 || fragmentIdx != -1) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   524
              // A standalone base is a valid URI according to spec
7f561c08de6b Initial load
duke
parents:
diff changeset
   525
              if (colonIdx == 0 || (p_base == null && fragmentIdx != 0 && !allowNonAbsoluteURI)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   526
                  throw new MalformedURIException("No scheme found in URI.");
7f561c08de6b Initial load
duke
parents:
diff changeset
   527
              }
7f561c08de6b Initial load
duke
parents:
diff changeset
   528
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
   529
          else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   530
              initializeScheme(uriSpec);
7f561c08de6b Initial load
duke
parents:
diff changeset
   531
              index = m_scheme.length()+1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   532
7f561c08de6b Initial load
duke
parents:
diff changeset
   533
              // Neither 'scheme:' or 'scheme:#fragment' are valid URIs.
7f561c08de6b Initial load
duke
parents:
diff changeset
   534
              if (colonIdx == uriSpecLen - 1 || uriSpec.charAt(colonIdx+1) == '#') {
7f561c08de6b Initial load
duke
parents:
diff changeset
   535
                  throw new MalformedURIException("Scheme specific part cannot be empty.");
7f561c08de6b Initial load
duke
parents:
diff changeset
   536
              }
7f561c08de6b Initial load
duke
parents:
diff changeset
   537
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
   538
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   539
      else if (p_base == null && uriSpec.indexOf('#') != 0 && !allowNonAbsoluteURI) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   540
          throw new MalformedURIException("No scheme found in URI.");
7f561c08de6b Initial load
duke
parents:
diff changeset
   541
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   542
7f561c08de6b Initial load
duke
parents:
diff changeset
   543
      // Two slashes means we may have authority, but definitely means we're either
7f561c08de6b Initial load
duke
parents:
diff changeset
   544
      // matching net_path or abs_path. These two productions are ambiguous in that
7f561c08de6b Initial load
duke
parents:
diff changeset
   545
      // every net_path (except those containing an IPv6Reference) is an abs_path.
7f561c08de6b Initial load
duke
parents:
diff changeset
   546
      // RFC 2396 resolves this ambiguity by applying a greedy left most matching rule.
7f561c08de6b Initial load
duke
parents:
diff changeset
   547
      // Try matching net_path first, and if that fails we don't have authority so
7f561c08de6b Initial load
duke
parents:
diff changeset
   548
      // then attempt to match abs_path.
7f561c08de6b Initial load
duke
parents:
diff changeset
   549
      //
7f561c08de6b Initial load
duke
parents:
diff changeset
   550
      // net_path = "//" authority [ abs_path ]
7f561c08de6b Initial load
duke
parents:
diff changeset
   551
      // abs_path = "/"  path_segments
7f561c08de6b Initial load
duke
parents:
diff changeset
   552
      if (((index+1) < uriSpecLen) &&
7f561c08de6b Initial load
duke
parents:
diff changeset
   553
          (uriSpec.charAt(index) == '/' && uriSpec.charAt(index+1) == '/')) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   554
          index += 2;
7f561c08de6b Initial load
duke
parents:
diff changeset
   555
          int startPos = index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   556
7f561c08de6b Initial load
duke
parents:
diff changeset
   557
          // Authority will be everything up to path, query or fragment
7f561c08de6b Initial load
duke
parents:
diff changeset
   558
          char testChar = '\0';
7f561c08de6b Initial load
duke
parents:
diff changeset
   559
          while (index < uriSpecLen) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   560
              testChar = uriSpec.charAt(index);
7f561c08de6b Initial load
duke
parents:
diff changeset
   561
              if (testChar == '/' || testChar == '?' || testChar == '#') {
7f561c08de6b Initial load
duke
parents:
diff changeset
   562
                  break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   563
              }
7f561c08de6b Initial load
duke
parents:
diff changeset
   564
              index++;
7f561c08de6b Initial load
duke
parents:
diff changeset
   565
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
   566
7f561c08de6b Initial load
duke
parents:
diff changeset
   567
          // Attempt to parse authority. If the section is an empty string
7f561c08de6b Initial load
duke
parents:
diff changeset
   568
          // this is a valid server based authority, so set the host to this
7f561c08de6b Initial load
duke
parents:
diff changeset
   569
          // value.
7f561c08de6b Initial load
duke
parents:
diff changeset
   570
          if (index > startPos) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   571
              // If we didn't find authority we need to back up. Attempt to
7f561c08de6b Initial load
duke
parents:
diff changeset
   572
              // match against abs_path next.
7f561c08de6b Initial load
duke
parents:
diff changeset
   573
              if (!initializeAuthority(uriSpec.substring(startPos, index))) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   574
                  index = startPos - 2;
7f561c08de6b Initial load
duke
parents:
diff changeset
   575
              }
7f561c08de6b Initial load
duke
parents:
diff changeset
   576
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
   577
          else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   578
              m_host = "";
7f561c08de6b Initial load
duke
parents:
diff changeset
   579
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
   580
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   581
7f561c08de6b Initial load
duke
parents:
diff changeset
   582
      initializePath(uriSpec, index);
7f561c08de6b Initial load
duke
parents:
diff changeset
   583
7f561c08de6b Initial load
duke
parents:
diff changeset
   584
      // Resolve relative URI to base URI - see RFC 2396 Section 5.2
7f561c08de6b Initial load
duke
parents:
diff changeset
   585
      // In some cases, it might make more sense to throw an exception
7f561c08de6b Initial load
duke
parents:
diff changeset
   586
      // (when scheme is specified is the string spec and the base URI
7f561c08de6b Initial load
duke
parents:
diff changeset
   587
      // is also specified, for example), but we're just following the
7f561c08de6b Initial load
duke
parents:
diff changeset
   588
      // RFC specifications
7f561c08de6b Initial load
duke
parents:
diff changeset
   589
      if (p_base != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   590
          absolutize(p_base);
7f561c08de6b Initial load
duke
parents:
diff changeset
   591
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   592
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   593
7f561c08de6b Initial load
duke
parents:
diff changeset
   594
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   595
  * Initializes this URI from a base URI and a URI specification string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   596
  * See RFC 2396 Section 4 and Appendix B for specifications on parsing
7f561c08de6b Initial load
duke
parents:
diff changeset
   597
  * the URI and Section 5 for specifications on resolving relative URIs
7f561c08de6b Initial load
duke
parents:
diff changeset
   598
  * and relative paths.
7f561c08de6b Initial load
duke
parents:
diff changeset
   599
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
   600
  * @param p_base the base URI (may be null if p_uriSpec is an absolute
7f561c08de6b Initial load
duke
parents:
diff changeset
   601
  *               URI)
7f561c08de6b Initial load
duke
parents:
diff changeset
   602
  * @param p_uriSpec the URI spec string which may be an absolute or
7f561c08de6b Initial load
duke
parents:
diff changeset
   603
  *                  relative URI (can only be null/empty if p_base
7f561c08de6b Initial load
duke
parents:
diff changeset
   604
  *                  is not null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   605
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
   606
  * @exception MalformedURIException if p_base is null and p_uriSpec
7f561c08de6b Initial load
duke
parents:
diff changeset
   607
  *                                  is not an absolute URI or if
7f561c08de6b Initial load
duke
parents:
diff changeset
   608
  *                                  p_uriSpec violates syntax rules
7f561c08de6b Initial load
duke
parents:
diff changeset
   609
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
   610
  private void initialize(URI p_base, String p_uriSpec)
7f561c08de6b Initial load
duke
parents:
diff changeset
   611
                         throws MalformedURIException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   612
7f561c08de6b Initial load
duke
parents:
diff changeset
   613
    String uriSpec = p_uriSpec;
7f561c08de6b Initial load
duke
parents:
diff changeset
   614
    int uriSpecLen = (uriSpec != null) ? uriSpec.length() : 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   615
7f561c08de6b Initial load
duke
parents:
diff changeset
   616
    if (p_base == null && uriSpecLen == 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   617
      throw new MalformedURIException(
7f561c08de6b Initial load
duke
parents:
diff changeset
   618
                  "Cannot initialize URI with empty parameters.");
7f561c08de6b Initial load
duke
parents:
diff changeset
   619
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   620
7f561c08de6b Initial load
duke
parents:
diff changeset
   621
    // just make a copy of the base if spec is empty
7f561c08de6b Initial load
duke
parents:
diff changeset
   622
    if (uriSpecLen == 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   623
      initialize(p_base);
7f561c08de6b Initial load
duke
parents:
diff changeset
   624
      return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   625
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   626
7f561c08de6b Initial load
duke
parents:
diff changeset
   627
    int index = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   628
7f561c08de6b Initial load
duke
parents:
diff changeset
   629
    // Check for scheme, which must be before '/', '?' or '#'.
7f561c08de6b Initial load
duke
parents:
diff changeset
   630
    int colonIdx = uriSpec.indexOf(':');
7f561c08de6b Initial load
duke
parents:
diff changeset
   631
    if (colonIdx != -1) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   632
        final int searchFrom = colonIdx - 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   633
        // search backwards starting from character before ':'.
7f561c08de6b Initial load
duke
parents:
diff changeset
   634
        int slashIdx = uriSpec.lastIndexOf('/', searchFrom);
7f561c08de6b Initial load
duke
parents:
diff changeset
   635
        int queryIdx = uriSpec.lastIndexOf('?', searchFrom);
7f561c08de6b Initial load
duke
parents:
diff changeset
   636
        int fragmentIdx = uriSpec.lastIndexOf('#', searchFrom);
7f561c08de6b Initial load
duke
parents:
diff changeset
   637
7f561c08de6b Initial load
duke
parents:
diff changeset
   638
        if (colonIdx == 0 || slashIdx != -1 ||
7f561c08de6b Initial load
duke
parents:
diff changeset
   639
            queryIdx != -1 || fragmentIdx != -1) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   640
            // A standalone base is a valid URI according to spec
7f561c08de6b Initial load
duke
parents:
diff changeset
   641
            if (colonIdx == 0 || (p_base == null && fragmentIdx != 0)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   642
                throw new MalformedURIException("No scheme found in URI.");
7f561c08de6b Initial load
duke
parents:
diff changeset
   643
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   644
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   645
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   646
            initializeScheme(uriSpec);
7f561c08de6b Initial load
duke
parents:
diff changeset
   647
            index = m_scheme.length()+1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   648
7f561c08de6b Initial load
duke
parents:
diff changeset
   649
            // Neither 'scheme:' or 'scheme:#fragment' are valid URIs.
7f561c08de6b Initial load
duke
parents:
diff changeset
   650
            if (colonIdx == uriSpecLen - 1 || uriSpec.charAt(colonIdx+1) == '#') {
7f561c08de6b Initial load
duke
parents:
diff changeset
   651
                throw new MalformedURIException("Scheme specific part cannot be empty.");
7f561c08de6b Initial load
duke
parents:
diff changeset
   652
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   653
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   654
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   655
    else if (p_base == null && uriSpec.indexOf('#') != 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   656
        throw new MalformedURIException("No scheme found in URI.");
7f561c08de6b Initial load
duke
parents:
diff changeset
   657
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   658
7f561c08de6b Initial load
duke
parents:
diff changeset
   659
    // Two slashes means we may have authority, but definitely means we're either
7f561c08de6b Initial load
duke
parents:
diff changeset
   660
    // matching net_path or abs_path. These two productions are ambiguous in that
7f561c08de6b Initial load
duke
parents:
diff changeset
   661
    // every net_path (except those containing an IPv6Reference) is an abs_path.
7f561c08de6b Initial load
duke
parents:
diff changeset
   662
    // RFC 2396 resolves this ambiguity by applying a greedy left most matching rule.
7f561c08de6b Initial load
duke
parents:
diff changeset
   663
    // Try matching net_path first, and if that fails we don't have authority so
7f561c08de6b Initial load
duke
parents:
diff changeset
   664
    // then attempt to match abs_path.
7f561c08de6b Initial load
duke
parents:
diff changeset
   665
    //
7f561c08de6b Initial load
duke
parents:
diff changeset
   666
    // net_path = "//" authority [ abs_path ]
7f561c08de6b Initial load
duke
parents:
diff changeset
   667
    // abs_path = "/"  path_segments
7f561c08de6b Initial load
duke
parents:
diff changeset
   668
    if (((index+1) < uriSpecLen) &&
7f561c08de6b Initial load
duke
parents:
diff changeset
   669
        (uriSpec.charAt(index) == '/' && uriSpec.charAt(index+1) == '/')) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   670
      index += 2;
7f561c08de6b Initial load
duke
parents:
diff changeset
   671
      int startPos = index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   672
7f561c08de6b Initial load
duke
parents:
diff changeset
   673
      // Authority will be everything up to path, query or fragment
7f561c08de6b Initial load
duke
parents:
diff changeset
   674
      char testChar = '\0';
7f561c08de6b Initial load
duke
parents:
diff changeset
   675
      while (index < uriSpecLen) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   676
        testChar = uriSpec.charAt(index);
7f561c08de6b Initial load
duke
parents:
diff changeset
   677
        if (testChar == '/' || testChar == '?' || testChar == '#') {
7f561c08de6b Initial load
duke
parents:
diff changeset
   678
          break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   679
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   680
        index++;
7f561c08de6b Initial load
duke
parents:
diff changeset
   681
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   682
7f561c08de6b Initial load
duke
parents:
diff changeset
   683
      // Attempt to parse authority. If the section is an empty string
7f561c08de6b Initial load
duke
parents:
diff changeset
   684
      // this is a valid server based authority, so set the host to this
7f561c08de6b Initial load
duke
parents:
diff changeset
   685
      // value.
7f561c08de6b Initial load
duke
parents:
diff changeset
   686
      if (index > startPos) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   687
        // If we didn't find authority we need to back up. Attempt to
7f561c08de6b Initial load
duke
parents:
diff changeset
   688
        // match against abs_path next.
7f561c08de6b Initial load
duke
parents:
diff changeset
   689
        if (!initializeAuthority(uriSpec.substring(startPos, index))) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   690
          index = startPos - 2;
7f561c08de6b Initial load
duke
parents:
diff changeset
   691
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   692
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   693
      else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   694
        m_host = "";
7f561c08de6b Initial load
duke
parents:
diff changeset
   695
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   696
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   697
7f561c08de6b Initial load
duke
parents:
diff changeset
   698
    initializePath(uriSpec, index);
7f561c08de6b Initial load
duke
parents:
diff changeset
   699
7f561c08de6b Initial load
duke
parents:
diff changeset
   700
    // Resolve relative URI to base URI - see RFC 2396 Section 5.2
7f561c08de6b Initial load
duke
parents:
diff changeset
   701
    // In some cases, it might make more sense to throw an exception
7f561c08de6b Initial load
duke
parents:
diff changeset
   702
    // (when scheme is specified is the string spec and the base URI
7f561c08de6b Initial load
duke
parents:
diff changeset
   703
    // is also specified, for example), but we're just following the
7f561c08de6b Initial load
duke
parents:
diff changeset
   704
    // RFC specifications
7f561c08de6b Initial load
duke
parents:
diff changeset
   705
    if (p_base != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   706
        absolutize(p_base);
7f561c08de6b Initial load
duke
parents:
diff changeset
   707
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   708
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   709
7f561c08de6b Initial load
duke
parents:
diff changeset
   710
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   711
   * Absolutize URI with given base URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
   712
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   713
   * @param p_base base URI for absolutization
7f561c08de6b Initial load
duke
parents:
diff changeset
   714
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   715
  public void absolutize(URI p_base) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   716
7f561c08de6b Initial load
duke
parents:
diff changeset
   717
      // check to see if this is the current doc - RFC 2396 5.2 #2
7f561c08de6b Initial load
duke
parents:
diff changeset
   718
      // note that this is slightly different from the RFC spec in that
7f561c08de6b Initial load
duke
parents:
diff changeset
   719
      // we don't include the check for query string being null
7f561c08de6b Initial load
duke
parents:
diff changeset
   720
      // - this handles cases where the urispec is just a query
7f561c08de6b Initial load
duke
parents:
diff changeset
   721
      // string or a fragment (e.g. "?y" or "#s") -
7f561c08de6b Initial load
duke
parents:
diff changeset
   722
      // see <http://www.ics.uci.edu/~fielding/url/test1.html> which
7f561c08de6b Initial load
duke
parents:
diff changeset
   723
      // identified this as a bug in the RFC
7f561c08de6b Initial load
duke
parents:
diff changeset
   724
      if (m_path.length() == 0 && m_scheme == null &&
7f561c08de6b Initial load
duke
parents:
diff changeset
   725
          m_host == null && m_regAuthority == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   726
          m_scheme = p_base.getScheme();
7f561c08de6b Initial load
duke
parents:
diff changeset
   727
          m_userinfo = p_base.getUserinfo();
7f561c08de6b Initial load
duke
parents:
diff changeset
   728
          m_host = p_base.getHost();
7f561c08de6b Initial load
duke
parents:
diff changeset
   729
          m_port = p_base.getPort();
7f561c08de6b Initial load
duke
parents:
diff changeset
   730
          m_regAuthority = p_base.getRegBasedAuthority();
7f561c08de6b Initial load
duke
parents:
diff changeset
   731
          m_path = p_base.getPath();
7f561c08de6b Initial load
duke
parents:
diff changeset
   732
7f561c08de6b Initial load
duke
parents:
diff changeset
   733
          if (m_queryString == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   734
              m_queryString = p_base.getQueryString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   735
7f561c08de6b Initial load
duke
parents:
diff changeset
   736
              if (m_fragment == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   737
                  m_fragment = p_base.getFragment();
7f561c08de6b Initial load
duke
parents:
diff changeset
   738
              }
7f561c08de6b Initial load
duke
parents:
diff changeset
   739
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
   740
          return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   741
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   742
7f561c08de6b Initial load
duke
parents:
diff changeset
   743
      // check for scheme - RFC 2396 5.2 #3
7f561c08de6b Initial load
duke
parents:
diff changeset
   744
      // if we found a scheme, it means absolute URI, so we're done
7f561c08de6b Initial load
duke
parents:
diff changeset
   745
      if (m_scheme == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   746
          m_scheme = p_base.getScheme();
7f561c08de6b Initial load
duke
parents:
diff changeset
   747
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   748
      else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   749
          return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   750
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   751
7f561c08de6b Initial load
duke
parents:
diff changeset
   752
      // check for authority - RFC 2396 5.2 #4
7f561c08de6b Initial load
duke
parents:
diff changeset
   753
      // if we found a host, then we've got a network path, so we're done
7f561c08de6b Initial load
duke
parents:
diff changeset
   754
      if (m_host == null && m_regAuthority == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   755
          m_userinfo = p_base.getUserinfo();
7f561c08de6b Initial load
duke
parents:
diff changeset
   756
          m_host = p_base.getHost();
7f561c08de6b Initial load
duke
parents:
diff changeset
   757
          m_port = p_base.getPort();
7f561c08de6b Initial load
duke
parents:
diff changeset
   758
          m_regAuthority = p_base.getRegBasedAuthority();
7f561c08de6b Initial load
duke
parents:
diff changeset
   759
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   760
      else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   761
          return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   762
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   763
7f561c08de6b Initial load
duke
parents:
diff changeset
   764
      // check for absolute path - RFC 2396 5.2 #5
7f561c08de6b Initial load
duke
parents:
diff changeset
   765
      if (m_path.length() > 0 &&
7f561c08de6b Initial load
duke
parents:
diff changeset
   766
              m_path.startsWith("/")) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   767
          return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   768
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   769
7f561c08de6b Initial load
duke
parents:
diff changeset
   770
      // if we get to this point, we need to resolve relative path
7f561c08de6b Initial load
duke
parents:
diff changeset
   771
      // RFC 2396 5.2 #6
7f561c08de6b Initial load
duke
parents:
diff changeset
   772
      String path = "";
7f561c08de6b Initial load
duke
parents:
diff changeset
   773
      String basePath = p_base.getPath();
7f561c08de6b Initial load
duke
parents:
diff changeset
   774
7f561c08de6b Initial load
duke
parents:
diff changeset
   775
      // 6a - get all but the last segment of the base URI path
7f561c08de6b Initial load
duke
parents:
diff changeset
   776
      if (basePath != null && basePath.length() > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   777
          int lastSlash = basePath.lastIndexOf('/');
7f561c08de6b Initial load
duke
parents:
diff changeset
   778
          if (lastSlash != -1) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   779
              path = basePath.substring(0, lastSlash+1);
7f561c08de6b Initial load
duke
parents:
diff changeset
   780
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
   781
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   782
      else if (m_path.length() > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   783
          path = "/";
7f561c08de6b Initial load
duke
parents:
diff changeset
   784
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   785
7f561c08de6b Initial load
duke
parents:
diff changeset
   786
      // 6b - append the relative URI path
7f561c08de6b Initial load
duke
parents:
diff changeset
   787
      path = path.concat(m_path);
7f561c08de6b Initial load
duke
parents:
diff changeset
   788
7f561c08de6b Initial load
duke
parents:
diff changeset
   789
      // 6c - remove all "./" where "." is a complete path segment
7f561c08de6b Initial load
duke
parents:
diff changeset
   790
      int index = -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   791
      while ((index = path.indexOf("/./")) != -1) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   792
          path = path.substring(0, index+1).concat(path.substring(index+3));
7f561c08de6b Initial load
duke
parents:
diff changeset
   793
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   794
7f561c08de6b Initial load
duke
parents:
diff changeset
   795
      // 6d - remove "." if path ends with "." as a complete path segment
7f561c08de6b Initial load
duke
parents:
diff changeset
   796
      if (path.endsWith("/.")) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   797
          path = path.substring(0, path.length()-1);
7f561c08de6b Initial load
duke
parents:
diff changeset
   798
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   799
7f561c08de6b Initial load
duke
parents:
diff changeset
   800
      // 6e - remove all "<segment>/../" where "<segment>" is a complete
7f561c08de6b Initial load
duke
parents:
diff changeset
   801
      // path segment not equal to ".."
7f561c08de6b Initial load
duke
parents:
diff changeset
   802
      index = 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   803
      int segIndex = -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   804
      String tempString = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   805
7f561c08de6b Initial load
duke
parents:
diff changeset
   806
      while ((index = path.indexOf("/../", index)) > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   807
          tempString = path.substring(0, path.indexOf("/../"));
7f561c08de6b Initial load
duke
parents:
diff changeset
   808
          segIndex = tempString.lastIndexOf('/');
7f561c08de6b Initial load
duke
parents:
diff changeset
   809
          if (segIndex != -1) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   810
              if (!tempString.substring(segIndex).equals("..")) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   811
                  path = path.substring(0, segIndex+1).concat(path.substring(index+4));
7f561c08de6b Initial load
duke
parents:
diff changeset
   812
                  index = segIndex;
7f561c08de6b Initial load
duke
parents:
diff changeset
   813
              }
7f561c08de6b Initial load
duke
parents:
diff changeset
   814
              else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   815
                  index += 4;
7f561c08de6b Initial load
duke
parents:
diff changeset
   816
              }
7f561c08de6b Initial load
duke
parents:
diff changeset
   817
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
   818
          else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   819
              index += 4;
7f561c08de6b Initial load
duke
parents:
diff changeset
   820
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
   821
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   822
7f561c08de6b Initial load
duke
parents:
diff changeset
   823
      // 6f - remove ending "<segment>/.." where "<segment>" is a
7f561c08de6b Initial load
duke
parents:
diff changeset
   824
      // complete path segment
7f561c08de6b Initial load
duke
parents:
diff changeset
   825
      if (path.endsWith("/..")) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   826
          tempString = path.substring(0, path.length()-3);
7f561c08de6b Initial load
duke
parents:
diff changeset
   827
          segIndex = tempString.lastIndexOf('/');
7f561c08de6b Initial load
duke
parents:
diff changeset
   828
          if (segIndex != -1) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   829
              path = path.substring(0, segIndex+1);
7f561c08de6b Initial load
duke
parents:
diff changeset
   830
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
   831
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   832
      m_path = path;
7f561c08de6b Initial load
duke
parents:
diff changeset
   833
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   834
7f561c08de6b Initial load
duke
parents:
diff changeset
   835
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   836
  * Initialize the scheme for this URI from a URI string spec.
7f561c08de6b Initial load
duke
parents:
diff changeset
   837
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
   838
  * @param p_uriSpec the URI specification (cannot be null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   839
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
   840
  * @exception MalformedURIException if URI does not have a conformant
7f561c08de6b Initial load
duke
parents:
diff changeset
   841
  *                                  scheme
7f561c08de6b Initial load
duke
parents:
diff changeset
   842
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
   843
  private void initializeScheme(String p_uriSpec)
7f561c08de6b Initial load
duke
parents:
diff changeset
   844
                 throws MalformedURIException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   845
    int uriSpecLen = p_uriSpec.length();
7f561c08de6b Initial load
duke
parents:
diff changeset
   846
    int index = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   847
    String scheme = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   848
    char testChar = '\0';
7f561c08de6b Initial load
duke
parents:
diff changeset
   849
7f561c08de6b Initial load
duke
parents:
diff changeset
   850
    while (index < uriSpecLen) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   851
      testChar = p_uriSpec.charAt(index);
7f561c08de6b Initial load
duke
parents:
diff changeset
   852
      if (testChar == ':' || testChar == '/' ||
7f561c08de6b Initial load
duke
parents:
diff changeset
   853
          testChar == '?' || testChar == '#') {
7f561c08de6b Initial load
duke
parents:
diff changeset
   854
        break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   855
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   856
      index++;
7f561c08de6b Initial load
duke
parents:
diff changeset
   857
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   858
    scheme = p_uriSpec.substring(0, index);
7f561c08de6b Initial load
duke
parents:
diff changeset
   859
7f561c08de6b Initial load
duke
parents:
diff changeset
   860
    if (scheme.length() == 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   861
      throw new MalformedURIException("No scheme found in URI.");
7f561c08de6b Initial load
duke
parents:
diff changeset
   862
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   863
    else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   864
      setScheme(scheme);
7f561c08de6b Initial load
duke
parents:
diff changeset
   865
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   866
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   867
7f561c08de6b Initial load
duke
parents:
diff changeset
   868
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   869
  * Initialize the authority (either server or registry based)
7f561c08de6b Initial load
duke
parents:
diff changeset
   870
  * for this URI from a URI string spec.
7f561c08de6b Initial load
duke
parents:
diff changeset
   871
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
   872
  * @param p_uriSpec the URI specification (cannot be null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   873
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
   874
  * @return true if the given string matched server or registry
7f561c08de6b Initial load
duke
parents:
diff changeset
   875
  * based authority
7f561c08de6b Initial load
duke
parents:
diff changeset
   876
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
   877
  private boolean initializeAuthority(String p_uriSpec) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   878
7f561c08de6b Initial load
duke
parents:
diff changeset
   879
    int index = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   880
    int start = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   881
    int end = p_uriSpec.length();
7f561c08de6b Initial load
duke
parents:
diff changeset
   882
7f561c08de6b Initial load
duke
parents:
diff changeset
   883
    char testChar = '\0';
7f561c08de6b Initial load
duke
parents:
diff changeset
   884
    String userinfo = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   885
7f561c08de6b Initial load
duke
parents:
diff changeset
   886
    // userinfo is everything up to @
7f561c08de6b Initial load
duke
parents:
diff changeset
   887
    if (p_uriSpec.indexOf('@', start) != -1) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   888
      while (index < end) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   889
        testChar = p_uriSpec.charAt(index);
7f561c08de6b Initial load
duke
parents:
diff changeset
   890
        if (testChar == '@') {
7f561c08de6b Initial load
duke
parents:
diff changeset
   891
          break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   892
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   893
        index++;
7f561c08de6b Initial load
duke
parents:
diff changeset
   894
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   895
      userinfo = p_uriSpec.substring(start, index);
7f561c08de6b Initial load
duke
parents:
diff changeset
   896
      index++;
7f561c08de6b Initial load
duke
parents:
diff changeset
   897
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   898
7f561c08de6b Initial load
duke
parents:
diff changeset
   899
    // host is everything up to last ':', or up to
7f561c08de6b Initial load
duke
parents:
diff changeset
   900
    // and including ']' if followed by ':'.
7f561c08de6b Initial load
duke
parents:
diff changeset
   901
    String host = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   902
    start = index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   903
    boolean hasPort = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   904
    if (index < end) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   905
      if (p_uriSpec.charAt(start) == '[') {
7f561c08de6b Initial load
duke
parents:
diff changeset
   906
        int bracketIndex = p_uriSpec.indexOf(']', start);
7f561c08de6b Initial load
duke
parents:
diff changeset
   907
        index = (bracketIndex != -1) ? bracketIndex : end;
7f561c08de6b Initial load
duke
parents:
diff changeset
   908
        if (index+1 < end && p_uriSpec.charAt(index+1) == ':') {
7f561c08de6b Initial load
duke
parents:
diff changeset
   909
          ++index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   910
          hasPort = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   911
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   912
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   913
          index = end;
7f561c08de6b Initial load
duke
parents:
diff changeset
   914
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   915
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   916
      else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   917
        int colonIndex = p_uriSpec.lastIndexOf(':', end);
7f561c08de6b Initial load
duke
parents:
diff changeset
   918
        index = (colonIndex > start) ? colonIndex : end;
7f561c08de6b Initial load
duke
parents:
diff changeset
   919
        hasPort = (index != end);
7f561c08de6b Initial load
duke
parents:
diff changeset
   920
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   921
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   922
    host = p_uriSpec.substring(start, index);
7f561c08de6b Initial load
duke
parents:
diff changeset
   923
    int port = -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   924
    if (host.length() > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   925
      // port
7f561c08de6b Initial load
duke
parents:
diff changeset
   926
      if (hasPort) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   927
        index++;
7f561c08de6b Initial load
duke
parents:
diff changeset
   928
        start = index;
7f561c08de6b Initial load
duke
parents:
diff changeset
   929
        while (index < end) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   930
          index++;
7f561c08de6b Initial load
duke
parents:
diff changeset
   931
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   932
        String portStr = p_uriSpec.substring(start, index);
7f561c08de6b Initial load
duke
parents:
diff changeset
   933
        if (portStr.length() > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   934
          // REVISIT: Remove this code.
7f561c08de6b Initial load
duke
parents:
diff changeset
   935
          /** for (int i = 0; i < portStr.length(); i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   936
            if (!isDigit(portStr.charAt(i))) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   937
              throw new MalformedURIException(
7f561c08de6b Initial load
duke
parents:
diff changeset
   938
                   portStr +
7f561c08de6b Initial load
duke
parents:
diff changeset
   939
                   " is invalid. Port should only contain digits!");
7f561c08de6b Initial load
duke
parents:
diff changeset
   940
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   941
          }**/
7f561c08de6b Initial load
duke
parents:
diff changeset
   942
          // REVISIT: Remove this code.
7f561c08de6b Initial load
duke
parents:
diff changeset
   943
          // Store port value as string instead of integer.
7f561c08de6b Initial load
duke
parents:
diff changeset
   944
          try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   945
            port = Integer.parseInt(portStr);
7f561c08de6b Initial load
duke
parents:
diff changeset
   946
            if (port == -1) --port;
7f561c08de6b Initial load
duke
parents:
diff changeset
   947
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
   948
          catch (NumberFormatException nfe) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   949
            port = -2;
7f561c08de6b Initial load
duke
parents:
diff changeset
   950
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
   951
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   952
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   953
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   954
7f561c08de6b Initial load
duke
parents:
diff changeset
   955
    if (isValidServerBasedAuthority(host, port, userinfo)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   956
      m_host = host;
7f561c08de6b Initial load
duke
parents:
diff changeset
   957
      m_port = port;
7f561c08de6b Initial load
duke
parents:
diff changeset
   958
      m_userinfo = userinfo;
7f561c08de6b Initial load
duke
parents:
diff changeset
   959
      return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   960
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   961
    // Note: Registry based authority is being removed from a
7f561c08de6b Initial load
duke
parents:
diff changeset
   962
    // new spec for URI which would obsolete RFC 2396. If the
7f561c08de6b Initial load
duke
parents:
diff changeset
   963
    // spec is added to XML errata, processing of reg_name
7f561c08de6b Initial load
duke
parents:
diff changeset
   964
    // needs to be removed. - mrglavas.
7f561c08de6b Initial load
duke
parents:
diff changeset
   965
    else if (isValidRegistryBasedAuthority(p_uriSpec)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   966
      m_regAuthority = p_uriSpec;
7f561c08de6b Initial load
duke
parents:
diff changeset
   967
      return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   968
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   969
    return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   970
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   971
7f561c08de6b Initial load
duke
parents:
diff changeset
   972
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   973
   * Determines whether the components host, port, and user info
7f561c08de6b Initial load
duke
parents:
diff changeset
   974
   * are valid as a server authority.
7f561c08de6b Initial load
duke
parents:
diff changeset
   975
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   976
   * @param host the host component of authority
7f561c08de6b Initial load
duke
parents:
diff changeset
   977
   * @param port the port number component of authority
7f561c08de6b Initial load
duke
parents:
diff changeset
   978
   * @param userinfo the user info component of authority
7f561c08de6b Initial load
duke
parents:
diff changeset
   979
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   980
   * @return true if the given host, port, and userinfo compose
7f561c08de6b Initial load
duke
parents:
diff changeset
   981
   * a valid server authority
7f561c08de6b Initial load
duke
parents:
diff changeset
   982
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   983
  private boolean isValidServerBasedAuthority(String host, int port, String userinfo) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   984
7f561c08de6b Initial load
duke
parents:
diff changeset
   985
    // Check if the host is well formed.
7f561c08de6b Initial load
duke
parents:
diff changeset
   986
    if (!isWellFormedAddress(host)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   987
      return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   988
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   989
7f561c08de6b Initial load
duke
parents:
diff changeset
   990
    // Check that port is well formed if it exists.
7f561c08de6b Initial load
duke
parents:
diff changeset
   991
    // REVISIT: There's no restriction on port value ranges, but
7f561c08de6b Initial load
duke
parents:
diff changeset
   992
    // perform the same check as in setPort to be consistent. Pass
7f561c08de6b Initial load
duke
parents:
diff changeset
   993
    // in a string to this method instead of an integer.
7f561c08de6b Initial load
duke
parents:
diff changeset
   994
    if (port < -1 || port > 65535) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   995
      return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   996
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   997
7f561c08de6b Initial load
duke
parents:
diff changeset
   998
    // Check that userinfo is well formed if it exists.
7f561c08de6b Initial load
duke
parents:
diff changeset
   999
    if (userinfo != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1000
      // Userinfo can contain alphanumerics, mark characters, escaped
7f561c08de6b Initial load
duke
parents:
diff changeset
  1001
      // and ';',':','&','=','+','$',','
7f561c08de6b Initial load
duke
parents:
diff changeset
  1002
      int index = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1003
      int end = userinfo.length();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1004
      char testChar = '\0';
7f561c08de6b Initial load
duke
parents:
diff changeset
  1005
      while (index < end) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1006
        testChar = userinfo.charAt(index);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1007
        if (testChar == '%') {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1008
          if (index+2 >= end ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1009
            !isHex(userinfo.charAt(index+1)) ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1010
            !isHex(userinfo.charAt(index+2))) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1011
            return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1012
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1013
          index += 2;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1014
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1015
        else if (!isUserinfoCharacter(testChar)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1016
          return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1017
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1018
        ++index;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1019
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1020
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1021
    return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1022
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1023
7f561c08de6b Initial load
duke
parents:
diff changeset
  1024
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1025
   * Determines whether the given string is a registry based authority.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1026
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1027
   * @param authority the authority component of a URI
7f561c08de6b Initial load
duke
parents:
diff changeset
  1028
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1029
   * @return true if the given string is a registry based authority
7f561c08de6b Initial load
duke
parents:
diff changeset
  1030
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1031
  private boolean isValidRegistryBasedAuthority(String authority) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1032
    int index = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1033
    int end = authority.length();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1034
    char testChar;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1035
7f561c08de6b Initial load
duke
parents:
diff changeset
  1036
    while (index < end) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1037
      testChar = authority.charAt(index);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1038
7f561c08de6b Initial load
duke
parents:
diff changeset
  1039
      // check for valid escape sequence
7f561c08de6b Initial load
duke
parents:
diff changeset
  1040
      if (testChar == '%') {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1041
        if (index+2 >= end ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1042
            !isHex(authority.charAt(index+1)) ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1043
            !isHex(authority.charAt(index+2))) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1044
            return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1045
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1046
        index += 2;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1047
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1048
      // can check against path characters because the set
7f561c08de6b Initial load
duke
parents:
diff changeset
  1049
      // is the same except for '/' which we've already excluded.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1050
      else if (!isPathCharacter(testChar)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1051
        return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1052
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1053
      ++index;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1054
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1055
    return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1056
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1057
7f561c08de6b Initial load
duke
parents:
diff changeset
  1058
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1059
  * Initialize the path for this URI from a URI string spec.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1060
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1061
  * @param p_uriSpec the URI specification (cannot be null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1062
  * @param p_nStartIndex the index to begin scanning from
7f561c08de6b Initial load
duke
parents:
diff changeset
  1063
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1064
  * @exception MalformedURIException if p_uriSpec violates syntax rules
7f561c08de6b Initial load
duke
parents:
diff changeset
  1065
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1066
  private void initializePath(String p_uriSpec, int p_nStartIndex)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1067
                 throws MalformedURIException {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1068
    if (p_uriSpec == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1069
      throw new MalformedURIException(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1070
                "Cannot initialize path from null string!");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1071
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1072
7f561c08de6b Initial load
duke
parents:
diff changeset
  1073
    int index = p_nStartIndex;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1074
    int start = p_nStartIndex;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1075
    int end = p_uriSpec.length();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1076
    char testChar = '\0';
7f561c08de6b Initial load
duke
parents:
diff changeset
  1077
7f561c08de6b Initial load
duke
parents:
diff changeset
  1078
    // path - everything up to query string or fragment
7f561c08de6b Initial load
duke
parents:
diff changeset
  1079
    if (start < end) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1080
        // RFC 2732 only allows '[' and ']' to appear in the opaque part.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1081
        if (getScheme() == null || p_uriSpec.charAt(start) == '/') {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1082
7f561c08de6b Initial load
duke
parents:
diff changeset
  1083
            // Scan path.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1084
            // abs_path = "/"  path_segments
7f561c08de6b Initial load
duke
parents:
diff changeset
  1085
            // rel_path = rel_segment [ abs_path ]
7f561c08de6b Initial load
duke
parents:
diff changeset
  1086
            while (index < end) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1087
                testChar = p_uriSpec.charAt(index);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1088
7f561c08de6b Initial load
duke
parents:
diff changeset
  1089
                // check for valid escape sequence
7f561c08de6b Initial load
duke
parents:
diff changeset
  1090
                if (testChar == '%') {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1091
                    if (index+2 >= end ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1092
                    !isHex(p_uriSpec.charAt(index+1)) ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1093
                    !isHex(p_uriSpec.charAt(index+2))) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1094
                        throw new MalformedURIException(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1095
                            "Path contains invalid escape sequence!");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1096
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1097
                    index += 2;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1098
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1099
                // Path segments cannot contain '[' or ']' since pchar
7f561c08de6b Initial load
duke
parents:
diff changeset
  1100
                // production was not changed by RFC 2732.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1101
                else if (!isPathCharacter(testChar)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1102
                    if (testChar == '?' || testChar == '#') {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1103
                        break;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1104
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1105
                    throw new MalformedURIException(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1106
                        "Path contains invalid character: " + testChar);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1107
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1108
                ++index;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1109
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1110
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1111
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1112
7f561c08de6b Initial load
duke
parents:
diff changeset
  1113
            // Scan opaque part.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1114
            // opaque_part = uric_no_slash *uric
7f561c08de6b Initial load
duke
parents:
diff changeset
  1115
            while (index < end) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1116
                testChar = p_uriSpec.charAt(index);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1117
7f561c08de6b Initial load
duke
parents:
diff changeset
  1118
                if (testChar == '?' || testChar == '#') {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1119
                    break;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1120
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1121
7f561c08de6b Initial load
duke
parents:
diff changeset
  1122
                // check for valid escape sequence
7f561c08de6b Initial load
duke
parents:
diff changeset
  1123
                if (testChar == '%') {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1124
                    if (index+2 >= end ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1125
                    !isHex(p_uriSpec.charAt(index+1)) ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1126
                    !isHex(p_uriSpec.charAt(index+2))) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1127
                        throw new MalformedURIException(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1128
                            "Opaque part contains invalid escape sequence!");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1129
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1130
                    index += 2;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1131
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1132
                // If the scheme specific part is opaque, it can contain '['
7f561c08de6b Initial load
duke
parents:
diff changeset
  1133
                // and ']'. uric_no_slash wasn't modified by RFC 2732, which
7f561c08de6b Initial load
duke
parents:
diff changeset
  1134
                // I've interpreted as an error in the spec, since the
7f561c08de6b Initial load
duke
parents:
diff changeset
  1135
                // production should be equivalent to (uric - '/'), and uric
7f561c08de6b Initial load
duke
parents:
diff changeset
  1136
                // contains '[' and ']'. - mrglavas
7f561c08de6b Initial load
duke
parents:
diff changeset
  1137
                else if (!isURICharacter(testChar)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1138
                    throw new MalformedURIException(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1139
                        "Opaque part contains invalid character: " + testChar);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1140
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1141
                ++index;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1142
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1143
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1144
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1145
    m_path = p_uriSpec.substring(start, index);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1146
7f561c08de6b Initial load
duke
parents:
diff changeset
  1147
    // query - starts with ? and up to fragment or end
7f561c08de6b Initial load
duke
parents:
diff changeset
  1148
    if (testChar == '?') {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1149
      index++;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1150
      start = index;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1151
      while (index < end) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1152
        testChar = p_uriSpec.charAt(index);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1153
        if (testChar == '#') {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1154
          break;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1155
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1156
        if (testChar == '%') {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1157
           if (index+2 >= end ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1158
              !isHex(p_uriSpec.charAt(index+1)) ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1159
              !isHex(p_uriSpec.charAt(index+2))) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1160
            throw new MalformedURIException(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1161
                    "Query string contains invalid escape sequence!");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1162
           }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1163
           index += 2;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1164
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1165
        else if (!isURICharacter(testChar)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1166
          throw new MalformedURIException(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1167
                "Query string contains invalid character: " + testChar);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1168
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1169
        index++;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1170
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1171
      m_queryString = p_uriSpec.substring(start, index);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1172
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1173
7f561c08de6b Initial load
duke
parents:
diff changeset
  1174
    // fragment - starts with #
7f561c08de6b Initial load
duke
parents:
diff changeset
  1175
    if (testChar == '#') {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1176
      index++;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1177
      start = index;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1178
      while (index < end) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1179
        testChar = p_uriSpec.charAt(index);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1180
7f561c08de6b Initial load
duke
parents:
diff changeset
  1181
        if (testChar == '%') {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1182
           if (index+2 >= end ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1183
              !isHex(p_uriSpec.charAt(index+1)) ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1184
              !isHex(p_uriSpec.charAt(index+2))) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1185
            throw new MalformedURIException(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1186
                    "Fragment contains invalid escape sequence!");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1187
           }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1188
           index += 2;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1189
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1190
        else if (!isURICharacter(testChar)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1191
          throw new MalformedURIException(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1192
                "Fragment contains invalid character: "+testChar);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1193
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1194
        index++;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1195
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1196
      m_fragment = p_uriSpec.substring(start, index);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1197
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1198
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1199
7f561c08de6b Initial load
duke
parents:
diff changeset
  1200
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1201
  * Get the scheme for this URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1202
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1203
  * @return the scheme for this URI
7f561c08de6b Initial load
duke
parents:
diff changeset
  1204
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1205
  public String getScheme() {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1206
    return m_scheme;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1207
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1208
7f561c08de6b Initial load
duke
parents:
diff changeset
  1209
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1210
  * Get the scheme-specific part for this URI (everything following the
7f561c08de6b Initial load
duke
parents:
diff changeset
  1211
  * scheme and the first colon). See RFC 2396 Section 5.2 for spec.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1212
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1213
  * @return the scheme-specific part for this URI
7f561c08de6b Initial load
duke
parents:
diff changeset
  1214
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1215
  public String getSchemeSpecificPart() {
17538
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
  1216
    final StringBuilder schemespec = new StringBuilder();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1217
7f561c08de6b Initial load
duke
parents:
diff changeset
  1218
    if (m_host != null || m_regAuthority != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1219
      schemespec.append("//");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1220
7f561c08de6b Initial load
duke
parents:
diff changeset
  1221
      // Server based authority.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1222
      if (m_host != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1223
7f561c08de6b Initial load
duke
parents:
diff changeset
  1224
        if (m_userinfo != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1225
          schemespec.append(m_userinfo);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1226
          schemespec.append('@');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1227
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1228
7f561c08de6b Initial load
duke
parents:
diff changeset
  1229
        schemespec.append(m_host);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1230
7f561c08de6b Initial load
duke
parents:
diff changeset
  1231
        if (m_port != -1) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1232
          schemespec.append(':');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1233
          schemespec.append(m_port);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1234
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1235
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1236
      // Registry based authority.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1237
      else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1238
        schemespec.append(m_regAuthority);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1239
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1240
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1241
7f561c08de6b Initial load
duke
parents:
diff changeset
  1242
    if (m_path != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1243
      schemespec.append((m_path));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1244
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1245
7f561c08de6b Initial load
duke
parents:
diff changeset
  1246
    if (m_queryString != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1247
      schemespec.append('?');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1248
      schemespec.append(m_queryString);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1249
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1250
7f561c08de6b Initial load
duke
parents:
diff changeset
  1251
    if (m_fragment != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1252
      schemespec.append('#');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1253
      schemespec.append(m_fragment);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1254
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1255
7f561c08de6b Initial load
duke
parents:
diff changeset
  1256
    return schemespec.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1257
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1258
7f561c08de6b Initial load
duke
parents:
diff changeset
  1259
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1260
  * Get the userinfo for this URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1261
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1262
  * @return the userinfo for this URI (null if not specified).
7f561c08de6b Initial load
duke
parents:
diff changeset
  1263
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1264
  public String getUserinfo() {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1265
    return m_userinfo;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1266
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1267
7f561c08de6b Initial load
duke
parents:
diff changeset
  1268
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1269
  * Get the host for this URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1270
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1271
  * @return the host for this URI (null if not specified).
7f561c08de6b Initial load
duke
parents:
diff changeset
  1272
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1273
  public String getHost() {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1274
    return m_host;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1275
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1276
7f561c08de6b Initial load
duke
parents:
diff changeset
  1277
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1278
  * Get the port for this URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1279
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1280
  * @return the port for this URI (-1 if not specified).
7f561c08de6b Initial load
duke
parents:
diff changeset
  1281
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1282
  public int getPort() {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1283
    return m_port;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1284
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1285
7f561c08de6b Initial load
duke
parents:
diff changeset
  1286
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1287
   * Get the registry based authority for this URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1288
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1289
   * @return the registry based authority (null if not specified).
7f561c08de6b Initial load
duke
parents:
diff changeset
  1290
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1291
  public String getRegBasedAuthority() {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1292
    return m_regAuthority;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1293
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1294
7f561c08de6b Initial load
duke
parents:
diff changeset
  1295
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1296
   * Get the authority for this URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1297
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1298
   * @return the authority
7f561c08de6b Initial load
duke
parents:
diff changeset
  1299
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1300
  public String getAuthority() {
17538
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
  1301
      final StringBuilder authority = new StringBuilder();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1302
      if (m_host != null || m_regAuthority != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1303
          authority.append("//");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1304
7f561c08de6b Initial load
duke
parents:
diff changeset
  1305
          // Server based authority.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1306
          if (m_host != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1307
7f561c08de6b Initial load
duke
parents:
diff changeset
  1308
              if (m_userinfo != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1309
                  authority.append(m_userinfo);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1310
                  authority.append('@');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1311
              }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1312
7f561c08de6b Initial load
duke
parents:
diff changeset
  1313
              authority.append(m_host);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1314
7f561c08de6b Initial load
duke
parents:
diff changeset
  1315
              if (m_port != -1) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1316
                  authority.append(':');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1317
                  authority.append(m_port);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1318
              }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1319
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1320
          // Registry based authority.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1321
          else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1322
              authority.append(m_regAuthority);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1323
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1324
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1325
      return authority.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1326
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1327
7f561c08de6b Initial load
duke
parents:
diff changeset
  1328
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1329
  * Get the path for this URI (optionally with the query string and
7f561c08de6b Initial load
duke
parents:
diff changeset
  1330
  * fragment).
7f561c08de6b Initial load
duke
parents:
diff changeset
  1331
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1332
  * @param p_includeQueryString if true (and query string is not null),
7f561c08de6b Initial load
duke
parents:
diff changeset
  1333
  *                             then a "?" followed by the query string
7f561c08de6b Initial load
duke
parents:
diff changeset
  1334
  *                             will be appended
7f561c08de6b Initial load
duke
parents:
diff changeset
  1335
  * @param p_includeFragment if true (and fragment is not null),
7f561c08de6b Initial load
duke
parents:
diff changeset
  1336
  *                             then a "#" followed by the fragment
7f561c08de6b Initial load
duke
parents:
diff changeset
  1337
  *                             will be appended
7f561c08de6b Initial load
duke
parents:
diff changeset
  1338
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1339
  * @return the path for this URI possibly including the query string
7f561c08de6b Initial load
duke
parents:
diff changeset
  1340
  *         and fragment
7f561c08de6b Initial load
duke
parents:
diff changeset
  1341
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1342
  public String getPath(boolean p_includeQueryString,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1343
                        boolean p_includeFragment) {
17538
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
  1344
    final StringBuilder pathString = new StringBuilder(m_path);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1345
7f561c08de6b Initial load
duke
parents:
diff changeset
  1346
    if (p_includeQueryString && m_queryString != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1347
      pathString.append('?');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1348
      pathString.append(m_queryString);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1349
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1350
7f561c08de6b Initial load
duke
parents:
diff changeset
  1351
    if (p_includeFragment && m_fragment != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1352
      pathString.append('#');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1353
      pathString.append(m_fragment);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1354
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1355
    return pathString.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1356
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1357
7f561c08de6b Initial load
duke
parents:
diff changeset
  1358
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1359
  * Get the path for this URI. Note that the value returned is the path
7f561c08de6b Initial load
duke
parents:
diff changeset
  1360
  * only and does not include the query string or fragment.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1361
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1362
  * @return the path for this URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1363
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1364
  public String getPath() {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1365
    return m_path;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1366
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1367
7f561c08de6b Initial load
duke
parents:
diff changeset
  1368
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1369
  * Get the query string for this URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1370
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1371
  * @return the query string for this URI. Null is returned if there
7f561c08de6b Initial load
duke
parents:
diff changeset
  1372
  *         was no "?" in the URI spec, empty string if there was a
7f561c08de6b Initial load
duke
parents:
diff changeset
  1373
  *         "?" but no query string following it.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1374
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1375
  public String getQueryString() {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1376
    return m_queryString;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1377
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1378
7f561c08de6b Initial load
duke
parents:
diff changeset
  1379
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1380
  * Get the fragment for this URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1381
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1382
  * @return the fragment for this URI. Null is returned if there
7f561c08de6b Initial load
duke
parents:
diff changeset
  1383
  *         was no "#" in the URI spec, empty string if there was a
7f561c08de6b Initial load
duke
parents:
diff changeset
  1384
  *         "#" but no fragment following it.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1385
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1386
  public String getFragment() {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1387
    return m_fragment;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1388
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1389
7f561c08de6b Initial load
duke
parents:
diff changeset
  1390
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1391
  * Set the scheme for this URI. The scheme is converted to lowercase
7f561c08de6b Initial load
duke
parents:
diff changeset
  1392
  * before it is set.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1393
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1394
  * @param p_scheme the scheme for this URI (cannot be null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1395
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1396
  * @exception MalformedURIException if p_scheme is not a conformant
7f561c08de6b Initial load
duke
parents:
diff changeset
  1397
  *                                  scheme name
7f561c08de6b Initial load
duke
parents:
diff changeset
  1398
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1399
  public void setScheme(String p_scheme) throws MalformedURIException {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1400
    if (p_scheme == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1401
      throw new MalformedURIException(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1402
                "Cannot set scheme from null string!");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1403
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1404
    if (!isConformantSchemeName(p_scheme)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1405
      throw new MalformedURIException("The scheme is not conformant.");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1406
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1407
7f561c08de6b Initial load
duke
parents:
diff changeset
  1408
    m_scheme = p_scheme.toLowerCase();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1409
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1410
7f561c08de6b Initial load
duke
parents:
diff changeset
  1411
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1412
  * Set the userinfo for this URI. If a non-null value is passed in and
7f561c08de6b Initial load
duke
parents:
diff changeset
  1413
  * the host value is null, then an exception is thrown.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1414
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1415
  * @param p_userinfo the userinfo for this URI
7f561c08de6b Initial load
duke
parents:
diff changeset
  1416
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1417
  * @exception MalformedURIException if p_userinfo contains invalid
7f561c08de6b Initial load
duke
parents:
diff changeset
  1418
  *                                  characters
7f561c08de6b Initial load
duke
parents:
diff changeset
  1419
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1420
  public void setUserinfo(String p_userinfo) throws MalformedURIException {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1421
    if (p_userinfo == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1422
      m_userinfo = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1423
      return;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1424
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1425
    else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1426
      if (m_host == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1427
        throw new MalformedURIException(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1428
                     "Userinfo cannot be set when host is null!");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1429
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1430
7f561c08de6b Initial load
duke
parents:
diff changeset
  1431
      // userinfo can contain alphanumerics, mark characters, escaped
7f561c08de6b Initial load
duke
parents:
diff changeset
  1432
      // and ';',':','&','=','+','$',','
7f561c08de6b Initial load
duke
parents:
diff changeset
  1433
      int index = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1434
      int end = p_userinfo.length();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1435
      char testChar = '\0';
7f561c08de6b Initial load
duke
parents:
diff changeset
  1436
      while (index < end) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1437
        testChar = p_userinfo.charAt(index);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1438
        if (testChar == '%') {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1439
          if (index+2 >= end ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1440
              !isHex(p_userinfo.charAt(index+1)) ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1441
              !isHex(p_userinfo.charAt(index+2))) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1442
            throw new MalformedURIException(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1443
                  "Userinfo contains invalid escape sequence!");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1444
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1445
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1446
        else if (!isUserinfoCharacter(testChar)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1447
          throw new MalformedURIException(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1448
                  "Userinfo contains invalid character:"+testChar);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1449
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1450
        index++;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1451
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1452
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1453
    m_userinfo = p_userinfo;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1454
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1455
7f561c08de6b Initial load
duke
parents:
diff changeset
  1456
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1457
  * <p>Set the host for this URI. If null is passed in, the userinfo
7f561c08de6b Initial load
duke
parents:
diff changeset
  1458
  * field is also set to null and the port is set to -1.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
  1459
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1460
  * <p>Note: This method overwrites registry based authority if it
7f561c08de6b Initial load
duke
parents:
diff changeset
  1461
  * previously existed in this URI.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
  1462
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1463
  * @param p_host the host for this URI
7f561c08de6b Initial load
duke
parents:
diff changeset
  1464
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1465
  * @exception MalformedURIException if p_host is not a valid IP
7f561c08de6b Initial load
duke
parents:
diff changeset
  1466
  *                                  address or DNS hostname.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1467
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1468
  public void setHost(String p_host) throws MalformedURIException {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1469
    if (p_host == null || p_host.length() == 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1470
      if (p_host != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1471
        m_regAuthority = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1472
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1473
      m_host = p_host;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1474
      m_userinfo = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1475
      m_port = -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1476
      return;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1477
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1478
    else if (!isWellFormedAddress(p_host)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1479
      throw new MalformedURIException("Host is not a well formed address!");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1480
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1481
    m_host = p_host;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1482
    m_regAuthority = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1483
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1484
7f561c08de6b Initial load
duke
parents:
diff changeset
  1485
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1486
  * Set the port for this URI. -1 is used to indicate that the port is
7f561c08de6b Initial load
duke
parents:
diff changeset
  1487
  * not specified, otherwise valid port numbers are  between 0 and 65535.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1488
  * If a valid port number is passed in and the host field is null,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1489
  * an exception is thrown.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1490
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1491
  * @param p_port the port number for this URI
7f561c08de6b Initial load
duke
parents:
diff changeset
  1492
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1493
  * @exception MalformedURIException if p_port is not -1 and not a
7f561c08de6b Initial load
duke
parents:
diff changeset
  1494
  *                                  valid port number
7f561c08de6b Initial load
duke
parents:
diff changeset
  1495
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1496
  public void setPort(int p_port) throws MalformedURIException {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1497
    if (p_port >= 0 && p_port <= 65535) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1498
      if (m_host == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1499
        throw new MalformedURIException(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1500
                      "Port cannot be set when host is null!");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1501
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1502
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1503
    else if (p_port != -1) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1504
      throw new MalformedURIException("Invalid port number!");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1505
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1506
    m_port = p_port;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1507
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1508
7f561c08de6b Initial load
duke
parents:
diff changeset
  1509
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1510
   * <p>Sets the registry based authority for this URI.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
  1511
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1512
   * <p>Note: This method overwrites server based authority
7f561c08de6b Initial load
duke
parents:
diff changeset
  1513
   * if it previously existed in this URI.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
  1514
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1515
   * @param authority the registry based authority for this URI
7f561c08de6b Initial load
duke
parents:
diff changeset
  1516
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1517
   * @exception MalformedURIException it authority is not a
7f561c08de6b Initial load
duke
parents:
diff changeset
  1518
   * well formed registry based authority
7f561c08de6b Initial load
duke
parents:
diff changeset
  1519
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1520
  public void setRegBasedAuthority(String authority)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1521
    throws MalformedURIException {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1522
7f561c08de6b Initial load
duke
parents:
diff changeset
  1523
        if (authority == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1524
          m_regAuthority = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1525
          return;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1526
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1527
        // reg_name = 1*( unreserved | escaped | "$" | "," |
7f561c08de6b Initial load
duke
parents:
diff changeset
  1528
        //            ";" | ":" | "@" | "&" | "=" | "+" )
7f561c08de6b Initial load
duke
parents:
diff changeset
  1529
        else if (authority.length() < 1 ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1530
          !isValidRegistryBasedAuthority(authority) ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1531
          authority.indexOf('/') != -1) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1532
      throw new MalformedURIException("Registry based authority is not well formed.");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1533
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1534
        m_regAuthority = authority;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1535
        m_host = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1536
        m_userinfo = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1537
        m_port = -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1538
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1539
7f561c08de6b Initial load
duke
parents:
diff changeset
  1540
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1541
  * Set the path for this URI. If the supplied path is null, then the
7f561c08de6b Initial load
duke
parents:
diff changeset
  1542
  * query string and fragment are set to null as well. If the supplied
7f561c08de6b Initial load
duke
parents:
diff changeset
  1543
  * path includes a query string and/or fragment, these fields will be
7f561c08de6b Initial load
duke
parents:
diff changeset
  1544
  * parsed and set as well. Note that, for URIs following the "generic
7f561c08de6b Initial load
duke
parents:
diff changeset
  1545
  * URI" syntax, the path specified should start with a slash.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1546
  * For URIs that do not follow the generic URI syntax, this method
7f561c08de6b Initial load
duke
parents:
diff changeset
  1547
  * sets the scheme-specific part.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1548
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1549
  * @param p_path the path for this URI (may be null)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1550
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1551
  * @exception MalformedURIException if p_path contains invalid
7f561c08de6b Initial load
duke
parents:
diff changeset
  1552
  *                                  characters
7f561c08de6b Initial load
duke
parents:
diff changeset
  1553
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1554
  public void setPath(String p_path) throws MalformedURIException {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1555
    if (p_path == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1556
      m_path = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1557
      m_queryString = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1558
      m_fragment = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1559
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1560
    else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1561
      initializePath(p_path, 0);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1562
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1563
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1564
7f561c08de6b Initial load
duke
parents:
diff changeset
  1565
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1566
  * Append to the end of the path of this URI. If the current path does
7f561c08de6b Initial load
duke
parents:
diff changeset
  1567
  * not end in a slash and the path to be appended does not begin with
7f561c08de6b Initial load
duke
parents:
diff changeset
  1568
  * a slash, a slash will be appended to the current path before the
7f561c08de6b Initial load
duke
parents:
diff changeset
  1569
  * new segment is added. Also, if the current path ends in a slash
7f561c08de6b Initial load
duke
parents:
diff changeset
  1570
  * and the new segment begins with a slash, the extra slash will be
7f561c08de6b Initial load
duke
parents:
diff changeset
  1571
  * removed before the new segment is appended.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1572
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1573
  * @param p_addToPath the new segment to be added to the current path
7f561c08de6b Initial load
duke
parents:
diff changeset
  1574
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1575
  * @exception MalformedURIException if p_addToPath contains syntax
7f561c08de6b Initial load
duke
parents:
diff changeset
  1576
  *                                  errors
7f561c08de6b Initial load
duke
parents:
diff changeset
  1577
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1578
  public void appendPath(String p_addToPath)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1579
                         throws MalformedURIException {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1580
    if (p_addToPath == null || p_addToPath.trim().length() == 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1581
      return;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1582
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1583
7f561c08de6b Initial load
duke
parents:
diff changeset
  1584
    if (!isURIString(p_addToPath)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1585
      throw new MalformedURIException(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1586
              "Path contains invalid character!");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1587
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1588
7f561c08de6b Initial load
duke
parents:
diff changeset
  1589
    if (m_path == null || m_path.trim().length() == 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1590
      if (p_addToPath.startsWith("/")) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1591
        m_path = p_addToPath;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1592
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1593
      else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1594
        m_path = "/" + p_addToPath;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1595
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1596
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1597
    else if (m_path.endsWith("/")) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1598
      if (p_addToPath.startsWith("/")) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1599
        m_path = m_path.concat(p_addToPath.substring(1));
7f561c08de6b Initial load
duke
parents:
diff changeset
  1600
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1601
      else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1602
        m_path = m_path.concat(p_addToPath);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1603
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1604
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1605
    else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1606
      if (p_addToPath.startsWith("/")) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1607
        m_path = m_path.concat(p_addToPath);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1608
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1609
      else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1610
        m_path = m_path.concat("/" + p_addToPath);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1611
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1612
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1613
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1614
7f561c08de6b Initial load
duke
parents:
diff changeset
  1615
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1616
  * Set the query string for this URI. A non-null value is valid only
7f561c08de6b Initial load
duke
parents:
diff changeset
  1617
  * if this is an URI conforming to the generic URI syntax and
7f561c08de6b Initial load
duke
parents:
diff changeset
  1618
  * the path value is not null.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1619
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1620
  * @param p_queryString the query string for this URI
7f561c08de6b Initial load
duke
parents:
diff changeset
  1621
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1622
  * @exception MalformedURIException if p_queryString is not null and this
7f561c08de6b Initial load
duke
parents:
diff changeset
  1623
  *                                  URI does not conform to the generic
7f561c08de6b Initial load
duke
parents:
diff changeset
  1624
  *                                  URI syntax or if the path is null
7f561c08de6b Initial load
duke
parents:
diff changeset
  1625
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1626
  public void setQueryString(String p_queryString) throws MalformedURIException {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1627
    if (p_queryString == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1628
      m_queryString = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1629
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1630
    else if (!isGenericURI()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1631
      throw new MalformedURIException(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1632
              "Query string can only be set for a generic URI!");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1633
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1634
    else if (getPath() == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1635
      throw new MalformedURIException(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1636
              "Query string cannot be set when path is null!");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1637
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1638
    else if (!isURIString(p_queryString)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1639
      throw new MalformedURIException(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1640
              "Query string contains invalid character!");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1641
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1642
    else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1643
      m_queryString = p_queryString;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1644
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1645
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1646
7f561c08de6b Initial load
duke
parents:
diff changeset
  1647
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1648
  * Set the fragment for this URI. A non-null value is valid only
7f561c08de6b Initial load
duke
parents:
diff changeset
  1649
  * if this is a URI conforming to the generic URI syntax and
7f561c08de6b Initial load
duke
parents:
diff changeset
  1650
  * the path value is not null.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1651
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1652
  * @param p_fragment the fragment for this URI
7f561c08de6b Initial load
duke
parents:
diff changeset
  1653
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1654
  * @exception MalformedURIException if p_fragment is not null and this
7f561c08de6b Initial load
duke
parents:
diff changeset
  1655
  *                                  URI does not conform to the generic
7f561c08de6b Initial load
duke
parents:
diff changeset
  1656
  *                                  URI syntax or if the path is null
7f561c08de6b Initial load
duke
parents:
diff changeset
  1657
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1658
  public void setFragment(String p_fragment) throws MalformedURIException {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1659
    if (p_fragment == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1660
      m_fragment = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1661
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1662
    else if (!isGenericURI()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1663
      throw new MalformedURIException(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1664
         "Fragment can only be set for a generic URI!");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1665
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1666
    else if (getPath() == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1667
      throw new MalformedURIException(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1668
              "Fragment cannot be set when path is null!");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1669
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1670
    else if (!isURIString(p_fragment)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1671
      throw new MalformedURIException(
7f561c08de6b Initial load
duke
parents:
diff changeset
  1672
              "Fragment contains invalid character!");
7f561c08de6b Initial load
duke
parents:
diff changeset
  1673
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1674
    else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1675
      m_fragment = p_fragment;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1676
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1677
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1678
7f561c08de6b Initial load
duke
parents:
diff changeset
  1679
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1680
  * Determines if the passed-in Object is equivalent to this URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1681
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1682
  * @param p_test the Object to test for equality.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1683
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1684
  * @return true if p_test is a URI with all values equal to this
7f561c08de6b Initial load
duke
parents:
diff changeset
  1685
  *         URI, false otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
  1686
  */
17538
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
  1687
  @Override
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1688
  public boolean equals(Object p_test) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1689
    if (p_test instanceof URI) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1690
      URI testURI = (URI) p_test;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1691
      if (((m_scheme == null && testURI.m_scheme == null) ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1692
           (m_scheme != null && testURI.m_scheme != null &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1693
            m_scheme.equals(testURI.m_scheme))) &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1694
          ((m_userinfo == null && testURI.m_userinfo == null) ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1695
           (m_userinfo != null && testURI.m_userinfo != null &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1696
            m_userinfo.equals(testURI.m_userinfo))) &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1697
          ((m_host == null && testURI.m_host == null) ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1698
           (m_host != null && testURI.m_host != null &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1699
            m_host.equals(testURI.m_host))) &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1700
            m_port == testURI.m_port &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1701
          ((m_path == null && testURI.m_path == null) ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1702
           (m_path != null && testURI.m_path != null &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1703
            m_path.equals(testURI.m_path))) &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1704
          ((m_queryString == null && testURI.m_queryString == null) ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1705
           (m_queryString != null && testURI.m_queryString != null &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1706
            m_queryString.equals(testURI.m_queryString))) &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1707
          ((m_fragment == null && testURI.m_fragment == null) ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1708
           (m_fragment != null && testURI.m_fragment != null &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1709
            m_fragment.equals(testURI.m_fragment)))) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1710
        return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1711
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1712
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1713
    return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1714
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1715
17538
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
  1716
    @Override
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
  1717
    public int hashCode() {
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
  1718
        int hash = 5;
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
  1719
        hash = 47 * hash + Objects.hashCode(this.m_scheme);
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
  1720
        hash = 47 * hash + Objects.hashCode(this.m_userinfo);
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
  1721
        hash = 47 * hash + Objects.hashCode(this.m_host);
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
  1722
        hash = 47 * hash + this.m_port;
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
  1723
        hash = 47 * hash + Objects.hashCode(this.m_path);
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
  1724
        hash = 47 * hash + Objects.hashCode(this.m_queryString);
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
  1725
        hash = 47 * hash + Objects.hashCode(this.m_fragment);
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
  1726
        return hash;
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
  1727
    }
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
  1728
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1729
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1730
  * Get the URI as a string specification. See RFC 2396 Section 5.2.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1731
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1732
  * @return the URI string specification
7f561c08de6b Initial load
duke
parents:
diff changeset
  1733
  */
17538
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
  1734
  @Override
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1735
  public String toString() {
17538
d8d911c4e5d4 8013900: More warnings compiling jaxp.
dfuchs
parents: 12457
diff changeset
  1736
    final StringBuilder uriSpecString = new StringBuilder();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
  1737
7f561c08de6b Initial load
duke
parents:
diff changeset
  1738
    if (m_scheme != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1739
      uriSpecString.append(m_scheme);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1740
      uriSpecString.append(':');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1741
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1742
    uriSpecString.append(getSchemeSpecificPart());
7f561c08de6b Initial load
duke
parents:
diff changeset
  1743
    return uriSpecString.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1744
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1745
7f561c08de6b Initial load
duke
parents:
diff changeset
  1746
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1747
  * Get the indicator as to whether this URI uses the "generic URI"
7f561c08de6b Initial load
duke
parents:
diff changeset
  1748
  * syntax.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1749
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1750
  * @return true if this URI uses the "generic URI" syntax, false
7f561c08de6b Initial load
duke
parents:
diff changeset
  1751
  *         otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
  1752
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1753
  public boolean isGenericURI() {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1754
    // presence of the host (whether valid or empty) means
7f561c08de6b Initial load
duke
parents:
diff changeset
  1755
    // double-slashes which means generic uri
7f561c08de6b Initial load
duke
parents:
diff changeset
  1756
    return (m_host != null);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1757
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1758
7f561c08de6b Initial load
duke
parents:
diff changeset
  1759
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1760
   * Returns whether this URI represents an absolute URI.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1761
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1762
   * @return true if this URI represents an absolute URI, false
7f561c08de6b Initial load
duke
parents:
diff changeset
  1763
   *         otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
  1764
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1765
  public boolean isAbsoluteURI() {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1766
      // presence of the scheme means absolute uri
7f561c08de6b Initial load
duke
parents:
diff changeset
  1767
      return (m_scheme != null);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1768
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1769
7f561c08de6b Initial load
duke
parents:
diff changeset
  1770
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1771
  * Determine whether a scheme conforms to the rules for a scheme name.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1772
  * A scheme is conformant if it starts with an alphanumeric, and
7f561c08de6b Initial load
duke
parents:
diff changeset
  1773
  * contains only alphanumerics, '+','-' and '.'.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1774
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1775
  * @return true if the scheme is conformant, false otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
  1776
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1777
  public static boolean isConformantSchemeName(String p_scheme) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1778
    if (p_scheme == null || p_scheme.trim().length() == 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1779
      return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1780
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1781
7f561c08de6b Initial load
duke
parents:
diff changeset
  1782
    if (!isAlpha(p_scheme.charAt(0))) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1783
      return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1784
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1785
7f561c08de6b Initial load
duke
parents:
diff changeset
  1786
    char testChar;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1787
    int schemeLength = p_scheme.length();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1788
    for (int i = 1; i < schemeLength; ++i) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1789
      testChar = p_scheme.charAt(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1790
      if (!isSchemeCharacter(testChar)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1791
        return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1792
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1793
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1794
7f561c08de6b Initial load
duke
parents:
diff changeset
  1795
    return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1796
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1797
7f561c08de6b Initial load
duke
parents:
diff changeset
  1798
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1799
  * Determine whether a string is syntactically capable of representing
7f561c08de6b Initial load
duke
parents:
diff changeset
  1800
  * a valid IPv4 address, IPv6 reference or the domain name of a network host.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1801
  * A valid IPv4 address consists of four decimal digit groups separated by a
7f561c08de6b Initial load
duke
parents:
diff changeset
  1802
  * '.'. Each group must consist of one to three digits. See RFC 2732 Section 3,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1803
  * and RFC 2373 Section 2.2, for the definition of IPv6 references. A hostname
7f561c08de6b Initial load
duke
parents:
diff changeset
  1804
  * consists of domain labels (each of which must begin and end with an alphanumeric
7f561c08de6b Initial load
duke
parents:
diff changeset
  1805
  * but may contain '-') separated & by a '.'. See RFC 2396 Section 3.2.2.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1806
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1807
  * @return true if the string is a syntactically valid IPv4 address,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1808
  * IPv6 reference or hostname
7f561c08de6b Initial load
duke
parents:
diff changeset
  1809
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1810
  public static boolean isWellFormedAddress(String address) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1811
    if (address == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1812
      return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1813
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1814
7f561c08de6b Initial load
duke
parents:
diff changeset
  1815
    int addrLength = address.length();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1816
    if (addrLength == 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1817
      return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1818
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1819
7f561c08de6b Initial load
duke
parents:
diff changeset
  1820
    // Check if the host is a valid IPv6reference.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1821
    if (address.startsWith("[")) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1822
      return isWellFormedIPv6Reference(address);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1823
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1824
7f561c08de6b Initial load
duke
parents:
diff changeset
  1825
    // Cannot start with a '.', '-', or end with a '-'.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1826
    if (address.startsWith(".") ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1827
        address.startsWith("-") ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1828
        address.endsWith("-")) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1829
      return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1830
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1831
7f561c08de6b Initial load
duke
parents:
diff changeset
  1832
    // rightmost domain label starting with digit indicates IP address
7f561c08de6b Initial load
duke
parents:
diff changeset
  1833
    // since top level domain label can only start with an alpha
7f561c08de6b Initial load
duke
parents:
diff changeset
  1834
    // see RFC 2396 Section 3.2.2
7f561c08de6b Initial load
duke
parents:
diff changeset
  1835
    int index = address.lastIndexOf('.');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1836
    if (address.endsWith(".")) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1837
      index = address.substring(0, index).lastIndexOf('.');
7f561c08de6b Initial load
duke
parents:
diff changeset
  1838
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1839
7f561c08de6b Initial load
duke
parents:
diff changeset
  1840
    if (index+1 < addrLength && isDigit(address.charAt(index+1))) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1841
      return isWellFormedIPv4Address(address);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1842
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1843
    else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1844
      // hostname      = *( domainlabel "." ) toplabel [ "." ]
7f561c08de6b Initial load
duke
parents:
diff changeset
  1845
      // domainlabel   = alphanum | alphanum *( alphanum | "-" ) alphanum
7f561c08de6b Initial load
duke
parents:
diff changeset
  1846
      // toplabel      = alpha | alpha *( alphanum | "-" ) alphanum
7f561c08de6b Initial load
duke
parents:
diff changeset
  1847
7f561c08de6b Initial load
duke
parents:
diff changeset
  1848
      // RFC 2396 states that hostnames take the form described in
7f561c08de6b Initial load
duke
parents:
diff changeset
  1849
      // RFC 1034 (Section 3) and RFC 1123 (Section 2.1). According
7f561c08de6b Initial load
duke
parents:
diff changeset
  1850
      // to RFC 1034, hostnames are limited to 255 characters.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1851
      if (addrLength > 255) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1852
        return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1853
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1854
7f561c08de6b Initial load
duke
parents:
diff changeset
  1855
      // domain labels can contain alphanumerics and '-"
7f561c08de6b Initial load
duke
parents:
diff changeset
  1856
      // but must start and end with an alphanumeric
7f561c08de6b Initial load
duke
parents:
diff changeset
  1857
      char testChar;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1858
      int labelCharCount = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1859
7f561c08de6b Initial load
duke
parents:
diff changeset
  1860
      for (int i = 0; i < addrLength; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1861
        testChar = address.charAt(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1862
        if (testChar == '.') {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1863
          if (!isAlphanum(address.charAt(i-1))) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1864
            return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1865
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1866
          if (i+1 < addrLength && !isAlphanum(address.charAt(i+1))) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1867
            return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1868
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1869
          labelCharCount = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1870
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1871
        else if (!isAlphanum(testChar) && testChar != '-') {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1872
          return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1873
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1874
        // RFC 1034: Labels must be 63 characters or less.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1875
        else if (++labelCharCount > 63) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1876
          return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1877
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1878
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1879
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1880
    return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1881
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1882
7f561c08de6b Initial load
duke
parents:
diff changeset
  1883
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1884
   * <p>Determines whether a string is an IPv4 address as defined by
7f561c08de6b Initial load
duke
parents:
diff changeset
  1885
   * RFC 2373, and under the further constraint that it must be a 32-bit
7f561c08de6b Initial load
duke
parents:
diff changeset
  1886
   * address. Though not expressed in the grammar, in order to satisfy
7f561c08de6b Initial load
duke
parents:
diff changeset
  1887
   * the 32-bit address constraint, each segment of the address cannot
7f561c08de6b Initial load
duke
parents:
diff changeset
  1888
   * be greater than 255 (8 bits of information).</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
  1889
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1890
   * <p><code>IPv4address = 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT</code></p>
7f561c08de6b Initial load
duke
parents:
diff changeset
  1891
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1892
   * @return true if the string is a syntactically valid IPv4 address
7f561c08de6b Initial load
duke
parents:
diff changeset
  1893
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1894
  public static boolean isWellFormedIPv4Address(String address) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1895
7f561c08de6b Initial load
duke
parents:
diff changeset
  1896
      int addrLength = address.length();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1897
      char testChar;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1898
      int numDots = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1899
      int numDigits = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1900
7f561c08de6b Initial load
duke
parents:
diff changeset
  1901
      // make sure that 1) we see only digits and dot separators, 2) that
7f561c08de6b Initial load
duke
parents:
diff changeset
  1902
      // any dot separator is preceded and followed by a digit and
7f561c08de6b Initial load
duke
parents:
diff changeset
  1903
      // 3) that we find 3 dots
7f561c08de6b Initial load
duke
parents:
diff changeset
  1904
      //
7f561c08de6b Initial load
duke
parents:
diff changeset
  1905
      // RFC 2732 amended RFC 2396 by replacing the definition
7f561c08de6b Initial load
duke
parents:
diff changeset
  1906
      // of IPv4address with the one defined by RFC 2373. - mrglavas
7f561c08de6b Initial load
duke
parents:
diff changeset
  1907
      //
7f561c08de6b Initial load
duke
parents:
diff changeset
  1908
      // IPv4address = 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT
7f561c08de6b Initial load
duke
parents:
diff changeset
  1909
      //
7f561c08de6b Initial load
duke
parents:
diff changeset
  1910
      // One to three digits must be in each segment.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1911
      for (int i = 0; i < addrLength; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1912
        testChar = address.charAt(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1913
        if (testChar == '.') {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1914
          if ((i > 0 && !isDigit(address.charAt(i-1))) ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1915
              (i+1 < addrLength && !isDigit(address.charAt(i+1)))) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1916
            return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1917
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1918
          numDigits = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1919
          if (++numDots > 3) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1920
            return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1921
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1922
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1923
        else if (!isDigit(testChar)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1924
          return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1925
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1926
        // Check that that there are no more than three digits
7f561c08de6b Initial load
duke
parents:
diff changeset
  1927
        // in this segment.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1928
        else if (++numDigits > 3) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1929
          return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1930
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1931
        // Check that this segment is not greater than 255.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1932
        else if (numDigits == 3) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1933
          char first = address.charAt(i-2);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1934
          char second = address.charAt(i-1);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1935
          if (!(first < '2' ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1936
               (first == '2' &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  1937
               (second < '5' ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  1938
               (second == '5' && testChar <= '5'))))) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1939
            return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1940
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1941
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1942
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1943
      return (numDots == 3);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1944
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1945
7f561c08de6b Initial load
duke
parents:
diff changeset
  1946
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1947
   * <p>Determines whether a string is an IPv6 reference as defined
7f561c08de6b Initial load
duke
parents:
diff changeset
  1948
   * by RFC 2732, where IPv6address is defined in RFC 2373. The
7f561c08de6b Initial load
duke
parents:
diff changeset
  1949
   * IPv6 address is parsed according to Section 2.2 of RFC 2373,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1950
   * with the additional constraint that the address be composed of
7f561c08de6b Initial load
duke
parents:
diff changeset
  1951
   * 128 bits of information.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
  1952
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1953
   * <p><code>IPv6reference = "[" IPv6address "]"</code></p>
7f561c08de6b Initial load
duke
parents:
diff changeset
  1954
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1955
   * <p>Note: The BNF expressed in RFC 2373 Appendix B does not
7f561c08de6b Initial load
duke
parents:
diff changeset
  1956
   * accurately describe section 2.2, and was in fact removed from
7f561c08de6b Initial load
duke
parents:
diff changeset
  1957
   * RFC 3513, the successor of RFC 2373.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
  1958
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1959
   * @return true if the string is a syntactically valid IPv6 reference
7f561c08de6b Initial load
duke
parents:
diff changeset
  1960
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1961
  public static boolean isWellFormedIPv6Reference(String address) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1962
7f561c08de6b Initial load
duke
parents:
diff changeset
  1963
      int addrLength = address.length();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1964
      int index = 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1965
      int end = addrLength-1;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1966
7f561c08de6b Initial load
duke
parents:
diff changeset
  1967
      // Check if string is a potential match for IPv6reference.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1968
      if (!(addrLength > 2 && address.charAt(0) == '['
7f561c08de6b Initial load
duke
parents:
diff changeset
  1969
          && address.charAt(end) == ']')) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1970
          return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1971
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1972
7f561c08de6b Initial load
duke
parents:
diff changeset
  1973
      // Counter for the number of 16-bit sections read in the address.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1974
      int [] counter = new int[1];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1975
7f561c08de6b Initial load
duke
parents:
diff changeset
  1976
      // Scan hex sequence before possible '::' or IPv4 address.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1977
      index = scanHexSequence(address, index, end, counter);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1978
      if (index == -1) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1979
          return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1980
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1981
      // Address must contain 128-bits of information.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1982
      else if (index == end) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1983
          return (counter[0] == 8);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1984
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1985
7f561c08de6b Initial load
duke
parents:
diff changeset
  1986
      if (index+1 < end && address.charAt(index) == ':') {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1987
          if (address.charAt(index+1) == ':') {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1988
              // '::' represents at least one 16-bit group of zeros.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1989
              if (++counter[0] > 8) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1990
                  return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1991
              }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1992
              index += 2;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1993
              // Trailing zeros will fill out the rest of the address.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1994
              if (index == end) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1995
                 return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1996
              }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1997
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1998
          // If the second character wasn't ':', in order to be valid,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1999
          // the remainder of the string must match IPv4Address,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2000
          // and we must have read exactly 6 16-bit groups.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2001
          else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2002
              return (counter[0] == 6) &&
7f561c08de6b Initial load
duke
parents:
diff changeset
  2003
                  isWellFormedIPv4Address(address.substring(index+1, end));
7f561c08de6b Initial load
duke
parents:
diff changeset
  2004
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2005
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2006
      else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2007
          return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2008
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2009
7f561c08de6b Initial load
duke
parents:
diff changeset
  2010
      // 3. Scan hex sequence after '::'.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2011
      int prevCount = counter[0];
7f561c08de6b Initial load
duke
parents:
diff changeset
  2012
      index = scanHexSequence(address, index, end, counter);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2013
7f561c08de6b Initial load
duke
parents:
diff changeset
  2014
      // We've either reached the end of the string, the address ends in
7f561c08de6b Initial load
duke
parents:
diff changeset
  2015
      // an IPv4 address, or it is invalid. scanHexSequence has already
7f561c08de6b Initial load
duke
parents:
diff changeset
  2016
      // made sure that we have the right number of bits.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2017
      return (index == end) ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  2018
          (index != -1 && isWellFormedIPv4Address(
7f561c08de6b Initial load
duke
parents:
diff changeset
  2019
          address.substring((counter[0] > prevCount) ? index+1 : index, end)));
7f561c08de6b Initial load
duke
parents:
diff changeset
  2020
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2021
7f561c08de6b Initial load
duke
parents:
diff changeset
  2022
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2023
   * Helper method for isWellFormedIPv6Reference which scans the
7f561c08de6b Initial load
duke
parents:
diff changeset
  2024
   * hex sequences of an IPv6 address. It returns the index of the
7f561c08de6b Initial load
duke
parents:
diff changeset
  2025
   * next character to scan in the address, or -1 if the string
7f561c08de6b Initial load
duke
parents:
diff changeset
  2026
   * cannot match a valid IPv6 address.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2027
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2028
   * @param address the string to be scanned
7f561c08de6b Initial load
duke
parents:
diff changeset
  2029
   * @param index the beginning index (inclusive)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2030
   * @param end the ending index (exclusive)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2031
   * @param counter a counter for the number of 16-bit sections read
7f561c08de6b Initial load
duke
parents:
diff changeset
  2032
   * in the address
7f561c08de6b Initial load
duke
parents:
diff changeset
  2033
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2034
   * @return the index of the next character to scan, or -1 if the
7f561c08de6b Initial load
duke
parents:
diff changeset
  2035
   * string cannot match a valid IPv6 address
7f561c08de6b Initial load
duke
parents:
diff changeset
  2036
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2037
  private static int scanHexSequence (String address, int index, int end, int [] counter) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2038
7f561c08de6b Initial load
duke
parents:
diff changeset
  2039
      char testChar;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2040
      int numDigits = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2041
      int start = index;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2042
7f561c08de6b Initial load
duke
parents:
diff changeset
  2043
      // Trying to match the following productions:
7f561c08de6b Initial load
duke
parents:
diff changeset
  2044
      // hexseq = hex4 *( ":" hex4)
7f561c08de6b Initial load
duke
parents:
diff changeset
  2045
      // hex4   = 1*4HEXDIG
7f561c08de6b Initial load
duke
parents:
diff changeset
  2046
      for (; index < end; ++index) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2047
        testChar = address.charAt(index);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2048
        if (testChar == ':') {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2049
            // IPv6 addresses are 128-bit, so there can be at most eight sections.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2050
            if (numDigits > 0 && ++counter[0] > 8) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2051
                return -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2052
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2053
            // This could be '::'.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2054
            if (numDigits == 0 || ((index+1 < end) && address.charAt(index+1) == ':')) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2055
                return index;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2056
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2057
            numDigits = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2058
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2059
        // This might be invalid or an IPv4address. If it's potentially an IPv4address,
7f561c08de6b Initial load
duke
parents:
diff changeset
  2060
        // backup to just after the last valid character that matches hexseq.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2061
        else if (!isHex(testChar)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2062
            if (testChar == '.' && numDigits < 4 && numDigits > 0 && counter[0] <= 6) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2063
                int back = index - numDigits - 1;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2064
                return (back >= start) ? back : (back+1);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2065
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2066
            return -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2067
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2068
        // There can be at most 4 hex digits per group.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2069
        else if (++numDigits > 4) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2070
            return -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2071
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2072
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2073
      return (numDigits > 0 && ++counter[0] <= 8) ? end : -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2074
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2075
7f561c08de6b Initial load
duke
parents:
diff changeset
  2076
7f561c08de6b Initial load
duke
parents:
diff changeset
  2077
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2078
  * Determine whether a char is a digit.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2079
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2080
  * @return true if the char is betweeen '0' and '9', false otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
  2081
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2082
  private static boolean isDigit(char p_char) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2083
    return p_char >= '0' && p_char <= '9';
7f561c08de6b Initial load
duke
parents:
diff changeset
  2084
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2085
7f561c08de6b Initial load
duke
parents:
diff changeset
  2086
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2087
  * Determine whether a character is a hexadecimal character.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2088
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2089
  * @return true if the char is betweeen '0' and '9', 'a' and 'f'
7f561c08de6b Initial load
duke
parents:
diff changeset
  2090
  *         or 'A' and 'F', false otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
  2091
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2092
  private static boolean isHex(char p_char) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2093
    return (p_char <= 'f' && (fgLookupTable[p_char] & ASCII_HEX_CHARACTERS) != 0);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2094
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2095
7f561c08de6b Initial load
duke
parents:
diff changeset
  2096
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2097
  * Determine whether a char is an alphabetic character: a-z or A-Z
7f561c08de6b Initial load
duke
parents:
diff changeset
  2098
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2099
  * @return true if the char is alphabetic, false otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
  2100
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2101
  private static boolean isAlpha(char p_char) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2102
      return ((p_char >= 'a' && p_char <= 'z') || (p_char >= 'A' && p_char <= 'Z' ));
7f561c08de6b Initial load
duke
parents:
diff changeset
  2103
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2104
7f561c08de6b Initial load
duke
parents:
diff changeset
  2105
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2106
  * Determine whether a char is an alphanumeric: 0-9, a-z or A-Z
7f561c08de6b Initial load
duke
parents:
diff changeset
  2107
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2108
  * @return true if the char is alphanumeric, false otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
  2109
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2110
  private static boolean isAlphanum(char p_char) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2111
     return (p_char <= 'z' && (fgLookupTable[p_char] & MASK_ALPHA_NUMERIC) != 0);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2112
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2113
7f561c08de6b Initial load
duke
parents:
diff changeset
  2114
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2115
  * Determine whether a character is a reserved character:
7f561c08de6b Initial load
duke
parents:
diff changeset
  2116
  * ';', '/', '?', ':', '@', '&', '=', '+', '$', ',', '[', or ']'
7f561c08de6b Initial load
duke
parents:
diff changeset
  2117
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2118
  * @return true if the string contains any reserved characters
7f561c08de6b Initial load
duke
parents:
diff changeset
  2119
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2120
  private static boolean isReservedCharacter(char p_char) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2121
     return (p_char <= ']' && (fgLookupTable[p_char] & RESERVED_CHARACTERS) != 0);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2122
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2123
7f561c08de6b Initial load
duke
parents:
diff changeset
  2124
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2125
  * Determine whether a char is an unreserved character.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2126
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2127
  * @return true if the char is unreserved, false otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
  2128
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2129
  private static boolean isUnreservedCharacter(char p_char) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2130
     return (p_char <= '~' && (fgLookupTable[p_char] & MASK_UNRESERVED_MASK) != 0);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2131
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2132
7f561c08de6b Initial load
duke
parents:
diff changeset
  2133
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2134
  * Determine whether a char is a URI character (reserved or
7f561c08de6b Initial load
duke
parents:
diff changeset
  2135
  * unreserved, not including '%' for escaped octets).
7f561c08de6b Initial load
duke
parents:
diff changeset
  2136
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2137
  * @return true if the char is a URI character, false otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
  2138
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2139
  private static boolean isURICharacter (char p_char) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2140
      return (p_char <= '~' && (fgLookupTable[p_char] & MASK_URI_CHARACTER) != 0);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2141
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2142
7f561c08de6b Initial load
duke
parents:
diff changeset
  2143
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2144
  * Determine whether a char is a scheme character.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2145
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2146
  * @return true if the char is a scheme character, false otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
  2147
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2148
  private static boolean isSchemeCharacter (char p_char) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2149
      return (p_char <= 'z' && (fgLookupTable[p_char] & MASK_SCHEME_CHARACTER) != 0);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2150
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2151
7f561c08de6b Initial load
duke
parents:
diff changeset
  2152
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2153
  * Determine whether a char is a userinfo character.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2154
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2155
  * @return true if the char is a userinfo character, false otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
  2156
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2157
  private static boolean isUserinfoCharacter (char p_char) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2158
      return (p_char <= 'z' && (fgLookupTable[p_char] & MASK_USERINFO_CHARACTER) != 0);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2159
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2160
7f561c08de6b Initial load
duke
parents:
diff changeset
  2161
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2162
  * Determine whether a char is a path character.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2163
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2164
  * @return true if the char is a path character, false otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
  2165
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2166
  private static boolean isPathCharacter (char p_char) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2167
      return (p_char <= '~' && (fgLookupTable[p_char] & MASK_PATH_CHARACTER) != 0);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2168
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2169
7f561c08de6b Initial load
duke
parents:
diff changeset
  2170
7f561c08de6b Initial load
duke
parents:
diff changeset
  2171
 /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  2172
  * Determine whether a given string contains only URI characters (also
7f561c08de6b Initial load
duke
parents:
diff changeset
  2173
  * called "uric" in RFC 2396). uric consist of all reserved
7f561c08de6b Initial load
duke
parents:
diff changeset
  2174
  * characters, unreserved characters and escaped characters.
7f561c08de6b Initial load
duke
parents:
diff changeset
  2175
  *
7f561c08de6b Initial load
duke
parents:
diff changeset
  2176
  * @return true if the string is comprised of uric, false otherwise
7f561c08de6b Initial load
duke
parents:
diff changeset
  2177
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
  2178
  private static boolean isURIString(String p_uric) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2179
    if (p_uric == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2180
      return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2181
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2182
    int end = p_uric.length();
7f561c08de6b Initial load
duke
parents:
diff changeset
  2183
    char testChar = '\0';
7f561c08de6b Initial load
duke
parents:
diff changeset
  2184
    for (int i = 0; i < end; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2185
      testChar = p_uric.charAt(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
  2186
      if (testChar == '%') {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2187
        if (i+2 >= end ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  2188
            !isHex(p_uric.charAt(i+1)) ||
7f561c08de6b Initial load
duke
parents:
diff changeset
  2189
            !isHex(p_uric.charAt(i+2))) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2190
          return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2191
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2192
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2193
          i += 2;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2194
          continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2195
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2196
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2197
      if (isURICharacter(testChar)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2198
          continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2199
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2200
      else {
7f561c08de6b Initial load
duke
parents:
diff changeset
  2201
        return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2202
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2203
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2204
    return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  2205
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  2206
}