List of usage examples for org.jdom2 Element getAttributeValue
public String getAttributeValue(final String attname)
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.
From source file:de.danielluedecke.zettelkasten.database.Daten.java
License:Open Source License
/** * This method returns the author-index-number of that author-value, that contains the * bibkey (i.e. the "bibkey"-attribute) given in {@code bibkey}. * //from www .ja va 2 s. c o m * @param bibkey the bibkey which position has to be found * @return the author-index-number (i.e. author-position in the authorFile.xml) of that author * which bibkey-attribute matches (case-sensitive!) the parameter {@code bibkey}, or {@code -1} if * no author with that bibkey-value was found. */ public int getBibkeyPosition(String bibkey) { // check for valid value if (null == bibkey || bibkey.trim().isEmpty()) return -1; // create a list of all author elements from the author xml file try { List<?> authorList = authorFile.getRootElement().getContent(); // and an iterator for the loop below Iterator<?> iterator = authorList.iterator(); // counter for the return value if a found author matches the parameter int cnt = 1; // iterate all author values while (iterator.hasNext()) { Element author = (Element) iterator.next(); // retrieve bibkey-attribute. since this attribute is optional, // "bk" also might be null! String bk = author.getAttributeValue(ATTRIBUTE_AUTHOR_BIBKEY); // if bibkey-attribute matches the parameter string, return the position if (bk != null && bk.equals(bibkey)) { return cnt; } // else increase counter cnt++; } // if no bibkey was found, return -1 return -1; } catch (IllegalStateException e) { Constants.zknlogger.log(Level.WARNING, e.getLocalizedMessage()); return -1; } }
From source file:de.danielluedecke.zettelkasten.database.Daten.java
License:Open Source License
/** * This method adds a new keyword item to the keyword xml datafile * //w w w .j a v a2s . c o m * @param kw the keyword which should be added * @param freq the new frequency of the keyword, or - if keyword already exists, e.g. in case * of merging entries or adding existing keywords to an entry - the increasement-step of the * frequency-occurences of existing keywords. use "1" if a keyword is simply added to an entry, so * in case the keyword already exists, its frequency is increased by 1. * @return position of the recently added keyword, or -1 if keyword could not be added */ public int addKeyword(String kw, int freq) { // check for valid value if (null == kw || kw.isEmpty()) { return -1; } // trim leading and trailing spaces kw = kw.trim(); // if keyeord is empty, return if (kw.isEmpty()) { return -1; } // check whether author already exists int pos = getKeywordPosition(kw, false); // if keyword already exists, just increase counter if (pos != -1) { try { // retrieve existing author Element keyw = retrieveElement(keywordFile, pos); // get the count-value, which indicates the frequency of occurences of this // keywords in the whole data file int f = Integer.parseInt(keyw.getAttributeValue(ATTRIBUTE_FREQUENCIES)); // increase frequency by 1 // change timestamp attribute updateKeywordTimestampAndID(keyw, f + freq, Tools.getTimeStampWithMilliseconds(), null); // change modified state setModified(true); // and return keyword index-number return pos; } catch (IllegalNameException ex) { Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage()); return -1; } catch (IllegalDataException ex) { Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage()); return -1; } } // check whether we have any empty elements in between where we can insert the keyword int emptypos = retrieveFirstEmptyElement(keywordFile); // if we have any empty elements, go on here if (emptypos != -1) { try { // retrieve empty element Element k = retrieveElement(keywordFile, emptypos); // set keyword string as new value k.setText(kw); // set frequency of occurences to 1 // set timestamp attribute // set ID attribute // but first, check the length of "kw", because we want max. 5 first chars of kw // in keyword id String kwid; try { kwid = kw.substring(0, 5); } catch (IndexOutOfBoundsException ex) { kwid = kw; } updateKeywordTimestampAndID(k, freq, Tools.getTimeStampWithMilliseconds(), String.valueOf(emptypos) + kwid + Tools.getTimeStampWithMilliseconds()); // change list-up-to-date-state setKeywordlistUpToDate(false); // change modified state setModified(true); // return the empty-position, which is now filled with the new keyword-value return emptypos; } catch (IllegalNameException ex) { Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage()); return -1; } catch (IllegalDataException ex) { Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage()); return -1; } } // get the root element of the keyword xml datafile else { try { Element kwFile = keywordFile.getRootElement(); // create a new keyword element Element newKeyword = new Element(ELEMENT_ENTRY); // add the new keyword element to the keyword datafile try { kwFile.addContent(newKeyword); // and finally add the parameter (new keyword string) to the recently created // keyword element newKeyword.addContent(kw); // set frequency of occurences to 1 // set timestamp attribute // set ID attribute // but first, check the length of "kw", because we want max. 5 first chars of kw // in keyword id String kwid; try { kwid = kw.substring(0, 5); } catch (IndexOutOfBoundsException ex) { kwid = kw; } updateKeywordTimestampAndID(newKeyword, freq, Tools.getTimeStampWithMilliseconds(), String.valueOf(keywordFile.getRootElement().getContent().size()) + kwid + Tools.getTimeStampWithMilliseconds()); // change list-up-to-date-state setKeywordlistUpToDate(false); // change modified state setModified(true); } catch (IllegalAddException e) { // do nothing here Constants.zknlogger.log(Level.WARNING, e.getLocalizedMessage()); } catch (IllegalNameException ex) { Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage()); } catch (IllegalDataException ex) { Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage()); } // return the new size of the keyword file, i.e. the keyword position of // the recently added keyword entry // // get a list with all entry-elements of the keyword data List<?> keywordList = keywordFile.getRootElement().getContent(); // and return the size of this list return keywordList.size(); } catch (IllegalStateException e) { Constants.zknlogger.log(Level.WARNING, e.getLocalizedMessage()); return -1; } } }
From source file:de.danielluedecke.zettelkasten.database.Daten.java
License:Open Source License
/** * This method adds a new author item to the author xml datafile * @param auth the author which should be added * @param freq the new frequency of the author, or - if author already exists, e.g. in case * of merging entries or adding existing authors to an entry - the increasement-step of the * frequency-occurences of existing authors. use "1" if an author is simply added to an entry, so * in case the author already exists, its frequency is increased by 1. * @return position of the recently added author, or -1 if author could not be added *//* w ww. j av a2s . co m*/ public int addAuthor(String auth, int freq) { // trim leading and trailing spaces auth = auth.trim(); // if author is empty, return if (auth.isEmpty()) return -1; // check whether author already exists int pos = getAuthorPosition(auth); // if author already exists, just increase counter if (pos != -1) { try { // retrieve existing author Element au = retrieveElement(authorFile, pos); // get the count-value, which indicates the frequency of occurences of this // author in the whole data file int f = Integer.parseInt(au.getAttributeValue(ATTRIBUTE_FREQUENCIES)); // increase frequency of occurences // change timestamp attribute updateAuthorTimestampAndID(au, f + freq, Tools.getTimeStampWithMilliseconds(), null); // change modified state setModified(true); // and return author index-number return pos; } catch (IllegalNameException ex) { Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage()); return -1; } catch (IllegalDataException ex) { Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage()); return -1; } } // check whether we have any empty elements in between where we can insert the author int emptypos = retrieveFirstEmptyElement(authorFile); // if we have any empty elements, go on here if (emptypos != -1) { try { // retrieve empty element Element au = retrieveElement(authorFile, emptypos); // set author string as new value au.setText(auth); // set frequency of occurences to 1 // set timestamp attribute // set ID attribute // but first, check the length of "auth", because we want max. 5 first chars of auth // in author id String auid; try { auid = auth.substring(0, 5); } catch (IndexOutOfBoundsException ex) { auid = auth; } updateAuthorTimestampAndID(au, freq, Tools.getTimeStampWithMilliseconds(), String.valueOf(emptypos) + auid + Tools.getTimeStampWithMilliseconds()); // change list-up-to-date-state setAuthorlistUpToDate(false); // change modified state setModified(true); // return the empty-position, which is now filled with the new author-value return emptypos; } catch (IllegalNameException ex) { Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage()); return -1; } catch (IllegalDataException ex) { Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage()); return -1; } } // get the root element of the author xml datafile else try { // get the root element of the author xml datafile Element authFile = authorFile.getRootElement(); // create a new author element Element newAuthor = new Element(ELEMENT_ENTRY); // add the new author element to the author datafile try { // add the new author element to the author datafile authFile.addContent(newAuthor); // and finally add the parameter (new author string) to the recently created // author element newAuthor.addContent(auth); // set frequency of occurences to 1 // set timestamp attribute // set ID attribute // but first, check the length of "auth", because we want max. 5 first chars of auth // in author id String auid; try { auid = auth.substring(0, 5); } catch (IndexOutOfBoundsException ex) { auid = auth; } updateAuthorTimestampAndID(newAuthor, freq, Tools.getTimeStampWithMilliseconds(), String.valueOf(authorFile.getRootElement().getContent().size()) + auid + Tools.getTimeStampWithMilliseconds()); // change list-up-to-date-state setAuthorlistUpToDate(false); // change modified state setModified(true); } catch (IllegalAddException ex) { Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage()); } catch (IllegalNameException ex) { Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage()); } catch (IllegalDataException ex) { Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage()); } // return the new size of the author file, i.e. the author position of // the recently added author entry // get a list with all entry-elements of the author data List<?> authorList = authorFile.getRootElement().getContent(); // and return the size of this list return authorList.size(); } catch (IllegalStateException e) { Constants.zknlogger.log(Level.WARNING, e.getLocalizedMessage()); return -1; } }
From source file:de.danielluedecke.zettelkasten.database.Daten.java
License:Open Source License
/** * This method checks whether an entry with the given index number {@code pos} * was automatically created from a bibtex file. if so, the XML element * contains an attribute {@code fromBibTex} with a value "1". * * @param pos the entry's index-number. The position {@code pos} is a value from <b>1</b> to * {@link #getCount(int) getCount(ZKNCOUNT)} * @return {@code true} when the entry was automatically created from a bibtex file, * {@code false} otherwise.//from ww w . j a v a2 s .c o m */ public boolean isContentFromBibTex(int pos) { // retrieve requested entry Element zettel = retrieveElement(zknFile, pos); // if entry does not exist, leave if (null == zettel) return false; // retrieve indicator, which is stored in an attribute String isFromBibTex = zettel.getAttributeValue("fromBibTex"); // if attribute exists and its value equals 1, we know that this // entry was created from a bibtex file return (isFromBibTex != null && isFromBibTex.equals("1")); }
From source file:de.danielluedecke.zettelkasten.database.Daten.java
License:Open Source License
/** * This method retrieves the rating-attribute of entries and return the current rating * of entry {@code nr} as float-value.<br><br> * Rating can be a value from 0 to 5, including decimal place. * * @param nr the number of the entry which rating-value is requested. <b>Caution!</b> The parameter * {@code nr} has to be value from <i>1 to (size of {@link #getCount(int) getCount(ZKNCOUNT)})</i> - in contrary * to usual array handling where the range is from 0 to (size-1). * @return the rating of the entry as float-value, or {@code 0} (zero) if no rating exists. *//*from www .jav a 2 s .co m*/ public float getZettelRating(int nr) { // check for valid parameter. If number-parameter is out of // bound, return 0 if (nr < 1 || nr > getCount(Daten.ZKNCOUNT)) return 0; // get entry Element entry = retrieveZettel(nr); // check for value return value if (entry != null) { // retrieve rating-attribute String rating = entry.getAttributeValue(ATTRIBUTE_RATING); // check whether rating-attribute exists if (rating != null && !rating.isEmpty()) { try { // try to convert value into float-variable and return that result float rateval = Float.parseFloat(rating); // and round to retrieve only one decimal place return (float) ((float) Math.round(rateval * 10) / 10.0); } 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 }); } } } // nothing found, return 0 return 0; }
From source file:de.danielluedecke.zettelkasten.database.Daten.java
License:Open Source License
/** * /* w ww . ja v a2s. c om*/ * @param nr * @return */ public int getZettelRatingCount(int nr) { // check for valid parameter. If number-parameter is out of // bound, return 0 if (nr < 1 || nr > getCount(Daten.ZKNCOUNT)) return 0; // get entry Element entry = retrieveZettel(nr); // check for value return value if (entry != null) { // retrieve rating-attribute String ratingcount = entry.getAttributeValue(ATTRIBUTE_RATINGCOUNT); // check whether rating-attribute exists if (ratingcount != null && !ratingcount.isEmpty()) { try { // try to convert value into float-variable and return that result return Integer.parseInt(ratingcount); } catch (NumberFormatException ex) { // log error Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage()); Constants.zknlogger.log(Level.WARNING, "Could not parse rating-count value of entry {0}. Atribute-value is \"{1}\".", new Object[] { String.valueOf(nr), ratingcount }); } } } // nothing found, return 0 return 0; }
From source file:de.danielluedecke.zettelkasten.database.Daten.java
License:Open Source License
/** * * @param nr/*from w w w.j a v a 2 s.com*/ * @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
/** * This method returns the timestamp of the entry "entry". the timestamp is returned in a string-array. * the first part contains the created date, the second patr the edited date * @param entry the entry-element which timestamp we want to have * @return a stringarray, the first part holding the created date, the second part the edited date - or * {@code null} if no timestamp available. *//*w ww. j a v a 2s . c o m*/ public String[] getTimestamp(Element entry) { // if no element exists, return empty array if (null == entry) return null; // get created date String created = entry.getAttributeValue(ATTRIBUTE_TIMESTAMP_CREATED); // get edited date String edited = entry.getAttributeValue(ATTRIBUTE_TIMESTAMP_EDITED); return new String[] { created, edited }; }
From source file:de.danielluedecke.zettelkasten.database.Daten.java
License:Open Source License
/** * This method returns the last modification (edited) timestamp of the entry "entry". * // w w w.java2 s. co m * @param entry the entry-element which edit-timestamp (last modification) we want to have * @return a string containig the edited date - or {@code null} if no timestamp available. */ public String getTimestampEdited(Element entry) { // if no element exists, return empty array if (null == entry) return null; // return edited date return entry.getAttributeValue(ATTRIBUTE_TIMESTAMP_EDITED); }
From source file:de.danielluedecke.zettelkasten.database.Daten.java
License:Open Source License
/** * This method returns the creation timestamp of the entry "entry". * /* w w w. ja v a 2 s. c o m*/ * @param entry the entry-element which created-timestamp we want to have * @return a string containig the creation date of that entry - or {@code null} if no timestamp available. */ public String getTimestampCreated(Element entry) { // if no element exists, return empty array if (null == entry) return null; // return edited date return entry.getAttributeValue(ATTRIBUTE_TIMESTAMP_CREATED); }