Example usage for org.dom4j XPath getText

List of usage examples for org.dom4j XPath getText

Introduction

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

Prototype

String getText();

Source Link

Document

getText will return the textual version of the XPath expression.

Usage

From source file:com.ten45.service.aggregator.ConfigurationServiceImpl.java

/**
 * Expand the original configuration document with the referred node.
 * It reads the referred document, finds the referred element in the same 
 * path as the original document, and moves all the children of the 
 * referred element into the original element.
 * //from ww  w.ja  v  a2 s  .co m
 * @param doc
 * @param node
 * @return
 * @throws DocumentException
 */
private Document expandReference(Document doc, Node node) throws DocumentException {
    // Find the 'anchor' element that contains the reference declaration.
    Element anchor = node.getParent();
    XPath anchorXPath = DocumentHelper.createXPath(anchor.getPath());

    // Remove the reference declaration node from the document.
    node.detach();

    // Read the new configuration.
    String resourceName = rootPath + node.getText() + ".xml";
    log.debug("Reading resource " + resourceName);
    InputStream in = this.getClass().getResourceAsStream(resourceName);
    SAXReader reader = new SAXReader();
    try {
        Document refDoc = reader.read(in);

        Element refElement = (Element) anchorXPath.selectSingleNode(refDoc);
        if (refElement != null) {
            log.debug("Expanding " + anchorXPath.getText() + " with " + refElement.asXML());
            // Move all elements from the referenced document into the anchor.
            List children = refElement.elements();
            if (children != null && children.size() > 0) {
                for (int i = 0; i < children.size(); i++) {
                    Element child = (Element) children.get(i);
                    XPath refXPath = DocumentHelper.createXPath(child.getPath());
                    if (refXPath.selectSingleNode(doc) == null) {
                        log.debug("Adding element " + refXPath.getText());
                        child.detach();
                        anchor.add(child);
                    } else {
                        log.debug("Ignore pre-existing element " + refXPath.getText());
                    }
                }
            }
        }
    } catch (DocumentException de) {
        throw de;
    }
    return doc;
}

From source file:org.gbif.portal.util.mhf.message.impl.xml.XMLMessage.java

License:Open Source License

/**
 * Gets a the value as a string for the given XPath
 *
 * @param location Xpath to evaluate/*from   w w  w.  jav  a2s.com*/
 * @return The value
 */
@SuppressWarnings("unchecked")
public String getPartAsString(Object location) throws MessageAccessException {
    if (!(location instanceof XPath)) {
        throw new MessageAccessException("Only XPath location's are supported for accessing XMLMessage parts - "
                + "received: " + location.getClass());
    }
    XPath xpath = (XPath) location;

    String key = GET_PART_AS_STRING_KEY_PREFIX + xpath.getText();
    if (getCache().containsKey(key)) {
        return (String) getCache().get(key);
    } else {
        String result = xpath.valueOf(getDocument());
        // Do not do this - it does not evaluate expressions returning "false"
        // for example
        // String result = xpath.selectSingleNode(getDocument()).getText();
        getCache().put(key, result);
        return result;
    }
}

From source file:org.gbif.portal.util.mhf.message.impl.xml.XMLMessage.java

License:Open Source License

/**
 * Gets a list of Strings for the multiple values that will be
 * returned from the XPath//  w  ww .j  a  v  a 2  s .  c  om
 *
 * @param location Xpath to evaluate
 * @return The List of Node
 */
@SuppressWarnings("unchecked")
public List<String> getPartsAsString(Object location) throws MessageAccessException {
    if (!(location instanceof XPath)) {
        throw new MessageAccessException("Only XPath location's are supported for accessing XMLMessage parts - "
                + "received: " + location.getClass());
    }
    XPath xpath = (XPath) location;
    String key = GET_PARTS_AS_STRING_KEY_PREFIX + xpath.getText();
    if (getCache().containsKey(key)) {
        return (List<String>) getCache().get(key);
    } else {
        List<Node> result = (List<Node>) xpath.selectNodes(getDocument());
        List<String> resultsAsString = new LinkedList<String>();
        for (Node node : result) {
            resultsAsString.add(node.getText());
        }
        getCache().put(key, resultsAsString);
        return resultsAsString;
    }
}

From source file:org.gbif.portal.util.mhf.message.impl.xml.XMLMessage.java

License:Open Source License

/**
 * Gets a list of MessageIndex//from  w ww . j a  va2  s  .  c o m
 * returned from the XPath
 *
 * @param location Xpath to evaluate
 * @return The List of Node
 */
@SuppressWarnings("unchecked")
public List<Message> getParts(Object location) throws MessageAccessException, MessageParseException {
    if (!(location instanceof XPath)) {
        throw new MessageAccessException("Only XPath location's are supported for accessing XMLMessage parts - "
                + "received: " + location.getClass());
    }
    XPath xpath = (XPath) location;

    String key = GET_PARTS_KEY_PREFIX + xpath.getText();
    if (getCache().containsKey(key)) {
        return (List<Message>) getCache().get(key);
    } else {
        List<Node> result = (List<Node>) xpath.selectNodes(getDocument());
        List<Message> resultsAsIndex = new LinkedList<Message>();
        for (Node node : result) {
            Message indexedResult = new XMLMessageFactory().build(node, true);
            resultsAsIndex.add(indexedResult);
        }
        getCache().put(key, resultsAsIndex);
        return resultsAsIndex;
    }
}

From source file:org.gbif.portal.util.mhf.message.impl.xml.XMLMessage.java

License:Open Source License

/**
 * Gets a list of MessageIndex//  w  ww . j a  va  2  s.  com
 * returned from the XPath
 *
 * @param location Xpath to evaluate
 * @return The List of Node
 */
@SuppressWarnings("unchecked")
public Message getPart(Object location) throws MessageAccessException, MessageParseException {
    if (!(location instanceof XPath)) {
        throw new MessageAccessException("Only XPath location's are supported for accessing XMLMessage parts - "
                + "received: " + location.getClass());
    }
    XPath xpath = (XPath) location;

    String key = GET_PART_KEY_PREFIX + xpath.getText();
    if (getCache().containsKey(key)) {
        return (Message) getCache().get(key);
    } else {
        Node result = xpath.selectSingleNode(getDocument());
        Message indexedResult = new XMLMessageFactory().build(result, true);
        getCache().put(key, indexedResult);
        return indexedResult;
    }
}