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 com.puppycrawl.tools.checkstyle.xpath.iterators.DescendantIterator;
23  import net.sf.saxon.Configuration;
24  import net.sf.saxon.om.AxisInfo;
25  import net.sf.saxon.om.GenericTreeInfo;
26  import net.sf.saxon.om.NamespaceUri;
27  import net.sf.saxon.om.NodeInfo;
28  import net.sf.saxon.tree.iter.ArrayIterator;
29  import net.sf.saxon.tree.iter.AxisIterator;
30  import net.sf.saxon.tree.iter.EmptyIterator;
31  import net.sf.saxon.tree.iter.SingleNodeIterator;
32  import net.sf.saxon.type.Type;
33  
34  /**
35   * Represents root node of Xpath-tree.
36   */
37  public abstract class AbstractRootNode extends AbstractNode {
38  
39      /** Name of the root element. */
40      private static final String ROOT_NAME = "ROOT";
41  
42      /** Constant for optimization. */
43      private static final AbstractNode[] EMPTY_ABSTRACT_NODE_ARRAY = new AbstractNode[0];
44  
45      /**
46       * Creates a new {@code AbstractRootNode} instance.
47       */
48      protected AbstractRootNode() {
49          super(new GenericTreeInfo(Configuration.newConfiguration()));
50      }
51  
52      /**
53       * Compares current object with specified for order.
54       * Throws {@code UnsupportedOperationException} because functionality not required here.
55       *
56       * @param nodeInfo another {@code NodeInfo} object
57       * @return number representing order of current object to specified one
58       */
59      @Override
60      public int compareOrder(NodeInfo nodeInfo) {
61          throw throwUnsupportedOperationException();
62      }
63  
64      /**
65       * Returns attribute value. Throws {@code UnsupportedOperationException} because root node
66       * has no attributes.
67       *
68       * @param namespace namespace
69       * @param localPart actual name of the attribute
70       * @return attribute value
71       */
72      @Override
73      public String getAttributeValue(NamespaceUri namespace, String localPart) {
74          throw throwUnsupportedOperationException();
75      }
76  
77      /**
78       * Returns local part.
79       *
80       * @return local part
81       */
82      @Override
83      public String getLocalPart() {
84          return ROOT_NAME;
85      }
86  
87      /**
88       * Returns type of the node.
89       *
90       * @return node kind
91       */
92      @Override
93      public int getNodeKind() {
94          return Type.DOCUMENT;
95      }
96  
97      /**
98       * Returns parent.
99       *
100      * @return parent
101      */
102     @Override
103     public NodeInfo getParent() {
104         return null;
105     }
106 
107     /**
108      * Returns root of the tree.
109      *
110      * @return root of the tree
111      */
112     @Override
113     public NodeInfo getRoot() {
114         return this;
115     }
116 
117     /**
118      * Determines axis iteration algorithm. Throws {@code UnsupportedOperationException} in case,
119      * when there is no axis iterator for given axisNumber.
120      *
121      * @param axisNumber element from {@code AxisInfo}
122      * @return {@code AxisIterator} object
123      */
124     @Override
125     public AxisIterator iterateAxis(int axisNumber) {
126         final AxisIterator result;
127         switch (axisNumber) {
128             case AxisInfo.ANCESTOR:
129             case AxisInfo.PARENT:
130             case AxisInfo.FOLLOWING:
131             case AxisInfo.FOLLOWING_SIBLING:
132             case AxisInfo.PRECEDING:
133             case AxisInfo.PRECEDING_SIBLING:
134                 result = EmptyIterator.ofNodes();
135                 break;
136             case AxisInfo.ANCESTOR_OR_SELF:
137             case AxisInfo.SELF:
138                 result = SingleNodeIterator.makeIterator(this);
139                 break;
140             case AxisInfo.CHILD:
141                 if (hasChildNodes()) {
142                     result = new ArrayIterator.OfNodes<>(
143                             getChildren().toArray(EMPTY_ABSTRACT_NODE_ARRAY));
144                 }
145                 else {
146                     result = EmptyIterator.ofNodes();
147                 }
148                 break;
149             case AxisInfo.DESCENDANT:
150                 if (hasChildNodes()) {
151                     result = new DescendantIterator(this, DescendantIterator.StartWith.CHILDREN);
152                 }
153                 else {
154                     result = EmptyIterator.ofNodes();
155                 }
156                 break;
157             case AxisInfo.DESCENDANT_OR_SELF:
158                 result = new DescendantIterator(this, DescendantIterator.StartWith.CURRENT_NODE);
159                 break;
160             default:
161                 throw throwUnsupportedOperationException();
162         }
163         return result;
164     }
165 
166     /**
167      * Getter method for node depth.
168      *
169      * @return always {@code 0}
170      */
171     @Override
172     public int getDepth() {
173         return 0;
174     }
175 
176     /**
177      * Returns UnsupportedOperationException exception.
178      *
179      * @return UnsupportedOperationException exception
180      */
181     private static UnsupportedOperationException throwUnsupportedOperationException() {
182         return new UnsupportedOperationException("Operation is not supported");
183     }
184 
185 }