1 16 19 20 package com.sun.org.apache.xpath.internal.domapi; 21 22 import javax.xml.transform.TransformerException ; 23 24 import com.sun.org.apache.xml.internal.utils.PrefixResolver; 25 import com.sun.org.apache.xpath.internal.XPath; 26 import com.sun.org.apache.xpath.internal.res.XPATHErrorResources; 27 import com.sun.org.apache.xpath.internal.res.XPATHMessages; 28 import org.w3c.dom.DOMException ; 29 import org.w3c.dom.Document ; 30 import org.w3c.dom.Node ; 31 import org.w3c.dom.xpath.XPathEvaluator; 32 import org.w3c.dom.xpath.XPathException; 33 import org.w3c.dom.xpath.XPathExpression; 34 import org.w3c.dom.xpath.XPathNSResolver; 35 36 57 public final class XPathEvaluatorImpl implements XPathEvaluator { 58 59 65 private class DummyPrefixResolver implements PrefixResolver { 66 67 70 DummyPrefixResolver() {} 71 72 78 public String getNamespaceForPrefix(String prefix, Node context) { 79 String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_NULL_RESOLVER, null); 80 throw new DOMException (DOMException.NAMESPACE_ERR, fmsg); } 82 83 89 public String getNamespaceForPrefix(String prefix) { 90 return getNamespaceForPrefix(prefix,null); 91 } 92 93 96 public boolean handlesNullPrefixes() { 97 return false; 98 } 99 100 103 public String getBaseIdentifier() { 104 return null; 105 } 106 107 } 108 109 113 private final Document m_doc; 114 115 121 public XPathEvaluatorImpl(Document doc) { 122 m_doc = doc; 123 } 124 125 130 public XPathEvaluatorImpl() { 131 m_doc = null; 132 } 133 134 158 public XPathExpression createExpression( 159 String expression, 160 XPathNSResolver resolver) 161 throws XPathException, DOMException { 162 163 try { 164 165 XPath xpath = new XPath(expression,null, 167 ((null == resolver) ? new DummyPrefixResolver() : ((PrefixResolver)resolver)), 168 XPath.SELECT); 169 170 return new XPathExpressionImpl(xpath, m_doc); 171 172 } catch (TransformerException e) { 173 if(e instanceof XPathStylesheetDOM3Exception) 176 throw new DOMException (DOMException.NAMESPACE_ERR,e.getMessageAndLocation()); 177 else 178 throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,e.getMessageAndLocation()); 179 180 } 181 } 182 183 199 public XPathNSResolver createNSResolver(Node nodeResolver) { 200 201 return new XPathNSResolverImpl((nodeResolver.getNodeType() == Node.DOCUMENT_NODE) 202 ? ((Document ) nodeResolver).getDocumentElement() : nodeResolver); 203 } 204 205 256 public Object evaluate( 257 String expression, 258 Node contextNode, 259 XPathNSResolver resolver, 260 short type, 261 Object result) 262 throws XPathException, DOMException { 263 264 XPathExpression xpathExpression = createExpression(expression, resolver); 265 266 return xpathExpression.evaluate(contextNode, type, result); 267 } 268 269 } 270 | Popular Tags |