jaxp/src/com/sun/org/apache/xerces/internal/util/URI.java

Print this page




   5 /*
   6  * Copyright 1999-2005 The Apache Software Foundation.
   7  *
   8  * Licensed under the Apache License, Version 2.0 (the "License");
   9  * you may not use this file except in compliance with the License.
  10  * You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 
  21 package com.sun.org.apache.xerces.internal.util;
  22 
  23 import java.io.IOException;
  24 import java.io.Serializable;

  25 
  26 /**********************************************************************
  27 * A class to represent a Uniform Resource Identifier (URI). This class
  28 * is designed to handle the parsing of URIs and provide access to
  29 * the various components (scheme, host, port, userinfo, path, query
  30 * string and fragment) that may constitute a URI.
  31 * <p>
  32 * Parsing of a URI specification is done according to the URI
  33 * syntax described in
  34 * <a href="http://www.ietf.org/rfc/rfc2396.txt?number=2396">RFC 2396</a>,
  35 * and amended by
  36 * <a href="http://www.ietf.org/rfc/rfc2732.txt?number=2732">RFC 2732</a>.
  37 * <p>
  38 * Every absolute URI consists of a scheme, followed by a colon (':'),
  39 * followed by a scheme-specific part. For URIs that follow the
  40 * "generic URI" syntax, the scheme-specific part begins with two
  41 * slashes ("//") and may be followed by an authority segment (comprised
  42 * of user information, host, and port), path segment, query segment
  43 * and fragment. Note that RFC 2396 no longer specifies the use of the
  44 * parameters segment and excludes the "user:password" syntax as part of


1195       m_fragment = p_uriSpec.substring(start, index);
1196     }
1197   }
1198 
1199  /**
1200   * Get the scheme for this URI.
1201   *
1202   * @return the scheme for this URI
1203   */
1204   public String getScheme() {
1205     return m_scheme;
1206   }
1207 
1208  /**
1209   * Get the scheme-specific part for this URI (everything following the
1210   * scheme and the first colon). See RFC 2396 Section 5.2 for spec.
1211   *
1212   * @return the scheme-specific part for this URI
1213   */
1214   public String getSchemeSpecificPart() {
1215     StringBuffer schemespec = new StringBuffer();
1216 
1217     if (m_host != null || m_regAuthority != null) {
1218       schemespec.append("//");
1219 
1220       // Server based authority.
1221       if (m_host != null) {
1222 
1223         if (m_userinfo != null) {
1224           schemespec.append(m_userinfo);
1225           schemespec.append('@');
1226         }
1227 
1228         schemespec.append(m_host);
1229 
1230         if (m_port != -1) {
1231           schemespec.append(':');
1232           schemespec.append(m_port);
1233         }
1234       }
1235       // Registry based authority.


1280   */
1281   public int getPort() {
1282     return m_port;
1283   }
1284 
1285   /**
1286    * Get the registry based authority for this URI.
1287    *
1288    * @return the registry based authority (null if not specified).
1289    */
1290   public String getRegBasedAuthority() {
1291     return m_regAuthority;
1292   }
1293 
1294   /**
1295    * Get the authority for this URI.
1296    *
1297    * @return the authority
1298    */
1299   public String getAuthority() {
1300       StringBuffer authority = new StringBuffer();
1301       if (m_host != null || m_regAuthority != null) {
1302           authority.append("//");
1303 
1304           // Server based authority.
1305           if (m_host != null) {
1306 
1307               if (m_userinfo != null) {
1308                   authority.append(m_userinfo);
1309                   authority.append('@');
1310               }
1311 
1312               authority.append(m_host);
1313 
1314               if (m_port != -1) {
1315                   authority.append(':');
1316                   authority.append(m_port);
1317               }
1318           }
1319           // Registry based authority.
1320           else {


1323       }
1324       return authority.toString();
1325   }
1326 
1327  /**
1328   * Get the path for this URI (optionally with the query string and
1329   * fragment).
1330   *
1331   * @param p_includeQueryString if true (and query string is not null),
1332   *                             then a "?" followed by the query string
1333   *                             will be appended
1334   * @param p_includeFragment if true (and fragment is not null),
1335   *                             then a "#" followed by the fragment
1336   *                             will be appended
1337   *
1338   * @return the path for this URI possibly including the query string
1339   *         and fragment
1340   */
1341   public String getPath(boolean p_includeQueryString,
1342                         boolean p_includeFragment) {
1343     StringBuffer pathString = new StringBuffer(m_path);
1344 
1345     if (p_includeQueryString && m_queryString != null) {
1346       pathString.append('?');
1347       pathString.append(m_queryString);
1348     }
1349 
1350     if (p_includeFragment && m_fragment != null) {
1351       pathString.append('#');
1352       pathString.append(m_fragment);
1353     }
1354     return pathString.toString();
1355   }
1356 
1357  /**
1358   * Get the path for this URI. Note that the value returned is the path
1359   * only and does not include the query string or fragment.
1360   *
1361   * @return the path for this URI.
1362   */
1363   public String getPath() {


1666       throw new MalformedURIException(
1667               "Fragment cannot be set when path is null!");
1668     }
1669     else if (!isURIString(p_fragment)) {
1670       throw new MalformedURIException(
1671               "Fragment contains invalid character!");
1672     }
1673     else {
1674       m_fragment = p_fragment;
1675     }
1676   }
1677 
1678  /**
1679   * Determines if the passed-in Object is equivalent to this URI.
1680   *
1681   * @param p_test the Object to test for equality.
1682   *
1683   * @return true if p_test is a URI with all values equal to this
1684   *         URI, false otherwise
1685   */

1686   public boolean equals(Object p_test) {
1687     if (p_test instanceof URI) {
1688       URI testURI = (URI) p_test;
1689       if (((m_scheme == null && testURI.m_scheme == null) ||
1690            (m_scheme != null && testURI.m_scheme != null &&
1691             m_scheme.equals(testURI.m_scheme))) &&
1692           ((m_userinfo == null && testURI.m_userinfo == null) ||
1693            (m_userinfo != null && testURI.m_userinfo != null &&
1694             m_userinfo.equals(testURI.m_userinfo))) &&
1695           ((m_host == null && testURI.m_host == null) ||
1696            (m_host != null && testURI.m_host != null &&
1697             m_host.equals(testURI.m_host))) &&
1698             m_port == testURI.m_port &&
1699           ((m_path == null && testURI.m_path == null) ||
1700            (m_path != null && testURI.m_path != null &&
1701             m_path.equals(testURI.m_path))) &&
1702           ((m_queryString == null && testURI.m_queryString == null) ||
1703            (m_queryString != null && testURI.m_queryString != null &&
1704             m_queryString.equals(testURI.m_queryString))) &&
1705           ((m_fragment == null && testURI.m_fragment == null) ||
1706            (m_fragment != null && testURI.m_fragment != null &&
1707             m_fragment.equals(testURI.m_fragment)))) {
1708         return true;
1709       }
1710     }
1711     return false;
1712   }
1713 













1714  /**
1715   * Get the URI as a string specification. See RFC 2396 Section 5.2.
1716   *
1717   * @return the URI string specification
1718   */

1719   public String toString() {
1720     StringBuffer uriSpecString = new StringBuffer();
1721 
1722     if (m_scheme != null) {
1723       uriSpecString.append(m_scheme);
1724       uriSpecString.append(':');
1725     }
1726     uriSpecString.append(getSchemeSpecificPart());
1727     return uriSpecString.toString();
1728   }
1729 
1730  /**
1731   * Get the indicator as to whether this URI uses the "generic URI"
1732   * syntax.
1733   *
1734   * @return true if this URI uses the "generic URI" syntax, false
1735   *         otherwise
1736   */
1737   public boolean isGenericURI() {
1738     // presence of the host (whether valid or empty) means
1739     // double-slashes which means generic uri
1740     return (m_host != null);




   5 /*
   6  * Copyright 1999-2005 The Apache Software Foundation.
   7  *
   8  * Licensed under the Apache License, Version 2.0 (the "License");
   9  * you may not use this file except in compliance with the License.
  10  * You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 
  21 package com.sun.org.apache.xerces.internal.util;
  22 
  23 import java.io.IOException;
  24 import java.io.Serializable;
  25 import java.util.Objects;
  26 
  27 /**********************************************************************
  28 * A class to represent a Uniform Resource Identifier (URI). This class
  29 * is designed to handle the parsing of URIs and provide access to
  30 * the various components (scheme, host, port, userinfo, path, query
  31 * string and fragment) that may constitute a URI.
  32 * <p>
  33 * Parsing of a URI specification is done according to the URI
  34 * syntax described in
  35 * <a href="http://www.ietf.org/rfc/rfc2396.txt?number=2396">RFC 2396</a>,
  36 * and amended by
  37 * <a href="http://www.ietf.org/rfc/rfc2732.txt?number=2732">RFC 2732</a>.
  38 * <p>
  39 * Every absolute URI consists of a scheme, followed by a colon (':'),
  40 * followed by a scheme-specific part. For URIs that follow the
  41 * "generic URI" syntax, the scheme-specific part begins with two
  42 * slashes ("//") and may be followed by an authority segment (comprised
  43 * of user information, host, and port), path segment, query segment
  44 * and fragment. Note that RFC 2396 no longer specifies the use of the
  45 * parameters segment and excludes the "user:password" syntax as part of


1196       m_fragment = p_uriSpec.substring(start, index);
1197     }
1198   }
1199 
1200  /**
1201   * Get the scheme for this URI.
1202   *
1203   * @return the scheme for this URI
1204   */
1205   public String getScheme() {
1206     return m_scheme;
1207   }
1208 
1209  /**
1210   * Get the scheme-specific part for this URI (everything following the
1211   * scheme and the first colon). See RFC 2396 Section 5.2 for spec.
1212   *
1213   * @return the scheme-specific part for this URI
1214   */
1215   public String getSchemeSpecificPart() {
1216     final StringBuilder schemespec = new StringBuilder();
1217 
1218     if (m_host != null || m_regAuthority != null) {
1219       schemespec.append("//");
1220 
1221       // Server based authority.
1222       if (m_host != null) {
1223 
1224         if (m_userinfo != null) {
1225           schemespec.append(m_userinfo);
1226           schemespec.append('@');
1227         }
1228 
1229         schemespec.append(m_host);
1230 
1231         if (m_port != -1) {
1232           schemespec.append(':');
1233           schemespec.append(m_port);
1234         }
1235       }
1236       // Registry based authority.


1281   */
1282   public int getPort() {
1283     return m_port;
1284   }
1285 
1286   /**
1287    * Get the registry based authority for this URI.
1288    *
1289    * @return the registry based authority (null if not specified).
1290    */
1291   public String getRegBasedAuthority() {
1292     return m_regAuthority;
1293   }
1294 
1295   /**
1296    * Get the authority for this URI.
1297    *
1298    * @return the authority
1299    */
1300   public String getAuthority() {
1301       final StringBuilder authority = new StringBuilder();
1302       if (m_host != null || m_regAuthority != null) {
1303           authority.append("//");
1304 
1305           // Server based authority.
1306           if (m_host != null) {
1307 
1308               if (m_userinfo != null) {
1309                   authority.append(m_userinfo);
1310                   authority.append('@');
1311               }
1312 
1313               authority.append(m_host);
1314 
1315               if (m_port != -1) {
1316                   authority.append(':');
1317                   authority.append(m_port);
1318               }
1319           }
1320           // Registry based authority.
1321           else {


1324       }
1325       return authority.toString();
1326   }
1327 
1328  /**
1329   * Get the path for this URI (optionally with the query string and
1330   * fragment).
1331   *
1332   * @param p_includeQueryString if true (and query string is not null),
1333   *                             then a "?" followed by the query string
1334   *                             will be appended
1335   * @param p_includeFragment if true (and fragment is not null),
1336   *                             then a "#" followed by the fragment
1337   *                             will be appended
1338   *
1339   * @return the path for this URI possibly including the query string
1340   *         and fragment
1341   */
1342   public String getPath(boolean p_includeQueryString,
1343                         boolean p_includeFragment) {
1344     final StringBuilder pathString = new StringBuilder(m_path);
1345 
1346     if (p_includeQueryString && m_queryString != null) {
1347       pathString.append('?');
1348       pathString.append(m_queryString);
1349     }
1350 
1351     if (p_includeFragment && m_fragment != null) {
1352       pathString.append('#');
1353       pathString.append(m_fragment);
1354     }
1355     return pathString.toString();
1356   }
1357 
1358  /**
1359   * Get the path for this URI. Note that the value returned is the path
1360   * only and does not include the query string or fragment.
1361   *
1362   * @return the path for this URI.
1363   */
1364   public String getPath() {


1667       throw new MalformedURIException(
1668               "Fragment cannot be set when path is null!");
1669     }
1670     else if (!isURIString(p_fragment)) {
1671       throw new MalformedURIException(
1672               "Fragment contains invalid character!");
1673     }
1674     else {
1675       m_fragment = p_fragment;
1676     }
1677   }
1678 
1679  /**
1680   * Determines if the passed-in Object is equivalent to this URI.
1681   *
1682   * @param p_test the Object to test for equality.
1683   *
1684   * @return true if p_test is a URI with all values equal to this
1685   *         URI, false otherwise
1686   */
1687   @Override
1688   public boolean equals(Object p_test) {
1689     if (p_test instanceof URI) {
1690       URI testURI = (URI) p_test;
1691       if (((m_scheme == null && testURI.m_scheme == null) ||
1692            (m_scheme != null && testURI.m_scheme != null &&
1693             m_scheme.equals(testURI.m_scheme))) &&
1694           ((m_userinfo == null && testURI.m_userinfo == null) ||
1695            (m_userinfo != null && testURI.m_userinfo != null &&
1696             m_userinfo.equals(testURI.m_userinfo))) &&
1697           ((m_host == null && testURI.m_host == null) ||
1698            (m_host != null && testURI.m_host != null &&
1699             m_host.equals(testURI.m_host))) &&
1700             m_port == testURI.m_port &&
1701           ((m_path == null && testURI.m_path == null) ||
1702            (m_path != null && testURI.m_path != null &&
1703             m_path.equals(testURI.m_path))) &&
1704           ((m_queryString == null && testURI.m_queryString == null) ||
1705            (m_queryString != null && testURI.m_queryString != null &&
1706             m_queryString.equals(testURI.m_queryString))) &&
1707           ((m_fragment == null && testURI.m_fragment == null) ||
1708            (m_fragment != null && testURI.m_fragment != null &&
1709             m_fragment.equals(testURI.m_fragment)))) {
1710         return true;
1711       }
1712     }
1713     return false;
1714   }
1715 
1716     @Override
1717     public int hashCode() {
1718         int hash = 5;
1719         hash = 47 * hash + Objects.hashCode(this.m_scheme);
1720         hash = 47 * hash + Objects.hashCode(this.m_userinfo);
1721         hash = 47 * hash + Objects.hashCode(this.m_host);
1722         hash = 47 * hash + this.m_port;
1723         hash = 47 * hash + Objects.hashCode(this.m_path);
1724         hash = 47 * hash + Objects.hashCode(this.m_queryString);
1725         hash = 47 * hash + Objects.hashCode(this.m_fragment);
1726         return hash;
1727     }
1728 
1729  /**
1730   * Get the URI as a string specification. See RFC 2396 Section 5.2.
1731   *
1732   * @return the URI string specification
1733   */
1734   @Override
1735   public String toString() {
1736     final StringBuilder uriSpecString = new StringBuilder();
1737 
1738     if (m_scheme != null) {
1739       uriSpecString.append(m_scheme);
1740       uriSpecString.append(':');
1741     }
1742     uriSpecString.append(getSchemeSpecificPart());
1743     return uriSpecString.toString();
1744   }
1745 
1746  /**
1747   * Get the indicator as to whether this URI uses the "generic URI"
1748   * syntax.
1749   *
1750   * @return true if this URI uses the "generic URI" syntax, false
1751   *         otherwise
1752   */
1753   public boolean isGenericURI() {
1754     // presence of the host (whether valid or empty) means
1755     // double-slashes which means generic uri
1756     return (m_host != null);