001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2020 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.xpath;
021
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.List;
025
026import com.puppycrawl.tools.checkstyle.api.DetailAST;
027import net.sf.saxon.Configuration;
028import net.sf.saxon.event.Receiver;
029import net.sf.saxon.expr.parser.Location;
030import net.sf.saxon.om.AtomicSequence;
031import net.sf.saxon.om.NamespaceBinding;
032import net.sf.saxon.om.NodeInfo;
033import net.sf.saxon.om.TreeInfo;
034import net.sf.saxon.pattern.NodeTest;
035import net.sf.saxon.tree.iter.AxisIterator;
036import net.sf.saxon.tree.util.FastStringBuffer;
037import net.sf.saxon.tree.util.Navigator;
038import net.sf.saxon.type.SchemaType;
039
040/**
041 * Represents general class for {@code ElementNode}, {@code RootNode} and {@code AttributeNode}.
042 *
043 */
044public abstract class AbstractNode implements NodeInfo {
045
046    /** The children. */
047    private final List<AbstractNode> children = new ArrayList<>();
048
049    /** The {@code TreeInfo} object. */
050    private final TreeInfo treeInfo;
051
052    /**
053     * Constructor of the abstract class {@code AbstractNode}.
054     *
055     * @param treeInfo {@code TreeInfo} object
056     */
057    protected AbstractNode(TreeInfo treeInfo) {
058        this.treeInfo = treeInfo;
059    }
060
061    /**
062     * Getter method for token type.
063     * @return token type
064     */
065    public abstract int getTokenType();
066
067    /**
068     * Returns underlying node.
069     * @return underlying node
070     */
071    public abstract DetailAST getUnderlyingNode();
072
073    /**
074     * Getter method for children.
075     * @return children list
076     */
077    protected List<AbstractNode> getChildren() {
078        return Collections.unmodifiableList(children);
079    }
080
081    /**
082     * Add new child node to children list.
083     * @param node child node
084     */
085    protected void addChild(AbstractNode node) {
086        children.add(node);
087    }
088
089    /**
090     * Returns true if nodes are same, false otherwise.
091     * @param nodeInfo other node
092     * @return {@code TreeInfo}
093     */
094    @Override
095    public boolean isSameNodeInfo(NodeInfo nodeInfo) {
096        return this == nodeInfo;
097    }
098
099    /**
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}