< prev index next >

src/java.xml.ws/share/classes/com/sun/xml/internal/messaging/saaj/util/JaxmURI.java

Print this page


   1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.messaging.saaj.util;
  27 
  28 // Imported from: org.apache.xerces.util
  29 // Needed to work around differences in JDK1.2 and 1.3 and deal with userInfo
  30 
  31 import java.io.IOException;
  32 import java.io.Serializable;
  33 
  34 
  35 /**********************************************************************
  36 * A class to represent a Uniform Resource Identifier (URI). This class
  37 * is designed to handle the parsing of URIs and provide access to
  38 * the various components (scheme, host, port, userinfo, path, query
  39 * string and fragment) that may constitute a URI.
  40 * <p>
  41 * Parsing of a URI specification is done according to the URI
  42 * syntax described in RFC 2396
  43 * <http://www.ietf.org/rfc/rfc2396.txt?number=2396>. Every URI consists
  44 * of a scheme, followed by a colon (':'), followed by a scheme-specific
  45 * part. For URIs that follow the "generic URI" syntax, the scheme-
  46 * specific part begins with two slashes ("//") and may be followed
  47 * by an authority segment (comprised of user information, host, and
  48 * port), path segment, query segment and fragment. Note that RFC 2396
  49 * no longer specifies the use of the parameters segment and excludes
  50 * the "user:password" syntax as part of the authority segment. If
  51 * "user:password" appears in a URI, the entire user/password string
  52 * is stored as userinfo.
  53 * <p>
  54 * For URIs that do not follow the "generic URI" syntax (e.g. mailto),
  55 * the entire scheme-specific part is treated as the "path" portion
  56 * of the URI.
  57 * <p>
  58 * Note that, unlike the java.net.URL class, this class does not provide
  59 * any built-in network access functionality nor does it provide any
  60 * scheme-specific functionality (for example, it does not know a
  61 * default port for a specific scheme). Rather, it only knows the
  62 * grammar and basic set of operations that can be applied to a URI.
  63 *
  64 * @version


 112 
 113   /** If specified, stores the userinfo for this URI; otherwise null */
 114   private String m_userinfo = null;
 115 
 116   /** If specified, stores the host for this URI; otherwise null */
 117   private String m_host = null;
 118 
 119   /** If specified, stores the port for this URI; otherwise -1 */
 120   private int m_port = -1;
 121 
 122   /** If specified, stores the path for this URI; otherwise null */
 123   private String m_path = null;
 124 
 125   /** If specified, stores the query string for this URI; otherwise
 126       null.  */
 127   private String m_queryString = null;
 128 
 129   /** If specified, stores the fragment for this URI; otherwise null */
 130   private String m_fragment = null;
 131 
 132   private static boolean DEBUG = false;
 133 
 134   /**
 135   * Construct a new and uninitialized URI.
 136   */
 137   public JaxmURI() {
 138   }
 139 
 140  /**
 141   * Construct a new URI from another URI. All fields for this URI are
 142   * set equal to the fields of the URI passed in.
 143   *
 144   * @param p_other the URI to copy (cannot be null)
 145   */
 146   public JaxmURI(JaxmURI p_other) {
 147     initialize(p_other);
 148   }
 149 
 150  /**
 151   * Construct a new URI from a URI specification string. If the
 152   * specification follows the "generic URI" syntax, (two slashes
 153   * following the first colon), the specification will be parsed


 708       m_fragment = p_uriSpec.substring(start, index);
 709     }
 710   }
 711 
 712  /**
 713   * Get the scheme for this URI.
 714   *
 715   * @return the scheme for this URI
 716   */
 717   public String getScheme() {
 718     return m_scheme;
 719   }
 720 
 721  /**
 722   * Get the scheme-specific part for this URI (everything following the
 723   * scheme and the first colon). See RFC 2396 Section 5.2 for spec.
 724   *
 725   * @return the scheme-specific part for this URI
 726   */
 727   public String getSchemeSpecificPart() {
 728     StringBuffer schemespec = new StringBuffer();
 729 
 730     if (m_userinfo != null || m_host != null || m_port != -1) {
 731       schemespec.append("//");
 732     }
 733 
 734     if (m_userinfo != null) {
 735       schemespec.append(m_userinfo);
 736       schemespec.append('@');
 737     }
 738 
 739     if (m_host != null) {
 740       schemespec.append(m_host);
 741     }
 742 
 743     if (m_port != -1) {
 744       schemespec.append(':');
 745       schemespec.append(m_port);
 746     }
 747 
 748     if (m_path != null) {


 788   public int getPort() {
 789     return m_port;
 790   }
 791 
 792  /**
 793   * Get the path for this URI (optionally with the query string and
 794   * fragment).
 795   *
 796   * @param p_includeQueryString if true (and query string is not null),
 797   *                             then a "?" followed by the query string
 798   *                             will be appended
 799   * @param p_includeFragment if true (and fragment is not null),
 800   *                             then a "#" followed by the fragment
 801   *                             will be appended
 802   *
 803   * @return the path for this URI possibly including the query string
 804   *         and fragment
 805   */
 806   public String getPath(boolean p_includeQueryString,
 807                         boolean p_includeFragment) {
 808     StringBuffer pathString = new StringBuffer(m_path);
 809 
 810     if (p_includeQueryString && m_queryString != null) {
 811       pathString.append('?');
 812       pathString.append(m_queryString);
 813     }
 814 
 815     if (p_includeFragment && m_fragment != null) {
 816       pathString.append('#');
 817       pathString.append(m_fragment);
 818     }
 819     return pathString.toString();
 820   }
 821 
 822  /**
 823   * Get the path for this URI. Note that the value returned is the path
 824   * only and does not include the query string or fragment.
 825   *
 826   * @return the path for this URI.
 827   */
 828   public String getPath() {


1131           ((m_fragment == null && testURI.m_fragment == null) ||
1132            (m_fragment != null && testURI.m_fragment != null &&
1133             m_fragment.equals(testURI.m_fragment)))) {
1134         return true;
1135       }
1136     }
1137     return false;
1138   }
1139 
1140   public int hashCode() {
1141           // No members safe to use, just default to a constant.
1142           return 153214;
1143   }
1144 
1145  /**
1146   * Get the URI as a string specification. See RFC 2396 Section 5.2.
1147   *
1148   * @return the URI string specification
1149   */
1150   public String toString() {
1151     StringBuffer uriSpecString = new StringBuffer();
1152 
1153     if (m_scheme != null) {
1154       uriSpecString.append(m_scheme);
1155       uriSpecString.append(':');
1156     }
1157     uriSpecString.append(getSchemeSpecificPart());
1158     return uriSpecString.toString();
1159   }
1160 
1161  /**
1162   * Get the indicator as to whether this URI uses the "generic URI"
1163   * syntax.
1164   *
1165   * @return true if this URI uses the "generic URI" syntax, false
1166   *         otherwise
1167   */
1168   public boolean isGenericURI() {
1169     // presence of the host (whether valid or empty) means
1170     // double-slashes which means generic uri
1171     return (m_host != null);


   1 /*
   2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.messaging.saaj.util;
  27 
  28 // Imported from: org.apache.xerces.util
  29 // Needed to work around differences in JDK1.2 and 1.3 and deal with userInfo
  30 
  31 import java.io.IOException;
  32 import java.io.Serializable;
  33 
  34 
  35 /**********************************************************************
  36 * A class to represent a Uniform Resource Identifier (URI). This class
  37 * is designed to handle the parsing of URIs and provide access to
  38 * the various components (scheme, host, port, userinfo, path, query
  39 * string and fragment) that may constitute a URI.
  40 * <p>
  41 * Parsing of a URI specification is done according to the URI
  42 * syntax described in <a href="http://www.ietf.org/rfc/rfc2396.txt?number=2396">RFC 2396</a>.
  43 * Every URI consists of a scheme, followed by a colon (':'), followed by a scheme-specific

  44 * part. For URIs that follow the "generic URI" syntax, the scheme-
  45 * specific part begins with two slashes ("//") and may be followed
  46 * by an authority segment (comprised of user information, host, and
  47 * port), path segment, query segment and fragment. Note that RFC 2396
  48 * no longer specifies the use of the parameters segment and excludes
  49 * the "user:password" syntax as part of the authority segment. If
  50 * "user:password" appears in a URI, the entire user/password string
  51 * is stored as userinfo.
  52 * <p>
  53 * For URIs that do not follow the "generic URI" syntax (e.g. mailto),
  54 * the entire scheme-specific part is treated as the "path" portion
  55 * of the URI.
  56 * <p>
  57 * Note that, unlike the java.net.URL class, this class does not provide
  58 * any built-in network access functionality nor does it provide any
  59 * scheme-specific functionality (for example, it does not know a
  60 * default port for a specific scheme). Rather, it only knows the
  61 * grammar and basic set of operations that can be applied to a URI.
  62 *
  63 * @version


 111 
 112   /** If specified, stores the userinfo for this URI; otherwise null */
 113   private String m_userinfo = null;
 114 
 115   /** If specified, stores the host for this URI; otherwise null */
 116   private String m_host = null;
 117 
 118   /** If specified, stores the port for this URI; otherwise -1 */
 119   private int m_port = -1;
 120 
 121   /** If specified, stores the path for this URI; otherwise null */
 122   private String m_path = null;
 123 
 124   /** If specified, stores the query string for this URI; otherwise
 125       null.  */
 126   private String m_queryString = null;
 127 
 128   /** If specified, stores the fragment for this URI; otherwise null */
 129   private String m_fragment = null;
 130 


 131   /**
 132   * Construct a new and uninitialized URI.
 133   */
 134   public JaxmURI() {
 135   }
 136 
 137  /**
 138   * Construct a new URI from another URI. All fields for this URI are
 139   * set equal to the fields of the URI passed in.
 140   *
 141   * @param p_other the URI to copy (cannot be null)
 142   */
 143   public JaxmURI(JaxmURI p_other) {
 144     initialize(p_other);
 145   }
 146 
 147  /**
 148   * Construct a new URI from a URI specification string. If the
 149   * specification follows the "generic URI" syntax, (two slashes
 150   * following the first colon), the specification will be parsed


 705       m_fragment = p_uriSpec.substring(start, index);
 706     }
 707   }
 708 
 709  /**
 710   * Get the scheme for this URI.
 711   *
 712   * @return the scheme for this URI
 713   */
 714   public String getScheme() {
 715     return m_scheme;
 716   }
 717 
 718  /**
 719   * Get the scheme-specific part for this URI (everything following the
 720   * scheme and the first colon). See RFC 2396 Section 5.2 for spec.
 721   *
 722   * @return the scheme-specific part for this URI
 723   */
 724   public String getSchemeSpecificPart() {
 725     StringBuilder schemespec = new StringBuilder();
 726 
 727     if (m_userinfo != null || m_host != null || m_port != -1) {
 728       schemespec.append("//");
 729     }
 730 
 731     if (m_userinfo != null) {
 732       schemespec.append(m_userinfo);
 733       schemespec.append('@');
 734     }
 735 
 736     if (m_host != null) {
 737       schemespec.append(m_host);
 738     }
 739 
 740     if (m_port != -1) {
 741       schemespec.append(':');
 742       schemespec.append(m_port);
 743     }
 744 
 745     if (m_path != null) {


 785   public int getPort() {
 786     return m_port;
 787   }
 788 
 789  /**
 790   * Get the path for this URI (optionally with the query string and
 791   * fragment).
 792   *
 793   * @param p_includeQueryString if true (and query string is not null),
 794   *                             then a "?" followed by the query string
 795   *                             will be appended
 796   * @param p_includeFragment if true (and fragment is not null),
 797   *                             then a "#" followed by the fragment
 798   *                             will be appended
 799   *
 800   * @return the path for this URI possibly including the query string
 801   *         and fragment
 802   */
 803   public String getPath(boolean p_includeQueryString,
 804                         boolean p_includeFragment) {
 805     StringBuilder pathString = new StringBuilder(m_path);
 806 
 807     if (p_includeQueryString && m_queryString != null) {
 808       pathString.append('?');
 809       pathString.append(m_queryString);
 810     }
 811 
 812     if (p_includeFragment && m_fragment != null) {
 813       pathString.append('#');
 814       pathString.append(m_fragment);
 815     }
 816     return pathString.toString();
 817   }
 818 
 819  /**
 820   * Get the path for this URI. Note that the value returned is the path
 821   * only and does not include the query string or fragment.
 822   *
 823   * @return the path for this URI.
 824   */
 825   public String getPath() {


1128           ((m_fragment == null && testURI.m_fragment == null) ||
1129            (m_fragment != null && testURI.m_fragment != null &&
1130             m_fragment.equals(testURI.m_fragment)))) {
1131         return true;
1132       }
1133     }
1134     return false;
1135   }
1136 
1137   public int hashCode() {
1138           // No members safe to use, just default to a constant.
1139           return 153214;
1140   }
1141 
1142  /**
1143   * Get the URI as a string specification. See RFC 2396 Section 5.2.
1144   *
1145   * @return the URI string specification
1146   */
1147   public String toString() {
1148     StringBuilder uriSpecString = new StringBuilder();
1149 
1150     if (m_scheme != null) {
1151       uriSpecString.append(m_scheme);
1152       uriSpecString.append(':');
1153     }
1154     uriSpecString.append(getSchemeSpecificPart());
1155     return uriSpecString.toString();
1156   }
1157 
1158  /**
1159   * Get the indicator as to whether this URI uses the "generic URI"
1160   * syntax.
1161   *
1162   * @return true if this URI uses the "generic URI" syntax, false
1163   *         otherwise
1164   */
1165   public boolean isGenericURI() {
1166     // presence of the host (whether valid or empty) means
1167     // double-slashes which means generic uri
1168     return (m_host != null);


< prev index next >