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

Print this page




   5 /*
   6  * Copyright 1999-2004 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  * $Id: URI.java,v 1.1.4.1 2005/09/08 11:03:20 suresh_emailid Exp $
  22  */
  23 package com.sun.org.apache.xml.internal.serializer.utils;
  24 

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


 846 
 847   /**
 848    * Get the scheme for this URI.
 849    *
 850    * @return the scheme for this URI
 851    */
 852   public String getScheme()
 853   {
 854     return m_scheme;
 855   }
 856 
 857   /**
 858    * Get the scheme-specific part for this URI (everything following the
 859    * scheme and the first colon). See RFC 2396 Section 5.2 for spec.
 860    *
 861    * @return the scheme-specific part for this URI
 862    */
 863   public String getSchemeSpecificPart()
 864   {
 865 
 866     StringBuffer schemespec = new StringBuffer();
 867 
 868     if (m_userinfo != null || m_host != null || m_port != -1)
 869     {
 870       schemespec.append("//");
 871     }
 872 
 873     if (m_userinfo != null)
 874     {
 875       schemespec.append(m_userinfo);
 876       schemespec.append('@');
 877     }
 878 
 879     if (m_host != null)
 880     {
 881       schemespec.append(m_host);
 882     }
 883 
 884     if (m_port != -1)
 885     {
 886       schemespec.append(':');


 938   }
 939 
 940   /**
 941    * Get the path for this URI (optionally with the query string and
 942    * fragment).
 943    *
 944    * @param p_includeQueryString if true (and query string is not null),
 945    *                             then a "?" followed by the query string
 946    *                             will be appended
 947    * @param p_includeFragment if true (and fragment is not null),
 948    *                             then a "#" followed by the fragment
 949    *                             will be appended
 950    *
 951    * @return the path for this URI possibly including the query string
 952    *         and fragment
 953    */
 954   public String getPath(boolean p_includeQueryString,
 955                         boolean p_includeFragment)
 956   {
 957 
 958     StringBuffer pathString = new StringBuffer(m_path);
 959 
 960     if (p_includeQueryString && m_queryString != null)
 961     {
 962       pathString.append('?');
 963       pathString.append(m_queryString);
 964     }
 965 
 966     if (p_includeFragment && m_fragment != null)
 967     {
 968       pathString.append('#');
 969       pathString.append(m_fragment);
 970     }
 971 
 972     return pathString.toString();
 973   }
 974 
 975   /**
 976    * Get the path for this URI. Note that the value returned is the path
 977    * only and does not include the query string or fragment.
 978    *


1304         Utils.messages.createMessage(MsgKey.ER_FRAG_WHEN_PATH_NULL, null)); //"Fragment cannot be set when path is null!");
1305     }
1306     else if (!isURIString(p_fragment))
1307     {
1308       throw new MalformedURIException(Utils.messages.createMessage(MsgKey.ER_FRAG_INVALID_CHAR, null)); //"Fragment contains invalid character!");
1309     }
1310     else
1311     {
1312       m_fragment = p_fragment;
1313     }
1314   }
1315 
1316   /**
1317    * Determines if the passed-in Object is equivalent to this URI.
1318    *
1319    * @param p_test the Object to test for equality.
1320    *
1321    * @return true if p_test is a URI with all values equal to this
1322    *         URI, false otherwise
1323    */

1324   public boolean equals(Object p_test)
1325   {
1326 
1327     if (p_test instanceof URI)
1328     {
1329       URI testURI = (URI) p_test;
1330 
1331       if (((m_scheme == null && testURI.m_scheme == null) || (m_scheme != null && testURI.m_scheme != null && m_scheme.equals(
1332               testURI.m_scheme))) && ((m_userinfo == null && testURI.m_userinfo == null) || (m_userinfo != null && testURI.m_userinfo != null && m_userinfo.equals(
1333               testURI.m_userinfo))) && ((m_host == null && testURI.m_host == null) || (m_host != null && testURI.m_host != null && m_host.equals(
1334               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(
1335               testURI.m_path))) && ((m_queryString == null && testURI.m_queryString == null) || (m_queryString != null && testURI.m_queryString != null && m_queryString.equals(
1336               testURI.m_queryString))) && ((m_fragment == null && testURI.m_fragment == null) || (m_fragment != null && testURI.m_fragment != null && m_fragment.equals(
1337               testURI.m_fragment))))
1338       {
1339         return true;
1340       }
1341     }
1342 
1343     return false;
1344   }
1345 













1346   /**
1347    * Get the URI as a string specification. See RFC 2396 Section 5.2.
1348    *
1349    * @return the URI string specification
1350    */

1351   public String toString()
1352   {
1353 
1354     StringBuffer uriSpecString = new StringBuffer();
1355 
1356     if (m_scheme != null)
1357     {
1358       uriSpecString.append(m_scheme);
1359       uriSpecString.append(':');
1360     }
1361 
1362     uriSpecString.append(getSchemeSpecificPart());
1363 
1364     return uriSpecString.toString();
1365   }
1366 
1367   /**
1368    * Get the indicator as to whether this URI uses the "generic URI"
1369    * syntax.
1370    *
1371    * @return true if this URI uses the "generic URI" syntax, false
1372    *         otherwise
1373    */
1374   public boolean isGenericURI()


1526     return true;
1527   }
1528 
1529   /**
1530    * Determine whether a char is a digit.
1531    *
1532    *
1533    * @param p_char the character to check
1534    * @return true if the char is betweeen '0' and '9', false otherwise
1535    */
1536   private static boolean isDigit(char p_char)
1537   {
1538     return p_char >= '0' && p_char <= '9';
1539   }
1540 
1541   /**
1542    * Determine whether a character is a hexadecimal character.
1543    *
1544    *
1545    * @param p_char the character to check
1546    * @return true if the char is betweeen '0' and '9', 'a' and 'f'
1547    *         or 'A' and 'F', false otherwise
1548    */
1549   private static boolean isHex(char p_char)
1550   {
1551     return (isDigit(p_char) || (p_char >= 'a' && p_char <= 'f')
1552             || (p_char >= 'A' && p_char <= 'F'));
1553   }
1554 
1555   /**
1556    * Determine whether a char is an alphabetic character: a-z or A-Z
1557    *
1558    *
1559    * @param p_char the character to check
1560    * @return true if the char is alphabetic, false otherwise
1561    */
1562   private static boolean isAlpha(char p_char)
1563   {
1564     return ((p_char >= 'a' && p_char <= 'z')
1565             || (p_char >= 'A' && p_char <= 'Z'));
1566   }




   5 /*
   6  * Copyright 1999-2004 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  * $Id: URI.java,v 1.1.4.1 2005/09/08 11:03:20 suresh_emailid Exp $
  22  */
  23 package com.sun.org.apache.xml.internal.serializer.utils;
  24 
  25 import com.sun.org.apache.xalan.internal.utils.Objects;
  26 import java.io.IOException;

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


 846 
 847   /**
 848    * Get the scheme for this URI.
 849    *
 850    * @return the scheme for this URI
 851    */
 852   public String getScheme()
 853   {
 854     return m_scheme;
 855   }
 856 
 857   /**
 858    * Get the scheme-specific part for this URI (everything following the
 859    * scheme and the first colon). See RFC 2396 Section 5.2 for spec.
 860    *
 861    * @return the scheme-specific part for this URI
 862    */
 863   public String getSchemeSpecificPart()
 864   {
 865 
 866     final StringBuilder schemespec = new StringBuilder();
 867 
 868     if (m_userinfo != null || m_host != null || m_port != -1)
 869     {
 870       schemespec.append("//");
 871     }
 872 
 873     if (m_userinfo != null)
 874     {
 875       schemespec.append(m_userinfo);
 876       schemespec.append('@');
 877     }
 878 
 879     if (m_host != null)
 880     {
 881       schemespec.append(m_host);
 882     }
 883 
 884     if (m_port != -1)
 885     {
 886       schemespec.append(':');


 938   }
 939 
 940   /**
 941    * Get the path for this URI (optionally with the query string and
 942    * fragment).
 943    *
 944    * @param p_includeQueryString if true (and query string is not null),
 945    *                             then a "?" followed by the query string
 946    *                             will be appended
 947    * @param p_includeFragment if true (and fragment is not null),
 948    *                             then a "#" followed by the fragment
 949    *                             will be appended
 950    *
 951    * @return the path for this URI possibly including the query string
 952    *         and fragment
 953    */
 954   public String getPath(boolean p_includeQueryString,
 955                         boolean p_includeFragment)
 956   {
 957 
 958     final StringBuilder pathString = new StringBuilder(m_path);
 959 
 960     if (p_includeQueryString && m_queryString != null)
 961     {
 962       pathString.append('?');
 963       pathString.append(m_queryString);
 964     }
 965 
 966     if (p_includeFragment && m_fragment != null)
 967     {
 968       pathString.append('#');
 969       pathString.append(m_fragment);
 970     }
 971 
 972     return pathString.toString();
 973   }
 974 
 975   /**
 976    * Get the path for this URI. Note that the value returned is the path
 977    * only and does not include the query string or fragment.
 978    *


1304         Utils.messages.createMessage(MsgKey.ER_FRAG_WHEN_PATH_NULL, null)); //"Fragment cannot be set when path is null!");
1305     }
1306     else if (!isURIString(p_fragment))
1307     {
1308       throw new MalformedURIException(Utils.messages.createMessage(MsgKey.ER_FRAG_INVALID_CHAR, null)); //"Fragment contains invalid character!");
1309     }
1310     else
1311     {
1312       m_fragment = p_fragment;
1313     }
1314   }
1315 
1316   /**
1317    * Determines if the passed-in Object is equivalent to this URI.
1318    *
1319    * @param p_test the Object to test for equality.
1320    *
1321    * @return true if p_test is a URI with all values equal to this
1322    *         URI, false otherwise
1323    */
1324   @Override
1325   public boolean equals(Object p_test)
1326   {
1327 
1328     if (p_test instanceof URI)
1329     {
1330       URI testURI = (URI) p_test;
1331 
1332       if (((m_scheme == null && testURI.m_scheme == null) || (m_scheme != null && testURI.m_scheme != null && m_scheme.equals(
1333               testURI.m_scheme))) && ((m_userinfo == null && testURI.m_userinfo == null) || (m_userinfo != null && testURI.m_userinfo != null && m_userinfo.equals(
1334               testURI.m_userinfo))) && ((m_host == null && testURI.m_host == null) || (m_host != null && testURI.m_host != null && m_host.equals(
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(
1336               testURI.m_path))) && ((m_queryString == null && testURI.m_queryString == null) || (m_queryString != null && testURI.m_queryString != null && m_queryString.equals(
1337               testURI.m_queryString))) && ((m_fragment == null && testURI.m_fragment == null) || (m_fragment != null && testURI.m_fragment != null && m_fragment.equals(
1338               testURI.m_fragment))))
1339       {
1340         return true;
1341       }
1342     }
1343 
1344     return false;
1345   }
1346 
1347   @Override
1348   public int hashCode() {
1349     int hash = 5;
1350     hash = 41 * hash + Objects.hashCode(this.m_scheme);
1351     hash = 41 * hash + Objects.hashCode(this.m_userinfo);
1352     hash = 41 * hash + Objects.hashCode(this.m_host);
1353     hash = 41 * hash + this.m_port;
1354     hash = 41 * hash + Objects.hashCode(this.m_path);
1355     hash = 41 * hash + Objects.hashCode(this.m_queryString);
1356     hash = 41 * hash + Objects.hashCode(this.m_fragment);
1357     return hash;
1358   }
1359 
1360   /**
1361    * Get the URI as a string specification. See RFC 2396 Section 5.2.
1362    *
1363    * @return the URI string specification
1364    */
1365   @Override
1366   public String toString()
1367   {
1368 
1369     final StringBuilder uriSpecString = new StringBuilder();
1370 
1371     if (m_scheme != null)
1372     {
1373       uriSpecString.append(m_scheme);
1374       uriSpecString.append(':');
1375     }
1376 
1377     uriSpecString.append(getSchemeSpecificPart());
1378 
1379     return uriSpecString.toString();
1380   }
1381 
1382   /**
1383    * Get the indicator as to whether this URI uses the "generic URI"
1384    * syntax.
1385    *
1386    * @return true if this URI uses the "generic URI" syntax, false
1387    *         otherwise
1388    */
1389   public boolean isGenericURI()


1541     return true;
1542   }
1543 
1544   /**
1545    * Determine whether a char is a digit.
1546    *
1547    *
1548    * @param p_char the character to check
1549    * @return true if the char is betweeen '0' and '9', false otherwise
1550    */
1551   private static boolean isDigit(char p_char)
1552   {
1553     return p_char >= '0' && p_char <= '9';
1554   }
1555 
1556   /**
1557    * Determine whether a character is a hexadecimal character.
1558    *
1559    *
1560    * @param p_char the character to check
1561    * @return true if the char is between '0' and '9', 'a' and 'f'
1562    *         or 'A' and 'F', false otherwise
1563    */
1564   private static boolean isHex(char p_char)
1565   {
1566     return (isDigit(p_char) || (p_char >= 'a' && p_char <= 'f')
1567             || (p_char >= 'A' && p_char <= 'F'));
1568   }
1569 
1570   /**
1571    * Determine whether a char is an alphabetic character: a-z or A-Z
1572    *
1573    *
1574    * @param p_char the character to check
1575    * @return true if the char is alphabetic, false otherwise
1576    */
1577   private static boolean isAlpha(char p_char)
1578   {
1579     return ((p_char >= 'a' && p_char <= 'z')
1580             || (p_char >= 'A' && p_char <= 'Z'));
1581   }