1 16 19 20 package org.apache.xpath.domapi; 21 22 import javax.xml.transform.TransformerException ; 23 24 import org.apache.xalan.res.XSLMessages; 25 import org.apache.xml.utils.PrefixResolver; 26 import org.apache.xpath.XPath; 27 import org.apache.xpath.res.XPATHErrorResources; 28 29 import org.w3c.dom.DOMException ; 30 import org.w3c.dom.Document ; 31 import org.w3c.dom.Node ; 32 import org.w3c.dom.xpath.XPathEvaluator; 33 import org.w3c.dom.xpath.XPathException; 34 import org.w3c.dom.xpath.XPathExpression; 35 import org.w3c.dom.xpath.XPathNSResolver; 36 37 58 public class XPathEvaluatorImpl implements XPathEvaluator { 59 60 66 class DummyPrefixResolver implements PrefixResolver { 67 68 71 public DummyPrefixResolver() {} 72 73 79 public String getNamespaceForPrefix(String prefix, Node context) { 80 String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NULL_RESOLVER, null); 81 throw new DOMException (DOMException.NAMESPACE_ERR, fmsg); } 83 84 90 public String getNamespaceForPrefix(String prefix) { 91 return getNamespaceForPrefix(prefix,null); 92 } 93 94 97 public boolean handlesNullPrefixes() { 98 return false; 99 } 100 101 104 public String getBaseIdentifier() { 105 return null; 106 } 107 108 } 109 110 114 private Document m_doc = null; 115 116 119 public XPathEvaluatorImpl() { 120 super(); 121 } 122 123 129 public XPathEvaluatorImpl(Document doc) { 130 m_doc = doc; 131 } 132 133 157 public XPathExpression createExpression( 158 String expression, 159 XPathNSResolver resolver) 160 throws XPathException, DOMException { 161 162 try { 163 164 XPath xpath = new XPath(expression,null, 166 ((null == resolver) ? new DummyPrefixResolver() : ((PrefixResolver)resolver)), 167 XPath.SELECT); 168 169 return new XPathExpressionImpl(xpath, m_doc); 170 171 } catch (TransformerException e) { 172 throw new DOMException (XPathException.INVALID_EXPRESSION_ERR,e.getMessageAndLocation()); 173 } 174 } 175 176 192 public XPathNSResolver createNSResolver(Node nodeResolver) { 193 194 return new XPathNSResolverImpl((nodeResolver.getNodeType() == Node.DOCUMENT_NODE) 195 ? ((Document ) nodeResolver).getDocumentElement() : nodeResolver); 196 } 197 198 249 public Object evaluate( 250 String expression, 251 Node contextNode, 252 XPathNSResolver resolver, 253 short type, 254 Object result) 255 throws XPathException, DOMException { 256 257 XPathExpression xpathExpression = createExpression(expression, resolver); 258 259 return xpathExpression.evaluate(contextNode, type, result); 260 } 261 262 } 263 | Popular Tags |