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 java.util.Collections;
23 import java.util.List;
24
25 import net.sf.saxon.Configuration;
26 import net.sf.saxon.event.Receiver;
27 import net.sf.saxon.om.AtomicSequence;
28 import net.sf.saxon.om.NamespaceBinding;
29 import net.sf.saxon.om.NamespaceMap;
30 import net.sf.saxon.om.NamespaceUri;
31 import net.sf.saxon.om.NodeInfo;
32 import net.sf.saxon.om.TreeInfo;
33 import net.sf.saxon.pattern.NodePredicate;
34 import net.sf.saxon.s9api.Location;
35 import net.sf.saxon.str.UnicodeString;
36 import net.sf.saxon.tree.iter.AxisIterator;
37 import net.sf.saxon.tree.util.Navigator;
38 import net.sf.saxon.type.SchemaType;
39
40 /**
41 * Represents general class for {@code ElementNode}, {@code RootNode} and {@code AttributeNode}.
42 */
43 public abstract class AbstractNode implements NodeInfo {
44
45 /** The {@code TreeInfo} object. */
46 private final TreeInfo treeInfo;
47
48 /** The children. */
49 private List<AbstractNode> children;
50
51 /**
52 * Constructor of the abstract class {@code AbstractNode}.
53 *
54 * @param treeInfo {@code TreeInfo} object
55 */
56 protected AbstractNode(TreeInfo treeInfo) {
57 this.treeInfo = treeInfo;
58 }
59
60 /**
61 * Getter method for token type.
62 *
63 * @return token type
64 */
65 public abstract int getTokenType();
66
67 /**
68 * Returns underlying node.
69 *
70 * @return underlying node
71 */
72 public abstract Object getUnderlyingNode();
73
74 /**
75 * Getter method for node depth.
76 *
77 * @return depth
78 */
79 public abstract int getDepth();
80
81 /**
82 * Creates nodes for children.
83 *
84 * @return children list
85 */
86 protected abstract List<AbstractNode> createChildren();
87
88 /**
89 * Getter method for children.
90 *
91 * @return children list
92 */
93 protected List<AbstractNode> getChildren() {
94 if (children == null) {
95 children = createChildren();
96 }
97 return Collections.unmodifiableList(children);
98 }
99
100 /**
101 * Returns true if nodes are same, false otherwise.
102 *
103 * @param nodeInfo other node
104 * @return {@code TreeInfo}
105 */
106 @Override
107 @SuppressWarnings("ReferenceEquality")
108 public boolean isSameNodeInfo(NodeInfo nodeInfo) {
109 return this == nodeInfo;
110 }
111
112 /**
113 * Returns if implementation provides fingerprints.
114 *
115 * @return {@code boolean}
116 */
117 @Override
118 public boolean hasFingerprint() {
119 return false;
120 }
121
122 /**
123 * Get the URI part of the name of this node.
124 *
125 * @return The URI of the namespace of this node.
126 */
127 @Override
128 public NamespaceUri getNamespaceUri() {
129 return NamespaceUri.NULL;
130 }
131
132 /**
133 * Returns uri of the namespace for the current node.
134 *
135 * @return uri
136 */
137 @Override
138 public String getURI() {
139 return "";
140 }
141
142 /**
143 * Determines axis iteration algorithm.
144 *
145 * @param axisNumber element from {@code AxisInfo}
146 * @param nodeTest filter for iterator
147 * @return {@code AxisIterator} object
148 */
149 @Override
150 public AxisIterator iterateAxis(int axisNumber, NodePredicate nodeTest) {
151 AxisIterator axisIterator = iterateAxis(axisNumber);
152 if (nodeTest != null) {
153 axisIterator = new Navigator.AxisFilter(axisIterator, nodeTest);
154 }
155 return axisIterator;
156 }
157
158 /**
159 * Returns tree info.
160 *
161 * @return tree info
162 */
163 @Override
164 public final TreeInfo getTreeInfo() {
165 return treeInfo;
166 }
167
168 /**
169 * Returns string value. Throws {@code UnsupportedOperationException}, because no child
170 * class implements it and this method is not used for querying.
171 *
172 * @return string value
173 */
174 @Override
175 public String getStringValue() {
176 throw createUnsupportedOperationException();
177 }
178
179 /**
180 * Returns namespace array. Throws {@code UnsupportedOperationException}, because no child
181 * class implements it and this method is not used for querying.
182 *
183 * @param namespaceBindings namespace array
184 * @return namespace array
185 */
186 @Override
187 public final NamespaceBinding[] getDeclaredNamespaces(NamespaceBinding[] namespaceBindings) {
188 throw createUnsupportedOperationException();
189 }
190
191 /**
192 * Returns namespace array. Throws {@code UnsupportedOperationException}, because no child
193 * class implements it and this method is not used for querying.
194 *
195 * @return namespace map
196 */
197 @Override
198 public NamespaceMap getAllNamespaces() {
199 throw createUnsupportedOperationException();
200 }
201
202 /**
203 * Returns boolean. Throws {@code UnsupportedOperationException}, because no child
204 * class implements it and this method is not used for querying.
205 *
206 * @return boolean
207 */
208 @Override
209 public final boolean isId() {
210 throw createUnsupportedOperationException();
211 }
212
213 /**
214 * Returns boolean. Throws {@code UnsupportedOperationException}, because no child
215 * class implements it and this method is not used for querying.
216 *
217 * @return boolean
218 */
219 @Override
220 public final boolean isIdref() {
221 throw createUnsupportedOperationException();
222 }
223
224 /**
225 * Returns boolean. Throws {@code UnsupportedOperationException}, because no child
226 * class implements it and this method is not used for querying.
227 *
228 * @return boolean
229 */
230 @Override
231 public final boolean isNilled() {
232 throw createUnsupportedOperationException();
233 }
234
235 /**
236 * Returns boolean. Throws {@code UnsupportedOperationException}, because no child
237 * class implements it and this method is not used for querying.
238 *
239 * @return boolean
240 */
241 @Override
242 public final boolean isStreamed() {
243 throw createUnsupportedOperationException();
244 }
245
246 /**
247 * Returns configuration. Throws {@code UnsupportedOperationException}, because no child
248 * class implements it and this method is not used for querying.
249 *
250 * @return configuration
251 */
252 @Override
253 public final Configuration getConfiguration() {
254 throw createUnsupportedOperationException();
255 }
256
257 /**
258 * Sets system id. Throws {@code UnsupportedOperationException}, because no child
259 * class implements it and this method is not used for querying.
260 *
261 * @param systemId system id
262 */
263 @Override
264 public final void setSystemId(String systemId) {
265 throw createUnsupportedOperationException();
266 }
267
268 /**
269 * Returns system id. Throws {@code UnsupportedOperationException}, because no child
270 * class implements it and this method is not used for querying.
271 *
272 * @return system id
273 */
274 @Override
275 public final String getSystemId() {
276 throw createUnsupportedOperationException();
277 }
278
279 /**
280 * Returns public id. Throws {@code UnsupportedOperationException}, because no child
281 * class implements it and this method is not used for querying.
282 *
283 * @return public id
284 */
285 @Override
286 public final String getPublicId() {
287 throw createUnsupportedOperationException();
288 }
289
290 /**
291 * Returns base uri. Throws {@code UnsupportedOperationException}, because no child
292 * class implements it and this method is not used for querying.
293 *
294 * @return base uri
295 */
296 @Override
297 public final String getBaseURI() {
298 throw createUnsupportedOperationException();
299 }
300
301 /**
302 * Returns location. Throws {@code UnsupportedOperationException}, because no child
303 * class implements it and this method is not used for querying.
304 *
305 * @return location
306 */
307 @Override
308 public final Location saveLocation() {
309 throw createUnsupportedOperationException();
310 }
311
312 /**
313 * Returns the value of the item as a Unicode string.
314 * Throws {@code UnsupportedOperationException}, because no child class implements it and
315 * this method is not used for querying.
316 *
317 * @return CharSequence string value
318 */
319 @Override
320 public final UnicodeString getUnicodeStringValue() {
321 throw createUnsupportedOperationException();
322 }
323
324 /**
325 * Returns fingerprint. Throws {@code UnsupportedOperationException}, because no child
326 * class implements it and this method is not used for querying.
327 *
328 * @return fingerprint
329 */
330 @Override
331 public final int getFingerprint() {
332 throw createUnsupportedOperationException();
333 }
334
335 /**
336 * Returns display name. Throws {@code UnsupportedOperationException}, because no child
337 * class implements it and this method is not used for querying.
338 *
339 * @return display name
340 */
341 @Override
342 public final String getDisplayName() {
343 throw createUnsupportedOperationException();
344 }
345
346 /**
347 * Returns prefix. Throws {@code UnsupportedOperationException}, because no child
348 * class implements it and this method is not used for querying.
349 *
350 * @return prefix
351 */
352 @Override
353 public final String getPrefix() {
354 throw createUnsupportedOperationException();
355 }
356
357 /**
358 * Returns type of the schema. Throws {@code UnsupportedOperationException}, because no child
359 * class implements it and this method is not used for querying.
360 *
361 * @return type of the schema
362 */
363 @Override
364 public final SchemaType getSchemaType() {
365 throw createUnsupportedOperationException();
366 }
367
368 /**
369 * Returns AtomicSequence. Throws {@code UnsupportedOperationException}, because no child
370 * class implements it and this method is not used for querying.
371 *
372 * @return AtomicSequence
373 */
374 @Override
375 public final AtomicSequence atomize() {
376 throw createUnsupportedOperationException();
377 }
378
379 /**
380 * Generate id method. Throws {@code UnsupportedOperationException}, because no child
381 * class implements it and this method is not used for querying.
382 *
383 * @param buffer buffer
384 */
385 @Override
386 public final void generateId(StringBuilder buffer) {
387 throw createUnsupportedOperationException();
388 }
389
390 /**
391 * Copy method. Throws {@code UnsupportedOperationException}, because no child
392 * class implements it and this method is not used for querying.
393 *
394 * @param receiver receiver
395 * @param index index
396 * @param location location
397 */
398 @Override
399 public final void copy(Receiver receiver, int index, Location location) {
400 throw createUnsupportedOperationException();
401 }
402
403 /**
404 * Returns UnsupportedOperationException exception. Methods which throws this exception are
405 * not supported for all nodes.
406 *
407 * @return UnsupportedOperationException exception
408 */
409 private static UnsupportedOperationException createUnsupportedOperationException() {
410 return new UnsupportedOperationException("Operation is not supported");
411 }
412
413 }