001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2018 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     * Returns attribute value. Throws {@code UnsupportedOperationException} because attribute node
053     * has no attributes.
054     * @param namespace namespace
055     * @param localPart actual name of the attribute
056     * @return attribute value
057     */
058    @Override
059    public String getAttributeValue(String namespace, String localPart) {
060        throw throwUnsupportedOperationException();
061    }
062
063    /**
064     * Returns local part.
065     * @return local part
066     */
067    // -@cs[SimpleAccessorNameNotation] Overrides method from the base class.
068    // Issue: https://github.com/sevntu-checkstyle/sevntu.checkstyle/issues/166
069    @Override
070    public String getLocalPart() {
071        return name;
072    }
073
074    /**
075     * Returns type of the node.
076     * @return node kind
077     */
078    @Override
079    public int getNodeKind() {
080        return Type.ATTRIBUTE;
081    }
082
083    /**
084     * Returns parent.  Never called for attribute node, throws
085     * {@code UnsupportedOperationException}.
086     * has no attributes.
087     * @return parent
088     */
089    @Override
090    public NodeInfo getParent() {
091        throw throwUnsupportedOperationException();
092    }
093
094    /**
095     * Returns root. Never called for attribute node, throws
096     * {@code UnsupportedOperationException}.
097     * @return root
098     */
099    @Override
100    public NodeInfo getRoot() {
101        throw throwUnsupportedOperationException();
102    }
103
104    /**
105     * Returns string value.
106     * @return string value
107     */
108    // -@cs[SimpleAccessorNameNotation] Overrides method from the base class.
109    // Issue: https://github.com/sevntu-checkstyle/sevntu.checkstyle/issues/166
110    @Override
111    public String getStringValue() {
112        return value;
113    }
114
115    /**
116     * Determines axis iteration algorithm. Attribute node can not be iterated, throws
117     * {@code UnsupportedOperationException}.
118     *
119     * @param axisNumber element from {@code AxisInfo}
120     * @return {@code AxisIterator} object
121     */
122    @Override
123    public AxisIterator iterateAxis(byte axisNumber) {
124        throw throwUnsupportedOperationException();
125    }
126
127    /**
128     * Returns line number. Attribute node has no line number, throws
129     * {@code UnsupportedOperationException}.
130     * @return line number
131     */
132    @Override
133    public int getLineNumber() {
134        throw throwUnsupportedOperationException();
135    }
136
137    /**
138     * Returns column number. Attribute node has no column number, throws
139     * {@code UnsupportedOperationException}.
140     * @return column number
141     */
142    @Override
143    public int getColumnNumber() {
144        throw throwUnsupportedOperationException();
145    }
146
147    /**
148     * Getter method for token type. Attribute node has no token type, throws
149     * {@code UnsupportedOperationException}.
150     * @return token type
151     */
152    @Override
153    public int getTokenType() {
154        throw throwUnsupportedOperationException();
155    }
156
157    /**
158     * Returns underlying node. Attribute node has no underlying node, throws
159     * {@code UnsupportedOperationException}.
160     * @return underlying node
161     */
162    @Override
163    public DetailAST getUnderlyingNode() {
164        throw throwUnsupportedOperationException();
165    }
166
167    /**
168     * Returns UnsupportedOperationException exception.
169     * @return UnsupportedOperationException exception
170     */
171    private static UnsupportedOperationException throwUnsupportedOperationException() {
172        return new UnsupportedOperationException("Operation is not supported");
173    }
174
175}