use of com.puppycrawl.tools.checkstyle.api.FileText in project checkstyle by checkstyle.
the class SuppressionsStringPrinter method printSuppressions.
/**
* Prints generated suppressions.
*
* @param file the file to process.
* @param suppressionLineColumnNumber line and column number of the suppression
* @param tabWidth length of the tab character
* @return generated suppressions.
* @throws IOException if the file could not be read.
* @throws IllegalStateException if suppressionLineColumnNumber is not of a valid format.
* @throws CheckstyleException if the file is not a Java source.
*/
public static String printSuppressions(File file, String suppressionLineColumnNumber, int tabWidth) throws IOException, CheckstyleException {
final Matcher matcher = VALID_SUPPRESSION_LINE_COLUMN_NUMBER_REGEX.matcher(suppressionLineColumnNumber);
if (!matcher.matches()) {
final String exceptionMsg = String.format(Locale.ROOT, "%s does not match valid format 'line:column'.", suppressionLineColumnNumber);
throw new IllegalStateException(exceptionMsg);
}
final FileText fileText = new FileText(file.getAbsoluteFile(), System.getProperty("file.encoding", StandardCharsets.UTF_8.name()));
final DetailAST detailAST = JavaParser.parseFileText(fileText, JavaParser.Options.WITH_COMMENTS);
final int lineNumber = Integer.parseInt(matcher.group(1));
final int columnNumber = Integer.parseInt(matcher.group(2));
return generate(fileText, detailAST, lineNumber, columnNumber, tabWidth);
}
use of com.puppycrawl.tools.checkstyle.api.FileText in project checkstyle by checkstyle.
the class RegexpCheck method findMatch.
/**
* Recursive method that finds the matches.
*/
// suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
@SuppressWarnings("deprecation")
private void findMatch() {
final boolean foundMatch = matcher.find();
if (foundMatch) {
final FileText text = getFileContents().getText();
final LineColumn start = text.lineColumn(matcher.start());
final int startLine = start.getLine();
final boolean ignore = isIgnore(startLine, text, start);
if (!ignore) {
matchCount++;
if (illegalPattern || checkForDuplicates && matchCount - 1 > duplicateLimit) {
errorCount++;
logMessage(startLine);
}
}
if (canContinueValidation(ignore)) {
findMatch();
}
} else if (!illegalPattern && matchCount == 0) {
logMessage(0);
}
}
use of com.puppycrawl.tools.checkstyle.api.FileText in project checkstyle by checkstyle.
the class SuppressWithPlainTextCommentFilter method getFileText.
/**
* Returns {@link FileText} instance created based on the given file name.
*
* @param fileName the name of the file.
* @return {@link FileText} instance.
* @throws IllegalStateException if the file could not be read.
*/
private static FileText getFileText(String fileName) {
final File file = new File(fileName);
FileText result = null;
// some violations can be on a directory, instead of a file
if (!file.isDirectory()) {
try {
result = new FileText(file, StandardCharsets.UTF_8.name());
} catch (IOException ex) {
throw new IllegalStateException("Cannot read source file: " + fileName, ex);
}
}
return result;
}
use of com.puppycrawl.tools.checkstyle.api.FileText in project checkstyle by checkstyle.
the class MultilineDetector method processLines.
/**
* Processes an entire text file looking for matches.
*
* @param fileText the text to process
*/
public void processLines(FileText fileText) {
text = new FileText(fileText);
resetState();
final String format = options.getFormat();
if (format == null || format.isEmpty()) {
options.getReporter().log(1, MSG_EMPTY);
} else {
matcher = options.getPattern().matcher(fileText.getFullText());
findMatch();
finish();
}
}
use of com.puppycrawl.tools.checkstyle.api.FileText in project checkstyle by checkstyle.
the class XpathFileGeneratorAuditListenerTest method createTreeWalkerAuditEvent.
private static TreeWalkerAuditEvent createTreeWalkerAuditEvent(String fileName, Violation violation) throws Exception {
final File file = new File(getPath(fileName));
final FileText fileText = new FileText(file.getAbsoluteFile(), System.getProperty("file.encoding", StandardCharsets.UTF_8.name()));
final FileContents fileContents = new FileContents(fileText);
final DetailAST rootAst = JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS);
return new TreeWalkerAuditEvent(fileContents, fileName, violation, rootAst);
}
Aggregations