List of usage examples for org.jdom2 Element getChild
public Element getChild(final String cname)
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//from www. j av a 2 s . com * * @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 searches the keyword-xml-file or author-xml-file for empty elements and returns the * number of the first empty element, if any. empty elements occur, when the user * deletes a keyword or author. in this case, to keep the permanent index-number of the other * keywords and authors, the keyword-/author-element is not completely removed, but only the text is * removed.//from www . j a v a 2 s . c o m * * @param the xml-document (either <i>keywordFile</i> or <i>authorFile</i>) * @return the number of the first empty element, or -1 if no empty element was found */ private int retrieveFirstEmptyEntry() { // create a list of all elements from the given xml file try { List<?> elementList = zknFile.getRootElement().getContent(); // and an iterator for the loop below Iterator<?> iterator = elementList.iterator(); // counter for the return value if a found author matches the parameter int cnt = 1; while (iterator.hasNext()) { Element el = (Element) iterator.next(); // if author matches the parameter string, return the position if (el.getChild(ELEMENT_TITLE).getText().isEmpty() && el.getChild(ELEMENT_CONTENT).getText().isEmpty() && el.getChild(ELEMENT_AUTHOR).getText().isEmpty()) { return cnt; } // else increase counter cnt++; } // if no author 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 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 v a 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 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 a 2 s . com * @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 adds a keyword, which is passed as string-parameter, to an existing * entry. the method first checks whether the entry {@code nr} already contains the keyword * {@code kw}. if not, the index-number of that keyword is retrieved and the new index-numbers * are added to the entry.//from w w w.ja v a 2s . c o m * * @param kw the keyword which should be added to the entry * @param nr the index-number of the entry where the keyword should be added to * @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. */ public void addKeywordToEntry(String kw, int nr, int freq) { // trim leading and trailing spaces kw = kw.trim(); // if keyword is empty, return if (kw.isEmpty()) { return; } // first check whether keyword already exists // return false, if keyword exists and we don't add it if (existsInKeywords(kw, nr, false)) { return; } // retrieve the current entry Element el = retrieveElement(zknFile, nr); // if we don't have a valid element, return false if (null == el || null == el.getChild(ELEMENT_KEYWORD)) { return; } // create empty stringbuffer StringBuilder sb = new StringBuilder(""); // append keywords sb.append(el.getChild(ELEMENT_KEYWORD).getText()); // append new separator, but only if we already have keywords if (sb.length() > 0) { sb.append(","); } // add it to the keyword-list int pos = addKeyword(kw, freq); // only proceed when success if (pos != -1) { // append index-number of the keyword which should be added sb.append(String.valueOf(pos)); // set the new keyword-index-numbers el.getChild(ELEMENT_KEYWORD).setText(sb.toString()); // finally, change modified state setModified(true); } }
From source file:de.danielluedecke.zettelkasten.database.Daten.java
License:Open Source License
/** * This method adds several keywords, that are passed as string-array, to an existing * entry. the method first checks whether the entry {@code nr} already contains one of the * keywords given in the array {@code kws}. * if not, the index-number of that keyword is retrieved and the new index-numbers * are added to the entry.//w w w.j av a 2 s . c o m * * @param kws a string-array with keywords that should be added to the entry * @param nr the index-number of the entry where the keywords should be added to * @param freq the new frequency of the keywords, or - if any one of the keywords inside the array * {@code kws} 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 */ public String[] addKeywordsToEntry(String[] kws, int nr, int freq) { // check for valid parameter. if not existing, return if (null == kws || kws.length < 1) return null; // clean keywords, i.e. retrieve only those keywords that are new // to the entry.... kws = retrieveNonexistingKeywords(kws, nr, false); // if we have any new keywords, go on here. if (kws != null && kws.length > 0) { // retrieve the current entry Element el = retrieveElement(zknFile, nr); // if we don't have a valid element, return false if (null == el || null == el.getChild(ELEMENT_KEYWORD)) return null; // create empty stringbuffer StringBuilder sb = new StringBuilder(""); // append keywords sb.append(el.getChild(ELEMENT_KEYWORD).getText()); // append new separator, but only if we already have keywords if (sb.length() > 0) sb.append(","); // go through all keywords... for (String kw : kws) { // trim leading and trailing spaces kw = kw.trim(); // if keyword is not empty and does not exist, go on if (!kw.isEmpty() && !existsInKeywords(kw, nr, false)) { // add it to the keyword-list int pos = addKeyword(kw, freq); // append index-number of the keyword which should be added if (pos != -1) sb.append(String.valueOf(pos)).append(","); } } // delete last comma if (sb.length() > 1) sb.setLength(sb.length() - 1); // set the new keyword-index-numbers el.getChild(ELEMENT_KEYWORD).setText(sb.toString()); // finally, change modified state setModified(true); } return kws; }
From source file:de.danielluedecke.zettelkasten.database.Daten.java
License:Open Source License
/** * This method adds an author, which is passed as string-parameter, to an existing * entry. the method first checks whether the entry "nr" already contains the author * "au". if not, the index-number of that author is retrieved and the new index-numbers * are added to the entry.// w w w . ja va 2 s . c o m * * @param au the author which should be added to the entry * @param nr the index-number of the entry where the keyword should be added to * @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. */ public void addAuthorToEntry(String au, int nr, int freq) { // trim leading and trailing spaces au = au.trim(); // if author is empty, return if (au.isEmpty()) return; // first check whether author already exists in that entry // return false, if author exists and we don't add it if (existsInAuthors(au, nr)) return; // retrieve the current entry Element el = retrieveElement(zknFile, nr); // if we don't have a valid element, return false if (null == el || null == el.getChild(ELEMENT_AUTHOR)) return; // create empty stringbuffer StringBuilder sb = new StringBuilder(""); // append author sb.append(el.getChild(ELEMENT_AUTHOR).getText()); // append new separator, but only if we already have authors if (sb.length() > 0) sb.append(","); // add it to the author-list int pos = addAuthor(au, freq); // only proceed when valid value if (pos != -1) { // append index-number of the author which should be added sb.append(String.valueOf(pos)); // set the new author-index-numbers el.getChild(ELEMENT_AUTHOR).setText(sb.toString()); // finally, change modified state setModified(true); } }
From source file:de.danielluedecke.zettelkasten.database.Daten.java
License:Open Source License
/** * This method changed an existing entry in the datafile. The needed parameters come from the JDialog * "CNewEntry.java". This dialog opens an edit-mask so the user can input the necessary information. * If everything is done, the JDialog retrieves all the information as string(-array)-variables * and simply passes these as paramaters to this method. * <br>/*from w ww . j a v a 2 s .c o m*/ * <br> * What we have to do here is to check whether the keywords or links e.g. partly exist, and if so, * find out the related index number. Keywords which until now do not already exist in the keyword * file have to be added to the keyword file and the new index number has to be addes to the * keyword-element of the entry. and so on... * * @param title the entry's title as string * @param content the entry's content as string * @param authors the entry's authors as string-array, retrieve index number and add it to the entry's author-element * @param keywords the entry's keywords as string-array. retrieve index numbers and add those to the entry's keyword-element * @param remarks the remarks as string * @param links the entry's links as string * @param timestamp the current date. in this case, add it as edit date to the timestamp * @param entrynumber the number of the entry that should be changed. * @return */ public boolean changeEntry(String title, String content, String[] authors, String[] keywords, String remarks, String[] links, String timestamp, int entrynumber) { // create a new zettel-element Element zettel = retrieveElement(zknFile, entrynumber); // create dummy element Element child; // if no entry exists, quit if (null == zettel) return false; // first of all, we remove all authors and keywords from the existing entry // we do this to update the frequency of the authors and keywords, so when adding // authors/keywords to the data-file, which already belonged to the entry, we would // increase the frequency although those authors/keywords are not new changeFrequencies(entrynumber, -1); // then, create form-images createFormImagesFromContent(content); try { // // change title // // retrieve the element child = zettel.getChild(ELEMENT_TITLE); // if child-element doesn't exist, add it to the zettel if (null == child) { // create new child element child = new Element(ELEMENT_TITLE); // and add it zettel.addContent(child); } // set value of the child element child.setText(title); // // change content // // retrieve the element child = zettel.getChild(ELEMENT_CONTENT); // if child-element doesn't exist, add it to the zettel if (null == child) { // create new child element child = new Element(ELEMENT_CONTENT); // and add it zettel.addContent(child); } // set value of the child element child.setText(content); // // change author // // retrieve the element child = zettel.getChild(ELEMENT_AUTHOR); // if child-element doesn't exist, add it to the zettel if (null == child) { // create new child element child = new Element(ELEMENT_AUTHOR); // and add it zettel.addContent(child); } // create empty string buffer which stores the index numbers // of the converted authors StringBuilder newau = new StringBuilder(""); // check whether we have authors at all if ((authors != null) && (authors.length > 0)) { // iterate the array and get the index number of each author string // if a keauthoryword does not already exist, add it to the authorfile for (String aut : authors) { // trim leading and trailing spaces aut = aut.trim(); // only proceed for this entry, if it contains a value if (!aut.isEmpty()) { // add it to the data file // and store the position of the new added author in the // variable authorPos int authorPos = addAuthor(aut, 1); // append the index number in the string buffer newau.append(String.valueOf(authorPos)); // separator for the the index numbers, since more authors // and thus more index numbers might be stored in the author element 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); } // store author index numbers child.setText(newau.toString()); // // change keywords // // retrieve the element child = zettel.getChild(ELEMENT_KEYWORD); // if child-element doesn't exist, add it to the zettel if (null == child) { // create new child element child = new Element(ELEMENT_KEYWORD); // and add it zettel.addContent(child); } // create empty string buffer which stores the index numbers // of the converted keywords StringBuilder newkw = new StringBuilder(""); // check whether we have keywords at all if ((keywords != null) && (keywords.length > 0)) { // iterate the array and get the index number of each keyword string // if a keyword does not already exist, add it to the keywordfile for (String keyw : keywords) { // trim leading and trailing spaces keyw = keyw.trim(); // only proceed for this entry, if it contains a value if (!keyw.isEmpty()) { // add it to the data file // and store the position of the new added keyword in the // variable keywordPos int keywordPos = addKeyword(keyw, 1); // append the index number in the string buffer newkw.append(String.valueOf(keywordPos)); // separator for the the index numbers, since more keywords // and thus more index numbers might be stored in the keyword element 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); } // store keyword index numbers child.setText(newkw.toString()); // // change manual links // addManualLink(entrynumber, extractManualLinksFromContent(content)); // // change hyperlinks // // retrieve the element child = zettel.getChild(ELEMENT_ATTACHMENTS); // if child-element doesn't exist, add it to the zettel if (null == child) { // create new child element child = new Element(ELEMENT_ATTACHMENTS); // and add it zettel.addContent(child); } // first, remove all existing links, since they are completely // set again child.removeChildren(ELEMENT_ATTCHILD); // add each hyperlink string // therefor, iterate the array for (String l : links) { // create a new subchuld-element Element sublink = new Element(ELEMENT_ATTCHILD); // and add the link-string from the array sublink.setText(l); child.addContent(sublink); } // // change remarks // // retrieve the element child = zettel.getChild(ELEMENT_REMARKS); // if child-element doesn't exist, add it to the zettel if (null == child) { // create new child element child = new Element(ELEMENT_REMARKS); // and add it zettel.addContent(child); } // set value of the content element child.setText(remarks); // // change timestamp // setTimestampEdited(zettel, timestamp); // // we don't need any changes on the luhmann number or for // manual links here... // // update the current zettel-position zettelPos = entrynumber; // and add the new position to the history... addToHistory(); // 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 follower- or sub-entry index-number to an entry. Followers', or * sub-entries', index-numbers are stored in the luhmann-tag. * <br><br>//from w w w . j a v a 2 s. c o m * It is similar to a typical tree: we have one "parent"-entry and several child-entries * (sub-entries or followers). each of these child-elements can have their own child-elements again * (whereby the child-element itself is then again understood as "parent"-entry). * <br><br> * So, the Luhmann-numbers of an entry only have one subordinated level of sub-entries. the tree- * structure comes from those sub-entries, that might have their own sub-entries again. * * @param entry the entry where the related insert-entry-index-number should be added to * @param addvalue the index-number of the inserted entry * @return {@code true} if everything was ok, false if the addvalue already existed or if the entry * indicated by "addvalue" itself already contains the entry "entry". in this case, we would * have an infinitive loop, with entry A having a sub-entry B, and B having a sub-entry A again * and so on... */ public boolean addLuhmannNumber(int entry, int addvalue) { // check whether entry and addvalue are identical if (entry == addvalue) return false; // get the entry where the luhmann-number should be added to Element zettel = retrieveElement(zknFile, entry); // get the entry where the luhmann-number should be added to Element tobeadded = retrieveElement(zknFile, addvalue); // if entry does not exist, leave if (null == zettel || null == zettel.getChild(ELEMENT_TRAILS)) return false; // if entry does not exist, leave if (null == tobeadded) 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()) { // copy all values to an array String[] lnrs = lnr.split(","); // go throughh array of current luhmann-numbers for (String exist : lnrs) { try { // if addvalue exist, return false if (Integer.parseInt(exist) == addvalue) return false; } catch (NumberFormatException ex) { Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage()); } } } // 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... lnr = 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 (!lnr.isEmpty() && existsInLuhmann(addvalue, entry, false)) return false; // get the luhmann-numbers of that entry StringBuilder sb = new StringBuilder(zettel.getChild(ELEMENT_TRAILS).getText()); // append separator comma, but only if we already have values if (sb.length() > 0) sb.append(","); // append the addvalue sb.append(String.valueOf(addvalue)); /* // the the string buffer contains at least two values, we want to sort them if (sb.indexOf(",")!=-1) { // copy all values of the buffer to an string array String[] dummy = sb.toString().split(","); // create integer array, because when we sort a string-array, // the value "12" would be smaller than "5". int[] intdummy = new int[dummy.length]; // iterate array for (int cnt=0; cnt<intdummy.length; cnt++) { try { // convert all strings to integer intdummy[cnt] = Integer.parseInt(dummy[cnt]); } catch (NumberFormatException ex) { CConstants.zknlogger.log(Level.WARNING,ex.getLocalizedMessage()); } } // sort the array if (intdummy!=null && intdummy.length>0) Arrays.sort(intdummy); // reset the string buffer sb.setLength(0); // iterate the sorted array for (int cnt=0; cnt<intdummy.length; cnt++) { // and append all values to the string buffer sb.append(String.valueOf(intdummy[cnt])); sb.append(","); } // finallay, remove the last "," 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 true; }
From source file:de.danielluedecke.zettelkasten.database.Daten.java
License:Open Source License
/** * This method adds a manual link to an entry. It is called * from {@link #addManualLink(int, int) addManualLink}. * // w w w . j a v a2 s . co m * @param entry the entry where the referred entry-number should be added to * @param addvalue the index-number of the referred entry * @return {@code true} if everything was ok, false if the addvalue already existed or other errors occured. */ private boolean addManLink(int entry, int addvalue) { // check whether entry and addvalue are identical if (entry == addvalue) return false; // 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_MANLINKS)) return false; // get the manual links of that entry String lnr = zettel.getChild(ELEMENT_MANLINKS).getText(); // check whether the addvalue already exists in that entry if (!lnr.isEmpty()) { // copy all values to an array String[] lnrs = lnr.split(","); // go throughh array of current luhmann-numbers for (String exist : lnrs) { try { // if addvalue exist, return false if (Integer.parseInt(exist) == addvalue) return false; } catch (NumberFormatException ex) { Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage()); } } } // get the luhmann-numbers of that entry StringBuilder sb = new StringBuilder(zettel.getChild(ELEMENT_MANLINKS).getText()); // append separator comma, but only if we already have values if (sb.length() > 0) sb.append(","); // append the addvalue sb.append(String.valueOf(addvalue)); // the the string buffer contains at least two values, we want to sort them if (sb.indexOf(",") != -1) { // copy all values of the buffer to an string array String[] dummy = sb.toString().split(","); // create integer array, because when we sort a string-array, // the value "12" would be smaller than "5". int[] intdummy = new int[dummy.length]; // iterate array for (int cnt = 0; cnt < intdummy.length; cnt++) { try { // convert all strings to integer intdummy[cnt] = Integer.parseInt(dummy[cnt]); } catch (NumberFormatException ex) { Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage()); } } // sort the array if (intdummy.length > 0) Arrays.sort(intdummy); // reset the string buffer sb.setLength(0); // iterate the sorted array for (int cnt = 0; cnt < intdummy.length; cnt++) { // and append all values to the string buffer sb.append(String.valueOf(intdummy[cnt])); sb.append(","); } // finallay, remove the last "," if (sb.length() > 1) sb.setLength(sb.length() - 1); } // and set the new string to the manlinks-tag zettel.getChild(ELEMENT_MANLINKS).setText(sb.toString()); // addvalue was successfully added setModified(true); return true; }