KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > sizes > LineLengthCheck


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

20 package com.puppycrawl.tools.checkstyle.checks.sizes;
21
22 import com.puppycrawl.tools.checkstyle.api.Check;
23 import com.puppycrawl.tools.checkstyle.api.DetailAST;
24 import com.puppycrawl.tools.checkstyle.api.Utils;
25
26 import java.util.regex.Pattern JavaDoc;
27 import java.util.regex.PatternSyntaxException JavaDoc;
28
29 import org.apache.commons.beanutils.ConversionException;
30
31 /**
32  * Checks for long lines.
33  *
34  * <p>
35  * Rationale: Long lines are hard to read in printouts or if developers
36  * have limited screen space for the source code, e.g. if the IDE displays
37  * additional information like project tree, class hierarchy, etc.
38  * </p>
39  *
40  * <p>
41  * Note: Support for the special handling of imports in CheckStyle Version 2
42  * has been dropped as it is a special case of regexp: The user can set
43  * the ignorePattern to "^import" and achieve the same effect.
44  * </p>
45  * <p>
46  * The default maximum allowable line length is 80 characters. To change the
47  * maximum, set property max.
48  * </p>
49  * <p>
50  * To ignore lines in the check, set property ignorePattern to a regular
51  * expression for the lines to ignore.
52  * </p>
53  * <p>
54  * An example of how to configure the check is:
55  * </p>
56  * <pre>
57  * &lt;module name="LineLength"/&gt;
58  * </pre>
59  * <p> An example of how to configure the check to accept lines up to 120
60  * characters long is:
61  *</p>
62  * <pre>
63  * &lt;module name="LineLength"&gt;
64  * &lt;property name="max" value="120"/&gt;
65  * &lt;/module&gt;
66  * </pre>
67  * <p> An example of how to configure the check to ignore lines that begin with
68  * &quot; * &quot;, followed by just one word, such as within a Javadoc comment,
69  * is:
70  * </p>
71  * <pre>
72  * &lt;module name="LineLength"&gt;
73  * &lt;property name="ignorePattern" value="^ *\* *[^ ]+$"/&gt;
74  * &lt;/module&gt;
75  * </pre>
76  *
77  * @author Lars Kühne
78  */

79 public class LineLengthCheck extends Check
80 {
81     /** default maximum number of columns in a line */
82     private static final int DEFAULT_MAX_COLUMNS = 80;
83
84     /** the maximum number of columns in a line */
85     private int mMax = DEFAULT_MAX_COLUMNS;
86
87     /** the regexp when long lines are ignored */
88     private Pattern JavaDoc mIgnorePattern;
89
90     /**
91      * Creates a new <code>LineLengthCheck</code> instance.
92      */

93     public LineLengthCheck()
94     {
95         setIgnorePattern("^$");
96     }
97
98     /** {@inheritDoc} */
99     public int[] getDefaultTokens()
100     {
101         return new int[0];
102     }
103
104     /** {@inheritDoc} */
105     public void beginTree(DetailAST aRootAST)
106     {
107         final String JavaDoc[] lines = getLines();
108         for (int i = 0; i < lines.length; i++) {
109
110             final String JavaDoc line = lines[i];
111             final int realLength = Utils.lengthExpandedTabs(
112                 line, line.length(), getTabWidth());
113
114
115             if ((realLength > mMax)
116                 && !mIgnorePattern.matcher(line).find())
117             {
118                 log(i + 1, "maxLineLen", new Integer JavaDoc(mMax));
119             }
120         }
121     }
122
123     /**
124      * @param aLength the maximum length of a line
125      */

126     public void setMax(int aLength)
127     {
128         mMax = aLength;
129     }
130
131     /**
132      * Set the ignore pattern.
133      * @param aFormat a <code>String</code> value
134      * @throws ConversionException unable to parse aFormat
135      */

136     public void setIgnorePattern(String JavaDoc aFormat)
137         throws ConversionException
138     {
139         try {
140             mIgnorePattern = Utils.getPattern(aFormat);
141         }
142         catch (final PatternSyntaxException JavaDoc e) {
143             throw new ConversionException("unable to parse " + aFormat, e);
144         }
145     }
146
147 }
148
Popular Tags