View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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 java.util.List;
23  
24  import com.puppycrawl.tools.checkstyle.api.DetailAST;
25  import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
26  import com.puppycrawl.tools.checkstyle.utils.XpathUtil;
27  
28  /**
29   * Represents DetailAST's element node of Xpath-tree.
30   */
31  public class ElementNode extends AbstractElementNode {
32  
33      /** The ast node. */
34      private final DetailAST detailAst;
35  
36      /**
37       * Creates a new {@code ElementNode} instance.
38       *
39       * @param root {@code Node} root of the tree
40       * @param parent {@code Node} parent of the current node
41       * @param detailAst reference to {@code DetailAST}
42       * @param depth the current node depth in the hierarchy
43       * @param indexAmongSiblings the current node index among the parent children nodes
44       */
45      public ElementNode(AbstractNode root, AbstractNode parent, DetailAST detailAst,
46              int depth, int indexAmongSiblings) {
47          super(root, parent, depth, indexAmongSiblings);
48          this.detailAst = detailAst;
49      }
50  
51      /**
52       * Iterates children of the current node and
53       * recursively creates new Xpath-nodes.
54       *
55       * @return children list
56       */
57      @Override
58      protected List<AbstractNode> createChildren() {
59          return XpathUtil.createChildren(getRoot(), this, detailAst.getFirstChild());
60      }
61  
62      /**
63       * Determine whether the node has any children.
64       *
65       * @return {@code true} is the node has any children.
66       */
67      @Override
68      public boolean hasChildNodes() {
69          return detailAst.hasChildren();
70      }
71  
72      /**
73       * Returns local part.
74       *
75       * @return local part
76       */
77      @Override
78      public String getLocalPart() {
79          return TokenUtil.getTokenName(detailAst.getType());
80      }
81  
82      /**
83       * Returns line number.
84       *
85       * @return line number
86       */
87      @Override
88      public int getLineNumber() {
89          return detailAst.getLineNo();
90      }
91  
92      /**
93       * Returns column number.
94       *
95       * @return column number
96       */
97      @Override
98      public int getColumnNumber() {
99          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 }