KickJava   Java API By Example, From Geeks To Geeks.

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


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;
20
21 import java.io.File JavaDoc;
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.RandomAccessFile JavaDoc;
25
26 import com.puppycrawl.tools.checkstyle.Defn;
27 import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
28 import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
29 import com.puppycrawl.tools.checkstyle.api.MessageDispatcher;
30 import com.puppycrawl.tools.checkstyle.api.Utils;
31 import org.apache.commons.beanutils.ConversionException;
32
33 /**
34  * <p>
35  * Checks that there is a newline at the end of each file.
36  * </p>
37  * <p>
38  * An example of how to configure the check is:
39  * </p>
40  * <pre>
41  * &lt;module name="NewlineAtEndOfFile"/&gt;</pre>
42  * <p>
43  * This will check against the platform-specific default line separator.
44  * </p>
45  * <p>
46  * It is also possible to enforce the use of a specific line-separator across
47  * platforms, with the 'lineSeparator' property:
48  * </p>
49  * <pre>
50  * &lt;module name="NewlineAtEndOfFile"&gt;
51  * &lt;property name="lineSeparator" value="lf"/&gt;
52  * &lt;/module&gt;</pre>
53  * <p>
54  * Valid values for the 'lineSeparator' property are 'system' (system default),
55  * 'crlf' (windows), 'cr' (mac) and 'lf' (unix).
56  * </p>
57  *
58  * @author Christopher Lenz
59  * @author lkuehne
60  * @version 1.0
61  */

62 public class NewlineAtEndOfFileCheck
63     extends AbstractFileSetCheck
64 {
65     /** the line separator to check against. */
66     private LineSeparatorOption mLineSeparator =
67         LineSeparatorOption.SYSTEM;
68
69     /**
70      * {@inheritDoc}
71      */

72     public void process(File JavaDoc[] aFiles)
73     {
74         final File JavaDoc[] files = filter(aFiles);
75         final MessageDispatcher dispatcher = getMessageDispatcher();
76         for (int i = 0; i < files.length; i++) {
77             final File JavaDoc file = files[i];
78             final String JavaDoc path = file.getPath();
79             dispatcher.fireFileStarted(path);
80             RandomAccessFile JavaDoc randomAccessFile = null;
81             try {
82                 randomAccessFile = new RandomAccessFile JavaDoc(file, "r");
83                 if (!endsWithNewline(randomAccessFile)) {
84                     log(0, "noNewlineAtEOF", path);
85                 }
86             }
87             catch (final IOException JavaDoc e) {
88                 ///CLOVER:OFF
89
logIOException(e);
90                 ///CLOVER:ON
91
}
92             finally {
93                 if (randomAccessFile != null) {
94                     try {
95                         randomAccessFile.close();
96                     }
97                     catch (final IOException JavaDoc e) {
98                         ///CLOVER:OFF
99
logIOException(e);
100                         ///CLOVER:ON
101
}
102                 }
103             }
104             fireErrors(path);
105             dispatcher.fireFileFinished(path);
106         }
107     }
108
109     /**
110      * Sets the line separator to one of 'crlf', 'lf' or 'cr'.
111      *
112      * @param aLineSeparator The line separator to set
113      * @throws IllegalArgumentException If the specified line separator is not
114      * one of 'crlf', 'lf' or 'cr'
115      */

116     public void setLineSeparator(String JavaDoc aLineSeparator)
117     {
118         final AbstractOption option =
119             LineSeparatorOption.SYSTEM.decode(aLineSeparator);
120
121         if (option == null) {
122             throw new ConversionException("unable to parse " + aLineSeparator);
123         }
124
125         mLineSeparator = (LineSeparatorOption) option;
126     }
127
128     /**
129      * Checks whether the content provided by the Reader ends with the platform
130      * specific line separator.
131      * @param aRandomAccessFile The reader for the content to check
132      * @return boolean Whether the content ends with a line separator
133      * @throws IOException When an IO error occurred while reading from the
134      * provided reader
135      */

136     private boolean endsWithNewline(RandomAccessFile JavaDoc aRandomAccessFile)
137         throws IOException JavaDoc
138     {
139         final int len = mLineSeparator.length();
140         if (aRandomAccessFile.length() < len) {
141             return false;
142         }
143         aRandomAccessFile.seek(aRandomAccessFile.length() - len);
144         final byte lastBytes[] = new byte[len];
145         aRandomAccessFile.read(lastBytes);
146         return mLineSeparator.matches(lastBytes);
147     }
148
149     /**
150      * Helper method to log an IO exception.
151      * @param aEx the exception that occured
152      */

153     ///CLOVER:OFF
154
private void logIOException(IOException JavaDoc aEx)
155     {
156         String JavaDoc[] args = null;
157         String JavaDoc key = "general.fileNotFound";
158         if (!(aEx instanceof FileNotFoundException JavaDoc)) {
159             args = new String JavaDoc[] {aEx.getMessage()};
160             key = "general.exception";
161         }
162         final LocalizedMessage message =
163             new LocalizedMessage(
164                 0,
165                 Defn.CHECKSTYLE_BUNDLE,
166                 key,
167                 args,
168                 getId(),
169                 this.getClass());
170         getMessageCollector().add(message);
171         Utils.getExceptionLogger().debug("IOException occured.", aEx);
172     }
173     ///CLOVER:ON
174
}
175
Popular Tags