Example usage for org.dom4j DocumentHelper createXPath

List of usage examples for org.dom4j DocumentHelper createXPath

Introduction

In this page you can find the example usage for org.dom4j DocumentHelper createXPath.

Prototype

public static XPath createXPath(String xpathExpression) throws InvalidXPathException 

Source Link

Document

createXPath parses an XPath expression and creates a new XPath XPath instance using the singleton DocumentFactory .

Usage

From source file:lost.tok.AuthorsHandler.java

License:Open Source License

/**
 * Constructor for Authors Handler from an XML file
 * /*from ww w  .ja  v  a 2 s .  c  o m*/
 * @param myToK
 * @param filename
 */
public AuthorsHandler(ToK myToK, String filename) {
    int max = 0;
    this.myToK = myToK;

    Document d = GeneralFunctions.readFromXML(filename);

    List result = DocumentHelper.createXPath("//id").selectNodes(d);
    for (Iterator i = result.iterator(); i.hasNext();) {
        Element element = (Element) i.next();
        if (Integer.valueOf(element.getText()) > max) {
            max = Integer.valueOf(element.getText());
        }
    }

    id = max;
}

From source file:lost.tok.AuthorsHandler.java

License:Open Source License

/**
 * Adding an author to the xml file// w w w. j a  v  a 2 s  .  c o m
 * 
 * @param author - the author to add
 * @param rank
 * @throws CoreException
 */
public void addAuthor(Author author, Integer rank) throws CoreException {

    if (!myToK.getProject().exists() || author == null) {
        throwCoreException("problem with atributes to addAuthor");
        return;
    }

    Document doc = readFromXML();

    // creating the xPath
    XPath xpathSelector = DocumentHelper.createXPath("//authorsGroup[id='" + rank + "']");
    List result = xpathSelector.selectNodes(doc);
    if (result.size() == 1) {
        Element authorsGroup = (Element) result.get(0);
        author.setID(++id);
        authorsGroup.add(author.toXML());

        writeToXml(doc);
    }
}

From source file:lost.tok.AuthorsHandler.java

License:Open Source License

/**
 * Update authors file with new authors that 
 * may have been added (in new sources)//  www.ja va 2  s.  com
 */
