Java 类org.jdom2.IllegalDataException 实例源码

项目:Zettelkasten    文件:DesktopData.java   
/**
 * This method sets the name of the desktop with the index {@code nr}.
 *
 * @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 | IllegalDataException ex) {
        Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
        return false;
    }
    return true;
}
项目:Zettelkasten    文件:DesktopData.java   
/**
 * This method renames the bullet which is given through the path
 * {@code tp}.
 *
 * @param timestamp the timestamp of the bullet that should be renamed
 * @param newName the new name of the bullet
 * @return {@code true} is renaming was successful, {@code false} if an
 * error occured.
 */
public boolean renameBullet(String timestamp, String newName) {
    // retrieve selected bullet element
    Element b = findEntryElementFromTimestamp(getCurrentDesktopElement(), timestamp);
    if (b != null) {
        try {
            // change name
            b.setAttribute("name", newName);
            // change modified state
            setModified(true);
        } catch (IllegalNameException | IllegalDataException ex) {
            Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
            return false;
        }
    }
    return true;
}
项目:Zettelkasten    文件:DesktopData.java   
/**
 * Pastes the bullet-point {@code clipbullet} into the xml-document. The
 * Element {@code clipbullet} must be retrieved by
 * {@link #copyBulletToClip(java.util.LinkedList) copyBulletToClip(java.util.LinkedList)}.
 *
 * @param timestamp the timestamp of the bullet that should be inserted
 * @param isRoot {@code true} when the drop-source is the root-element of
 * the jTree, {@code false} when the drop-source is any other parent-element
 */
public void pasteBulletFromClip(String timestamp, boolean isRoot) {
    // find the bulletgroup that is described in the treepath
    Element parentb = (isRoot) ? getCurrentDesktopElement() : findEntryElementFromTimestamp(getCurrentDesktopElement(), timestamp);
    // if we have a valid bullet-group, add the new bullet to the xml-file
    if (parentb != null && clipbullet != null) {
        // create a new element
        Element b = new Element(ELEMENT_BULLET);
        try {
            // set the name of the copied bullet-value
            b.setAttribute("name", clipbullet.getAttributeValue("name"));
            // and use copied timestamp
            b.setAttribute(ATTR_TIMESTAMP, clipbullet.getAttributeValue(ATTR_TIMESTAMP));
            // clone the content from the clipboard-bullet to the new element. we
            // have to clone the content, since the element "clipbullet" still might
            // be attached to the document, when the bullet is just copied, not cut.
            b.addContent(clipbullet.cloneContent());
            // add new bullet to the bulletgroup
            parentb.addContent(b);
            // change modified state
            setModified(true);
        } catch (IllegalNameException | IllegalDataException | IllegalAddException ex) {
            Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage());
        }
    }
}
项目:Zettelkasten    文件:DesktopData.java   
/**
 * Sets the comment of the selected entry in the jTree in the
 * CDesktop-frame. This method traverses the xml-datafile, looking in each
 * "depth-level" for the element that is given in the linked list parameter
 * <i>tp</i>. This parameter contains the treepath to the selected element.
 *
 * @param timestamp
 * @param comment a string containing the comment
 * @return {@code true} if comment was successfully set, {@code false} if an
 * error occured.
 */
public boolean setComment(String timestamp, String comment) {
    // retrieve element that matches the timestamp
    Element e = findEntryElementFromTimestamp(getCurrentDesktopElement(), timestamp);
    // check whether an entry was found or not
    if (e != null) {
        try {
            // check whether we have a bullet-point
            if (e.getName().equals(ELEMENT_BULLET)) {
                // if we have a bullet, return the text of it's comment-child.
                e.getChild(ATTR_COMMENT).setText(comment);
            } else {
                // set comment for entry
                e.setText(comment);
            }
            // change modified state
            setModified(true);
        } catch (IllegalDataException ex) {
            Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage());
            return false;
        }
    }
    return true;
}
项目:Zettelkasten    文件:DesktopData.java   
/**
 * This method changes the content of a modified entry in the
 * {@link #modifiedEntries modifiedEntries}-Document. Modified entries are
 * entries that the user modifies on the desktop, while the original content
 * of that entry in the main database should be left unchanged.
 *
 * @param timestamp a unique timestamp which is associated to the entry that
 * is modified, so we can later easily find the related modified content.
 * @param content the modified content itself.
 * @return {@code true} if the entry was successfully changed, {@code false}
 * if an error occured.
 */
