001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2022 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.Collections;
023import java.util.List;
024
025import com.puppycrawl.tools.checkstyle.api.DetailAST;
026import net.sf.saxon.Configuration;
027import net.sf.saxon.event.Receiver;
028import net.sf.saxon.om.AtomicSequence;
029import net.sf.saxon.om.NamespaceBinding;
030import net.sf.saxon.om.NamespaceMap;
031import net.sf.saxon.om.NodeInfo;
032import net.sf.saxon.om.TreeInfo;
033import net.sf.saxon.pattern.NodePredicate;
034import net.sf.saxon.s9api.Location;
035import net.sf.saxon.str.UnicodeString;
036import net.sf.saxon.tree.iter.AxisIterator;
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 */
043public abstract class AbstractNode implements NodeInfo {
044
045    /** The {@code TreeInfo} object. */
046    private final TreeInfo treeInfo;
047
048    /** The children. */
049    private List<AbstractNode> children;
050
051    /**
052     * Constructor of the abstract class {@code AbstractNode}.
053     *
054     * @param treeInfo {@code TreeInfo} object
055     */
056    protected AbstractNode(TreeInfo treeInfo) {
057        this.treeInfo = treeInfo;
058    }
059
060    /**
061     * Getter method for token type.
062     *
063     * @return token type
064     */
065    public abstract int getTokenType();
066
067    /**
068     * Returns underlying node.
069     *
070     * @return underlying node
071     */
072    public abstract DetailAST getUnderlyingNode();
073
074    /**
075     * Getter method for node depth.
076     *
077     * @return depth
078     */
079    public abstract int getDepth();
080
081    /**
082     * Creates nodes for children.
083     *
084     * @return children list
085     */
086    protected abstract List<AbstractNode> createChildren();
087
088    /**
089     * Getter method for children.
090     *
091     * @return children list
092     */
093    protected List<AbstractNode> getChildren() {
094        if (children == null) {
095            children = createChildren();
096        }
097        return Collections.unmodifiableList(children);
098    }
099
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     * Returns uri of the namespace for the current node.
123     *
124     * @return uri
125     */
126    @Override
127    public String getURI() {
128        return "";
129    }
130
131    /**
132     * Determines axis iteration algorithm.
133     *
134     * @param axisNumber element from {@code AxisInfo}
135     * @param nodeTest filter for iterator
136     * @return {@code AxisIterator} object
137     */
138    @Override
139    public AxisIterator iterateAxis(int axisNumber, NodePredicate nodeTest) {
140        AxisIterator axisIterator = iterateAxis(axisNumber);
141        if (nodeTest != null) {
142            axisIterator = new Navigator.AxisFilter(axisIterator, nodeTest);
143        }
144        return axisIterator;
145    }
146
147    /**
148     * Returns tree info.
149     *
150     * @return tree info
151     */
152    @Override
153    public final TreeInfo getTreeInfo() {
154        return treeInfo;
155    }
156
157    /**
158     * Returns string value. Throws {@code UnsupportedOperationException}, because no child
159     * class implements it and this method is not used for querying.
160     *
161     * @return string value
162     */
163    @Override
164    public String getStringValue() {
165        throw createUnsupportedOperationException();
166    }
167
168    /**
169     * Returns namespace array. Throws {@code UnsupportedOperationException}, because no child
170     * class implements it and this method is not used for querying.
171     *
172     * @param namespaceBindings namespace array
173     * @return namespace array
174     */
175    @Override
176    public final NamespaceBinding[] getDeclaredNamespaces(NamespaceBinding[] namespaceBindings) {
177        throw createUnsupportedOperationException();
178    }
179
180    /**
181     * Returns namespace array. Throws {@code UnsupportedOperationException}, because no child
182     * class implements it and this method is not used for querying.
183     *
184     * @return namespace map
185     */
186    @Override
187    public NamespaceMap getAllNamespaces() {
188        throw createUnsupportedOperationException();
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     *
195     * @return boolean
196     */
197    @Override
198    public final boolean isId() {
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 isIdref() {
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 isNilled() {
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 isStreamed() {
232        throw createUnsupportedOperationException();
233    }
234
235    /**
236     * Returns configuration. Throws {@code UnsupportedOperationException}, because no child
237     * class implements it and this method is not used for querying.
238     *
239     * @return configuration
240     */
241    @Override
242    public final Configuration getConfiguration() {
243        throw createUnsupportedOperationException();
244    }
245
246    /**
247     * Sets system id. Throws {@code UnsupportedOperationException}, because no child
248     * class implements it and this method is not used for querying.
249     *
250     * @param systemId system id
251     */
252    @Override
253    public final void setSystemId(String systemId) {
254        throw createUnsupportedOperationException();
255    }
256
257    /**
258     * Returns system id. Throws {@code UnsupportedOperationException}, because no child
259     * class implements it and this method is not used for querying.
260     *
261     * @return system id
262     */
263    @Override
264    public final String getSystemId() {
265        throw createUnsupportedOperationException();
266    }
267
268    /**
269     * Returns public id. Throws {@code UnsupportedOperationException}, because no child
270     * class implements it and this method is not used for querying.
271     *
272     * @return public id
273     */
274    @Override
275    public final String getPublicId() {
276        throw createUnsupportedOperationException();
277    }
278
279    /**
280     * Returns base uri. Throws {@code UnsupportedOperationException}, because no child
281     * class implements it and this method is not used for querying.
282     *
283     * @return base uri
284     */
285    @Override
286    public final String getBaseURI() {
287        throw createUnsupportedOperationException();
288    }
289
290    /**
291     * Returns location. Throws {@code UnsupportedOperationException}, because no child
292     * class implements it and this method is not used for querying.
293     *
294     * @return location
295     */
296    @Override
297    public final Location saveLocation() {
298        throw createUnsupportedOperationException();
299    }
300
301    /**
302     * Returns the value of the item as a Unicode string.
303     * Throws {@code UnsupportedOperationException}, because no child class implements it and
304     * this method is not used for querying.
305     *
306     * @return CharSequence string value
307     */
308    @Override
309    public final UnicodeString getUnicodeStringValue() {
310        throw createUnsupportedOperationException();
311    }
312
313    /**
314     * Returns fingerprint. Throws {@code UnsupportedOperationException}, because no child
315     * class implements it and this method is not used for querying.
316     *
317     * @return fingerprint
318     */
319    @Override
320    public final int getFingerprint() {
321        throw createUnsupportedOperationException();
322    }
323
324    /**
325     * Returns display name. Throws {@code UnsupportedOperationException}, because no child
326     * class implements it and this method is not used for querying.
327     *
328     * @return display name
329     */
330    @Override
331    public final String getDisplayName() {
332        throw createUnsupportedOperationException();
333    }
334
335    /**
336     * Returns prefix. Throws {@code UnsupportedOperationException}, because no child
337     * class implements it and this method is not used for querying.
338     *
339     * @return prefix
340     */
341    @Override
342    public final String getPrefix() {
343        throw createUnsupportedOperationException();
344    }
345
346    /**
347     * Returns type of the schema. Throws {@code UnsupportedOperationException}, because no child
348     * class implements it and this method is not used for querying.
349     *
350     * @return type of the schema
351     */
352    @Override
353    public final SchemaType getSchemaType() {
354        throw createUnsupportedOperationException();
355    }
356
357    /**
358     * Returns AtomicSequence. Throws {@code UnsupportedOperationException}, because no child
359     * class implements it and this method is not used for querying.
360     *
361     * @return AtomicSequence
362     */
363    @Override
364    public final AtomicSequence atomize() {
365        throw createUnsupportedOperationException();
366    }
367
368    /**
369     * Generate id method. Throws {@code UnsupportedOperationException}, because no child
370     * class implements it and this method is not used for querying.
371     *
372     * @param buffer buffer
373     */
374    @Override
375    public final void generateId(StringBuilder buffer) {
376        throw createUnsupportedOperationException();
377    }
378
379    /**
380     * Copy method. Throws {@code UnsupportedOperationException}, because no child
381     * class implements it and this method is not used for querying.
382     *
383     * @param receiver receiver
384     * @param index index
385     * @param location location
386     */
387    @Override
388    public final void copy(Receiver receiver, int index, Location location) {
389        throw createUnsupportedOperationException();
390    }
391
392    /**
393     * Returns UnsupportedOperationException exception. Methods which throws this exception are
394     * not supported for all nodes.
395     *
396     * @return UnsupportedOperationException exception
397     */
398    private static UnsupportedOperationException createUnsupportedOperationException() {
399        return new UnsupportedOperationException("Operation is not supported");
400    }
401
402}