Example usage for org.dom4j XPath selectSingleNode

List of usage examples for org.dom4j XPath selectSingleNode

Introduction

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

Prototype

Node selectSingleNode(Object context);

Source Link

Document

selectSingleNode evaluates this XPath expression on the given Node or List of Node s and returns the result as a single Node instance.

Usage

From source file:org.codehaus.modello.plugin.utils.Dom4jUtils.java

License:Apache License

/**
 * Asserts <b>non-existence</b> of an element specified by an XPATH
 * expression in the target {@link Document}.
 * //from ww w . j  ava2  s  . c om
 * @param doc target {@link Document} instance where the element is to be
 *            verified.
 * @param xpathToNode XPATH expression to locate the element.
 */
public static void assertElementNotExists(Document doc, String xpathToNode) {
    if (StringUtils.isEmpty(xpathToNode)) {
        throw new AssertionFailedError("Unable to assert an attribute using an empty xpath.");
    }

    if (doc == null) {
        throw new AssertionFailedError("Unable to assert an attribute using a null document.");
    }

    XPath xpath = doc.createXPath(xpathToNode);

    Node node = xpath.selectSingleNode(doc);

    if (node != null) {
        throw new AssertionFailedError("Element at '" + xpathToNode + "' should not exist.");
    }

    // In case node returns something other than an element.
}

From source file:org.codehaus.modello.plugin.utils.Dom4jUtils.java

License:Apache License

/**
 * Locates and returns ther {@link Element} specified by a passed in XPATH
 * expression in a target {@link Document}.
 * /*  www.  j a  v  a2  s.  c  om*/
 * @param doc target {@link Document} instance where the element is to be
 *            located.
 * @param xpathToNode XPATH expression to locate the element.
 * @return {@link Element} instance that matches the XPATH expression. An
 *         {@link AssertionFailedError} is thrown if no matching Element was
 *         found.
 */
public static Element findElement(Document doc, String xpathToNode) {
    if (StringUtils.isEmpty(xpathToNode)) {
        throw new AssertionFailedError("Unable to assert an attribute using an empty xpath.");
    }

    if (doc == null) {
        throw new AssertionFailedError("Unable to assert an attribute using a null document.");
    }

    XPath xpath = doc.createXPath(xpathToNode);

    Node node = xpath.selectSingleNode(doc);

    if (node == null) {
        throw new AssertionFailedError("Expected Node at '" + xpathToNode + "', but was not found.");
    }

    if (node.getNodeType() != Node.ELEMENT_NODE) {
        throw new AssertionFailedError("Node at '" + xpathToNode + "' is not an xml element.");
    }

    return (Element) node;
}

From source file:org.craftercms.core.util.XmlUtils.java

License:Open Source License

/**
 * Executes the specified namespace aware XPath query as a single node query, returning the resulting single node.
 *//*from  w  w  w.  java  2 s.  c o  m*/
public static Node selectSingleNode(Node node, String xpathQuery, Map<String, String> namespaceUris) {
    XPath xpath = DocumentHelper.createXPath(xpathQuery);
    xpath.setNamespaceURIs(namespaceUris);

    return xpath.selectSingleNode(node);
}

From source file:org.danann.cernunnos.xml.SingleNodePhrase.java

License:Apache License

public Object evaluate(TaskRequest req, TaskResponse res) {

    Node src = (Node) source.evaluate(req, res);
    final String xpathExpresion = (String) this.xpath.evaluate(req, res);
    final XPath xpath = this.xpathCache.getCachedObject(req, res, xpathExpresion, XPathCacheFactory.INSTANCE);
    try {/*from   w w w. jav a 2s  .  c o m*/
        xpath.setVariableContext(new RequestVariableContext(req));
        return xpath.selectSingleNode(src);
    } finally {
        xpath.setVariableContext(null);
    }
}

From source file:org.etudes.component.app.melete.MeleteImportServiceImpl.java

License:Apache License

/**
 * Parses the manifest and build modules
 *
 * @param document document//from  ww w  .  j a  v  a 2 s .  c om
 * @param unZippedDirPath unZipped fiels Directory Path
 * @exception throws exception
 */
public void parseAndBuildModules(Document document, String unZippedDirPath) throws Exception {
    if (logger.isDebugEnabled())
        logger.debug("Entering parseAndBuildModules");

    Map uris = new HashMap();
    uris.put("imscp", DEFAULT_NAMESPACE_URI);
    uris.put("imsmd", IMSMD_NAMESPACE_URI);

    // organizations
    XPath xpath = document.createXPath("/imscp:manifest/imscp:organizations/imscp:organization");
    xpath.setNamespaceURIs(uris);

    Element eleOrg = (Element) xpath.selectSingleNode(document);

    // build module
    // loop thru organization elements - item elements
    if (eleOrg != null) {
        importThreadLocal.set("MeleteIMSImportFiles", new HashSet<String>());
        List elements = eleOrg.elements();
        for (Iterator iter = eleOrg.elementIterator("item"); iter.hasNext();) {
            Element element = (Element) iter.next();
            // for moodle packages which don't have parent item tag for modules.
            Element blankElement = checkModuleItem(element, eleOrg);
            if (blankElement != null) {
                buildModule(blankElement, document, unZippedDirPath,
                        ToolManager.getCurrentPlacement().getContext());
                break;
            } else
                buildModule(element, document, unZippedDirPath, ToolManager.getCurrentPlacement().getContext());
        }
    }

    xpath = document.createXPath("/imscp:manifest/imscp:resources");
    xpath.setNamespaceURIs(uris);

    Element eleResource = (Element) xpath.selectSingleNode(document);
    if (eleResource != null)
        processManageItems(eleResource, unZippedDirPath, ToolManager.getCurrentPlacement().getContext(),
                document);

    if (logger.isDebugEnabled())
        logger.debug("Exiting parseAndBuildModules");
}

