Example usage for org.dom4j XPath setNamespaceURIs

List of usage examples for org.dom4j XPath setNamespaceURIs

Introduction

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

Prototype

void setNamespaceURIs(Map<String, String> map);

Source Link

Document

Sets the current NamespaceContext from a Map where the keys are the String namespace prefixes and the values are the namespace URIs.

Usage

From source file:com.collabnet.ccf.core.utils.XPathUtils.java

License:Open Source License

/**
 * Extracts the value of the supplied attribute
 * /*from   www.  j a  va 2s  . c  o  m*/
 * @param element
 *            element with attribute in question
 * @param attributeName
 *            name of the attribute in question
 * @param failIfNotFound
 *            determines if exception should be thrown if attribute is not
 *            found
 * @return value of the attribute in question, null if not found and
 *         failIfNotFound is set to false
 * @throws GenericArtifactParsingException
 *             exception thrown is attribute is missing and failIfNotFound
 *             is set to true
 */
public static String getAttributeValue(Element element, String attributeName, boolean failIfNotFound)
        throws GenericArtifactParsingException {
    XPath xpath = new DefaultXPath("@" + attributeName);
    xpath.setNamespaceURIs(ccfNamespaceMap);
    Node attributeNode = xpath.selectSingleNode(element);
    if (attributeNode == null) {
        if (failIfNotFound) {
            throw new GenericArtifactParsingException(
                    "Missing attribute: " + attributeName + " in element " + element.getName());
        } else {
            return null;
        }
    } else {
        return attributeNode.getText();
    }
}

From source file:com.digiaplus.modules.scorm.ScormCPManifestTreeModel.java

License:Apache License

/**
 * Constructor of the content packaging tree model
 * @param manifest the imsmanifest.xml file
 * @param itemStatus a Map containing the status of each item like "completed, not attempted, ..."
 */// w  w  w.  j  a va 2 s  . c  o m
