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