001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 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 java.util.List; 023 024import net.sf.saxon.om.AtomicSequence; 025import net.sf.saxon.om.GNode; 026import net.sf.saxon.om.NamespaceUri; 027import net.sf.saxon.om.NodeInfo; 028import net.sf.saxon.om.SequenceIterator; 029import net.sf.saxon.pattern.nodetest.NodePredicate; 030import net.sf.saxon.str.StringView; 031import net.sf.saxon.type.Type; 032import net.sf.saxon.value.StringValue; 033 034/** 035 * Represents attribute of the element. 036 * 037 */ 038public class AttributeNode extends AbstractNode { 039 040 /** The name of the attribute. */ 041 private final String name; 042 043 /** The value of the attribute. */ 044 private final String value; 045 046 /** 047 * Creates a new {@code AttributeNode} instance. 048 * 049 * @param name name of the attribute 050 * @param value value of the attribute 051 */ 052 public AttributeNode(String name, String value) { 053 super(null); 054 this.name = name; 055 this.value = value; 056 } 057 058 /** 059 * Compares current object with specified for order. 060 * Throws {@code UnsupportedOperationException} because functionality not required here. 061 * 062 * @param other another {@code GNode} object 063 * @return number representing order of current object to specified one 064 */ 065 @Override 066 public int compareOrder(GNode other) { 067 throw throwUnsupportedOperationException(); 068 } 069 070 /** 071 * Returns attribute value. Throws {@code UnsupportedOperationException} because attribute node 072 * has no attributes. 073 * 074 * @param namespace namespace 075 * @param localPart actual name of the attribute 076 * @return attribute value 077 */ 078 @Override 079 public String getAttributeValue(NamespaceUri namespace, String localPart) { 080 throw throwUnsupportedOperationException(); 081 } 082 083 /** 084 * Returns local part. 085 * 086 * @return local part 087 */ 088 @Override 089 public String getLocalPart() { 090 return name; 091 } 092 093 /** 094 * Returns type of the node. 095 * 096 * @return node kind 097 */ 098 @Override 099 public int getNodeKind() { 100 return Type.ATTRIBUTE; 101 } 102 103 @Override 104 public AtomicSequence atomize() { 105 return StringValue.makeUntypedAtomic(StringView.of(value)); 106 } 107 108 /** 109 * Returns parent. Never called for attribute node, throws 110 * {@code UnsupportedOperationException}. 111 * has no attributes. 112 * 113 * @return parent 114 */ 115 @Override 116 public NodeInfo getParent() { 117 throw throwUnsupportedOperationException(); 118 } 119 120 /** 121 * Returns root. Never called for attribute node, throws 122 * {@code UnsupportedOperationException}. 123 * 124 * @return root 125 */ 126 @Override 127 public NodeInfo getRoot() { 128 throw throwUnsupportedOperationException(); 129 } 130 131 /** 132 * Returns string value. 133 * 134 * @return string value 135 */ 136 @Override 137 public String getStringValue() { 138 return value; 139 } 140 141 /** 142 * Child axis. Attribute node has no children, throws 143 * {@code UnsupportedOperationException}. 144 * 145 * @param predicate filter for the iterator, may be null 146 * @return never 147 */ 148 @Override 149 public SequenceIterator iterateChildAxis(NodePredicate predicate) { 150 throw throwUnsupportedOperationException(); 151 } 152 153 /** 154 * Following-sibling axis. Attribute node can not be iterated, throws 155 * {@code UnsupportedOperationException}. 156 * 157 * @param predicate filter for the iterator, may be null 158 * @return never 159 */ 160 @Override 161 public SequenceIterator iterateFollowingSiblingAxis(NodePredicate predicate) { 162 throw throwUnsupportedOperationException(); 163 } 164 165 /** 166 * Preceding-sibling axis. Attribute node can not be iterated, throws 167 * {@code UnsupportedOperationException}. 168 * 169 * @param predicate filter for the iterator, may be null 170 * @return never 171 */ 172 @Override 173 public SequenceIterator iteratePrecedingSiblingAxis(NodePredicate predicate) { 174 throw throwUnsupportedOperationException(); 175 } 176 177 /** 178 * Returns line number. Attribute node has no line number, throws 179 * {@code UnsupportedOperationException}. 180 * 181 * @return line number 182 */ 183 @Override 184 public int getLineNumber() { 185 throw throwUnsupportedOperationException(); 186 } 187 188 /** 189 * Returns column number. Attribute node has no column number, throws 190 * {@code UnsupportedOperationException}. 191 * 192 * @return column number 193 */ 194 @Override 195 public int getColumnNumber() { 196 throw throwUnsupportedOperationException(); 197 } 198 199 /** 200 * Getter method for token type. Attribute node has no token type, throws 201 * {@code UnsupportedOperationException}. 202 * 203 * @return token type 204 */ 205 @Override 206 public int getTokenType() { 207 throw throwUnsupportedOperationException(); 208 } 209 210 /** 211 * Returns underlying node. Attribute node has no underlying node, throws 212 * {@code UnsupportedOperationException}. 213 * 214 * @return underlying node 215 */ 216 @Override 217 public Object getUnderlyingNode() { 218 throw throwUnsupportedOperationException(); 219 } 220 221 /** 222 * Getter method for node depth. This method is not applicable to attribute nodes, 223 * throws unsupported exception. 224 * 225 * @return never 226 */ 227 @Override 228 public int getDepth() { 229 throw throwUnsupportedOperationException(); 230 } 231 232 /** 233 * Creates nodes for children. Attribute node has no children, so this method throws unsupported 234 * exception. 235 * 236 * @return never 237 */ 238 @Override 239 protected List<AbstractNode> createChildren() { 240 throw throwUnsupportedOperationException(); 241 } 242 243 /** 244 * Determine whether the node has any children. 245 * 246 * @return always {@code false} 247 */ 248 @Override 249 public boolean hasChildNodes() { 250 return false; 251 } 252 253 /** 254 * Returns UnsupportedOperationException exception. 255 * 256 * @return UnsupportedOperationException exception 257 */ 258 private static UnsupportedOperationException throwUnsupportedOperationException() { 259 return new UnsupportedOperationException("Operation is not supported"); 260 } 261 262}