private static Document extractBetween( SectionsMarkdownVisitor v, Node parent, Header header1, Header header2) { /*Node parent1 = header1 != null ? header1.jjtGetParent() : null; Node parent2 = header2 != null ? header2.jjtGetParent() : null; /*if (parent1 != null && parent2 != null && parent1 != parent2) { throw new RuntimeException(); }* / Node parent = parent1 != null ? parent1 : parent2;*/ Document doc = new Document(Parser.JJTDOCUMENT); final int numChildren = parent.jjtGetNumChildren(); final int start = header1 != null ? Markdown.getIndexInParent(header1) + 1 : 0; final int end = header2 != null ? Markdown.getIndexInParent(header2) - 1 : numChildren - 1; for (int nodeIndex = start; nodeIndex <= end; nodeIndex++) { Node child = parent.jjtGetChild(nodeIndex); child.jjtSetParent(doc); doc.jjtAddChild(child, nodeIndex - start); } // Add a copy of all the resource definitions // to each document for (ResourceDefinition rd : v.resourceDefinitions) { ResourceDefinition copy = copy(rd); copy.jjtSetParent(doc); doc.jjtAddChild(copy, doc.jjtGetNumChildren()); } return doc; }
static int getIndexInParent(Node node) { Node parent = node.jjtGetParent(); for (int ii = 0; ii < parent.jjtGetNumChildren(); ii++) { if (parent.jjtGetChild(ii) == node) { return ii; } } throw new IllegalStateException(); }
static Node getNext(Node node) { Node parent = node.jjtGetParent(); int ii = getIndexInParent(node); return ii < parent.jjtGetNumChildren() - 1 ? parent.jjtGetChild(ii + 1) : null; }
static Node getPrev(Node node) { Node parent = node.jjtGetParent(); int ii = getIndexInParent(node); return ii > 0 ? parent.jjtGetChild(ii - 1) : null; }
static Node getLastSibling(Node node) { Node parent = node.jjtGetParent(); return parent.jjtGetChild(parent.jjtGetNumChildren() - 1); }
static Node getFirstSibling(Node node) { return node.jjtGetParent().jjtGetChild(0); }