KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > header > HeaderCheck


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.header;
21
22 import java.util.Arrays JavaDoc;
23
24 import com.puppycrawl.tools.checkstyle.api.DetailAST;
25 import com.puppycrawl.tools.checkstyle.checks.AbstractHeaderCheck;
26
27 /**
28  * Checks the header of the source against a fixed header file.
29  *
30  * @author Lars Kühne
31  */

32 public class HeaderCheck extends AbstractHeaderCheck
33 {
34     /** empty array to avoid instantiations. */
35     private static final int[] EMPTY_INT_ARRAY = new int[0];
36
37     /** the header lines to ignore in the check, sorted. */
38     private int[] mIgnoreLines = EMPTY_INT_ARRAY;
39
40     /**
41      * @param aLineNo a line number
42      * @return if <code>aLineNo</code> is one of the ignored header lines.
43      */

44     private boolean isIgnoreLine(int aLineNo)
45     {
46         return (Arrays.binarySearch(mIgnoreLines, aLineNo) >= 0);
47     }
48
49     /**
50      * Checks if a code line matches the required header line.
51      * @param aLineNumber the linenumber to check against the header
52      * @return true if and only if the line matches the required header line
53      */

54     protected boolean isMatch(int aLineNumber)
55     {
56         final String JavaDoc line = getLines()[aLineNumber];
57         // skip lines we are meant to ignore
58
return isIgnoreLine(aLineNumber + 1)
59             || getHeaderLines()[aLineNumber].equals(line);
60     }
61
62     /**
63      * Set the lines numbers to ignore in the header check.
64      * @param aList comma separated list of line numbers to ignore in header.
65      */

66     public void setIgnoreLines(int[] aList)
67     {
68         if ((aList == null) || (aList.length == 0)) {
69             mIgnoreLines = EMPTY_INT_ARRAY;
70             return;
71         }
72
73         mIgnoreLines = new int[aList.length];
74         System.arraycopy(aList, 0, mIgnoreLines, 0, aList.length);
75         Arrays.sort(mIgnoreLines);
76     }
77
78     /** {@inheritDoc} */
79     public void beginTree(DetailAST aRootAST)
80     {
81         if (getHeaderLines().length > getLines().length) {
82             log(1, "header.missing");
83         }
84         else {
85             for (int i = 0; i < getHeaderLines().length; i++) {
86                 if (!isMatch(i)) {
87                     log(i + 1, "header.mismatch", getHeaderLines()[i]);
88                     break; // stop checking
89
}
90             }
91         }
92     }
93 }
94
Popular Tags