Example usage for org.jdom2 Element getText

List of usage examples for org.jdom2 Element getText

Introduction

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

Prototype

public String getText() 

Source Link

Document

Returns the textual content directly held under this element as a string.

Usage

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

License:Open Source License

/**
 * //from  w ww  .j  ava  2s. com
 * @param dataObj
 * @param newbookmarks 
 */
public void appendBookmarks(Daten dataObj, Document newbookmarks) {
    // create a list of all elements from the given xml file
    try {
        List<?> elementList = newbookmarks.getRootElement().getContent();
        try {
            // iterate all importet bookmakrs
            for (int cnt = 0; cnt < newbookmarks.getContentSize(); cnt++) {
                // retrieve each single bookmark-element
                Element b = (Element) elementList.get(cnt);
                // get bookmark-id (i.e. unique entry-ID)
                String id = b.getAttributeValue("id");
                // check for valid value
                if (id != null && !id.isEmpty()) {
                    // find entry number from ID
                    int index = dataObj.findZettelFromID(id);
                    // check for valid return parameter
                    if (index != -1) {
                        // we now have the entry's number. now retrieve
                        // bookmark-category
                        String cat = b.getAttributeValue("cat");
                        // check for valid value
                        if (cat != null && !cat.isEmpty()) {
                            // retrieve possible comment
                            String comment = b.getText();
                            // and add new importet bookmark
                            addBookmark(index, cat, comment);
                        }
                    }
                }
            }
        } catch (IndexOutOfBoundsException e) {
        }
    } catch (IllegalStateException e) {
        Constants.zknlogger.log(Level.WARNING, e.getLocalizedMessage());
    }
}

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

License:Open Source License

/**
 * This method returns the position of a bookmark-category in the bookmarks XML file.
 * if the category doesn't exist, the return value is -1
 * //from w  w w .j  a v a2  s . co  m
 * @param cat the string-value of the category
 * @return the position of the category within the xml-file, or -1 if no category was found
 */
public int getCategoryPosition(String cat) {
    // create a list of all category elements from the bookmark xml file
    try {
        // we are interested in the children of the child-element "category"
        List<?> catList = bookmarks.getRootElement().getChild("category").getContent();
        // an iterator for the loop below
        Iterator<?> iterator = catList.iterator();
        // counter for the return value if a found category string matches the parameter
        int cnt = 0;
        // start loop
        while (iterator.hasNext()) {
            // retrieve each element
            Element catname = (Element) iterator.next();
            // if category string matches the parameter integer value, return the position
            if (catname.getText().equals(cat)) {
                return cnt;
            }
            // else increase counter
            cnt++;
        }
        // if no category was found, return -1
        return -1;
    } catch (IllegalStateException e) {
        return -1;
    }
}

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

License:Open Source License

/**
 * Retrieves the user defined path to attachments
 * @return the user defined path to attachments
 *//*from   w ww .  java 2s .  co  m*/
public File getUserAttachmentPath() {
    // retrieve version-element
    Element el = metainfFile.getRootElement().getChild(ELEMENT_ATTACHMENT_PATH);
    // check whether it's null or not
    if (null == el || el.getText().trim().isEmpty()) {
        return null;
    }
    // else return the attribute value
    return new File(el.getText());
}

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

License:Open Source License

/**
 * Retrieves the user defined path to images
 * @return the user defined path to images
 *///ww w . j  a va2 s . c o m
public File getUserImagePath() {
    // retrieve version-element
    Element el = metainfFile.getRootElement().getChild(ELEMENT_IMAGE_PATH);
    // check whether it's null or not
    if (null == el || el.getText().trim().isEmpty()) {
        return null;
    }
    // else return the attribute value
    return new File(el.getText());
}

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

License:Open Source License

/**
 * This method returns the description of the zettelkasten-data, which is stored
 * in the metainformation-file of the zipped data-file. Usually needed when showing
 * information on the opened datafile//from w w  w .ja va 2 s .  c om
 * 
 * @return a string with the description of this zettelkasten
 */
public String getZknDescription() {
    // get the child element
    Element el = metainfFile.getRootElement().getChild(ELEMEMT_DESCRIPTION);
    // check whether it's null
    if (null == el) {
        return "";
    }
    // else return element-text
    return el.getText();
}

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

License:Open Source License

/**
 * This method duplicates an entry and inserts it at the end or the next empty place in the
 * data file/*from   www.ja v a 2s  .c  o m*/
 * 
 * @param nr the number of the entry that should be duplicated
 * @return 
 */
