KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > api > FullIdent


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2005 Oliver Burn
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
package com.puppycrawl.tools.checkstyle.api;
20
21 /**
22  * Represents a full identifier, including dots, with associated
23  * position information.
24  *
25  * <p>
26  * Identifiers such as <code>java.util.HashMap</code> are spread across
27  * multiple AST nodes in the syntax tree (three IDENT nodes, two DOT nodes).
28  * A FullIdent represents the whole String (excluding any intermediate
29  * whitespace), which is often easier to work with in Checks.
30  * </p>
31  *
32  * @author Oliver Burn
33  * @see TokenTypes#DOT
34  * @see TokenTypes#IDENT
35  **/

36 public final class FullIdent
37 {
38     /** the string **/
39     private final StringBuffer JavaDoc mBuffer = new StringBuffer JavaDoc();
40     /** the line number **/
41     private int mLineNo;
42     /** the column number **/
43     private int mColNo;
44
45     /** hide default constructor */
46     private FullIdent()
47     {
48     }
49
50     /** @return the text **/
51     public String JavaDoc getText()
52     {
53         return mBuffer.toString();
54     }
55
56     /** @return the line number **/
57     public int getLineNo()
58     {
59         return mLineNo;
60     }
61
62     /** @return the column number **/
63     public int getColumnNo()
64     {
65         return mColNo;
66     }
67
68     /**
69      * Append the specified text.
70      * @param aText the text to append
71      */

72     private void append(String JavaDoc aText)
73     {
74         mBuffer.append(aText);
75     }
76
77     /**
78      * Append the specified token and also recalibrate the first line and
79      * column.
80      * @param aAST the token to append
81      */

82     private void append(DetailAST aAST)
83     {
84         mBuffer.append(aAST.getText());
85         if (mLineNo == 0) {
86             mLineNo = aAST.getLineNo();
87         }
88         else if (aAST.getLineNo() > 0) {
89             mLineNo = Math.min(mLineNo, aAST.getLineNo());
90         }
91         // TODO: make a function
92
if (mColNo == 0) {
93             mColNo = aAST.getColumnNo();
94         }
95         else if (aAST.getColumnNo() > 0) {
96             mColNo = Math.min(mColNo, aAST.getColumnNo());
97         }
98     }
99
100     /**
101      * Creates a new FullIdent starting from the specified node.
102      * @param aAST the node to start from
103      * @return a <code>FullIdent</code> value
104      */

105     public static FullIdent createFullIdent(DetailAST aAST)
106     {
107         final FullIdent fi = new FullIdent();
108         extractFullIdent(fi, aAST);
109         return fi;
110     }
111
112     /**
113      * Creates a new FullIdent starting from the child of the specified node.
114      * @param aAST the parent node from where to start from
115      * @return a <code>FullIdent</code> value
116      */

117     public static FullIdent createFullIdentBelow(DetailAST aAST)
118     {
119         return createFullIdent((DetailAST) aAST.getFirstChild());
120     }
121
122     /**
123      * Recursively extract a FullIdent.
124      *
125      * @param aFull the FullIdent to add to
126      * @param aAST the node to recurse from
127      */

128     private static void extractFullIdent(FullIdent aFull, DetailAST aAST)
129     {
130         if (aAST == null) {
131             return;
132         }
133
134         if (aAST.getType() == TokenTypes.DOT) {
135             extractFullIdent(aFull, (DetailAST) aAST.getFirstChild());
136             aFull.append(".");
137             extractFullIdent(
138                 aFull, (DetailAST) aAST.getFirstChild().getNextSibling());
139         }
140         else {
141             aFull.append(aAST);
142         }
143     }
144
145     /** {@inheritDoc} */
146     public String JavaDoc toString()
147     {
148         return getText() + "[" + getLineNo() + "x" + getColumnNo() + "]";
149     }
150
151 }
152
Popular Tags