1 16 19 20 21 package com.sun.org.apache.xpath.internal.domapi; 22 23 import javax.xml.transform.TransformerException ; 24 25 import com.sun.org.apache.xpath.internal.XPath; 26 import com.sun.org.apache.xpath.internal.XPathContext; 27 import com.sun.org.apache.xpath.internal.objects.XObject; 28 import com.sun.org.apache.xpath.internal.res.XPATHErrorResources; 29 import com.sun.org.apache.xpath.internal.res.XPATHMessages; 30 import org.w3c.dom.DOMException ; 31 import org.w3c.dom.Document ; 32 import org.w3c.dom.Node ; 33 import org.w3c.dom.xpath.XPathException; 34 import org.w3c.dom.xpath.XPathExpression; 35 import org.w3c.dom.xpath.XPathNamespace; 36 37 51 class XPathExpressionImpl implements XPathExpression { 52 53 56 final private XPath m_xpath; 57 58 62 final private Document m_doc; 63 64 71 XPathExpressionImpl(XPath xpath, Document doc) { 72 m_xpath = xpath; 73 m_doc = doc; 74 } 75 76 120 public Object evaluate( 121 Node contextNode, 122 short type, 123 Object result) 124 throws XPathException, DOMException { 125 126 if (m_doc != null) { 128 129 if ((contextNode != m_doc) && (!contextNode.getOwnerDocument().equals(m_doc))) { 131 String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_WRONG_DOCUMENT, null); 132 throw new DOMException (DOMException.WRONG_DOCUMENT_ERR, fmsg); 133 } 134 135 short nodeType = contextNode.getNodeType(); 137 if ((nodeType != Document.DOCUMENT_NODE) && 138 (nodeType != Document.ELEMENT_NODE) && 139 (nodeType != Document.ATTRIBUTE_NODE) && 140 (nodeType != Document.TEXT_NODE) && 141 (nodeType != Document.CDATA_SECTION_NODE) && 142 (nodeType != Document.COMMENT_NODE) && 143 (nodeType != Document.PROCESSING_INSTRUCTION_NODE) && 144 (nodeType != XPathNamespace.XPATH_NAMESPACE_NODE)) { 145 String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_WRONG_NODETYPE, null); 146 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, fmsg); 147 } 148 } 149 150 if (!XPathResultImpl.isValidType(type)) { 154 String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_INVALID_XPATH_TYPE, new Object [] {new Integer (type)}); 155 throw new XPathException(XPathException.TYPE_ERR,fmsg); } 157 158 XPathContext xpathSupport = new XPathContext(); 160 161 if (null != m_doc) { 163 xpathSupport.getDTMHandleFromNode(m_doc); 164 } 165 166 XObject xobj = null; 167 try { 168 xobj = m_xpath.execute(xpathSupport, contextNode, null); 169 } catch (TransformerException te) { 170 throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,te.getMessageAndLocation()); 172 } 173 174 return new XPathResultImpl(type,xobj,contextNode, m_xpath); 179 } 180 181 } 182 | Popular Tags |