Example usage for org.jdom2 Parent getContentSize

List of usage examples for org.jdom2 Parent getContentSize

Introduction

In this page you can find the example usage for org.jdom2 Parent getContentSize.

Prototype

int getContentSize();

Source Link

Document

Returns the number of children in this parent's content list.

Usage

From source file:mil.tatrc.physiology.datamodel.doxygen.XSDToDoxygen.java

License:Open Source License

/**
 * Obtains any XML comments sibling content that immediately preceed this
 * element/*ww w.  j ava2s .  com*/
 * @param element
 * @return
 */
private String getComment(Element element) {
    Parent parent = element.getParent();
    int i = parent.indexOf(element);

    StringBuffer comments = new StringBuffer();

    // Search backward from this position for preceding comments
    for (int x = i - 1; x >= 0; x--) {
        Content c = parent.getContent(x);
        if (c.getCType() == Content.CType.Comment) {
            Comment comment = (Comment) c;
            String text = comment.getText().trim();
            if (isValidComment(text)) {
                // This comment pertains to the preceding element
                if (text.startsWith("<<")) {
                    break;
                }
                if (comments.length() > 0) {
                    comments.insert(0, '\n');
                }
                comments.insert(0, text);
            }
        } else if (c.getCType() == Content.CType.Element) {
            // Stop looking when we hit another element 
            break;
        }
    }

    // Search forward from this position for inline commentx
    for (int x = i + 1; x < parent.getContentSize(); x++) {
        Content c = parent.getContent(x);
        if (c.getCType() == Content.CType.Comment) {
            Comment comment = (Comment) c;
            String text = comment.getText().trim();
            if (isValidComment(text)) {
                // This comment pertains to the preceding element
                if (text.startsWith("<<")) {
                    // Strip the "<<"
                    text = text.substring(2);
                    if (comments.length() > 0) {
                        comments.insert(0, '\n');
                    }
                    comments.insert(0, text);
                } else {
                    break;
                }
            }
        } else if (c.getCType() == Content.CType.Element) {
            // Stop looking when we hit another element 
            break;
        }
    }
    return comments.toString();
}