| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| NewlineAtEndOfFileCheck |
|
| 3.6666666666666665;3.667 |
| 1 | //////////////////////////////////////////////////////////////////////////////// | |
| 2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
| 3 | // Copyright (C) 2001-2014 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 com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck; | |
| 22 | import com.puppycrawl.tools.checkstyle.api.Utils; | |
| 23 | import java.io.File; | |
| 24 | import java.io.IOException; | |
| 25 | import java.io.RandomAccessFile; | |
| 26 | import java.util.List; | |
| 27 | import org.apache.commons.beanutils.ConversionException; | |
| 28 | ||
| 29 | /** | |
| 30 | * <p> | |
| 31 | * Checks that there is a newline at the end of each file. | |
| 32 | * </p> | |
| 33 | * <p> | |
| 34 | * An example of how to configure the check is: | |
| 35 | * </p> | |
| 36 | * <pre> | |
| 37 | * <module name="NewlineAtEndOfFile"/></pre> | |
| 38 | * <p> | |
| 39 | * This will check against the platform-specific default line separator. | |
| 40 | * </p> | |
| 41 | * <p> | |
| 42 | * It is also possible to enforce the use of a specific line-separator across | |
| 43 | * platforms, with the 'lineSeparator' property: | |
| 44 | * </p> | |
| 45 | * <pre> | |
| 46 | * <module name="NewlineAtEndOfFile"> | |
| 47 | * <property name="lineSeparator" value="lf"/> | |
| 48 | * </module></pre> | |
| 49 | * <p> | |
| 50 | * Valid values for the 'lineSeparator' property are 'system' (system default), | |
| 51 | * 'crlf' (windows), 'cr' (mac) and 'lf' (unix). | |
| 52 | * </p> | |
| 53 | * | |
| 54 | * @author Christopher Lenz | |
| 55 | * @author lkuehne | |
| 56 | * @version 1.0 | |
| 57 | */ | |
| 58 | 4 | public class NewlineAtEndOfFileCheck |
| 59 | extends AbstractFileSetCheck | |
| 60 | { | |
| 61 | /** the line separator to check against. */ | |
| 62 | 4 | private LineSeparatorOption mLineSeparator = LineSeparatorOption.SYSTEM; |
| 63 | ||
| 64 | @Override | |
| 65 | protected void processFiltered(File aFile, List<String> aLines) | |
| 66 | { | |
| 67 | // Cannot use aLines as the line separators have been removed! | |
| 68 | 3 | RandomAccessFile randomAccessFile = null; |
| 69 | try { | |
| 70 | 3 | randomAccessFile = new RandomAccessFile(aFile, "r"); |
| 71 | 3 | if (!endsWithNewline(randomAccessFile)) { |
| 72 | 2 | log(0, "noNewlineAtEOF", aFile.getPath()); |
| 73 | } | |
| 74 | } | |
| 75 | 0 | catch (final IOException e) { |
| 76 | 0 | log(0, "unable.open", aFile.getPath()); |
| 77 | } | |
| 78 | finally { | |
| 79 | 3 | Utils.closeQuietly(randomAccessFile); |
| 80 | 3 | } |
| 81 | 3 | } |
| 82 | ||
| 83 | /** | |
| 84 | * Sets the line separator to one of 'crlf', 'lf' or 'cr'. | |
| 85 | * | |
| 86 | * @param aLineSeparator The line separator to set | |
| 87 | * @throws IllegalArgumentException If the specified line separator is not | |
| 88 | * one of 'crlf', 'lf' or 'cr' | |
| 89 | */ | |
| 90 | public void setLineSeparator(String aLineSeparator) | |
| 91 | { | |
| 92 | try { | |
| 93 | 4 | mLineSeparator = |
| 94 | Enum.valueOf(LineSeparatorOption.class, aLineSeparator.trim() | |
| 95 | .toUpperCase()); | |
| 96 | } | |
| 97 | 1 | catch (IllegalArgumentException iae) { |
| 98 | 1 | throw new ConversionException("unable to parse " + aLineSeparator, |
| 99 | iae); | |
| 100 | 3 | } |
| 101 | 3 | } |
| 102 | ||
| 103 | /** | |
| 104 | * Checks whether the content provided by the Reader ends with the platform | |
| 105 | * specific line separator. | |
| 106 | * @param aRandomAccessFile The reader for the content to check | |
| 107 | * @return boolean Whether the content ends with a line separator | |
| 108 | * @throws IOException When an IO error occurred while reading from the | |
| 109 | * provided reader | |
| 110 | */ | |
| 111 | private boolean endsWithNewline(RandomAccessFile aRandomAccessFile) | |
| 112 | throws IOException | |
| 113 | { | |
| 114 | 3 | final int len = mLineSeparator.length(); |
| 115 | 3 | if (aRandomAccessFile.length() < len) { |
| 116 | 1 | return false; |
| 117 | } | |
| 118 | 2 | aRandomAccessFile.seek(aRandomAccessFile.length() - len); |
| 119 | 2 | final byte lastBytes[] = new byte[len]; |
| 120 | 2 | final int readBytes = aRandomAccessFile.read(lastBytes); |
| 121 | 2 | if (readBytes != len) { |
| 122 | 0 | throw new IOException("Unable to read " + len + " bytes, got " |
| 123 | + readBytes); | |
| 124 | } | |
| 125 | 2 | return mLineSeparator.matches(lastBytes); |
| 126 | } | |
| 127 | } |