Example usage for javax.xml.xpath XPathConstants NODE

List of usage examples for javax.xml.xpath XPathConstants NODE

Introduction

In this page you can find the example usage for javax.xml.xpath XPathConstants NODE.

Prototype

QName NODE

To view the source code for javax.xml.xpath XPathConstants NODE.

Click Source Link

Document

The XPath 1.0 NodeSet data type.

Usage

From source file:com.sonyericsson.hudson.plugins.gerrit.trigger.spec.DuplicateGerritListenersPreloadedProjectHudsonTestCase.java

/**
 * Evaluates the xpath expression on the document and returns the node it resolves to.
 *
 * @param doc        the doc to search/*from  w w w. java  2  s .c  o  m*/
 * @param expression the xpath expression
 * @return the node
 * @throws XPathExpressionException if so.
 */
Node xPath(Document doc, String expression) throws XPathExpressionException {
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression expr = xpath.compile(expression);
    return (Node) expr.evaluate(doc, XPathConstants.NODE);
}

From source file:com.mediaworx.xmlutils.XmlHelper.java

/**
 * Retrieves a single node at a given XPath from the given ancestor node.
 * @param ancestorNode the node from which the Node is to be read
 * @param xPath        the XPath (relative to the ancestor node)
 * @return the Node for the given XPath/*from ww  w . j a v a 2s .  co m*/
 * @throws XPathExpressionException if the given XPath can't be evaluated (e.g. because it does not exist or
 *                                  because it does not point to a single node)
 */
public Node getSingleNodeForXPath(Node ancestorNode, String xPath) throws XPathExpressionException {
    XPath xpath = xPathfactory.newXPath();
    return (Node) xpath.evaluate(xPath, ancestorNode, XPathConstants.NODE);
}

From source file:org.eclipse.lyo.testsuite.server.oslcv1tests.ServiceDescriptionTests.java

@Test
public void changeManagementServiceDescriptionHasValidSelectionDialog() throws XPathException {
    //If ServiceDescription is oslc_cm, make sure it has a valid selection dialog child element
    Node cmRequest = (Node) OSLCUtils.getXPath().evaluate("//oslc_cm:changeRequests", doc, XPathConstants.NODE);
    if (cmRequest != null) {
        NodeList sD = (NodeList) OSLCUtils.getXPath()
                .evaluate("//oslc_cm:changeRequests/oslc_cm:selectionDialog", doc, XPathConstants.NODESET);
        for (int i = 0; i < sD.getLength(); i++) {
            Node sQUrl = (Node) OSLCUtils.getXPath().evaluate(
                    "//oslc_cm:changeRequests/oslc_cm:selectionDialog[" + (i + 1) + "]/oslc_cm:url", doc,
                    XPathConstants.NODE);
            assertNotNull(sQUrl);//from  w  ww .  ja  va  2 s  .  c  om
            Node sDtitle = (Node) OSLCUtils.getXPath().evaluate(
                    "//oslc_cm:changeRequests/oslc_cm:selectionDialog[" + (i + 1) + "]/dc:title", doc,
                    XPathConstants.NODE);
            assertNotNull(sDtitle);
            assertFalse(sDtitle.getTextContent().isEmpty());
        }
    }
}

From source file:com.baracuda.piepet.nexusrelease.nexus.StageClient.java

/**
 * Retrieve the Nexus servers version./*from ww  w.  j a  v a  2 s  .  c o m*/
 * 
 * @return the String representation of the server version.
 * @throws StageException if we could not obtain the nexus server version.
 */
protected String getServerVersion() throws StageException {
    if (nexusVersion == null) {
        try {
            URL url = new URL(nexusURL, "service/local/status");
            Document doc = getDocument(url);
            Node node = (Node) evaluateXPath("//version", doc, XPathConstants.NODE);
            if (node == null) {
                throw new StageException(
                        "Invalid reponse from server - is the URL a Nexus Professional server?");
            }
            nexusVersion = node.getTextContent();
            log.debug("This nexus server has version: {}", nexusVersion);
            return nexusVersion;
        } catch (MalformedURLException ex) {
            throw createStageExceptionForIOException(nexusURL, ex);
        }
    }
    return nexusVersion;
}

From source file:de.ingrid.iplug.opensearch.converter.IngridRSSConverter.java

