View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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   * @author Timur Tibeyev
31   */
32  public class AttributeNode extends AbstractNode {
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          this.name = name;
47          this.value = value;
48      }
49  
50      /**
51       * Returns attribute value. Throws {@code UnsupportedOperationException} because attribute node
52       * has no attributes.
53       * @param namespace namespace
54       * @param localPart actual name of the attribute
55       * @return attribute value
56       */
57      @Override
58      public String getAttributeValue(String namespace, String localPart) {
59          throw throwUnsupportedOperationException();
60      }
61  
62      /**
63       * Returns local part.
64       * @return local part
65       */
66      // -@cs[SimpleAccessorNameNotation] Overrides method from the base class.
67      // Issue: https://github.com/sevntu-checkstyle/sevntu.checkstyle/issues/166
68      @Override
69      public String getLocalPart() {
70          return name;
71      }
72  
73      /**
74       * Returns type of the node.
75       * @return node kind
76       */
77      @Override
78      public int getNodeKind() {
79          return Type.ATTRIBUTE;
80      }
81  
82      /**
83       * Returns parent.  Never called for attribute node, throws
84       * {@code UnsupportedOperationException}.
85       * has no attributes.
86       * @return parent
87       */
88      @Override
89      public NodeInfo getParent() {
90          throw throwUnsupportedOperationException();
91      }
92  
93      /**
94       * Returns root. Never called for attribute node, throws
95       * {@code UnsupportedOperationException}.
96       * @return root
97       */
98      @Override
99      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 }