1 /////////////////////////////////////////////////////////////////////////////////////////////// 2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules. 3 // Copyright (C) 2001-2025 the original author or authors. 4 // 5 // This library is free software; you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 2.1 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library; if not, write to the Free Software 17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 /////////////////////////////////////////////////////////////////////////////////////////////// 19 20 package com.puppycrawl.tools.checkstyle.xpath.iterators; 21 22 import java.util.LinkedList; 23 import java.util.Queue; 24 25 import net.sf.saxon.om.AxisInfo; 26 import net.sf.saxon.om.NodeInfo; 27 import net.sf.saxon.tree.iter.AxisIterator; 28 import net.sf.saxon.tree.iter.SingleNodeIterator; 29 30 /** 31 * Recursive-free implementation of the descendant axis iterator. 32 */ 33 public class DescendantIterator implements AxisIterator { 34 35 /** 36 * Enum defines starting node for iterator. 37 */ 38 public enum StartWith { 39 /** Start with current node. */ 40 CURRENT_NODE, 41 /** Omit current node and start with child nodes. */ 42 CHILDREN, 43 } 44 45 /** 46 * Queue for sibling nodes. 47 */ 48 private final Queue<NodeInfo> queue = new LinkedList<>(); 49 /** 50 * Descendant axis iterator. 51 */ 52 private AxisIterator descendantEnum; 53 54 /** 55 * Create an iterator over the "descendant" axis. 56 * 57 * @param start the initial context node. 58 * @param startWith mode of the iterator, see {@link StartWith}. 59 */ 60 public DescendantIterator(NodeInfo start, StartWith startWith) { 61 if (startWith == StartWith.CURRENT_NODE) { 62 descendantEnum = SingleNodeIterator.makeIterator(start); 63 } 64 else if (startWith == StartWith.CHILDREN) { 65 descendantEnum = start.iterateAxis(AxisInfo.CHILD); 66 } 67 } 68 69 /** 70 * Get the next item in the sequence. 71 * 72 * @return the next Item. If there are no more nodes, return null. 73 */ 74 @Override 75 public NodeInfo next() { 76 NodeInfo result = null; 77 do { 78 if (descendantEnum == null) { 79 if (queue.isEmpty()) { 80 break; 81 } 82 descendantEnum = queue.poll().iterateAxis(AxisInfo.CHILD); 83 } 84 else { 85 result = descendantEnum.next(); 86 if (result == null) { 87 descendantEnum = null; 88 } 89 } 90 } while (result == null); 91 92 if (result != null) { 93 queue.add(result); 94 } 95 return result; 96 } 97 }