Example usage for org.jdom2 Element getAttributeValue

List of usage examples for org.jdom2 Element getAttributeValue

Introduction

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

Prototype

public String getAttributeValue(final String attname) 

Source Link

Document

This returns the attribute value for the attribute with the given name and within no namespace, null if there is no such attribute, and the empty string if the attribute value is empty.

Usage

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

License:Open Source License

/**
 * This method retrieves the element of a modified entry. the modified entries' content is stored
 * in a separated XML-Document (see {@link #modifiedEntries modifiedEntries}. each element of this
 * document has a timestamp-attribute that equals the timestamp-attribute of an entry in the
 * {@link #desktop desktop}-Document.//from  w  w  w.j a v a2 s . co  m
 * <br><br>
 * So, by passing a {@code timestamp} value, this method searches whether we have any modified entry
 * that has the same timestamp-attribut, and if so, it returns that element which was
 * modified (and thus differs from an entry's content as it is stored in the original database).
 *
 * @param timestamp the timestamp which should match the requested entry's timestamp-attribute
 * @return the modified entry as element, or {@code null} if no entry was found.
 */
private Element retrieveModifiedEntryElementFromTimestamp(String timestamp) {
    // retrieve all elements
    List<Content> elementList = modifiedEntries.getRootElement().getContent();
    // when we have any content, go on...
    if (elementList.size() > 0) {
        for (Content elementList1 : elementList) {
            // retrieve each single element
            Element e = (Element) elementList1;
            // retrieve timestamp-attribute
            String att = e.getAttributeValue(ATTR_TIMESTAMP);
            // compare timestamp-attribute-value to timestamp-parameter
            if (att != null && att.equals(timestamp)) {
                // if they match, return that element
                return e;
            }
        }
    }
    // else return null
    return null;
}

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
 *//*from www .jav  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

/**
 * 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
 * @param note the content of the notes-textfield
 *///from   ww w .j  a va2 s.c o  m
public void setDesktopNotes(int nr, String note) {
    // check for valid parameter
    if (nr >= 1 && nr <= 3 && note != null) {
        // first check, whether the note has been modified or not. therefor, retrieve
        // current note-text and compare it to the parameter
        String currentnote = getDesktopNotes(nr);
        // if notes don't equal, go on...
        if (!currentnote.equals(note)) {
            // 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 el = desk.getChild("notes" + String.valueOf(nr));
                    // set note text
                    el.setText(note);
                    // change modify-tag
                    setModified(true);
                }
            }
        }
    }
}

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

License:Open Source License

/**
 * This method updates the timestamp-attributes when the datafile is being updated due to
 * a newer file-version.<br><br>
 * This method is called when updating a file with version number 3.0 or 3.1 to 3.2 and higher.
 *
 * @param e the initial element, where the updating should start. usually, use something
 * like {@link #getDesktopElement(int) getDesktopElement(int)}.
 *///from   w  w w .  j a  v  a  2s.c  om
private void updateTimestamps(Element e) {
    // get a list with all children of the element
    List<Element> children = e.getChildren();
    // create an iterator
    Iterator<Element> it = children.iterator();
    DecimalFormat df = new DecimalFormat("00000");
    // go through all children
    while (it.hasNext()) {
        // get the child
        e = it.next();
        try {
            if (e.getName().equals(ELEMENT_ENTRY)) {
                String timestamp = e.getAttributeValue(ATTR_TIMESTAMP);
                if (timestamp != null)
                    e.setAttribute(ATTR_TIMESTAMP, timestamp.concat(df.format(timestampid++)));
            }
            if (e.getName().equals(ELEMENT_BULLET)) {
                e.setAttribute(ATTR_TIMESTAMP, Tools.getTimeStamp() + df.format(timestampid++));
                e.setAttribute("treefold", TREE_FOLDING_EXPANDED);
            }
        } catch (IllegalNameException ex) {
            Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
        } catch (IllegalDataException ex) {
            Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
        }
        // when the new element also has children, call this method again,
        // so we go through the strucuture recursively...
        if (hasChildren(e)) {
            updateTimestamps(e);
        }
    }
}

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 www. j  av  a 2  s . co  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

