📄 nodefilterlocator.java
字号:
// Copyright 1997-1999 by softwarebuero m&b (SMB). All rights reserved.//// You can redistribute this software and/or modify it under the terms of// the Ozone Library License version 1 published by softwarebuero m&b (SMB).//// $Id: NodeFilterLocator.java,v 1.3 2000/10/28 16:55:25 daniela Exp $package org.ozoneDB.xml.xpath;import java.util.Stack;import java.util.Vector;import org.w3c.dom.Document;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.w3c.dom.traversal.DocumentTraversal;import org.w3c.dom.traversal.NodeFilter;import org.w3c.dom.traversal.TreeWalker;import org.apache.xalan.xpath.*;import org.apache.xalan.xpath.xml.*;/** * * This class only compiles if you have a working Xalan 1.0.1 in your classpath! * * * @version $Revision: 1.3 $ $Date: 2000/10/28 16:55:25 $ * @author <a href="http://www.softwarebuero.de">SMB</a> */public class NodeFilterLocator extends SimpleNodeLocator { protected TreeWalker walker = null; protected Stack walkerStack; protected NodeFilter filter; protected DocumentTraversal traversal; /** The singleton instance of this class.*/ private static NodeFilterLocator m_locater = null; public NodeFilterLocator( DocumentTraversal traversal, NodeFilter filter ) { this.traversal = traversal; this.filter = filter; this.walkerStack = new Stack(); } public NodeFilterLocator() { } public static XLocator getDefaultLocator() { m_locater = null == m_locater ? new NodeFilterLocator() : m_locater; return m_locater; } /** need overwrite this method, to build a treewalker stack */ protected MutableNodeList step( XPath xpath, XPathSupport execContext, Node context, int opPos, NodeCallback callback, Object callbackInfo, boolean isSimpleFollowing, boolean stopAtFirst ) throws org.xml.sax.SAXException { // cerate a new walker with current context node walker = traversal.createTreeWalker( context, NodeFilter.SHOW_ALL, filter, true ); walkerStack.push( walker ); MutableNodeList result = super.step( xpath, execContext, context, opPos, callback, callbackInfo, isSimpleFollowing, stopAtFirst ); walker = (TreeWalker)walkerStack.pop(); return result; } /** need to overwrite this method, to call traversal methods */ protected MutableNodeList findChildren( XPath xpath, XPathSupport execContext, Node context, int opPos, int stepType, MutableNodeList subQueryResults, NodeCallback callback, Object callbackInfo, boolean isSimpleFollowing, boolean stopAtFirst ) throws org.xml.sax.SAXException { int argLen = xpath.getArgLengthOfStep( opPos ); opPos = xpath.getFirstChildPosOfStep( opPos ); // If using a document fragment with references (non-standard) // we can not use the next-sibling business at the top level. if (Node.DOCUMENT_FRAGMENT_NODE != context.getNodeType()) { // DOM2-Traversal Node c = walker.firstChild(); while (null != c) { if (XPath.MATCH_SCORE_NONE != nodeTest( xpath, execContext, c, opPos, argLen, stepType )) { // or else call execute method. If no execute method, // add the node. // subQueryResults.addNode(c); if (isSimpleFollowing && null != callback) { execContext.incrementContextNodePosition( c ); if (predicate( xpath, execContext, c, opPos + argLen )) { callback.processLocatedNode( execContext, c, callbackInfo ); if (stopAtFirst) { break; } } } else { subQueryResults = addNode( subQueryResults, c ); } } // DOM2-Traversal c = walker.nextSibling(); } } else { // DOM2-Traversal Node c = walker.firstChild(); while (null != c) { if (XPath.MATCH_SCORE_NONE != nodeTest( xpath, execContext, c, opPos, argLen, stepType )) { // subQueryResults.addNode(c); if (isSimpleFollowing && null != callback) { execContext.incrementContextNodePosition( c ); if (predicate( xpath, execContext, c, opPos + argLen )) { callback.processLocatedNode( execContext, c, callbackInfo ); if (stopAtFirst) { break; } } } else { subQueryResults = addNode( subQueryResults, c ); } } // DOM2-Traversal c = walker.nextSibling(); } } return subQueryResults; } /** need overwrite this method, to call traversal methods */ protected MutableNodeList findDescendants( XPath xpath, XPathSupport execContext, Node context, int opPos, int stepType, MutableNodeList subQueryResults, NodeCallback callback, Object callbackInfo, boolean isSimpleFollowing, boolean stopAtFirst ) throws org.xml.sax.SAXException { // return super.findDescendants (xpath, execContext, context, opPos, stepType, // subQueryResults, callback, callbackInfo, isSimpleFollowing, stopAtFirst); int argLen = xpath.getArgLengthOfStep( opPos ); opPos = xpath.getFirstChildPosOfStep( opPos ); Node savedNode = walker.getCurrentNode(); Node pos = savedNode; // If using a document fragment with references (non-standard) // we can not use the next-sibling business at the top level. if (Node.DOCUMENT_FRAGMENT_NODE != context.getNodeType()) { while (null != pos) { if (stepType == XPath.FROM_DESCENDANTS_OR_SELF || context != pos) { if (XPath.MATCH_SCORE_NONE != nodeTest( xpath, execContext, pos, opPos, argLen, stepType )) { // subQueryResults.addNode(pos); if (isSimpleFollowing && null != callback) { execContext.incrementContextNodePosition( pos ); if (predicate( xpath, execContext, pos, opPos + argLen )) { callback.processLocatedNode( execContext, pos, callbackInfo ); if (stopAtFirst) { break; } } } else { subQueryResults = addNode( subQueryResults, pos ); } } } // Node nextNode = pos.getFirstChild(); Node nextNode = walker.firstChild(); while (null == nextNode) { if (context.equals( pos )) { break; } // nextNode = pos.getNextSibling (); nextNode = walker.nextSibling(); if (null == nextNode) { // pos = pos.getParentNode(); pos = walker.parentNode(); if (pos == null || context.equals( pos )) { nextNode = null; break; } } } pos = nextNode; } } else { NodeList children = context.getChildNodes(); int n = children.getLength(); for (int i = 0; i < n; i++) { pos = children.item( i ); context = pos; while (null != pos) { if (stepType == XPath.FROM_DESCENDANTS_OR_SELF || !context.equals( pos )) { if (XPath.MATCH_SCORE_NONE != nodeTest( xpath, execContext, pos, opPos, argLen, stepType )) { if (isSimpleFollowing && null != callback) { execContext.incrementContextNodePosition( pos ); if (predicate( xpath, execContext, pos, opPos + argLen )) { callback.processLocatedNode( execContext, pos, callbackInfo ); if (stopAtFirst) { break; } } } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -