View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.ArrayDeque;
23  import java.util.Queue;
24  
25  import net.sf.saxon.om.NodeInfo;
26  import net.sf.saxon.om.SequenceIterator;
27  import net.sf.saxon.tree.iter.SingletonIterator;
28  
29  /**
30   * Recursive-free implementation of the descendant axis iterator.
31   */
32  public class DescendantIterator implements SequenceIterator {
33  
34      /**
35       * Enum defines starting node for iterator.
36       */
37      public enum StartWith {
38  
39          /** Start with current node. */
40          CURRENT_NODE,
41          /** Omit current node and start with child nodes. */
42          CHILDREN,
43  
44      }
45  
46      /**
47       * Queue for sibling nodes.
48       */
49      private final Queue<NodeInfo> queue = new ArrayDeque<>();
50      /**
51       * Descendant axis iterator.
52       */
53      private SequenceIterator descendantEnum;
54  
55      /**
56       * Create an iterator over the "descendant" axis.
57       *
58       * @param start the initial context node.
59       * @param startWith mode of the iterator, see {@link StartWith}.
60       */
61      public DescendantIterator(NodeInfo start, StartWith startWith) {
62          if (startWith == StartWith.CURRENT_NODE) {
63              descendantEnum = SingletonIterator.makeIterator(start);
64          }
65          else if (startWith == StartWith.CHILDREN) {
66              descendantEnum = start.iterateChildAxis(null);
67          }
68      }
69  
70      /**
71       * Get the next item in the sequence.
72       *
73       * @return the next Item. If there are no more nodes, return null.
74       */
75      @Override
76      public NodeInfo next() {
77          NodeInfo result = null;
78          do {
79              if (descendantEnum == null) {
80                  if (queue.isEmpty()) {
81                      break;
82                  }
83                  descendantEnum = queue.poll().iterateChildAxis(null);
84              }
85              else {
86                  result = (NodeInfo) descendantEnum.next();
87                  if (result == null) {
88                      descendantEnum = null;
89                  }
90              }
91          } while (result == null);
92  
93          if (result != null) {
94              queue.add(result);
95          }
96          return result;
97      }
98  
99  }