Example usage for org.jdom2 Parent indexOf

List of usage examples for org.jdom2 Parent indexOf

Introduction

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

Prototype

int indexOf(Content child);

Source Link

Document

Returns the index of the supplied child in the content list, or -1 if not a child of this parent.

Usage

From source file:AIR.Common.xml.XmlElement.java

License:Open Source License

public XmlElement getPreviousSibling() {
    if (_element.getParent() == null)
        return null;

    Parent parent = _element.getParent();
    int indexForThis = parent.indexOf(_element);
    if (indexForThis > 0)
        return new XmlElement(parent.getContent(--indexForThis));
    return null;/*from  w  ww  .j ava 2s .com*/
}

From source file:AIR.Common.xml.XmlElement.java

License:Open Source License

public XmlElement removeChild(Content node) {
    if (_element instanceof Parent) {
        Parent castedElement = (Parent) _element;
        // TODO: Minor point. Should we just use .removeContent(Content) instead?
        // That way we can avoid two look ups through the descendants list.
        int existingPosition = castedElement.indexOf(node);
        return new XmlElement(castedElement.removeContent(existingPosition));
    }/*from  w w w.ja  v a 2s . c o  m*/
    return null;
}

From source file:de.danielluedecke.zettelkasten.database.DesktopData.java

License:Open Source License

/**
 * This method retrieves a bullet element from the given path {@code tp} and copies it into
 * a global Element-variable, so this element is stored for later use. Furthermore, the element
 * retrieve from the treepath {@code tp} is removed from the XML-document
 *
 * @param timestamp the timestamp of the bullet that should be copied to the "clipboard" and deleted afterwards
 * (cut-operation)/* w w  w.j a va2 s  .  c  om*/
 */
public void cutBulletToClip(String timestamp) {
    // retrieve the entry that should be deleted
    Element bullet = findEntryElementFromTimestamp(getCurrentDesktopElement(), timestamp);
    if (bullet != null) {
        // get the entry's parent
        Parent p = bullet.getParent();
        // get the index from "bullet"
        int index = p.indexOf(bullet);
        // if we have a valid index, go on
        if (index != -1) {
            // remove the content and save it to the clipboard-element
            clipbullet = (Element) p.removeContent(index);
            // change modified state
            setModified(true);
        }
    }
}

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// w w w  .ja v  a  2 s.  co m
 * @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();
}

From source file:org.kdp.word.transformer.FootnodeTransformer.java

License:Apache License

@Override
public void transform(Context context) {

    Map<String, Footnode> footnodes = new LinkedHashMap<>();

    Element root = context.getSourceRoot();
    for (Element el : root.getChildren()) {
        findFootnodes(context, el, footnodes);
    }//w ww .j  a v  a2s.co m

    JDOMFactory factory = context.getJDOMFactory();
    for (Footnode fn : footnodes.values()) {

        // Footnode Ref
        Element fnref = fn.fnref;
        String text = getFootnodeText(fnref);
        Parent parent = fnref.getParent();
        int index = parent.indexOf(fnref);
        parent.removeContent(index);
        Element span = factory.element("span");
        span.setAttribute("class", "MsoFootnoteReference");
        Element a = fnref.clone();
        a.removeContent();
        a.setText(text);
        //a.setAttribute("type", "noteref", OPFTransformer.NS_OPF);
        span.addContent(a);
        parent.addContent(index, span);

        /* Footnode Text
        Element fntxt = fn.fntxt;
        Element p = findMsoFootnoteText(fntxt); 
        text = getFootnodeText(p);
        p.getAttributes().clear();
        p.removeContent();
        p.setText(text);
        String divid = fn.id.substring(1);
        Element div = JDOMUtils.findElement(root, "div", "id", divid);
        IllegalStateAssertion.assertSame(p.getParentElement(), div, "Unexpected parent: " + div);
        Parent divparent = div.getParent();
        Element aside = factory.element("aside");
        aside.setAttribute("type", "footnote", OPFTransformer.NS_OPF);
        aside.setAttribute("id", fn.id);
        index = divparent.indexOf(div);
        divparent.removeContent(div);
        aside.addContent(p.clone());
        divparent.addContent(index, aside);
        */
    }
}