View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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     public boolean isSameNodeInfo(NodeInfo nodeInfo) {
108         return this == nodeInfo;
109     }
110 
111     /**
112      * Returns if implementation provides fingerprints.
113      *
114      * @return {@code boolean}
115      */
116     @Override
117     public boolean hasFingerprint() {
118         return false;
119     }
120 
121     /**
122      * Get the URI part of the name of this node.
123      *
124      * @return The URI of the namespace of this node.
125      */
126     @Override
127     public NamespaceUri getNamespaceUri() {
128         return NamespaceUri.NULL;
129     }
130 
131     /**
132      * Returns uri of the namespace for the current node.
133      *
134      * @return uri
135      */
136     @Override
137     public String getURI() {
138         return "";
139     }
140 
141     /**
142      * Determines axis iteration algorithm.
143      *
144      * @param axisNumber element from {@code AxisInfo}
145      * @param nodeTest filter for iterator
146      * @return {@code AxisIterator} object
147      */
148     @Override
149     public AxisIterator iterateAxis(int axisNumber, NodePredicate nodeTest) {
150         AxisIterator axisIterator = iterateAxis(axisNumber);
151         if (nodeTest != null) {
152             axisIterator = new Navigator.AxisFilter(axisIterator, nodeTest);
153         }
154         return axisIterator;
155     }
156 
157     /**
158      * Returns tree info.
159      *
160      * @return tree info
161      */
162     @Override
163     public final TreeInfo getTreeInfo() {
164         return treeInfo;
165     }
166 
167     /**
168      * Returns string value. Throws {@code UnsupportedOperationException}, because no child
169      * class implements it and this method is not used for querying.
170      *
171      * @return string value
172      */
173     @Override
174     public String getStringValue() {
175         throw createUnsupportedOperationException();
176     }
177 
178     /**
179      * Returns namespace array. Throws {@code UnsupportedOperationException}, because no child
180      * class implements it and this method is not used for querying.
181      *
182      * @param namespaceBindings namespace array
183      * @return namespace array
184      */
185     @Override
186     public final NamespaceBinding[] getDeclaredNamespaces(NamespaceBinding[] namespaceBindings) {
187         throw createUnsupportedOperationException();
188     }
189 
190     /**
191      * Returns namespace array. Throws {@code UnsupportedOperationException}, because no child
192      * class implements it and this method is not used for querying.
193      *
194      * @return namespace map
195      */
196     @Override
197     public NamespaceMap getAllNamespaces() {
198         throw createUnsupportedOperationException();
199     }
200 
201     /**
202      * Returns boolean. Throws {@code UnsupportedOperationException}, because no child
203      * class implements it and this method is not used for querying.
204      *
205      * @return boolean
206      */
207     @Override
208     public final boolean isId() {
209         throw createUnsupportedOperationException();
210     }
211 
212     /**
213      * Returns boolean. Throws {@code UnsupportedOperationException}, because no child
214      * class implements it and this method is not used for querying.
215      *
216      * @return boolean
217      */
218     @Override
219     public final boolean isIdref() {
220         throw createUnsupportedOperationException();
221     }
222 
223     /**
224      * Returns boolean. Throws {@code UnsupportedOperationException}, because no child
225      * class implements it and this method is not used for querying.
226      *
227      * @return boolean
228      */
229     @Override
230     public final boolean isNilled() {
231         throw createUnsupportedOperationException();
232     }
233 
234     /**
235      * Returns boolean. Throws {@code UnsupportedOperationException}, because no child
236      * class implements it and this method is not used for querying.
237      *
238      * @return boolean
239      */
240     @Override
241     public final boolean isStreamed() {
242         throw createUnsupportedOperationException();
243     }
244 
245     /**
246      * Returns configuration. Throws {@code UnsupportedOperationException}, because no child
247      * class implements it and this method is not used for querying.
248      *
249      * @return configuration
250      */
251     @Override
252     public final Configuration getConfiguration() {
253         throw createUnsupportedOperationException();
254     }
255 
256     /**
257      * Sets system id. Throws {@code UnsupportedOperationException}, because no child
258      * class implements it and this method is not used for querying.
259      *
260      * @param systemId system id
261      */
262     @Override
263     public final void setSystemId(String systemId) {
264         throw createUnsupportedOperationException();
265     }
266 
267     /**
268      * Returns system id. Throws {@code UnsupportedOperationException}, because no child
269      * class implements it and this method is not used for querying.
270      *
271      * @return system id
272      */
273     @Override
274     public final String getSystemId() {
275         throw createUnsupportedOperationException();
276     }
277 
278     /**
279      * Returns public id. Throws {@code UnsupportedOperationException}, because no child
280      * class implements it and this method is not used for querying.
281      *
282      * @return public id
283      */
284     @Override
285     public final String getPublicId() {
286         throw createUnsupportedOperationException();
287     }
288 
289     /**
290      * Returns base uri. Throws {@code UnsupportedOperationException}, because no child
291      * class implements it and this method is not used for querying.
292      *
293      * @return base uri
294      */
295     @Override
296     public final String getBaseURI() {
297         throw createUnsupportedOperationException();
298     }
299 
300     /**
301      * Returns location. Throws {@code UnsupportedOperationException}, because no child
302      * class implements it and this method is not used for querying.
303      *
304      * @return location
305      */
306     @Override
307     public final Location saveLocation() {
308         throw createUnsupportedOperationException();
309     }
310 
311     /**
312      * Returns the value of the item as a Unicode string.
313      * Throws {@code UnsupportedOperationException}, because no child class implements it and
314      * this method is not used for querying.
315      *
316      * @return CharSequence string value
317      */
318     @Override
319     public final UnicodeString getUnicodeStringValue() {
320         throw createUnsupportedOperationException();
321     }
322 
323     /**
324      * Returns fingerprint. Throws {@code UnsupportedOperationException}, because no child
325      * class implements it and this method is not used for querying.
326      *
327      * @return fingerprint
328      */
329     @Override
330     public final int getFingerprint() {
331         throw createUnsupportedOperationException();
332     }
333 
334     /**
335      * Returns display name. Throws {@code UnsupportedOperationException}, because no child
336      * class implements it and this method is not used for querying.
337      *
338      * @return display name
339      */
340     @Override
341     public final String getDisplayName() {
342         throw createUnsupportedOperationException();
343     }
344 
345     /**
346      * Returns prefix. Throws {@code UnsupportedOperationException}, because no child
347      * class implements it and this method is not used for querying.
348      *
349      * @return prefix
350      */
351     @Override
352     public final String getPrefix() {
353         throw createUnsupportedOperationException();
354     }
355 
356     /**
357      * Returns type of the schema. Throws {@code UnsupportedOperationException}, because no child
358      * class implements it and this method is not used for querying.
359      *
360      * @return type of the schema
361      */
362     @Override
363     public final SchemaType getSchemaType() {
364         throw createUnsupportedOperationException();
365     }
366 
367     /**
368      * Returns AtomicSequence. Throws {@code UnsupportedOperationException}, because no child
369      * class implements it and this method is not used for querying.
370      *
371      * @return AtomicSequence
372      */
373     @Override
374     public final AtomicSequence atomize() {
375         throw createUnsupportedOperationException();
376     }
377 
378     /**
379      * Generate id method. Throws {@code UnsupportedOperationException}, because no child
380      * class implements it and this method is not used for querying.
381      *
382      * @param buffer buffer
383      */
384     @Override
385     public final void generateId(StringBuilder buffer) {
386         throw createUnsupportedOperationException();
387     }
388 
389     /**
390      * Copy method. Throws {@code UnsupportedOperationException}, because no child
391      * class implements it and this method is not used for querying.
392      *
393      * @param receiver receiver
394      * @param index index
395      * @param location location
396      */
397     @Override
398     public final void copy(Receiver receiver, int index, Location location) {
399         throw createUnsupportedOperationException();
400     }
401 
402     /**
403      * Returns UnsupportedOperationException exception. Methods which throws this exception are
404      * not supported for all nodes.
405      *
406      * @return UnsupportedOperationException exception
407      */
408     private static UnsupportedOperationException createUnsupportedOperationException() {
409         return new UnsupportedOperationException("Operation is not supported");
410     }
411 
412 }