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

License:Open Source License

/**
 * This method checks whether a keyword-element with the number {@code nr} has a unique
 * ID assigned to it or not.//from w w w. j  av a 2 s .c o m
 * <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).
 *
 * @param nr the number of the keyword-element that shoud be checked for a valid ID.
 * the position of the element, ranged from 1 to {@link #getCount(int) getCount(KWCOUNT)}
 * @return {@code true} if an ID was found, {@code false} otherwise.
 */
public boolean hasKeywordID(int nr) {
    // retrieve element
    Element keyword = retrieveElement(keywordFile, nr);
    // check for valid value
    if (keyword != null) {
        // now add id to keyword-element
        String id = keyword.getAttributeValue(ATTRIBUTE_KEYWORD_ID);
        // check for valid value
        if (id != null && !id.isEmpty()) {
            return true;
        }
    }
    return false;
}

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

License:Open Source License

/**
 * This method retrieves the name of the desktop with the index {@code nr}.
 * //  w w w.  ja v a 2 s.c o m
 * @param nr the desktop-index of which we want to have the name, ranged from 0 to (count-1)
 * @return a String containing the name of the desktop, or a string {@code noDesktopNameFound}
 * if the desktop was not found...
 */
public String getDesktopName(int nr) {
    // get the requested dektop-element
    Element d = getDesktopElement(nr);
    // if no dektop was found, return empty string
    if (null == d)
        return resourceMap.getString("noDesktopNameFound");
    // get the desktop's name-attribute
    String retval = d.getAttributeValue("name");
    // check for content
    if ((null == retval) || (retval.isEmpty()))
        retval = resourceMap.getString("noDesktopNameFound");
    // and return it
    return retval;
}

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

License:Open Source License

/**
 * This method removes all entries which entry-number are passed in the int-array {@code entries}
 * from all available desktop-elements./* ww w .  j  av a 2 s.  c o  m*/
 * 
 * @param entries an integer-array containing the entry-numbers of those entries that should be
 * removed from the desktop
 * @return 
 */
public boolean deleteEntries(int[] entries) {
    // indicator for deletes entries
    boolean haveDeletedEntries = false;
    // check for valid parameter
    if (entries != null && entries.length > 0 && getCount() > 0) {
        // create linked list which will contain all to be deleted entries...
        LinkedList<Element> finallist = new LinkedList<Element>();
        // go through all desktops (outer loop)...
        for (int cnt = 0; cnt < getCount(); cnt++) {
            // ...and search for all entries in each desktop (inner loop)
            for (int e : entries) {
                // retrieve all found entries within the desktop
                LinkedList<Element> lle = searchForEntry(cnt, e);
                // check whether we have any returned entries...
                if (lle != null && lle.size() > 0) {
                    // create a new iterator for the found results
                    Iterator<Element> prepare = lle.iterator();
                    // iterate them
                    while (prepare.hasNext()) {
                        // get each single entry as element
                        Element entry = prepare.next();
                        // and add it to the final list
                        if (entry != null)
                            finallist.add(entry);
                    }
                }
            }
        }
        // if we found any elements that should be deleted, do
        // this now...
        if (finallist.size() > 0) {
            // create iterator
            Iterator<Element> i = finallist.iterator();
            // go trhough linked list
            while (i.hasNext()) {
                // get each element that should be deleted
                Element entry = i.next();
                // if we have a valid element, go on...
                if (entry != null) {
                    // retrieve timestamp
                    String timestamp = entry.getAttributeValue(ATTR_TIMESTAMP);
                    // check whether we have any modified entry. if so, delete it, since
                    // we no longer need it...
                    deleteModifiedEntry(timestamp);
                    // get the entry's parent
                    Parent p = entry.getParent();
                    // remove entry from parent
                    p.removeContent(entry);
                    // set deleted-indicator to true
                    haveDeletedEntries = true;
                }
            }
        }
    }
    // change modified state
    if (haveDeletedEntries)
        setModified(true);
    // return result
    return haveDeletedEntries;
}

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

License:Open Source License

/**
 * This method is called from the {@link #checkForDoubleEntry(int, int) checkForDoubleEntry(int, int)} method
 * and used to recursivly scan all elements of a desktop. If an entry-element which id-attribute matches
 * the parameter {@code entrynumber} was found, this element is returned, else {@code null} is returned.
 *
 * @param e the element where we start scanning. for the first call, use {@link #getDesktopElement(int) getDesktopElement(int)}
 * to retrieve the root-element for starting the recursive scan.
 * @param entrynumber the number of the entry we are looking for. if any element's id-attribut matches this
 * parameter, the element is return, else null
 * @return an element which id-attribute matches the parameter {@code entrynumber}, or null if no element
 * was found//from w  w w.jav  a2 s.c om
 */