public boolean changeEntry(String timestamp, String content) {
    // retrieve entry-element that should be changed
    Element entry = retrieveModifiedEntryElementFromTimestamp(timestamp);
    // check for valid element
    if (null == entry) {
        // if we have no such element, add the modified entry
        return addModifiedEntry(timestamp, content);
    } else {
        try {
            // else change the content of that element
            entry.setText(content);
            // and change modified flag
            setModified(true);
        } catch (IllegalDataException ex) {
            Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
            return false;
        }
    }
    return true;
}
项目:Zettelkasten    文件:DesktopData.java   
/**
 * This method initialises the desktopNotes-xml-file during an
 * update-process.<br><br>
 * This method is called when updating a file with version number 3.0 or 3.1
 * to 3.2 and higher.
 */
public void initDesktopNotesUpdate() {
    // iterate all desktops
    for (int cnt = 0; cnt < getCount(); cnt++) {
        // retrieve desktopname
        String dname = getDesktopName(cnt);
        // create new desktop-element
        Element desk = new Element("desktop");
        try {
            // set name attribute
            desk.setAttribute("name", dname);
            // add notes-sub-elements
            desk.addContent(new Element("notes1"));
            desk.addContent(new Element("notes2"));
            desk.addContent(new Element("notes3"));
            // add element to desktop-notes
            desktopNotes.getRootElement().addContent(desk);
        } catch (IllegalNameException | IllegalDataException | IllegalAddException ex) {
            Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
        }
    }
}
项目:Zettelkasten    文件:Daten.java   
/**
 * This method sets the description of the zettelkasten-data, which is
 * stored in the metainformation-file of the zipped data-file.
 *
 * @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 | IllegalDataException ex) {
        Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
        return false;
    }
    // return success
    return true;
}
项目:Zettelkasten    文件:Daten.java   
/**
 * This method sets a keyword to a given position in the keyword datafile
 * could be used for overwriting/changing existing keywords.
 * <br><br>
 * 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 | IllegalDataException ex) {
                Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
            }
        }
    } catch (IllegalStateException e) {
        // do nothing here
        Constants.zknlogger.log(Level.WARNING, e.getLocalizedMessage());
    }
}
项目:Zettelkasten    文件:Daten.java   
/**
 * This method changes the remarks of a given entry with the entry-number
 * {@code pos}.
 *
 * @param pos the entry from which we want to change the remarks
 * @param remarks the new remarks-content
 * @return {@code true} if remarks have been successfully changed, false
 * otherwise
 */
public boolean setRemarks(int pos, String remarks) {
    // retrieve the element from the main xml-file
    Element el = retrieveElement(zknFile, pos);
    // if element or child element is null, return false
    if (null == el || null == el.getChild(ELEMENT_REMARKS)) {
        return false;
    }
    try {
        // else change remarks
        el.getChild(ELEMENT_REMARKS).setText(remarks);
        // change modified state
        setModified(true);
    } catch (IllegalDataException ex) {
        Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
        return false;
    }
    // and tell about success.
    return true;
}
项目:Zettelkasten    文件:Daten.java   
/**
 * 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
 * {@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 | IllegalDataException ex) {
        Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
        return false;
    }
    return true;
}
项目:Zettelkasten    文件:Daten.java   
/**
 * This method returns the title of a certain entry
 *
 * @param pos the index number of the entry which title is requested. Must
 * be a number from 1 to {@link #getCount(int) getCount(CDaten.ZKNCOUNT)}.
 * @param title
 */
public void setZettelTitle(int pos, String title) {
    // retrieve the element from the main xml-file
    Element el = retrieveElement(zknFile, pos);
    // if element or child element is null, return empty string
    if (null == el || null == el.getChild(ELEMENT_TITLE)) {
        return;
    }
    try {
        // else change title
        el.getChild(ELEMENT_TITLE).setText(title);
        // reset title-list
        setTitlelistUpToDate(false);
        // change modified state
        setModified(true);
    } catch (IllegalDataException ex) {
        Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
    }
}
项目:Zettelkasten    文件:StenoData.java   
/**
 * Adds a new pair of steno/long words to the document
 *
 * @param abbr the short, steno-abbreviation of the word
 * @param longword the long, original version of the word
 * @return {@code true} if element was successfully addes, false if {@code stenoword} already existed
 */
