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.DesktopData.java

License:Open Source License

/**
 * Returns one of the three notes that can be saved with the desktop.
 * @param nr the number of the notes, either 1, 2 or 3
 * @return the content of the notes-textfield, or an empty if an error occured
 */// w  ww .  j  av  a 2 s  .  c  o m
public String getDesktopNotes(int nr) {
    // check for valid parameter
    if (nr >= 1 && nr <= 3) {
        // get all children from deskopNotes, since we need to find the right
        // desktop-element first...
        List<Element> elementList = desktopNotes.getRootElement().getChildren();
        // create an iterartor
        Iterator<Element> it = elementList.iterator();
        // go through all desktop-elements of the desktopNores-file
        while (it.hasNext()) {
            // retrieve element
            Element desk = it.next();
            // get name sttribute
            String att = desk.getAttributeValue("name");
            // check for desktop-name
            if (att != null && att.equals(getCurrentDesktopName())) {
                // retrieve notes-element
                Element note = desk.getChild("notes" + String.valueOf(nr));
                // return note
                return (note != null) ? note.getText() : "";
            }
        }
    }
    return "";
}

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

License:Open Source License

/**
 * This method retrieves the comment of an entry-element which <i>timestamp</i>-attribute
 * matches the parameter {@code t}.//from w  w  w.j a  v a  2s.  c  o  m
 *
 * @param e the initial element where the search starts. usually, use
 * {@link #getCurrentDesktopElement() getCurrentDesktopElement()} fo this.
 * @param t the timestamp which should match the timestamp-attribute of the entry-element
 * @param c the comment as string. when initially calling this method, pass an empty string
 * as parameter
 * @return the comment as string, or an emtpy string if no comment was found
 */
private String findEntryComment(Element e, String t, String c) {
    // check for valid string
    if (null == c)
        return "";
    // check for valid comment. if we already found a comment,
    // return it
    if (!c.isEmpty())
        return c;
    // get a list with all children of the element
    List<Element> children = e.getChildren();
    // create an iterator
    Iterator<Element> it = children.iterator();
    // go through all children
    while (it.hasNext()) {
        // get the child
        e = it.next();
        // check whether element has a timestamp value at all, and if it matches the parameter "t".
        if (e.getAttributeValue(ATTR_TIMESTAMP) != null && e.getAttributeValue(ATTR_TIMESTAMP).equals(t)) {
            // check whether we have a bullet-point
            if (e.getName().equals(ELEMENT_BULLET)) {
                // if we have a bullet, return the text of it's comment-child.
                Element comel = e.getChild(ATTR_COMMENT);
                return (comel != null) ? comel.getText() : "";
            } else {
                // else return the element's text
                return e.getText();
            }
        }
        // when the new element also has children, call this method again,
        // so we go through the strucuture recursively...
        if (hasChildren(e)) {
            c = findEntryComment(e, t, c);
        }
    }
    return c;
}

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

License:Open Source License

/**
 * This method archives the desktop-data of the desktop with the name {@code name} to a
 * zipped xml-file. The file contains the desktop-data, the modifed-entries-data for those entries
 * that appear on the desktop and the saved desktop-notes.
 *
 * @param name the name of the desktop that should be archived.
 * @return the archived document as XML-focument, or {@code null} if an error occured.
 *//*  ww w  .j  a v a2s .  com*/
public Document archiveDesktop(String name) {
    // create new document
    Document archive = new Document(new Element("archivedDesktop"));
    // add desktop-element of desktop that should be archived
    Element deskel = getDesktopElement(name);
    // if we found a valid value, go on
    if (deskel != null) {
        try {
            // set name attribute
            archive.getRootElement().setAttribute("name", name);
            // create desktop-element
            Element content_desktop = new Element("desktop");
            // clone content from current desktop
            content_desktop.addContent(deskel.cloneContent());
            // add element to archive-file
            archive.getRootElement().addContent(content_desktop);
            // now retrieve desktop-notes
            Element noteel = getDesktopNotes(name);
            // if we found a valid value, go on
            if (noteel != null) {
                // create notes-element
                Element content_notes = new Element("desktopNotes");
                // clone content from current desktop-notes
                content_notes.addContent(noteel.cloneContent());
                // add content
                archive.getRootElement().addContent(content_notes);
            }
            // now retrieve all timestamps from the archived desktop
            // and look for modified entries...
            // create new list that will contain the timestamps
            timestampList = new ArrayList<String>();
            // fill list with all entry-numbers. since we have sub-bullets/levels, we
            // recursevly go through the desktop-data
            retrieveDesktopEntriesTimestamps(deskel);
            // if we have any results, go on...
            if (timestampList.size() > 0) {
                // create base element
                Element modifiedel = new Element("modifiedEntries");
                // add all modified entries that appear on the archived desktop
                String[] timestamps = timestampList.toArray(new String[timestampList.size()]);
                for (String ts : timestamps) {
                    // retrieve modifed entry
                    Element me_dummy = retrieveModifiedEntryElementFromTimestamp(ts);
                    // check for valid value
                    if (me_dummy != null) {
                        // crate new modified-entry-element
                        Element me = new Element(ELEMENT_ENTRY);
                        // set timestamp-attribute
                        me.setAttribute(ATTR_TIMESTAMP, ts);
                        // and add modified text
                        me.setText(me_dummy.getText());
                        // and add content
                        modifiedel.addContent(me);
                    }
                }
                archive.getRootElement().addContent(modifiedel);
            }
        } catch (IllegalNameException ex) {
            Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
            return null;
        } catch (IllegalDataException ex) {
            Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
            return null;
        } catch (IllegalAddException ex) {
            Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
            return null;
        }
    }
    return archive;
}

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

