stephan     2003/01/30 11:57:47

  Modified:    src/java/org/apache/cocoon/components/source SourceUtil.java
  Log:
  Add methods to exract the specifing parts of an URI, using
  a regex pattern.
  
  Revision  Changes    Path
  1.18      +140 -1    
xml-cocoon2/src/java/org/apache/cocoon/components/source/SourceUtil.java
  
  Index: SourceUtil.java
  ===================================================================
  RCS file: 
/home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/source/SourceUtil.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- SourceUtil.java   30 Jan 2003 10:59:23 -0000      1.17
  +++ SourceUtil.java   30 Jan 2003 19:57:47 -0000      1.18
  @@ -71,6 +71,10 @@
   import org.apache.excalibur.source.SourceParameters;
   import org.apache.excalibur.source.SourceResolver;
   import org.apache.excalibur.xmlizer.XMLizer;
  +import org.apache.regexp.RE;
  +import org.apache.regexp.RECompiler;
  +import org.apache.regexp.REProgram;
  +import org.apache.regexp.RESyntaxException;
   import org.w3c.dom.Document;
   import org.w3c.dom.DocumentFragment;
   import org.xml.sax.ContentHandler;
  @@ -87,6 +91,16 @@
    */
   public final class SourceUtil {
   
  +    private static REProgram uripattern = null;
  +
  +    static {
  +        try {
  +            uripattern = (new 
RECompiler()).compile("^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?$");
  +        } catch (RESyntaxException rese) {
  +            rese.printStackTrace();
  +        }
  +    }
  +
       /** Avoid instantiation */
       protected SourceUtil() {
       }
  @@ -524,6 +538,131 @@
               throw new ProcessingException(ce);
           } finally {
               resolver.release(source);
  +        }
  +    }
  +
  +    /**
  +     * Return the scheme of a URI. Just as there are many different methods 
  +     * of access to resources, there are a variety of schemes for identifying 
  +     * such resources.
  +     * (see <a href="http://www.ietf.org/rfc/rfc2396.txt";>RFC 2396</a>).
  +     *
  +     * @param uri Uniform resource identifier.
  +     *
  +     * @return Scheme of the URI.
  +     */
  +    public static String getScheme(String uri) {
  +        RE re = new RE(uripattern);
  +
  +        if (re.match(uri)) {
  +            return re.getParen(2);
  +        } else {
  +            throw new IllegalArgumentException("'"+uri+
  +                                               "' is not a correct URI");
  +        }
  +    }
  +
  +    /**
  +     * Return the authority of a URI. This authority is
  +     * typically defined by an Internet-based server or a scheme-specific
  +     * registry of naming authorities
  +     * (see <a href="http://www.ietf.org/rfc/rfc2396.txt";>RFC 2396</a>).
  +     *
  +     * @param uri Uniform resource identifier.
  +     *
  +     * @return Scheme of the URI.
  +     */
  +    public static String getAuthority(String uri) {
  +        RE re = new RE(uripattern);
  +
  +        if (re.match(uri)) {
  +            return re.getParen(4);
  +        } else {
  +            throw new IllegalArgumentException("'"+uri+
  +                                               "' is not a correct URI");
  +        }
  +    }
  +
  +    /**
  +     * Return the path of a URI. The path contains data, specific to the 
  +     * authority (or the scheme if there is no authority component), 
  +     * identifying the resource within the scope of that scheme and authority
  +     * (see <a href="http://www.ietf.org/rfc/rfc2396.txt";>RFC 2396</a>).
  +     *
  +     * @param uri Uniform resource identifier.
  +     *
  +     * @return Path of the URI.
  +     */
  +    public static String getPath(String uri) {
  +        RE re = new RE(uripattern);
  +
  +        if (re.match(uri)) {
  +            return re.getParen(5);
  +        } else {
  +            throw new IllegalArgumentException("'"+uri+
  +                                               "' is not a correct URI");
  +        }
  +    }
  +
  +    /**
  +     * Return the path of a URI, if the URI can't contains a authority. 
  +     * This implementation differ to the RFC 2396.
  +     *
  +     * @param uri Uniform resource identifier.
  +     *
  +     * @return Path of the URI.
  +     */
  +    public static String getPathWithoutAuthority(String uri) {
  +        RE re = new RE(uripattern);
  +
  +        if (re.match(uri)) {
  +            return re.getParen(4)+re.getParen(5);
  +        } else {
  +            throw new IllegalArgumentException("'"+uri+
  +                                               "' is not a correct URI");
  +        }
  +    }
  +
  +    /**
  +     * Return the query of a URI. The query is a string of information to 
  +     * be interpreted by the resource
  +     * (see <a href="http://www.ietf.org/rfc/rfc2396.txt";>RFC 2396</a>).
  +     *
  +     * @param uri Uniform resource identifier.
  +     *
  +     * @return Query of the URI.
  +     */
  +    public static String getQuery(String uri) {
  +        RE re = new RE(uripattern);
  +
  +        if (re.match(uri)) {
  +            return re.getParen(7);
  +        } else {
  +            throw new IllegalArgumentException("'"+uri+
  +                                               "' is not a correct URI");
  +        }
  +    }
  +
  +    /**
  +     * Return the fragment of a URI. When a URI reference is used to perform 
  +     * a retrieval action on the identified resource, the optional fragment 
  +     * identifier, consists of additional reference information to be 
  +     * interpreted by the user agent after the retrieval action has been 
  +     * successfully completed
  +     * (see <a href="http://www.ietf.org/rfc/rfc2396.txt";>RFC 2396</a>).
  +     *
  +     * @param uri Uniform resource identifier.
  +     *
  +     * @return Fragment of the URI.
  +     */
  +    public static String getFragment(String uri) {
  +        RE re = new RE(uripattern);
  +
  +        if (re.match(uri)) {
  +            return re.getParen(9);
  +        } else {
  +            throw new IllegalArgumentException("'"+uri+
  +                                               "' is not a correct URI");
           }
       }
   }
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     [EMAIL PROTECTED]
To unsubscribe, e-mail:          [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to