public boolean addElement(String abbr, String longword) {
    // check for existence
    if (exists(abbr)) {
        return false;
    }
    // if it doesn't already exist, create new element
    Element e = new Element(Daten.ELEMENT_ENTRY);
    try {
        // set id-attribute
        e.setAttribute("id", abbr);
        // set content
        e.setText(longword);
        // and add it to the document
        steno.getRootElement().addContent(e);
    }
    catch (IllegalAddException | IllegalDataException ex) {
        Constants.zknlogger.log(Level.SEVERE,ex.getLocalizedMessage());
        return false;
    }
    // return success
    return true;
}
项目:Zettelkasten    文件:Synonyms.java   
/**
 * This method sets a new synonm-line, i.e. a synonym (as index-word) with its related synonyms.
 * The new synonyms have to passed as string-parameter {@code synline}.
 *
 * @param nr the number of the requested synonym, with a range from 0 to (getCount()-1)
 * @param synline a string-array with the first element being the index-word, and the following
 * elements being the related synonyms
 */
public void setSynonymLine(int nr, String[] synline) {
    // get element
    Element synonym = retrieveElement(nr);
    // remove all child-content (i.e. all synonyms)
    synonym.removeContent();
    try {
        // set the original word as value-attribute to the "entry"-element
        synonym.setAttribute("indexword", synline[0]);
        // now go through the rest of the string-array
        for (int cnt = 1; cnt < synline.length; cnt++) {
            // create a sub-child "syn" for each further synonym
            Element syn = new Element("syn");
            // set text from string array
            syn.setText(synline[cnt]);
            // add child to synonym-element
            synonym.addContent(syn);
            setModified(true);
        }
    } catch (IllegalDataException | IllegalNameException ex) {
        Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
    }
}
项目:Zettelkasten    文件:Synonyms.java   
/**
 * This method adds a new synonym {@code appendsyn} to an existing synonym-line {@code nr}.
 *
 * @param nr the number of the existing synonym-line
 * @param appendsyn the new synonym that should be appended to that line.
 */
public void appendSingleSynonym(int nr, String appendsyn) {
    // get element
    Element synonym = retrieveElement(nr);
    // chekc for valid value
    if (synonym != null) {
        try {
            // create a sub-child "syn" for each further synonym
            Element syn = new Element("syn");
            // set text from string array
            syn.setText(appendsyn);
            // add child to synonym-element
            synonym.addContent(syn);
            setModified(true);
        } catch (IllegalAddException | IllegalDataException ex) {
            Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
        }
    }
}
项目:p2pEngine    文件:AbstractAdvertisement.java   
/**
 * Initialize this advertisement with the root element handle by JXTA.
 * @param root
 */
@SuppressWarnings("rawtypes")
protected void initialize(net.jxta.document.Element root) {
    if (!TextElement.class.isInstance(root)) {
           throw new IllegalArgumentException(getClass().getName() + "initialize needs a TextElement.");
       }

    TextElement doc = (TextElement) root; //create the doc.
    if (!doc.getName().equals(getAdvType())) {
           throw new IllegalArgumentException(
                "Error : " + getClass().getName() + " isn't " + doc.getName());
           //Jxta didn't call the right advertisement to construct. Unexpected error.
       }

    Enumeration elements = doc.getChildren();
       while(elements.hasMoreElements()) {
        TextElement elem = (TextElement) elements.nextElement();
        Element e = new Element(elem.getName()); //convert into a Jdom element.
        e.addContent(elem.getValue());
        if (!superHandleElement(e)) {
               throw new IllegalDataException(elem.getName());
               //this element is unknown for this advertisement.
           }
       }
}
项目:Zettelkasten    文件:DesktopData.java   
/**
 * Adds a new bullet-point to the xml-document.
 *
 * @param timestamp the timestamp of the currently selected bullet-point
 * after which the new bullet should be inserted.
 * @param name the name of the bullet-point
 * @return the timestamp of the added bullet-element as {@code String} or
 * {@code null} if an error occured.
 */
