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