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.List;
23  
24  import net.sf.saxon.om.NamespaceUri;
25  import net.sf.saxon.om.NodeInfo;
26  import net.sf.saxon.tree.iter.AxisIterator;
27  import net.sf.saxon.type.Type;
28  
29  /**
30   * Represents attribute of the element.
31   *
32   */
33  public class AttributeNode extends AbstractNode {
34  
35      /** The name of the attribute. */
36      private final String name;
37  
38      /** The value of the attribute. */
39      private final String value;
40  
41      /**
42       * Creates a new {@code AttributeNode} instance.
43       *
44       * @param name name of the attribute
45       * @param value value of the attribute
46       */
47      public AttributeNode(String name, String value) {
48          super(null);
49          this.name = name;
50          this.value = value;
51      }
52  
53      /**
54       * Compares current object with specified for order.
55       * Throws {@code UnsupportedOperationException} because functionality not required here.
56       *
57       * @param nodeInfo another {@code NodeInfo} object
58       * @return number representing order of current object to specified one
59       */
60      @Override
61      public int compareOrder(NodeInfo nodeInfo) {
62          throw throwUnsupportedOperationException();
63      }
64  
65      /**
66       * Returns attribute value. Throws {@code UnsupportedOperationException} because attribute node
67       * has no attributes.
68       *
69       * @param namespace namespace
70       * @param localPart actual name of the attribute
71       * @return attribute value
72       */
73      @Override
74      public String getAttributeValue(NamespaceUri namespace, String localPart) {
75          throw throwUnsupportedOperationException();
76      }
77  
78      /**
79       * Returns local part.
80       *
81       * @return local part
82       */
83      @Override
84      public String getLocalPart() {
85          return name;
86      }
87  
88      /**
89       * Returns type of the node.
90       *
91       * @return node kind
92       */
93      @Override
94      public int getNodeKind() {
95          return Type.ATTRIBUTE;
96      }
97  
98      /**
99       * Returns parent.  Never called for attribute node, throws
100      * {@code UnsupportedOperationException}.
101      * has no attributes.
102      *
103      * @return parent
104      */
105     @Override
106     public NodeInfo getParent() {
107         throw throwUnsupportedOperationException();
108     }
109 
110     /**
111      * Returns root. Never called for attribute node, throws
112      * {@code UnsupportedOperationException}.
113      *
114      * @return root
115      */
116     @Override
117     public NodeInfo getRoot() {
118         throw throwUnsupportedOperationException();
119     }
120 
121     /**
122      * Returns string value.
123      *
124      * @return string value
125      */
126     @Override
127     public String getStringValue() {
128         return value;
129     }
130 
131     /**
132      * Determines axis iteration algorithm. Attribute node can not be iterated, throws
133      * {@code UnsupportedOperationException}.
134      *
135      * @param axisNumber element from {@code AxisInfo}
136      * @return {@code AxisIterator} object
137      */
138     @Override
139     public AxisIterator iterateAxis(int axisNumber) {
140         throw throwUnsupportedOperationException();
141     }
142 
143     /**
144      * Returns line number. Attribute node has no line number, throws
145      * {@code UnsupportedOperationException}.
146      *
147      * @return line number
148      */
149     @Override
150     public int getLineNumber() {
151         throw throwUnsupportedOperationException();
152     }
153 
154     /**
155      * Returns column number. Attribute node has no column number, throws
156      * {@code UnsupportedOperationException}.
157      *
158      * @return column number
159      */
160     @Override
161     public int getColumnNumber() {
162         throw throwUnsupportedOperationException();
163     }
164 
165     /**
166      * Getter method for token type. Attribute node has no token type, throws
167      * {@code UnsupportedOperationException}.
168      *
169      * @return token type
170      */
171     @Override
172     public int getTokenType() {
173         throw throwUnsupportedOperationException();
174     }
175 
176     /**
177      * Returns underlying node. Attribute node has no underlying node, throws
178      * {@code UnsupportedOperationException}.
179      *
180      * @return underlying node
181      */
182     @Override
183     public Object getUnderlyingNode() {
184         throw throwUnsupportedOperationException();
185     }
186 
187     /**
188      * Getter method for node depth. This method is not applicable to attribute nodes,
189      * throws unsupported exception.
190      *
191      * @return never
192      */
193     @Override
194     public int getDepth() {
195         throw throwUnsupportedOperationException();
196     }
197 
198     /**
199      * Creates nodes for children. Attribute node has no children, so
200      * this method throws unsupported exception.
201      *
202      * @return never
203      */
204     @Override
205     protected List<AbstractNode> createChildren() {
206         throw throwUnsupportedOperationException();
207     }
208 
209     /**
210      * Determine whether the node has any children.
211      *
212      * @return always {@code false}
213      */
214     @Override
215     public boolean hasChildNodes() {
216         return false;
217     }
218 
219     /**
220      * Returns UnsupportedOperationException exception.
221      *
222      * @return UnsupportedOperationException exception
223      */
224     private static UnsupportedOperationException throwUnsupportedOperationException() {
225         return new UnsupportedOperationException("Operation is not supported");
226     }
227 
228 }