public String addBullet(String timestamp, String name) {
    // find the bulletgroup that is described in the treepath
    Element parentb = (timestamp != null) ? findEntryElementFromTimestamp(getCurrentDesktopElement(), timestamp) : getCurrentDesktopElement();
    String newts;
    // if we have a valid bullet-group, add the new bullet to the xml-file
    if (parentb != null) {
        // create a new element from the given name
        Element b = new Element(ELEMENT_BULLET);
        try {
            b.setAttribute("name", name);
            // set attribute for collapsed/expanded state of bullet
            b.setAttribute("treefold", TREE_FOLDING_EXPANDED);
            // create timestamp
            newts = Tools.getTimeStampWithMilliseconds();
            // check whether timestamp already exists. this is particulary the
            // case when a user adds several entries at once.
            while (timeStampExists(newts)) {
                newts = Tools.getTimeStampWithMilliseconds();
            }
            // set timestamp as attribute
            b.setAttribute(ATTR_TIMESTAMP, newts);
            // add a "comment" to that bullet. remember, that each bullet
            // automatically gets a child-element called "comment"
            b.addContent(new Element(ATTR_COMMENT));
            // add new bullet to the bulletgroup
            parentb.addContent(b);
            // change modified state
            setModified(true);
            // return timestamp of addes bullet
            return newts;
        } catch (IllegalNameException | IllegalDataException | IllegalAddException ex) {
            Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
            return null;
        }
    }
    return null;
}
项目:Zettelkasten    文件:DesktopData.java   
/**
 * This method adds a new entry (child-node) to the xml-document.
 *
 * @param timestamp the timestamp of the element, where the entry "nr"
 * should be inserted as new child
 * @param nr the entry-number of the entry that should be added
 * @param insertpos the position where the new entry should be inserted.
 * necessary when we have already more children and the entry should be
 * inserted in between, at the beginning or end of the children-list.
 * @return the timestamp of the added entry-element as {@code String} or
 * {@code null} if an error occured.
 */
public String addEntry(String timestamp, String nr, int insertpos) {
    // find the bullet that is described in the treepath
    Element b = findEntryElementFromTimestamp(getCurrentDesktopElement(), timestamp);
    // if we have a valid bullet, add the new enry to the xml-file
    if (b != null) {
        // check whether element is a bullet, if not, retrieve its parent element
        if (!b.getName().equals(ELEMENT_BULLET)) {
            b = b.getParentElement();
        }
        // create a new element
        Element e = new Element(ELEMENT_ENTRY);
        try {
            e.setAttribute("id", nr);
            // create timestamp
            String ts = Tools.getTimeStampWithMilliseconds();
            // check whether timestamp already exists. this is particulary the
            // case when a user adds several entries at once.
            while (timeStampExists(ts)) {
                ts = Tools.getTimeStampWithMilliseconds();
            }
            // add timestamp to entry element
            e.setAttribute(ATTR_TIMESTAMP, ts);
            // add new enry to the bullet at insert-position+1 (because at first
            // position in the bullet is always the comment)
            b.addContent(insertpos, e);
            // change modified state
            setModified(true);
            // return timestamp
            return ts;
        } catch (IllegalNameException | IllegalDataException | IllegalAddException ex) {
            Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage());
        }
    }
    return null;
}
项目:Zettelkasten    文件:DesktopData.java   
/**
 * This method changes the treefold-state of a bullet-point. The
 * <i>treefold</i>-attribute of the bullet-point is either set to
 * {@link #TREE_FOLDING_EXPANDED TREE_FOLDING_EXPANDED} or
 * {@link #TREE_FOLDING_COLLAPSED TREE_FOLDING_COLLAPSED}.<br><br>
 * With this attribute, we can check whether a bullet-point should be
 * expanded or not, when creating the TreeView in the CDesktop-Window.
 *
 * @param timestamp the timestamp of the bullet point which treefold-state
 * was changed
 * @param expanded {@code true} if the bullet point was expanded,
 * {@code false} if it was collapsed.
 */
public void setBulletTreefold(String timestamp, boolean expanded) {
    // retrieve bullet of which fold-state should be switched
    Element b = findEntryElementFromTimestamp(getCurrentDesktopElement(), timestamp);
    // check for valid value
    if (b != null) {
        try {
            // set new treefold-attribute
            b.setAttribute("treefold", (expanded) ? TREE_FOLDING_EXPANDED : TREE_FOLDING_COLLAPSED);
        } catch (IllegalNameException | IllegalDataException ex) {
            Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage());
        }
    }
}
项目:Zettelkasten    文件:DesktopData.java   
/**
 * This method adds a modified entry to the
 * {@link #modifiedEntries modifiedEntries}-Document, in case the user
 * modifies an entry on the desktop, while the original content of that
 * entry in the main database should be left unchanged.
 *
 * @param timestamp a unique timestamp which is associated to the entry that
 * is modified, so we can later easily find the related modified content.
 * @param content the modified content itself.
 * @return {@code true} if entry was successfully saved, {@code false} if an
 * error occured.
 */
