1 16 19 20 21 package org.apache.xpath.domapi; 22 23 import javax.xml.transform.TransformerException ; 24 25 import org.apache.xalan.res.XSLMessages; 26 import org.apache.xml.utils.PrefixResolver; 27 import org.apache.xpath.XPath; 28 import org.apache.xpath.XPathContext; 29 import org.apache.xpath.objects.XObject; 30 import org.apache.xpath.res.XPATHErrorResources; 31 32 import org.w3c.dom.DOMException ; 33 import org.w3c.dom.Document ; 34 import org.w3c.dom.Node ; 35 import org.w3c.dom.xpath.XPathException; 36 import org.w3c.dom.xpath.XPathExpression; 37 import org.w3c.dom.xpath.XPathNamespace; 38 39 51 public class XPathExpressionImpl implements XPathExpression { 52 53 private PrefixResolver m_resolver; 54 55 58 private XPath m_xpath; 59 60 64 private Document m_doc = null; 65 66 73 XPathExpressionImpl(XPath xpath, Document doc) { 74 m_xpath = xpath; 75 m_doc = doc; 76 } 77 78 122 public Object evaluate( 123 Node contextNode, 124 short type, 125 Object result) 126 throws XPathException, DOMException { 127 128 if (m_doc != null) { 130 131 if ((contextNode != m_doc) && (!contextNode.getOwnerDocument().equals(m_doc))) { 133 String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_WRONG_DOCUMENT, null); 134 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, fmsg); 135 } 136 137 short nodeType = contextNode.getNodeType(); 139 if ((nodeType != Document.DOCUMENT_NODE) && 140 (nodeType != Document.ELEMENT_NODE) && 141 (nodeType != Document.ATTRIBUTE_NODE) && 142 (nodeType != Document.TEXT_NODE) && 143 (nodeType != Document.CDATA_SECTION_NODE) && 144 (nodeType != Document.COMMENT_NODE) && 145 (nodeType != Document.PROCESSING_INSTRUCTION_NODE) && 146 (nodeType != XPathNamespace.XPATH_NAMESPACE_NODE)) { 147 String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_WRONG_NODETYPE, null); 148 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, fmsg); 149 } 150 } 151 152 if (!XPathResultImpl.isValidType(type)) { 156 String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_INVALID_XPATH_TYPE, new Object [] {new Integer (type)}); 157 throw new XPathException(XPathException.TYPE_ERR,fmsg); } 159 160 XPathContext xpathSupport = new XPathContext(); 162 163 if (null != m_doc) { 165 xpathSupport.getDTMHandleFromNode(m_doc); 166 } 167 168 XObject xobj = null; 169 try { 170 xobj = m_xpath.execute(xpathSupport, contextNode, m_resolver ); 171 } catch (TransformerException te) { 172 throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,te.getMessageAndLocation()); 174 } 175 176 return new XPathResultImpl(type,xobj,contextNode); 181 } 182 183 } 184 | Popular Tags |