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

/**
 * Removes a certain entry-number from the luhmann-numbers of an entry.
 * /*from  w  w  w . j  a va 2  s. co  m*/
 * @param entry the entry where a luhmann-number should be removed
 * @param removevalue the index-number that should be removed from "entry"
 */
public void deleteLuhmannNumber(int entry, int removevalue) {
    // check whether entry and removevalue are identical
    if (entry == removevalue)
        return;
    // get the entry where the luhmann-number should be added to
    Element zettel = retrieveElement(zknFile, entry);
    // if entry does not exist, leave
    if (null == zettel || null == zettel.getChild(ELEMENT_TRAILS))
        return;
    // get the luhmann-numbers  of that entry
    String lnr = zettel.getChild(ELEMENT_TRAILS).getText();
    // check whether the addvalue already exists in that entry
    if (!lnr.isEmpty()) {
        // copy all values to an array
        String[] lnrs = lnr.split(",");
        // create new string buffer for the final values
        StringBuilder sb = new StringBuilder("");
        // convert remove-value to string, so we can compare
        String removenr = String.valueOf(removevalue);
        // go through array of current luhmann-numbers
        for (String exist : lnrs) {
            // if the current luhhmann-number is not the one which should be deleted...
            if (!exist.equals(removenr)) {
                // ...add it to the buffer
                sb.append(exist);
                sb.append(",");
            }
        }
        // finally, remove trailing comma
        if (sb.length() > 1)
            sb.setLength(sb.length() - 1);
        // and set the new string to the luhmann-tag
        zettel.getChild(ELEMENT_TRAILS).setText(sb.toString());
        // addvalue was successfully added
        setModified(true);
    }
}

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

License:Open Source License

/**
 * This method inserts the entry-number {@code insertnr} as luhmann-number at the position
 * {@code pos} within the entry's {@code entry} luhmann-numbers.
 *
 * @param entry the entry where the luhmann-number {@code insertnr} should be inserted
 * @param insertnr the number of the entry that should be added as luhmann-number
 * @param pos the position of the {@code insertnr}, i.e. at which position {@code insertnr}
 * should be added as luhmann-number/*  w w  w. j av a 2s  .  c o m*/
 * @return 
 */
public boolean insertLuhmannNumber(int entry, int insertnr, int pos) {
    // check whether entry and removevalue are identical
    if (entry == insertnr)
        return false;
    // get the entry where the luhmann-number should be added to
    Element zettel = retrieveElement(zknFile, entry);
    // get the entry that should be added as luhmann-number
    Element tobeadded = retrieveElement(zknFile, insertnr);
    // if entry does not exist, leave
    if (null == zettel || null == zettel.getChild(ELEMENT_TRAILS))
        return false;
    // get the luhmann-numbers  of that entry
    String lnr = zettel.getChild(ELEMENT_TRAILS).getText();
    // check whether the addvalue already exists in that entry
    if (!lnr.isEmpty()) {
        // now we have to check, whether the current entry is already existing
        // in the entry that index-number (addvalue) we want to add to the luhmann-numbers
        // if "entry" already exists in entry "addvalue"'s luhmann-tag, we would have
        // an infinite loop...
        // the problem is, that we here have to recursively check not only the "addvalue"
        // entry's luhmann-tag, but also each sub-entry that consists in the "addvalue"
        // entry's tag...
        String alnr = tobeadded.getChild(ELEMENT_TRAILS).getText();
        // check whether the addvalue already exists in that entry
        // if entry exists in the addvalue-entry luhmann-tag, or in any sub-entry
        // of the addvalue-entry, leave method to prevent infinite loops
        if (!alnr.isEmpty() && existsInLuhmann(insertnr, entry, false))
            return false;
        // copy all values to an array
        String[] lnrs = lnr.split(",");
        // create list
        List<String> luhmannnrs = new ArrayList<String>();
        // copy all numbers to list, so we can insert the new number via this list
        // for (String ln : lnrs) luhmannnrs.add(ln);
        luhmannnrs.addAll(Arrays.asList(lnrs));
        try {
            // now insert the new number
            luhmannnrs.add(pos, String.valueOf(insertnr));
        } catch (IndexOutOfBoundsException e) {
            // if the index-number was out of bounds, append number to the end of the list
            luhmannnrs.add(String.valueOf(insertnr));
        }
        // create stringbuilder
        StringBuilder sb = new StringBuilder("");
        for (String luhmannnr : luhmannnrs) {
            sb.append(luhmannnr).append(",");
        }
        // finally, remove trailing comma
        if (sb.length() > 1)
            sb.setLength(sb.length() - 1);
        // and set the new string to the luhmann-tag
        zettel.getChild(ELEMENT_TRAILS).setText(sb.toString());
        // addvalue was successfully added
        setModified(true);
        // return success
        return true;
    }
    // return success
    return false;
}

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

License:Open Source License