public boolean addModifiedEntry(String timestamp, String content) {
    // check for valid parameters
    if (null == timestamp || timestamp.isEmpty()) {
        return false;
    }
    if (null == content) {
        content = "";
    }
    // first check, whether modified entry already exists. this may
    // occur, when importing archived desktop-files
    if (null == retrieveModifiedEntryElementFromTimestamp(timestamp)) {
        // create a new element
        Element entry = new Element(ELEMENT_ENTRY);
        try {
            // set timestamp-attribute
            entry.setAttribute(ATTR_TIMESTAMP, timestamp);
            // add content-string
            entry.setText(content);
            // add element to XML-document
            modifiedEntries.getRootElement().addContent(entry);
            // change modified-flag
            setModified(true);
        } catch (IllegalNameException | IllegalDataException | IllegalAddException ex) {
            Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage());
            return false;
        }
    }
    return true;
}
项目:Zettelkasten    文件:DesktopData.java   
/**
 * This method updates the timestamp-attributes when the datafile is being
 * updated due to a newer file-version.<br><br>
 * This method is called when updating a file with version number 3.0 or 3.1
 * to 3.2 and higher.
 *
 * @param e the initial element, where the updating should start. usually,
 * use something like
 * {@link #getDesktopElement(int) getDesktopElement(int)}.
 */
private void updateTimestamps(Element e) {
    // get a list with all children of the element
    List<Element> children = e.getChildren();
    // create an iterator
    Iterator<Element> it = children.iterator();
    DecimalFormat df = new DecimalFormat("00000");
    // go through all children
    while (it.hasNext()) {
        // get the child
        e = it.next();
        try {
            if (e.getName().equals(ELEMENT_ENTRY)) {
                String timestamp = e.getAttributeValue(ATTR_TIMESTAMP);
                if (timestamp != null) {
                    e.setAttribute(ATTR_TIMESTAMP, timestamp.concat(df.format(timestampid++)));
                }
            }
            if (e.getName().equals(ELEMENT_BULLET)) {
                e.setAttribute(ATTR_TIMESTAMP, Tools.getTimeStamp() + df.format(timestampid++));
                e.setAttribute("treefold", TREE_FOLDING_EXPANDED);
            }
        } catch (IllegalNameException | IllegalDataException ex) {
            Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
        }
        // when the new element also has children, call this method again,
        // so we go through the strucuture recursively...
        if (hasChildren(e)) {
            updateTimestamps(e);
        }
    }
}
项目:Zettelkasten    文件:Bookmarks.java   
/**
 * This method deletes a category from the data file. The category that
 * should be deleted is indicated by the category's index-number, passed as
 * parameter "nr". If the index-number is not known, use
 * {@link #deleteCategory(java.lang.String) deleteCategory(String cat)} to
 * delete that category or
 * {@link #getCategoryPosition getCategoryPosition(int nr)} to retrieve that
 * number.
 * <br><br>
 * <b>Attention!</b> All related bookmarks that are assigned to this
 * category are deleted as well!
 *
 * @param nr the index-number of the to be deleted category.
 */
