diff --git a/src/main/java/com/sleekbyte/tailor/common/Messages.java b/src/main/java/com/sleekbyte/tailor/common/Messages.java index bb7978b5..bd2fc184 100644 --- a/src/main/java/com/sleekbyte/tailor/common/Messages.java +++ b/src/main/java/com/sleekbyte/tailor/common/Messages.java @@ -81,6 +81,7 @@ public class Messages { public static final String FORCE_CAST = "Force casts should be avoided"; public static final String BLANK_LINE_BEFORE = "must have at least one blank line before it"; public static final String BLANK_LINE_AFTER = "must have at least one blank line after it"; + public static final String LEADING_WHITESPACE = "should not start with whitespace"; // Usage messages public static final String CMD_LINE_SYNTAX = "tailor"; diff --git a/src/main/java/com/sleekbyte/tailor/listeners/FileListener.java b/src/main/java/com/sleekbyte/tailor/listeners/FileListener.java index 2e2bf213..835b2e23 100644 --- a/src/main/java/com/sleekbyte/tailor/listeners/FileListener.java +++ b/src/main/java/com/sleekbyte/tailor/listeners/FileListener.java @@ -40,6 +40,7 @@ public void verify() throws IOException { verifyFileLength(maxLengths.maxFileLength); verifyLineLengths(maxLengths.maxLineLength); verifyNewlineTerminated(); + verifyNoLeadingWhitespace(); } private void verifyFileLength(int maxLines) throws IOException { @@ -71,4 +72,11 @@ private void verifyNewlineTerminated() throws IOException { } } + private void verifyNoLeadingWhitespace() throws IOException { + if (SourceFileUtil.hasLeadingWhitespace(this.inputFile)) { + Location location = new Location(1, 1); + this.printer.warn(Messages.FILE + Messages.LEADING_WHITESPACE, location); + } + } + } diff --git a/src/main/java/com/sleekbyte/tailor/utils/SourceFileUtil.java b/src/main/java/com/sleekbyte/tailor/utils/SourceFileUtil.java index 5dbadfed..f5c11654 100644 --- a/src/main/java/com/sleekbyte/tailor/utils/SourceFileUtil.java +++ b/src/main/java/com/sleekbyte/tailor/utils/SourceFileUtil.java @@ -2,6 +2,7 @@ import org.antlr.v4.runtime.ParserRuleContext; +import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.LineNumberReader; @@ -110,4 +111,20 @@ public static boolean singleNewlineTerminated(File inputFile) throws IOException randomAccessFile.seek(inputFile.length() - 2); return (Byte.compare(randomAccessFile.readByte(), NEWLINE_DELIMITER) != 0); } + + /** + * Checks whether a file contains any leading whitespace characters. + * + * @param inputFile the file to check for leading whitespace + * @return true if file starts with whitespace + * @throws IOException if the file cannot be read + */ + public static boolean hasLeadingWhitespace(File inputFile) throws IOException { + BufferedReader reader = Files.newBufferedReader(inputFile.toPath()); + int character = reader.read(); + if (character != -1 && Character.isWhitespace((char) character)) { + return true; + } + return false; + } } diff --git a/src/test/java/com/sleekbyte/tailor/utils/SourceFileUtilTest.java b/src/test/java/com/sleekbyte/tailor/utils/SourceFileUtilTest.java index 0f52e90f..41f29cef 100644 --- a/src/test/java/com/sleekbyte/tailor/utils/SourceFileUtilTest.java +++ b/src/test/java/com/sleekbyte/tailor/utils/SourceFileUtilTest.java @@ -205,6 +205,41 @@ public void testNewlineTerminatedWithSomeContentAndMultipleNewlines() throws IOE assertFalse(SourceFileUtil.singleNewlineTerminated(inputFile)); } + @Test + public void testHasLeadingWhitespaceBlankFile() throws IOException { + assertFalse(SourceFileUtil.hasLeadingWhitespace(inputFile)); + } + + @Test + public void testHasLeadingWhitespaceOnlyNewline() throws IOException { + writeNumOfLines(1, ""); + assertTrue(SourceFileUtil.hasLeadingWhitespace(inputFile)); + } + + @Test + public void testHasLeadingWhitespaceWithSingleLine() throws IOException { + writeNumOfLines(1, NORMAL_LINE); + assertFalse(SourceFileUtil.hasLeadingWhitespace(inputFile)); + } + + @Test + public void testHasLeadingWhitespaceWithSingleLineAndPrecedingNewline() throws IOException { + writeNumOfLines(1, "\n" + NORMAL_LINE); + assertTrue(SourceFileUtil.hasLeadingWhitespace(inputFile)); + } + + @Test + public void testHasLeadingWhitespaceWithSingleLineAndPrecedingSpace() throws IOException { + writeNumOfLines(1, " " + NORMAL_LINE); + assertTrue(SourceFileUtil.hasLeadingWhitespace(inputFile)); + } + + @Test + public void testHasLeadingWhitespaceWithSingleLineAndPrecedingTab() throws IOException { + writeNumOfLines(1, "\t" + NORMAL_LINE); + assertTrue(SourceFileUtil.hasLeadingWhitespace(inputFile)); + } + private void writeNumOfLines(int numOfLines, String data) { for (int i = 1; i <= numOfLines; i++) { writer.println(data);