Example usage for org.jdom2 Element getChild

List of usage examples for org.jdom2 Element getChild

Introduction

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

Prototype

public Element getChild(final String cname) 

Source Link

Document

This returns the first child element within this element with the given local name and belonging to no namespace.

Usage

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

License:Open Source License

/**
 * Checks whether an entry at the given {@code pos} is empty (thus deleted) or not.
 * @param entry/*from   ww  w . ja  va2s . c o  m*/
 * @return {@code true} when the entry with the number {@code pos} is empty, false otherwise
 */
public boolean isEmpty(Element entry) {
    // if no element exists, return false
    if (null == entry)
        return true;
    // else return whether content available or not
    return entry.getChild(ELEMENT_CONTENT).getText().isEmpty();
}

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

License:Open Source License

/**
 * This method returns the links of an entry. since we can have more than just one 
 * link/hyperlink per entry, the return-value is of the type {@code List<Element>}, i.e.
 * we return a list of xml-elements which contain the links of an entry.
 * /*  w  ww. j  a va 2s . c o  m*/
 * @param pos the entry from which we want to retrieve the hyperlinks
 * @return a List of xml-Elements, or null if no links are available
 */
public List<Element> getAttachments(int pos) {
    // retrieve the entry
    Element entry = retrieveElement(zknFile, pos);
    // if no element exists, return empty array
    if (null == entry)
        return null;
    // retrieve list of attachments
    List<Element> dummy = entry.getChild(ELEMENT_ATTACHMENTS).getChildren();
    List<Element> attachments = new LinkedList<Element>();
    // we have to manually copy all elements from one list to the other,
    // so we don't change the original content.
    Iterator<Element> it = dummy.iterator();
    // go through list
    while (it.hasNext()) {
        // retrieve element
        Element att = it.next();
        // change separator chars
        String attstring = Tools.convertSeparatorChars(att.getText(), settings);
        // add element
        if (!attstring.isEmpty()) {
            // create new element
            Element e = new Element(ELEMENT_ATTCHILD);
            // set text
            e.setText(attstring);
            // add element to return-list
            attachments.add(e);
        }
    }
    // else return the child-elements of the links-element
    return attachments;
}

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

License:Open Source License

/**
 * //from  w w w  .  j ava  2  s  . c  o m
 * @param pos
 * @return 
 */
public boolean hasAttachments(int pos) {
    // retrieve the entry
    Element entry = retrieveElement(zknFile, pos);
    // if no element exists, return empty array
    if (null == entry)
        return false;
    // retrieve list of attachments
    List<Element> dummy = entry.getChild(ELEMENT_ATTACHMENTS).getChildren();
    return (dummy != null && dummy.size() > 0);
}

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

License:Open Source License

/**
 * This method returns the links of an entry. since we can have more than just one
 * link/hyperlink per entry, the return-value is string-arra y which contain the
 * links of an entry./* w ww  .  j  a va2 s  .  co  m*/
 *
 * @param pos the entry from which we want to retrieve the hyperlinks
 * @param makeLinkToAttachment {@code true} if the attachment should be linked, in case the attachment
 * isan existing file on the hard disk. in this case, the attachment is surrounded by "file://" references.
 * @return a string-array of links/attachments, or null if no links are available
 */
public String[] getAttachmentsAsString(int pos, boolean makeLinkToAttachment) {
    // retrieve the entry
    Element entry = retrieveElement(zknFile, pos);
    // if no element exists, return empty array
    if (null == entry)
        return null;
    // get the child-elements of the links-element
    List<Element> links = entry.getChild(ELEMENT_ATTACHMENTS).getChildren();
    // create iterator and copy all elements to a linked list
    Iterator<Element> i = links.iterator();
    ArrayList<String> list = new ArrayList<String>();
    // copy list to array
    while (i.hasNext()) {
        // get each link-element
        Element e = i.next();
        // get link
        String link = e.getText();
        if (!link.isEmpty()) {
            // convert separator chars
            link = Tools.convertSeparatorChars(link, settings);
            // if the attachment should be linked, check whether it is an existing file
            //                if (makeLinkToAttachment) {
            // TODO hier noch weitermachen? Anhnge automatsch verlinken
            //                    if (!CCommonMethods.isHyperlink(link)) {
            //                        File linkfile = CCommonMethods.getLinkFile(settings, link);
            //                        // convert all file-attachments to hyperlinks
            //                        String file = "file://";
            //                        if (System.getProperty("os.name").toLowerCase().startsWith("windows")) file = File.separatorChar+file;
            //                        link = "<a href=\""+file+linkfile.toString()+"\">"+link+"</a>";
            //                    }
            //                }
            list.add(link);
        }
    }
    return list.toArray(new String[list.size()]);
}

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

