Example usage for org.jdom2 Element getAttributeValue

List of usage examples for org.jdom2 Element getAttributeValue

Introduction

In this page you can find the example usage for org.jdom2 Element getAttributeValue.

Prototype

public String getAttributeValue(final String attname) 

Source Link

Document

This returns the attribute value for the attribute with the given name and within no namespace, null if there is no such attribute, and the empty string if the attribute value is empty.

Usage

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

License:Open Source License

/**
 * Gets a String-pair of auto-correct data, i.e. a string array with 2 fields. first
 * field contains the wrong/mispelled writing, the second field holds the correct
 * version of the word//w  ww  . ja v  a2s.  c o m
 * @param nr the position of the element we want to retrieve
 * @return a string array containing the wrong and right spelling
 */
public String[] getElement(int nr) {
    // get all elements
    List<Element> all = autokorrektur.getRootElement().getChildren();
    // get the requested element
    Element el = all.get(nr);
    // new string array
    String[] retval = null;
    // if we found an element, return its content
    if (el != null) {
        retval = new String[2];
        retval[0] = el.getAttributeValue("id");
        retval[1] = el.getText();
    }

    return retval;
}

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

License:Open Source License

/**
 *
 * @param wrong//ww  w  .  jav  a2s .c  om
 * @return the correct spelling of the word {@code wrong} or {@code null}, if no spellcorrection was found
 */
public String getCorrectSpelling(String wrong) {
    // get all elements
    List<Element> all = autokorrektur.getRootElement().getChildren();
    // create an iterator
    Iterator<Element> i = all.iterator();
    // go through list
    while (i.hasNext()) {
        // get element
        Element el = i.next();
        // get attribute value
        String att = el.getAttributeValue("id");
        // check for existing attribute
        if (null == att) {
            return null;
        }
        // get spell-check-word
        String correct = att.toLowerCase();
        // get lower-case word of mistyped wrong word...
        String retval = wrong.toLowerCase();
        // if the element's id equals the requestes string "e", return true
        // i.e. the string e already exists as element
        if (correct.equalsIgnoreCase(wrong)) {
            // now that we found the correct word, we want to see whether
            // the word starts with an upper case letter - and if so, convert
            // the first letter of the return value to upper case as well
            String firstLetter = wrong.substring(0, 1);
            String firstBigLetter = wrong.substring(0, 1).toUpperCase();
            // get return value
            retval = el.getText();
            // if both matches, we have upper case initial letter
            // convert first letter to uppercase
            if (firstLetter.equals(firstBigLetter)) {
                retval = retval.substring(0, 1).toUpperCase() + retval.substring(1);
            }
            return retval;
        }
        // when the misspelled phrase starts with an asterisk, we know that we should check the
        // end of or in between the typed word "wrong".
        if (correct.startsWith("*")) {
            // first we remove the asterisk
            correct = correct.substring(1);
            // if the misspelled phrase also ends with an asterisk, we have to check
            // for the phrase in between - that means, "wrong" is not allowed to end or start
            // with "correct"
            if (correct.endsWith(("*"))) {
                // remove trailing asterisk
                correct = correct.substring(0, correct.length() - 1);
                // if the mistyped word "wrong" does not start and end with "correct", we know
                // that we have a correction in between
                if (retval.contains(correct)) {
                    // return correct word for wrong spelling
                    return correctWithCase(retval, correct, el.getText(), wrong);
                }
            }
            // if the mistyped word "wrong" does not end with "correct", we know
            // that
            else if (retval.endsWith(correct) && retval.contains(correct)) {
                // return correct word for wrong spelling
                return correctWithCase(retval, correct, el.getText(), wrong);
            }
        } else if (correct.endsWith("*")) {
            // get lower-case word of mistyped wrong word...
            retval = wrong.toLowerCase();
            // if the mistyped word "wrong" does not end with "correct", we know
            // that we have the correction at thr word beginning
            if (retval.startsWith(correct) && retval.contains(correct)) {
                // return correct word for wrong spelling
                return correctWithCase(retval, correct, el.getText(), wrong);
            }
        }
    }
    // return null, no correct word found
    return null;
}

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

License:Open Source License

/**
 * This method returns a complete bookmark at the position {@code nr}, i.e.
 * the bookmarked entry-index-number, the category name and the bookmark's comment
 * are return as an string-array.// w ww .  j a v a  2 s.  co m
 * <br><br>
 * The counterpart of this method is "addBookmark", which adds a new complete
 * bookmark including comment and category.
 * 
 * @param nr the bookmark which should be retrieved
 * @return an array with all bookmark-information:<br>
 * - String[0]: entry-number<br>
 * - String[1]: category-name and<br>
 * - String[2]: comment
 * or {@code null} if no bookmark-element was found
 */
