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;
21
22 import com.puppycrawl.tools.checkstyle.xpath.iterators.DescendantIterator;
23 import net.sf.saxon.Configuration;
24 import net.sf.saxon.om.GNode;
25 import net.sf.saxon.om.GenericTreeInfo;
26 import net.sf.saxon.om.NamespaceUri;
27 import net.sf.saxon.om.NodeInfo;
28 import net.sf.saxon.om.SequenceIterator;
29 import net.sf.saxon.pattern.nodetest.NodePredicate;
30 import net.sf.saxon.tree.iter.ArrayIterator;
31 import net.sf.saxon.tree.iter.EmptyIterator;
32 import net.sf.saxon.tree.iter.SingletonIterator;
33 import net.sf.saxon.type.Type;
34
35 /**
36 * Represents root node of Xpath-tree.
37 */
38 public abstract class AbstractRootNode extends AbstractNode {
39
40 /** Name of the root element. */
41 private static final String ROOT_NAME = "ROOT";
42
43 /** Constant for optimization. */
44 private static final AbstractNode[] EMPTY_ABSTRACT_NODE_ARRAY = new AbstractNode[0];
45
46 /**
47 * Creates a new {@code AbstractRootNode} instance.
48 */
49 protected AbstractRootNode() {
50 super(new GenericTreeInfo(Configuration.newConfiguration()));
51 }
52
53 /**
54 * Compares current object with specified for order.
55 * Throws {@code UnsupportedOperationException} because functionality not required here.
56 *
57 * @param other another {@code GNode} object
58 * @return number representing order of current object to specified one
59 */
60 @Override
61 public int compareOrder(GNode other) {
62 throw throwUnsupportedOperationException();
63 }
64
65 /**
66 * Returns attribute value. Throws {@code UnsupportedOperationException} because root node has
67 * no attributes.
68 *
69 * @param namespace namespace
70 * @param localPart actual name of the attribute
71 * @return attribute value
72 */
73 @Override
74 public String getAttributeValue(NamespaceUri namespace, String localPart) {
75 throw throwUnsupportedOperationException();
76 }
77
78 /**
79 * Returns local part.
80 *
81 * @return local part
82 */
83 @Override
84 public String getLocalPart() {
85 return ROOT_NAME;
86 }
87
88 /**
89 * Returns type of the node.
90 *
91 * @return node kind
92 */
93 @Override
94 public int getNodeKind() {
95 return Type.DOCUMENT;
96 }
97
98 /**
99 * Returns parent.
100 *
101 * @return parent
102 */
103 @Override
104 public NodeInfo getParent() {
105 return null;
106 }
107
108 /**
109 * Returns root of the tree.
110 *
111 * @return root of the tree
112 */
113 @Override
114 public NodeInfo getRoot() {
115 return this;
116 }
117
118 /**
119 * Returns ancestor axis iterator. Root has no ancestors.
120 *
121 * @param predicate filter for the iterator, may be null
122 * @return empty iterator
123 */
124 @Override
125 public SequenceIterator iterateAncestorAxis(NodePredicate predicate) {
126 return createEmptyIterator();
127 }
128
129 /**
130 * Returns parent axis iterator. Root has no parent.
131 *
132 * @param predicate filter for the iterator, may be null
133 * @return empty iterator
134 */
135 @Override
136 public SequenceIterator iterateParentAxis(NodePredicate predicate) {
137 return createEmptyIterator();
138 }
139
140 /**
141 * Returns following axis iterator. Root has no following nodes.
142 *
143 * @param predicate filter for the iterator, may be null
144 * @return empty iterator
145 */
146 @Override
147 public SequenceIterator iterateFollowingAxis(NodePredicate predicate) {
148 return createEmptyIterator();
149 }
150
151 /**
152 * Returns following-sibling axis iterator. Root has no siblings.
153 *
154 * @param predicate filter for the iterator, may be null
155 * @return empty iterator
156 */
157 @Override
158 public SequenceIterator iterateFollowingSiblingAxis(NodePredicate predicate) {
159 return createEmptyIterator();
160 }
161
162 /**
163 * Returns preceding axis iterator. Root has no preceding nodes.
164 *
165 * @param predicate filter for the iterator, may be null
166 * @return empty iterator
167 */
168 @Override
169 public SequenceIterator iteratePrecedingAxis(NodePredicate predicate) {
170 return createEmptyIterator();
171 }
172
173 /**
174 * Returns preceding-sibling axis iterator. Root has no siblings.
175 *
176 * @param predicate filter for the iterator, may be null
177 * @return empty iterator
178 */
179 @Override
180 public SequenceIterator iteratePrecedingSiblingAxis(NodePredicate predicate) {
181 return createEmptyIterator();
182 }
183
184 /**
185 * Returns ancestor-or-self axis iterator.
186 *
187 * @param predicate filter for the iterator, may be null
188 * @return iterator over the current node
189 */
190 @Override
191 public SequenceIterator iterateAncestorOrSelfAxis(NodePredicate predicate) {
192 return filter(SingletonIterator.makeIterator(this), predicate);
193 }
194
195 /**
196 * Returns self axis iterator.
197 *
198 * @param predicate filter for the iterator, may be null
199 * @return iterator over the current node
200 */
201 @Override
202 public SequenceIterator iterateSelfAxis(NodePredicate predicate) {
203 return filter(SingletonIterator.makeIterator(this), predicate);
204 }
205
206 /**
207 * Returns child axis iterator.
208 *
209 * @param predicate filter for the iterator, may be null
210 * @return iterator over the children
211 */
212 @Override
213 public SequenceIterator iterateChildAxis(NodePredicate predicate) {
214 final SequenceIterator result;
215 if (hasChildNodes()) {
216 result = filter(new ArrayIterator.OfNodes<>(
217 getChildren().toArray(EMPTY_ABSTRACT_NODE_ARRAY)), predicate);
218 }
219 else {
220 result = createEmptyIterator();
221 }
222 return result;
223 }
224
225 /**
226 * Returns descendant axis iterator.
227 *
228 * @param predicate filter for the iterator, may be null
229 * @return iterator over the descendants
230 */
231 @Override
232 public SequenceIterator iterateDescendantAxis(NodePredicate predicate) {
233 final SequenceIterator result;
234 if (hasChildNodes()) {
235 result = filter(
236 new DescendantIterator(this, DescendantIterator.StartWith.CHILDREN), predicate);
237 }
238 else {
239 result = createEmptyIterator();
240 }
241 return result;
242 }
243
244 /**
245 * Returns descendant-or-self axis iterator.
246 *
247 * @param predicate filter for the iterator, may be null
248 * @return iterator over the current node and its descendants
249 */
250 @Override
251 public SequenceIterator iterateDescendantOrSelfAxis(NodePredicate predicate) {
252 return filter(
253 new DescendantIterator(this, DescendantIterator.StartWith.CURRENT_NODE), predicate);
254 }
255
256 /**
257 * Attribute axis is not supported for the root node.
258 *
259 * @param predicate filter for the iterator, may be null
260 * @return never
261 */
262 @Override
263 public SequenceIterator iterateAttributeAxis(NodePredicate predicate) {
264 throw throwUnsupportedOperationException();
265 }
266
267 /**
268 * Namespace axis is not supported for the root node.
269 *
270 * @param predicate filter for the iterator, may be null
271 * @return never
272 */
273 @Override
274 public SequenceIterator iterateNamespaceAxis(NodePredicate predicate) {
275 throw throwUnsupportedOperationException();
276 }
277
278 /**
279 * Getter method for node depth.
280 *
281 * @return always {@code 0}
282 */
283 @Override
284 public int getDepth() {
285 return 0;
286 }
287
288 /**
289 * Creates an iterator without items.
290 *
291 * @return the shared empty iterator
292 */
293 private static SequenceIterator createEmptyIterator() {
294 return EmptyIterator.getInstance();
295 }
296
297 /**
298 * Returns UnsupportedOperationException exception.
299 *
300 * @return UnsupportedOperationException exception
301 */
302 private static UnsupportedOperationException throwUnsupportedOperationException() {
303 return new UnsupportedOperationException("Operation is not supported");
304 }
305
306 }