/**
 * This method returns the content of an entry's luhmann-tag, i.e. the follower-
 * or sub-entries of an entry. These numbers are displayed in the tabbedpane in the
 * jTreeLuhmann (see ZettelkastenView.java for more details).
 * /*from   w ww .  j a  va2 s  .c om*/
 * @param pos the position of the entry which luhmann-numbers we want to have
 * @return a string with the comma-separated luhmann-numbers, or an empty string if the entry
 * has not luhmann-numbers.
 */
public String getLuhmannNumbers(int pos) {
    // get the entry
    Element zettel = retrieveElement(zknFile, pos);
    // if it exists...
    // return the content of the luhmann-child-element
    if (zettel != null && zettel.getChild(ELEMENT_TRAILS) != null)
        return zettel.getChild(ELEMENT_TRAILS).getText();
    // return result
    return "";
}

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

License:Open Source License

/**
 * Sets the manual links for an entry. these links appear in the main-window's tabbed pane
 * on the "links"-page, in the jTableManLinks.
 * // w  ww. java2  s  . c  o m
 * @param pos the entry-number that should get new manlinks
 * @param manlinks the entry-numbers where the entry "pos" refers to, stored in an integer-array
 */
public void setManualLinks(int pos, int[] manlinks) {
    // get the entry
    Element zettel = retrieveElement(zknFile, pos);
    // if we found an entry-element, go on
    if (zettel != null) {
        // if no child-element ELEMENT_MANLINKS exists, create it...
        if (null == zettel.getChild(ELEMENT_MANLINKS))
            zettel.addContent(new Element(ELEMENT_MANLINKS));
        // create stringbuilder
        StringBuilder sb = new StringBuilder("");
        // iterate int-array
        for (int ml : manlinks) {
            // and copy all int-values to array
            sb.append(String.valueOf(ml));
            sb.append(",");
        }
        // delete last comma
        if (sb.length() > 1)
            sb.setLength(sb.length() - 1);
        // and set value to element...
        zettel.getChild(ELEMENT_MANLINKS).setText(sb.toString());
        setModified(true);
    }
}

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

License:Open Source License

/**
 * This method returns the manual links for an entry as string-array.
 * /*from  w w w.ja v a2  s  . c o  m*/
 * @param pos the position of the entry which manual links we want to have
 * @return an string-array containing the entry-numbers where the entry {@code pos} refers to,
 * or {@code null} if no entry-numbers exist...
 */
public String[] getManualLinksAsString(int pos) {
    // get the entry
    Element zettel = retrieveElement(zknFile, pos);
    // if it exists...
    if (zettel != null && zettel.getChild(ELEMENT_MANLINKS) != null) {
        // get manual links
        String ml = zettel.getChild(ELEMENT_MANLINKS).getText();
        // if no manual links there, quit...
        if (ml.isEmpty())
            return null;
        // else split them into an array...
        String[] manlinks = ml.split(",");
        // if we have no manual links, return null...
        if ((null == manlinks) || manlinks.length < 1)
            return null;
        // return the content of the luhmann-child-element
        return manlinks;
    }
    // return result
    return null;
}

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

License:Open Source License

/**
 * This method returns the manual links for an entry as siingle string with
 * comma separated values/* w w  w .  j  a  v a2s. c  om*/
 * 
 * @param pos the position of the entry which manual links we want to have
 * @return siingle string with comma separated values where the entry {@code pos} refers to,
 * or {@code null} if no entry-numbers exist...
 */
public String getManualLinksAsSingleString(int pos) {
    // get the entry
    Element zettel = retrieveElement(zknFile, pos);
    // if it exists...
    if (zettel != null && zettel.getChild(ELEMENT_MANLINKS) != null) {
        // get manual links
        String ml = zettel.getChild(ELEMENT_MANLINKS).getText();
        // if no manual links there, quit...
        if (ml.isEmpty())
            return null;
        // else split them into an array...
        // return the content of the luhmann-child-element
        return ml;
    }
    // return result
    return null;
}

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

License:Open Source License

/**
 * Sets the manual links for an entry. these links appear in the main-window's tabbed pane
 * on the "links"-page, in the jTableManLinks.
 * //from   w w  w  .ja v a2s  .co  m
 * @param pos the entry-number that should get new manlinks
 * @param manlinks the entry-numbers where the entry "pos" refers to, stored in a string-value
 * (comma-separated)
 */
public void setManualLinks(int pos, String manlinks) {
    // get the entry
    Element zettel = retrieveElement(zknFile, pos);
    // if we found an entry-element, go on
    if (zettel != null) {
        // if no child-element ELEMENT_MANLINKS exists, create it...
        if (null == zettel.getChild(ELEMENT_MANLINKS))
            zettel.addContent(new Element(ELEMENT_MANLINKS));
        // and set value to element...
        zettel.getChild(ELEMENT_MANLINKS).setText(manlinks);
        setModified(true);
    }
}

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

License:Open Source License

