Example usage for org.jdom2 Element getChildren

List of usage examples for org.jdom2 Element getChildren

Introduction

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

Prototype

public List<Element> getChildren() 

Source Link

Document

This returns a List of all the child elements nested directly (one level deep) within this element, as Element objects.

Usage

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

License:Open Source License

/**
 * This methods checks whether a given element {@code e} has child-elements
 * or not./*from w ww  .  jav  a2 s  .  c  o  m*/
 * @param e the element which should be checked for children
 * @return {@code true} if {@code e} has children, {@code false} otherwise
 */
public boolean hasChildren(Element e) {
    if (null == e)
        return false;
    return (!e.getChildren().isEmpty());
}

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

License:Open Source License

/**
 * /* ww w. j  a va  2 s.c  o m*/
 * @param e
 */
private void retrieveDesktopEntries(Element 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();
        // we have to ignore the comment-tags here. comments are no tags that will
        // be displayed in the jtree, but that store comments which will be displayed
        // in the jeditorpane (see "updateDisplay" method for further details)
        if (!e.getName().equals(ATTR_COMMENT)) {
            // check whether we have no bullet, just an entry
            if (!e.getName().equals(ELEMENT_BULLET)) {
                // retrieve id-attribute
                String att = e.getAttributeValue("id");
                // check for valid value
                if (att != null) {
                    // get entry-number
                    int enr = Integer.parseInt(att);
                    // sort list so we can search whether entry-number already exists
                    Collections.sort(retrieveList);
                    // search for double entries
                    if (Collections.binarySearch(retrieveList, enr) < 0) {
                        // now we know we have an entry. so get the entry number...
                        retrieveList.add(enr);
                    }
                }
            }
            // when the new element also has children, call this method again,
            // so we go through the strucuture recursively...
            if (hasChildren(e)) {
                retrieveDesktopEntries(e);
            }
        }
    }
}

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

License:Open Source License

/**
 * This method retrieves all entries' timestamps of the element {@code e} and all its
 * child-element. Thus, {@code e} could either be a root-(desktop-)element or a bullet-element.
 *
 * @param e the starting-element from where we want to have all entries' timestamps, including
 * all children of {@code e}.//from www. j a  v  a  2  s .co m
 */
private void retrieveDesktopEntriesTimestamps(Element 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();
        // we have to ignore the comment-tags here. comments are no tags that will
        // be displayed in the jtree, but that store comments which will be displayed
        // in the jeditorpane (see "updateDisplay" method for further details)
        if (!e.getName().equals(ATTR_COMMENT)) {
            // check whether we have no bullet, just an entry
            if (!e.getName().equals(ELEMENT_BULLET)) {
                // get timestamp-attribute
                String att = e.getAttributeValue(ATTR_TIMESTAMP);
                // and add its timestamp to the list
                if (att != null)
                    timestampList.add(att);
            }
            // when the new element also has children, call this method again,
            // so we go through the strucuture recursively...
            if (hasChildren(e)) {
                retrieveDesktopEntriesTimestamps(e);
            }
        }
    }
}

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)}.
 *//*  w ww  .j a  va2s.co  m*/
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  w  w  w .  j a  v  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

/**
 * This method retrieves the entry-element which <i>timestamp</i>-attribute
 * matches the parameter {@code t}./*from ww  w  .  jav  a 2  s  .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
 * @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.Synonyms.java

License:Open Source License

public String[] getSynonymLine(Document doc, int nr, boolean matchcase) {
    // get element
    Element syn = retrieveElement(doc, nr);
    // init return value
    String[] retval = null;/*from  ww  w.  j a  v  a  2 s .  c  o m*/
    // if we have a valid element, go on
    if (syn != null) {
        // get list of child-element with synonyms
        List l = syn.getChildren();
        // create array
        retval = new String[l.size() + 1];
        // first element of the array is the index word
        if (matchcase) {
            // retrieve indexword-attribute
            String attr = syn.getAttributeValue("indexword");
            // check whether value ok
            if (attr != null) {
                // then use it for array
                retval[0] = attr;
            } else {
                // else log error message, telling the number of the corrupted synonym-data
                Constants.zknlogger.log(Level.WARNING, "No index word for synonym {0} found.",
                        String.valueOf(nr));
                // and return null
                return null;
            }
        } else {
            // retrieve indexword-attribute
            String attr = syn.getAttributeValue("indexword");
            // check whether value ok
            if (attr != null) {
                // then use it for array
                retval[0] = attr.toLowerCase();
            } else {
                // else log error message, telling the number of the corrupted synonym-data
                Constants.zknlogger.log(Level.WARNING, "No index word for synonym {0} found.",
                        String.valueOf(nr));
                // and return null
                return null;
            }
        }
        // following elements are the synonyms. therefore, copy the children's text to the array
        for (int cnt = 0; cnt < l.size(); cnt++) {
            // get the element
            Element e = (Element) l.get(cnt);
            // get the element's text
            if (matchcase) {
                retval[cnt + 1] = e.getText();
            } else {
                retval[cnt + 1] = e.getText().toLowerCase();
            }
        }
    }
    return retval;
}

From source file:de.danielluedecke.zettelkasten.DesktopFrame.java

License:Open Source License

