001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 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.List;
023
024import net.sf.saxon.om.NamespaceUri;
025import net.sf.saxon.om.NodeInfo;
026import net.sf.saxon.tree.iter.AxisIterator;
027import net.sf.saxon.type.Type;
028
029/**
030 * Represents attribute of the element.
031 *
032 */
033public class AttributeNode extends AbstractNode {
034
035    /** The name of the attribute. */
036    private final String name;
037
038    /** The value of the attribute. */
039    private final String value;
040
041    /**
042     * Creates a new {@code AttributeNode} instance.
043     *
044     * @param name name of the attribute
045     * @param value value of the attribute
046     */
047    public AttributeNode(String name, String value) {
048        super(null);
049        this.name = name;
050        this.value = value;
051    }
052
053    /**
054     * Compares current object with specified for order.
055     * Throws {@code UnsupportedOperationException} because functionality not required here.
056     *
057     * @param nodeInfo another {@code NodeInfo} object
058     * @return number representing order of current object to specified one
059     */
060    @Override
061    public int compareOrder(NodeInfo nodeInfo) {
062        throw throwUnsupportedOperationException();
063    }
064
065    /**
066     * Returns attribute value. Throws {@code UnsupportedOperationException} because attribute node
067     * has no attributes.
068     *
069     * @param namespace namespace
070     * @param localPart actual name of the attribute
071     * @return attribute value
072     */
073    @Override
074    public String getAttributeValue(NamespaceUri namespace, String localPart) {
075        throw throwUnsupportedOperationException();
076    }
077
078    /**
079     * Returns local part.
080     *
081     * @return local part
082     */
083    @Override
084    public String getLocalPart() {
085        return name;
086    }
087
088    /**
089     * Returns type of the node.
090     *
091     * @return node kind
092     */
093    @Override
094    public int getNodeKind() {
095        return Type.ATTRIBUTE;
096    }
097
098    /**
099     * 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}