/** * Method returns line number the key is detected in the checked properties * files first. * * @param fileText * {@link FileText} object contains the lines to process * @param keyName * key name to look for * @return line number of first occurrence. If no key found in properties * file, 1 is returned */ private static int getLineNumber(FileText fileText, String keyName) { final Pattern keyPattern = getKeyPattern(keyName); int lineNumber = 1; final Matcher matcher = keyPattern.matcher(""); for (int index = 0; index < fileText.size(); index++) { final String line = fileText.get(index); matcher.reset(line); if (matcher.matches()) { break; } ++lineNumber; } // -1 as check seeks for the first duplicate occurrence in file, // so it cannot be the last line. if (lineNumber > fileText.size() - 1) { lineNumber = 1; } return lineNumber; }
/** * Parse a file and return the parse tree. * @param file the file to parse. * @return the root node of the parse tree. * @throws IOException if the file could not be read. */ private static DetailNode parseFile(File file) throws IOException { final FileText text = new FileText(file.getAbsoluteFile(), System.getProperty("file.encoding", StandardCharsets.UTF_8.name())); return parseJavadocAsDetailNode(text.getFullText().toString()); }
/** * Get a single line. * For internal use only, as getText().get(lineNo) is just as * suitable for external use and avoids method duplication. * @param lineNo the number of the line to get * @return the corresponding line, without terminator * @throws IndexOutOfBoundsException if lineNo is invalid */ private String line(int lineNo) { return text.get(lineNo); }
/** * Get the full text of the file. * @return an object containing the full text of the file */ public FileText getText() { return new FileText(text); }
@Test public void testLineColumnBeforeCopyConstructor() throws IOException { final String charsetName = StandardCharsets.ISO_8859_1.name(); final FileText fileText = new FileText(new File(getPath("InputFileTextImportControl.xml")), charsetName); final LineColumn lineColumn = fileText.lineColumn(100); final FileText copy = new FileText(fileText); assertEquals("Invalid linecolumn", lineColumn, copy.lineColumn(100)); }
@Override public void beginTree(DetailAST rootAST) { matcher = format.matcher(getFileContents().getText().getFullText()); matchCount = 0; errorCount = 0; findMatch(); }
/** * Gets the lines in the file. * @return the lines in the file */ public String[] getLines() { return text.toLinesArray(); }
/** * Detect ignore situation. * @param startLine position of line * @param text file text * @param start line column * @return true is that need to be ignored */ private boolean isIgnore(int startLine, FileText text, LineColumn start) { final LineColumn end; if (matcher.end() == 0) { end = text.lineColumn(0); } else { end = text.lineColumn(matcher.end() - 1); } boolean ignore = false; if (ignoreComments) { final FileContents theFileContents = getFileContents(); final int startColumn = start.getColumn(); final int endLine = end.getLine(); final int endColumn = end.getColumn(); ignore = theFileContents.hasIntersectionWithComment(startLine, startColumn, endLine, endColumn); } return ignore; }
@Test public void testSupportedCharset() throws IOException { final String charsetName = StandardCharsets.ISO_8859_1.name(); final FileText fileText = new FileText(new File(getPath("InputFileTextImportControl.xml")), charsetName); assertEquals("Invalid charset name", charsetName, fileText.getCharset().name()); }
@Override protected void processFiltered(File aFile, List<String> aLines) { String text = FileText.fromLines(aFile, aLines).getFullText().toString();
@Override public void beginTree(DetailAST rootAST) { currentImportControl = null; processCurrentFile = path.matcher(getFileContents().getFileName()).find(); fileName = getFileContents().getText().getFile().getName(); final int period = fileName.lastIndexOf('.'); if (period != -1) { fileName = fileName.substring(0, period); } }
/** * Get the line from text of the file. * @param index index of the line * @return line from text of the file */ public String getLine(int index) { return text.get(index); }
/** * Get FileText from a file. * @param file the file to get the FileText from. * @return the FileText. * @throws IOException if the file could not be read. */ private static FileText getFileText(File file) throws IOException { return new FileText(file.getAbsoluteFile(), System.getProperty("file.encoding", StandardCharsets.UTF_8.name())); }
/** * Returns the list of {@link Suppression} instances retrieved from the given {@link FileText}. * @param fileText {@link FileText} instance. * @return list of {@link Suppression} instances. */ private List<Suppression> getSuppressions(FileText fileText) { final List<Suppression> suppressions = new ArrayList<>(); for (int lineNo = 0; lineNo < fileText.size(); lineNo++) { final Optional<Suppression> suppression = getSuppression(fileText, lineNo); suppression.ifPresent(suppressions::add); } return suppressions; }
@Test public void testLineColumnAtTheStartOfFile() throws IOException { final String charsetName = StandardCharsets.ISO_8859_1.name(); final FileText fileText = new FileText(new File(getPath("InputFileTextImportControl.xml")), charsetName); final FileText copy = new FileText(fileText); final LineColumn lineColumn = copy.lineColumn(0); assertEquals("Invalid line", 1, lineColumn.getLine()); assertEquals("Invalid column", 0, lineColumn.getColumn()); }
/** * Static helper method to parses a Java source file. * @param contents contains the contents of the file * @return the root of the AST * @throws CheckstyleException if the contents is not a valid Java source */ public static DetailAST parse(FileContents contents) throws CheckstyleException { final String fullText = contents.getText().getFullText().toString(); final Reader reader = new StringReader(fullText); final GeneratedJavaLexer lexer = new GeneratedJavaLexer(reader); lexer.setCommentListener(contents); lexer.setTokenObjectClass("antlr.CommonHiddenStreamToken"); final TokenStreamHiddenTokenFilter filter = new TokenStreamHiddenTokenFilter(lexer); filter.hide(TokenTypes.SINGLE_LINE_COMMENT); filter.hide(TokenTypes.BLOCK_COMMENT_BEGIN); final GeneratedJavaRecognizer parser = new GeneratedJavaRecognizer(filter); parser.setFilename(contents.getFileName()); parser.setASTNodeClass(DetailAST.class.getName()); try { parser.compilationUnit(); } catch (RecognitionException | TokenStreamException ex) { final String exceptionMsg = String.format(Locale.ROOT, "%s occurred while parsing file %s.", ex.getClass().getSimpleName(), contents.getFileName()); throw new CheckstyleException(exceptionMsg, ex); } return (DetailAST) parser.getAST(); }
final String[] sourceLines = getFileText(file).toLinesArray();
/** Method that finds the matches. */ private void findMatch() { try { boolean foundMatch = matcher.find(); while (foundMatch) { currentMatches++; if (currentMatches > options.getMaximum()) { final LineColumn start = text.lineColumn(matcher.start()); if (options.getMessage().isEmpty()) { options.getReporter().log(start.getLine(), MSG_REGEXP_EXCEEDED, matcher.pattern().toString()); } else { options.getReporter() .log(start.getLine(), options.getMessage()); } } foundMatch = matcher.find(); } } // see http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6337993 et al. catch (StackOverflowError ignored) { // OK http://blog.igorminar.com/2008/05/catching-stackoverflowerror-and-bug-in.html // http://programmers.stackexchange.com/questions/ // 209099/is-it-ever-okay-to-catch-stackoverflowerror-in-java options.getReporter().log(1, MSG_STACKOVERFLOW, matcher.pattern().toString()); } }