private void printOrphanCommentsEnding(final Node node){ List<Node> everything = new LinkedList<Node>(); everything.addAll(node.getChildrenNodes()); sortByBeginPosition(everything); if (everything.size()==0) return; int commentsAtEnd = 0; boolean findingComments = true; while (findingComments){ Node last = everything.get(everything.size()-1-commentsAtEnd); findingComments = (last instanceof Comment); if (findingComments) commentsAtEnd++; } for (int i=0;i<commentsAtEnd;i++){ everything.get(everything.size()-commentsAtEnd+i).accept(this,null); } }
public void visitDepthFirst(Node node){ process(node); for (Node child : node.getChildrenNodes()){ visitDepthFirst(child); } }
private void printOrphanCommentsBeforeThisChildNode(final Node node){ if (node instanceof Comment) return; Node parent = node.getParentNode(); if (parent==null) return; List<Node> everything = new LinkedList<Node>(); everything.addAll(parent.getChildrenNodes()); sortByBeginPosition(everything); int positionOfTheChild = -1; for (int i=0;i<everything.size();i++){ if (everything.get(i)==node) positionOfTheChild=i; } if (positionOfTheChild==-1) throw new RuntimeException("My index not found!!! "+node); int positionOfPreviousChild = -1; for (int i=positionOfTheChild-1;i>=0 && positionOfPreviousChild==-1;i--){ if (!(everything.get(i) instanceof Comment)) positionOfPreviousChild = i; } for (int i=positionOfPreviousChild+1;i<positionOfTheChild;i++){ Node nodeToPrint = everything.get(i); if (!(nodeToPrint instanceof Comment)) throw new RuntimeException("Expected comment, instead "+nodeToPrint.getClass()+". Position of previous child: "+positionOfPreviousChild+", position of child "+positionOfTheChild); nodeToPrint.accept(this,null); } }
/** * This is the list of Comment which are contained in the Node either because * they are properly associated to one of its children or because they are floating * around inside the Node. * @return */ public List<Comment> getAllContainedComments(){ List<Comment> comments = new LinkedList<Comment>(); comments.addAll(getOrphanComments()); for (Node child : getChildrenNodes()){ if (child.getComment()!=null){ comments.add(child.getComment()); } comments.addAll(child.getAllContainedComments()); } return comments; }
private static void placeOrphanComment(Node node,Comment comment){ for (Node child : node.getChildrenNodes()){ if (child.contains(comment)){ placeOrphanComment(child,comment); return; } } node.addOrphanComment(comment); }
List<Node> children = node.getChildrenNodes(); sortByBeginPosition(children);