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