private LinkedList<Element> findElements(Element e, String entrynumber, LinkedList<Element> foundelements) {
    // if we don't have any element, return null
    if (e == null)
        return foundelements;
    // 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();
        // else check whether we have child-elements - if so, re-call method
        if (hasChildren(e))
            foundelements = findElements(e, entrynumber, foundelements);
        // check whether we have an entry-element that matched the requested id-number
        if (e != null && e.getName().equals(ELEMENT_ENTRY)) {
            // check whether attribute exists
            String att = e.getAttributeValue("id");
            // if so, and it machtes the requested id-number, add element to list
            if (att != null && att.equals(entrynumber))
                foundelements.add(e);
        }
    }
    return foundelements;
}

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

License:Open Source License

/**
 * This method recursivly scans all elements of a desktop. If an entry-element which id-attribute matches
 * the parameter {@code entrynumber} was found, this method returns {@code true}.
 *
 * @param e the element where we start scanning. for the first call, use {@link #getDesktopElement(int) getDesktopElement(int)}
 * to retrieve the root-element for starting the recursive scan.
 * @param entrynumber the number of the entry we are looking for. if any element's id-attribut matches this
 * parameter, the element is return, else null
 * @param found the initial value, should be {@code false} when initially called
 * @return {@code true} when the entry with the number {@code entrynumber} was found, {@code false} otherwise.
 *///  w w w . j  a  va  2 s.  com
public boolean desktopHasElement(Element e, String entrynumber, boolean found) {
    // if we don't have any element, return null
    if (e == null)
        return false;
    // 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();
        // else check whether we have child-elements - if so, re-call method
        if (hasChildren(e))
            found = desktopHasElement(e, entrynumber, found);
        // check whether an entry was found in children
        if (found)
            return true;
        // check whether we have an entry-element that matched the requested id-number
        if (e != null && e.getName().equals(ELEMENT_ENTRY)) {
            // check whether attribute exists
            String att = e.getAttributeValue("id");
            // if so, and it machtes the requested id-number, add element to list
            if (att != null && att.equals(entrynumber)) {
                // save element
                foundDesktopElement = e;
                return true;
            }
        }
    }
    return found;
}

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

License:Open Source License

/**
 * Checks whether a bullet-point's treefold-state is expanded or not.
 *
 * @param timestamp the timestamp of the bzllet point, which treefold-state we want to check
 * @return {@code true} if bullet is expanded, {@code false} if it is collapsed
 *///from  ww  w  . j a v  a  2  s .  c o  m
public boolean isBulletTreefoldExpanded(String timestamp) {
    // retrieve bullet of which fold-state should be switched
    Element b = findEntryElementFromTimestamp(getCurrentDesktopElement(), timestamp);
    // check for valid value
    if (b != null) {
        // check whether attribute exists
        String tf = b.getAttributeValue("treefold");
        // set new treefold-attribute
        return (tf != null && !tf.isEmpty()) ? tf.equals(TREE_FOLDING_EXPANDED) : true;
    }
    return true;
}

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

License:Open Source License

/**
 * This method deletes the current activated desktop.
 *///from   w  ww.java2 s.c o m
public void deleteDesktop() {
    // check whether we have any desktops at all
    if (getCount() > 0) {
        // get current desktop
        Element d = getCurrentDesktopElement();
        // retrieve all timestamps of those entries which are children of the to be
        // deleted desktop
        String[] timestamps = retrieveBulletTimestamps(d);
        // now remove all possible modified entries of that list
        if (timestamps != null && timestamps.length > 0) {
            // iterate all timestamps
            for (String singlets : timestamps) {
                // delete modified entries that already have been deleted due
                // to the removal of the bullet-point
                deleteModifiedEntry(singlets);
            }
        }
        // delete notes that are associated to this desktop
        deleteDesktopNotes(d.getAttributeValue("name"));
        // and remove it from the root
        desktop.getRootElement().removeContent(d);
        // set currentDesktop index to the new desktop-element
        currentDesktop = desktop.getRootElement().getContentSize() - 1;
        // change modified state
        setModified(true);
    }
}

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

License:Open Source License

private void deleteDesktopNotes(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();
        // get desktop-name-attribute
        String att = desk.getAttributeValue("name");
        // check for desktop-name
        if (att != null && att.equals(desktopname)) {
            // if we found it, remove desktop-notes
            desktopNotes.getRootElement().removeContent(desk);
            // change modified flag
            setModified(true);//  ww  w  . j av a 2  s.  c o  m
            // and leave methode
            return;
        }
    }
}

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

License:Open Source License

/**
 * /*from   w  ww . j  a v a 2  s  . c  om*/
 * @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   w  w w.  j a  va2s .c o  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);
            }
        }
    }
}