vgritsenko 2003/07/31 18:01:49
Added: xmlutil/src/java/org/apache/excalibur/xml/xpath
AbstractProcessorImpl.java
Log:
Abstract xpath processor containing code common to all implementations
Revision Changes Path
1.1
avalon-excalibur/xmlutil/src/java/org/apache/excalibur/xml/xpath/AbstractProcessorImpl.java
Index: AbstractProcessorImpl.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software
* itself, if and wherever such third-party acknowledgments
* normally appear.
*
* 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
* must not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.excalibur.xml.xpath;
import java.util.HashMap;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* This class defines base class for the implementations of the
* [EMAIL PROTECTED] XPathProcessor} component.
* Provides implementation of the [EMAIL PROTECTED] PrefixResolver} and common
* implementation of five selectXXX methods.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Vadim Gritsenko</a>
* @version CVS $Revision: 1.1 $ $Date: 2003/08/01 01:01:49 $ $Author: vgritsenko $
*/
public abstract class AbstractProcessorImpl
extends AbstractLogEnabled
implements XPathProcessor, Configurable, PrefixResolver
{
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
* 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( final Node contextNode,
final String str )
{
return selectSingleNode(contextNode, str, this);
}
/**
* 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( final Node contextNode,
final String str )
{
return selectNodeList(contextNode, str, this);
}
/**
* 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 )
{
return evaluateAsBoolean(contextNode, str, this);
}
/**
* 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 )
{
return evaluateAsNumber(contextNode, str, this);
}
/**
* 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 )
{
return evaluateAsString(contextNode, str, this);
}
/**
* Evaluate XPath expression within a context.
*
* @param contextNode The context node.
* @param str A valid XPath string.
* @param resolver a PrefixResolver, used for resolving namespace prefixes
* @return expression result as boolean.
*/
public abstract boolean evaluateAsBoolean(Node contextNode, String str,
PrefixResolver resolver);
/**
* Evaluate XPath expression within a context.
*
* @param contextNode The context node.
* @param str A valid XPath string.
* @param resolver a PrefixResolver, used for resolving namespace prefixes
* @return expression result as number.
*/
public abstract Number evaluateAsNumber(Node contextNode, String str,
PrefixResolver resolver);
/**
* Evaluate XPath expression within a context.
*
* @param contextNode The context node.
* @param str A valid XPath string.
* @param resolver a PrefixResolver, used for resolving namespace prefixes
* @return expression result as string.
*/
public abstract String evaluateAsString(Node contextNode, String str,
PrefixResolver resolver);
/**
* Use an XPath string to select a single node.
*
* @param contextNode The node to start searching from.
* @param str A valid XPath string.
* @param resolver a PrefixResolver, used for resolving namespace prefixes
* @return The first node found that matches the XPath, or null.
*/
public abstract Node selectSingleNode(Node contextNode, String str,
PrefixResolver resolver);
/**
* Use an XPath string to select a nodelist.
*
* @param contextNode The node to start searching from.
* @param str A valid XPath string.
* @param resolver a PrefixResolver, used for resolving namespace prefixes
* @return A List, should never be null.
*/
public abstract NodeList selectNodeList(Node contextNode, String str,
PrefixResolver resolver);
public String prefixToNamespace(String prefix)
{
return (String)m_mappings.get( prefix );
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]