/**
 * Get the total number of hits./* ww  w  . j a va 2  s. com*/
 * 
 * @param doc
 * @return
 * @throws XPathExpressionException
 */
private int getTotalResults(Document doc) throws XPathExpressionException {
    XPath xpath = XPathFactory.newInstance().newXPath();
    Node node = (Node) xpath.evaluate("/rss/channel/totalResults", doc, XPathConstants.NODE);
    if (node.getTextContent() == "") {
        return 0;
    } else {
        return Integer.valueOf(node.getTextContent());
    }
}

From source file:com.vimeo.VimeoUploadClient.java

/**
 * call method//from   w w w .  j a  v  a  2  s  .c  o m
 * 
 * @param method
 * @param params
 * @return
 */
private ResponseWrapper call(String method, Map<String, String> params) throws APIException {
    if (verbose)
        System.out.println("Calling method: \"" + method + "\"");
    OAuthRequest orequest = new OAuthRequest(Verb.GET, VimeoUploadClient.ENDPOINT);
    orequest.addQuerystringParameter("method", method);
    if (params != null) {
        for (Map.Entry<String, String> p : params.entrySet()) {
            orequest.addQuerystringParameter(p.getKey(), p.getValue());
        }
    }
    service.signRequest(accessToken, orequest);
    Response response = orequest.send();
    if (verbose)
        System.out.println(response.getBody());
    /*
     * NodeList nodes = (NodeList) path(response.getBody(), "//username",
     * XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++)
     * { System.out.println(nodes.item(i).getTextContent()); }
     */

    NamedNodeMap nodeMap3 = ((Node) path(response.getBody(), "//rsp", XPathConstants.NODE)).getAttributes();
    String stat = nodeMap3.getNamedItem("stat").getNodeValue();

    if (stat.equals("fail")) {
        System.out.println(" response " + response);
        throw new APIException(method, response);
    }

    return new ResponseWrapper(response, stat);
}

From source file:gov.niem.ws.util.SecurityUtil.java

