final String stringAst = DetailNodeTreeStringPrinter.printFileAst(file); System.out.print(stringAst);
/** * Print AST. * @param ast the root AST node. * @param rootPrefix prefix for the root node * @param prefix prefix for other nodes * @return string AST. */ public static String printTree(DetailNode ast, String rootPrefix, String prefix) { final StringBuilder messageBuilder = new StringBuilder(1024); DetailNode node = ast; while (node != null) { if (node.getType() == JavadocTokenTypes.JAVADOC) { messageBuilder.append(rootPrefix); } else { messageBuilder.append(prefix); } messageBuilder.append(getIndentation(node)) .append(JavadocUtil.getTokenName(node.getType())).append(" -> ") .append(JavadocUtil.escapeAllControlChars(node.getText())).append(" [") .append(node.getLineNumber()).append(':').append(node.getColumnNumber()) .append(']').append(LINE_SEPARATOR) .append(printTree(JavadocUtil.getFirstChild(node), rootPrefix, prefix)); node = JavadocUtil.getNextSibling(node); } return messageBuilder.toString(); }
/** * Parses block comment as javadoc and prints its tree. * @param node block comment begin * @return string javadoc tree */ private static String parseAndPrintJavadocTree(DetailAST node) { final DetailAST javadocBlock = node.getParent(); final DetailNode tree = DetailNodeTreeStringPrinter.parseJavadocAsDetailNode(javadocBlock); String baseIndentation = getIndentation(node); baseIndentation = baseIndentation.substring(0, baseIndentation.length() - 2); final String rootPrefix = baseIndentation + " `--"; final String prefix = baseIndentation + " "; return DetailNodeTreeStringPrinter.printTree(tree, rootPrefix, prefix); }
@Test public void testParseJavadocAsDetailNode() throws Exception { final DetailAST ast = JavaParser.parseFile( new File(getPath("InputJavadocDetailNodeParser.java")), JavaParser.Options.WITH_COMMENTS) .getNextSibling().getFirstChild().getFirstChild(); final JavadocDetailNodeParser parser = new JavadocDetailNodeParser(); final JavadocDetailNodeParser.ParseStatus status = parser.parseJavadocAsDetailNode(ast); final String actual = DetailNodeTreeStringPrinter.printTree(status.getTree(), "", ""); final String expected; // line separators in the input file while running this test on Windows are different, // so when we try to print tree, output also will have different line separators on windows // and linux. if (OS_NAME.startsWith("windows")) { expected = new String(Files.readAllBytes(Paths.get( getPath("ExpectedJavadocDetailNodeParserWindows.txt"))), StandardCharsets.UTF_8); } else { expected = new String(Files.readAllBytes(Paths.get( getPath("ExpectedJavadocDetailNodeParser.txt"))), StandardCharsets.UTF_8); } assertEquals("Invalid parse result", expected, actual); }
/** * Parse javadoc comment to DetailNode tree. * @param javadocComment javadoc comment content * @return tree */ private static DetailNode parseJavadocAsDetailNode(String javadocComment) { final DetailAST blockComment = CommonUtil.createBlockCommentNode(javadocComment); return parseJavadocAsDetailNode(blockComment); }
/** * Parse block comment DetailAST as Javadoc DetailNode tree. * @param blockComment DetailAST * @return DetailNode tree */ public static DetailNode parseJavadocAsDetailNode(DetailAST blockComment) { final JavadocDetailNodeParser parser = new JavadocDetailNodeParser(); final ParseStatus status = parser.parseJavadocAsDetailNode(blockComment); if (status.getParseErrorMessage() != null) { throw new IllegalArgumentException(getParseErrorMessage(status.getParseErrorMessage())); } return status.getTree(); }
/** * Parses block comment as javadoc and prints its tree. * @param node block comment begin * @return string javadoc tree */ private static String parseAndPrintJavadocTree(DetailAST node) { final DetailAST javadocBlock = node.getParent(); final DetailNode tree = DetailNodeTreeStringPrinter.parseJavadocAsDetailNode(javadocBlock); String baseIndentation = getIndentation(node); baseIndentation = baseIndentation.substring(0, baseIndentation.length() - 2); final String rootPrefix = baseIndentation + " `--"; final String prefix = baseIndentation + " "; return DetailNodeTreeStringPrinter.printTree(tree, rootPrefix, prefix); }
/** * 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()); }
/** * Parse block comment DetailAST as Javadoc DetailNode tree. * @param blockComment DetailAST * @return DetailNode tree */ public static DetailNode parseJavadocAsDetailNode(DetailAST blockComment) { final JavadocDetailNodeParser parser = new JavadocDetailNodeParser(); final ParseStatus status = parser.parseJavadocAsDetailNode(blockComment); if (status.getParseErrorMessage() != null) { throw new IllegalArgumentException(getParseErrorMessage(status.getParseErrorMessage())); } return status.getTree(); }
/** * Verifies the javadoc tree generated for the supplied javadoc file against the expected tree * in the supplied text file. * @param expectedTextPrintFilename name of the text file having the expected tree. * @param actualJavadocFilename name of the file containing the javadoc. * @throws Exception if exception occurs during verification. */ protected static void verifyJavadocTree(String expectedTextPrintFilename, String actualJavadocFilename) throws Exception { final String expectedContents = readFile(expectedTextPrintFilename); final String actualContents = DetailNodeTreeStringPrinter.printFileAst( new File(actualJavadocFilename)).replaceAll(CRLF_REGEX, LF_REGEX); assertEquals("Generated tree from the javadoc file should match the pre-defined tree", expectedContents, actualContents); }
/** * Print AST. * @param ast the root AST node. * @param rootPrefix prefix for the root node * @param prefix prefix for other nodes * @return string AST. */ public static String printTree(DetailNode ast, String rootPrefix, String prefix) { final StringBuilder messageBuilder = new StringBuilder(1024); DetailNode node = ast; while (node != null) { if (node.getType() == JavadocTokenTypes.JAVADOC) { messageBuilder.append(rootPrefix); } else { messageBuilder.append(prefix); } messageBuilder.append(getIndentation(node)) .append(JavadocUtil.getTokenName(node.getType())).append(" -> ") .append(JavadocUtil.escapeAllControlChars(node.getText())).append(" [") .append(node.getLineNumber()).append(':').append(node.getColumnNumber()) .append(']').append(LINE_SEPARATOR) .append(printTree(JavadocUtil.getFirstChild(node), rootPrefix, prefix)); node = JavadocUtil.getNextSibling(node); } return messageBuilder.toString(); }
/** * Extracts the first sentence as HTML formatted text from the comment of an DetailAST. * The end of the sentence is determined by the symbol "period", "exclamation mark" or * "question mark", followed by a space or the end of the text. Inline tags @code and @literal * are converted to HTML code. * @param ast to extract the first sentence * @return the first sentence of the inner {@code TokenTypes.BLOCK_COMMENT_BEGIN} node * or {@code null} if the first sentence is absent or malformed (does not end with period) * @throws CheckstyleException if a javadoc comment can not be parsed or an unsupported inline * tag found */ private static String getFirstJavadocSentence(DetailAST ast) throws CheckstyleException { String firstSentence = null; for (DetailAST child = ast.getFirstChild(); child != null && firstSentence == null; child = child.getNextSibling()) { // If there is an annotation, the javadoc comment will be a child of it. if (child.getType() == TokenTypes.ANNOTATION) { firstSentence = getFirstJavadocSentence(child); } // Otherwise, the javadoc comment will be right here. else if (child.getType() == TokenTypes.BLOCK_COMMENT_BEGIN && JavadocUtil.isJavadocComment(child)) { final DetailNode tree = DetailNodeTreeStringPrinter.parseJavadocAsDetailNode(child); firstSentence = getFirstJavadocSentence(tree); } } return firstSentence; }
@Test public void testParseFileWithError() throws Exception { try { DetailNodeTreeStringPrinter.printFileAst( new File(getPath("InputDetailNodeTreeStringPrinterJavadocWithError.javadoc"))); Assert.fail("Javadoc parser didn't fail on missing end tag"); } catch (IllegalArgumentException ex) { final String expected = (String) GET_PARSE_ERROR_MESSAGE.invoke(null, new ParseErrorMessage(0, MSG_JAVADOC_MISSED_HTML_CLOSE, 1, "qwe")); assertEquals("Generated and expected parse error messages don't match", expected, ex.getMessage()); } }
/** * Parse javadoc comment to DetailNode tree. * @param javadocComment javadoc comment content * @return tree */ private static DetailNode parseJavadocAsDetailNode(String javadocComment) { final DetailAST blockComment = CommonUtil.createBlockCommentNode(javadocComment); return parseJavadocAsDetailNode(blockComment); }
@Test public void testUnescapedJavaCodeWithGenericsInJavadoc() throws Exception { try { DetailNodeTreeStringPrinter.printFileAst(new File( getPath("InputDetailNodeTreeStringPrinter" + "UnescapedJavaCodeWithGenericsInJavadoc.javadoc"))); Assert.fail("Exception is expected"); } catch (IllegalArgumentException ex) { final String expected = (String) GET_PARSE_ERROR_MESSAGE.invoke(null, new ParseErrorMessage(35, MSG_JAVADOC_MISSED_HTML_CLOSE, 7, "parsing")); assertEquals("Generated and expected parse error messages don't match", expected, ex.getMessage()); } }
/** * 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()); }
@Test public void testOmittedStartTagForHtmlElement() throws Exception { try { DetailNodeTreeStringPrinter.printFileAst(new File(getPath( "InputDetailNodeTreeStringPrinterOmittedStartTagForHtmlElement.javadoc" ))); Assert.fail("Exception is expected"); } catch (IllegalArgumentException ex) { final String expected = (String) GET_PARSE_ERROR_MESSAGE.invoke(null, new ParseErrorMessage(0, MSG_JAVADOC_MISSED_HTML_CLOSE, 3, "a")); assertEquals("Generated and expected parse error messages don't match", expected, ex.getMessage()); } }
/** * Extracts the first sentence as HTML formatted text from the comment of an DetailAST. * The end of the sentence is determined by the symbol "period", "exclamation mark" or * "question mark", followed by a space or the end of the text. Inline tags @code and @literal * are converted to HTML code. * @param ast to extract the first sentence * @return the first sentence of the inner {@code TokenTypes.BLOCK_COMMENT_BEGIN} node * or {@code null} if the first sentence is absent or malformed (does not end with period) * @throws CheckstyleException if a javadoc comment can not be parsed or an unsupported inline * tag found */ private static String getFirstJavadocSentence(DetailAST ast) throws CheckstyleException { String firstSentence = null; for (DetailAST child = ast.getFirstChild(); child != null && firstSentence == null; child = child.getNextSibling()) { // If there is an annotation, the javadoc comment will be a child of it. if (child.getType() == TokenTypes.ANNOTATION) { firstSentence = getFirstJavadocSentence(child); } // Otherwise, the javadoc comment will be right here. else if (child.getType() == TokenTypes.BLOCK_COMMENT_BEGIN && JavadocUtil.isJavadocComment(child)) { final DetailNode tree = DetailNodeTreeStringPrinter.parseJavadocAsDetailNode(child); firstSentence = getFirstJavadocSentence(tree); } } return firstSentence; }