From source file:org.etudes.component.app.melete.MeleteImportServiceImpl.java

License:Apache License

private Element getResource(String resName, Document document) throws Exception {
    if (logger.isDebugEnabled())
        logger.debug("Entering getResource...");

    Map uris = new HashMap();
    uris.put("imscp", DEFAULT_NAMESPACE_URI);
    uris.put("imsmd", IMSMD_NAMESPACE_URI);

    //resource//from  w ww  .  java 2 s.c  o  m
    XPath xpath = document
            .createXPath("/imscp:manifest/imscp:resources/imscp:resource[@identifier = '" + resName + "']");
    xpath.setNamespaceURIs(uris);

    Element eleRes = (Element) xpath.selectSingleNode(document);

    if (logger.isDebugEnabled())
        logger.debug("Exiting getResource...");

    return eleRes;
}

From source file:org.etudes.component.app.melete.MeleteImportServiceImpl.java

License:Apache License

private Element getMergeResource(String resName, Document document) throws Exception {
    if (logger.isDebugEnabled())
        logger.debug("Entering getResource...");

    Map uris = new HashMap();
    uris.put("imscp", DEFAULT_NAMESPACE_URI);
    uris.put("imsmd", IMSMD_NAMESPACE_URI);

    // resource/*from w ww . j  a  v  a  2s.  c om*/
    XPath xpath = document.createXPath("//resource[@identifier = '" + resName + "']");
    xpath.setNamespaceURIs(uris);

    Element eleRes = (Element) xpath.selectSingleNode(document);

    if (logger.isDebugEnabled())
        logger.debug("Exiting getResource...");

    return eleRes;
}

From source file:org.etudes.component.app.melete.MeleteImportServiceImpl.java

License:Apache License

public String getContentSourceInfo(Document document) {
    Map uris = new HashMap();
    uris.put("imscp", DEFAULT_NAMESPACE_URI);
    uris.put("imsmd", IMSMD_NAMESPACE_URI);

    try {// w w w  . jav a 2 s. co m
        // description
        XPath xpath = document
                .createXPath("/imscp:manifest/imscp:metadata/imsmd:lom/imsmd:rights/imsmd:description");
        xpath.setNamespaceURIs(uris);

        Element eleOrg = (Element) xpath.selectSingleNode(document);
        if (eleOrg != null) {
            // logger.debug("got desc element" + eleOrg.toString());
            return eleOrg.selectSingleNode(".//imsmd:langstring").getText();
        } else
            return null;
    } catch (Exception e) {
        logger.debug("error in reading other contact info" + e.toString());
        return null;
    }
}

From source file:org.etudes.jforum.view.admin.ImportExportAction.java

License:Apache License

/**
 * Parses the manifest and build topics// ww  w  .  j a v  a  2  s .c o  m
 * 
 * @param document
 *            document
 * @param unZippedDirPath
 *            unZipped files Directory Path
 * @exception throws exception
 */
public void parseAndCreateTopics(Document document, String unZippedDirPath) throws Exception {
    if (logger.isDebugEnabled())
        logger.debug("Entering parseAndCreateTopics");

    boolean isfacilitator = JForumUserUtil.isJForumFacilitator(UserDirectoryService.getCurrentUser().getId())
            || SecurityService.isSuperUser();

    if (!isfacilitator) {
        this.context.put("message", I18n.getMessage("User.NotAuthorizedToManage"));
        this.setTemplateName(TemplateKeys.MANAGE_NOT_AUTHORIZED);
        return;
    }

    // get current site
    Site site = SiteService.getSite(ToolManager.getCurrentPlacement().getContext());
    Collection sakaiSiteGroups = site.getGroups();

    Map uris = new HashMap();
    uris.put("imscp", DEFAULT_NAMESPACE_URI);
    uris.put("imsmd", IMSMD_NAMESPACE_URI);

    // organizations
    XPath xpath = document.createXPath("/imscp:manifest/imscp:organizations/imscp:organization");
    xpath.setNamespaceURIs(uris);

    Element eleOrg = (Element) xpath.selectSingleNode(document);

    // create category, forum if not existing and then add task topics of
    // the forum
    // loop thru organization elements - item elements(category)
    List elements = eleOrg.elements();
    for (Iterator iter = elements.iterator(); iter.hasNext();) {
        Element element = (Element) iter.next();
        if (element.getName().equalsIgnoreCase("item")) {
            processXML(element, document, unZippedDirPath);
        }

    }

    if (logger.isDebugEnabled())
        logger.debug("Exiting parseAndCreateTopics");
}

From source file:org.etudes.jforum.view.admin.ImportExportAction.java

License:Apache License

/**
 * gets the resource element//from   w w  w.  jav  a 2  s.  com
 * 
 * @param resName
 *            resource name
 * @param document
 *            document
 * @return resource element
 * @throws Exception
 */
private Element getResource(String resName, Document document) throws Exception {
    if (logger.isDebugEnabled())
        logger.debug("Entering getResource...");

    Map uris = new HashMap();
    uris.put("imscp", DEFAULT_NAMESPACE_URI);
    uris.put("imsmd", IMSMD_NAMESPACE_URI);

    // resource
    XPath xpath = document
            .createXPath("/imscp:manifest/imscp:resources/imscp:resource[@identifier = '" + resName + "']");
    xpath.setNamespaceURIs(uris);

    Element eleRes = (Element) xpath.selectSingleNode(document);

    if (logger.isDebugEnabled())
        logger.debug("Exiting getResource...");

    return eleRes;
}