public boolean duplicateEntry(int nr) {
    // first of all, we duplicate all authors and keywords frequencies from the existing entry.
    // therefore, we first retrieve all author-index-numbers from that entry
    changeFrequencies(nr, 1);
    // retrieve entry that should be duplicated
    Element oldzettel = retrieveElement(zknFile, nr);
    // create new zettel
    Element zettel = new Element(ELEMENT_ZETTEL);
    // check whether we have any empty elements in between where we can insert the new entry
    int emptypos = retrieveFirstEmptyEntry();
    // if we have any empty elements...
    if (emptypos != -1 && settings.getInsertNewEntryAtEmpty()) {
        // retrieve empty element
        zettel = retrieveElement(zknFile, emptypos);
        // and remove former content, so we can add new content
        zettel.removeContent();
    }
    try {
        setZettelID(zettel);
        //
        // add title
        //
        // create child element with title information
        Element t = new Element(ELEMENT_TITLE);
        // and add it to the zettel-element
        zettel.addContent(t);
        // set value of the child element
        t.setText(oldzettel.getChild(ELEMENT_TITLE).getText());
        //
        // add content
        //
        // create child element with content information
        Element c = new Element(ELEMENT_CONTENT);
        // and add it to the zettel-element
        zettel.addContent(c);
        // set value of the content element
        c.setText(oldzettel.getChild(ELEMENT_CONTENT).getText());
        //
        // add author
        //
        // create child element with author information
        Element a = new Element(ELEMENT_AUTHOR);
        // and add it to the zettel-element
        zettel.addContent(a);
        // set value of author element
        a.setText(oldzettel.getChild(ELEMENT_AUTHOR).getText());
        //
        // add keywords
        //
        // create child element with keyword information
        Element k = new Element(ELEMENT_KEYWORD);
        // and add it to the zettel-element
        zettel.addContent(k);
        // store keyword index numbers
        k.setText(oldzettel.getChild(ELEMENT_KEYWORD).getText());
        //
        // now comes the manual links to other entries
        //
        Element m = new Element(ELEMENT_MANLINKS);
        zettel.addContent(m);
        m.setText("");
        //
        // add hyperlinks
        //
        // create child element with link information
        Element h = new Element(ELEMENT_ATTACHMENTS);
        // and add it to the zettel-element
        zettel.addContent(h);
        // add each hyperlink. therefor, iterate the array
        List<Element> links = oldzettel.getChild(ELEMENT_ATTACHMENTS).getChildren();
        Iterator<Element> i = links.iterator();
        while (i.hasNext()) {
            // create a new subchuld-element
            Element sublink = new Element(ELEMENT_ATTCHILD);
            Element le = i.next();
            // and add the link-string from the array
            sublink.setText(le.getText());
            h.addContent(sublink);
        }
        //
        // add remarks
        //
        // create child element with content information
        Element r = new Element(ELEMENT_REMARKS);
        // and add it to the zettel-element
        zettel.addContent(r);
        // set value of the content element
        r.setText(oldzettel.getChild(ELEMENT_REMARKS).getText());
        //
        // add timestamp
        //
        // set creation timestamp, but set no text for edit timestamp
        // since the entry is not edited
        setTimestamp(zettel, Tools.getTimeStamp(), "");
        //
        // now comes the luhmann number
        //
        Element l = new Element(ELEMENT_TRAILS);
        zettel.addContent(l);
        l.setText(oldzettel.getChild(ELEMENT_TRAILS).getText());
        //
        // complete datafile
        //
        // if we have any empty elements, go on here
        if (emptypos != -1 && settings.getInsertNewEntryAtEmpty()) {
            // return the empty-position, which is now filled with the new author-value
            zettelPos = emptypos;
        } else {
            // finally, add the whole element to the data file
            zknFile.getRootElement().addContent(zettel);
            // set the zettel-position to the new entry
            zettelPos = getCount(ZKNCOUNT);
        }
        // duplicate this entry into the correct entry order
        // by changing the prev/nex references (or pointers) of the entries.
        changeZettelPointer(zettelPos, nr);
        // titles have to be updated.
        setTitlelistUpToDate(false);
        // set modified state
        setModified(true);
    } catch (IllegalAddException ex) {
        Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
        return false;
    } catch (IllegalDataException ex) {
        Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
        return false;
    }
    return true;
}

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

License:Open Source License

/**
 * This method returns the position of a keyword in the keyword XML file {@link #keywordFile}.
 * if the keyword doesn't exist, the return value is {@code -1}.
 * /*from  w w  w  .j  a v  a 2s.  c o  m*/
 * @param kw keyword which is searched for in the keyword list
 * @param matchcase whether the keyword-search is case-sensitive ({@code true}) or not ({@code false})
 * @return the position of the author string or -1 if no match was found
 */