public String[] getCompleteBookmark(int nr) {
    // retrieve the bookmark-element
    Element bm = retrieveBookmarkElement(nr);
    // if it does not exist, leave method
    if (null == bm) {
        return null;
    }
    // else create return value
    String[] retval = new String[3];
    // retrieve the entry-number, which is bookmarked
    String entry_id = bm.getAttributeValue("id");
    // check for valid attribute-value
    if (entry_id != null) {
        // valid attribute, so store entry-id
        retval[0] = entry_id;
    } else {
        // else return null
        return null;
    }
    // get the category-string
    String category = bm.getAttributeValue("cat");
    // check for valid value
    if (category != null) {
        // save category
        try {
            retval[1] = getCategory(Integer.parseInt(category));
        } catch (NumberFormatException ex) {
            Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage());
            return null;
        }
    } else {
        // else return null
        return null;
    }
    // get the bookmarks comment
    retval[2] = bm.getText();

    return retval;
}

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

License:Open Source License

/**
 * //from w  w w  . ja v  a2  s  .  c  o  m
 * @param dataObj
 * @param newbookmarks 
 */
public void appendBookmarks(Daten dataObj, Document newbookmarks) {
    // create a list of all elements from the given xml file
    try {
        List<?> elementList = newbookmarks.getRootElement().getContent();
        try {
            // iterate all importet bookmakrs
            for (int cnt = 0; cnt < newbookmarks.getContentSize(); cnt++) {
                // retrieve each single bookmark-element
                Element b = (Element) elementList.get(cnt);
                // get bookmark-id (i.e. unique entry-ID)
                String id = b.getAttributeValue("id");
                // check for valid value
                if (id != null && !id.isEmpty()) {
                    // find entry number from ID
                    int index = dataObj.findZettelFromID(id);
                    // check for valid return parameter
                    if (index != -1) {
                        // we now have the entry's number. now retrieve
                        // bookmark-category
                        String cat = b.getAttributeValue("cat");
                        // check for valid value
                        if (cat != null && !cat.isEmpty()) {
                            // retrieve possible comment
                            String comment = b.getText();
                            // and add new importet bookmark
                            addBookmark(index, cat, comment);
                        }
                    }
                }
            }
        } catch (IndexOutOfBoundsException e) {
        }
    } catch (IllegalStateException e) {
        Constants.zknlogger.log(Level.WARNING, e.getLocalizedMessage());
    }
}

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

License:Open Source License

/**
 * 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>//from   w  w w.j a  v a  2 s  .  c om
 * <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 e) {
                Constants.zknlogger.log(Level.WARNING, e.getLocalizedMessage());
            } catch (IllegalNameException e) {
                Constants.zknlogger.log(Level.WARNING, e.getLocalizedMessage());
            } catch (IllegalDataException e) {
                Constants.zknlogger.log(Level.WARNING, e.getLocalizedMessage());
            }
            // change modified state
            setModified(true);
        }
    }
}

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

License:Open Source License

/**
 * This method returns the position of a bookmarked entry-number in the bookmarks XML file.
 * if the entry-number was not bookmarked (i.e. the entry-number does not exist as bookmark-number),
 * the return value is -1//from  w  w  w.  j a  va 2  s . com
 * 
 * @param nr the number of the bookmarked entry
 * @return the position of the bookmark within the xml-file, or -1 if no bookmark was found
 */
