Example usage for org.jdom2 Parent removeContent

List of usage examples for org.jdom2 Parent removeContent

Introduction

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

Prototype

Content removeContent(int index);

Source Link

Document

Removes and returns the child at the given index, or returns null if there's no such child.

Usage

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 www .ja  v  a  2 s.co  m
    return null;
}

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

License:Open Source License

/**
 * This method removes all entries which entry-number are passed in the int-array {@code entries}
 * from all available desktop-elements./*from  w  w  w .  j  ava 2s . c o m*/
 * 
 * @param entries an integer-array containing the entry-numbers of those entries that should be
 * removed from the desktop
 * @return 
 */
public boolean deleteEntries(int[] entries) {
    // indicator for deletes entries
    boolean haveDeletedEntries = false;
    // check for valid parameter
    if (entries != null && entries.length > 0 && getCount() > 0) {
        // create linked list which will contain all to be deleted entries...
        LinkedList<Element> finallist = new LinkedList<Element>();
        // go through all desktops (outer loop)...
        for (int cnt = 0; cnt < getCount(); cnt++) {
            // ...and search for all entries in each desktop (inner loop)
            for (int e : entries) {
                // retrieve all found entries within the desktop
                LinkedList<Element> lle = searchForEntry(cnt, e);
                // check whether we have any returned entries...
                if (lle != null && lle.size() > 0) {
                    // create a new iterator for the found results
                    Iterator<Element> prepare = lle.iterator();
                    // iterate them
                    while (prepare.hasNext()) {
                        // get each single entry as element
                        Element entry = prepare.next();
                        // and add it to the final list
                        if (entry != null)
                            finallist.add(entry);
                    }
                }
            }
        }
        // if we found any elements that should be deleted, do
        // this now...
        if (finallist.size() > 0) {
            // create iterator
            Iterator<Element> i = finallist.iterator();
            // go trhough linked list
            while (i.hasNext()) {
                // get each element that should be deleted
                Element entry = i.next();
                // if we have a valid element, go on...
                if (entry != null) {
                    // retrieve timestamp
                    String timestamp = entry.getAttributeValue(ATTR_TIMESTAMP);
                    // check whether we have any modified entry. if so, delete it, since
                    // we no longer need it...
                    deleteModifiedEntry(timestamp);
                    // get the entry's parent
                    Parent p = entry.getParent();
                    // remove entry from parent
                    p.removeContent(entry);
                    // set deleted-indicator to true
                    haveDeletedEntries = true;
                }
            }
        }
    }
    // change modified state
    if (haveDeletedEntries)
        setModified(true);
    // return result
    return haveDeletedEntries;
}

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)/*from w w w  . j  av  a2 s  . co  m*/
 */
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:de.danielluedecke.zettelkasten.database.DesktopData.java

License:Open Source License

/**
 * This method deletes the selected entry from the desktop.
 * /*from w  w w .  j a  v a  2s .c om*/
 * @param timestamp the timestamp of the to be deleted entry
 */
public void deleteEntry(String timestamp) {
    // retrieve the entry that should be deleted
    Element entry = findEntryElementFromTimestamp(getCurrentDesktopElement(), timestamp);
    if (entry != null) {
        // check whether we have any modified entry. if so, delete it, since
        // we no longer need it...
        deleteModifiedEntry(timestamp);
        // get the entry's parent
        Parent p = entry.getParent();
        // remove entry from parent
        p.removeContent(entry);
        // change modified state
        setModified(true);
    }
}

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

License:Open Source License

/**
 * This method deletes the selected entry from the desktop.
 * /*www .  j ava2  s.  c om*/
 * @param timestamp the timestamp of the to the selected entry.
 */
public void deleteBullet(String timestamp) {
    // retrieve the entry that should be deleted
    Element bullet = findEntryElementFromTimestamp(getCurrentDesktopElement(), timestamp);
    if (bullet != null) {
        // retrieve all timestamps of those entries which are children of the to be
        // deleted bullet
        String[] timestamps = retrieveBulletTimestamps(bullet);
        // get the entry's parent
        Parent p = bullet.getParent();
        // remove entry from parent
        p.removeContent(bullet);
        // now remove all possible modified entries of that list
        if (timestamps != null && timestamps.length > 0) {
            // iterate all timestamps
            for (String singlets : timestamps) {
                // delete modified entries that already have been deleted due
                // to the removal of the bullet-point
                deleteModifiedEntry(singlets);
            }
        }
        // change modified state
        setModified(true);
    }
}

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);
    }// ww w.  jav  a  2  s.c  o 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);
        */
    }
}

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

License:Apache License

