1 17 package org.apache.excalibur.xml.xpath; 18 19 import java.util.List ; 20 21 import org.apache.avalon.framework.component.Component; 22 import org.apache.avalon.framework.thread.ThreadSafe; 23 24 import org.jaxen.NamespaceContext; 25 import org.jaxen.dom.DOMXPath; 26 import org.w3c.dom.Node ; 27 import org.w3c.dom.NodeList ; 28 29 44 public final class JaxenProcessorImpl 45 extends AbstractProcessorImpl 46 implements XPathProcessor, Component, ThreadSafe 47 { 48 56 public boolean evaluateAsBoolean(Node contextNode, String str, PrefixResolver resolver) 57 { 58 try 59 { 60 final DOMXPath path = new DOMXPath( str ); 61 path.setNamespaceContext( new JaxenResolver(resolver) ); 62 return path.booleanValueOf( contextNode ); 63 } 64 catch( final Exception e ) 65 { 66 if (getLogger().isDebugEnabled()) { 67 getLogger().debug("Failed to evaluate '" + str + "'", e); 68 } 69 70 return false; 72 } 73 } 74 75 83 public Number evaluateAsNumber(Node contextNode, String str, PrefixResolver resolver) 84 { 85 try 86 { 87 final DOMXPath path = new DOMXPath( str ); 88 path.setNamespaceContext( new JaxenResolver(resolver) ); 89 return path.numberValueOf( contextNode ); 90 } 91 catch( final Exception e ) 92 { 93 if (getLogger().isDebugEnabled()) { 94 getLogger().debug("Failed to evaluate '" + str + "'", e); 95 } 96 97 return null; 99 } 100 } 101 102 110 public String evaluateAsString(Node contextNode, String str, PrefixResolver resolver) 111 { 112 try 113 { 114 final DOMXPath path = new DOMXPath( str ); 115 path.setNamespaceContext( new JaxenResolver(resolver) ); 116 return path.stringValueOf( contextNode ); 117 } 118 catch( final Exception e ) 119 { 120 if (getLogger().isDebugEnabled()) { 121 getLogger().debug("Failed to evaluate '" + str + "'", e); 122 } 123 124 return null; 126 } 127 } 128 129 137 public Node selectSingleNode(Node contextNode, String str, PrefixResolver resolver) 138 { 139 try 140 { 141 final DOMXPath path = new DOMXPath( str ); 142 path.setNamespaceContext( new JaxenResolver(resolver) ); 143 return (Node )path.selectSingleNode( contextNode ); 144 } 145 catch( final Exception e ) 146 { 147 if (getLogger().isDebugEnabled()) { 148 getLogger().debug("Failed to evaluate '" + str + "'", e); 149 } 150 151 return null; 153 } 154 } 155 156 164 public NodeList selectNodeList(Node contextNode, String str, PrefixResolver resolver) 165 { 166 try 167 { 168 final DOMXPath path = new DOMXPath( str ); 169 path.setNamespaceContext( new JaxenResolver(resolver) ); 170 final List list = path.selectNodes( contextNode ); 171 return new SimpleNodeList( list ); 172 } 173 catch( final Exception e ) 174 { 175 if (getLogger().isDebugEnabled()) { 176 getLogger().debug("Failed to evaluate '" + str + "'", e); 177 } 178 179 return new EmptyNodeList(); 181 } 182 } 183 184 187 private static class JaxenResolver implements NamespaceContext 188 { 189 private final PrefixResolver resolver; 190 191 public JaxenResolver(PrefixResolver resolver) 192 { 193 this.resolver = resolver; 194 } 195 196 public String translateNamespacePrefixToUri(String prefix) 197 { 198 return resolver.prefixToNamespace(prefix); 199 } 200 } 201 } 202 | Popular Tags |