License:Open Source License

/**
 * This method imports an archived desktop-file and appends it to the current desktop-data.
 * desktop-content, desktop-notes and modifed entries are being added.
 * @param archive the archive-file as xml-Document
 * @return one of the following return values:<br>
 * <ul>//from  ww  w  .java  2  s . c  o m
 * <li>{@link #IMPORT_ARCHIVE_OK IMPORT_ARCHIVE_OK} in case the import was successful</li>
 * <li>{@link #IMPORT_ARCHIVE_ERR_DESKTOPNAME_EXISTS IMPORT_ARCHIVE_ERR_DESKTOPNAME_EXISTS} in case
 * the desktop-name already exists, so the user is asked to enter another name</li>>
 * <li>{@link #IMPORT_ARCHIVE_ERR_OTHER IMPORT_ARCHIVE_ERR_OTHER} in case a general error occured</li>
 * </ul>
 */
public int importArchivedDesktop(Document archive) {
    // get imported desktopname
    String name = archive.getRootElement().getAttributeValue("name");
    // check whether we have any name at all. if not, return false
    if (null == name || name.isEmpty())
        return IMPORT_ARCHIVE_ERR_DESKTOPNAME_EXISTS;
    // first of all, go through all desktops and check whether the name
    // already exist, to avoid double naming...
    // when such a desktopname as "name" already exists, return false
    for (int cnt = 0; cnt < getCount(); cnt++)
        if (name.equalsIgnoreCase(getDesktopName(cnt)))
            return IMPORT_ARCHIVE_ERR_DESKTOPNAME_EXISTS;
    // create new element
    Element d = new Element("desktop");
    try {
        // set the desktop's name as attribute
        d.setAttribute("name", name);
        // get desktop-content from archive
        d.addContent(archive.getRootElement().getChild("desktop").cloneContent());
        // add the element to the desktop
        desktop.getRootElement().addContent(d);
        // set currentDesktop index to the new desktop-element
        currentDesktop = desktop.getRootElement().getContentSize() - 1;
        // also add new desktop-notes-element
        Element desk = new Element("desktop");
        // set name attribute
        desk.setAttribute("name", name);
        // create notes elements
        Element n1 = new Element("notes1");
        Element n2 = new Element("notes2");
        Element n3 = new Element("notes3");
        // get notes-child
        Element noteschild = archive.getRootElement().getChild("desktopNotes");
        // check whether we have any content
        if (noteschild != null) {
            // get and add notes...
            Element nc1 = noteschild.getChild("notes1");
            if (nc1 != null)
                n1.setText(nc1.getText());
            // get and add notes...
            Element nc2 = noteschild.getChild("notes2");
            if (nc2 != null)
                n2.setText(nc2.getText());
            // get and add notes...
            Element nc3 = noteschild.getChild("notes3");
            if (nc3 != null)
                n3.setText(nc3.getText());
        }
        // add notes-sub-elements
        desk.addContent(n1);
        desk.addContent(n2);
        desk.addContent(n3);
        // add element to desktop-notes
        desktopNotes.getRootElement().addContent(desk);
        // finally, add modified entries...
        List<Element> modent = archive.getRootElement().getChild("modifiedEntries").getChildren();
        // create iterator
        Iterator<Element> modit = modent.iterator();
        // and add all mofied entries
        while (modit.hasNext()) {
            // get element
            Element mod = modit.next();
            // and add modified entry
            addModifiedEntry(mod.getAttributeValue(ATTR_TIMESTAMP), mod.getText());
        }
        setModified(true);
    } catch (IllegalNameException ex) {
        Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
        return IMPORT_ARCHIVE_ERR_OTHER;
    } catch (IllegalDataException ex) {
        Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
        return IMPORT_ARCHIVE_ERR_OTHER;
    } catch (IllegalAddException ex) {
        Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
        return IMPORT_ARCHIVE_ERR_OTHER;
    }
    return IMPORT_ARCHIVE_OK;
}

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

License:Open Source License

/**
 * This method retrieves the searchterms for a given search-request.
 * //from  w  w  w  .j  a v a2  s .c  om
 * @param nr The number of the search request
 * @return the searchterms as string array, or null if an error occured
 */
