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 * 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 @Override 068 public String getLocalPart() { 069 return name; 070 } 071 072 /** 073 * Returns type of the node. 074 * @return node kind 075 */ 076 @Override 077 public int getNodeKind() { 078 return Type.ATTRIBUTE; 079 } 080 081 /** 082 * Returns parent. Never called for attribute node, throws 083 * {@code UnsupportedOperationException}. 084 * has no attributes. 085 * @return parent 086 */ 087 @Override 088 public NodeInfo getParent() { 089 throw throwUnsupportedOperationException(); 090 } 091 092 /** 093 * Returns root. Never called for attribute node, throws 094 * {@code UnsupportedOperationException}. 095 * @return root 096 */ 097 @Override 098 public NodeInfo getRoot() { 099 throw throwUnsupportedOperationException(); 100 } 101 102 /** 103 * Returns string value. 104 * @return string value 105 */ 106 @Override 107 public String getStringValue() { 108 return value; 109 } 110 111 /** 112 * Determines axis iteration algorithm. Attribute node can not be iterated, throws 113 * {@code UnsupportedOperationException}. 114 * 115 * @param axisNumber element from {@code AxisInfo} 116 * @return {@code AxisIterator} object 117 */ 118 @Override 119 public AxisIterator iterateAxis(byte axisNumber) { 120 throw throwUnsupportedOperationException(); 121 } 122 123 /** 124 * Returns line number. Attribute node has no line number, throws 125 * {@code UnsupportedOperationException}. 126 * @return line number 127 */ 128 @Override 129 public int getLineNumber() { 130 throw throwUnsupportedOperationException(); 131 } 132 133 /** 134 * Returns column number. Attribute node has no column number, throws 135 * {@code UnsupportedOperationException}. 136 * @return column number 137 */ 138 @Override 139 public int getColumnNumber() { 140 throw throwUnsupportedOperationException(); 141 } 142 143 /** 144 * Getter method for token type. Attribute node has no token type, throws 145 * {@code UnsupportedOperationException}. 146 * @return token type 147 */ 148 @Override 149 public int getTokenType() { 150 throw throwUnsupportedOperationException(); 151 } 152 153 /** 154 * Returns underlying node. Attribute node has no underlying node, throws 155 * {@code UnsupportedOperationException}. 156 * @return underlying node 157 */ 158 @Override 159 public DetailAST getUnderlyingNode() { 160 throw throwUnsupportedOperationException(); 161 } 162 163 /** 164 * Returns UnsupportedOperationException exception. 165 * @return UnsupportedOperationException exception 166 */ 167 private static UnsupportedOperationException throwUnsupportedOperationException() { 168 return new UnsupportedOperationException("Operation is not supported"); 169 } 170 171}