/**
 * This method updates the jTreeView. Each time an update for the treevuew is needed, this
 * method is called. It then recursevly traverses all XML-Elements of the currently activated
 * desktop-element, where the starting desktop-element is passed in the parameter {@code e}.
 * <br><br>//from  w ww  .  j a va 2s .  c  o m
 * The method retrieves each element, checks whether the element is an entry- or a bullet-element,
 * than either, in case of a bullet point, uses the name-attribute as node-name and appends the
 * timestamp-attribute as ID; or it retrieves the entry's title from the entry-number that is stored
 * in each entry-element, and appends the entry's timestamp-attribute as ID.
 * <br><br>
 * After that, the node is inserted in the jTree.
 *
 * @param e
 * @param n
 * @param dtm
 */
private void fillChildren(Element e, DefaultMutableTreeNode n, DefaultTreeModel dtm) {
    // 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();
        // create a new node
        DefaultMutableTreeNode node;
        // we have to ignore the comment-tags here. comments are no tags that will
        // be displayed in the jtree, but that store comments which will be displayed
        // in the jeditorpane (see "updateDisplay" method for further details)
        if (!e.getName().equals("comment")) {
            // if the child is a bullet...
            if (e.getName().equals("bullet")) {
                // create new stringbuilder
                StringBuilder sb = new StringBuilder("");
                // append name of bullet point
                sb.append(e.getAttributeValue("name"));
                //                    // and append unique id, which is the element's timestamp
                //                    sb.append(" [id#").append(e.getAttributeValue("timestamp")).append("]");
                //                    // create a node with the element's name-attribute
                //                    node = new DefaultMutableTreeNode(sb.toString());
                // create a node with the element's name-attribute
                node = new DefaultMutableTreeNode(
                        new TreeUserObject(sb.toString(), e.getAttributeValue("timestamp"), ""));
                // and tell node to have children
                node.setAllowsChildren(true);
            } else {
                // now we know we have an entry. so get the entry number...
                int nr = Integer.parseInt(e.getAttributeValue("id"));
                // get the zettel title
                String title = TreeUtil.retrieveNodeTitle(dataObj, settingsObj.getShowDesktopEntryNumber(),
                        String.valueOf(nr));
                // create a new node
                node = new DefaultMutableTreeNode(
                        new TreeUserObject(title, e.getAttributeValue("timestamp"), String.valueOf(nr)));
                // and tell node not to have children
                node.setAllowsChildren(false);
            }
            // add new node to treeview
            dtm.insertNodeInto(node, n, n.getChildCount());
            // when the new element also has children, call this method again,
            // so we go through the strucuture recursively...
            if (desktopObj.hasChildren(e)) {
                fillChildren(e, node, dtm);
            }
        }
    }
}

From source file:de.danielluedecke.zettelkasten.DesktopFrame.java

License:Open Source License

/**
 * This method retrieves all entries on the desktop and adds their number to the
 * list {@code liste}. This array of entry-numbers is needed in the export-dialog.
 *
 * @param e the starting point for the jTree-enumeration, either the root elementor a bullet (if only
 * a bullet should be exported, see {@link #exportDesktopBullet() exportDesktopBullet()}).
 * @param liste an array-object that will hold the found entry-nubers
 *///from   www .j  a  v  a2  s.  c om
private void createExportEntries(Element e, ArrayList<Object> liste) {
    // if we have no element, return.
    if (null == e)
        return;
    // 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();
        // we have to ignore the comment-tags here. comments are no tags that will
        // be displayed in the jtree, but that store comments which will be displayed
        // in the jeditorpane (see "updateDisplay" method for further details)
        if (!e.getName().equals("comment")) {
            // if the child is a bullet...
            if (e.getName().equals("bullet")) {
                // first, we want to retrieve the header-level
                int headerlevel = 1;
                // get bullet's parent
                Element f = e.getParentElement();
                // as long as we have not reached the root, get further parent-elements
                // and increase counter for header-level
                while (!f.isRootElement()) {
                    f = f.getParentElement();
                    headerlevel++;
                }
                // add the element's name-attribute. since headers might consist of only numbers,
                // we add a char here. this is necessary, since the export-methods distinguish
                // between headers and entry-numbers simply by parsing integer-values. if the parsing
                // succeeds, we have an entry, if a NumberFormatException is thrown, we have a headline.
                // to treat headline with numbers only as headlines, we add a char to be sure that every
                // headline will throw an exception when parsing the array's elements to integer.
                liste.add("h" + String.valueOf(headerlevel) + e.getAttributeValue("name"));
            } else {
                // now we know we have an entry. so get the entry number...
                int nr = Integer.parseInt(e.getAttributeValue("id"));
                liste.add(nr);
            }
            // when the new element also has children, call this method again,
            // so we go through the strucuture recursively...
            if (desktopObj.hasChildren(e)) {
                createExportEntries(e, liste);
            }
        }
    }
}

From source file:de.dfki.iui.mmds.scxml.engine.impl.SCXMLEngineImpl.java

License:Apache License

private void propagateNamespace(Element element) {
    List children = element.getChildren();
    for (Object o : children) {
        Element child = (Element) o;
        if (child.getName().equals("raise")) {
            child.setNamespace(Namespace.getNamespace("ca", "http://www.dfki.de/mmds/scxml/customaction"));
        } else {//w  w  w  . j  a  v  a 2  s. c o m
            child.setNamespace(element.getNamespace());
        }
        propagateNamespace(child);
    }
}