public void deleteCategory(int nr) {
    // get cat-element
    Element category = bookmarks.getRootElement().getChild("category");
    // delete category from the xml-file
    if (category != null && category.removeContent(nr) != null) {
        // if we have successfully deleted a category, delete all bookmarks from
        // that category as well
        for (int cnt = getCount() - 1; cnt >= 0; cnt--) {
            // get each bookmark
            Element bm = retrieveBookmarkElement(cnt);
            try {
                // get category-atribute
                String cat = bm.getAttributeValue("cat");
                // check whether attribute exists
                if (cat != null) {
                    // get cat id
                    int catid = Integer.parseInt(cat);
                    // if catid equals the deleted category, delete bookmark
                    if (catid == nr) {
                        // get bookmark-element
                        Element child = bookmarks.getRootElement().getChild("bookmark");
                        // if it exists, remove it
                        if (child != null) {
                            child.removeContent(cnt);
                        }
                    } // in case the category-id was greater than the deleted category index-number,
                    // we have to update the category-index-number of the non-deleted bookmark
                    else if (catid > nr) {
                        bm.setAttribute("cat", String.valueOf(catid - 1));
                    }
                }
            } catch (NumberFormatException | IllegalNameException | IllegalDataException e) {
                Constants.zknlogger.log(Level.WARNING, e.getLocalizedMessage());
            }
            // change modified state
            setModified(true);
        }
    }
}
项目:Zettelkasten    文件:Bookmarks.java   
/**
 * Adds a new bookmark to the bookmark-file. First, we have to check whether
 * the bookmark already exists. If not, add it. Then we have to check for
 * the category. If it already exists, retrieve the category's index-number.
 * Else add a new category.
 * <br><br>
 * Use "getCompleteBookmark" for retrieving a bookmark's entry-number,
 * category and comment.
 *
 * @param index the index-number of the bookmark, i.e. the entry's number
 * @param cat the category, under which the bookmark should appear.
 * @param comment an optional comment for the bookmark
 */
public void addBookmark(int index, String cat, String comment) {
    // first check whether this index-number was already bookmarked...
    // if not, a -1 is return, else the index-number
    // if the bookmark already exists, do nothing
    if (-1 == getBookmarkPosition(index)) {
        try {
            // retrieve the position of the category
            int catpos = getCategoryPosition(cat);
            // check whether the category exists
            if (-1 == catpos) {
                // if the category doesn't already exist, add it
                catpos = addCategory(cat);
            }
            // create new bookmark-element
            Element bm = new Element(Daten.ELEMENT_ENTRY);
            // set the id, i.e. the number of the entry which is bookmarked
            bm.setAttribute("id", String.valueOf(index));
            // set the category-index for this bookmark
            bm.setAttribute("cat", String.valueOf(catpos));
            // and add the comment
            if (null == comment) {
                comment = "";
            }
            bm.setText(comment);
            // retrieve the bookmark-"root"-element
            Element bookbase = bookmarks.getRootElement().getChild("bookmark");
            // and add the bookmark-element
            bookbase.addContent(bm);
            // change modified-state
            setModified(true);
        } catch (IllegalAddException | IllegalNameException | IllegalDataException ex) {
            Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
        }
    }
}
项目:Zettelkasten    文件:Bookmarks.java   
/**
 * Changes an existing bookmark in the bookmark-file. First, we have to
 * check whether the bookmark already exists. If not, leave method. Then we
 * have to check for the category. If it already exists, retrieve the
 * category's index-number. Else add a new category.
 *
 * @param index the index-number of the bookmark, i.e. the entry's number
 * @param cat the category, under which the bookmark should appear.
 * @param comment an optional comment for the bookmark
 */
public void changeBookmark(int index, String cat, String comment) {
    // get the bookmark position
    int pos = getBookmarkPosition(index);
    // check whether it exists and go on...
    if (pos != -1) {
        // retrieve the position of the category
        int catpos = getCategoryPosition(cat);
        // check whether the category exists
        if (-1 == catpos) {
            // if the category doesn't already exist, add it
            catpos = addCategory(cat);
        }
        try {
            // get bookmark-element
            Element bm = retrieveBookmarkElement(pos);
            // set the id, i.e. the number of the entry which is bookmarked
            bm.setAttribute("id", String.valueOf(index));
            // set the category-index for this bookmark
            bm.setAttribute("cat", String.valueOf(catpos));
            // and add the comment
            bm.setText(comment);
            // change modified-state
            setModified(true);
        } catch (IllegalNameException | IllegalDataException ex) {
            Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
        }
    }
}
项目:Zettelkasten    文件:Daten.java   
/**
 * This method sets the links of an entry.
 *
 * @param pos the entry from which we want to set/change the hyperlinks and
 * attachments
 * @param attachments a string-array containing the hyperlinks,
 * attachmentspaths etc.
 */
