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 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 recursively creates new Xpath-nodes.
053     *
054     * @return children list
055     */
056    @Override
057    protected List<AbstractNode> createChildren() {
058        return XpathUtil.createChildren(getRoot(), this, detailAst.getFirstChild());
059    }
060
061    /**
062     * Determine whether the node has any children.
063     *
064     * @return {@code true} is the node has any children.
065     */
066    @Override
067    public boolean hasChildNodes() {
068        return detailAst.hasChildren();
069    }
070
071    /**
072     * Returns local part.
073     *
074     * @return local part
075     */
076    @Override
077    public String getLocalPart() {
078        return TokenUtil.getTokenName(detailAst.getType());
079    }
080
081    /**
082     * Returns line number.
083     *
084     * @return line number
085     */
086    @Override
087    public int getLineNumber() {
088        return detailAst.getLineNo();
089    }
090
091    /**
092     * Returns column number.
093     *
094     * @return column number
095     */
096    @Override
097    public int getColumnNumber() {
098        return detailAst.getColumnNo();
099    }
100
101    /**
102     * Getter method for token type.
103     *
104     * @return token type
105     */
106    @Override
107    public int getTokenType() {
108        return detailAst.getType();
109    }
110
111    /**
112     * Returns underlying node.
113     *
114     * @return underlying node
115     */
116    @Override
117    public DetailAST getUnderlyingNode() {
118        return detailAst;
119    }
120
121    @Override
122    protected AttributeNode createAttributeNode() {
123        final AttributeNode result;
124
125        if (XpathUtil.supportsTextAttribute(detailAst)) {
126            result = new AttributeNode(TEXT_ATTRIBUTE_NAME,
127                    XpathUtil.getTextAttributeValue(detailAst));
128        }
129        else {
130            result = null;
131        }
132
133        return result;
134    }
135
136}