1 17 package org.apache.excalibur.xml.xpath; 18 19 import javax.xml.transform.TransformerException ; 20 21 import org.apache.avalon.framework.component.Component; 22 import org.apache.avalon.framework.configuration.Configurable; 23 import org.apache.avalon.framework.configuration.Configuration; 24 import org.apache.avalon.framework.configuration.ConfigurationException; 25 import org.apache.avalon.framework.thread.ThreadSafe; 26 import org.apache.xpath.XPathAPI; 27 import org.apache.xpath.objects.XObject; 28 29 import org.w3c.dom.Node ; 30 import org.w3c.dom.NodeList ; 31 32 47 public final class XPathProcessorImpl 48 extends AbstractProcessorImpl 49 implements XPathProcessor, Configurable, Component, ThreadSafe 50 { 51 private String m_baseURI; 52 53 public void configure( Configuration configuration ) throws ConfigurationException 54 { 55 super.configure(configuration); 56 final Configuration namespaceMappings = configuration.getChild( "namespace-mappings", true ); 57 m_baseURI = namespaceMappings.getAttribute( "base-uri", null ); 58 } 59 60 68 public boolean evaluateAsBoolean(Node contextNode, String str, PrefixResolver resolver) 69 { 70 try 71 { 72 final XObject result = XPathAPI.eval( contextNode, str, new XalanResolver(resolver) ); 73 return result.bool(); 74 } 75 catch( final TransformerException e ) 76 { 77 if (getLogger().isDebugEnabled()) { 78 getLogger().debug("Failed to evaluate '" + str + "'", e); 79 } 80 81 return false; 83 } 84 } 85 86 94 public Number evaluateAsNumber(Node contextNode, String str, PrefixResolver resolver) 95 { 96 try 97 { 98 final XObject result = XPathAPI.eval( contextNode, str, new XalanResolver(resolver) ); 99 return new Double ( result.num() ); 100 } 101 catch( final TransformerException e ) 102 { 103 if (getLogger().isDebugEnabled()) { 104 getLogger().debug("Failed to evaluate '" + str + "'", e); 105 } 106 107 return null; 109 } 110 } 111 112 120 public String evaluateAsString(Node contextNode, String str, PrefixResolver resolver) 121 { 122 try 123 { 124 final XObject result = XPathAPI.eval( contextNode, str, new XalanResolver(resolver) ); 125 return result.str(); 126 } 127 catch( final TransformerException e ) 128 { 129 if (getLogger().isDebugEnabled()) { 130 getLogger().debug("Failed to evaluate '" + str + "'", e); 131 } 132 133 return null; 135 } 136 } 137 138 146 public Node selectSingleNode(Node contextNode, String str, PrefixResolver resolver) 147 { 148 try 149 { 150 final XObject result = XPathAPI.eval( contextNode, str, new XalanResolver(resolver) ); 151 return result.nodeset().nextNode(); 152 } 153 catch( final TransformerException e ) 154 { 155 if (getLogger().isDebugEnabled()) { 156 getLogger().debug("Failed to evaluate '" + str + "'", e); 157 } 158 159 return null; 161 } 162 } 163 164 172 public NodeList selectNodeList(Node contextNode, String str, PrefixResolver resolver) 173 { 174 try 175 { 176 final XObject result = XPathAPI.eval( contextNode, str, new XalanResolver(resolver) ); 177 return result.nodelist(); 178 } 179 catch( final TransformerException e ) 180 { 181 if (getLogger().isDebugEnabled()) { 182 getLogger().debug("Failed to evaluate '" + str + "'", e); 183 } 184 185 return new EmptyNodeList(); 187 } 188 } 189 190 193 private final class XalanResolver implements org.apache.xml.utils.PrefixResolver { 194 private final PrefixResolver resolver; 195 196 public XalanResolver(PrefixResolver resolver) { 197 this.resolver = resolver; 198 } 199 200 public String getNamespaceForPrefix(String prefix) 201 { 202 return resolver.prefixToNamespace(prefix); 203 } 204 205 public String getNamespaceForPrefix(String prefix, Node context) 206 { 207 return resolver.prefixToNamespace(prefix); 208 } 209 210 public String getBaseIdentifier() 211 { 212 return m_baseURI; 213 } 214 215 public boolean handlesNullPrefixes() 216 { 217 return false; 218 } 219 } 220 } 221 | Popular Tags |