Java Code Examples for org.jdom.Element#getContentSize()

The following examples show how to use org.jdom.Element#getContentSize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UpgradeXGAPP.java    From gate-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * If this element has any child <em>elements</em>, add whitespace
 * text nodes inside this element to indent its children to the
 * appropriate level, and then do the same recursively for the children.
 * This doesn't work for elements with mixed content - content must be
 * either other elements or text, not both.
 * @param e the element to pretty print
 * @param numAncestors the number of ancestor elements this element
 *                     will have once it is inserted into the tree
 *                     (i.e. the "depth" of this element).
 */
@SuppressWarnings("unchecked")
private static void prettyPrint(Element e, int numAncestors) {
  if(!e.getChildren().isEmpty()) {
    int numChildren = e.getContentSize();
    // add indent before the closing tag
    e.addContent(numChildren, new Text("\n" + StringUtils.repeat(" ", 2*numAncestors)));
    // add a bit more indent before each child - we have to work backwards
    // as the act of adding text nodes changes the content indexes for
    // subsequent children
    String indentStr = "\n" + StringUtils.repeat(" ", 2*(numAncestors+1));
    for(int i = numChildren - 1; i >= 0; i--) {
      e.addContent(i, new Text(indentStr));
    }

    // now recursively prettify the children
    for(Element child : (List<Element>)e.getChildren()) {
      prettyPrint(child, numAncestors + 1);
    }
  }
}