public void updateFile() {
    Source[] sources = myToK.getSources();

    for (Source src : sources) {

        String authName = src.getAuthor();
        Document doc = readFromXML();
        Integer defRank = AuthorsEditor.DEFAULT_RANK_ID;

        XPath xpathSelector = DocumentHelper.createXPath("//author[.='" + authName + "']");
        List result = xpathSelector.selectNodes(doc);

        //author is NOT in file
        if (result.size() == 0) {
            try {
                this.addAuthor(new Author(defRank, authName), defRank);
            } catch (CoreException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:lost.tok.AuthorsHandler.java

License:Open Source License

/**
 * Get ranks// w  ww. j a  va2 s . c  o m
 * @return array of ranks objects
 */
public Rank[] getRanks() {
    Document doc = readFromXML();

    XPath xpathSelector = DocumentHelper.createXPath("//authorsGroup");
    List result = xpathSelector.selectNodes(doc);

    Rank[] ss = new Rank[result.size()];
    int i = 0;
    for (Object object : result) {
        Element e = (Element) object;
        ss[i++] = new Rank(e);
    }

    return ss;
}

From source file:lost.tok.AuthorsHandler.java

License:Open Source License

/**
 * Relocate author to target rank//from   w  w w  . j ava 2  s.  c o m
 * 
 * @param srcRank
 * @param trgtRank
 */
public void relocateAuthor(String authName, Integer trgtRank) {

    Document doc = readFromXML();

    // If the author and the target rank can be found,
    // the author will move to the target rank
    XPath xpathSelector1 = DocumentHelper.createXPath("//author[.='" + authName + "']");
    List result = xpathSelector1.selectNodes(doc);
    if (result.size() == 1) {
        Element element = (Element) result.get(0);
        XPath xpathSelector2 = DocumentHelper
                .createXPath("//authorsGroup[id='" + java.lang.Integer.toString(trgtRank) + "']");
        List targets = xpathSelector2.selectNodes(doc);
        if (targets.size() == 1) {
            Element target = (Element) targets.get(0);
            element.detach();
            target.add(element);
        }
    }

    writeToXml(doc);
}

From source file:lost.tok.AuthorsHandler.java

License:Open Source License

/**
 * Remove author from the authors groups
 * //from  w w w  . ja  v  a2s .c o m
 * @param authorName
 */
public void removeAuthor(String authName) {

    Document doc = readFromXML();

    // Remove all the authors (should be only one) with the given name
    // if there are several authors with the given name, fix it by removing all
    // of them if there are no authors with the given name, do nothing
    XPath xpathSelector1 = DocumentHelper.createXPath("//author[getText()='" + authName + "']");
    List result = xpathSelector1.selectNodes(doc);
    for (Iterator i = result.iterator(); i.hasNext();) {
        Element element = (Element) i.next();
        element.detach();
    }

    writeToXml(doc);
}

From source file:lost.tok.AuthorsHandler.java

License:Open Source License

/**
 * Get author's rank//from   w  ww .j  a v  a2s . c  o  m
 * @param authName
 * @return the rank of given author
 */
public int getAuthorRank(String authName) {

    int resultRank = 0;

    try {
        Document doc = readFromXML();

        XPath xpathSelector1 = DocumentHelper.createXPath("//authorsGroup/author[.='" + authName + "']/../id");
        List result = xpathSelector1.selectNodes(doc);

        if (result.size() == 1) {
            Element element = (Element) result.get(0);

            resultRank = Integer.valueOf(element.getText());
        }
    } catch (Exception e) {
    }

    return resultRank;
}

From source file:lost.tok.Link.java

License:Open Source License

/**
 * Loads a discussion link from an XML element
 * @param linkedDiscussion the discussion linked by this link
 * @param linkElm the XML element describing this link
 *///from w  ww.  j a  v  a  2 s . c o  m
public Link(Discussion linkedDiscussion, Element linkElm) {
    subLinkList = new LinkedList<SubLink>();

    this.linkedDiscussion = linkedDiscussion;
    this.linkFile = linkedDiscussion.getMyToK().getLinkFile();

    Node typeNode = DocumentHelper.createXPath("type").selectSingleNode(linkElm); //$NON-NLS-1$
    Node subjNode = DocumentHelper.createXPath("linkSubject").selectSingleNode(linkElm); //$NON-NLS-1$
    this.linkTypeXML = typeNode.getText();
    this.subject = subjNode.getText();

    List sublinkElms = DocumentHelper.createXPath("sublink").selectNodes(linkElm); //$NON-NLS-1$
    for (Object oSublinkElm : sublinkElms) {
        Element sublinkElm = (Element) oSublinkElm;

        subLinkList.add(new SubLink(linkedDiscussion.getMyToK(), sublinkElm));
    }

}

From source file:lost.tok.navigator.InformationComparator.java

License:Open Source License

/** 
 * Compares according to the order.xml file which should be in the resource directory
 * Is not consistent with equals// w w w.  j a v  a  2 s  .c  o m
 * Resources should be in the same path
 * 
 * @param r1 first res
 * @param r2 second res
 * @return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
 */
public int compare(IResource r1, IResource r2) {
    IFolder f = (IFolder) r1.getParent();
    IFile file = f.getFile("order.xml");

    if (!file.exists()) {
        System.err.println("Error: Missing order.xml when comparing " + r1.getProjectRelativePath().toString());
        return r1.getName().compareTo(r2.getName());
    }

    Document d = GeneralFunctions.readFromXML(file);

    String s1 = r1.getName();
    String s2 = r2.getName();

    XPath xpathSelector = DocumentHelper.createXPath("//sub[name='" + s1 + "']");
    Node node1 = xpathSelector.selectSingleNode(d);

    xpathSelector = DocumentHelper.createXPath("//sub[name='" + s2 + "']");
    Node node2 = xpathSelector.selectSingleNode(d);

    xpathSelector = DocumentHelper.createXPath("//sub");
    List list = xpathSelector.selectNodes(d);
    int i1 = list.indexOf(node1);
    int i2 = list.indexOf(node2);

    return i1 - i2;
}

From source file:lost.tok.SubLink.java

License:Open Source License

/** Creates a sublink from an XML sublink element */
public SubLink(ToK tok, Element sublinkElm) {
    exList = new LinkedList<Excerption>();

    String srcName = DocumentHelper.createXPath("sourceFile").selectSingleNode(sublinkElm).getText(); //$NON-NLS-1$
    this.linkedSource = new Source(tok, srcName);

    SourceDocument srcDoc = new SourceDocument();
    srcDoc.set(linkedSource);//from   w w w. j  a v  a2  s.  c  om

    for (Object exElmObj : DocumentHelper.createXPath("excerption").selectNodes(sublinkElm)) //$NON-NLS-1$
    {
        Element exElm = (Element) exElmObj;
        Excerption excerption = new Excerption(exElm);
        excerption.loadText(srcDoc);
        exList.add(excerption);
    }
}