Example usage for org.jdom2 Element addContent

List of usage examples for org.jdom2 Element addContent

Introduction

In this page you can find the example usage for org.jdom2 Element addContent.

Prototype

@Override
public Element addContent(final Collection<? extends Content> newContent) 

Source Link

Document

Appends all children in the given collection to the end of the content list.

Usage

From source file:de.danielluedecke.zettelkasten.tasks.export.ExportToXmlTask.java

License:Open Source License

/**
 * //from   w  w  w . j  a  v  a  2s .  co m
 * @param counter
 * @return 
 */
private Element exportEntries(int counter) {
    try {
        // retrieve zettelnumber
        int zettelnummer = Integer.parseInt(exportentries.get(counter).toString());
        // get the zettel-element
        Element zettel = dataObj.retrieveZettel(zettelnummer);
        // create new zettel-element for our final export file
        Element el_zettel = new Element(Daten.ELEMENT_ZETTEL);
        // see whether the bit "EXPORT_TITLE" is set
        // in the exportparts-variabe. if so, export title
        if ((exportparts & Constants.EXPORT_TITLE) != 0) {
            // create new title element
            Element el = new Element(Daten.ELEMENT_TITLE);
            // set the text from the data-file
            el.setText(zettel.getChild(Daten.ELEMENT_TITLE).getText());
            // and add it to our final document
            el_zettel.addContent(el);
        }
        // see whether the bit "EXPORT_CONTENT" is set
        // in the exportparts-variabe. if so, export content
        if ((exportparts & Constants.EXPORT_CONTENT) != 0) {
            // create new content element
            Element el = new Element(Daten.ELEMENT_CONTENT);
            // set the text from the data-file
            el.setText((removeformattags) ? dataObj.getCleanZettelContent(zettelnummer)
                    : zettel.getChild(Daten.ELEMENT_CONTENT).getText());
            // and add it to our final document
            el_zettel.addContent(el);
        }
        // see whether the bit "EXPORT_AUTHOR" is set
        // in the exportparts-variabe. if so, export author
        if ((exportparts & Constants.EXPORT_AUTHOR) != 0) {
            // create new content element
            Element el = new Element(Daten.ELEMENT_AUTHORS);
            // if the user wants all data in one file, get the
            // author string and replace the index-number with the string value
            if (allinone) {
                // first check, whether we have any keywords at all
                if (zettel.getChild(Daten.ELEMENT_AUTHOR).getText().isEmpty()) {
                    // if not, set empty string
                    el.setText("");
                } else {
                    // get the author string
                    String[] aus = zettel.getChild(Daten.ELEMENT_AUTHOR).getText().split(",");
                    // if we have any author, go on
                    if (aus != null && aus.length > 0) {
                        // iterate array
                        for (String a : aus) {
                            // create new child-element
                            Element au = new Element(Daten.ELEMENT_AUTHOR);
                            // set the text from the data-file
                            au.setText(dataObj.getAuthor(Integer.parseInt(a)));
                            // add child-element
                            el.addContent(au);
                        }
                    } else {
                        // else set empty string
                        el.setText("");
                    }
                }
            } else {
                // set the text from the data-file
                el.setText(zettel.getChild(Daten.ELEMENT_AUTHOR).getText());
            }
            // and add it to our final document
            el_zettel.addContent(el);
        }
        // see whether the bit "EXPORT_KEYWORDS" is set
        // in the exportparts-variabe. if so, export keywords
        if ((exportparts & Constants.EXPORT_KEYWORDS) != 0) {
            // create new content element
            Element el = new Element(Daten.ELEMENT_KEYWORD);
            // if the user wants all data in one file, get the
            // keyword string and replace the index-number with the string value
            if (allinone) {
                // first check, whether we have any keywords at all
                if (zettel.getChild(Daten.ELEMENT_KEYWORD).getText().isEmpty()) {
                    // if not, set empty string
                    el.setText("");
                } else {
                    // get the index numbers. we now have all keyword-index-numbers
                    // as a string array. these numbers reference to the keyword-string-values
                    // in the keyword-xml-file
                    String[] nrs = zettel.getChild(Daten.ELEMENT_KEYWORD).getText().split(",");
                    // if we have any author, go on
                    if (nrs != null && nrs.length > 0) {
                        // iterate the array
                        for (String n : nrs) {
                            // create new child element
                            Element kw = new Element("keyword");
                            // now get the keyword string from the keyword-xml-file
                            kw.setText(dataObj.getKeyword(Integer.parseInt(n)));
                            // and add this subchild
                            el.addContent(kw);
                        }
                    } else {
                        // else set empty string
                        el.setText("");
                    }
                }
            } else {
                // set the text from the data-file
                el.setText(zettel.getChild(Daten.ELEMENT_KEYWORD).getText());
            }
            // and add it to our final document
            el_zettel.addContent(el);
        }
        // see whether the bit "EXPORT_MANLINKS" is set
        // in the exportparts-variabe. if so, export manual links
        if ((exportparts & Constants.EXPORT_MANLINKS) != 0) {
            // create new manlinks element
            Element el = new Element(Daten.ELEMENT_MANLINKS);
            // set the text from the data-file
            el.setText(zettel.getChild(Daten.ELEMENT_MANLINKS).getText());
            // and add it to our final document
            el_zettel.addContent(el);
        }
        // see whether the bit "EXPORT_MANLINKS" is set
        // in the exportparts-variabe. if so, export manual links
        if ((exportparts & Constants.EXPORT_LUHMANN) != 0) {
            // create new manlinks element
            Element el = new Element(Daten.ELEMENT_TRAILS);
            // set the text from the data-file
            el.setText(zettel.getChild(Daten.ELEMENT_TRAILS).getText());
            // and add it to our final document
            el_zettel.addContent(el);
        }
        // see whether the bit "EXPORT_LINKS" is set
        // in the exportparts-variabe. if so, export links
        if ((exportparts & Constants.EXPORT_LINKS) != 0) {
            // create new link element
            Element el = new Element(Daten.ELEMENT_ATTACHMENTS);
            // add the content from the data-file. we cannot use settext here,
            // because we might have several sub-children
            // get the list of all sub-children
            List<Element> l = zettel.getChild(Daten.ELEMENT_ATTACHMENTS).getChildren();
            // create an iterator
            Iterator<Element> i = l.iterator();
            // go through loop and add all children
            while (i.hasNext()) {
                // create child-element for our parent-element
                Element el_link = new Element(Daten.ELEMENT_ATTCHILD);
                // get the child-element from the list
                Element el_dummy = i.next();
                // and set the text to our created child element
                el_link.setText(el_dummy.getText());
                // add the child-element to our parent
                el.addContent(el_link);
            }
            // and add it to our final document
            el_zettel.addContent(el);
        }
        // see whether the bit "EXPORT_REMARKS" is set
        // in the exportparts-variabe. if so, export remarks
        if ((exportparts & Constants.EXPORT_REMARKS) != 0) {
            // create new remarks element
            Element el = new Element(Daten.ELEMENT_REMARKS);
            // set the text from the data-file
            el.setText(zettel.getChild(Daten.ELEMENT_REMARKS).getText());
            // and add it to our final document
            el_zettel.addContent(el);
        }
        // see whether the bit "EXPORT_TIMESTAMP" is set
        // in the exportparts-variabe. if so, export timestamp
        if ((exportparts & Constants.EXPORT_TIMESTAMP) != 0) {
            // set timestamp for export element
            dataObj.setTimestamp(el_zettel, dataObj.getTimestampCreated(zettel),
                    dataObj.getTimestampEdited(zettel));
        }
        return el_zettel;
    } catch (NumberFormatException e) {
        // create new headline element
        Element headline = new Element("headline");
        // add headline-text to it.
        headline.setText(exportentries.get(counter).toString().substring(2));
        return headline;
    }
}