vgritsenko 2003/07/31 18:02:43
Modified: xmlutil/src/java/org/apache/excalibur/xml/xpath
JaxenProcessorImpl.java XPathProcessorImpl.java
Log:
* Use abstract base class to reduce code duplication
* In case of error, print a DEBUG message saying so
Revision Changes Path
1.12 +32 -90
avalon-excalibur/xmlutil/src/java/org/apache/excalibur/xml/xpath/JaxenProcessorImpl.java
Index: JaxenProcessorImpl.java
===================================================================
RCS file:
/home/cvs/avalon-excalibur/xmlutil/src/java/org/apache/excalibur/xml/xpath/JaxenProcessorImpl.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- JaxenProcessorImpl.java 20 May 2003 10:43:00 -0000 1.11
+++ JaxenProcessorImpl.java 1 Aug 2003 01:02:43 -0000 1.12
@@ -54,15 +54,11 @@
*/
package org.apache.excalibur.xml.xpath;
-import java.util.HashMap;
import java.util.List;
import org.apache.avalon.framework.component.Component;
-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.apache.avalon.framework.thread.ThreadSafe;
+
import org.jaxen.NamespaceContext;
import org.jaxen.dom.DOMXPath;
import org.w3c.dom.Node;
@@ -83,84 +79,10 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Davanum Srinivas</a>
* @version CVS $Revision$ $Date$ $Author$
*/
-public final class JaxenProcessorImpl extends AbstractLogEnabled implements
XPathProcessor, Configurable, Component, ThreadSafe, PrefixResolver
+public final class JaxenProcessorImpl
+ extends AbstractProcessorImpl
+ implements XPathProcessor, Component, ThreadSafe
{
- 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.
*
@@ -179,6 +101,11 @@
}
catch( final Exception e )
{
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug("Failed to evaluate '" + str + "'", e);
+ }
+
+ // ignore it
return false;
}
}
@@ -201,6 +128,11 @@
}
catch( final Exception e )
{
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug("Failed to evaluate '" + str + "'", e);
+ }
+
+ // ignore it
return null;
}
}
@@ -223,6 +155,11 @@
}
catch( final Exception e )
{
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug("Failed to evaluate '" + str + "'", e);
+ }
+
+ // ignore it
return null;
}
}
@@ -245,6 +182,10 @@
}
catch( final Exception e )
{
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug("Failed to evaluate '" + str + "'", e);
+ }
+
// ignore it
return null;
}
@@ -269,23 +210,24 @@
}
catch( final Exception e )
{
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug("Failed to evaluate '" + str + "'", e);
+ }
+
// ignore it
return new EmptyNodeList();
}
}
- public String prefixToNamespace(String prefix)
- {
- return (String)m_mappings.get( prefix );
- }
-
/**
* A Jaxen-specific wrapper for the PrefixResolver.
*/
- private static class JaxenResolver implements NamespaceContext {
+ private static class JaxenResolver implements NamespaceContext
+ {
private final PrefixResolver resolver;
- public JaxenResolver(PrefixResolver resolver) {
+ public JaxenResolver(PrefixResolver resolver)
+ {
this.resolver = resolver;
}
1.13 +36 -88
avalon-excalibur/xmlutil/src/java/org/apache/excalibur/xml/xpath/XPathProcessorImpl.java
Index: XPathProcessorImpl.java
===================================================================
RCS file:
/home/cvs/avalon-excalibur/xmlutil/src/java/org/apache/excalibur/xml/xpath/XPathProcessorImpl.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- XPathProcessorImpl.java 20 May 2003 10:43:00 -0000 1.12
+++ XPathProcessorImpl.java 1 Aug 2003 01:02:43 -0000 1.13
@@ -54,18 +54,16 @@
*/
package org.apache.excalibur.xml.xpath;
-import java.util.HashMap;
-
import javax.xml.transform.TransformerException;
import org.apache.avalon.framework.component.Component;
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.apache.avalon.framework.thread.ThreadSafe;
import org.apache.xpath.XPathAPI;
import org.apache.xpath.objects.XObject;
+
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
@@ -84,86 +82,17 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Davanum Srinivas</a>
* @version CVS $Revision$ $Date$ $Author$
*/
-public final class XPathProcessorImpl extends AbstractLogEnabled implements
XPathProcessor, Configurable, PrefixResolver, Component, ThreadSafe
+public final class XPathProcessorImpl
+ extends AbstractProcessorImpl
+ implements XPathProcessor, Configurable, Component, ThreadSafe
{
-
private String m_baseURI;
- private final HashMap m_mappings = new HashMap();
public void configure( Configuration configuration ) throws
ConfigurationException
{
+ super.configure(configuration);
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
- * 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);
}
/**
@@ -181,8 +110,13 @@
final XObject result = XPathAPI.eval( contextNode, str, new
XalanResolver(resolver) );
return result.bool();
}
- catch( final TransformerException te )
+ catch( final TransformerException e )
{
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug("Failed to evaluate '" + str + "'", e);
+ }
+
+ // ignore it
return false;
}
}
@@ -202,8 +136,13 @@
final XObject result = XPathAPI.eval( contextNode, str, new
XalanResolver(resolver) );
return new Double( result.num() );
}
- catch( final TransformerException te )
+ catch( final TransformerException e )
{
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug("Failed to evaluate '" + str + "'", e);
+ }
+
+ // ignore it
return null;
}
}
@@ -223,8 +162,13 @@
final XObject result = XPathAPI.eval( contextNode, str, new
XalanResolver(resolver) );
return result.str();
}
- catch( final TransformerException te )
+ catch( final TransformerException e )
{
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug("Failed to evaluate '" + str + "'", e);
+ }
+
+ // ignore it
return null;
}
}
@@ -244,8 +188,13 @@
final XObject result = XPathAPI.eval( contextNode, str, new
XalanResolver(resolver) );
return result.nodeset().nextNode();
}
- catch( final TransformerException te )
+ catch( final TransformerException e )
{
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug("Failed to evaluate '" + str + "'", e);
+ }
+
+ // ignore it
return null;
}
}
@@ -265,17 +214,17 @@
final XObject result = XPathAPI.eval( contextNode, str, new
XalanResolver(resolver) );
return result.nodelist();
}
- catch( final TransformerException te )
+ catch( final TransformerException e )
{
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug("Failed to evaluate '" + str + "'", e);
+ }
+
+ // ignore it
return new EmptyNodeList();
}
}
- public String prefixToNamespace(String prefix)
- {
- return (String)m_mappings.get( prefix );
- }
-
/**
* A Xalan-specific wrapper for the PrefixResolver.
*/
@@ -307,4 +256,3 @@
}
}
}
-
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]