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