public static PublicKey getSignaturePublicKey(Document assertion)
        throws ParserConfigurationException, SAXException, IOException {
    Node keyInfoNode = null;/*from w w w .jav  a 2 s .  c o  m*/
    try {
        keyInfoNode = (Node) signatureKeyInfoPath.evaluate(assertion, XPathConstants.NODE);
    } catch (XPathExpressionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
    if (keyInfoNode == null) {
        return null;
    }

    X509Certificate signatureCert = getCertificateFromKeyInfo(keyInfoNode);
    if (signatureCert != null) {
        return signatureCert.getPublicKey();
    }

    return getPublicKeyFromKeyInfo(keyInfoNode);
}

From source file:es.juntadeandalucia.panelGestion.negocio.utiles.geosearch.Geosearch.java

private static boolean checkCoreCreationResponse(String createCoreResponse, String coreName)
        throws GeosearchException {
    boolean createdCore = false;

    String errorMessage = null;//from  ww w .  ja  va 2s . c  o  m

    try {
        // parses the response string as XML
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document createCoreResponseXML = db
                .parse(new ByteArrayInputStream(createCoreResponse.getBytes("UTF-8")));

        XPath xpath = XPathFactory.newInstance().newXPath();

        // gets the status, saved and the core nodes
        Node statusNode = (Node) xpath.compile("/response/lst/int[@name='status']")
                .evaluate(createCoreResponseXML, XPathConstants.NODE);
        Node coreNode = (Node) xpath.compile("/response/str[@name='core']").evaluate(createCoreResponseXML,
                XPathConstants.NODE);
        Node savedNode = (Node) xpath.compile("/response/str[@name='saved']").evaluate(createCoreResponseXML,
                XPathConstants.NODE);

        // checks if the status node has the correct value
        String statusString = statusNode.getTextContent();
        createdCore = (Integer.parseInt(statusString) == GEOSEARCH_RESPONSE_OK_CODE);

        // checks if the core node has the same value as the created core name
        String createdCoreName = coreNode.getTextContent();
        createdCore = (createdCore && coreName.equals(createdCoreName));

        // checks if the saved node is present
        createdCore = (createdCore && (savedNode != null));
    } catch (ParserConfigurationException e) {
        errorMessage = "Error al parsear el XML de respuesta";
    } catch (UnsupportedEncodingException e) {
        errorMessage = "Error en la codificacin del XML de respuesta";
    } catch (SAXException e) {
        errorMessage = "Error al parsear el XML de respuesta";
    } catch (IOException e) {
        errorMessage = "Error al parsear el XML de respuesta";
    } catch (XPathExpressionException e) {
        errorMessage = "Respuesta de Geobsquedas vaca o invlida";
    }

    if (errorMessage != null) {
        throw new GeosearchException(errorMessage);
    }

    return createdCore;
}

From source file:com.redhat.plugin.eap6.EAP6DeploymentStructureMojo.java

protected void buildDeploymentStructure(Document doc, Map<Artifact, String> moduleMap,
        List<SubDeployment> subdeployments) throws MojoFailureException, XPathExpressionException {
    Element root = doc.getDocumentElement();
    if (!root.getTagName().equals("jboss-deployment-structure"))
        throw new MojoFailureException("Root element is not jboss-deployment-structure");

    Element deployment = (Element) xp_deployment.evaluate(doc, XPathConstants.NODE);
    if (deployment == null) {
        deployment = doc.createElement("deployment");
        root.insertBefore(deployment, root.getFirstChild());
    }//from   w  w w .j  a va  2 s .com

    Element depDependencies = (Element) xp_dependencies.evaluate(deployment, XPathConstants.NODE);
    if (depDependencies == null) {
        depDependencies = doc.createElement("dependencies");
        deployment.appendChild(depDependencies);
    }

    Collection<String> mods = moduleMap.values();
    getLog().debug("From project-dependencies" + mods);
    fillModuleEntries(doc, depDependencies, mods);
    getLog().debug("Element <" + depDependencies.getTagName() + ">: "
            + depDependencies.getChildNodes().getLength() + " elements");

    if (subdeployments != null && !subdeployments.isEmpty()) {
        for (SubDeployment sd : subdeployments) {
            XPathExpression xp = xpf.newXPath()
                    .compile("/jboss-deployment-structure/sub-deployment [@name='" + sd.getName() + "']");
            Element subEl = (Element) xp.evaluate(doc, XPathConstants.NODE);
            if (subEl == null) {
                getLog().debug("Creating sub-deployment-section for <" + sd.getName() + ">");
                subEl = doc.createElement("sub-deployment");
                root.appendChild(subEl);
                subEl.setAttribute("name", sd.getName());
            }
            Element subDependencies = (Element) xp_dependencies.evaluate(subEl, XPathConstants.NODE);
            if (subDependencies == null) {
                subDependencies = doc.createElement("dependencies");
                subEl.appendChild(subDependencies);
            }
            Set<String> modules = new HashSet<String>();
            xp = xpf.newXPath().compile("/jboss-deployment-structure/deployment/dependencies/module/@name");
            NodeList nl = (NodeList) xp.evaluate(sd.getDocument(), XPathConstants.NODESET);
            int n = nl.getLength();
            for (int i = 0; i < n; i++) {
                if (moduleMap.values().contains(nl.item(i).getTextContent()))
                    continue;
                modules.add(nl.item(i).getTextContent());
            }
            getLog().debug("From sub-deployment <" + sd.getName() + ">:" + modules);
            fillModuleEntries(doc, subDependencies, modules);
            getLog().debug("Child-Elements for <" + subEl.getAttribute("name") + ">: "
                    + subEl.getChildNodes().getLength());
            getLog().debug("Element <" + subEl.getTagName() + "." + subDependencies.getTagName() + ">: "
                    + subDependencies.getChildNodes().getLength() + " elements");

        }
    }
    NodeList nlSub = (NodeList) xp_subdeployment.evaluate(doc, XPathConstants.NODESET);
    int nSub = nlSub.getLength();
    getLog().debug("Retrieved subdeployment-sections (" + nSub + ")");
}

From source file:es.juntadeandalucia.panelGestion.negocio.utiles.geosearch.GeosearchDataImportWriter.java

private Node getDataConfigNode() throws XPathExpressionException {
    Node dataConfig = null;//from  ww  w .  j ava2s .  c  o m

    dataConfig = (Node) xpath.compile("/dataConfig").evaluate(dataImportXML, XPathConstants.NODE);
    if (dataConfig == null) {
        dataConfig = this.dataImportXML.createElement("dataConfig");
        this.dataImportXML.appendChild(dataConfig);
    }

    return dataConfig;
}