KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > coding > MultipleStringLiteralsCheck


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.checks.coding;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.regex.Pattern JavaDoc;
26
27 import com.puppycrawl.tools.checkstyle.api.Check;
28 import com.puppycrawl.tools.checkstyle.api.DetailAST;
29 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30 import com.puppycrawl.tools.checkstyle.api.Utils;
31
32 /**
33  * Checks for multiple occurrences of the same string literal within a
34  * single file.
35  *
36  * @author Daniel Grenner
37  */

38 public class MultipleStringLiteralsCheck extends Check
39 {
40     /**
41      * The found strings and their positions.
42      * <String, ArrayList>, with the ArrayList containing StringInfo objects.
43      */

44     private final HashMap JavaDoc mStringMap = new HashMap JavaDoc();
45     /**
46      * The allowed number of string duplicates in a file before an error is
47      * generated.
48      */

49     private int mAllowedDuplicates = 1;
50
51     /**
52      * Sets the maximum allowed duplicates of a string.
53      * @param aAllowedDuplicates The maximum number of duplicates.
54      */

55     public void setAllowedDuplicates(int aAllowedDuplicates)
56     {
57         mAllowedDuplicates = aAllowedDuplicates;
58     }
59
60     /**
61      * Pattern for matching ignored strings.
62      */

63     private Pattern JavaDoc mPattern;
64
65     /**
66      * Construct an instance with default values.
67      */

68     public MultipleStringLiteralsCheck()
69     {
70         setIgnoreStringsRegexp("^\"\"$");
71     }
72
73     /**
74      * Sets regexp pattern for ignored strings.
75      * @param aIgnoreStringsRegexp regexp pattern for ignored strings
76      */

77     public void setIgnoreStringsRegexp(String JavaDoc aIgnoreStringsRegexp)
78     {
79         if ((aIgnoreStringsRegexp != null)
80             && (aIgnoreStringsRegexp.length() > 0))
81         {
82             mPattern = Utils.getPattern(aIgnoreStringsRegexp);
83         }
84         else {
85             mPattern = null;
86         }
87     }
88
89     /** {@inheritDoc} */
90     public int[] getDefaultTokens()
91     {
92         return new int[] {TokenTypes.STRING_LITERAL};
93     }
94
95     /** {@inheritDoc} */
96     public void visitToken(DetailAST aAST)
97     {
98         final String JavaDoc currentString = aAST.getText();
99         if ((mPattern == null) || !mPattern.matcher(currentString).find()) {
100             ArrayList JavaDoc hitList = (ArrayList JavaDoc) mStringMap.get(currentString);
101             if (hitList == null) {
102                 hitList = new ArrayList JavaDoc();
103                 mStringMap.put(currentString, hitList);
104             }
105             final int line = aAST.getLineNo();
106             final int col = aAST.getColumnNo();
107             hitList.add(new StringInfo(line, col));
108         }
109     }
110
111     /** {@inheritDoc} */
112     public void beginTree(DetailAST aRootAST)
113     {
114         super.beginTree(aRootAST);
115         mStringMap.clear();
116     }
117
118     /** {@inheritDoc} */
119     public void finishTree(DetailAST aRootAST)
120     {
121         final Set JavaDoc keys = mStringMap.keySet();
122         final Iterator JavaDoc keyIterator = keys.iterator();
123         while (keyIterator.hasNext()) {
124             final String JavaDoc key = (String JavaDoc) keyIterator.next();
125             final ArrayList JavaDoc hits = (ArrayList JavaDoc) mStringMap.get(key);
126             if (hits.size() > mAllowedDuplicates) {
127                 final StringInfo firstFinding = (StringInfo) hits.get(0);
128                 final int line = firstFinding.getLine();
129                 final int col = firstFinding.getCol();
130                 final Object JavaDoc[] args =
131                     new Object JavaDoc[]{key, new Integer JavaDoc(hits.size())};
132                 log(line, col, "multiple.string.literal", args);
133             }
134         }
135     }
136
137     /**
138      * This class contains information about where a string was found.
139      */

140     private static final class StringInfo
141     {
142         /**
143          * Line of finding
144          */

145         private final int mLine;
146         /**
147          * Column of finding
148          */

149         private final int mCol;
150         /**
151          * Creates information about a string position.
152          * @param aLine int
153          * @param aCol int
154          */

155         private StringInfo(int aLine, int aCol)
156         {
157             mLine = aLine;
158             mCol = aCol;
159         }
160
161         /**
162          * The line where a string was found.
163          * @return int Line of the string.
164          */

165         private int getLine()
166         {
167             return mLine;
168         }
169
170         /**
171          * The column where a string was found.
172          * @return int Column of the string.
173          */

174         private int getCol()
175         {
176             return mCol;
177         }
178     }
179
180 }
181
Popular Tags