jericho     02/05/19 11:12:16

  Modified:    src/util/org/apache/util URI.java
  Log:
  - Add a URI constructor as an escaped form.
    And in order to support it, change the arguments and parsing routine of
    the parseUriReference and parseAuthoirty methods.
    It will need to have to support the communications part of a URI.
  - Add the basename methods like getRawName, getEscapedName, getName methods.
  - Fix typo...
  
  Revision  Changes    Path
  1.7       +115 -37   jakarta-slide/src/util/org/apache/util/URI.java
  
  Index: URI.java
  ===================================================================
  RCS file: /home/cvs/jakarta-slide/src/util/org/apache/util/URI.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- URI.java  16 May 2002 04:57:42 -0000      1.6
  +++ URI.java  19 May 2002 18:12:16 -0000      1.7
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-slide/src/util/org/apache/util/URI.java,v 1.6 
2002/05/16 04:57:42 jericho Exp $
  - * $Revision: 1.6 $
  - * $Date: 2002/05/16 04:57:42 $
  + * $Header: /home/cvs/jakarta-slide/src/util/org/apache/util/URI.java,v 1.7 
2002/05/19 18:12:16 jericho Exp $
  + * $Revision: 1.7 $
  + * $Date: 2002/05/19 18:12:16 $
    *
    * ====================================================================
    *
  @@ -128,7 +128,7 @@
    * relative URL(RFC 1808).
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Sung-Gu</a>
  - * @version $Revision: 1.6 $ $Date: 2002/03/14 15:14:01 
  + * @version $Revision: 1.7 $ $Date: 2002/03/14 15:14:01 
    */
   
   public class URI implements Comparable, java.io.Serializable {
  @@ -140,6 +140,18 @@
       }
   
       /**
  +     * Construct a URI as an escaped form of a character array.
  +     *
  +     * @param escaped the URI character sequence
  +     * @throws NullPointerException if <code>escaped</code> is <code>null</code>
  +     * @exception Exception
  +     */
  +    public URI(char[] escaped) throws Exception {
  +        parseUriReference(new String(escaped), true);
  +    }
  +
  +
  +    /**
        * Construct a URI from the given string.
        * <p><blockquote><pre>
        *   URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
  @@ -150,7 +162,7 @@
        * @exception Exception
        */
       public URI(String original) throws Exception {
  -        parseUriReference(original);
  +        parseUriReference(original, false);
       }
   
   
  @@ -230,7 +242,7 @@
               buff.append('#');
               buff.append(fragment);
           }
  -        parseUriReference(buff.toString());
  +        parseUriReference(buff.toString(), false);
       }
   
   
  @@ -333,6 +345,11 @@
               this.setUriReference();
               return;
           }
  +        if (relative._is_only_fragment) {
  +            this._fragment = relative._fragment;
  +            this.setUriReference();
  +            return;
  +        }
           if (relative._scheme != null) {
               this._scheme = relative._scheme;
               this._is_net_path = relative._is_net_path;
  @@ -1543,10 +1560,12 @@
        * </pre></blockquote><p>
        *
        * @param original the original character sequence
  +     * @param escaped <code>true</code> if <code>original</code> is escaped
        * @return the original character sequence
        * @exception Exception
        */
  -    protected void parseUriReference(String original) throws Exception {
  +    protected void parseUriReference(String original, boolean escaped)
  +    throws Exception {
           // validate and contruct the URI character sequence
           
           if (original == null) {
  @@ -1562,7 +1581,7 @@
            * Consider of the character encoding of the document.
            * The platform's charset is used for the document by default.
            */
  -        if (_documentCharset != null) {
  +        if (_documentCharset != null && !escaped) {
               tmp = new String(tmp.getBytes(_documentCharset), _documentCharset);
           }
   
  @@ -1599,6 +1618,8 @@
               char[] target = tmp.substring(0, at).toLowerCase().toCharArray();
               if (validate(target, scheme)) {
                   _scheme = target;
  +            } else {
  +                throw new IllegalArgumentException("incorrect scheme");
               }
               from = ++at;
           }
  @@ -1622,7 +1643,7 @@
                       next = (tmp.substring(at + 2).length() == 0) ? at + 2 :
                       tmp.length();
                   }
  -                parseAuthority(tmp.substring(at + 2, next));
  +                parseAuthority(tmp.substring(at + 2, next), escaped);
                   from = at = next;
                   // Set flag
                   _is_net_path = true;
  @@ -1647,18 +1668,23 @@
               if (next == -1) {
                   next = tmp.length();
               }
  -            if (prevalidate(tmp.substring(from, next),
  -                        disallowed_rel_path)) {
  -                // Set flag
  -                _is_rel_path = true;
  -            } else if (prevalidate(tmp.substring(from, next),
  -                        disallowed_opaque_part)) {
  -                // validating before escape encoding // is_absoluteURI
  -                // Set flag
  -                _is_opaque_part = true;
  -            } else {
  -                // the path component is never undefined, though it may be empty
  -                _path = new char[] {'\0'};
  +            if (!_is_abs_path) {
  +                if (!escaped && prevalidate(tmp.substring(from, next),
  +                            disallowed_rel_path) || escaped &&
  +                        validate(tmp.substring(from, next).toCharArray(),
  +                            rel_path)) {
  +                    // Set flag
  +                    _is_rel_path = true;
  +                } else if (!escaped && prevalidate(tmp.substring(from, next),
  +                            disallowed_opaque_part) || escaped &&
  +                        validate(tmp.substring(from, next).toCharArray(),
  +                            opaque_part)) {
  +                    // Set flag
  +                    _is_opaque_part = true;
  +                } else {
  +                    // the path component may be empty
  +                    _path = new char[] {'\0'};
  +                }
               }
               setPath(tmp.substring(from, next));
               at = next;
  @@ -1676,7 +1702,8 @@
               if (next == -1) {
                   next = tmp.length();
               }
  -            _query = encode(tmp.substring(at + 1, next), allowed_query);
  +            _query = (escaped) ? tmp.substring(at + 1, next).toCharArray() :
  +            encode(tmp.substring(at + 1, next), allowed_query);
               at = next;
           }
   
  @@ -1688,7 +1715,8 @@
            * </pre></blockquote><p>
            */
           if (0 <= at && at+1 < length && tmp.charAt(at) == '#') {
  -            _fragment = encode(tmp.substring(at + 1), allowed_fragment);
  +            _fragment = (escaped) ? tmp.substring(at + 1).toCharArray() :
  +            encode(tmp.substring(at + 1), allowed_fragment);
               if (!_is_abs_path && !_is_rel_path && !_is_opaque_part) {
                   // correct validation.  possibly, only fragment.
                   // is_relativeURI and is_absoluteURI must be false
  @@ -1794,11 +1822,13 @@
        * Parse the authority component.
        *
        * @param original the original character sequence of authority component
  +     * @param escaped <code>true</code> if <code>original</code> is escaped
        * @exception Exception
        * if NumberFormatException, port isn't integer
        * if IllegalArgumentException, incorrect Pv6reference or wrong host
        */
  -    protected void parseAuthority(String original) throws Exception {
  +    protected void parseAuthority(String original, boolean escaped)
  +    throws Exception {
           // Reset flags
           _is_reg_name = _is_server =
           _is_hostname = _is_IPv4address = _is_IPv6reference = false;
  @@ -1806,9 +1836,9 @@
           int from = 0;
           int next = original.indexOf('@');
           if (next != -1) { // neither -1 and 0
  -            // if next == 0, for example, in ftp, userinfo = 'anonymous'
               // each protocol extented from URI supports the specific userinfo
  -            _userinfo = encode(original.substring(0, next), allowed_userinfo);
  +            _userinfo = (escaped) ? original.substring(0, next).toCharArray() :
  +            encode(original.substring(0, next), allowed_userinfo);
               from = next + 1;
           }
           next = original.indexOf('[', from);
  @@ -1820,8 +1850,8 @@
                   next++;
               }
               // In IPv6reference, '[', ']' should be excluded
  -            _host = encode(original.substring(from, next),
  -                    allowed_IPv6reference);
  +            _host = (escaped) ? original.substring(from, next).toCharArray() :
  +            encode(original.substring(from, next), allowed_IPv6reference);
               // Set flag
               _is_IPv6reference = true;
           } else { // only for !_is_IPv6reference
  @@ -1847,7 +1877,8 @@
               _is_server = _is_hostname = _is_IPv4address =
               _is_IPv6reference = false;
               // set a registry-based naming authority
  -            _authority = encode(original.toString(), allowed_reg_name);
  +            _authority = (escaped) ? original.toString().toCharArray() :
  +            encode(original.toString(), allowed_reg_name);
           } else {
               next = original.indexOf('/', from);
               if (next > 0 && original.charAt(next) == ':') { // not empty
  @@ -2172,7 +2203,7 @@
        * UnsupportedEncodingException
        */
       public void setAuthority(String authority) throws Exception {
  -        parseAuthority(authority);
  +        parseAuthority(authority, false);
           setUriReference();
       }
   
  @@ -2482,12 +2513,59 @@
        * </pre></blockquote><p>
        * @return the path string
        * @exception Exception
  -     * @decode
  +     * @see decode
        */
       public String getPath() throws Exception {
           return decode(getRawPath());
       }
   
  +
  +    /**
  +     * Get the raw-escaped basename of the path.
  +     *
  +     * @return the raw-escaped basename
  +     */
  +    public char[] getRawName() {
  +        if (_path == null || _path.length == 0) {
  +            return _path;
  +        }
  +        int at = 0;
  +        for (int i = _path.length - 1; i >= 0 ; i--) {
  +            if (_path[i] == '/') {
  +                at = i;
  +                break;
  +            }
  +        }
  +        int len = _path.length - at;
  +        char[] basename =  new char[len];
  +        System.arraycopy(_path, at + 1, basename, 0, len);
  +        return basename;
  +    }
  +
  +
  +    /**
  +     * Get the escaped basename of the path.
  +     *
  +     * @return the escaped basename string
  +     */
  +    public String getEscapedName() {
  +        char[] basename = getRawName();
  +        return (basename == null) ? null : new String(basename);
  +    }
  +
  +
  +    /**
  +     * Get the basename of the path.
  +     *
  +     * @return the basename string
  +     * @exception Exception
  +     * @see decode
  +     */
  +    public String getName() throws Exception {
  +        char[] basename = getRawName();
  +        return (basename == null) ? null : decode(getRawName());
  +    }
  +
       // ----------------------------------------------------- The path and query 
   
       /**
  @@ -2530,7 +2608,7 @@
        *
        * @return the path and query string.
        * @exception Exception
  -     * @decode
  +     * @see decode
        */
       public String getPathQuery() throws Exception {
           return decode(getRawPathQuery());
  @@ -2543,7 +2621,7 @@
        *
        * @param the query string.
        * @exception Exception
  -     * @encode
  +     * @see encode
        */
       public void setQuery(String query) throws Exception {
           _query = encode(query, allowed_query);
  @@ -2576,7 +2654,7 @@
        *
        * @return the query string.
        * @exception Exception
  -     * @decode
  +     * @see decode
        */
       public String getQuery() throws Exception {
           return decode(_query);
  @@ -2635,7 +2713,7 @@
        *
        * @return the fragment string
        * @exception Exception
  -     * @decode
  +     * @see decode
        */
       public String getFragment() throws Exception {
           return decode(_fragment);
  @@ -2848,7 +2926,7 @@
        * public.
        *
        * When you want to get each part of the userinfo, you need to use the
  -     * specific methods in the specific URL. It's depends on the specific URL.
  +     * specific methods in the specific URL. It depends on the specific URL.
        *
        * @return URI character sequence
        */
  @@ -2873,7 +2951,7 @@
        *
        * @return the URI string
        * @exception Exception
  -     * @decode
  +     * @see decode
        */
       public String getURI() throws Exception {
           return decode(_uri);
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to