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