@Override
public void transform(Context context) {
    JDOMFactory factory = context.getJDOMFactory();

    Sections sections = new Sections();
    context.putAttribute(Sections.class, sections);

    Element root = context.getSourceRoot();
    for (Element el : root.getChildren()) {
        findWordSections(context, sections, el);
    }/*from   w  w w  .  j  a  va2  s  . c o m*/

    boolean navfound = false;
    Iterator<Section> itsec = sections.iterator();
    while (itsec.hasNext()) {
        Section section = itsec.next();

        if (navfound) {
            itsec.remove();
            continue;
        }
        navfound = section.isnav;

        // Remove the section from the original document
        Element element = section.element;
        Parent parent = element.getParent();
        parent.removeContent(element);

        // Build the target document 
        Element rootClone = root.clone();
        Element bodyClone = JDOMUtils.findElement(rootClone, "body");
        bodyClone.removeContent();
        bodyClone.addContent(element.clone());

        // Write the section document
        Document doc = factory.document(rootClone);
        File outfile = section.target.toFile();
        try {
            outfile.getParentFile().mkdirs();
            FileOutputStream fos = new FileOutputStream(outfile);
            IOUtils.writeDocument(context, doc, fos);
            fos.close();
        } catch (IOException ex) {
            throw new IllegalStateException(ex);
        }
    }
}

From source file:org.xflatdb.xflat.query.XPathUpdate.java

License:Apache License

/**
 * Applies the update operations to the given DOM Element representing
 * the data in a selected row./*  w w w.  ja  v a  2 s. c  o m*/
 * @param rowData The DOM Element representing the data in a selected row.
 * @return true if any updates were applied.
 */
public int apply(Element rowData) {
    int updateCount = 0;

    for (Update update : this.updates) {
        //the update's value will be one or the other, don't know which
        Content asContent = null;
        String asString = null;

        if (update.value instanceof String) {
            asString = (String) update.value;
        }

        if (update.value instanceof Content) {
            asContent = (Content) update.value;
        }

        for (Object node : update.path.evaluate(rowData)) {
            if (node == null)
                continue;

            Parent parent;
            Element parentElement;

            if (update.getUpdateType() == UpdateType.UNSET) {
                if (node instanceof Attribute) {
                    parentElement = ((Attribute) node).getParent();
                    if (parentElement != null) {
                        parentElement.removeAttribute((Attribute) node);
                        updateCount++;
                    }
                } else if (node instanceof Content) {
                    parent = ((Content) node).getParent();
                    //remove this node from its parent element
                    if (parent != null) {
                        parent.removeContent((Content) node);
                        updateCount++;
                    }
                }

                continue;
            }

            //it's a set

            if (node instanceof Attribute) {
                //for attributes we set the value to empty string
                //this way it can still be selected by xpath for future updates
                if (update.value == null) {
                    ((Attribute) node).setValue("");
                    updateCount++;
                } else {
                    if (asString == null) {
                        asString = getStringValue(update.value);
                    }

                    //if we fail conversion then do nothing.
                    if (asString != null) {
                        ((Attribute) node).setValue(asString);
                        updateCount++;
                    }
                }

                continue;
            } else if (!(node instanceof Content)) {
                //can't do anything
                continue;
            }

            Content contentNode = (Content) node;

            //need to convert
            if (update.value != null && asContent == null) {
                asContent = getContentValue(update.value);
                if (asContent == null) {
                    //failed conversion, try text
                    asString = getStringValue(update.value);
                    if (asString != null) {
                        //success!
                        asContent = new Text(asString);
                    }
                }
            }

            if (node instanceof Element) {
                //for elements we also set the value, but the value could be Content
                if (update.value == null) {
                    ((Element) node).removeContent();
                    updateCount++;
                } else if (asContent != null) {
                    if (asContent.getParent() != null) {
                        //we used the content before, need to clone it
                        asContent = asContent.clone();
                    }

                    ((Element) node).setContent(asContent);
                    updateCount++;
                }
                continue;
            }

            //at this point the node is Text, CDATA or something else.
            //The strategy now is to replace the value in its parent.
            parentElement = contentNode.getParentElement();
            if (parentElement == null) {
                //can't do anything
                continue;
            }

            if (update.value == null || asContent != null) {
                //replace this content in the parent element
                int index = parentElement.indexOf(contentNode);
                parentElement.removeContent(index);
                if (update.value != null) {
                    //if it was null then act like an unset, otherwise
                    //its a replace

                    if (asContent.getParent() != null) {
                        //we used the content before, need to clone it
                        asContent = asContent.clone();
                    }

                    parentElement.addContent(index, asContent);
                }
                updateCount++;
            }
        }
    }

    return updateCount;
}