Example usage for org.jdom2 Element setAttribute

List of usage examples for org.jdom2 Element setAttribute

Introduction

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

Prototype

public Element setAttribute(final String name, final String value) 

Source Link

Document

This sets an attribute value for this element.

Usage

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

License:Open Source License

/**
 * This method updates the version-information of the loaded file to the latest version number.
 */// ww w.ja  v  a 2s  .c o  m
public void updateVersionInfo() {
    // retrieve version-element
    Element el = metainfFile.getRootElement().getChild(ELEMENT_VERSION_INFO);
    // check whether it's null or not
    if (el != null) {
        el.setAttribute("id", currentVersion);
    }
}

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

License:Open Source License

/**
 * This method sets or changes the frequency-attribute, the timestamp-attribute and the ID-attribute of
 * either author- or keyword-elements./*from  w  w  w  .j  av  a 2  s.  c o m*/
 * 
 * @param e The element, either an author-element (see {@link #authorFile}) or keyword-element (see {@link #keywordFile})
 * @param attr_f the string value of the frequencies attribute name, usually use {@link #ATTRIBUTE_FREQUENCIES} here.
 * @param attr_ts the string value of the timestamp attribute name, use either {@link #ATTRIBUTE_AUTHOR_TIMESTAMP} or {@link #ATTRIBUTE_KEYWORD_TIMESTAMP}
 * @param attr_id the string value of the ID attribute name, use either {@link #ATTRIBUTE_AUTHOR_ID} or {@link #ATTRIBUTE_KEYWORD_ID}
 * @param freq the new frequency-value of the frequency-attribute. Use {@code -1} if you don't want to change this attribute value.
 * @param ts the new timestamp as string. use {@code null} as parameter if you don't want to change the timestamp attribute.
 * @param id the new ID as string. use {@code null} as parameter if you don't want to change the ID attribute.
 */
private void updateTimestampAndID(Element e, String attr_f, String attr_ts, String attr_id, int freq, String ts,
        String id) {
    // set frequency of occurences to 1
    if (freq != -1) {
        e.setAttribute(attr_f, String.valueOf(freq));
    }
    // set timestamp attribute
    if (attr_ts != null & !attr_ts.isEmpty() && ts != null) {
        e.setAttribute(attr_ts, ts);
    }
    // set ID attribute
    if (attr_id != null & !attr_id.isEmpty() && id != null) {
        e.setAttribute(attr_id, id);
    }
}

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

License:Open Source License

/**
 * This method sets a keyword to a given position in the keyword datafile
 * could be used for overwriting/changing existing keywords.
 * <br><br>//w w  w . jav  a2s.c o  m
 * This method is only used to update a data file from an older data
 * version, see CUpdateVersion for more details...
 *
 * @param pos the position of the keyword
 * @param kw the keyword string itself
 * @param freq the frequency of the keyword
 */
public void setKeyword(int pos, String kw, int freq) {
    // create a list of all keyword elements from the keyword xml file
    try {
        // retrieve keyword
        Element keyword = retrieveElement(keywordFile, pos);
        // if a valid element was found...
        if (keyword != null) {
            try {
                // ...set the new text
                keyword.setText(kw);
                // and the frequency
                keyword.setAttribute(ATTRIBUTE_FREQUENCIES, String.valueOf(freq));
                // and change the modified state of the file
                setModified(true);
            } catch (IllegalNameException ex) {
                Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
            } catch (IllegalDataException ex) {
                Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
            }
        }
    } catch (IllegalStateException e) {
        // do nothing here
        Constants.zknlogger.log(Level.WARNING, e.getLocalizedMessage());
    }
}

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

License:Open Source License

/**
 * This method deletes a keyword by removing the content from the element
 * inside of the keyword xml datafile. the element itself is kept and left
 * empty. this ensures that the order and numbering of a keyword never
 * changes. Since the zettelkasten datafile stores the index-numbers of the keywords
 * a changing in the position/order/numbering of the keyword datafile would lead
 * to corrupted keyword associations in the zettelkasten data file
 * //from www  .jav  a  2  s. com
 * @param pos (position of keyword which should be deleted)
 */
public void deleteKeyword(int pos) {
    // check whether keyword exists...
    if (!getKeyword(pos).isEmpty()) {
        // ...delete its content
        // therefore, get the keyword's index-number as string (for comparison below)
        String nr = String.valueOf(pos);
        // create new string buffer
        StringBuilder newKw = 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 keyword-index-numbers
            String[] kws = zettel.getChild(ELEMENT_KEYWORD).getText().split(",");
            // reset buffer
            newKw.setLength(0);
            for (String kw : kws) {
                // if deleted value does not equal keyword-value, add it
                if (!kw.equals(nr)) {
                    // append index-number
                    newKw.append(kw);
                    // and a seperator-comma
                    newKw.append(",");
                }
            }
            // shorten the stringbuffer by one char, since we have a
            // superfluous comma char (see for-loop above)
            if (newKw.length() > 0) {
                newKw.setLength(newKw.length() - 1);
            }
            // now set the new keyword-index-numbers to the zettel
            zettel.getChild(ELEMENT_KEYWORD).setText(newKw.toString());
        }
        // we don't want to remove the element itself, because this would lead
        // to changing index-numbers/element-position within the document. however,
        // a keyword should ever keep the same index-number. rather, we could fill
        // this "empty space" with new keywords,
        Element keyword = retrieveElement(keywordFile, pos);
        // if we have an element, go on
        if (keyword != null) {
            // delete text
            keyword.setText("");
            // and reset attributes
            keyword.setAttribute(ATTRIBUTE_FREQUENCIES, "0");
            keyword.setAttribute(ATTRIBUTE_KEYWORD_ID, "");
            keyword.setAttribute(ATTRIBUTE_KEYWORD_TIMESTAMP, "");
        }
        // and change modified state
        setModified(true);
    }
}

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

License:Open Source License

/**
 * This method deletes an entry at thegiven position. since an entry-index-number
 * should never change to ensure that each entry always keeps its index-number,
 * we don't completely remove the element from the xml-file. rather, we simply delete
 * the content by setting empty values, so we have an "empty" element.
 * /*from   ww  w  . j  a v a2s .c om*/
 * @param pos the position of the entry which should be deleted
 * @return {@code true} if entry was successfully deleted {@code false} if it could not be deleted
 * (because it already has been deleted before, or entry-element did not exist).
 */
public boolean deleteZettel(int pos) {
    // check whether entry has already been deleted
    if (isDeleted(pos)) {
        // log error
        Constants.zknlogger.log(Level.WARNING, "Could not delete entry {0}! Entry already has been deleted!",
                String.valueOf(pos));
        return false;
    }
    // retrieve the entry-element at the given position
    Element zettel = retrieveElement(zknFile, pos);
    // if the entry-element exists...
    if (zettel != null) {
        // remove this entry from the visible order
        // therefore, the previous entry of this entry should point
        // to the next entry of this entry
        setNextZettel(getPrevZettel(pos), getNextZettel(pos));
        // and the the next entry of this entry should point
        // to the previous entry of this entry
        setPrevZettel(getNextZettel(pos), getPrevZettel(pos));
        // check whether deleted entry was first entry
        if (pos == getFirstZettel()) {
            setFirstZettel(getNextZettel(pos));
        }
        // check whether deleted entry was last entry
        if (pos == getLastZettel()) {
            setLastZettel(getPrevZettel(pos));
        }
        // change zettelcounter
        zettelPos = getNextZettel(pos);
        // check whether it's out of bounds
        if (zettelPos > getCount(ZKNCOUNT) || zettelPos == -1) {
            zettelPos = getFirstZettel();
        }
        // change author-and keyword-frequencies
        changeFrequencies(pos, -1);
        // retrieve manual links, so we can delete the backlinks from other entries.
        // each manual link from this entry to other entries creates a "backlink" from
        // other entries to this one. if we delete the manual links from this entry,
        // all backlinks to this entry are removed.
        String[] manlinks = zettel.getChild(ELEMENT_MANLINKS).getText().split(",");
        // delete manual links
        deleteManualLinks(manlinks);
        // ...delete entry's attributes
        zettel.setAttribute(ATTRIBUTE_ZETTEL_ID, "");
        zettel.setAttribute(ATTRIBUTE_RATINGCOUNT, "");
        zettel.setAttribute(ATTRIBUTE_RATING, "");
        zettel.setAttribute(ATTRIBUTE_NEXT_ZETTEL, "");
        zettel.setAttribute(ATTRIBUTE_PREV_ZETTEL, "");
        // ...delete entry's content
        zettel.getChild(ELEMENT_TITLE).setText("");
        zettel.getChild(ELEMENT_CONTENT).setText("");
        zettel.getChild(ELEMENT_AUTHOR).setText("");
        zettel.getChild(ELEMENT_KEYWORD).setText("");
        zettel.getChild(ELEMENT_MANLINKS).setText("");
        zettel.getChild(ELEMENT_REMARKS).setText("");
        zettel.getChild(ELEMENT_TRAILS).setText("");
        zettel.getChild(ELEMENT_ATTACHMENTS).removeContent();
        //            zettel.getChild(ELEMENT_LUHMANN_NUMBER).setText("");
        // remove timestamp by setting creation and last modification timestamp
        // to empty strings
        setTimestamp(zettel, "", "");
        // and change modified state
        setModified(true);
        // update title list
        setTitlelistUpToDate(false);
        // return success
        return true;
    }
    // log error
    Constants.zknlogger.log(Level.WARNING, "Could not delete entry {0}! XML-element is null!",
            String.valueOf(pos));
    return false;
}

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

License:Open Source License

/**
 * This method sets/adds an indicator to an entry so we know that his entry was automatically
 * created from a bibtex-file (annotation/abstract of a bibtex-entry). We might use this
 * to check which entries have been automatically created and can be updated, when the user
 * re-imports a bibtex-file in purpose to update the Zettelkasten-data.
 *
 * @param pos the entry's index-number. The position {@code pos} is a value from <b>1</b> to
 * {@link #getCount(int) getCount(ZKNCOUNT)}
 * @param val {@code true} if entry's content is from a bibtex-file, {@code false} otherwise
 * (typically not used, since you can use the
 * {@link #addEntry(java.lang.String, java.lang.String, java.lang.String[], java.lang.String[], java.lang.String, java.lang.String[], java.lang.String, int) addEntry()}
 * method if you want to "normally" add an entry).
 *//*w  w w . ja  va2 s  .  c o m*/
public void setContentFromBibTexRemark(int pos, boolean val) {
    // retrieve requested entry
    Element zettel = retrieveElement(zknFile, pos);
    // if entry does not exist, leave
    if (null == zettel)
        return;
    // add attribute to indicate that this entry was importet
    // from a bibtex file
    zettel.setAttribute("fromBibTex", (val) ? "1" : "0");
}

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

License:Open Source License

/**
 * This method sets an author to a given position in the author datafile
 * could be used for overwriting/changing existing authors
 *
 * @param pos the position of the author. The position {@code pos} is a value from <b>1</b> to
 * {@link #getCount(int) getCount(AUCOUNT)}
 * @param auth the author string itself//from  w  w  w  .jav a 2  s  . com
 * @param freq the frequency of the author. use <b>-1</b> when the frequency-attribute
 * should be left unchanged.
 */
private void setAuthorValue(int pos, String auth, String bibkey, int freq) {
    // retrieve author
    Element author = retrieveElement(authorFile, pos);
    // if a valid element was found...
    if (author != null) {
        // ...set the new text
        author.setText(auth);
        // and new frequency, but only if it is not -1
        if (freq != -1)
            author.setAttribute(ATTRIBUTE_FREQUENCIES, String.valueOf(freq));
        // change bibkey
        if (bibkey != null) {
            setAuthorBibKey(auth, bibkey.trim());
        }
        // and change the modified state of the file
        setModified(true);
    }
}

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
 * /* ww  w . j a  v a  2s  . 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

/**
 *
 * @param nr/* w w w. j  a  va 2  s  .c  o  m*/
 * @param rate
 * @return
 */
public boolean addZettelRating(int nr, float rate) {
    // check for valid parameter. If number-parameter is out of
    // bound, return 0
    if (nr < 1 || nr > getCount(Daten.ZKNCOUNT))
        return false;
    // get entry
    Element entry = retrieveZettel(nr);
    // check for value return value
    if (entry != null) {
        // check whether rating-attribute exists
        String rating = entry.getAttributeValue(ATTRIBUTE_RATING);
        // if attribute does not exist, create it
        if (null == rating) {
            // set rating-attribute
            entry.setAttribute(ATTRIBUTE_RATING, String.valueOf(rate));
            // set rating count value to 1
            entry.setAttribute(ATTRIBUTE_RATINGCOUNT, "1");
            // set modified state
            setModified(true);
            // title list has to be updated
            setTitlelistUpToDate(false);
            // and quit
            return true;
        } else {
            // check whether rating-count value exists
            String ratingcount = entry.getAttributeValue(ATTRIBUTE_RATINGCOUNT);
            // if attribute does not exist, we have no base to calculate the average
            // rating, so we "reset" the rating by setting the new rating as default
            if (null == ratingcount) {
                // log error
                Constants.zknlogger.log(Level.WARNING,
                        "Could not find rating-count attribute of entry {0}. The rating was reset to default value.",
                        String.valueOf(nr));
                // set rating-attribute
                entry.setAttribute(ATTRIBUTE_RATING, String.valueOf(rate));
                // set rating count value to 1
                entry.setAttribute(ATTRIBUTE_RATINGCOUNT, "1");
                // set modified state
                setModified(true);
                // title list has to be updated
                setTitlelistUpToDate(false);
                // and quit
                return true;
            }
            // now calculate new average rating
            try {
                // try to convert value into float-variable and return that result
                float ratingvalue = Float.parseFloat(rating);
                // convert rating count
                int ratingcnt = Integer.parseInt(ratingcount);
                // check for valid values, i.e. if we have already rating-values,
                // and not for instance reset values.
                if (ratingcnt > 0 && ratingvalue > 0.0) {
                    // calulate new rating
                    float newrating = (float) (((ratingvalue * ratingcnt) + rate) / (ratingcnt + 1));
                    // set back new values
                    entry.setAttribute(ATTRIBUTE_RATING, String.valueOf(newrating));
                    entry.setAttribute(ATTRIBUTE_RATINGCOUNT, String.valueOf(ratingcnt + 1));
                }
                // in case we have reset-values, set new rate as default value
                else {
                    // set rating-attribute
                    entry.setAttribute(ATTRIBUTE_RATING, String.valueOf(rate));
                    // set rating count value to 1
                    entry.setAttribute(ATTRIBUTE_RATINGCOUNT, "1");
                }
                // set modified state
                setModified(true);
                // title list has to be updated
                setTitlelistUpToDate(false);
                // return success
                return true;
            } catch (NumberFormatException ex) {
                // log error
                Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage());
                Constants.zknlogger.log(Level.WARNING,
                        "Could not parse rating value of entry {0}. Atribute-value is \"{1}\".",
                        new Object[] { String.valueOf(nr), rating });
                Constants.zknlogger.log(Level.WARNING,
                        "Could not parse rating-count value of entry {0}. Atribute-value is \"{1}\".",
                        new Object[] { String.valueOf(nr), ratingcount });
            }

        }
    }
    return false;
}

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

License:Open Source License

/**
 *
 * @param nr//from  w w w. j a v a2  s. c  o  m
 */
public void resetZettelRating(int nr) {
    // check for valid parameter. If number-parameter is out of
    // bound, return 0
    if (nr < 1 || nr > getCount(Daten.ZKNCOUNT))
        return;
    // get entry
    Element entry = retrieveZettel(nr);
    // check for value return value
    if (entry != null) {
        // reset rating-values
        entry.setAttribute(ATTRIBUTE_RATING, "0");
        entry.setAttribute(ATTRIBUTE_RATINGCOUNT, "0");
        // set modified state
        setModified(true);
        // title list has to be updated
        setTitlelistUpToDate(false);
    }
}