public void setAttachments(int pos, String[] attachments) {
    // retrieve the entry
    Element entry = retrieveElement(zknFile, pos);
    // if no element exists, return empty array
    if (null == entry || null == attachments || attachments.length < 1) {
        return;
    }
    // remove all existing links from that entry
    entry.getChild(ELEMENT_ATTACHMENTS).removeChildren(ELEMENT_ATTCHILD);
    // save modification-stata
    boolean mod = false;
    // add each hyperlink string
    // therefor, iterate the array
    for (String a : attachments) {
        try {
            // create a new subchuld-element
            Element sublink = new Element(ELEMENT_ATTCHILD);
            // add the link-string from the array
            sublink.setText(a);
            // and add sublink-element to entry's child's content
            entry.getChild(ELEMENT_ATTACHMENTS).addContent(sublink);
            // change modification state
            mod = true;
        } catch (IllegalDataException | IllegalAddException ex) {
            Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage());
        }
    }
    // change modified state
    if (mod) {
        setModified(true);
    }
}
项目:Zettelkasten    文件:Daten.java   
/**
 * This method add the links to an entry.
 *
 * @param pos the entry from which we want to set/change the hyperlinks and
 * attachments
 * @param attachments a string-array containing the hyperlinks,
 * attachmentspaths etc.
 */
public void addAttachments(int pos, String[] attachments) {
    // retrieve the entry
    Element entry = retrieveElement(zknFile, pos);
    // if no element exists, return empty array
    if (null == entry || null == attachments || attachments.length < 1) {
        return;
    }
    // save modification-stata
    boolean mod = false;
    // add each hyperlink string
    // therefor, iterate the array
    for (String a : attachments) {
        try {
            // create a new subchuld-element
            Element sublink = new Element(ELEMENT_ATTCHILD);
            // add the link-string from the array
            sublink.setText(a);
            // and add sublink-element to entry's child's content
            entry.getChild(ELEMENT_ATTACHMENTS).addContent(sublink);
            // change modification state
            mod = true;
        } catch (IllegalDataException | IllegalAddException ex) {
            Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage());
        }
    }
    // change modified state
    if (mod) {
        setModified(true);
    }
}
项目:Zettelkasten    文件:Synonyms.java   
public void addSynonym(String[] synline) {
    // we need at least two elements in the array: the original word and at least one synonym
    if (null == synline || synline.length < 2) {
        return;
    }
    // if the synonyms-index-word already exists, don't add it...
    if (getSynonymPosition(synline[0]) != -1) {
        return;
    }
    // create new synonyms element
    Element synonym = new Element(Daten.ELEMENT_ENTRY);
    try {
        // trim spaces
        synline[0] = synline[0].trim();
        // set the original word as value-attribute to the "entry"-element
        synonym.setAttribute("indexword", synline[0]);
        // now go through the rest of the string-array
        for (int cnt = 1; cnt < synline.length; cnt++) {
            // create a sub-child "syn" for each further synonym
            Element syn = new Element("syn");
            // set text from string array
            syn.setText(synline[cnt].trim());
            // add child to synonym-element
            synonym.addContent(syn);
        }
        // finally, add new element to the document
        synonymsFile.getRootElement().addContent(synonym);
        setModified(true);
    } catch (IllegalNameException | IllegalDataException | IllegalAddException ex) {
        Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
    }
}
项目:p2pEngine    文件:AbstractAdvertisement.java   
/**
 * Initialize this advertisement with a Jdom root element
 * @param root 
 */
protected void initialize(Element root) {
    for(Element e: root.getChildren()) {
        if(!superHandleElement(e)) {
            throw new IllegalDataException(e.getName());
               //this element is unknown for this advertisement.
        }
    }
}
项目:Zettelkasten    文件:DesktopData.java   
/**
 * This method adds a new desktop-element to the document. Furthermore,
 * currentDesktop-number is set to the latest added desktop-index-number.
 *
 * @param name the name of the desktop, which appears in the desktopDialog's
 * combobox
 * @param notes the initial desktop-notes that should be associated with
 * this desktop. use this e.g. when importing an archived desktop (see
 * {@link #importArchivedDesktop(org.jdom.Document) importArchivedDesktop()}.
 * use {@code null} when no notes-content should be added (i.e. the elements
 * are being created, but they have no text).
 * @return {@code true} if the desktop was successfully added, {@code false}
 * otherwise (e.g. because the desktop-name already existed)
 */