public ScormCPManifestTreeModel(File manifest, Map itemStatus) {
    this.itemStatus = itemStatus;
    Document doc = loadDocument(manifest);
    // get all organization elements. need to set namespace
    rootElement = doc.getRootElement();
    String nsuri = rootElement.getNamespace().getURI();
    nsuris.put("ns", nsuri);

    XPath meta = rootElement.createXPath("//ns:organization");
    meta.setNamespaceURIs(nsuris);

    XPath metares = rootElement.createXPath("//ns:resources");
    metares.setNamespaceURIs(nsuris);
    Element elResources = (Element) metares.selectSingleNode(rootElement);
    if (elResources == null)
        throw new DAPRuntimeException(this.getClass(), "could not find element resources");

    List resourcesList = elResources.elements("resource");
    resources = new HashMap(resourcesList.size());
    for (Iterator iter = resourcesList.iterator(); iter.hasNext();) {
        Element elRes = (Element) iter.next();
        String identVal = elRes.attributeValue("identifier");
        String hrefVal = elRes.attributeValue("href");
        if (hrefVal != null) { // href is optional element for resource element
            try {
                hrefVal = URLDecoder.decode(hrefVal, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                // each JVM must implement UTF-8
            }
        }
        resources.put(identVal, hrefVal);
    }
    /*
     * Get all organizations
     */
    List organizations = new LinkedList();
    organizations = meta.selectNodes(rootElement);
    if (organizations.isEmpty()) {
        throw new DAPRuntimeException(this.getClass(), "could not find element organization");
    }
    GenericTreeNode gtn = buildTreeNodes(organizations);
    setRootNode(gtn);
    rootElement = null; // help gc
    resources = null;
}

From source file:com.digiaplus.modules.scorm.ScormCPManifestTreeModel.java

License:Apache License

private GenericTreeNode buildNode(Element item) {
    GenericTreeNode treeNode = new GenericTreeNode();

    // extract title
    String title = item.elementText("title");
    if (title == null)
        title = item.attributeValue("identifier");
    treeNode.setAltText(title);//from w w  w.  j a v  a 2 s .  c o  m
    treeNode.setTitle(title);

    if (item.getName().equals("organization")) {
        treeNode.setIconCssClass("o_scorm_org");
        treeNode.setAccessible(false);
    } else if (item.getName().equals("item")) {
        scormIdToNode.put(new Integer(nodeId).toString(), treeNode);
        nodeToId.put(treeNode, new Integer(nodeId));

        //set node images according to scorm sco status
        String itemStatusDesc = (String) itemStatus.get(Integer.toString(nodeId));
        treeNode.setIconCssClass("o_scorm_item");
        if (itemStatusDesc != null) {
            // add icon decorator for current status
            treeNode.setIconDecorator1CssClass("o_scorm_" + itemStatusDesc);
        }

        nodeId++;
        //set resolved file path directly
        String identifierref = item.attributeValue("identifierref");
        XPath meta = rootElement.createXPath("//ns:resource[@identifier='" + identifierref + "']");
        meta.setNamespaceURIs(nsuris);
        String href = (String) resources.get(identifierref);
        if (href != null) {
            treeNode.setUserObject(href);
            // allow lookup of a treenode given a href so we can quickly adjust the menu if the user clicks on hyperlinks within the text
            hrefToTreeNode.put(href, treeNode);
        } else
            treeNode.setAccessible(false);
    }

    List chds = item.elements("item");
    int childcnt = chds.size();
    for (int i = 0; i < childcnt; i++) {
        Element childitem = (Element) chds.get(i);
        GenericTreeNode gtnchild = buildNode(childitem);
        treeNode.addChild(gtnchild);
    }
    return treeNode;
}

From source file:com.liferay.alloy.tools.builder.taglib.TagBuilder.java

License:Open Source License

protected Document mergeTlds(Document sourceDoc, Document targetDoc) {
    Element targetRoot = targetDoc.getRootElement();

    DocumentFactory factory = SAXReaderUtil.getDocumentFactory();

    XPath xpathTags = factory.createXPath("//tld:tag");

    Map<String, String> namespaceContextMap = new HashMap<String, String>();

    namespaceContextMap.put(_TLD_XPATH_PREFIX, _TLD_XPATH_URI);

    NamespaceContext namespaceContext = new AlloyGeneratorNamespaceContext(namespaceContextMap);

    xpathTags.setNamespaceContext(namespaceContext);

    List<Node> sources = xpathTags.selectNodes(sourceDoc);

    for (Node source : sources) {
        Element sourceElement = (Element) source;

        String sourceName = sourceElement.elementText("name");

        String xpathTagValue = "//tld:tag[tld:name='" + sourceName + "']";

        XPath xpathTag = factory.createXPath(xpathTagValue);

        xpathTag.setNamespaceContext(namespaceContext);

        List<Node> targets = xpathTag.selectNodes(targetDoc);

        if (targets.size() > 0) {
            Element targetElement = (Element) targets.get(0);

            XPath xpathAttributes = factory.createXPath(xpathTagValue + "//tld:attribute");

            Map<String, String> namespaces = new HashMap<String, String>();

            namespaces.put("tld", StringPool.EMPTY);

            xpathAttributes.setNamespaceURIs(namespaces);

            List<Node> sourceAttributes = xpathAttributes.selectNodes(source);

            for (Node sourceAttribute : sourceAttributes) {
                Element sourceAttributeElement = (Element) sourceAttribute;

                String attributeName = sourceAttributeElement.elementText("name");

                String xpathAttributeValue = "//tld:attribute[tld:name='" + attributeName + "']";

                XPath xpathAttribute = factory.createXPath(xpathTagValue + xpathAttributeValue);

                xpathAttribute.setNamespaceContext(namespaceContext);

                Node targetAttribute = xpathAttribute.selectSingleNode(targetElement);

                if (targetAttribute != null) {
                    targetAttribute.detach();
                }/*from  www .  ja  va 2s  .c o m*/

                targetElement.add(sourceAttributeElement.createCopy());
            }

            Element dynamicAttrElement = targetElement.element("dynamic-attributes");

            if (dynamicAttrElement != null) {
                targetElement.add(dynamicAttrElement.detach());
            }
        } else {
            targetRoot.add(sourceElement.createCopy());
        }
    }

    return targetDoc;
}

From source file:com.nokia.ant.Database.java

License:Open Source License

private void readSignals(Element root, String antFile) throws DocumentException, IOException {
    SAXReader xmlReader = new SAXReader();
    Document antDoc = xmlReader.read(new File(antFile));

    XPath xpath = DocumentHelper.createXPath("//hlm:signalListenerConfig");
    xpath.setNamespaceURIs(map);
    List signalNodes = xpath.selectNodes(antDoc);
    for (Iterator iterator = signalNodes.iterator(); iterator.hasNext();) {
        signaldoc = antDoc;//from ww  w . ja  v a 2 s  .com
        Element propertyNode = (Element) iterator.next();
        String signalid = propertyNode.attributeValue("id");

        String signaltarget = propertyNode.attributeValue("target");
        List existinglist = globalSignalList.get(signaltarget);
        String failbuild = signalType(signalid, signaldoc);
        if (existinglist == null)
            existinglist = new ArrayList<String>();
        existinglist.add(signalid + "," + failbuild);
        globalSignalList.put(signaltarget, existinglist);
    }
}

From source file:com.nokia.ant.Database.java

License:Open Source License

private String signalType(String signalid, Document antDoc) {
    XPath xpath2 = DocumentHelper
            .createXPath("//hlm:signalListenerConfig[@id='" + signalid + "']/signalNotifierInput/signalInput");
    xpath2.setNamespaceURIs(map);
    List signalNodes3 = xpath2.selectNodes(antDoc);

    for (Iterator iterator3 = signalNodes3.iterator(); iterator3.hasNext();) {
        Element propertyNode3 = (Element) iterator3.next();
        String signalinputid = propertyNode3.attributeValue("refid");

        XPath xpath3 = DocumentHelper.createXPath("//hlm:signalInput[@id='" + signalinputid + "']");
        xpath3.setNamespaceURIs(map);/* ww w.  j  av a 2 s  .  c o  m*/
        List signalNodes4 = xpath3.selectNodes(antDoc);
        for (Iterator iterator4 = signalNodes4.iterator(); iterator4.hasNext();) {
            Element propertyNode4 = (Element) iterator4.next();
            return propertyNode4.attributeValue("failbuild");
        }
    }
    return null;
}

From source file:com.nokia.helium.ant.data.Database.java

License:Open Source License

@SuppressWarnings("unchecked")
private void readSignals(String antFile) throws IOException {
    SAXReader xmlReader = new SAXReader();
    Document antDoc;//from  ww  w  .  j  a v  a2  s  .  c o m
    try {
        antDoc = xmlReader.read(new File(antFile));
    } catch (DocumentException e) {
        throw new IOException(e.getMessage());
    }

    XPath xpath = DocumentHelper.createXPath("//hlm:signalListenerConfig");
    xpath.setNamespaceURIs(namespaceMap);
    List<Node> signalNodes = xpath.selectNodes(antDoc);
    for (Iterator<Node> iterator = signalNodes.iterator(); iterator.hasNext();) {
        signaldoc = antDoc;
        Element propertyNode = (Element) iterator.next();
        String signalid = propertyNode.attributeValue("id");
        String signaltarget = propertyNode.attributeValue("target");
        List<String> existinglist = globalSignalList.get(signaltarget);
        String failbuild = findSignalFailMode(signalid, signaldoc);
        if (existinglist == null) {
            existinglist = new ArrayList<String>();
        }
        existinglist.add(signalid + "," + failbuild);
        globalSignalList.put(signaltarget, existinglist);
    }
}

From source file:com.nokia.helium.ant.data.Database.java

License:Open Source License

@SuppressWarnings("unchecked")
private String findSignalFailMode(String signalid, Document antDoc) {
    XPath xpath2 = DocumentHelper
            .createXPath("//hlm:signalListenerConfig[@id='" + signalid + "']/signalNotifierInput/signalInput");
    xpath2.setNamespaceURIs(namespaceMap);
    List signalNodes3 = xpath2.selectNodes(antDoc);

    for (Iterator iterator3 = signalNodes3.iterator(); iterator3.hasNext();) {
        Element propertyNode3 = (Element) iterator3.next();
        String signalinputid = propertyNode3.attributeValue("refid");

        XPath xpath3 = DocumentHelper.createXPath("//hlm:signalInput[@id='" + signalinputid + "']");
        xpath3.setNamespaceURIs(namespaceMap);
        List signalNodes4 = xpath3.selectNodes(antDoc);
        for (Iterator iterator4 = signalNodes4.iterator(); iterator4.hasNext();) {
            Element propertyNode4 = (Element) iterator4.next();
            return propertyNode4.attributeValue("failbuild");
        }/*www  . j a va2s  .  co  m*/
    }
    return null;
}

From source file:com.nokia.helium.ant.data.ProjectMeta.java

License:Open Source License

@SuppressWarnings("unchecked")
public void getConfigSignals(String targetName, List<String> signals) {
    XPath xpath = DocumentHelper.createXPath("//hlm:signalListenerConfig[@target='" + targetName + "']");
    xpath.setNamespaceURIs(Database.NAMESPACE_MAP);
    List<Node> signalNodes = xpath.selectNodes(getNode());
    for (Iterator<Node> iterator = signalNodes.iterator(); iterator.hasNext();) {
        Element propertyNode = (Element) iterator.next();
        String signalid = propertyNode.attributeValue("id");
        String failbuild = findSignalFailMode(signalid, getNode().getDocument());
        signals.add(signalid + "(" + failbuild + ")");
    }//www . j  ava2  s. c o m
}

From source file:com.nokia.helium.ant.data.ProjectMeta.java

License:Open Source License

@SuppressWarnings("rawtypes")
private String findSignalFailMode(String signalid, Document antDoc) {
    XPath xpath2 = DocumentHelper
            .createXPath("//hlm:signalListenerConfig[@id='" + signalid + "']/signalNotifierInput/signalInput");
    xpath2.setNamespaceURIs(Database.NAMESPACE_MAP);
    List signalNodes3 = xpath2.selectNodes(antDoc);
    for (Iterator iterator3 = signalNodes3.iterator(); iterator3.hasNext();) {
        Element propertyNode3 = (Element) iterator3.next();
        String signalinputid = propertyNode3.attributeValue("refid");

        XPath xpath3 = DocumentHelper.createXPath("//hlm:signalInput[@id='" + signalinputid + "']");
        xpath3.setNamespaceURIs(Database.NAMESPACE_MAP);
        List signalNodes4 = xpath3.selectNodes(antDoc);
        for (Iterator iterator4 = signalNodes4.iterator(); iterator4.hasNext();) {
            Element propertyNode4 = (Element) iterator4.next();
            return propertyNode4.attributeValue("failbuild");
        }// w w  w  . j a va  2s .co  m
    }
    return null;
}