List of usage examples for org.jdom2 Element setText
public Element setText(final String text)
From source file:de.danielluedecke.zettelkasten.database.Daten.java
License:Open Source License
/** * Ssaves the user defined path to attachments * @param path the user defined path to attachments *//* w w w . j a va 2s.com*/ public void setUserAttachmentPath(String path) { // retrieve version-element Element el = metainfFile.getRootElement().getChild(ELEMENT_ATTACHMENT_PATH); // check whether it's null or not if (null == el) { // than create an empty atachment-path and add it el = new Element(ELEMENT_ATTACHMENT_PATH); metainfFile.getRootElement().addContent(el); } // check for valid parameter if (path != null) { // set new path el.setText(path); // and change modified state setMetaModified(true); } }
From source file:de.danielluedecke.zettelkasten.database.Daten.java
License:Open Source License
/** * Ssaves the user defined path to images * @param path the user defined path to images *//* w w w . j av a 2 s . co m*/ public void setUserImagePath(String path) { // retrieve version-element Element el = metainfFile.getRootElement().getChild(ELEMENT_IMAGE_PATH); // check whether it's null or not if (null == el) { // than create an empty atachment-path and add it el = new Element(ELEMENT_IMAGE_PATH); metainfFile.getRootElement().addContent(el); } // check for valid parameter if (path != null) { // set new path el.setText(path); // and change modified state setMetaModified(true); } }
From source file:de.danielluedecke.zettelkasten.database.Daten.java
License:Open Source License
/** * This method sets the description of the zettelkasten-data, which is stored * in the metainformation-file of the zipped data-file. * /*from ww w .j ava 2s. com*/ * @param desc a string with the description of this zettelkasten * @return */ public boolean setZknDescription(String desc) { // get the element Element el = metainfFile.getRootElement().getChild(ELEMEMT_DESCRIPTION); try { // check whether element exists if (null == el) { // if element does not exist, create it el = new Element(ELEMEMT_DESCRIPTION); // and add it to the meta-xml-file metainfFile.getRootElement().addContent(el); } // finally, set the text el.setText(desc); // change modified state setMetaModified(true); } catch (IllegalAddException ex) { Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage()); return false; } catch (IllegalDataException ex) { Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage()); return false; } // return success return true; }
From source file:de.danielluedecke.zettelkasten.database.Daten.java
License:Open Source License
/** * This method duplicates an entry and inserts it at the end or the next empty place in the * data file/*w w w .ja v a2 s. c o m*/ * * @param nr the number of the entry that should be duplicated * @return */ public boolean duplicateEntry(int nr) { // first of all, we duplicate all authors and keywords frequencies from the existing entry. // therefore, we first retrieve all author-index-numbers from that entry changeFrequencies(nr, 1); // retrieve entry that should be duplicated Element oldzettel = retrieveElement(zknFile, nr); // create new zettel Element zettel = new Element(ELEMENT_ZETTEL); // check whether we have any empty elements in between where we can insert the new entry int emptypos = retrieveFirstEmptyEntry(); // if we have any empty elements... if (emptypos != -1 && settings.getInsertNewEntryAtEmpty()) { // retrieve empty element zettel = retrieveElement(zknFile, emptypos); // and remove former content, so we can add new content zettel.removeContent(); } try { setZettelID(zettel); // // add title // // create child element with title information Element t = new Element(ELEMENT_TITLE); // and add it to the zettel-element zettel.addContent(t); // set value of the child element t.setText(oldzettel.getChild(ELEMENT_TITLE).getText()); // // add content // // create child element with content information Element c = new Element(ELEMENT_CONTENT); // and add it to the zettel-element zettel.addContent(c); // set value of the content element c.setText(oldzettel.getChild(ELEMENT_CONTENT).getText()); // // add author // // create child element with author information Element a = new Element(ELEMENT_AUTHOR); // and add it to the zettel-element zettel.addContent(a); // set value of author element a.setText(oldzettel.getChild(ELEMENT_AUTHOR).getText()); // // add keywords // // create child element with keyword information Element k = new Element(ELEMENT_KEYWORD); // and add it to the zettel-element zettel.addContent(k); // store keyword index numbers k.setText(oldzettel.getChild(ELEMENT_KEYWORD).getText()); // // now comes the manual links to other entries // Element m = new Element(ELEMENT_MANLINKS); zettel.addContent(m); m.setText(""); // // add hyperlinks // // create child element with link information Element h = new Element(ELEMENT_ATTACHMENTS); // and add it to the zettel-element zettel.addContent(h); // add each hyperlink. therefor, iterate the array List<Element> links = oldzettel.getChild(ELEMENT_ATTACHMENTS).getChildren(); Iterator<Element> i = links.iterator(); while (i.hasNext()) { // create a new subchuld-element Element sublink = new Element(ELEMENT_ATTCHILD); Element le = i.next(); // and add the link-string from the array sublink.setText(le.getText()); h.addContent(sublink); } // // add remarks // // create child element with content information Element r = new Element(ELEMENT_REMARKS); // and add it to the zettel-element zettel.addContent(r); // set value of the content element r.setText(oldzettel.getChild(ELEMENT_REMARKS).getText()); // // add timestamp // // set creation timestamp, but set no text for edit timestamp // since the entry is not edited setTimestamp(zettel, Tools.getTimeStamp(), ""); // // now comes the luhmann number // Element l = new Element(ELEMENT_TRAILS); zettel.addContent(l); l.setText(oldzettel.getChild(ELEMENT_TRAILS).getText()); // // complete datafile // // if we have any empty elements, go on here if (emptypos != -1 && settings.getInsertNewEntryAtEmpty()) { // return the empty-position, which is now filled with the new author-value zettelPos = emptypos; } else { // finally, add the whole element to the data file zknFile.getRootElement().addContent(zettel); // set the zettel-position to the new entry zettelPos = getCount(ZKNCOUNT); } // duplicate this entry into the correct entry order // by changing the prev/nex references (or pointers) of the entries. changeZettelPointer(zettelPos, nr); // titles have to be updated. setTitlelistUpToDate(false); // set modified state setModified(true); } catch (IllegalAddException 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 method adds a new keyword item to the keyword xml datafile * //from www. j a v a 2 s .com * @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 several keywords to the keyword xml datafile, without assigning them * to a certain entry//from w w w . java 2 s. c o m * * @param kws the keywords which should be added */ public void addKeywordsToDatabase(String[] kws) { // if keyeord is empty, return if (null == kws || 0 == kws.length) { return; } // iterate all keywords for (String kw : kws) { // check whether keyword already exists int pos = getKeywordPosition(kw, false); // no, we have a new keyword. so add it... if (-1 == pos) { // 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 0 // 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, 0, Tools.getTimeStampWithMilliseconds(), String.valueOf(emptypos) + kwid + Tools.getTimeStampWithMilliseconds()); // change list-up-to-date-state setKeywordlistUpToDate(false); // change modified state setModified(true); } catch (IllegalNameException ex) { Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage()); } catch (IllegalDataException ex) { Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage()); } } // get the root element of the keyword xml datafile else { 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 0 // 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, 0, 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()); } } } } }
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 * //w w w . ja v a 2 s.com * @param pos the position of the keyword * @param kw the keyword string itself */ public void setKeyword(int pos, String kw) { // retrieve and store the old keyword that should be replaced by the new one. // we need this to check whether we also have to replace synonyms... String oldkeyword = getKeyword(pos); // 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) { // ...set the new text keyword.setText(kw); // find the oldkeyword in the synonymsfile... int synpos = synonymsObj.findSynonym(oldkeyword, true); // if we found a synonym, ask the user whether it also should be replaced if (synpos != -1) { // create a JOptionPane with yes/no/cancel options int option = JOptionPane.showConfirmDialog(zknframe.getFrame(), zknframe.getResourceMap().getString("replaceKeywordsInSynonymsMsg", oldkeyword, kw), zknframe.getResourceMap().getString("replaceKeywordsInSynonymsTitle"), JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE); // when the user applied to yes, we also change the synonym if (JOptionPane.YES_OPTION == option) { // get the synonymline String[] synline = synonymsObj.getSynonymLine(synpos, true); // go through all synonyms... if (synline != null && synline.length > 1) { for (int cnt = 0; cnt < synline.length; cnt++) { // ...and check whether the synonym-word equals the old keyword. if yes, replace // the synonym at that position with the new keyword if (synline[cnt].equals(oldkeyword)) { synline[cnt] = kw; } } } // finally, set back the synonyms. synonymsObj.setSynonymLine(synpos, synline); } } // and change the modified state of the file setModified(true); } } 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 sets a keyword to a given position in the keyword datafile * could be used for overwriting/changing existing keywords. * <br><br>// w w w . ja va 2 s . 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 w ww.ja va 2 s . c om*/ * @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 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 *//*from w ww.ja v a 2s .c o 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; } }