KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > AbstractHeaderCheck


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;
21
22 import java.io.FileReader JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.LineNumberReader JavaDoc;
25 import java.io.Reader JavaDoc;
26 import java.io.StringReader JavaDoc;
27 import java.util.ArrayList JavaDoc;
28
29 import com.puppycrawl.tools.checkstyle.api.Check;
30 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
31
32 import org.apache.commons.beanutils.ConversionException;
33
34 /**
35  * Abstract super class for header checks.
36  * Provides support for headerFile property.
37  * @author o_sukhosolsky
38  */

39 public abstract class AbstractHeaderCheck extends Check
40 {
41     /** the lines of the header file. */
42     private String JavaDoc[] mHeaderLines;
43
44     /**
45      * Return the header lines to check against.
46      * @return the header lines to check against.
47      */

48     protected String JavaDoc[] getHeaderLines()
49     {
50         return mHeaderLines;
51     }
52
53     /**
54      * Set the header file to check against.
55      * @param aFileName the file that contains the header to check against.
56      * @throws ConversionException if the file cannot be loaded
57      */

58     public void setHeaderFile(String JavaDoc aFileName)
59         throws ConversionException
60     {
61         // Handle empty param
62
if ((aFileName == null) || (aFileName.trim().length() == 0)) {
63             return;
64         }
65
66         checkHeaderNotInitialized();
67
68         // load the file
69
Reader JavaDoc headerReader = null;
70         try {
71             headerReader = new FileReader JavaDoc(aFileName);
72             loadHeader(headerReader);
73         }
74         catch (final IOException JavaDoc ex) {
75             throw new ConversionException(
76                     "unable to load header file " + aFileName, ex);
77         }
78         finally {
79             if (headerReader != null) {
80                 try {
81                     headerReader.close();
82                 }
83                 catch (final IOException JavaDoc ex) {
84                     throw new ConversionException(
85                             "unable to close header file " + aFileName, ex);
86                 }
87             }
88         }
89     }
90
91     /**
92      * Set the header to check against. Individual lines in the header
93      * must be separated by '\n' characters.
94      * @param aHeader header content to check against.
95      * @throws ConversionException if the header cannot be interpreted
96      */

97     public void setHeader(String JavaDoc aHeader)
98     {
99         if ((aHeader == null) || (aHeader.trim().length() == 0)) {
100             return;
101         }
102
103         checkHeaderNotInitialized();
104
105         final String JavaDoc headerExpandedNewLines = aHeader.replaceAll("\\\\n", "\n");
106
107         final Reader JavaDoc headerReader = new StringReader JavaDoc(headerExpandedNewLines);
108         try {
109             loadHeader(headerReader);
110         }
111         catch (final IOException JavaDoc ex) {
112             throw new ConversionException(
113                     "unable to load header", ex);
114         }
115         finally {
116             try {
117                 headerReader.close();
118             }
119             catch (final IOException JavaDoc ex) {
120                 // shouldn't happen with StringReader
121
throw new ConversionException(
122                         "unable to close header", ex);
123             }
124         }
125
126     }
127
128     /**
129      * Called before initializing the header.
130      * @throws ConversionException if header has already been set
131      */

132     private void checkHeaderNotInitialized()
133     {
134         if (mHeaderLines != null) {
135             throw new ConversionException(
136                     "header has already been set - "
137                     + "set either header or headerFile, not both");
138         }
139     }
140
141     /**
142      * Load header to check against from a Reader into mHeaderLines.
143      * @param aHeaderReader delivers the header to check against
144      * @throws IOException if
145      */

146     private void loadHeader(final Reader JavaDoc aHeaderReader) throws IOException JavaDoc
147     {
148         final LineNumberReader JavaDoc lnr = new LineNumberReader JavaDoc(aHeaderReader);
149         final ArrayList JavaDoc lines = new ArrayList JavaDoc();
150         while (true) {
151             final String JavaDoc l = lnr.readLine();
152             if (l == null) {
153                 break;
154             }
155             lines.add(l);
156         }
157         mHeaderLines = (String JavaDoc[]) lines.toArray(new String JavaDoc[0]);
158     }
159
160     /**
161      * Checks that required args were specified.
162      * @throws CheckstyleException {@inheritDoc}
163      * @see com.puppycrawl.tools.checkstyle.api.AutomaticBean#finishLocalSetup
164      */

165     protected final void finishLocalSetup() throws CheckstyleException
166     {
167         if (mHeaderLines == null) {
168             throw new CheckstyleException(
169                     "property 'headerFile' is missing or invalid in module "
170                     + getConfiguration().getName());
171         }
172     }
173
174     /** {@inheritDoc} */
175     public final int[] getDefaultTokens()
176     {
177         return new int[0];
178     }
179 }
180
Popular Tags