jaxp/src/com/sun/org/apache/xml/internal/utils/URI.java

Print this page




  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  * $Id: URI.java,v 1.2.4.1 2005/09/15 08:16:00 suresh_emailid Exp $
  22  */
  23 package com.sun.org.apache.xml.internal.utils;
  24 
  25 import java.io.IOException;
  26 import java.io.Serializable;
  27 
  28 import com.sun.org.apache.xml.internal.res.XMLErrorResources;
  29 import com.sun.org.apache.xml.internal.res.XMLMessages;

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


 866 
 867   /**
 868    * Get the scheme for this URI.
 869    *
 870    * @return the scheme for this URI
 871    */
 872   public String getScheme()
 873   {
 874     return m_scheme;
 875   }
 876 
 877   /**
 878    * Get the scheme-specific part for this URI (everything following the
 879    * scheme and the first colon). See RFC 2396 Section 5.2 for spec.
 880    *
 881    * @return the scheme-specific part for this URI
 882    */
 883   public String getSchemeSpecificPart()
 884   {
 885 
 886     StringBuffer schemespec = new StringBuffer();
 887 
 888     if (m_userinfo != null || m_host != null || m_port != -1)
 889     {
 890       schemespec.append("//");
 891     }
 892 
 893     if (m_userinfo != null)
 894     {
 895       schemespec.append(m_userinfo);
 896       schemespec.append('@');
 897     }
 898 
 899     if (m_host != null)
 900     {
 901       schemespec.append(m_host);
 902     }
 903 
 904     if (m_port != -1)
 905     {
 906       schemespec.append(':');


 958   }
 959 
 960   /**
 961    * Get the path for this URI (optionally with the query string and
 962    * fragment).
 963    *
 964    * @param p_includeQueryString if true (and query string is not null),
 965    *                             then a "?" followed by the query string
 966    *                             will be appended
 967    * @param p_includeFragment if true (and fragment is not null),
 968    *                             then a "#" followed by the fragment
 969    *                             will be appended
 970    *
 971    * @return the path for this URI possibly including the query string
 972    *         and fragment
 973    */
 974   public String getPath(boolean p_includeQueryString,
 975                         boolean p_includeFragment)
 976   {
 977 
 978     StringBuffer pathString = new StringBuffer(m_path);
 979 
 980     if (p_includeQueryString && m_queryString != null)
 981     {
 982       pathString.append('?');
 983       pathString.append(m_queryString);
 984     }
 985 
 986     if (p_includeFragment && m_fragment != null)
 987     {
 988       pathString.append('#');
 989       pathString.append(m_fragment);
 990     }
 991 
 992     return pathString.toString();
 993   }
 994 
 995   /**
 996    * Get the path for this URI. Note that the value returned is the path
 997    * only and does not include the query string or fragment.
 998    *


1324         XMLMessages.createXMLMessage(XMLErrorResources.ER_FRAG_WHEN_PATH_NULL, null)); //"Fragment cannot be set when path is null!");
1325     }
1326     else if (!isURIString(p_fragment))
1327     {
1328       throw new MalformedURIException(XMLMessages.createXMLMessage(XMLErrorResources.ER_FRAG_INVALID_CHAR, null)); //"Fragment contains invalid character!");
1329     }
1330     else
1331     {
1332       m_fragment = p_fragment;
1333     }
1334   }
1335 
1336   /**
1337    * Determines if the passed-in Object is equivalent to this URI.
1338    *
1339    * @param p_test the Object to test for equality.
1340    *
1341    * @return true if p_test is a URI with all values equal to this
1342    *         URI, false otherwise
1343    */

1344   public boolean equals(Object p_test)
1345   {
1346 
1347     if (p_test instanceof URI)
1348     {
1349       URI testURI = (URI) p_test;
1350 
1351       if (((m_scheme == null && testURI.m_scheme == null) || (m_scheme != null && testURI.m_scheme != null && m_scheme.equals(
1352               testURI.m_scheme))) && ((m_userinfo == null && testURI.m_userinfo == null) || (m_userinfo != null && testURI.m_userinfo != null && m_userinfo.equals(
1353               testURI.m_userinfo))) && ((m_host == null && testURI.m_host == null) || (m_host != null && testURI.m_host != null && m_host.equals(
1354               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(
1355               testURI.m_path))) && ((m_queryString == null && testURI.m_queryString == null) || (m_queryString != null && testURI.m_queryString != null && m_queryString.equals(
1356               testURI.m_queryString))) && ((m_fragment == null && testURI.m_fragment == null) || (m_fragment != null && testURI.m_fragment != null && m_fragment.equals(
1357               testURI.m_fragment))))
1358       {
1359         return true;
1360       }
1361     }
1362 
1363     return false;
1364   }
1365 













1366   /**
1367    * Get the URI as a string specification. See RFC 2396 Section 5.2.
1368    *
1369    * @return the URI string specification
1370    */

1371   public String toString()
1372   {
1373 
1374     StringBuffer uriSpecString = new StringBuffer();
1375 
1376     if (m_scheme != null)
1377     {
1378       uriSpecString.append(m_scheme);
1379       uriSpecString.append(':');
1380     }
1381 
1382     uriSpecString.append(getSchemeSpecificPart());
1383 
1384     return uriSpecString.toString();
1385   }
1386 
1387   /**
1388    * Get the indicator as to whether this URI uses the "generic URI"
1389    * syntax.
1390    *
1391    * @return true if this URI uses the "generic URI" syntax, false
1392    *         otherwise
1393    */
1394   public boolean isGenericURI()




  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  * $Id: URI.java,v 1.2.4.1 2005/09/15 08:16:00 suresh_emailid Exp $
  22  */
  23 package com.sun.org.apache.xml.internal.utils;
  24 
  25 import java.io.IOException;
  26 import java.io.Serializable;
  27 
  28 import com.sun.org.apache.xml.internal.res.XMLErrorResources;
  29 import com.sun.org.apache.xml.internal.res.XMLMessages;
  30 import java.util.Objects;
  31 
  32 /**
  33  * A class to represent a Uniform Resource Identifier (URI). This class
  34  * is designed to handle the parsing of URIs and provide access to
  35  * the various components (scheme, host, port, userinfo, path, query
  36  * string and fragment) that may constitute a URI.
  37  * <p>
  38  * Parsing of a URI specification is done according to the URI
  39  * syntax described in RFC 2396
  40  * <http://www.ietf.org/rfc/rfc2396.txt?number=2396>. Every URI consists
  41  * of a scheme, followed by a colon (':'), followed by a scheme-specific
  42  * part. For URIs that follow the "generic URI" syntax, the scheme-
  43  * specific part begins with two slashes ("//") and may be followed
  44  * by an authority segment (comprised of user information, host, and
  45  * port), path segment, query segment and fragment. Note that RFC 2396
  46  * no longer specifies the use of the parameters segment and excludes
  47  * the "user:password" syntax as part of the authority segment. If
  48  * "user:password" appears in a URI, the entire user/password string
  49  * is stored as userinfo.
  50  * <p>


 867 
 868   /**
 869    * Get the scheme for this URI.
 870    *
 871    * @return the scheme for this URI
 872    */
 873   public String getScheme()
 874   {
 875     return m_scheme;
 876   }
 877 
 878   /**
 879    * Get the scheme-specific part for this URI (everything following the
 880    * scheme and the first colon). See RFC 2396 Section 5.2 for spec.
 881    *
 882    * @return the scheme-specific part for this URI
 883    */
 884   public String getSchemeSpecificPart()
 885   {
 886 
 887     final StringBuilder schemespec = new StringBuilder();
 888 
 889     if (m_userinfo != null || m_host != null || m_port != -1)
 890     {
 891       schemespec.append("//");
 892     }
 893 
 894     if (m_userinfo != null)
 895     {
 896       schemespec.append(m_userinfo);
 897       schemespec.append('@');
 898     }
 899 
 900     if (m_host != null)
 901     {
 902       schemespec.append(m_host);
 903     }
 904 
 905     if (m_port != -1)
 906     {
 907       schemespec.append(':');


 959   }
 960 
 961   /**
 962    * Get the path for this URI (optionally with the query string and
 963    * fragment).
 964    *
 965    * @param p_includeQueryString if true (and query string is not null),
 966    *                             then a "?" followed by the query string
 967    *                             will be appended
 968    * @param p_includeFragment if true (and fragment is not null),
 969    *                             then a "#" followed by the fragment
 970    *                             will be appended
 971    *
 972    * @return the path for this URI possibly including the query string
 973    *         and fragment
 974    */
 975   public String getPath(boolean p_includeQueryString,
 976                         boolean p_includeFragment)
 977   {
 978 
 979     final StringBuilder pathString = new StringBuilder(m_path);
 980 
 981     if (p_includeQueryString && m_queryString != null)
 982     {
 983       pathString.append('?');
 984       pathString.append(m_queryString);
 985     }
 986 
 987     if (p_includeFragment && m_fragment != null)
 988     {
 989       pathString.append('#');
 990       pathString.append(m_fragment);
 991     }
 992 
 993     return pathString.toString();
 994   }
 995 
 996   /**
 997    * Get the path for this URI. Note that the value returned is the path
 998    * only and does not include the query string or fragment.
 999    *


1325         XMLMessages.createXMLMessage(XMLErrorResources.ER_FRAG_WHEN_PATH_NULL, null)); //"Fragment cannot be set when path is null!");
1326     }
1327     else if (!isURIString(p_fragment))
1328     {
1329       throw new MalformedURIException(XMLMessages.createXMLMessage(XMLErrorResources.ER_FRAG_INVALID_CHAR, null)); //"Fragment contains invalid character!");
1330     }
1331     else
1332     {
1333       m_fragment = p_fragment;
1334     }
1335   }
1336 
1337   /**
1338    * Determines if the passed-in Object is equivalent to this URI.
1339    *
1340    * @param p_test the Object to test for equality.
1341    *
1342    * @return true if p_test is a URI with all values equal to this
1343    *         URI, false otherwise
1344    */
1345   @Override
1346   public boolean equals(Object p_test)
1347   {
1348 
1349     if (p_test instanceof URI)
1350     {
1351       URI testURI = (URI) p_test;
1352 
1353       if (((m_scheme == null && testURI.m_scheme == null) || (m_scheme != null && testURI.m_scheme != null && m_scheme.equals(
1354               testURI.m_scheme))) && ((m_userinfo == null && testURI.m_userinfo == null) || (m_userinfo != null && testURI.m_userinfo != null && m_userinfo.equals(
1355               testURI.m_userinfo))) && ((m_host == null && testURI.m_host == null) || (m_host != null && testURI.m_host != null && m_host.equals(
1356               testURI.m_host))) && m_port == testURI.m_port && ((m_path == null && testURI.m_path == null) || (m_path != null && testURI.m_path != null && m_path.equals(
1357               testURI.m_path))) && ((m_queryString == null && testURI.m_queryString == null) || (m_queryString != null && testURI.m_queryString != null && m_queryString.equals(
1358               testURI.m_queryString))) && ((m_fragment == null && testURI.m_fragment == null) || (m_fragment != null && testURI.m_fragment != null && m_fragment.equals(
1359               testURI.m_fragment))))
1360       {
1361         return true;
1362       }
1363     }
1364 
1365     return false;
1366   }
1367 
1368   @Override
1369   public int hashCode() {
1370     int hash = 7;
1371     hash = 59 * hash + Objects.hashCode(this.m_scheme);
1372     hash = 59 * hash + Objects.hashCode(this.m_userinfo);
1373     hash = 59 * hash + Objects.hashCode(this.m_host);
1374     hash = 59 * hash + this.m_port;
1375     hash = 59 * hash + Objects.hashCode(this.m_path);
1376     hash = 59 * hash + Objects.hashCode(this.m_queryString);
1377     hash = 59 * hash + Objects.hashCode(this.m_fragment);
1378     return hash;
1379   }
1380 
1381   /**
1382    * Get the URI as a string specification. See RFC 2396 Section 5.2.
1383    *
1384    * @return the URI string specification
1385    */
1386   @Override
1387   public String toString()
1388   {
1389 
1390     final StringBuilder uriSpecString = new StringBuilder();
1391 
1392     if (m_scheme != null)
1393     {
1394       uriSpecString.append(m_scheme);
1395       uriSpecString.append(':');
1396     }
1397 
1398     uriSpecString.append(getSchemeSpecificPart());
1399 
1400     return uriSpecString.toString();
1401   }
1402 
1403   /**
1404    * Get the indicator as to whether this URI uses the "generic URI"
1405    * syntax.
1406    *
1407    * @return true if this URI uses the "generic URI" syntax, false
1408    *         otherwise
1409    */
1410   public boolean isGenericURI()