⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xpathresultimpl.java

📁 java1.6众多例子参考
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright 2002-2005 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//* * $Id: XPathResultImpl.java,v 1.2.4.1 2005/09/10 04:18:54 jeffsuttor Exp $ */package com.sun.org.apache.xpath.internal.domapi;import javax.xml.transform.TransformerException;import com.sun.org.apache.xpath.internal.XPath;import com.sun.org.apache.xpath.internal.objects.XObject;import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;import com.sun.org.apache.xpath.internal.res.XPATHMessages;import org.w3c.dom.DOMException;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.w3c.dom.events.Event;import org.w3c.dom.events.EventListener;import org.w3c.dom.events.EventTarget;import org.w3c.dom.traversal.NodeIterator;import org.w3c.dom.xpath.XPathException;import org.w3c.dom.xpath.XPathResult;/** * * The class provides an implementation XPathResult according  * to the DOM L3 XPath Specification, Working Group Note 26 February 2004. * * <p>See also the <a href='http://www.w3.org/TR/2004/NOTE-DOM-Level-3-XPath-20040226'>Document Object Model (DOM) Level 3 XPath Specification</a>.</p> *  * <p>The <code>XPathResult</code> interface represents the result of the  * evaluation of an XPath expression within the context of a particular  * node. Since evaluation of an XPath expression can result in various  * result types, this object makes it possible to discover and manipulate  * the type and value of the result.</p> *  * <p>This implementation wraps an <code>XObject</code>. *  * @see com.sun.org.apache.xpath.internal.objects.XObject * @see org.w3c.dom.xpath.XPathResult *  * @xsl.usage internal */class XPathResultImpl implements XPathResult, EventListener {	/**     *  The wrapped XObject     */	final private XObject m_resultObj;		/**	 * The xpath object that wraps the expression used for this result.	 */	final private XPath m_xpath;			/**     *  This the type specified by the user during construction.  Typically	 *  the constructor will be called by com.sun.org.apache.xpath.internal.XPath.evaluate().     */	final private short m_resultType; 		private boolean m_isInvalidIteratorState = false;	    /**     * Only used to attach a mutation event handler when specified     * type is an iterator type.     */	final private Node m_contextNode;		/**     *  The iterator, if this is an iterator type.     */	private NodeIterator m_iterator = null;;		/**     *  The list, if this is a snapshot type.     */	private NodeList m_list = null;		/**	 * Constructor for XPathResultImpl.     *      * For internal use only.	 */	 XPathResultImpl(short type, XObject result, Node contextNode, XPath xpath) {		// Check that the type is valid		if (!isValidType(type)) {            String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_INVALID_XPATH_TYPE, new Object[] {new Integer(type)});                   throw new XPathException(XPathException.TYPE_ERR,fmsg); // Invalid XPath type argument: {0}           		}        // Result object should never be null!        if (null == result) {            String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_EMPTY_XPATH_RESULT, null);                   throw new XPathException(XPathException.INVALID_EXPRESSION_ERR,fmsg); // Empty XPath result object        }                this.m_resultObj = result;        this.m_contextNode = contextNode;        this.m_xpath = xpath;        // If specified result was ANY_TYPE, determine XObject type        if (type == ANY_TYPE) {            this.m_resultType = getTypeFromXObject(result);        } else {            this.m_resultType = type;        }		        // If the context node supports DOM Events and the type is one of the iterator        // types register this result as an event listener        if (((m_resultType == XPathResult.ORDERED_NODE_ITERATOR_TYPE) ||            (m_resultType == XPathResult.UNORDERED_NODE_ITERATOR_TYPE))) {            	addEventListener();                        }// else can we handle iterator types if contextNode doesn't support EventTarget??    				        // If this is an iterator type get the iterator        if ((m_resultType == ORDERED_NODE_ITERATOR_TYPE) ||            (m_resultType == UNORDERED_NODE_ITERATOR_TYPE) ||            (m_resultType == ANY_UNORDERED_NODE_TYPE) ||            (m_resultType == FIRST_ORDERED_NODE_TYPE))  {                try {                m_iterator = m_resultObj.nodeset();            } catch (TransformerException te) {                // probably not a node type				String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_INCOMPATIBLE_TYPES, new Object[] {m_xpath.getPatternString(), getTypeString(getTypeFromXObject(m_resultObj)),getTypeString(m_resultType)});         			    throw new XPathException(XPathException.TYPE_ERR, fmsg);  // "The XPathResult of XPath expression {0} has an XPathResultType of {1} which cannot be coerced into the specified XPathResultType of {2}."},           }                    // If user requested ordered nodeset and result is unordered                 // need to sort...TODO    //            if ((m_resultType == ORDERED_NODE_ITERATOR_TYPE) &&    //                (!(((DTMNodeIterator)m_iterator).getDTMIterator().isDocOrdered()))) {    //     //            }            // If it's a snapshot type, get the nodelist        } else if ((m_resultType == UNORDERED_NODE_SNAPSHOT_TYPE) ||                   (m_resultType == ORDERED_NODE_SNAPSHOT_TYPE)) {            try {       	        	   m_list = m_resultObj.nodelist();            } catch (TransformerException te) {        		// probably not a node type 				String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_INCOMPATIBLE_TYPES, new Object[] {m_xpath.getPatternString(), getTypeString(getTypeFromXObject(m_resultObj)),getTypeString(m_resultType)});       				throw new XPathException(XPathException.TYPE_ERR, fmsg); // "The XPathResult of XPath expression {0} has an XPathResultType of {1} which cannot be coerced into the specified XPathResultType of {2}."},            }    	}	    	 	}	/**	 * @see org.w3c.dom.xpath.XPathResult#getResultType()	 */	public short getResultType() {		return m_resultType;	}	/**	 *  The value of this number result.     * @exception XPathException     *   TYPE_ERR: raised if <code>resultType</code> is not      *   <code>NUMBER_TYPE</code>.	 * @see org.w3c.dom.xpath.XPathResult#getNumberValue()	 */	public double getNumberValue() throws XPathException {		if (getResultType() != NUMBER_TYPE) {			String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_CANT_CONVERT_XPATHRESULTTYPE_TO_NUMBER, new Object[] {m_xpath.getPatternString(), getTypeString(m_resultType)});					throw new XPathException(XPathException.TYPE_ERR,fmsg);//		"The XPathResult of XPath expression {0} has an XPathResultType of {1} which cannot be converted to a number"		} else {			try {			   return m_resultObj.num();			} catch (Exception e) {				// Type check above should prevent this exception from occurring.				throw new XPathException(XPathException.TYPE_ERR,e.getMessage());			}		}			}	/**	 * The value of this string result.     * @exception XPathException     *   TYPE_ERR: raised if <code>resultType</code> is not      *   <code>STRING_TYPE</code>.     * 	 * @see org.w3c.dom.xpath.XPathResult#getStringValue()	 */	public String getStringValue() throws XPathException {		if (getResultType() != STRING_TYPE) {			String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_CANT_CONVERT_TO_STRING, new Object[] {m_xpath.getPatternString(), m_resultObj.getTypeString()});					throw new XPathException(XPathException.TYPE_ERR,fmsg);//		"The XPathResult of XPath expression {0} has an XPathResultType of {1} which cannot be converted to a string."		} else {			try {			   return m_resultObj.str();			} catch (Exception e) {				// Type check above should prevent this exception from occurring.				throw new XPathException(XPathException.TYPE_ERR,e.getMessage());			}		}	}	/**	 * @see org.w3c.dom.xpath.XPathResult#getBooleanValue()	 */	public boolean getBooleanValue() throws XPathException {		if (getResultType() != BOOLEAN_TYPE) {			String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_CANT_CONVERT_TO_BOOLEAN, new Object[] {m_xpath.getPatternString(), getTypeString(m_resultType)});					throw new XPathException(XPathException.TYPE_ERR,fmsg);//		"The XPathResult of XPath expression {0} has an XPathResultType of {1} which cannot be converted to a boolean."					} else {			try {			   return m_resultObj.bool();			} catch (TransformerException e) {				// Type check above should prevent this exception from occurring.				throw new XPathException(XPathException.TYPE_ERR,e.getMessage());			}		}	}	/**	 * The value of this single node result, which may be <code>null</code>.     * @exception XPathException     *   TYPE_ERR: raised if <code>resultType</code> is not      *   <code>ANY_UNORDERED_NODE_TYPE</code> or      *   <code>FIRST_ORDERED_NODE_TYPE</code>.     * 	 * @see org.w3c.dom.xpath.XPathResult#getSingleNodeValue()	 */	public Node getSingleNodeValue() throws XPathException {				if ((m_resultType != ANY_UNORDERED_NODE_TYPE) &&		    (m_resultType != FIRST_ORDERED_NODE_TYPE)) {				String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_CANT_CONVERT_TO_SINGLENODE, new Object[] {m_xpath.getPatternString(), getTypeString(m_resultType)});                        				throw new XPathException(XPathException.TYPE_ERR,fmsg);//				"The XPathResult of XPath expression {0} has an XPathResultType of {1} which cannot be converted to a single node. //				 This method applies only to types ANY_UNORDERED_NODE_TYPE and FIRST_ORDERED_NODE_TYPE."	    }	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -