1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2019 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.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import com.puppycrawl.tools.checkstyle.api.DetailAST;
27  import net.sf.saxon.Configuration;
28  import net.sf.saxon.event.Receiver;
29  import net.sf.saxon.expr.parser.Location;
30  import net.sf.saxon.om.AtomicSequence;
31  import net.sf.saxon.om.NamespaceBinding;
32  import net.sf.saxon.om.NodeInfo;
33  import net.sf.saxon.om.TreeInfo;
34  import net.sf.saxon.pattern.NodeTest;
35  import net.sf.saxon.tree.iter.AxisIterator;
36  import net.sf.saxon.tree.util.FastStringBuffer;
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   */
44  public abstract class AbstractNode implements NodeInfo {
45  
46      /** The children. */
47      private final List<AbstractNode> children = new ArrayList<>();
48  
49      /** The {@code TreeInfo} object. */
50      private final TreeInfo treeInfo;
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       * @return token type
64       */
65      public abstract int getTokenType();
66  
67      /**
68       * Returns underlying node.
69       * @return underlying node
70       */
71      public abstract DetailAST getUnderlyingNode();
72  
73      /**
74       * Getter method for children.
75       * @return children list
76       */
77      protected List<AbstractNode> getChildren() {
78          return Collections.unmodifiableList(children);
79      }
80  
81      /**
82       * Add new child node to children list.
83       * @param node child node
84       */
85      protected void addChild(AbstractNode node) {
86          children.add(node);
87      }
88  
89      /**
90       * Returns true if nodes are same, false otherwise.
91       * @param nodeInfo other node
92       * @return {@code TreeInfo}
93       */
94      @Override
95      public boolean isSameNodeInfo(NodeInfo nodeInfo) {
96          return this == nodeInfo;
97      }
98  
99      /**
100      * Returns if implementation provides fingerprints.
101      * @return {@code boolean}
102      */
103     @Override
104     public boolean hasFingerprint() {
105         return false;
106     }
107 
108     /**
109      * Returns uri of the namespace for the current node.
110      * @return uri
111      */
112     @Override
113     public String getURI() {
114         return "";
115     }
116 
117     /**
118      * Returns if current node has children.
119      * @return if current node has children
120      */
121     @Override
122     public boolean hasChildNodes() {
123         return !children.isEmpty();
124     }
125 
126     /**
127      * Determines axis iteration algorithm.
128      * @param axisNumber element from {@code AxisInfo}
129      * @param nodeTest filter for iterator
130      * @return {@code AxisIterator} object
131      */
132     @Override
133     public AxisIterator iterateAxis(byte axisNumber, NodeTest nodeTest) {
134         AxisIterator axisIterator = iterateAxis(axisNumber);
135         if (nodeTest != null) {
136             axisIterator = new Navigator.AxisFilter(axisIterator, nodeTest);
137         }
138         return axisIterator;
139     }
140 
141     /**
142      * Compares current object with specified for order.
143      * @param nodeInfo another {@code NodeInfo} object
144      * @return number representing order of current object to specified one
145      */
146     @Override
147     public int compareOrder(NodeInfo nodeInfo) {
148         return getLocalPart().compareTo(nodeInfo.getLocalPart());
149     }
150 
151     /**
152      * Returns tree info.
153      * @return tree info
154      */
155     @Override
156     public final TreeInfo getTreeInfo() {
157         return treeInfo;
158     }
159 
160     /**
161      * Returns namespace array. Throws {@code UnsupportedOperationException}, because no child
162      * class implements it and this method is not used for querying.
163      * @param namespaceBindings namespace array
164      * @return namespace array
165      */
166     @Override
167     public final NamespaceBinding[] getDeclaredNamespaces(NamespaceBinding[] namespaceBindings) {
168         throw throwUnsupportedOperationException();
169     }
170 
171     /**
172      * Returns boolean. Throws {@code UnsupportedOperationException}, because no child
173      * class implements it and this method is not used for querying.
174      * @return boolean
175      */
176     @Override
177     public final boolean isId() {
178         throw throwUnsupportedOperationException();
179     }
180 
181     /**
182      * Returns boolean. Throws {@code UnsupportedOperationException}, because no child
183      * class implements it and this method is not used for querying.
184      * @return boolean
185      */
186     @Override
187     public final boolean isIdref() {
188         throw throwUnsupportedOperationException();
189     }
190 
191     /**
192      * Returns boolean. Throws {@code UnsupportedOperationException}, because no child
193      * class implements it and this method is not used for querying.
194      * @return boolean
195      */
196     @Override
197     public final boolean isNilled() {
198         throw throwUnsupportedOperationException();
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      * @return boolean
205      */
206     @Override
207     public final boolean isStreamed() {
208         throw throwUnsupportedOperationException();
209     }
210 
211     /**
212      * Returns configuration. Throws {@code UnsupportedOperationException}, because no child
213      * class implements it and this method is not used for querying.
214      * @return configuration
215      */
216     @Override
217     public final Configuration getConfiguration() {
218         throw throwUnsupportedOperationException();
219     }
220 
221     /**
222      * Sets system id. Throws {@code UnsupportedOperationException}, because no child
223      * class implements it and this method is not used for querying.
224      * @param systemId system id
225      */
226     @Override
227     public final void setSystemId(String systemId) {
228         throw throwUnsupportedOperationException();
229     }
230 
231     /**
232      * Returns system id. Throws {@code UnsupportedOperationException}, because no child
233      * class implements it and this method is not used for querying.
234      * @return system id
235      */
236     @Override
237     public final String getSystemId() {
238         throw throwUnsupportedOperationException();
239     }
240 
241     /**
242      * Returns public id. Throws {@code UnsupportedOperationException}, because no child
243      * class implements it and this method is not used for querying.
244      * @return public id
245      */
246     @Override
247     public final String getPublicId() {
248         throw throwUnsupportedOperationException();
249     }
250 
251     /**
252      * Returns base uri. Throws {@code UnsupportedOperationException}, because no child
253      * class implements it and this method is not used for querying.
254      * @return base uri
255      */
256     @Override
257     public final String getBaseURI() {
258         throw throwUnsupportedOperationException();
259     }
260 
261     /**
262      * Returns location. Throws {@code UnsupportedOperationException}, because no child
263      * class implements it and this method is not used for querying.
264      * @return location
265      */
266     @Override
267     public final Location saveLocation() {
268         throw throwUnsupportedOperationException();
269     }
270 
271     /**
272      * Returns CharSequence string value. Throws {@code UnsupportedOperationException},
273      * because no child class implements it and this method is not used for querying.
274      * @return CharSequence string value
275      */
276     @Override
277     public final CharSequence getStringValueCS() {
278         throw throwUnsupportedOperationException();
279     }
280 
281     /**
282      * Returns fingerprint. Throws {@code UnsupportedOperationException}, because no child
283      * class implements it and this method is not used for querying.
284      * @return fingerprint
285      */
286     @Override
287     public final int getFingerprint() {
288         throw throwUnsupportedOperationException();
289     }
290 
291     /**
292      * Returns display name. Throws {@code UnsupportedOperationException}, because no child
293      * class implements it and this method is not used for querying.
294      * @return display name
295      */
296     @Override
297     public final String getDisplayName() {
298         throw throwUnsupportedOperationException();
299     }
300 
301     /**
302      * Returns prefix. Throws {@code UnsupportedOperationException}, because no child
303      * class implements it and this method is not used for querying.
304      * @return prefix
305      */
306     @Override
307     public final String getPrefix() {
308         throw throwUnsupportedOperationException();
309     }
310 
311     /**
312      * Returns type of the schema. Throws {@code UnsupportedOperationException}, because no child
313      * class implements it and this method is not used for querying.
314      * @return type of the schema
315      */
316     @Override
317     public final SchemaType getSchemaType() {
318         throw throwUnsupportedOperationException();
319     }
320 
321     /**
322      * Returns AtomicSequence. Throws {@code UnsupportedOperationException}, because no child
323      * class implements it and this method is not used for querying.
324      * @return AtomicSequence
325      */
326     @Override
327     public final AtomicSequence atomize() {
328         throw throwUnsupportedOperationException();
329     }
330 
331     /**
332      * Generate id method. Throws {@code UnsupportedOperationException}, because no child
333      * class implements it and this method is not used for querying.
334      * @param fastStringBuffer fastStringBuffer
335      */
336     @Override
337     public final void generateId(FastStringBuffer fastStringBuffer) {
338         throw throwUnsupportedOperationException();
339     }
340 
341     /**
342      * Copy method. Throws {@code UnsupportedOperationException}, because no child
343      * class implements it and this method is not used for querying.
344      * @param receiver receiver
345      * @param index index
346      * @param location location
347      */
348     @Override
349     public final void copy(Receiver receiver, int index, Location location) {
350         throw throwUnsupportedOperationException();
351     }
352 
353     /**
354      * Returns UnsupportedOperationException exception. Methods which throws this exception are
355      * not supported for all nodes.
356      * @return UnsupportedOperationException exception
357      */
358     private static UnsupportedOperationException throwUnsupportedOperationException() {
359         return new UnsupportedOperationException("Operation is not supported");
360     }
361 
362 }