📄 xpathresultimpl.java
字号:
NodeIterator result = null; try { result = m_resultObj.nodeset(); } catch (TransformerException te) { throw new XPathException(XPathException.TYPE_ERR,te.getMessage()); } if (null == result) return null; Node node = result.nextNode(); // Wrap "namespace node" in an XPathNamespace if (isNamespaceNode(node)) { return new XPathNamespaceImpl(node); } else { return node; } } /** * @see org.w3c.dom.xpath.XPathResult#getInvalidIteratorState() */ public boolean getInvalidIteratorState() { return m_isInvalidIteratorState; } /** * The number of nodes in the result snapshot. Valid values for * snapshotItem indices are <code>0</code> to * <code>snapshotLength-1</code> inclusive. * @exception XPathException * TYPE_ERR: raised if <code>resultType</code> is not * <code>UNORDERED_NODE_SNAPSHOT_TYPE</code> or * <code>ORDERED_NODE_SNAPSHOT_TYPE</code>. * * @see org.w3c.dom.xpath.XPathResult#getSnapshotLength() */ public int getSnapshotLength() throws XPathException { if ((m_resultType != UNORDERED_NODE_SNAPSHOT_TYPE) && (m_resultType != ORDERED_NODE_SNAPSHOT_TYPE)) { String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_CANT_GET_SNAPSHOT_LENGTH, new Object[] {m_xpath.getPatternString(), getTypeString(m_resultType)}); throw new XPathException(XPathException.TYPE_ERR,fmsg); // "The method getSnapshotLength cannot be called on the XPathResult of XPath expression {0} because its XPathResultType is {1}. } return m_list.getLength(); } /** * Iterates and returns the next node from the node set or * <code>null</code>if there are no more nodes. * @return Returns the next node. * @exception XPathException * TYPE_ERR: raised if <code>resultType</code> is not * <code>UNORDERED_NODE_ITERATOR_TYPE</code> or * <code>ORDERED_NODE_ITERATOR_TYPE</code>. * @exception DOMException * INVALID_STATE_ERR: The document has been mutated since the result was * returned. * @see org.w3c.dom.xpath.XPathResult#iterateNext() */ public Node iterateNext() throws XPathException, DOMException { if ((m_resultType != UNORDERED_NODE_ITERATOR_TYPE) && (m_resultType != ORDERED_NODE_ITERATOR_TYPE)) { String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_NON_ITERATOR_TYPE, new Object[] {m_xpath.getPatternString(), getTypeString(m_resultType)}); throw new XPathException(XPathException.TYPE_ERR, fmsg); // "The method iterateNext cannot be called on the XPathResult of XPath expression {0} because its XPathResultType is {1}. // This method applies only to types UNORDERED_NODE_ITERATOR_TYPE and ORDERED_NODE_ITERATOR_TYPE."}, } if (getInvalidIteratorState()) { String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_DOC_MUTATED, null); throw new DOMException(DOMException.INVALID_STATE_ERR,fmsg); // Document mutated since result was returned. Iterator is invalid. } Node node = m_iterator.nextNode(); if(null == node) removeEventListener(); // JIRA 1673 // Wrap "namespace node" in an XPathNamespace if (isNamespaceNode(node)) { return new XPathNamespaceImpl(node); } else { return node; } } /** * Returns the <code>index</code>th item in the snapshot collection. If * <code>index</code> is greater than or equal to the number of nodes in * the list, this method returns <code>null</code>. Unlike the iterator * result, the snapshot does not become invalid, but may not correspond * to the current document if it is mutated. * @param index Index into the snapshot collection. * @return The node at the <code>index</code>th position in the * <code>NodeList</code>, or <code>null</code> if that is not a valid * index. * @exception XPathException * TYPE_ERR: raised if <code>resultType</code> is not * <code>UNORDERED_NODE_SNAPSHOT_TYPE</code> or * <code>ORDERED_NODE_SNAPSHOT_TYPE</code>. * * @see org.w3c.dom.xpath.XPathResult#snapshotItem(int) */ public Node snapshotItem(int index) throws XPathException { if ((m_resultType != UNORDERED_NODE_SNAPSHOT_TYPE) && (m_resultType != ORDERED_NODE_SNAPSHOT_TYPE)) { String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_NON_SNAPSHOT_TYPE, new Object[] {m_xpath.getPatternString(), getTypeString(m_resultType)}); throw new XPathException(XPathException.TYPE_ERR, fmsg); // "The method snapshotItem cannot be called on the XPathResult of XPath expression {0} because its XPathResultType is {1}. // This method applies only to types UNORDERED_NODE_SNAPSHOT_TYPE and ORDERED_NODE_SNAPSHOT_TYPE."}, } Node node = m_list.item(index); // Wrap "namespace node" in an XPathNamespace if (isNamespaceNode(node)) { return new XPathNamespaceImpl(node); } else { return node; } } /** * Check if the specified type is one of the supported types. * @param type The specified type * * @return true If the specified type is supported; otherwise, returns false. */ static boolean isValidType( short type ) { switch (type) { case ANY_TYPE: case NUMBER_TYPE: case STRING_TYPE: case BOOLEAN_TYPE: case UNORDERED_NODE_ITERATOR_TYPE: case ORDERED_NODE_ITERATOR_TYPE: case UNORDERED_NODE_SNAPSHOT_TYPE: case ORDERED_NODE_SNAPSHOT_TYPE: case ANY_UNORDERED_NODE_TYPE: case FIRST_ORDERED_NODE_TYPE: return true; default: return false; } } /** * @see org.w3c.dom.events.EventListener#handleEvent(Event) */ public void handleEvent(Event event) { if (event.getType().equals("DOMSubtreeModified")) { // invalidate the iterator m_isInvalidIteratorState = true; // deregister as a listener to reduce computational load removeEventListener(); } } /** * Given a request type, return the equivalent string. * For diagnostic purposes. * * @return type string */ private String getTypeString(int type) { switch (type) { case ANY_TYPE: return "ANY_TYPE"; case ANY_UNORDERED_NODE_TYPE: return "ANY_UNORDERED_NODE_TYPE"; case BOOLEAN_TYPE: return "BOOLEAN"; case FIRST_ORDERED_NODE_TYPE: return "FIRST_ORDERED_NODE_TYPE"; case NUMBER_TYPE: return "NUMBER_TYPE"; case ORDERED_NODE_ITERATOR_TYPE: return "ORDERED_NODE_ITERATOR_TYPE"; case ORDERED_NODE_SNAPSHOT_TYPE: return "ORDERED_NODE_SNAPSHOT_TYPE"; case STRING_TYPE: return "STRING_TYPE"; case UNORDERED_NODE_ITERATOR_TYPE: return "UNORDERED_NODE_ITERATOR_TYPE"; case UNORDERED_NODE_SNAPSHOT_TYPE: return "UNORDERED_NODE_SNAPSHOT_TYPE"; default: return "#UNKNOWN"; } } /** * Given an XObject, determine the corresponding DOM XPath type * * @return type string */ private short getTypeFromXObject(XObject object) { switch (object.getType()) { case XObject.CLASS_BOOLEAN: return BOOLEAN_TYPE; case XObject.CLASS_NODESET: return UNORDERED_NODE_ITERATOR_TYPE; case XObject.CLASS_NUMBER: return NUMBER_TYPE; case XObject.CLASS_STRING: return STRING_TYPE; // XPath 2.0 types // case XObject.CLASS_DATE: // case XObject.CLASS_DATETIME:// case XObject.CLASS_DTDURATION:// case XObject.CLASS_GDAY:// case XObject.CLASS_GMONTH:// case XObject.CLASS_GMONTHDAY:// case XObject.CLASS_GYEAR:// case XObject.CLASS_GYEARMONTH: // case XObject.CLASS_TIME:// case XObject.CLASS_YMDURATION: return STRING_TYPE; // treat all date types as strings? case XObject.CLASS_RTREEFRAG: return UNORDERED_NODE_ITERATOR_TYPE; case XObject.CLASS_NULL: return ANY_TYPE; // throw exception ? default: return ANY_TYPE; // throw exception ? } } /** * Given a node, determine if it is a namespace node. * * @param node * * @return boolean Returns true if this is a namespace node; otherwise, returns false. */ private boolean isNamespaceNode(Node node) { if ((null != node) && (node.getNodeType() == Node.ATTRIBUTE_NODE) && (node.getNodeName().startsWith("xmlns:") || node.getNodeName().equals("xmlns"))) { return true; } else { return false; } } /** * Add m_contextNode to Event Listner to listen for Mutations Events * */ private void addEventListener(){ if(m_contextNode instanceof EventTarget) ((EventTarget)m_contextNode).addEventListener("DOMSubtreeModified",this,true); } /** * Remove m_contextNode to Event Listner to listen for Mutations Events * */private void removeEventListener(){ if(m_contextNode instanceof EventTarget) ((EventTarget)m_contextNode).removeEventListener("DOMSubtreeModified",this,true);} }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -