You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by cz...@apache.org on 2002/04/22 12:06:05 UTC

cvs commit: jakarta-avalon-excalibur/xmlbundle/src/java/org/apache/avalon/excalibur/xml/xpath JaxenProcessorImpl.java XPathProcessor.java XPathProcessorImpl.java package.html

cziegeler    02/04/22 03:06:05

  Modified:    .        build.xml
               xmlbundle build.xml default.properties
  Added:       xmlbundle/src/java/org/apache/avalon/excalibur/xml
                        AbstractXMLConsumer.java ContentHandlerWrapper.java
                        EntityResolver.java JaxpParser.java Parser.java
                        XMLConsumer.java XMLFragment.java XMLizable.java
                        XercesParser.java
               xmlbundle/src/java/org/apache/avalon/excalibur/xml/xpath
                        JaxenProcessorImpl.java XPathProcessor.java
                        XPathProcessorImpl.java package.html
  Removed:     all/src/java/org/apache/avalon/excalibur/xml
                        AbstractXMLConsumer.java ContentHandlerWrapper.java
                        EntityResolver.java JaxpParser.java Parser.java
                        XMLConsumer.java XMLFragment.java XMLizable.java
                        XercesParser.java
               all/src/java/org/apache/avalon/excalibur/xml/xpath
                        JaxenProcessorImpl.java XPathProcessor.java
                        XPathProcessorImpl.java package.html
               xmlbundle/src/java dummy.txt
  Log:
  Moved xmlbundle
  
  Revision  Changes    Path
  1.157     +6 -0      jakarta-avalon-excalibur/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/build.xml,v
  retrieving revision 1.156
  retrieving revision 1.157
  diff -u -r1.156 -r1.157
  --- build.xml	22 Apr 2002 09:36:14 -0000	1.156
  +++ build.xml	22 Apr 2002 10:06:03 -0000	1.157
  @@ -24,6 +24,7 @@
           <ant dir="logger" target="dist"/>
           <ant dir="threadcontext" target="dist"/>
           <ant dir="sourceresolve" target="dist"/>
  +        <ant dir="xmlbundle" target="dist"/>
           
           <!-- Sub projects with dependencies. -->
           <ant dir="pool" target="dist"/>               <!-- collections, concurrent, instrument -->
  @@ -89,6 +90,8 @@
               <pathelement location="sourceresolve/src/test"/>
               <pathelement location="tar/src/java"/>
               <pathelement location="tar/src/test"/>
  +            <pathelement location="xmlbundle/src/java"/>
  +            <pathelement location="xmlbundle/src/test"/>
               <pathelement location="zip/src/java"/>
               <pathelement location="zip/src/test"/>
           </path>
  @@ -138,6 +141,7 @@
       <target name="jar" description="Generates avalon-excalibur.jar">
           <ant dir="datasource" target="jar"/>
           <ant dir="sourceresolve" target="jar"/>
  +        <ant dir="xmlbundle" target="jar"/>
           <ant dir="all" target="all"/>
           <ant dir="cli" target="jar"/>
           <ant dir="component" target="jar"/>
  @@ -169,6 +173,7 @@
                       <include name="util/build/lib/*.jar"/>
                       <include name="instrument/build/lib/*.jar"/>
                       <include name="sourceresolve/build/lib/*.jar"/>
  +                    <include name="xmlbundle/build/lib/*.jar"/>
                   </fileset>
               </mergefiles>
           </jlink>
  @@ -189,6 +194,7 @@
           <ant dir="logger" target="jar"/>
           <ant dir="threadcontext" target="jar"/>
           <ant dir="sourceresolve" target="jar"/>
  +        <ant dir="xmlbundle" target="jar"/>
           
           <!-- Sub projects with dependencies. -->
           <ant dir="pool" target="jar"/>               <!-- collections, concurrent, instrument, sourceresolve -->
  
  
  
  1.13      +32 -4     jakarta-avalon-excalibur/xmlbundle/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/xmlbundle/build.xml,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- build.xml	22 Apr 2002 09:36:14 -0000	1.12
  +++ build.xml	22 Apr 2002 10:06:04 -0000	1.13
  @@ -15,8 +15,10 @@
           <pathelement location="${build.classes}"/>
           <pathelement location="${avalon-framework.jar}"/>
           <pathelement location="${excalibur-pool.jar}"/>
  -        <pathelement location="${excalibur-core.jar}"/>
           <pathelement location="${checkstyle.jar}"/>
  +        <pathelement location="${xalan.jar}"/>
  +        <pathelement location="${xerces.jar}"/>
  +        <pathelement location="${xml-apis.jar}"/>
       </path>
   
       <path id="tools.class.path">
  @@ -36,11 +38,33 @@
       <target name="main" depends="jar" description="Build the project"/>
       <target name="rebuild" depends="clean,main" description="Rebuild the project"/>
   
  +    <!-- Checks the environment for existing resources -->
  +    <target name="check-environment">
  +        <available property="xpath.present" classname="org.apache.xpath.XPathAPI">
  +            <classpath refid="project.class.path"/>
  +        </available>
  +        <available property="jaxen.present" classname="org.jaxen.dom.XPath">
  +            <classpath refid="project.class.path"/>
  +        </available>
  +    </target>
  +
       <target name="dependencies" description="Check dependencies" unless="skip.dependencies">
           <ant antfile="${depchecker.prefix}/depchecker.xml" target="checkCommon"/>
           <ant antfile="${depchecker.prefix}/depchecker.xml" target="checkFramework"/>
           <ant antfile="${depchecker.prefix}/depchecker.xml" target="checkPool"/>
  -        <ant antfile="${depchecker.prefix}/depchecker.xml" target="checkCore"/>
  +        <ant antfile="${depchecker.prefix}/depchecker.xml" target="checkRequiredFile">
  +            <property name="name" value="xerces.jar"/>
  +            <property name="path" value="${xerces.jar}"/>
  +        </ant>
  +        <ant antfile="${depchecker.prefix}/depchecker.xml" target="checkRequiredFile">
  +            <property name="name" value="xalan.jar"/>
  +            <property name="path" value="${xalan.jar}"/>
  +        </ant>
  +        <ant antfile="${depchecker.prefix}/depchecker.xml" target="checkRequiredClass">
  +            <property name="name" value="xml-apis.jar"/>
  +            <property name="class" value="javax.xml.parsers.SAXParser"/>
  +            <property name="classpath" value="${cp}"/>
  +        </ant>
       </target>
   
       <target name="dependencies-test" depends="dist-jar, dependencies"
  @@ -52,7 +76,7 @@
   
   
       <!-- Compiles the source code -->
  -    <target name="compile" depends="dependencies" description="Compiles the source code">
  +    <target name="compile" depends="dependencies, check-environment" description="Compiles the source code">
   
           <mkdir dir="${build.classes}"/>
   
  @@ -64,7 +88,11 @@
               deprecation="${build.deprecation}"
               target="1.2">
               <classpath refid="project.class.path" />
  -            <include name="**/*.java"/>
  +            <exclude name="**/test/**"/>
  +            <exclude name="org/apache/avalon/excalibur/xml/xpath/XPathProcessorImpl.java"
  +                unless="xpath.present"/> 
  +            <exclude name="org/apache/avalon/excalibur/xml/xpath/JaxenProcessorImpl.java"
  +                unless="jaxen.present"/>
           </javac>
   
           <!-- copy resources to same location as .class files -->
  
  
  
  1.6       +5 -5      jakarta-avalon-excalibur/xmlbundle/default.properties
  
  Index: default.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/xmlbundle/default.properties,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- default.properties	22 Apr 2002 09:36:14 -0000	1.5
  +++ default.properties	22 Apr 2002 10:06:04 -0000	1.6
  @@ -25,12 +25,12 @@
   excalibur-pool.lib=${excalibur-pool.home}
   excalibur-pool.jar=${excalibur-pool.lib}/excalibur-pool-1.0.jar
   
  -# ----- Excalibur Core -----
  -excalibur-core.home=${basedir}/../all
  -excalibur-core.lib=${excalibur-core.home}/build/lib
  -excalibur-core.jar=${excalibur-core.lib}/excalibur-core.jar
  -
   # --------------------------------------------------
  +tools.dir=${basedir}/../../jakarta-avalon/tools
  +
  +xml-apis.jar = ${tools.dir}/lib/xml-apis.jar
  +xalan.jar = ${tools.dir}/lib/xalan-2.2.0-D13.jar
  +xerces.jar=${tools.dir}/lib/xerces-1.4.4.jar
   
   #  Settings used to configure compile environment
   build.debug = on
  
  
  
  1.1                  jakarta-avalon-excalibur/xmlbundle/src/java/org/apache/avalon/excalibur/xml/AbstractXMLConsumer.java
  
  Index: AbstractXMLConsumer.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.excalibur.xml;
  
  import org.apache.avalon.framework.logger.AbstractLogEnabled;
  import org.xml.sax.Attributes;
  import org.xml.sax.Locator;
  import org.xml.sax.SAXException;
  
  /**
   * This abstract class provides default implementation of the methods specified
   * by the <code>XMLConsumer</code> interface.
   *
   * @author <a href="mailto:fumagalli@exoffice.com">Pierpaolo Fumagalli</a>
   *         (Apache Software Foundation, Exoffice Technologies)
   * @version CVS $Revision: 1.1 $ $Date: 2002/04/22 10:06:04 $
   */
  public abstract class AbstractXMLConsumer
      extends AbstractLogEnabled
      implements XMLConsumer
  {
  
      /**
       * Receive an object for locating the origin of SAX document events.
       *
       * @param locator An object that can return the location of any SAX
       *                document event.
       */
      public void setDocumentLocator( Locator locator )
      {
      }
  
      /**
       * Receive notification of the beginning of a document.
       */
      public void startDocument()
          throws SAXException
      {
      }
  
      /**
       * Receive notification of the end of a document.
       */
      public void endDocument()
          throws SAXException
      {
      }
  
      /**
       * Begin the scope of a prefix-URI Namespace mapping.
       *
       * @param prefix The Namespace prefix being declared.
       * @param uri The Namespace URI the prefix is mapped to.
       */
      public void startPrefixMapping( String prefix, String uri )
          throws SAXException
      {
      }
  
      /**
       * End the scope of a prefix-URI mapping.
       *
       * @param prefix The prefix that was being mapping.
       */
      public void endPrefixMapping( String prefix )
          throws SAXException
      {
      }
  
      /**
       * Receive notification of the beginning of an element.
       *
       * @param uri The Namespace URI, or the empty string if the element has no
       *            Namespace URI or if Namespace
       *            processing is not being performed.
       * @param loc The local name (without prefix), or the empty string if
       *            Namespace processing is not being performed.
       * @param raw The raw XML 1.0 name (with prefix), or the empty string if
       *            raw names are not available.
       * @param a The attributes attached to the element. If there are no
       *          attributes, it shall be an empty Attributes object.
       */
      public void startElement( String uri, String loc, String raw, Attributes a )
          throws SAXException
      {
      }
  
      /**
       * Receive notification of the end of an element.
       *
       * @param uri The Namespace URI, or the empty string if the element has no
       *            Namespace URI or if Namespace
       *            processing is not being performed.
       * @param loc The local name (without prefix), or the empty string if
       *            Namespace processing is not being performed.
       * @param raw The raw XML 1.0 name (with prefix), or the empty string if
       *            raw names are not available.
       */
      public void endElement( String uri, String loc, String raw )
          throws SAXException
      {
      }
  
      /**
       * Receive notification of character data.
       *
       * @param ch The characters from the XML document.
       * @param start The start position in the array.
       * @param len The number of characters to read from the array.
       */
      public void characters( char ch[], int start, int len )
          throws SAXException
      {
      }
  
      /**
       * Receive notification of ignorable whitespace in element content.
       *
       * @param ch The characters from the XML document.
       * @param start The start position in the array.
       * @param len The number of characters to read from the array.
       */
      public void ignorableWhitespace( char ch[], int start, int len )
          throws SAXException
      {
      }
  
      /**
       * Receive notification of a processing instruction.
       *
       * @param target The processing instruction target.
       * @param data The processing instruction data, or null if none was
       *             supplied.
       */
      public void processingInstruction( String target, String data )
          throws SAXException
      {
      }
  
      /**
       * Receive notification of a skipped entity.
       *
       * @param name The name of the skipped entity.  If it is a  parameter
       *             entity, the name will begin with '%'.
       */
      public void skippedEntity( String name )
          throws SAXException
      {
      }
  
      /**
       * Report the start of DTD declarations, if any.
       *
       * @param name The document type name.
       * @param publicId The declared public identifier for the external DTD
       *                 subset, or null if none was declared.
       * @param systemId The declared system identifier for the external DTD
       *                 subset, or null if none was declared.
       */
      public void startDTD( String name, String publicId, String systemId )
          throws SAXException
      {
      }
  
      /**
       * Report the end of DTD declarations.
       */
      public void endDTD()
          throws SAXException
      {
      }
  
      /**
       * Report the beginning of an entity.
       *
       * @param name The name of the entity. If it is a parameter entity, the
       *             name will begin with '%'.
       */
      public void startEntity( String name )
          throws SAXException
      {
      }
  
      /**
       * Report the end of an entity.
       *
       * @param name The name of the entity that is ending.
       */
      public void endEntity( String name )
          throws SAXException
      {
      }
  
      /**
       * Report the start of a CDATA section.
       */
      public void startCDATA()
          throws SAXException
      {
      }
  
      /**
       * Report the end of a CDATA section.
       */
      public void endCDATA()
          throws SAXException
      {
      }
  
      /**
       * Report an XML comment anywhere in the document.
       *
       * @param ch An array holding the characters in the comment.
       * @param start The starting position in the array.
       * @param len The number of characters to use from the array.
       */
      public void comment( char ch[], int start, int len )
          throws SAXException
      {
      }
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/xmlbundle/src/java/org/apache/avalon/excalibur/xml/ContentHandlerWrapper.java
  
  Index: ContentHandlerWrapper.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.excalibur.xml;
  
  import org.apache.avalon.excalibur.pool.Recyclable;
  import org.xml.sax.Attributes;
  import org.xml.sax.ContentHandler;
  import org.xml.sax.Locator;
  import org.xml.sax.SAXException;
  import org.xml.sax.ext.LexicalHandler;
  
  /**
   * This class is an utility class &quot;wrapping&quot; around a SAX version 2.0
   * <code>ContentHandler</code> and forwarding it those events received throug
   * its <code>XMLConsumers</code> interface.
   * <br>
   *
   * @author <a href="mailto:dims@yahoo.com">Davanum Srinivas</a>
   *         (Apache Software Foundation, Computer Associates)
   * @version CVS $Revision: 1.1 $ $Date: 2002/04/22 10:06:04 $
   */
  public class ContentHandlerWrapper
      extends AbstractXMLConsumer
      implements Recyclable
  {
  
      /** The current <code>ContentHandler</code>. */
      protected ContentHandler contentHandler;
  
      /** The optional <code>LexicalHandler</code> */
      protected LexicalHandler lexicalHandler;
  
      /**
       * Create a new <code>ContentHandlerWrapper</code> instance.
       */
      public ContentHandlerWrapper()
      {
          super();
      }
  
      /**
       * Create a new <code>ContentHandlerWrapper</code> instance.
       */
      public ContentHandlerWrapper( ContentHandler contentHandler )
      {
          this();
          this.setContentHandler( contentHandler );
      }
  
      /**
       * Create a new <code>ContentHandlerWrapper</code> instance.
       */
      public ContentHandlerWrapper( ContentHandler contentHandler,
                                    LexicalHandler lexicalHandler )
      {
          this();
          this.setContentHandler( contentHandler );
          this.setLexicalHandler( lexicalHandler );
      }
  
      /**
       * Set the <code>ContentHandler</code> that will receive XML data.
       *
       * @exception IllegalStateException If the <code>ContentHandler</code>
       *                                  was already set.
       */
      public void setContentHandler( ContentHandler contentHandler )
          throws IllegalStateException
      {
          if( this.contentHandler != null ) throw new IllegalStateException();
          this.contentHandler = contentHandler;
      }
  
      /**
       * Set the <code>LexicalHandler</code> that will receive XML data.
       *
       * @exception IllegalStateException If the <code>LexicalHandler</code>
       *                                  was already set.
       */
      public void setLexicalHandler( LexicalHandler lexicalHandler )
          throws IllegalStateException
      {
          if( this.lexicalHandler != null ) throw new IllegalStateException();
          this.lexicalHandler = lexicalHandler;
      }
  
      public void recycle()
      {
          this.contentHandler = null;
          this.lexicalHandler = null;
      }
  
      /**
       * Receive an object for locating the origin of SAX document events.
       */
      public void setDocumentLocator( Locator locator )
      {
          if( this.contentHandler == null )
              return;
          else
              this.contentHandler.setDocumentLocator( locator );
      }
  
      /**
       * Receive notification of the beginning of a document.
       */
      public void startDocument()
          throws SAXException
      {
          if( this.contentHandler == null )
              throw new SAXException( "ContentHandler not set" );
          this.contentHandler.startDocument();
      }
  
      /**
       * Receive notification of the end of a document.
       */
      public void endDocument()
          throws SAXException
      {
          this.contentHandler.endDocument();
      }
  
      /**
       * Begin the scope of a prefix-URI Namespace mapping.
       */
      public void startPrefixMapping( String prefix, String uri )
          throws SAXException
      {
          if( this.contentHandler == null )
              throw new SAXException( "ContentHandler not set" );
          this.contentHandler.startPrefixMapping( prefix, uri );
      }
  
      /**
       * End the scope of a prefix-URI mapping.
       */
      public void endPrefixMapping( String prefix )
          throws SAXException
      {
          this.contentHandler.endPrefixMapping( prefix );
      }
  
      /**
       * Receive notification of the beginning of an element.
       */
      public void startElement( String uri, String loc, String raw, Attributes a )
          throws SAXException
      {
          this.contentHandler.startElement( uri, loc, raw, a );
      }
  
      /**
       * Receive notification of the end of an element.
       */
      public void endElement( String uri, String loc, String raw )
          throws SAXException
      {
          this.contentHandler.endElement( uri, loc, raw );
      }
  
      /**
       * Receive notification of character data.
       */
      public void characters( char ch[], int start, int len )
          throws SAXException
      {
          this.contentHandler.characters( ch, start, len );
      }
  
      /**
       * Receive notification of ignorable whitespace in element content.
       */
      public void ignorableWhitespace( char ch[], int start, int len )
          throws SAXException
      {
          this.contentHandler.ignorableWhitespace( ch, start, len );
      }
  
      /**
       * Receive notification of a processing instruction.
       */
      public void processingInstruction( String target, String data )
          throws SAXException
      {
          this.contentHandler.processingInstruction( target, data );
      }
  
      /**
       * Receive notification of a skipped entity.
       *
       * @param name The name of the skipped entity.  If it is a  parameter
       *             entity, the name will begin with '%'.
       */
      public void skippedEntity( String name )
          throws SAXException
      {
          this.contentHandler.skippedEntity( name );
      }
  
      /**
       * Report the start of DTD declarations, if any.
       *
       * @param name The document type name.
       * @param publicId The declared public identifier for the external DTD
       *                 subset, or null if none was declared.
       * @param systemId The declared system identifier for the external DTD
       *                 subset, or null if none was declared.
       */
      public void startDTD( String name, String publicId, String systemId )
          throws SAXException
      {
          if( this.lexicalHandler != null )
              this.lexicalHandler.startDTD( name, publicId, systemId );
      }
  
      /**
       * Report the end of DTD declarations.
       */
      public void endDTD()
          throws SAXException
      {
          if( this.lexicalHandler != null )
              this.lexicalHandler.endDTD();
      }
  
      /**
       * Report the beginning of an entity.
       *
       * @param name The name of the entity. If it is a parameter entity, the
       *             name will begin with '%'.
       */
      public void startEntity( String name )
          throws SAXException
      {
          if( this.lexicalHandler != null )
              this.lexicalHandler.startEntity( name );
      }
  
      /**
       * Report the end of an entity.
       *
       * @param name The name of the entity that is ending.
       */
      public void endEntity( String name )
          throws SAXException
      {
          if( this.lexicalHandler != null )
              this.lexicalHandler.endEntity( name );
      }
  
      /**
       * Report the start of a CDATA section.
       */
      public void startCDATA()
          throws SAXException
      {
          if( this.lexicalHandler != null )
              this.lexicalHandler.startCDATA();
      }
  
      /**
       * Report the end of a CDATA section.
       */
      public void endCDATA()
          throws SAXException
      {
          if( this.lexicalHandler != null )
              this.lexicalHandler.endCDATA();
      }
  
      /**
       * Report an XML comment anywhere in the document.
       *
       * @param ch An array holding the characters in the comment.
       * @param start The starting position in the array.
       * @param len The number of characters to use from the array.
       */
      public void comment( char ch[], int start, int len )
          throws SAXException
      {
          if( this.lexicalHandler != null )
              this.lexicalHandler.comment( ch, start, len );
      }
  
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/xmlbundle/src/java/org/apache/avalon/excalibur/xml/EntityResolver.java
  
  Index: EntityResolver.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.excalibur.xml;
  
  import java.io.IOException;
  import org.apache.avalon.framework.component.Component;
  import org.xml.sax.InputSource;
  import org.xml.sax.SAXException;
  
  /**
   * A component that uses catalogs for resolving Entities.
   *
   * @author <a href="mailto:dims@yahoo.com">Davanum Srinivas</a>
   * @version CVS $Revision: 1.1 $ $Date: 2002/04/22 10:06:04 $
   */
  public interface EntityResolver
      extends Component, org.xml.sax.EntityResolver
  {
  
      String ROLE = EntityResolver.class.getName();
  
      /**
       * Allow the application to resolve external entities.
       *
       * <p>The Parser will call this method before opening any external
       * entity except the top-level document entity (including the
       * external DTD subset, external entities referenced within the
       * DTD, and external entities referenced within the document
       * element): the application may request that the parser resolve
       * the entity itself, that it use an alternative URI, or that it
       * use an entirely different input source.</p>
       *
       * <p>Application writers can use this method to redirect external
       * system identifiers to secure and/or local URIs, to look up
       * public identifiers in a catalogue, or to read an entity from a
       * database or other input source (including, for example, a dialog
       * box).</p>
       *
       * <p>If the system identifier is a URL, the SAX parser must
       * resolve it fully before reporting it to the application.</p>
       *
       * @param publicId The public identifier of the external entity
       *        being referenced, or null if none was supplied.
       * @param systemId The system identifier of the external entity
       *        being referenced.
       * @return An InputSource object describing the new input source,
       *         or null to request that the parser open a regular
       *         URI connection to the system identifier.
       * @exception org.xml.sax.SAXException Any SAX exception, possibly
       *            wrapping another exception.
       * @exception java.io.IOException A Java-specific IO exception,
       *            possibly the result of creating a new InputStream
       *            or Reader for the InputSource.
       * @see org.xml.sax.InputSource
       */
      InputSource resolveEntity( String publicId,
                                 String systemId )
          throws SAXException, IOException;
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/xmlbundle/src/java/org/apache/avalon/excalibur/xml/JaxpParser.java
  
  Index: JaxpParser.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.excalibur.xml;
  
  import java.io.IOException;
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.parsers.ParserConfigurationException;
  import javax.xml.parsers.SAXParserFactory;
  import org.apache.avalon.excalibur.pool.Poolable;
  import org.apache.avalon.framework.component.ComponentException;
  import org.apache.avalon.framework.component.ComponentManager;
  import org.apache.avalon.framework.component.Composable;
  import org.apache.avalon.framework.logger.AbstractLogEnabled;
  import org.apache.avalon.framework.parameters.ParameterException;
  import org.apache.avalon.framework.parameters.Parameterizable;
  import org.apache.avalon.framework.parameters.Parameters;
  import org.w3c.dom.Document;
  import org.xml.sax.ContentHandler;
  import org.xml.sax.ErrorHandler;
  import org.xml.sax.InputSource;
  import org.xml.sax.SAXException;
  import org.xml.sax.SAXParseException;
  import org.xml.sax.XMLReader;
  import org.xml.sax.ext.LexicalHandler;
  
  /**
   * An XMLParser that is only dependant on JAXP 1.1 compliant parsers.
   *
   * The configuration can contain the following parameters :
   * <ul>
   * <li>validate (boolean, default = <code>false</code>) : should the parser
   *     validate parsed documents ?
   * </li>
   * <li>namespace-prefixes (boolean, default = <code>false</code>) : do we want
   *     namespaces declarations also as 'xmlns:' attributes ?<br>
   *     <i>Note</i> : setting this to <code>true</code> confuses some XSL
   *     processors (e.g. Saxon).
   * </li>
   * <li>stop-on-warning (boolean, default = <code>true</code>) : should the parser
   *     stop parsing if a warning occurs ?
   * </li>
   * <li>stop-on-recoverable-error (boolean, default = <code>true</code>) : should the parser
   *     stop parsing if a recoverable error occurs ?
   * </li>
   * <li>reuse-parsers (boolean, default = <code>true</code>) : do we want to reuse
   *     parsers or create a new parser for each parse ?<br>
   *     <i>Note</i> : even if this parameter is <code>true</code>, parsers are not
   *     recycled in case of parsing errors : some parsers (e.g. Xerces) don't like
   *     to be reused after failure.
   * </li>
   * <li>sax-parser-factory (string, optional) : the name of the <code>SAXParserFactory</code>
   *     implementation class to be used instead of using the standard JAXP mechanism
   *     (<code>SAXParserFactory.newInstance()</code>). This allows to choose
   *     unambiguously the JAXP implementation to be used when several of them are
   *     available in the classpath.
   * </li>
   * <li>document-builder-factory (string, optional) : the name of the
   *     <code>DocumentBuilderFactory</code> implementation to be used (similar to
   *     <code>sax-parser-factory</code> for DOM).
   * </li>
   * </ul>
   *
   * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
   * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
   * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
   * @version CVS $Revision: 1.1 $ $Date: 2002/04/22 10:06:04 $
   */
  public class JaxpParser
      extends AbstractLogEnabled
      implements Parser, ErrorHandler, Composable, Parameterizable, Poolable
  {
  
      /** the SAX Parser factory */
      protected SAXParserFactory factory;
  
      /** the Document Builder factory */
      protected DocumentBuilderFactory docFactory;
  
      /** The SAX reader. It is created lazily by {@link #setupXMLReader()}
       and cleared if a parsing error occurs. */
      protected XMLReader reader;
  
      /** The DOM builder. It is created lazily by {@link #setupDocumentBuilder()}
       and cleared if a parsing error occurs. */
      protected DocumentBuilder docBuilder;
  
      /** the component manager */
      protected ComponentManager manager;
  
      /** the Entity Resolver */
      protected EntityResolver resolver;
  
      /** do we want namespaces also as attributes ? */
      protected boolean nsPrefixes;
  
      /** do we want to reuse parsers ? */
      protected boolean reuseParsers;
  
      /** do we stop on warnings ? */
      protected boolean stopOnWarning;
  
      /** do we stop on recoverable errors ? */
      protected boolean stopOnRecoverableError;
  
      /**
       * Get the Entity Resolver from the component manager
       */
      public void compose( ComponentManager manager )
          throws ComponentException
      {
          this.manager = manager;
          if( this.manager.hasComponent( EntityResolver.ROLE ) )
          {
              this.resolver = (EntityResolver)this.manager.lookup( EntityResolver.ROLE );
              if( this.getLogger().isDebugEnabled() )
              {
                  this.getLogger().debug( "JaxpParser: Using EntityResolver: " + this.resolver );
              }
          }
      }
  
      /**
       * Configure
       */
      public void parameterize( Parameters params )
          throws ParameterException
      {
          // Validation and namespace prefixes parameters
          boolean validate = params.getParameterAsBoolean( "validate", false );
          this.nsPrefixes = params.getParameterAsBoolean( "namespace-prefixes", false );
          this.reuseParsers = params.getParameterAsBoolean( "reuse-parsers", true );
          this.stopOnWarning = params.getParameterAsBoolean( "stop-on-warning", true );
          this.stopOnRecoverableError = params.getParameterAsBoolean( "stop-on-recoverable-error", true );
  
          // Get the SAXFactory
          final String saxParserFactoryName = params.getParameter( "sax-parser-factory",
                                                                   "javax.xml.parsers.SAXParserFactory" );
          if( "javax.xml.parsers.SAXParserFactory".equals( saxParserFactoryName ) )
          {
              this.factory = SAXParserFactory.newInstance();
          }
          else
          {
              try
              {
                  final Class factoryClass = this.loadClass( saxParserFactoryName );
                  this.factory = (SAXParserFactory)factoryClass.newInstance();
              }
              catch( Exception e )
              {
                  throw new ParameterException( "Cannot load SAXParserFactory class " + saxParserFactoryName, e );
              }
          }
          this.factory.setNamespaceAware( true );
          this.factory.setValidating( validate );
  
          // Get the DocumentFactory
          final String documentBuilderFactoryName = params.getParameter( "document-builder-factory",
                                                                         "javax.xml.parsers.DocumentBuilderFactory" );
          if( "javax.xml.parsers.DocumentBuilderFactory".equals( documentBuilderFactoryName ) )
          {
              this.docFactory = DocumentBuilderFactory.newInstance();
          }
          else
          {
              try
              {
                  final Class factoryClass = this.loadClass( documentBuilderFactoryName );
                  this.docFactory = (DocumentBuilderFactory)factoryClass.newInstance();
              }
              catch( Exception e )
              {
                  throw new ParameterException( "Cannot load DocumentBuilderFactory class " + documentBuilderFactoryName, e );
              }
          }
          this.docFactory.setNamespaceAware( true );
          this.docFactory.setValidating( validate );
  
          if( this.getLogger().isDebugEnabled() )
          {
              this.getLogger().debug( "JaxpParser: validating: " + validate +
                                      ", namespace-prefixes: " + this.nsPrefixes +
                                      ", reuse parser: " + this.reuseParsers +
                                      ", stop on warning: " + this.stopOnWarning +
                                      ", stop on recoverable-error: " + this.stopOnRecoverableError +
                                      ", saxParserFactory: " + saxParserFactoryName +
                                      ", documentBuilderFactory: " + documentBuilderFactoryName );
          }
      }
  
      /**
       * Load a class
       */
      protected Class loadClass( String name ) throws Exception
      {
          ClassLoader loader = Thread.currentThread().getContextClassLoader();
          if( loader == null )
          {
              loader = this.getClass().getClassLoader();
          }
          return loader.loadClass( name );
      }
  
      /**
       * Parse the <code>InputSource</code> and send
       * SAX events to the consumer.
       * Attention: the consumer can either be an XMLConsumer
       * or implement the <code>LexicalHandler</code> as well.
       * The parse should take care of this.
       */
      public void parse( InputSource in, ContentHandler consumer )
          throws SAXException, IOException
      {
          if( consumer instanceof LexicalHandler )
          {
              this.parse( in, consumer, (LexicalHandler)consumer );
          }
          else
          {
              this.parse( in, consumer, null );
          }
      }
  
      /**
       * Parse the <code>InputSource</code> and send
       * SAX events to the consumer.
       * Attention: the consumer can  implement the
       * <code>LexicalHandler</code> as well.
       * The parse should take care of this.
       */
      public void parse( InputSource in,
                         ContentHandler contentHandler,
                         LexicalHandler lexicalHandler )
          throws SAXException, IOException
      {
          this.setupXMLReader();
  
          // Ensure we will use a fresh new parser at next parse in case of failure
          XMLReader tmpReader = this.reader;
          this.reader = null;
  
          try
          {
              if( null != lexicalHandler )
              {
                  tmpReader.setProperty( "http://xml.org/sax/properties/lexical-handler",
                                         lexicalHandler );
              }
          }
          catch( SAXException e )
          {
              this.getLogger().warn( "SAX2 driver does not support property: " +
                                     "'http://xml.org/sax/properties/lexical-handler'" );
          }
  
          tmpReader.setErrorHandler( this );
          tmpReader.setContentHandler( contentHandler );
          if( null != this.resolver )
          {
              tmpReader.setEntityResolver( this.resolver );
          }
  
          tmpReader.parse( in );
  
          // Here, parsing was successful : restore this.reader
          if( this.reuseParsers )
              this.reader = tmpReader;
      }
  
      /**
       * Parses a new Document object from the given InputSource.
       */
      public Document parseDocument( InputSource input )
          throws SAXException, IOException
      {
          this.setupDocumentBuilder();
  
          // Ensure we will use a fresh new parser at next parse in case of failure
          DocumentBuilder tmpBuilder = this.docBuilder;
          this.docBuilder = null;
  
          if( null != this.resolver )
          {
              tmpBuilder.setEntityResolver( this.resolver );
          }
  
          Document result = tmpBuilder.parse( input );
  
          // Here, parsing was successful : restore this.builder
          if( this.reuseParsers )
              this.docBuilder = tmpBuilder;
  
          return result;
      }
  
      /**
       * Creates a new <code>XMLReader</code> if needed.
       */
      protected void setupXMLReader()
          throws SAXException
      {
          if( null == this.reader )
          {
              // Create the XMLReader
              try
              {
                  this.reader = factory.newSAXParser().getXMLReader();
              }
              catch( ParserConfigurationException pce )
              {
                  throw new SAXException( "Cannot produce a valid parser", pce );
              }
              if( this.nsPrefixes )
              {
                  try
                  {
                      this.reader.setFeature( "http://xml.org/sax/features/namespace-prefixes", this.nsPrefixes );
                  }
                  catch( SAXException e )
                  {
                      this.getLogger().warn( "SAX2 XMLReader does not support setting feature: " +
                                             "'http://xml.org/sax/features/namespace-prefixes'" );
                  }
              }
          }
      }
  
      /**
       * Creates a new <code>DocumentBuilder</code> if needed.
       */
      protected void setupDocumentBuilder()
          throws SAXException
      {
          if( null == this.docBuilder )
          {
              try
              {
                  this.docBuilder = this.docFactory.newDocumentBuilder();
              }
              catch( ParserConfigurationException pce )
              {
                  throw new SAXException( "Could not create DocumentBuilder", pce );
              }
          }
      }
  
      /**
       * Return a new <code>Document</code>.
       */
      public Document createDocument()
          throws SAXException
      {
          this.setupDocumentBuilder();
          return this.docBuilder.newDocument();
      }
  
      /**
       * Receive notification of a recoverable error.
       */
      public void error( SAXParseException e )
          throws SAXException
      {
          final String msg = "Error parsing " + e.getSystemId() + " (line " +
              e.getLineNumber() + " col. " + e.getColumnNumber() +
              "): " + e.getMessage();
          if( this.stopOnRecoverableError )
          {
              throw new SAXException( msg, e );
          }
          this.getLogger().error( msg, e );
      }
  
      /**
       * Receive notification of a fatal error.
       */
      public void fatalError( SAXParseException e )
          throws SAXException
      {
          throw new SAXException( "Fatal error parsing " + e.getSystemId() + " (line " +
                                  e.getLineNumber() + " col. " + e.getColumnNumber() +
                                  "): " + e.getMessage(), e );
      }
  
      /**
       * Receive notification of a warning.
       */
      public void warning( SAXParseException e )
          throws SAXException
      {
          final String msg = "Warning parsing " + e.getSystemId() + " (line " +
              e.getLineNumber() + " col. " + e.getColumnNumber() +
              "): " + e.getMessage();
          if( this.stopOnWarning )
          {
              throw new SAXException( msg, e );
          }
          this.getLogger().warn( msg, e );
      }
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/xmlbundle/src/java/org/apache/avalon/excalibur/xml/Parser.java
  
  Index: Parser.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.excalibur.xml;
  
  import java.io.IOException;
  import org.apache.avalon.framework.component.Component;
  import org.w3c.dom.Document;
  import org.xml.sax.ContentHandler;
  import org.xml.sax.InputSource;
  import org.xml.sax.SAXException;
  import org.xml.sax.ext.LexicalHandler;
  
  /**
   *
   * The parser can be used to parse any XML document given
   * by a <code>InputSource</code> object.
   * It can either send XML events or create a DOM from
   * the parsed document.
   *
   * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
   * @version CVS $Revision: 1.1 $ $Date: 2002/04/22 10:06:04 $
   */
  public interface Parser extends Component
  {
  
      String ROLE = Parser.class.getName();
  
      /**
       * Parse the <code>InputSource</code> and send
       * SAX events to the consumer.
       * Attention: the consumer can  implement the
       * <code>LexicalHandler</code> as well.
       * The parse should take care of this.
       */
      void parse( InputSource in, ContentHandler consumer )
          throws SAXException, IOException;
  
      /**
       * Parse the <code>InputSource</code> and send
       * SAX events to the content handler and
       * the lexical handler.
       */
      void parse( InputSource in,
                  ContentHandler contentHandler,
                  LexicalHandler lexicalHandler )
          throws SAXException, IOException;
  
      /**
       * Parse the <code>InputSource</code> and create
       * a DOM out of it.
       */
      Document parseDocument( InputSource in )
          throws SAXException, IOException;
  
      /**
       * Return a new <code>Document</code>.
       */
      Document createDocument() throws SAXException;
  
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/xmlbundle/src/java/org/apache/avalon/excalibur/xml/XMLConsumer.java
  
  Index: XMLConsumer.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.excalibur.xml;
  
  import org.xml.sax.ContentHandler;
  import org.xml.sax.ext.LexicalHandler;
  
  /**
   * This interfaces identifies classes that consume XML data, receiving
   * notification of SAX events.
   * <br>
   * This interface unites the idea of SAX <code>ContentHandler</code> and
   * <code>LexicalHandler</code>.
   *
   * @author <a href="mailto:fumagalli@exoffice.com">Pierpaolo Fumagalli</a>
   *         (Apache Software Foundation, Exoffice Technologies)
   * @version CVS $Revision: 1.1 $ $Date: 2002/04/22 10:06:04 $
   */
  public interface XMLConsumer extends ContentHandler, LexicalHandler
  {
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/xmlbundle/src/java/org/apache/avalon/excalibur/xml/XMLFragment.java
  
  Index: XMLFragment.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.excalibur.xml;
  
  import org.w3c.dom.DOMException;
  import org.w3c.dom.Node;
  
  /**
   * This interface must be implemented by classes willing
   * to provide an XML representation of their current state.
   * <br/>
   *
   * @author <a href="mailto:sylvain.wallez@anyware-tech.com">Sylvain Wallez</a>
   * @author <a href="mailto:ricardo@apache.org">Ricardo Rocha</a> for the original XObject class
   * @version CVS $Revision: 1.1 $ $Date: 2002/04/22 10:06:04 $
   */
  
  public interface XMLFragment
  {
  
      /**
       * Appends children representing the object's state to the given node.
       */
      void toDOM( Node node ) throws DOMException;
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/xmlbundle/src/java/org/apache/avalon/excalibur/xml/XMLizable.java
  
  Index: XMLizable.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.excalibur.xml;
  
  import org.xml.sax.ContentHandler;
  import org.xml.sax.SAXException;
  
  /**
   * This interface can be implemented by classes willing to provide an XML representation
   * of their current state as SAX events.
   *
   * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
   * @version CVS $Revision: 1.1 $ $Date: 2002/04/22 10:06:04 $
   */
  
  public interface XMLizable
  {
      /**
       * Generates SAX events representing the object's state.
       * <b>NOTE</b> : if the implementation can produce lexical events, care should be taken
       * that <code>handler</code> can actually be a {@link XMLConsumer} that accepts such
       * events or directly implements the LexicalHandler interface!
       */
      void toSAX( ContentHandler handler ) throws SAXException;
  
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/xmlbundle/src/java/org/apache/avalon/excalibur/xml/XercesParser.java
  
  Index: XercesParser.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.excalibur.xml;
  
  import java.io.IOException;
  import org.apache.avalon.framework.logger.AbstractLogEnabled;
  import org.apache.avalon.framework.thread.SingleThreaded;
  import org.apache.xerces.dom.DocumentImpl;
  import org.apache.xerces.parsers.DOMParser;
  import org.apache.xerces.parsers.SAXParser;
  import org.w3c.dom.Document;
  import org.xml.sax.ContentHandler;
  import org.xml.sax.ErrorHandler;
  import org.xml.sax.InputSource;
  import org.xml.sax.SAXException;
  import org.xml.sax.SAXParseException;
  import org.xml.sax.ext.LexicalHandler;
  
  /**
   *
   * @author <a href="mailto:fumagalli@exoffice.com">Pierpaolo Fumagalli</a>
   *         (Apache Software Foundation, Exoffice Technologies)
   * @version CVS $Revision: 1.1 $ $Date: 2002/04/22 10:06:04 $
   */
  public class XercesParser
      extends AbstractLogEnabled
      implements Parser, ErrorHandler, SingleThreaded
  {
  
      /** the SAX Parser */
      final SAXParser parser;
  
      public XercesParser()
          throws SAXException
      {
          this.parser = new SAXParser();
  
          this.parser.setFeature( "http://xml.org/sax/features/validation", false );
          this.parser.setFeature( "http://xml.org/sax/features/namespaces", true );
          this.parser.setFeature( "http://xml.org/sax/features/namespace-prefixes",
                                  true );
      }
  
      public void parse( InputSource in, ContentHandler consumer )
          throws SAXException, IOException
      {
          if( consumer instanceof LexicalHandler )
          {
              this.parse( in, consumer, (LexicalHandler)consumer );
          }
          else
          {
              this.parse( in, consumer, null );
          }
      }
  
      /**
       * Parse the <code>InputSource</code> and send
       * SAX events to the content handler and
       * the lexical handler.
       */
      public void parse( InputSource in,
                         ContentHandler contentHandler,
                         LexicalHandler lexicalHandler )
          throws SAXException, IOException
      {
          if( null != lexicalHandler )
          {
              this.parser.setProperty( "http://xml.org/sax/properties/lexical-handler",
                                       lexicalHandler );
          }
          this.parser.setErrorHandler( this );
          this.parser.setContentHandler( contentHandler );
          this.parser.parse( in );
      }
  
      /**
       * Parses a new Document object from the given InputSource.
       */
      public Document parseDocument( InputSource input )
          throws SAXException, IOException
      {
          DOMParser parser = null;
  
          try
          {
              parser = new DOMParser();
  
              parser.setFeature( "http://xml.org/sax/features/validation", false );
              parser.setFeature( "http://xml.org/sax/features/namespaces", true );
              parser.setFeature( "http://xml.org/sax/features/namespace-prefixes",
                                 true );
  
              parser.parse( input );
          }
          catch( Exception pce )
          {
              getLogger().error( "Could not build DocumentBuilder", pce );
              return null;
          }
  
          return parser.getDocument();
      }
  
      /**
       * Return a new <code>Document</code>.
       */
      public Document createDocument() throws SAXException
      {
          return new DocumentImpl();
      }
  
      /**
       * Receive notification of a recoverable error.
       */
      public void error( SAXParseException e )
          throws SAXException
      {
          throw new SAXException( "Error parsing " + e.getSystemId() + " (line " +
                                  e.getLineNumber() + " col. " + e.getColumnNumber() +
                                  "): " + e.getMessage(), e );
      }
  
      /**
       * Receive notification of a fatal error.
       */
      public void fatalError( SAXParseException e )
          throws SAXException
      {
          throw new SAXException( "Fatal error parsing " + e.getSystemId() + " (line " +
                                  e.getLineNumber() + " col. " + e.getColumnNumber() +
                                  "): " + e.getMessage(), e );
      }
  
      /**
       * Receive notification of a warning.
       */
      public void warning( SAXParseException e )
          throws SAXException
      {
          throw new SAXException( "Warning parsing " + e.getSystemId() + " (line " +
                                  e.getLineNumber() + " col. " + e.getColumnNumber() +
                                  "): " + e.getMessage(), e );
      }
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/xmlbundle/src/java/org/apache/avalon/excalibur/xml/xpath/JaxenProcessorImpl.java
  
  Index: JaxenProcessorImpl.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included  with this distribution in *
   * the LICENSE.txt file.                                                         *
   *****************************************************************************/
  package org.apache.avalon.excalibur.xml.xpath;
  
  import java.util.List;
  import org.apache.avalon.framework.logger.AbstractLoggable;
  import org.apache.avalon.framework.thread.ThreadSafe;
  import org.jaxen.dom.XPath;
  import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
  
  /**
   * This class defines the implementation of the {@link XPathProcessor}
   * component.
   *
   * To configure it, add the following lines in the
   * <file>cocoon.xconf</file> file:
   *
   * <pre>
   * &lt;xpath-processor class="org.apache.cocoon.components.xpath.JaxenProcessorImpl"&gt;
   * &lt;/xpath-processor&gt;
   * </pre>
   *
   * @author <a href="mailto:dims@yahoo.com">Davanum Srinivas</a>
   * @version CVS $Revision: 1.1 $ $Date: 2002/04/22 10:06:05 $ $Author: cziegeler $
   */
  public class JaxenProcessorImpl
      extends AbstractLoggable
      implements XPathProcessor, ThreadSafe
  {
      /**
       * Use an XPath string to select a single node. XPath namespace
       * prefixes are resolved from the context node, which may not
       * be what you want (see the next method).
       *
       * @param contextNode The node to start searching from.
       * @param str A valid XPath string.
       * @return The first node found that matches the XPath, or null.
       */
      public Node selectSingleNode( Node contextNode, String str )
      {
          try
          {
              XPath path = new XPath( str );
              return (Node)path.selectSingleNode( (Object)contextNode );
          }
          catch( Exception e )
          {
              // ignore it
          }
          return null;
      }
  
      /**
       *  Use an XPath string to select a nodelist.
       *  XPath namespace prefixes are resolved from the contextNode.
       *
       *  @param contextNode The node to start searching from.
       *  @param str A valid XPath string.
       *  @return A NodeList, should never be null.
       */
      public NodeList selectNodeList( Node contextNode, String str )
      {
          try
          {
              XPath path = new XPath( str );
              List list = path.selectNodes( (Object)contextNode );
              return new NodeListEx( list );
          }
          catch( Exception e )
          {
              // ignore it
          }
          return new NodeListEx();
      }
  
      class NodeListEx implements NodeList
      {
          List list = null;
  
          NodeListEx()
          {
          }
  
          NodeListEx( List l )
          {
              list = l;
          }
  
          public Node item( int index )
          {
              if( list == null )
                  return null;
              return (Node)list.get( index );
          }
  
          public int getLength()
          {
              if( list == null )
                  return 0;
              return list.size();
          }
      }
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/xmlbundle/src/java/org/apache/avalon/excalibur/xml/xpath/XPathProcessor.java
  
  Index: XPathProcessor.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included  with this distribution in *
   * the LICENSE.txt file.                                                         *
   *****************************************************************************/
  package org.apache.avalon.excalibur.xml.xpath;
  
  import org.apache.avalon.framework.component.Component;
  import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
  
  /**
   * This is the interface of the XPath processor.
   *
   * @author <a href="mailto:dims@yahoo.com">Davanum Srinivas</a>
   * @version CVS $Revision: 1.1 $ $Date: 2002/04/22 10:06:05 $ $Author: cziegeler $
   */
  public interface XPathProcessor extends Component
  {
      /**
       * The role implemented by an <code>XSLTProcessor</code>.
       */
      String ROLE = XPathProcessor.class.getName();
  
      /**
       * Use an XPath string to select a single node. XPath namespace
       * prefixes are resolved from the context node, which may not
       * be what you want (see the next method).
       *
       * @param contextNode The node to start searching from.
       * @param str A valid XPath string.
       * @return The first node found that matches the XPath, or null.
       */
      Node selectSingleNode( Node contextNode, String str );
  
      /**
       *  Use an XPath string to select a nodelist.
       *  XPath namespace prefixes are resolved from the contextNode.
       *
       *  @param contextNode The node to start searching from.
       *  @param str A valid XPath string.
       *  @return A List, should never be null.
       */
      NodeList selectNodeList( Node contextNode, String str );
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/xmlbundle/src/java/org/apache/avalon/excalibur/xml/xpath/XPathProcessorImpl.java
  
  Index: XPathProcessorImpl.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included  with this distribution in *
   * the LICENSE.txt file.                                                         *
   *****************************************************************************/
  package org.apache.avalon.excalibur.xml.xpath;
  
  import org.apache.avalon.framework.logger.AbstractLoggable;
  import org.apache.avalon.framework.thread.ThreadSafe;
  import org.apache.xpath.XPathAPI;
  import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
  
  /**
   * This class defines the implementation of the {@link XPathProcessor}
   * component.
   *
   * To configure it, add the following lines in the
   * <file>cocoon.xconf</file> file:
   *
   * <pre>
   * &lt;xpath-processor class="org.apache.cocoon.components.xpath.XPathProcessorImpl"&gt;
   * &lt;/xpath-processor&gt;
   * </pre>
   *
   * @author <a href="mailto:dims@yahoo.com">Davanum Srinivas</a>
   * @version CVS $Revision: 1.1 $ $Date: 2002/04/22 10:06:05 $ $Author: cziegeler $
   */
  public class XPathProcessorImpl
      extends AbstractLoggable
      implements XPathProcessor, ThreadSafe
  {
      /**
       * Use an XPath string to select a single node. XPath namespace
       * prefixes are resolved from the context node, which may not
       * be what you want (see the next method).
       *
       * @param contextNode The node to start searching from.
       * @param str A valid XPath string.
       * @return The first node found that matches the XPath, or null.
       */
      public Node selectSingleNode( Node contextNode, String str )
      {
          try
          {
              return XPathAPI.selectSingleNode( contextNode, str );
          }
          catch( javax.xml.transform.TransformerException e )
          {
              return null;
          }
      }
  
      /**
       *  Use an XPath string to select a nodelist.
       *  XPath namespace prefixes are resolved from the contextNode.
       *
       *  @param contextNode The node to start searching from.
       *  @param str A valid XPath string.
       *  @return A NodeList, should never be null.
       */
      public NodeList selectNodeList( Node contextNode, String str )
      {
          try
          {
              return XPathAPI.selectNodeList( contextNode, str );
          }
          catch( javax.xml.transform.TransformerException e )
          {
              return new NodeList()
              {
                  public Node item( int index )
                  {
                      return null;
                  }
  
                  public int getLength()
                  {
                      return 0;
                  }
              };
          }
      }
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/xmlbundle/src/java/org/apache/avalon/excalibur/xml/xpath/package.html
  
  Index: package.html
  ===================================================================
  <html>
  <body>
  XPath processor and implementation.
  </body>
  </html>
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>