1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2019 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 com.puppycrawl.tools.checkstyle.api.DetailAST;
23  import net.sf.saxon.om.NodeInfo;
24  import net.sf.saxon.tree.iter.AxisIterator;
25  import net.sf.saxon.type.Type;
26  
27  /**
28   * Represents attribute of the element.
29   *
30   */
31  public class AttributeNode extends AbstractNode {
32  
33      /** The name of the attribute. */
34      private final String name;
35  
36      /** The value of the attribute. */
37      private final String value;
38  
39      /**
40       * Creates a new {@code AttributeNode} instance.
41       *
42       * @param name name of the attribute
43       * @param value value of the attribute
44       */
45      public AttributeNode(String name, String value) {
46          super(null);
47          this.name = name;
48          this.value = value;
49      }
50  
51      /**
52       * Returns attribute value. Throws {@code UnsupportedOperationException} because attribute node
53       * has no attributes.
54       * @param namespace namespace
55       * @param localPart actual name of the attribute
56       * @return attribute value
57       */
58      @Override
59      public String getAttributeValue(String namespace, String localPart) {
60          throw throwUnsupportedOperationException();
61      }
62  
63      /**
64       * Returns local part.
65       * @return local part
66       */
67      // -@cs[SimpleAccessorNameNotation] Overrides method from the base class.
68      // Issue: https://github.com/sevntu-checkstyle/sevntu.checkstyle/issues/166
69      @Override
70      public String getLocalPart() {
71          return name;
72      }
73  
74      /**
75       * Returns type of the node.
76       * @return node kind
77       */
78      @Override
79      public int getNodeKind() {
80          return Type.ATTRIBUTE;
81      }
82  
83      /**
84       * Returns parent.  Never called for attribute node, throws
85       * {@code UnsupportedOperationException}.
86       * has no attributes.
87       * @return parent
88       */
89      @Override
90      public NodeInfo getParent() {
91          throw throwUnsupportedOperationException();
92      }
93  
94      /**
95       * Returns root. Never called for attribute node, throws
96       * {@code UnsupportedOperationException}.
97       * @return root
98       */
99      @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 }