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 sets the bibkey-string of an author-value. the bibkey-string referres to a
 * BibTex-entry in a given BibTex-file, so the "formatted" author of the author-value saved
 * in our authorXml-file can be retrieved via a BibTex-File.<br><br>
 * This method does the work for both/* w ww. ja  va  2s .  c  om*/
 * {@link #setAuthorBibKey(java.lang.String, java.lang.String) setAuthorBibKey(String, String)}
 * and {@link #setAuthorBibKey(int, java.lang.String) setAuthorBibKey(int, String)}.
 *
 * @param pos the index number of the author which you are looking for
 * @param key the bibkey of the related BibTex-entry.
 * @return {@code true} if bibkey-attribute was successfully changed, {@code false} if an error occured
 */
private boolean setAuthorBibKeyValue(int pos, String key) {
    // retrieve the keyword element
    Element author = retrieveElement(authorFile, pos);
    // check whether element is null
    if (null == author)
        return false;
    try {
        // if everything ok, set new attribute-value
        author.setAttribute(ATTRIBUTE_AUTHOR_BIBKEY, key);
        // and change modified-state
        setModified(true);
    } catch (IllegalNameException ex) {
        Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
        return false;
    } catch (IllegalDataException ex) {
        Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
        return false;
    }
    return true;
}

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

License:Open Source License

/**
 * This methods updates the data base when updating to versiob 3.3. Each entry element
 * receives a unique ID as XML-attribute value stored in the data base. The unique ID consists
 * of entry's edited timestamp, file name and entry number.
 *///from  ww  w.  j a va 2 s .  c o m
public void db_updateZettelIDs() {
    // iterate all elements
    for (int cnt = 1; cnt <= getCount(ZKNCOUNT); cnt++) {
        // retrieve element
        Element zettel = retrieveZettel(cnt);
        // check for valid value
        // and check whether entry already has an ID
        if (zettel != null && !hasZettelID(cnt)) {
            // if not, set unique ID-attribute to entry
            // init variable
            StringBuilder id = new StringBuilder("");
            // retrieve timestamp
            String[] ts = getTimestamp(cnt);
            // check for valid entry
            if (ts != null && ts[0] != null && !ts[0].isEmpty()) {
                // append timestamp
                id.append(ts[0]);
            } else {
                // else, if entry has no create-timestamp, add manual timestamp
                id.append(Tools.getTimeStampWithMilliseconds());
            }
            id.append(String.valueOf(cnt)).append(settings.getFileName()).append(String.valueOf(cnt));
            // now add id to zettel-element
            zettel.setAttribute(ATTRIBUTE_ZETTEL_ID, id.toString());
        }
    }
    // change modified state
    setModified(true);
}

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

License:Open Source License

/**
 * This method updates the data base when updating from data base version 3.3 to 3.4.
 * Here each author and keyword element in the {@link #authorFile} and {@link #keywordFile}
 * get 2 new attributes: {@link #ATTRIBUTE_AUTHOR_TIMESTAMP} and {@link #ATTRIBUTE_AUTHOR_ID} (resp.
 * their keyword-equivalence)./*  w  w  w  .j  av a 2  s  .c  o  m*/
 */
public void db_updateAuthorAndKeywordIDs() {
    // iterate all elements
    for (int cnt = 1; cnt <= getCount(AUCOUNT); cnt++) {
        // retrieve element
        Element author = retrieveElement(authorFile, cnt);
        // check for valid value
        // and check whether element already has an ID
        if (author != null && !hasAuthorID(cnt)) {
            // if not, set unique ID-attribute to entry
            // init variable
            StringBuilder id = new StringBuilder("");
            // add manual timestamp
            id.append(Tools.getTimeStampWithMilliseconds()).append(settings.getFileName())
                    .append(String.valueOf(cnt));
            // now add id to zettel-element
            author.setAttribute(ATTRIBUTE_AUTHOR_ID, id.toString());
            // and add timestamp attribute
            author.setAttribute(ATTRIBUTE_AUTHOR_TIMESTAMP, Tools.getTimeStampWithMilliseconds());
        }
    }
    // iterate all elements
    for (int cnt = 1; cnt <= getCount(KWCOUNT); cnt++) {
        // retrieve element
        Element keyword = retrieveElement(keywordFile, cnt);
        // check for valid value
        // and check whether element already has an ID
        if (keyword != null && !hasKeywordID(cnt)) {
            // if not, set unique ID-attribute to entry
            // init variable
            StringBuilder id = new StringBuilder("");
            // add manual timestamp
            id.append(Tools.getTimeStampWithMilliseconds()).append(settings.getFileName())
                    .append(String.valueOf(cnt));
            // now add id to zettel-element
            keyword.setAttribute(ATTRIBUTE_KEYWORD_ID, id.toString());
            // and add timestamp attribute
            keyword.setAttribute(ATTRIBUTE_KEYWORD_TIMESTAMP, Tools.getTimeStampWithMilliseconds());
        }
    }
    // change modified state
    setModified(true);
}

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

License:Open Source License

/**
 * This method sets the number of the next Zettel which is positioned
 * after the Zettel with the number {@code pos}. This method is needed
 * for reordering the entries without changing their original number,
 * i.e. position in the database.<br><br>
 * Sample://from w  w w. j  a v a2  s. c  om
 * {@code setNextZettel(5,9)} sets the reference to the next entry of
 * entry 5 to number 9, i.e. entry 5 points to entry 9 as next entry.
 * 
 * @param pos The number or position of the current entry, where the reference
 * to the next entry should be set
 * @param nr the number of the next entry, where the entry {@code pos} should
 * refer to.
 */
public void setNextZettel(int pos, int nr) {
    // retrieve current zettel-element
    Element zettel = retrieveZettel(pos);
    // check for valid value
    if (zettel != null) {
        // check whether number is within bounds
        if (nr >= 1 && nr <= getCount(ZKNCOUNT) || -1 == nr /* We need -1 here for restting the value */) {
            // set attribute
            zettel.setAttribute(ATTRIBUTE_NEXT_ZETTEL, String.valueOf(nr));
        }
    }
}

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

License:Open Source License

/**
 * This method sets the number of the previous Zettel which is positioned
 * before the Zettel with the number {@code pos}. This method is needed
 * for reordering the entries without changing their original number,
 * i.e. position in the database.<br><br>
 * Sample:/*from  ww w.  j ava2  s. c  o m*/
 * {@code setPrevZettel(5,9)} sets the reference to the previous entry of
 * entry 5 to number 9, i.e. entry 5 points to entry 9 as previous entry.
 * 
 * @param pos The number or position of the current entry, where the reference
 * to the previous entry should be set
 * @param nr the number of the previous entry, where the entry {@code pos} should
 * refer to.
 */
public void setPrevZettel(int pos, int nr) {
    // retrieve current zettel-element
    Element zettel = retrieveZettel(pos);
    // check for valid value
    if (zettel != null) {
        // check whether number is within bounds
        if (nr >= 1 && nr <= getCount(ZKNCOUNT) || -1 == nr /* We need -1 here for restting the value */) {
            // set attribute
            zettel.setAttribute(ATTRIBUTE_PREV_ZETTEL, String.valueOf(nr));
        }
    }
}

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

License:Open Source License

/**
 * This method changes the timestamp attributes of an entry.
 * /* w  ww  .jav a2s.c  om*/
 * @param zettel the entry-element in the XML-database.
 * @param created the new value for the creation timestamp of an entry as string.
 * Use {@code null} if this attribute value should not be changed.
 * @param edited the new value for the last modification timestamp of an entry as string.
 * Use {@code null} if this attribute value should not be changed.
 */
public void setTimestamp(Element zettel, String created, String edited) {
    // check for valid parameter
    if (zettel != null) {
        // check for valid parameter and change created attribute
        if (created != null) {
            zettel.setAttribute(ATTRIBUTE_TIMESTAMP_CREATED, created);
        }
        // check for valid parameter and change edited attribute
        if (edited != null) {
            zettel.setAttribute(ATTRIBUTE_TIMESTAMP_EDITED, edited);
        }
        setModified(true);
    }
}

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

License:Open Source License

/**
 * This method changes the timestamp attributes of an entry.
 * /*from  w ww.  jav  a2s  .c o m*/
 * @param zettel the entry-element in the XML-database.
 * @param edited the new value for the last modification timestamp of an entry as string.
 * Use {@code null} if this attribute value should not be changed.
 */
public void setTimestampEdited(Element zettel, String edited) {
    // check for valid parameter
    if (zettel != null) {
        // check for valid parameter and change edited attribute
        if (edited != null) {
            zettel.setAttribute(ATTRIBUTE_TIMESTAMP_EDITED, edited);
        }
        setModified(true);
    }
}

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

License:Open Source License

/**
 * This method changes the timestamp attributes of an entry.
 * /*from www.  j a  v  a2 s  .co  m*/
 * @param zettel the entry-element in the XML-database.
 * @param created the new value for the creation timestamp of an entry as string.
 * Use {@code null} if this attribute value should not be changed.
 */
public void setTimestampCreated(Element zettel, String created) {
    // check for valid parameter
    if (zettel != null) {
        // check for valid parameter and change created attribute
        if (created != null) {
            zettel.setAttribute(ATTRIBUTE_TIMESTAMP_CREATED, created);
        }
        setModified(true);
    }
}

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

License:Open Source License

/**
 * This method creates a new unique ID for an entry and adds it as ID-attribute to
 * the zettel-XML-element./*from  w ww. ja v  a  2 s. com*/
 * <br><br>
 *
 * @param zettel the entry that shoud receive a new ID, as XML-element
 */
public void setZettelID(Element zettel) {
    // check for valid value
    if (zettel != null) {
        // now add id to zettel-element
        zettel.setAttribute(ATTRIBUTE_ZETTEL_ID, Tools.createZknID(settings.getFileName()));
        // change modified state
        setModified(true);
    }
}

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

License:Open Source License

/**
 * This method sets the name of the desktop with the index {@code nr}.
 * //from  ww  w  .ja  va2 s  .co m
 * @param nr the desktop-index of which we want to have the name
 * @param name the new name of the desktop
 * @return {@code true} if name was successfully set, {@code false}
 */
public boolean setDesktopName(int nr, String name) {
    // get the requested dektop-element
    Element d = getDesktopElement(nr);
    // if no dektop was found, return empty string
    if (null == d)
        return false;
    try {
        // change attribute
        d.setAttribute("name", name);
        // change modified state
        setModified(true);
    } catch (IllegalNameException ex) {
        Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
        return false;
    } catch (IllegalDataException ex) {
        Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
        return false;
    }
    return true;
}