public String[] getSearchTerms(int nr) {
    // get the element
    Element el = retrieveElement(nr);
    // if we found an element, go on...
    if (el != null) {
        // retrieve a list with all search terms
        List<?> terms = el.getChild("searchterms").getChildren();
        // if we have no elements, return null
        if ((terms.isEmpty()) || (terms.size() < 1)) {
            return null;
        }
        // create linked list for return results
        LinkedList<String> st = new LinkedList<String>();
        // create iterator
        Iterator<?> i = terms.iterator();
        // go through all search terms
        while (i.hasNext()) {
            // get each search-term-element
            Element e = (Element) i.next();
            // if it has text, add it to linked list
            if (!e.getText().isEmpty()) {
                st.add(e.getText());
            }
        }
        // when we searched for entries whithout authors or keywords e.g., the
        // searchterms are emptry. in this case, return null
        if (st.isEmpty()) {
            return null;
        }
        // copy all children to an array
        String[] retval = st.toArray(new String[st.size()]);
        // return results
        return retval;
    }
    return null;
}

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

License:Open Source License

/**
 * This method retrieves the searchterms for a given search-request.
 *
 * @param nr The number of the search request
 * @return {@code true} if the search was a synonym-search, false otherweise
 *//*from w ww  . ja  v a2s.  co m*/
public boolean isSynonymSearch(int nr) {
    // get the element
    Element el = retrieveElement(nr);
    // if we found an element, return "true"
    if (el != null) {
        // retrieve synonyms-child-element
        el = el.getChild("synonyms");
        // if we have any element, return value
        if (el != null) {
            return (el.getText().equalsIgnoreCase("true"));
        }
    }
    return false;
}

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

License:Open Source License

/**
 * This method retrieves the reg-ex-state, i.e. whether a search contained regular expressions
 * as search terms or "normal" search terms
 *
 * @param nr The number of the search request
 * @return {@code true} if the search was a regular-expression-search, false otherweise
 *///from  w  w  w.  j  a  va 2  s.c o  m
public boolean isRegExSearch(int nr) {
    // get the element
    Element el = retrieveElement(nr);
    // if we found an element, return "true"
    if (el != null) {
        // retrieve synonyms-child-element
        el = el.getChild("regex");
        // if we have any element, return value
        if (el != null) {
            return (el.getText().equalsIgnoreCase("true"));
        }
    }
    return false;
}

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

License:Open Source License

/**
 * Retrieves the recent document at the position {@code nr}. Returns {@code null} if recent document
 * does not exist or is empty/*from  w  w  w .  j  a  va  2s.  co m*/
 * @param nr the number of the requested recent document. use a value from 1 to {@link #recentDocCount recentDocCount}.
 * @return the recent document (the file path) as string, or {@code null} if no such element or path exists.
 */
public String getRecentDoc(int nr) {
    // retrieve element
    Element el = settingsFile.getRootElement().getChild(SETTING_RECENT_DOC + String.valueOf(nr));
    // if we have any valid document
    if (el != null) {
        // check whether its value is empty
        String retval = el.getText();
        // and if not, return in
        if (!retval.isEmpty())
            return retval;
    }
    // else return null
    return null;
}

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

License:Open Source License

/**
 * Retrieves the filepath of the last used main datafile
 * @return the filepath of the last used main datafile, or null if no filepath was specified.
 *//*from ww  w.j a  v  a2  s.  c om*/
public File getFilePath() {
    // we do this step by step rather that appending a ".getText()" to the line below, because
    // by doing so we can check whether the child element exists or not, and avoiding null pointer
    // exceptions
    // first, get the filepath, which is in relation to the zkn-path
    Element el = settingsFile.getRootElement().getChild(SETTING_FILEPATH);
    // create an empty string as return value
    String value = "";
    // is the element exists, copy the text to the return value
    if (el != null)
        value = el.getText();
    // when we have no filename, return null
    if (value.isEmpty())
        return null;
    // else return filepath
    return new File(value);
}

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

License:Open Source License

/**
 * Retrieves the filepath of the last used image path when inserting images to a new entry
 * @return the filepath of the last opened image directory, or null if no filepath was specified.
 *//* ww w  .  j a  v  a  2 s  .  c  o m*/
public File getLastOpenedImageDir() {
    // we do this step by step rather that appending a ".getText()" to the line below, because
    // by doing so we can check whether the child element exists or not, and avoiding null pointer
    // exceptions
    // first, get the filepath, which is in relation to the zkn-path
    Element el = settingsFile.getRootElement().getChild(SETTING_LASTOPENEDIMAGEDIR);
    // create an empty string as return value
    String value = "";
    // is the element exists, copy the text to the return value
    if (el != null)
        value = el.getText();
    // when we have no filename, return null
    if (value.isEmpty())
        return null;
    // else return filepath
    return new File(value);
}