public int getKeywordPosition(String kw, boolean matchcase) {
    // check for valid value
    if (null == kw || kw.trim().isEmpty())
        return -1;
    // create a list of all keyword elements from the keyword xml file
    try {
        List<?> keywordList = keywordFile.getRootElement().getContent();
        // and an iterator for the loop below
        Iterator<?> iterator = keywordList.iterator();
        // counter for the return value if a found author matches the parameter
        int cnt = 1;
        // iterate loop
        while (iterator.hasNext()) {
            // retrieve each single element
            Element keyword = (Element) iterator.next();
            // if keyword matches the parameter string, return the position
            if (matchcase && kw.equals(keyword.getText())) {
                return cnt;
            } else if (!matchcase && kw.equalsIgnoreCase(keyword.getText())) {
                return cnt;
            }
            // else increase counter
            cnt++;
        }
        // if no keyword was found, return -1
        return -1;
    } catch (IllegalStateException e) {
        Constants.zknlogger.log(Level.WARNING, e.getLocalizedMessage());
        return -1;
    }
}

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

License:Open Source License

/**
 * This methods returns the keyword of a given position in the <b>keyword-datafile</b>.<br><br>
 * <b>Caution!</b> The position {@code pos} is a value from <b>1</b> to
 * {@link #getCount(int) getCount(KWCOUNT)} - in contrary
 * to usual array handling where the range is from 0 to (size-1).
 * /*from  w  ww. j a v  a 2 s  .c  om*/
 * @param pos a valid position of an element, ranged from 1 to {@link #getCount(int) getCount(KWCOUNT)}
 * @return the keyword string, or an empty string, if no such keyword exists
 */
public String getKeyword(int pos) {
    // retrieve the keyword element
    Element keyword = retrieveElement(keywordFile, pos);
    // return the matching string value of the keyword element
    String retval;
    // check whether element is null
    if (null == keyword) {
        retval = "";
    } else {
        retval = keyword.getText();
    }

    return retval;
}

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

License:Open Source License

/**
 * This method searches the keyword-xml-file or author-xml-file for empty elements and returns the
 * number of the first empty element, if any. empty elements occur, when the user
 * deletes a keyword or author. in this case, to keep the permanent index-number of the other
 * keywords and authors, the keyword-/author-element is not completely removed, but only the text is
 * removed.//from   ww w. ja v a 2 s .  c o m
 * 
 * @param the xml-document (either <i>keywordFile</i> or <i>authorFile</i>)
 * @return the number of the first empty element, or -1 if no empty element was found
 */
private int retrieveFirstEmptyElement(Document doc) {
    // create a list of all elements from the given xml file
    try {
        List<?> elementList = doc.getRootElement().getContent();
        // and an iterator for the loop below
        Iterator<?> iterator = elementList.iterator();
        // counter for the return value if a found author matches the parameter
        int cnt = 1;
        // iterare all elements
        while (iterator.hasNext()) {
            Element el = (Element) iterator.next();
            // if author matches the parameter string, return the position
            if (el.getText().isEmpty()) {
                return cnt;
            }
            // else increase counter
            cnt++;
        }
        // if no author was found, return -1
        return -1;
    } catch (IllegalStateException e) {
        Constants.zknlogger.log(Level.WARNING, e.getLocalizedMessage());
        return -1;
    }
}

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

License:Open Source License

/**
 * This method returns the position of an author in the author XML file {@link #authorFile}.
 * if the author doesn't exist, the return value is {@code -1}.
 * /*from w  ww  .j av  a 2 s  .co m*/
 * @param auth author which is searched for in the author list
 * @return the position of the author string or -1 if no match was found
 */
public int getAuthorPosition(String auth) {
    // check for valid value
    if (null == auth || auth.trim().isEmpty()) {
        return -1;
    }
    // create a list of all author elements from the author xml file
    try {
        List<?> authorList = authorFile.getRootElement().getContent();
        // and an iterator for the loop below
        Iterator<?> iterator = authorList.iterator();
        // counter for the return value if a found author matches the parameter
        int cnt = 1;
        // iterate all author values
        while (iterator.hasNext()) {
            Element author = (Element) iterator.next();
            // if author matches the parameter string, return the position
            if (auth.equalsIgnoreCase(author.getText()))
                return cnt;
            // else increase counter
            cnt++;
        }
        // if no author was found, return -1
        return -1;
    } catch (IllegalStateException e) {
        return -1;
    }
}