Example usage for org.jdom2 Element getChild

List of usage examples for org.jdom2 Element getChild

Introduction

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

Prototype

public Element getChild(final String cname) 

Source Link

Document

This returns the first child element within this element with the given local name and belonging to no namespace.

Usage

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

License:Open Source License

/**
 * This method removes wrong placed edit-tags in the xml-file. this error occured in the
 * {@link #changeEditTimeStamp(int) changeEditTimeStamp()} method, where the edit-element,
 * child-element of the timestamp-element, was set as child of the zettel-element (and not its
 * child "timestamp"). This method tries to fix this error...
 *///from w w  w.  ja  va2  s. co m
public void fixWrongEditTags() {
    // iterate all elements
    for (int cnt = 1; cnt <= getCount(ZKNCOUNT); cnt++) {
        // retrieve element
        Element zettel = retrieveZettel(cnt);
        // check for valid value
        if (zettel != null) {
            // check whether element has a child named "edited". if so, it either
            // has to be moved as sub-child to the child-element timestamp, or removed
            // if "timestamp" already has an edit-element
            Element edited = zettel.getChild("edited");
            // only proceed, if wrong placed edited element exists
            if (edited != null) {
                // retrieve timestamp-element
                Element timestamp = zettel.getChild("timestamp");
                // check for valid value
                if (timestamp != null) {
                    // retrieve edited-timestamp
                    Element timestampedit = timestamp.getChild("edited");
                    // check whether edited-element exists
                    if (null == timestampedit) {
                        // if timestampedit is null, the element has no edited-element
                        // so we add the content of the wrong placed element as new edited-element
                        // create new edited element
                        Element ed = new Element("edited");
                        // add to timestamp
                        timestamp.addContent(ed);
                        // set content
                        ed.setText(edited.getText());
                    } else {
                        // now we know that an edited-element already exists
                        // we now want to check whether the existing editing-timestamp
                        // is older than the value in the wrong placed edited-element,
                        // and if so, update the timestamp
                        if (timestamp.getText().compareTo(edited.getText()) < 0)
                            timestampedit.setText(edited.getText());
                    }
                }
                // and remove wrong edited element
                zettel.removeChild("edited");
            }
        }
    }
}

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

License:Open Source License

/**
 * This method updates the timestamp-attributes in the data base. The former
 * XML-elements (timestamp) with the children "created" and "edited" are from database
 * version 3.4 on simply stored as attribute of each element.
 *//*from w w w  .j  a  va 2s  .co  m*/
public void db_updateTimestampAttributes() {
    // iterate all elements
    for (int cnt = 1; cnt <= getCount(ZKNCOUNT); cnt++) {
        // retrieve element
        Element zettel = retrieveZettel(cnt);
        // check for valid value
        if (zettel != null) {
            // init strings with default values
            String created = Tools.getTimeStamp();
            String edited = "";
            // retrieve created-timestamp
            Element el = zettel.getChild("timestamp").getChild("created");
            if (el != null)
                created = el.getText();
            // retrieve edited-timestamp
            el = zettel.getChild("timestamp").getChild("edited");
            if (el != null)
                edited = el.getText();
            // remove old values
            zettel.removeChild("timestamp");
            // and set timestamp as attributes
            setTimestamp(zettel, created, edited);
        }
    }
}

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

License:Open Source License

/**
 * This method replaces all numeral references to other entries
 * from manual links and trailing entry references with the referenced
 * entries' IDs.//from w  ww.j a va  2  s . c  o  m
 * <br><br>
 * That means, the trails and manual link child attributes that contain
 * numbers of other entries will be modified, so these attributes contain
 * the entries' IDs instead of their numbers.
 * 
 * @param zettel the entry element where the number references should be replaced with
 * entry IDs
 */
private void replaceAttributeNrWithID(Element zettel) {
    // create string array for attribute iteration
    String[] attributes = new String[] { ELEMENT_TRAILS, ELEMENT_MANLINKS };
    // iterate array
    for (String attr : attributes) {
        // check whether entry has luhmann-element
        if (zettel.getChild(attr) != null) {
            // get Luhmann-numbers (trailing-entries)
            String luh = zettel.getChild(attr).getText();
            // check whether entry has trailing-numbers
            if (!luh.isEmpty()) {
                // split them into an array...
                String[] luhmann = luh.split(",");
                // prepare string builder
                StringBuilder sb = new StringBuilder("");
                // if we have trailing numbers, go on
                if (luhmann != null && luhmann.length > 0) {
                    for (String luhmann1 : luhmann) {
                        // retrieve luhmann number
                        try {
                            // retrieve number of luhmann entry
                            int nr = Integer.parseInt(luhmann1);
                            // get unique ID of that entry
                            String val = getZettelID(nr);
                            // replace numeral reference with ID number
                            if (val != null) {
                                sb.append(val).append(",");
                            }
                        } catch (NumberFormatException ex) {
                        }
                    }
                    // finallay, remove the last ","
                    if (sb.length() > 1) {
                        sb.setLength(sb.length() - 1);
                    }
                    // update attribute
                    zettel.getChild(attr).setText(sb.toString());
                }
            }
        }
    }
}

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

