Example usage for org.dom4j XPath selectNodes

List of usage examples for org.dom4j XPath selectNodes

Introduction

In this page you can find the example usage for org.dom4j XPath selectNodes.

Prototype

List<Node> selectNodes(Object context);

Source Link

Document

selectNodes performs this XPath expression on the given Node or List of Node s instances appending all the results together into a single list.

Usage

From source file:lost.tok.AuthorsHandler.java

License:Open Source License

/**
 * Adding an author to the xml file/*from   ww  w .  ja  v a 2s .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)/*from   w w w  .  j  av  a2s  .co m*/
 */
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//from   ww  w .  ja v  a 2s. 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  a  va  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   ww  w.j a v a 2s. co  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 ww w .j a va2 s . co 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.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//from w w  w.  j  ava 2s .  c  om
 * 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:net.form105.rm.server.ant.container.InboundConfiguration.java

License:Apache License

public void readXml(Node xmlNode) {

    List<Node> nodes;
    XPath xPath;

    inboundType = xmlNode.valueOf("@type");

    xPath = DocumentHelper.createXPath("//inbound/parameters/parameter");
    nodes = (List<Node>) xPath.selectNodes(xmlNode);

    for (Node node : nodes) {
        logger.info(node);//w  w w .java 2  s  .co  m
        paramMap.addParameter(new ConfigParameter(node.valueOf("@key"), node.valueOf("@value")));
    }

    xPath = DocumentHelper.createXPath("//inbound/validators/validator");
    nodes = (List<Node>) xPath.selectNodes(xmlNode);
    for (Node node : nodes) {

        IInboundValidator validator = (IInboundValidator) Agent.getComponentById(node.getText());
        validatorList.add(validator);
    }

    xPath = DocumentHelper.createXPath("//inbound/listeners/listener");
    nodes = (List<Node>) xPath.selectNodes(xmlNode);
    for (Node node : nodes) {
        IInboundListener inboundListener = (IInboundListener) Agent.getComponentById(node.getText());
        listenerList.add(inboundListener);
    }

    xPath = DocumentHelper.createXPath("//inbound/executor");
    logger.info(xPath.selectSingleNode(xmlNode).getText());

    command = (AbstractCallbackCommand) Agent.getComponentById(xPath.selectSingleNode(xmlNode).getText());
}

From source file:net.nikr.eve.jeveasset.io.local.update.Update.java

License:Open Source License

void setVersion(final File xml, final int newVersion) throws DocumentException {
    SAXReader xmlReader = new SAXReader();
    Document doc = xmlReader.read(xml);

    XPath xpathSelector = DocumentHelper.createXPath("/settings");
    List<?> results = xpathSelector.selectNodes(doc);
    for (Iterator<?> iter = results.iterator(); iter.hasNext();) {
        Element element = (Element) iter.next();
        Attribute attr = element.attribute("version");
        if (attr == null) {
            element.add(new DefaultAttribute("version", String.valueOf(newVersion)));
        } else {/*www  .  j  ava 2s  . c o  m*/
            attr.setText(String.valueOf(newVersion));
        }
    }

    try {
        FileOutputStream fos = new FileOutputStream(xml);
        OutputFormat outformat = OutputFormat.createPrettyPrint();
        outformat.setEncoding("UTF-16");
        XMLWriter writer = new XMLWriter(fos, outformat);
        writer.write(doc);
        writer.flush();
    } catch (IOException ioe) {
        LOG.error("Failed to update the serttings.xml version number", ioe);
        throw new RuntimeException(ioe);
    }
}

From source file:net.nikr.eve.jeveasset.io.local.update.updates.Update1To2.java

License:Open Source License

private void convertModes(final Document doc) {
    XPath xpathSelector = DocumentHelper.createXPath("/settings/filters/filter/row");
    List<?> results = xpathSelector.selectNodes(doc);
    for (Iterator<?> iter = results.iterator(); iter.hasNext();) {
        Element elem = (Element) iter.next();
        Attribute attr = elem.attribute("mode");
        String currentValue = attr.getText();
        attr.setText(convertMode(currentValue));
    }/*from  w  ww.j  av  a 2s.  c o  m*/
}