You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by mi...@apache.org on 2002/08/01 23:35:39 UTC

cvs commit: jakarta-avalon-excalibur/xmlutil/src/java/org/apache/avalon/excalibur/xml/xpath JaxenProcessorImpl.java XPathProcessorImpl.java

mirceatoma    2002/08/01 14:35:39

  Modified:    xmlutil/src/java/org/apache/avalon/excalibur/xml/xpath
                        JaxenProcessorImpl.java XPathProcessorImpl.java
  Log:
  Add evaluation methods with for XPath expression that can return string, number, or boolean.
  Add namespace mapping prefix->uri when evaluating namespace aware documents.
  
  Revision  Changes    Path
  1.5       +93 -8     jakarta-avalon-excalibur/xmlutil/src/java/org/apache/avalon/excalibur/xml/xpath/JaxenProcessorImpl.java
  
  Index: JaxenProcessorImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/xmlutil/src/java/org/apache/avalon/excalibur/xml/xpath/JaxenProcessorImpl.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- JaxenProcessorImpl.java	10 Jul 2002 08:53:17 -0000	1.4
  +++ JaxenProcessorImpl.java	1 Aug 2002 21:35:39 -0000	1.5
  @@ -7,12 +7,19 @@
    *****************************************************************************/
   package org.apache.avalon.excalibur.xml.xpath;
   
  +import java.util.HashMap;
   import java.util.List;
  -import org.apache.avalon.framework.logger.AbstractLoggable;
  -import org.apache.avalon.framework.thread.ThreadSafe;
  +import org.apache.avalon.framework.configuration.Configurable;
  +import org.apache.avalon.framework.configuration.ConfigurationException;
  +import org.apache.avalon.framework.configuration.Configuration;
  +import org.apache.avalon.framework.logger.AbstractLogEnabled;
   import org.jaxen.dom.DOMXPath;
  +import org.apache.avalon.framework.component.Component;
  +import org.apache.avalon.framework.thread.ThreadSafe;
   import org.w3c.dom.Node;
   import org.w3c.dom.NodeList;
  +import org.jaxen.NamespaceContext;
  +
   
   /**
    * This class defines the implementation of the {@link XPathProcessor}
  @@ -29,10 +36,22 @@
    * @author <a href="mailto:dims@yahoo.com">Davanum Srinivas</a>
    * @version CVS $Revision$ $Date$ $Author$
    */
  -public final class JaxenProcessorImpl
  -    extends AbstractLoggable
  -    implements XPathProcessor, ThreadSafe
  +public final class JaxenProcessorImpl extends AbstractLogEnabled implements XPathProcessor, Configurable, Component, ThreadSafe, NamespaceContext
   {
  +    private final HashMap m_mappings = new HashMap();
  +        
  +    public void configure(Configuration configuration) throws ConfigurationException 
  +    {
  +        final Configuration namespaceMappings = configuration.getChild( "namespace-mappings", true );        
  +        final Configuration[] namespaces = namespaceMappings.getChildren( "namespace" );
  +        for ( int i = 0; i < namespaces.length; i++ ) 
  +        {
  +            final String prefix = namespaces[i].getAttribute( "prefix" );
  +            final String uri = namespaces[i].getAttribute( "uri" );
  +            m_mappings.put( prefix, uri );
  +        }
  +    }
  +    
       /**
        * Use an XPath string to select a single node. XPath namespace
        * prefixes are resolved from the context node, which may not
  @@ -48,7 +67,8 @@
           try
           {
               final DOMXPath path = new DOMXPath( str );
  -            return (Node)path.selectSingleNode( (Object)contextNode );
  +            path.setNamespaceContext( this );
  +            return (Node)path.selectSingleNode( contextNode );
           }
           catch( final Exception e )
           {
  @@ -71,7 +91,8 @@
           try
           {
               final DOMXPath path = new DOMXPath( str );
  -            final List list = path.selectNodes( (Object)contextNode );
  +            path.setNamespaceContext( this );
  +            final List list = path.selectNodes( contextNode );
               return new SimpleNodeList( list );
           }
           catch( final Exception e )
  @@ -80,4 +101,68 @@
               return new EmptyNodeList();
           }
       }
  +    
  +    /** Evaluate XPath expression within a context.
  +     *
  +     * @param contextNode The context node.
  +     * @param str A valid XPath string.
  +     * @return expression result as boolean.
  +     */
  +    public boolean evaluateAsBoolean(Node contextNode, String str) {
  +        try
  +        {
  +            final DOMXPath path = new DOMXPath( str );
  +            path.setNamespaceContext( this );
  +            return path.booleanValueOf( contextNode );
  +        }
  +        catch( final Exception e )
  +        {
  +            return false;
  +        }
  +    }
  +    
  +    /** Evaluate XPath expression within a context.
  +     *
  +     * @param contextNode The context node.
  +     * @param str A valid XPath string.
  +     * @return expression result as number.
  +     */
  +    public Number evaluateAsNumber( Node contextNode, String str ) 
  +    {
  +        try
  +        {
  +            final DOMXPath path = new DOMXPath( str );
  +            path.setNamespaceContext( this );
  +            return path.numberValueOf( contextNode );
  +        }
  +        catch( final Exception e )
  +        {
  +            return null;
  +        }
  +    }
  +        
  +    /** Evaluate XPath expression within a context.
  +     *
  +     * @param contextNode The context node.
  +     * @param str A valid XPath string.
  +     * @return expression result as string.
  +     */
  +    public String evaluateAsString(Node contextNode, String str) 
  +    {
  +        try
  +        {
  +            final DOMXPath path = new DOMXPath( str );
  +            path.setNamespaceContext( this );
  +            return path.stringValueOf( contextNode );
  +        }
  +        catch( final Exception e )
  +        {
  +            return null;
  +        }
  +    }    
  +    
  +    public String translateNamespacePrefixToUri( String prefix ) 
  +    {
  +        return (String)m_mappings.get( prefix );
  +    }    
   }
  
  
  
  1.4       +104 -9    jakarta-avalon-excalibur/xmlutil/src/java/org/apache/avalon/excalibur/xml/xpath/XPathProcessorImpl.java
  
  Index: XPathProcessorImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/xmlutil/src/java/org/apache/avalon/excalibur/xml/xpath/XPathProcessorImpl.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- XPathProcessorImpl.java	10 Jul 2002 08:53:17 -0000	1.3
  +++ XPathProcessorImpl.java	1 Aug 2002 21:35:39 -0000	1.4
  @@ -7,12 +7,19 @@
    */
   package org.apache.avalon.excalibur.xml.xpath;
   
  -import org.apache.avalon.framework.logger.AbstractLoggable;
  -import org.apache.avalon.framework.thread.ThreadSafe;
  +import java.util.HashMap;
  +import javax.xml.transform.TransformerException;
  +import org.apache.xpath.objects.XObject;
   import org.apache.xpath.XPathAPI;
  +import org.apache.xml.utils.PrefixResolver;
  +import org.apache.avalon.framework.component.Component;
  +import org.apache.avalon.framework.thread.ThreadSafe;
  +import org.apache.avalon.framework.configuration.Configurable;
  +import org.apache.avalon.framework.configuration.ConfigurationException;
  +import org.apache.avalon.framework.configuration.Configuration;
  +import org.apache.avalon.framework.logger.AbstractLogEnabled;
   import org.w3c.dom.Node;
   import org.w3c.dom.NodeList;
  -import javax.xml.transform.TransformerException;
   
   /**
    * This class defines the implementation of the {@link XPathProcessor}
  @@ -29,10 +36,26 @@
    * @author <a href="mailto:dims@yahoo.com">Davanum Srinivas</a>
    * @version CVS $Revision$ $Date$ $Author$
    */
  -public final class XPathProcessorImpl
  -    extends AbstractLoggable
  -    implements XPathProcessor, ThreadSafe
  +public final class XPathProcessorImpl extends AbstractLogEnabled implements XPathProcessor, Configurable, PrefixResolver, Component, ThreadSafe
   {
  +    
  +    private String m_baseURI;
  +    private final HashMap m_mappings = new HashMap();
  +        
  +    public void configure(Configuration configuration) throws ConfigurationException 
  +    {
  +        final Configuration namespaceMappings = configuration.getChild( "namespace-mappings", true );
  +        m_baseURI = namespaceMappings.getAttribute( "base-uri", null);
  +        
  +        final Configuration[] namespaces = namespaceMappings.getChildren( "namespace" );
  +        for ( int i = 0; i < namespaces.length; i++ ) 
  +        {
  +            final String prefix = namespaces[i].getAttribute( "prefix" );
  +            final String uri = namespaces[i].getAttribute( "uri" );
  +            m_mappings.put( prefix, uri );
  +        }
  +    }
  +
       /**
        * Use an XPath string to select a single node. XPath namespace
        * prefixes are resolved from the context node, which may not
  @@ -47,7 +70,8 @@
       {
           try
           {
  -            return XPathAPI.selectSingleNode( contextNode, str );
  +            final XObject result = XPathAPI.eval( contextNode, str, this );
  +            return result.nodeset().nextNode();
           }
           catch( final TransformerException te )
           {
  @@ -68,11 +92,82 @@
       {
           try
           {
  -            return XPathAPI.selectNodeList( contextNode, str );
  +            final XObject result = XPathAPI.eval( contextNode, str, this );
  +            return result.nodelist();
           }
           catch( final TransformerException te )
           {
               return new EmptyNodeList();
           }
       }
  +    
  +    /** Evaluate XPath expression within a context.
  +     *
  +     * @param contextNode The context node.
  +     * @param str A valid XPath string.
  +     * @return expression result as boolean.
  +     */
  +    public boolean evaluateAsBoolean(Node contextNode, String str) 
  +    {
  +        try
  +        {
  +            final XObject result = XPathAPI.eval( contextNode, str, this );
  +            return result.bool();
  +        }
  +        catch( final TransformerException te )
  +        {
  +            return false;
  +        }
  +    }    
  +        
  +    /** Evaluate XPath expression within a context.
  +     *
  +     * @param contextNode The context node.
  +     * @param str A valid XPath string.
  +     * @return expression result as number.
  +     */
  +    public Number evaluateAsNumber(Node contextNode, String str) {
  +        try
  +        {
  +            final XObject result = XPathAPI.eval( contextNode, str, this );
  +            return new Double(result.num());
  +        }
  +        catch( final TransformerException te )
  +        {
  +            return null;
  +        }
  +    }
  +        
  +    /** Evaluate XPath expression within a context.
  +     *
  +     * @param contextNode The context node.
  +     * @param str A valid XPath string.
  +     * @return expression result as string.
  +     */
  +    public String evaluateAsString(Node contextNode, String str) {
  +        try
  +        {
  +            final XObject result = XPathAPI.eval( contextNode, str, this );
  +            return result.str();
  +        }
  +        catch( final TransformerException te )
  +        {
  +            return null;
  +        }
  +    }
  +    
  +    public String getBaseIdentifier() 
  +    {
  +        return m_baseURI;
  +    }
  +    
  +    public String getNamespaceForPrefix( String prefix ) 
  +    {
  +        return (String)m_mappings.get( prefix );
  +    }
  +    
  +    public String getNamespaceForPrefix( String prefix, Node node ) 
  +    {
  +        return getNamespaceForPrefix( prefix );
  +    }    
   }
  
  
  

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