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 com.puppycrawl.tools.checkstyle.api.DetailAST;
023import net.sf.saxon.om.NodeInfo;
024import net.sf.saxon.tree.iter.AxisIterator;
025import net.sf.saxon.type.Type;
026
027/**
028 * Represents attribute of the element.
029 *
030 */
031public class AttributeNode extends AbstractNode {
032
033    /** The name of the attribute. */
034    private final String name;
035
036    /** The value of the attribute. */
037    private final String value;
038
039    /**
040     * Creates a new {@code AttributeNode} instance.
041     *
042     * @param name name of the attribute
043     * @param value value of the attribute
044     */
045    public AttributeNode(String name, String value) {
046        super(null);
047        this.name = name;
048        this.value = value;
049    }
050
051    /**
052     * Compares current object with specified for order.
053     * Throws {@code UnsupportedOperationException} because functionality not required here.
054     *
055     * @param nodeInfo another {@code NodeInfo} object
056     * @return number representing order of current object to specified one
057     */
058    @Override
059    public int compareOrder(NodeInfo nodeInfo) {
060        throw throwUnsupportedOperationException();
061    }
062
063    /**
064     * Returns attribute value. Throws {@code UnsupportedOperationException} because attribute node
065     * has no attributes.
066     *
067     * @param namespace namespace
068     * @param localPart actual name of the attribute
069     * @return attribute value
070     */
071    @Override
072    public String getAttributeValue(String namespace, String localPart) {
073        throw throwUnsupportedOperationException();
074    }
075
076    /**
077     * Returns local part.
078     *
079     * @return local part
080     */
081    @Override
082    public String getLocalPart() {
083        return name;
084    }
085
086    /**
087     * Returns type of the node.
088     *
089     * @return node kind
090     */
091    @Override
092    public int getNodeKind() {
093        return Type.ATTRIBUTE;
094    }
095
096    /**
097     * Returns parent.  Never called for attribute node, throws
098     * {@code UnsupportedOperationException}.
099     * has no attributes.
100     *
101     * @return parent
102     */
103    @Override
104    public NodeInfo getParent() {
105        throw throwUnsupportedOperationException();
106    }
107
108    /**
109     * Returns root. Never called for attribute node, throws
110     * {@code UnsupportedOperationException}.
111     *
112     * @return root
113     */
114    @Override
115    public NodeInfo getRoot() {
116        throw throwUnsupportedOperationException();
117    }
118
119    /**
120     * Returns string value.
121     *
122     * @return string value
123     */
124    @Override
125    public String getStringValue() {
126        return value;
127    }
128
129    /**
130     * Determines axis iteration algorithm. Attribute node can not be iterated, throws
131     * {@code UnsupportedOperationException}.
132     *
133     * @param axisNumber element from {@code AxisInfo}
134     * @return {@code AxisIterator} object
135     */
136    @Override
137    public AxisIterator iterateAxis(byte axisNumber) {
138        throw throwUnsupportedOperationException();
139    }
140
141    /**
142     * Returns line number. Attribute node has no line number, throws
143     * {@code UnsupportedOperationException}.
144     *
145     * @return line number
146     */
147    @Override
148    public int getLineNumber() {
149        throw throwUnsupportedOperationException();
150    }
151
152    /**
153     * Returns column number. Attribute node has no column number, throws
154     * {@code UnsupportedOperationException}.
155     *
156     * @return column number
157     */
158    @Override
159    public int getColumnNumber() {
160        throw throwUnsupportedOperationException();
161    }
162
163    /**
164     * Getter method for token type. Attribute node has no token type, throws
165     * {@code UnsupportedOperationException}.
166     *
167     * @return token type
168     */
169    @Override
170    public int getTokenType() {
171        throw throwUnsupportedOperationException();
172    }
173
174    /**
175     * Returns underlying node. Attribute node has no underlying node, throws
176     * {@code UnsupportedOperationException}.
177     *
178     * @return underlying node
179     */
180    @Override
181    public DetailAST getUnderlyingNode() {
182        throw throwUnsupportedOperationException();
183    }
184
185    /**
186     * Returns UnsupportedOperationException exception.
187     *
188     * @return UnsupportedOperationException exception
189     */
190    private static UnsupportedOperationException throwUnsupportedOperationException() {
191        return new UnsupportedOperationException("Operation is not supported");
192    }
193
194}