public int findEntryNrFromTimestamp(int desktopnr, String t) {
    Element e = findEntryElementFromTimestamp(getDesktopElement(desktopnr), t);
    // check for valid entry
    if (null == e)
        return -1;
    String att = e.getAttributeValue("id");
    if (att != null) {
        try {//www  .java  2s .co m
            // get entry-number
            return Integer.parseInt(att);
        } catch (NumberFormatException ex) {
            return -1;
        }
    }
    return -1;
}

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

License:Open Source License

/**
 * This method retrieves the entry-element which <i>timestamp</i>-attribute
 * matches the parameter {@code t}.//from   w ww .  jav a  2s  .  c  om
 *
 * @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
 * @return the element which timestamp-attribute matches the parameter {@code t}, or {@code null}
 * if no element was found
 */
public Element findEntryElementFromTimestamp(Element e, String t) {
    // check for valid entry
    if (null == e)
        return null;
    // check whether the element "e" passed as parameter already has a timestamp-attribute that
    // matches the parameter "t". if so, return that element.
    if (e.getAttributeValue(ATTR_TIMESTAMP) != null && e.getAttributeValue(ATTR_TIMESTAMP).equals(t))
        return e;
    // 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))
            return e;
        // when the new element also has children, call this method again,
        // so we go through the strucuture recursively...
        if (hasChildren(e)) {
            // go into method again to traverse child-elements
            e = findEntryElementFromTimestamp(e, t);
            // when one of the child matches the requested entry, leave function without any further iteration
            if (e != null && e.getAttributeValue(ATTR_TIMESTAMP) != null
                    && e.getAttributeValue(ATTR_TIMESTAMP).equals(t))
                return e;
        }
    }
    if (e != null)
        return (e.getAttributeValue(ATTR_TIMESTAMP) != null && e.getAttributeValue(ATTR_TIMESTAMP).equals(t))
                ? e
                : null;
    else
        return null;
}

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>/* w  ww. j a  va  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.DesktopData.java

License:Open Source License

/**
 * This method returns all desktop-notes of the desktop with the name {@code desktopname}
 * as {@code Element} with the notes as children.
 * If no such desktop exists that matches the parameter {@code desktopname}, {@code null}
 * is returned./* w w  w .  ja  v  a 2s.  co m*/
 *
 * @param desktopname the name of the desktop of which we want to retrieve the notes-elements
 * @return the note-elements of the requested desktop-data, or {@code null} if an error occured
 */
public Element getDesktopNotes(String desktopname) {
    // 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();
        // check for desktop-name
        if (desk.getAttributeValue("name").equals(desktopname)) {
            // return note
            return desk;
        }
    }
    return null;
}

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

License:Open Source License

/**
 * Retrieves settings for the mainfont (the font used for the main-entry-textfield).
 * @param what (indicates, which font-characteristic we want to have. use following constants:<br>
 * - FONTNAME<br>/*  w w  w.  j  av a2  s.c  o  m*/
 * - FONTSIZE<br>
 * - FONTCOLOR<br>
 * - FONTSTYLE<br>
 * - FONTWEIGHT<br>
 * @return the related font-information as string.
 */
public String getMainfont(int what) {
    Element el = settingsFile.getRootElement().getChild(SETTING_MAINFONT);
    String retval = "";
    if (el != null) {
        switch (what) {
        case FONTNAME:
            retval = el.getText();
            break;
        case FONTSIZE:
            retval = el.getAttributeValue("size");
            break;
        case FONTCOLOR:
            retval = el.getAttributeValue("color");
            break;
        case FONTSTYLE:
            retval = el.getAttributeValue("style");
            break;
        case FONTWEIGHT:
            retval = el.getAttributeValue("weight");
            break;
        }
    }
    return retval;
}