License:Open Source License

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

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

License:Open Source License

/**
 * 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.
 *//*from   w  w  w.  ja v a2  s .c o  m*/
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 ex) {
            Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage());
        } catch (IllegalAddException ex) {
            Constants.zknlogger.log(Level.WARNING, ex.getLocalizedMessage());
        }
    }
    // change modified state
    if (mod)
        setModified(true);
}

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

License:Open Source License

/**
 * This method returns the remarks of a given entry. The entry-number which
 * remarks should be retrieved, is passed via paramter (pos).
 * /* w  ww  .ja va2 s  .  co  m*/
 * @param pos (the entry from which we want to retrieve the hyperlinks)
 * @return a string containing the remarks of an entry, or an empty string if no remarks found
 */
public String getRemarks(int pos) {
    // 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_REMARKS))
        return "";
    // else return remarks
    return el.getChild(ELEMENT_REMARKS).getText();
}

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

License:Open Source License

/**
 * 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
 *//*from  w w w  .ja  va  2 s  .  c  om*/
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;
}

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

License:Open Source License

/**
 * This method retrieves all keywords of a given entry.
 * //ww  w. jav a2s .co m
 * <b>Caution!</b> The position {@code pos} is a value <i>from 1 to (size of zknfile)</i> - in contrary
 * to usual array handling where the range is from 0 to (size-1) - , so we can directly
 * use the index number which are displayed in the jTable of the main window. However,
 * the access to the xml files are ranged between 0 and size-1, but this is achieved
 * in the retrieveElement-method, where we use "pos-1" to locate the correct entry
 *
 * @param pos a value from 1 to (size of zknfile), indicating the entry-number of which keywords are requested
 * @return a string array with all keywords of the requested entry, or <i>null</i> if no
 * keywords were found.
 */
public String[] getKeywords(int pos) {
    // first retrieve the current "zettel" element
    Element kw = retrieveElement(zknFile, pos);
    // if no element exist, return failed value
    if (null == kw)
        return null;
    // if no keyword index numbers exist, return failed value
    if (kw.getChild(ELEMENT_KEYWORD).getText().isEmpty())
        return null;
    // then get the keyword indexnumbers
    String[] kwa = kw.getChild(ELEMENT_KEYWORD).getText().split(",");
    // create a new string array return value, which will contain the keyword strings
    String[] retval = new String[kwa.length];
    // iterate the array
    // convert each keyword index number into an integer value
    // and get the related keyword string from the keyword data file
    // (this is achieved by the getKeyword-Method)
    for (int cnt = 0; cnt < kwa.length; cnt++) {
        retval[cnt] = getKeyword(Integer.parseInt(kwa[cnt]));
    }
    return retval;
}

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

License:Open Source License

/**
 * This method retrieves all authors of a given entry.
 * /*from w  w  w  .ja  v  a 2  s. com*/
 * <b>Caution!</b> The position {@code pos} is a value <i>from 1 to (size of zknfile)</i> - in contrary
 * to usual array handling where the range is from 0 to (size-1) - so we can directly
 * use the index number which are displayed in the jTable of the main window. However,
 * the access to the xml files are ranged between 0 and size-1, but this is achieved
 * in the retrieveElement-method, where we use "pos-1" to locate the correct entry
 *
 * @param pos a value from 1 to (size of zknfile), indicating the entrynumber of the entry which authors are requested
 * @return a string array with all authors of the requested entry, or <i>null</i> if no author was found
 */
public String[] getAuthors(int pos) {
    // first retrieve the current "zettel" element
    Element au = retrieveElement(zknFile, pos);
    // if no element exist, return failed value
    if (null == au)
        return null;
    // if no author index numbers exist, return failed value
    if (au.getChild(ELEMENT_AUTHOR).getText().isEmpty())
        return null;
    // then get the author indexnumbers
    String[] aunr = au.getChild(ELEMENT_AUTHOR).getText().split(",");
    // create a new string array return value, which will contain the author strings
    String[] retval = new String[aunr.length];
    // iterate the array
    // convert each authorindex number into an integer value
    // and get the related keyword string from the author data file
    // (this is achieved by the getAuthor-Method)
    for (int cnt = 0; cnt < aunr.length; cnt++)
        retval[cnt] = getAuthor(Integer.parseInt(aunr[cnt]));
    return retval;
}