Search in sources :

Example 21 with FileText

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;
}
Also used : RegexpSinglelineJavaCheck(com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck) Arrays(java.util.Arrays) MessageFormat(java.text.MessageFormat) AbstractSuperCheck(com.puppycrawl.tools.checkstyle.checks.coding.AbstractSuperCheck) HashSet(java.util.HashSet) JavadocUtil(com.puppycrawl.tools.checkstyle.utils.JavadocUtil) Charset(java.nio.charset.Charset) Locale(java.util.Locale) Document(org.w3c.dom.Document) Node(org.w3c.dom.Node) TokenUtil(com.puppycrawl.tools.checkstyle.utils.TokenUtil) ClassPath(com.google.common.reflect.ClassPath) ModuleReflectionUtil(com.puppycrawl.tools.checkstyle.utils.ModuleReflectionUtil) Properties(java.util.Properties) NodeList(org.w3c.dom.NodeList) AbstractParenPadCheck(com.puppycrawl.tools.checkstyle.checks.whitespace.AbstractParenPadCheck) Set(java.util.Set) IOException(java.io.IOException) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) File(java.io.File) FileText(com.puppycrawl.tools.checkstyle.api.FileText) Element(org.w3c.dom.Element) DocumentBuilder(javax.xml.parsers.DocumentBuilder) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) RegexpMultilineCheck(com.puppycrawl.tools.checkstyle.checks.regexp.RegexpMultilineCheck) RegexpSinglelineCheck(com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineCheck) AbstractAccessControlNameCheck(com.puppycrawl.tools.checkstyle.checks.naming.AbstractAccessControlNameCheck) AbstractNameCheck(com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck) FileText(com.puppycrawl.tools.checkstyle.api.FileText) File(java.io.File)

Example 22 with FileText

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        " + "&amp;1line\\n        &gt;2line\\n        &lt;3line\\n        ']");
    assertWithMessage("Generated queries do not match expected ones").that(expected).isEqualTo(actual);
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) FileText(com.puppycrawl.tools.checkstyle.api.FileText) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 23 with FileText

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);
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) FileText(com.puppycrawl.tools.checkstyle.api.FileText) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 24 with FileText

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);
        }
    }
}
Also used : TokenStreamException(antlr.TokenStreamException) TokenStreamRecognitionException(antlr.TokenStreamRecognitionException) FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) FileText(com.puppycrawl.tools.checkstyle.api.FileText) TokenStreamRecognitionException(antlr.TokenStreamRecognitionException) RecognitionException(antlr.RecognitionException)

Example 25 with FileText

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);
}
Also used : FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) FileText(com.puppycrawl.tools.checkstyle.api.FileText)

Aggregations

FileText (com.puppycrawl.tools.checkstyle.api.FileText)49 File (java.io.File)41 Test (org.junit.jupiter.api.Test)29 FileContents (com.puppycrawl.tools.checkstyle.api.FileContents)18 DetailAST (com.puppycrawl.tools.checkstyle.api.DetailAST)15 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)7 Violation (com.puppycrawl.tools.checkstyle.api.Violation)7 ArrayList (java.util.ArrayList)6 TreeWalkerAuditEvent (com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent)5 DefaultConfiguration (com.puppycrawl.tools.checkstyle.DefaultConfiguration)4 TypeNameCheck (com.puppycrawl.tools.checkstyle.checks.naming.TypeNameCheck)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 TestLoggingReporter (com.puppycrawl.tools.checkstyle.internal.testmodules.TestLoggingReporter)2 IOException (java.io.IOException)2 Field (java.lang.reflect.Field)2 RecognitionException (antlr.RecognitionException)1 TokenStreamException (antlr.TokenStreamException)1 TokenStreamRecognitionException (antlr.TokenStreamRecognitionException)1 ClassPath (com.google.common.reflect.ClassPath)1 LineColumn (com.puppycrawl.tools.checkstyle.api.LineColumn)1