public int getBookmarkPosition(int nr) {
    // create a list of all bookmarks elements from the bookmark xml file
    try {
        // get root element
        Element bme = bookmarks.getRootElement();
        // get child element
        Element bookchild = bme.getChild("bookmark");
        // check for valid value
        if (bookchild != null) {
            // we are interested in the children of the child-element "bookmark"
            List<?> bookmarkList = bookchild.getContent();
            // an iterator for the loop below
            Iterator<?> iterator = bookmarkList.iterator();
            // counter for the return value if a found bookmark-number matches the parameter
            int cnt = 0;
            // start loop
            while (iterator.hasNext()) {
                // retrieve each element
                Element bm = (Element) iterator.next();
                // if bookmark-number matches the parameter integer value, return the position
                if (Integer.parseInt(bm.getAttributeValue("id")) == nr) {
                    return cnt;
                }
                // else increase counter
                cnt++;
            }
        }
        // if no bookmark 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.Bookmarks.java

License:Open Source License

/**
 * This method returns the category-id (category-number) of a given bookmark {@code nr}.
 * Use {@link #getEntryCategory(int) getEntryCategory(int)} if you want to get the category
 * of an entry (with the number {@code nr}) that was bookmarked, and not the category of a
 * bookmark-id itself.//from  ww w.j  a  va2  s.co  m
 * 
 * @param nr the bookmark-index-number as it appears in the database (<b>not</b> the entry-number of the bookmark)
 * @return the number of the bookmark's category-id, or -1 of no category was found
 */
public int getBookmarkCategory(int nr) {
    // check for valid parameter
    if (nr < 0 || nr >= getCount()) {
        return -1;
    }
    // retrieve the bookmark-element
    Element bm = retrieveBookmarkElement(nr);
    // if it does not exist, leave method
    if (null == bm) {
        return -1;
    }
    // else return category-id
    return Integer.parseInt(bm.getAttributeValue("cat"));
}

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

License:Open Source License

/**
 * This method returns the entry-number of a given bookmark {@code nr}.
 * The value {@code nr} has a range from 0 to {@link #getCount() getCount()}-1.
 * /*from w  w w  .j  ava2 s .c o  m*/
 * @param nr the bookmark-index-number as it appears in the database (<b>not</b> the entry-number of the bookmark)
 * @return the number of the entry that was bookmarked, or -1 of no bookmark was found
 */
public int getBookmarkEntry(int nr) {
    // retrieve the bookmark-element
    Element bm = retrieveBookmarkElement(nr);
    // if it does not exist, leave method
    if (null == bm) {
        return -1;
    }
    // else return category-id
    return Integer.parseInt(bm.getAttributeValue("id"));
}

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

License:Open Source License

/**
 * This method returns the version of the fileformat. the filestructure and data-storing
 * might change due to further development of this programm, so here we can check
 * for the current fileformat-version if necessary. This information is stored in the
 * metainformation-file.//from  w w w .  j  a v a2 s  . com
 * <br><br>
 * This may differ from the version of the <i>current</i> fileformat. see
 * {@link #getCurrentVersionInfo() getCurrentVersionInfo()} to retrieve the version-number
 * of the current fileformat.
 * 
 * @return a string containing the version-number of the zettelkasten-file-format, or {@code null} if
 * no such attribute exists
 */
public String getVersionInfo() {
    // retrieve version-element
    Element el = metainfFile.getRootElement().getChild(ELEMENT_VERSION_INFO);
    // check whether it's null or not
    if (null == el) {
        // log error
        Constants.zknlogger.log(Level.WARNING, "Could not read file version info. XML-element is null!");
        return null;
    }
    // get id-attribute
    String id = el.getAttributeValue("id");
    // check for valid value
    if (null == id || id.isEmpty()) {
        // log error
        Constants.zknlogger.log(Level.WARNING, "Could not read file version info. XML-attribute is null!");
        return null;
    }
    // return the attribute value
    return id;
}

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

License:Open Source License

/**
 * This method changes the frequencies of an entry's authors and keywords by the given value {@code addvalue}.
 * @param nr the entrynumber, which author- and keywords-frequencies should be changed
 * @param addvalue the amount of increase or decrease of each author/keyword-frequency
 *///from w  ww  .  ja va  2  s .  c  om
private void changeFrequencies(int nr, int addvalue) {
    // 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
    int[] aus = getAuthorIndexNumbers(nr);
    // check whether we have any values at all
    if (aus != null && aus.length > 0) {
        // iterate the array
        for (int a : aus) {
            // check for valid value
            if (a != -1) {
                try {
                    // retrieve existing author
                    Element au = retrieveElement(authorFile, a);
                    // chek for valid value
                    if (au != null) {
                        // get the count-value, which indicates the frequency of occurences of this
                        // author in the whole data file
                        String freq = au.getAttributeValue(ATTRIBUTE_FREQUENCIES);
                        if (freq != null) {
                            int f = Integer.parseInt(freq);
                            // increase frequency by 1
                            au.setAttribute(ATTRIBUTE_FREQUENCIES, String.valueOf(f + addvalue));
                        }
                    }
                } catch (NumberFormatException ex) {
                    Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
                } catch (IllegalNameException ex) {
                    Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
                } catch (IllegalDataException ex) {
                    Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
                }
            }
        }
    }
    // now do this for the keywords. retrieve all keyword -index-numbers from that entry
    int[] kws = getKeywordIndexNumbers(nr);
    // check whether we have any values at all
    if (kws != null && kws.length > 0) {
        // iterate the array
        for (int k : kws) {
            // check for valid value
            if (k != -1) {
                try {
                    // retrieve existing author
                    Element kw = retrieveElement(keywordFile, k);
                    // chek for valid value
                    if (kw != null) {
                        // get the count-value, which indicates the frequency of occurences of this
                        // keyword in the whole data file
                        String freq = kw.getAttributeValue(ATTRIBUTE_FREQUENCIES);
                        if (freq != null) {
                            int f = Integer.parseInt(freq);
                            // increase frequency by 1
                            kw.setAttribute(ATTRIBUTE_FREQUENCIES, String.valueOf(f + addvalue));
                        }
                    }
                } catch (NumberFormatException ex) {
                    Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
                } catch (IllegalNameException ex) {
                    Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
                } catch (IllegalDataException ex) {
                    Constants.zknlogger.log(Level.SEVERE, ex.getLocalizedMessage());
                }
            }
        }
    }
}