public boolean addNewDesktop(String name, String[] notes) {
    // first of all, go through all desktops and check whether the name
    // already exist, to avoid double naming...
    // when such a desktopname as "name" already exists, return false
    for (int cnt = 0; cnt < getCount(); cnt++) {
        if (name.equalsIgnoreCase(getDesktopName(cnt))) {
            return false;
        }
    }
    // create new element
    Element d = new Element("desktop");
    try {
        // set the desktop's name as attribute
        d.setAttribute("name", name);
        // add the element to the desktop
        desktop.getRootElement().addContent(d);
        // set currentDesktop index to the new desktop-element
        currentDesktop = desktop.getRootElement().getContentSize() - 1;
        // also add new desktop-notes-element
        Element desk = new Element("desktop");
        // set name attribute
        desk.setAttribute("name", name);
        // create notes elements
        Element n1 = new Element("notes1");
        Element n2 = new Element("notes2");
        Element n3 = new Element("notes3");
        // set initial notes text, if we have any...
        if (notes != null && notes.length > 0) {
            // check for array-length before setting text
            if (notes.length > 0) {
                n1.setText(notes[0]);
            }
            // check for array-length before setting text
            if (notes.length > 1) {
                n2.setText(notes[1]);
            }
            // check for array-length before setting text
            if (notes.length > 2) {
                n3.setText(notes[2]);
            }
        }
        // add notes-sub-elements
        desk.addContent(n1);
        desk.addContent(n2);
        desk.addContent(n3);
        // add element to desktop-notes
        desktopNotes.getRootElement().addContent(desk);
        // change modified state
        setModified(true);
    } catch (IllegalAddException | IllegalNameException | IllegalDataException ex) {
        Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
        return false;
    }
    return true;
}
项目:Zettelkasten    文件:DesktopData.java   
/**
 * This method archives the desktop-data of the desktop with the name
 * {@code name} to a zipped xml-file. The file contains the desktop-data,
 * the modifed-entries-data for those entries that appear on the desktop and
 * the saved desktop-notes.
 *
 * @param name the name of the desktop that should be archived.
 * @return the archived document as XML-focument, or {@code null} if an
 * error occured.
 */
public Document archiveDesktop(String name) {
    // create new document
    Document archive = new Document(new Element("archivedDesktop"));
    // add desktop-element of desktop that should be archived
    Element deskel = getDesktopElement(name);
    // if we found a valid value, go on
    if (deskel != null) {
        try {
            // set name attribute
            archive.getRootElement().setAttribute("name", name);
            // create desktop-element
            Element content_desktop = new Element("desktop");
            // clone content from current desktop
            content_desktop.addContent(deskel.cloneContent());
            // add element to archive-file
            archive.getRootElement().addContent(content_desktop);
            // now retrieve desktop-notes
            Element noteel = getDesktopNotes(name);
            // if we found a valid value, go on
            if (noteel != null) {
                // create notes-element
                Element content_notes = new Element("desktopNotes");
                // clone content from current desktop-notes
                content_notes.addContent(noteel.cloneContent());
                // add content
                archive.getRootElement().addContent(content_notes);
            }
            // now retrieve all timestamps from the archived desktop
            // and look for modified entries...
            // create new list that will contain the timestamps
            timestampList = new ArrayList<>();
            // fill list with all entry-numbers. since we have sub-bullets/levels, we
            // recursevly go through the desktop-data
            retrieveDesktopEntriesTimestamps(deskel);
            // if we have any results, go on...
            if (timestampList.size() > 0) {
                // create base element
                Element modifiedel = new Element("modifiedEntries");
                // add all modified entries that appear on the archived desktop
                String[] timestamps = timestampList.toArray(new String[timestampList.size()]);
                for (String ts : timestamps) {
                    // retrieve modifed entry
                    Element me_dummy = retrieveModifiedEntryElementFromTimestamp(ts);
                    // check for valid value
                    if (me_dummy != null) {
                        // crate new modified-entry-element
                        Element me = new Element(ELEMENT_ENTRY);
                        // set timestamp-attribute
                        me.setAttribute(ATTR_TIMESTAMP, ts);
                        // and add modified text
                        me.setText(me_dummy.getText());
                        // and add content
                        modifiedel.addContent(me);
                    }
                }
                archive.getRootElement().addContent(modifiedel);
            }
        } catch (IllegalNameException | IllegalDataException | IllegalAddException ex) {
            Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
            return null;
        }
    }
    return archive;
}