001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2024 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 com.puppycrawl.tools.checkstyle.api.DetailAST; 025import com.puppycrawl.tools.checkstyle.utils.TokenUtil; 026import com.puppycrawl.tools.checkstyle.utils.XpathUtil; 027 028/** 029 * Represents DetailAST's element node of Xpath-tree. 030 */ 031public class ElementNode extends AbstractElementNode { 032 033 /** The ast node. */ 034 private final DetailAST detailAst; 035 036 /** 037 * Creates a new {@code ElementNode} instance. 038 * 039 * @param root {@code Node} root of the tree 040 * @param parent {@code Node} parent of the current node 041 * @param detailAst reference to {@code DetailAST} 042 * @param depth the current node depth in the hierarchy 043 * @param indexAmongSiblings the current node index among the parent children nodes 044 */ 045 public ElementNode(AbstractNode root, AbstractNode parent, DetailAST detailAst, 046 int depth, int indexAmongSiblings) { 047 super(root, parent, depth, indexAmongSiblings); 048 this.detailAst = detailAst; 049 } 050 051 /** 052 * Iterates children of the current node and 053 * recursively creates new Xpath-nodes. 054 * 055 * @return children list 056 */ 057 @Override 058 protected List<AbstractNode> createChildren() { 059 return XpathUtil.createChildren(getRoot(), this, detailAst.getFirstChild()); 060 } 061 062 /** 063 * Determine whether the node has any children. 064 * 065 * @return {@code true} is the node has any children. 066 */ 067 @Override 068 public boolean hasChildNodes() { 069 return detailAst.hasChildren(); 070 } 071 072 /** 073 * Returns local part. 074 * 075 * @return local part 076 */ 077 @Override 078 public String getLocalPart() { 079 return TokenUtil.getTokenName(detailAst.getType()); 080 } 081 082 /** 083 * Returns line number. 084 * 085 * @return line number 086 */ 087 @Override 088 public int getLineNumber() { 089 return detailAst.getLineNo(); 090 } 091 092 /** 093 * Returns column number. 094 * 095 * @return column number 096 */ 097 @Override 098 public int getColumnNumber() { 099 return detailAst.getColumnNo(); 100 } 101 102 /** 103 * Getter method for token type. 104 * 105 * @return token type 106 */ 107 @Override 108 public int getTokenType() { 109 return detailAst.getType(); 110 } 111 112 /** 113 * Returns underlying node. 114 * 115 * @return underlying node 116 */ 117 @Override 118 public DetailAST getUnderlyingNode() { 119 return detailAst; 120 } 121 122 @Override 123 protected AttributeNode createAttributeNode() { 124 final AttributeNode result; 125 126 if (XpathUtil.supportsTextAttribute(detailAst)) { 127 result = new AttributeNode(TEXT_ATTRIBUTE_NAME, 128 XpathUtil.getTextAttributeValue(detailAst)); 129 } 130 else { 131 result = null; 132 } 133 134 return result; 135 } 136}