use of com.puppycrawl.tools.checkstyle.api.FileText in project checkstyle by checkstyle.
the class CheckUtil method getLineSeparatorForFile.
public static String getLineSeparatorForFile(String filepath, Charset charset) throws IOException {
final boolean[] crFound = { false };
new FileText(new File(filepath), charset.name()).getFullText().chars().peek(character -> {
if (character == '\r') {
crFound[0] = true;
}
}).filter(character -> character == '\n').findFirst();
final String result;
if (crFound[0]) {
result = CRLF;
} else {
result = "\n";
}
return result;
}
use of com.puppycrawl.tools.checkstyle.api.FileText in project checkstyle by checkstyle.
the class XpathQueryGeneratorTest method testTextBlocks.
@Test
public void testTextBlocks() throws Exception {
final File testFile = new File(getNonCompilablePath("InputXpathQueryGeneratorTextBlock.java"));
final FileText testFileText = new FileText(testFile, StandardCharsets.UTF_8.name());
final DetailAST detailAst = JavaParser.parseFile(testFile, JavaParser.Options.WITHOUT_COMMENTS);
final int tabWidth = 8;
final int lineNumber = 6;
final int columnNumber = 25;
final XpathQueryGenerator queryGenerator = new XpathQueryGenerator(detailAst, lineNumber, columnNumber, testFileText, tabWidth);
final List<String> actual = queryGenerator.generate();
final List<String> expected = Collections.singletonList("/COMPILATION_UNIT/CLASS_DEF" + "[./IDENT[@text='InputXpathQueryGeneratorTextBlock']]/OBJBLOCK/" + "VARIABLE_DEF[./IDENT[@text='testOne']]/ASSIGN/EXPR/" + "TEXT_BLOCK_LITERAL_BEGIN/TEXT_BLOCK_CONTENT[@text='\\n " + "&1line\\n >2line\\n <3line\\n ']");
assertWithMessage("Generated queries do not match expected ones").that(expected).isEqualTo(actual);
}
use of com.puppycrawl.tools.checkstyle.api.FileText in project checkstyle by checkstyle.
the class XpathQueryGeneratorTest method testTabWidthEndOfLine.
@Test
public void testTabWidthEndOfLine() throws Exception {
final File testFile = new File(getPath("InputXpathQueryGeneratorTabWidth.java"));
final FileText testFileText = new FileText(testFile, StandardCharsets.UTF_8.name());
final DetailAST detailAst = JavaParser.parseFile(testFile, JavaParser.Options.WITHOUT_COMMENTS);
final int lineNumber = 16;
final int columnNumber = 58;
final int tabWidth = 8;
final XpathQueryGenerator queryGenerator = new XpathQueryGenerator(detailAst, lineNumber, columnNumber, testFileText, tabWidth);
final List<String> actual = queryGenerator.generate();
final List<String> expected = Collections.singletonList("/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='InputXpathQueryGeneratorTabWidth']]" + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='endLineTab']]/SEMI");
assertWithMessage("Generated queries do not match expected ones").that(actual).isEqualTo(expected);
}
use of com.puppycrawl.tools.checkstyle.api.FileText in project checkstyle by checkstyle.
the class TreeWalker method processFiltered.
@Override
protected void processFiltered(File file, List<String> lines) throws CheckstyleException {
// check if already checked and passed the file
if (CommonUtils.matchesFileExtension(file, getFileExtensions())) {
final String msg = "%s occurred during the analysis of file %s.";
final String fileName = file.getPath();
try {
final FileText text = FileText.fromLines(file, lines);
final FileContents contents = new FileContents(text);
final DetailAST rootAST = parse(contents);
getMessageCollector().reset();
walk(rootAST, contents, AstState.ORDINARY);
final DetailAST astWithComments = appendHiddenCommentNodes(rootAST);
walk(astWithComments, contents, AstState.WITH_COMMENTS);
} catch (final TokenStreamRecognitionException tre) {
final String exceptionMsg = String.format(Locale.ROOT, msg, "TokenStreamRecognitionException", fileName);
throw new CheckstyleException(exceptionMsg, tre);
} catch (RecognitionException | TokenStreamException ex) {
final String exceptionMsg = String.format(Locale.ROOT, msg, ex.getClass().getSimpleName(), fileName);
throw new CheckstyleException(exceptionMsg, ex);
}
}
}
use of com.puppycrawl.tools.checkstyle.api.FileText in project checkstyle by checkstyle.
the class MainFrameModel method parseFileWithComments.
/**
* Parse a file and return the parse tree with comment nodes.
* @param file the file to parse.
* @return the root node of the parse tree.
* @throws IOException if the file could not be read.
* @throws ANTLRException if the file is not a Java source.
*/
public DetailAST parseFileWithComments(File file) throws IOException, ANTLRException {
final FileText fileText = getFileText(file);
final FileContents contents = new FileContents(fileText);
return TreeWalker.parseWithComments(contents);
}
Aggregations