License:Open Source License

/**
 * This method replaces all ID references to other entries
 * from manual links and trailing entry references with the 
 * entries' number./*  w ww  . j  a v a 2s . c om*/
 * <br><br>
 * That means, the trails and manual link child attributes that have been converted
 * by the {@link #replaceAttributeNrWithID(org.jdom.Element) replaceAttributeNrWithID} method
 * will be "resetted", so these attributes contain the entries' numbers again instead of their IDs.
 * 
 * @param zettel the entry element where the IDs should be replaced with
 * the entry number
 */
private void replaceAttributeIDWithNr(Element zettel) {
    // check whether entry is deleted or not
    if (isDeleted(zettel)) {
        return;
    }
    //
    // here we change the entry's luhmann-numbers (trailing entries) and the
    // entry's manual links with the unique IDs
    //
    // create string array for attribute iteration
    String[] attributes = new String[] { ELEMENT_TRAILS, ELEMENT_MANLINKS };
    // iterate array
    for (String attr : attributes) {
        // we have to re-convert the unique entry-ID's to their
        // related entry-numbers, which we do here
        // first, retrieve list of IDs
        String[] values = zettel.getChild(attr).getText().split(",");
        // create variable with re-converted values
        StringBuilder final_values = new StringBuilder("");
        // check for valid values                            
        if (values.length > 0) {
            // iterate all numbers, which are at the moment
            // still unique IDs instead of numbers (referring to an entry)
            for (String v : values) {
                // retrieve entry number of that entry with
                // the uniqe ID stored in "l"
                int nr = findZettelFromID(v);
                // check whether ID was found
                if (nr != -1) {
                    // append number to stringbuilder
                    final_values.append(String.valueOf(nr)).append(",");
                }
            }
            // check whether we have any values in the stringbuilder
            // and truncate last comma
            if (final_values.length() > 0) {
                final_values.setLength(final_values.length() - 1);
            }
        }
        // set back converted parameters
        zettel.getChild(attr).setText(final_values.toString());
    }
}

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

License:Open Source License

public int getCommentCount(Element e, int commentcount) {
    // if we don't have any element, return null
    if (e == null)
        return commentcount;
    // 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();//from   w w  w .  j  a  va 2s. co m
        // else check whether we have child-elements - if so, re-call method
        if (hasChildren(e))
            commentcount = getCommentCount(e, commentcount);
        // check whether we have an entry-element that matched the requested id-number
        if (e != null) {
            // 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);
                if (comel != null && !comel.getText().isEmpty())
                    commentcount++;
            } else {
                // else return the element's text
                if (!e.getText().isEmpty())
                    commentcount++;
            }
        }
    }
    return commentcount;
}

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

License:Open Source License

/**
 * Sets the comment of the selected entry in the jTree in the CDesktop-frame. This method
 * traverses the xml-datafile, looking in each "depth-level" for the element that is 
 * given in the linked list parameter <i>tp</i>. This parameter contains the treepath to
 * the selected element.//from   w ww.  j a  v  a  2  s  . c o m
 * 
 * @param timestamp
 * @param comment a string containing the comment
 * @return {@code true} if comment was successfully set, {@code false} if an error occured.
 */
public boolean setComment(String timestamp, String comment) {
    // retrieve element that matches the timestamp
    Element e = findEntryElementFromTimestamp(getCurrentDesktopElement(), timestamp);
    // check whether an entry was found or not
    if (e != null) {
        try {
            // 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.
                e.getChild(ATTR_COMMENT).setText(comment);
            } else {
                // set comment for entry
                e.setText(comment);
            }
            // change modified state
            setModified(true);
        } catch (IllegalDataException ex) {
            Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage());
            return false;
        }
    }
    return true;
}

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 w  w .j  ava2s  .  c  om
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 w  w  w .j  ava  2  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 retrieves the comment of an entry-element which <i>timestamp</i>-attribute
 * matches the parameter {@code t}./*from w  ww .  j  ava 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 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. j  a v  a  2  s .co  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;
}