/** * Gets first child node of specified node. * * @param node DetailNode * @return first child */ public static DetailNode getFirstChild(DetailNode node) { DetailNode resultNode = null; if (node.getChildren().length > 0) { resultNode = node.getChildren()[0]; } return resultNode; }
/** * Returns the child of parent at index. * @param parent the node to get a child from. * @param index the index of a child. * @return the child of parent at index. */ public Object getChild(Object parent, int index) { final Object result; if (parent instanceof DetailNode) { result = ((DetailNode) parent).getChildren()[index]; } else { result = getChildAtDetailAst((DetailAST) parent, index); } return result; }
/** * Finds the last position of node without children. * @param detailNode DetailNode node. * @return Last position of node without children. */ private int findLastPosition(final DetailNode detailNode) { final int lastPosition; if (detailNode.getChildren().length == 0) { lastPosition = lines2position.get(detailNode.getLineNumber()) + detailNode.getColumnNumber() + detailNode.getText().length(); } else { final DetailNode lastChild = detailNode.getChildren()[detailNode.getChildren().length - 1]; lastPosition = findLastPosition(lastChild); } return lastPosition; }
/** * Finds and returns first sentence. * @param ast Javadoc root node. * @return first sentence. */ private static String getFirstSentence(DetailNode ast) { final StringBuilder result = new StringBuilder(256); final String periodSuffix = PERIOD + ' '; for (DetailNode child : ast.getChildren()) { final String text; if (child.getChildren().length == 0) { text = child.getText(); } else { text = getFirstSentence(child); } if (text.contains(periodSuffix)) { result.append(text, 0, text.indexOf(periodSuffix) + 1); break; } else { result.append(text); } } return result.toString(); }
/** * Checks if period is at the end of sentence. * @param ast Javadoc root node. * @return error string */ private static String getSummarySentence(DetailNode ast) { boolean flag = true; final StringBuilder result = new StringBuilder(256); for (DetailNode child : ast.getChildren()) { if (ALLOWED_TYPES.contains(child.getType())) { result.append(child.getText()); } else if (child.getType() == JavadocTokenTypes.HTML_ELEMENT && CommonUtil.isBlank(result.toString().trim())) { result.append(getStringInsideTag(result.toString(), child.getChildren()[0].getChildren()[0])); } else if (child.getType() == JavadocTokenTypes.JAVADOC_TAG) { flag = false; } if (!flag) { break; } } return result.toString().trim(); }
/** * Gets previous sibling of specified node. * @param node DetailNode * @return previous sibling */ public static DetailNode getPreviousSibling(DetailNode node) { DetailNode previousSibling = null; final int previousSiblingIndex = node.getIndex() - 1; if (previousSiblingIndex >= 0) { final DetailNode parent = node.getParent(); final DetailNode[] children = parent.getChildren(); previousSibling = children[previousSiblingIndex]; } return previousSibling; }
/** * Gets next sibling of specified node. * * @param node DetailNode * @return next sibling. */ public static DetailNode getNextSibling(DetailNode node) { DetailNode nextSibling = null; final DetailNode parent = node.getParent(); if (parent != null) { final int nextSiblingIndex = node.getIndex() + 1; final DetailNode[] children = parent.getChildren(); if (nextSiblingIndex <= children.length - 1) { nextSibling = children[nextSiblingIndex]; } } return nextSibling; }
/** * Concatenates the HTML text from AST of a JavadocTokenTypes.HTML_ELEMENT. * @param builder to append * @param node to format */ private static void formatHtmlElement(StringBuilder builder, DetailNode node) { switch (node.getType()) { case JavadocTokenTypes.START: case JavadocTokenTypes.HTML_TAG_NAME: case JavadocTokenTypes.END: case JavadocTokenTypes.TEXT: case JavadocTokenTypes.SLASH: builder.append(node.getText()); break; default: for (DetailNode child : node.getChildren()) { formatHtmlElement(builder, child); } break; } }
/** * Checks if the node starts with an {@inheritDoc}. * @param root The root node to examine. * @return {@code true} if the javadoc starts with an {@inheritDoc}. */ private static boolean startsWithInheritDoc(DetailNode root) { boolean found = false; final DetailNode[] children = root.getChildren(); for (int i = 0; !found; i++) { final DetailNode child = children[i]; if (child.getType() == JavadocTokenTypes.JAVADOC_INLINE_TAG && child.getChildren()[1].getType() == JavadocTokenTypes.INHERIT_DOC_LITERAL) { found = true; } else if (child.getType() != JavadocTokenTypes.LEADING_ASTERISK && !CommonUtil.isBlank(child.getText())) { break; } } return found; }
String firstSentence = null; final StringBuilder builder = new StringBuilder(128); for (DetailNode node : tree.getChildren()) { if (node.getType() == JavadocTokenTypes.TEXT) { final Matcher matcher = END_OF_SENTENCE_PATTERN.matcher(node.getText());
/** * Checks order of atclauses in tag section node. * @param javadoc Javadoc root node. */ private void checkOrderInTagSection(DetailNode javadoc) { int maxIndexOfPreviousTag = 0; for (DetailNode node : javadoc.getChildren()) { if (node.getType() == JavadocTokenTypes.JAVADOC_TAG) { final String tagText = JavadocUtil.getFirstChild(node).getText(); final int indexOfCurrentTag = tagOrder.indexOf(tagText); if (indexOfCurrentTag != -1) { if (indexOfCurrentTag < maxIndexOfPreviousTag) { log(node.getLineNumber(), MSG_KEY, tagOrder.toString()); } else { maxIndexOfPreviousTag = indexOfCurrentTag; } } } } }
throws CheckstyleException { boolean wrapWithCodeTag = false; for (DetailNode node : inlineTag.getChildren()) { switch (node.getType()) { case JavadocTokenTypes.CODE_LITERAL:
/** * Adjust first line nodes to javadoc indent. * @param tree DetailNode tree root * @param javadocColumnNumber javadoc indent */ private void adjustFirstLineToJavadocIndent(DetailNode tree, int javadocColumnNumber) { if (tree.getLineNumber() == blockCommentLineNumber) { ((JavadocNodeImpl) tree).setColumnNumber(tree.getColumnNumber() + javadocColumnNumber); final DetailNode[] children = tree.getChildren(); for (DetailNode child : children) { adjustFirstLineToJavadocIndent(child, javadocColumnNumber); } } }
/** * Returns the number of children of parent. * @param parent the node to count children for. * @return the number of children of the node parent. */ public int getChildCount(Object parent) { final int result; if (parent instanceof DetailNode) { result = ((DetailNode) parent).getChildren().length; } else { if (parseMode == ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS && ((AST) parent).getType() == TokenTypes.COMMENT_CONTENT && JavadocUtil.isJavadocComment(((DetailAST) parent).getParent())) { //getChildCount return 0 on COMMENT_CONTENT, //but we need to attach javadoc tree, that is separate tree result = 1; } else { result = ((DetailAST) parent).getChildCount(); } } return result; }
@Test public void testDetailNodeLeafSelection() { final DetailNode javadoc = (DetailNode) model.getParseTreeTableModel() .getChild(tree.getFirstChild().getNextSibling().getFirstChild(), 0); final DetailNode javadocLeaf = javadoc.getChildren()[2]; final CodeSelectorPresentation selector = new CodeSelectorPresentation(javadocLeaf, linesToPosition); selector.findSelectionPositions(); Assert.assertEquals("Invalid selection start", 76, selector.getSelectionStart()); Assert.assertEquals("Invalid selection end", 90, selector.getSelectionEnd()); }
/** * Gets first child node of specified node. * * @param node DetailNode * @return first child */ public static DetailNode getFirstChild(DetailNode node) { DetailNode resultNode = null; if (node.getChildren().length > 0) { resultNode = node.getChildren()[0]; } return resultNode; }
/** * Returns the child of parent at index. * @param parent the node to get a child from. * @param index the index of a child. * @return the child of parent at index. */ public Object getChild(Object parent, int index) { final Object result; if (parent instanceof DetailNode) { result = ((DetailNode) parent).getChildren()[index]; } else { result = getChildAtDetailAst((DetailAST) parent, index); } return result; }
/** * Finds the last position of node without children. * @param detailNode DetailNode node. * @return Last position of node without children. */ private int findLastPosition(final DetailNode detailNode) { final int lastPosition; if (detailNode.getChildren().length == 0) { lastPosition = lines2position.get(detailNode.getLineNumber()) + detailNode.getColumnNumber() + detailNode.getText().length(); } else { final DetailNode lastChild = detailNode.getChildren()[detailNode.getChildren().length - 1]; lastPosition = findLastPosition(lastChild); } return lastPosition; }
/** * Gets previous sibling of specified node. * @param node DetailNode * @return previous sibling */ public static DetailNode getPreviousSibling(DetailNode node) { DetailNode previousSibling = null; final int previousSiblingIndex = node.getIndex() - 1; if (previousSiblingIndex >= 0) { final DetailNode parent = node.getParent(); final DetailNode[] children = parent.getChildren(); previousSibling = children[previousSiblingIndex]; } return previousSibling; }
/** * Adjust first line nodes to javadoc indent. * @param tree DetailNode tree root * @param javadocColumnNumber javadoc indent */ private void adjustFirstLineToJavadocIndent(DetailNode tree, int javadocColumnNumber) { if (tree.getLineNumber() == blockCommentLineNumber) { ((JavadocNodeImpl) tree).setColumnNumber(tree.getColumnNumber() + javadocColumnNumber); final DetailNode[] children = tree.getChildren(); for (DetailNode child : children) { adjustFirstLineToJavadocIndent(child, javadocColumnNumber); } } }