/**
 * This method checks, whether a given value already exist in an entry's luhmann-tag, or
 * in any of the entry's sub-entries luhmann-tags. We need this to prevent infinite loops
 * when displaying the sub-entries. An entry A may contain an entry B as sub-entry, but
 * entry B or any of entry B's sub-entries may not contain entry A!
 * /*from w ww .ja va 2  s .c om*/
 * @param entry (the entry which luhmann-tag we want to check)
 * @param checkvalue (the entry which may not part of entry's luhmann-tag)
 * @param found (whether the checkvalue already exists in the enry's luhmann-tag or not, initially should be "false")
 * @return {@code true} when the checkvalue exists, false otherwise. actually the "found"-value is returned
 */
private boolean existsInLuhmann(int entry, int checkvalue, boolean found) {
    // if we found anything by now, return true
    if (found)
        return true;
    // get the entry
    Element zettel = retrieveElement(zknFile, entry);
    // if it exists, go on
    if (zettel != null && zettel.getChild(ELEMENT_TRAILS) != null) {
        // get the text from the luhmann-numbers
        String lnr = zettel.getChild(ELEMENT_TRAILS).getText();
        // if we have any luhmann-numbers, go on...
        if (!lnr.isEmpty()) {
            // copy all values to an array
            String[] lnrs = lnr.split(",");
            // go throughh array of current luhmann-numbers
            for (String exist : lnrs) {
                // check whether luhmann-value exists, by re-calling this method
                // again and go through a recusrive loop
                found = existsInLuhmann(Integer.parseInt(exist), checkvalue, found);
                // if we have found a check-value, return true
                if (found)
                    return true;
                // else check whether the current entry equals the checkvalue
                found = (entry == checkvalue);
            }
        }
        // else check whether the current entry equals the checkvalue
        else {
            found = (entry == checkvalue);
        }
    }
    // return result
    return found;
}

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

License:Open Source License

/**
 * This method deletes an author by removing the content from the element
 * inside of the author xml datafile. the element itself is kept and left
 * empty. this ensures that the order and numbering of an author never
 * changes. Since the zettelkasten datafile stores the index-numbers of the authors
 * a changing in the position/order/numbering of the author datafile would lead
 * to corrupted author associations in the zettelkasten data file
 * //from ww w .  j a v a 2  s .  c o m
 * @param pos position of author which should be deleted
 */
public void deleteAuthor(int pos) {
    // check whether author exists...
    if (!getAuthor(pos).isEmpty()) {
        // ...delete its content
        // therefore, get the author's index-number as string (for comparison below)
        String nr = String.valueOf(pos);
        // create new string buffer
        StringBuilder newau = new StringBuilder("");
        // and delete this index-number from all entries
        for (int cnt = 1; cnt <= getCount(ZKNCOUNT); cnt++) {
            // get each element
            Element zettel = retrieveElement(zknFile, cnt);
            // get the author-index-numbers
            String[] aunr = zettel.getChild(ELEMENT_AUTHOR).getText().split(",");
            // reset buffer
            newau.setLength(0);
            for (String aunr1 : aunr) {
                // if deleted value does not equal author-value, add it
                if (!aunr1.equals(nr)) {
                    // append index-number
                    newau.append(aunr1);
                    // and a seperator-comma
                    newau.append(",");
                }
            }
            // shorten the stringbuffer by one char, since we have a
            // superfluous comma char (see for-loop above)
            if (newau.length() > 0)
                newau.setLength(newau.length() - 1);
            // now set the new author-index-numbers to the zettel
            zettel.getChild(ELEMENT_AUTHOR).setText(newau.toString());
        }
        // we don't want to remove the element itself, because this would lead
        // to changing index-numbers/element-position within the document. however,
        // an author should ever keep the same index-number. rather, we could fill
        // this "empty space" with new authors
        Element author = retrieveElement(authorFile, pos);
        // if we have an author, go on...
        if (author != null) {
            // clear text
            author.setText("");
            // and reset attributes
            author.setAttribute(ATTRIBUTE_FREQUENCIES, "0");
            author.setAttribute(ATTRIBUTE_AUTHOR_ID, "");
            author.setAttribute(ATTRIBUTE_AUTHOR_TIMESTAMP, "");
            // and reset bibkey
            author.setAttribute(ATTRIBUTE_AUTHOR_BIBKEY, "");
        }
        // and change modified state
        setModified(true);
    }
}

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

License:Open Source License

/**
 * Checks whether an entry at the given {@code pos} is empty (thus deleted) or not.
 * @param pos the entry-number of that entry which has to be checked
 * @return {@code true} when the entry with the number {@code pos} is empty, false otherwise
 *///from  w  w  w. j  a  v  a 2 s  .  c  om
public boolean isEmpty(int pos) {
    // retrieve the entry
    Element entry = retrieveElement(zknFile, pos);
    // if no element exists, return false
    if (null == entry)
        return true;
    // else return whether content available or not
    return entry.getChild(ELEMENT_CONTENT).getText().isEmpty();
}