Example usage for javax.xml.xpath XPathExpression evaluate

List of usage examples for javax.xml.xpath XPathExpression evaluate

Introduction

In this page you can find the example usage for javax.xml.xpath XPathExpression evaluate.

Prototype

public Object evaluate(InputSource source, QName returnType) throws XPathExpressionException;

Source Link

Document

Evaluate the compiled XPath expression in the context of the specified InputSource and return the result as the specified type.

Usage

From source file:org.jboss.tools.aerogear.hybrid.android.core.adt.AndroidProjectGenerator.java

private void updateAppName(String appName) throws CoreException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);//ww  w. j  ava 2  s  .c  o  m
    DocumentBuilder db;

    try {
        db = dbf.newDocumentBuilder();
        IPath stringsPath = new Path(getDestination().toString()).append(DIR_RES).append(DIR_VALUES)
                .append(FILE_XML_STRINGS);
        File strings = stringsPath.toFile();
        Document configDocument = db.parse(strings);
        XPath xpath = XPathFactory.newInstance().newXPath();

        try {
            XPathExpression expr = xpath.compile("//string[@name=\"app_name\"]");
            Node node = (Node) expr.evaluate(configDocument, XPathConstants.NODE);
            node.setTextContent(appName);

            configDocument.setXmlStandalone(true);

            Source source = new DOMSource(configDocument);

            StreamResult result = new StreamResult(strings);

            // Write the DOM document to the file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer xformer = transformerFactory.newTransformer();

            xformer.transform(source, result);

        } catch (XPathExpressionException e) {//We continue because this affects the displayed app name
                                              // which is not a show stopper during development
            AndroidCore.log(IStatus.ERROR, "Error when updating the application name", e);
        } catch (TransformerConfigurationException e) {
            AndroidCore.log(IStatus.ERROR, "Error when updating the application name", e);
        } catch (TransformerException e) {
            AndroidCore.log(IStatus.ERROR, "Error when updating the application name", e);
        }

    } catch (ParserConfigurationException e) {
        throw new CoreException(new Status(IStatus.ERROR, HybridCore.PLUGIN_ID,
                "Parser error when parsing /res/values/strings.xml", e));
    } catch (SAXException e) {
        throw new CoreException(
                new Status(IStatus.ERROR, HybridCore.PLUGIN_ID, "Parsing error on /res/values/strings.xml", e));
    } catch (IOException e) {
        throw new CoreException(new Status(IStatus.ERROR, HybridCore.PLUGIN_ID,
                "IO error when parsing /res/values/strings.xml", e));
    }
}

From source file:org.jbpm.designer.server.service.DefaultDesignerAssetServiceTest.java

private Element getProcessElementFromXml(String content) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    ByteArrayInputStream input = new ByteArrayInputStream(content.getBytes("UTF-8"));

    Document xml = builder.parse(input);
    XPathExpression expr = xpath.compile("/definitions/process");
    Element element = (Element) expr.evaluate(xml, XPathConstants.NODE);

    return element;
}

From source file:org.jbpm.designer.server.service.DefaultDesignerAssetServiceTest.java

private Element getMetaDataElementFromXml(String content) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    ByteArrayInputStream input = new ByteArrayInputStream(content.getBytes("UTF-8"));

    Document xml = builder.parse(input);
    XPathExpression expr = xpath.compile("/definitions/process/extensionElements/metaData");
    Element element = (Element) expr.evaluate(xml, XPathConstants.NODE);

    return element;
}

From source file:org.jbpm.designer.server.service.DefaultDesignerAssetServiceTest.java

private Element getDefinitionsElementFromXml(String content) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    ByteArrayInputStream input = new ByteArrayInputStream(content.getBytes("UTF-8"));

    Document xml = builder.parse(input);
    XPathExpression expr = xpath.compile("/definitions");
    Element element = (Element) expr.evaluate(xml, XPathConstants.NODE);

    return element;
}

From source file:org.jembi.rhea.orchestration.QueryPatientGetListOfOpenEMPIPersonIds.java

@Override
public Object onCall(MuleEventContext eventContext) throws Exception {
    MuleMessage msg = eventContext.getMessage();

    String openempiPeopleXml = (String) msg.getPayloadAsString();

    try {/*from   ww  w.  j a  va  2 s.  c om*/
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        Document document = dbf.newDocumentBuilder()
                .parse(new InputSource(new StringReader(openempiPeopleXml)));

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

        XPathExpression expression = xpath.compile("/people/person/personId");
        NodeList patientIdNodeList = (NodeList) expression.evaluate(document, XPathConstants.NODESET);

        String idListStr = "";
        for (int i = 0; i < patientIdNodeList.getLength(); i++) {
            Node node = patientIdNodeList.item(i);
            String id = node.getTextContent();
            if (idListStr.isEmpty()) {
                //idListStr = id;
                idListStr = "personId=" + id;
            } else {
                //idListStr += "," + id;
                idListStr += "&personId=" + id;
            }
        }

        msg.setOutboundProperty("idList", idListStr);

    } catch (SAXParseException s) {
        throw new InvalidOpenEMPIResponseException("Empty or invalid response from OpenEMPI");
    }

    return msg;
}

From source file:org.jembi.rhea.orchestration.SaveEncounterORU_R01ValidatorAndEnricher.java

private String getNodeContent(XPath xpath, Document document, String path) throws XPathExpressionException {
    XPathExpression expression = xpath.compile(path);
    Node givenNameNode = (Node) expression.evaluate(document, XPathConstants.NODE);
    return givenNameNode.getTextContent();
}

From source file:org.josso.tooling.gshell.install.installer.JBossInstaller.java

protected boolean configureRealm(Node serverXmlDom) throws Exception {

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

    // Because we removed all realms, we always add JOSSO realm
    String usersClassNames = "org.josso.gateway.identity.service.BaseUserImpl";
    String roleClassNames = "org.josso.gateway.identity.service.BaseRoleImpl";

    String realmClass = "org.josso.jb5.agent.JBossCatalinaRealm";

    if (getPlatformVersion().startsWith("4.2")) {
        realmClass = "org.josso.jb42.agent.JBossCatalinaRealm";
    } else if (getPlatformVersion().startsWith("4.0")) {
        realmClass = "org.josso.jb4.agent.JBossCatalinaRealm";
    } else if (getPlatformVersion().startsWith("3.2")) {
        realmClass = "org.josso.jb32.agent.JBossCatalinaRealm";
    }/*from ww  w.j av  a2 s  . co  m*/

    // Check if josso agent valve is already present

    XPathExpression findAgentRealm = xpath
            .compile("/Server/Service/Engine/Realm[@className=\"" + realmClass + "\"]");
    NodeList agentRealms = (NodeList) findAgentRealm.evaluate(serverXmlDom, XPathConstants.NODESET);

    // If we already have a JOSSO Valve, do nothing!
    if (agentRealms != null && agentRealms.getLength() > 0) {
        for (int i = 0; i < agentRealms.getLength(); i++) {
            Node valve = agentRealms.item(i);
            Node valveClassNode = valve.getAttributes().getNamedItem("className");
            getPrinter().printActionWarnStatus("Configure", "JOSSO JASS Realm", "Already configured : "
                    + (valveClassNode != null ? valveClassNode.getNodeValue() : "<unknown>"));
        }
        return false;
    }

    XPathExpression findRealmsExpr = xpath.compile("/Server/Service/Engine/Realm");
    NodeList realms = (NodeList) findRealmsExpr.evaluate(serverXmlDom, XPathConstants.NODESET);

    if (realms != null && realms.getLength() > 0) {
        String qryModifications = "\n<xupdate:remove select=\"/Server/Service/Engine/Realm\"/>";
        String qry = XUpdateUtil.XUPDATE_START + qryModifications + XUpdateUtil.XUPDATE_END;
        log.debug("XUPDATE QUERY: \n" + qry);
        XUpdateQuery xq = new XUpdateQueryImpl();
        xq.setQString(qry);
        xq.execute(serverXmlDom);

        for (int i = 0; i < realms.getLength(); i++) {
            Node realmDom = realms.item(i);
            Node className = realmDom.getAttributes().getNamedItem("className");
            getPrinter().printActionOkStatus("Removed", "Tomcat default Realm ",
                    (className != null ? className.getNodeValue() : "<unknown>"));
        }
    }

    String appendJossoRealmQryMod = "\n\t<xupdate:insert-before select=\"//Server/Service/Engine/Host[1]\" >"
            + "\n\t<xupdate:comment>" + " ================================================== \n"
            + "</xupdate:comment>" + "\n\t<xupdate:comment>"
            + "   JOSSO JAAS Realm, configuration automatially generated by JOSSO Installer\n"
            + "</xupdate:comment>" + "\n\t<xupdate:element name=\"Realm\">"
            + "<xupdate:attribute name=\"appName\">josso</xupdate:attribute>"
            + "<xupdate:attribute name=\"debug\">1</xupdate:attribute>"
            + "\n\t<xupdate:attribute name=\"className\">" + realmClass + "</xupdate:attribute>"
            + "\n\t<xupdate:attribute name=\"userClassNames\">" + usersClassNames + "</xupdate:attribute>"
            + "\n\t<xupdate:attribute name=\"roleClassNames\">" + roleClassNames + "</xupdate:attribute>"
            + "</xupdate:element>" + "\n\t<xupdate:comment>"
            + " ================================================== \n" + "</xupdate:comment>\n\t"
            + "</xupdate:insert-before>";

    String appendJossoRealmQryStr = XUpdateUtil.XUPDATE_START + appendJossoRealmQryMod
            + XUpdateUtil.XUPDATE_END;
    log.debug("XUPDATE QUERY: \n" + appendJossoRealmQryStr);

    XUpdateQuery appendJossoRealmQry = new XUpdateQueryImpl();
    appendJossoRealmQry.setQString(appendJossoRealmQryStr);
    appendJossoRealmQry.execute(serverXmlDom);

    getPrinter().printActionOkStatus("Configured", "JOSSO JAAS Realm ", realmClass);

    return true;

}

From source file:org.josso.tooling.gshell.install.installer.JBossInstaller.java

protected boolean configureValve(Node serverXmlDom) throws Exception {

    if (getPlatformVersion().startsWith("5") || getPlatformVersion().startsWith("6")) {
        return false;
    }/*  w  ww .j  av a2  s . co  m*/

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

    String valveClass = "org.josso.tc55.agent.SSOAgentValve";

    if (getPlatformVersion().startsWith("4.0"))
        valveClass = "org.josso.tc55.agent.SSOAgentValve";
    else if (getPlatformVersion().startsWith("3.2"))
        valveClass = "org.josso.tc50.agent.SSOAgentValve";

    // Check if josso agent valve is already present

    XPathExpression findAgentValve = xpath
            .compile("/Server/Service/Engine/Host/Valve[@className=\"" + valveClass + "\"]");
    NodeList agentValves = (NodeList) findAgentValve.evaluate(serverXmlDom, XPathConstants.NODESET);

    // If we already have a JOSSO Valve, do nothing!
    if (agentValves != null && agentValves.getLength() > 0) {
        for (int i = 0; i < agentValves.getLength(); i++) {
            Node valve = agentValves.item(i);
            Node valveClassNode = valve.getAttributes().getNamedItem("className");
            getPrinter().printActionWarnStatus("Configure", "JOSSO Agent Valve", "Already configured : "
                    + (valveClassNode != null ? valveClassNode.getNodeValue() : "<unknown>"));
        }
        return false;
    }

    String appendJossoValveQryMod = "\n\t<xupdate:append select=\"//Server/Service/Engine/Host\" >"
            + "\n\t<xupdate:comment>" + " ================================================== \n"
            + "</xupdate:comment>" + "\n\t<xupdate:comment>"
            + "   JOSSO Agent Valve, configuration automatially generated by JOSSO Installer\n"
            + "</xupdate:comment>" + "\n\t<xupdate:element name=\"Valve\">"
            + "<xupdate:attribute name=\"appName\">josso</xupdate:attribute>"
            + "<xupdate:attribute name=\"debug\">1</xupdate:attribute>"
            + "\n\t<xupdate:attribute name=\"className\">" + valveClass + "</xupdate:attribute>"
            + "</xupdate:element>" + "\n\t<xupdate:comment>"
            + " ================================================== \n" + "</xupdate:comment>\n\t"
            + "</xupdate:append>";

    String appendJossoValveQryStr = XUpdateUtil.XUPDATE_START + appendJossoValveQryMod
            + XUpdateUtil.XUPDATE_END;
    log.debug("XUPDATE QUERY: \n" + appendJossoValveQryStr);

    XUpdateQuery appendJossoValveQry = new XUpdateQueryImpl();
    appendJossoValveQry.setQString(appendJossoValveQryStr);
    appendJossoValveQry.execute(serverXmlDom);

    getPrinter().printActionOkStatus("Configured", "JOSSO Agent Valve ", valveClass);

    return true;

}

From source file:org.josso.tooling.gshell.install.installer.JBossInstaller.java

protected boolean configureLoginConfig(Node loginConfigXmlDom) throws Exception {
    XPath xpath = XPathFactory.newInstance().newXPath();

    // Check if josso agent valve is already present

    XPathExpression findAppPolicy = xpath.compile("/policy/application-policy[@name=\"josso\"]");
    NodeList jossoAppPolicies = (NodeList) findAppPolicy.evaluate(loginConfigXmlDom, XPathConstants.NODESET);

    // If we already have a JOSSO Valve, do nothing!
    if (jossoAppPolicies != null && jossoAppPolicies.getLength() > 0) {
        for (int i = 0; i < jossoAppPolicies.getLength(); i++) {
            Node jossoAppPolicy = jossoAppPolicies.item(i);
            Node jossoAppPolicyName = jossoAppPolicy.getAttributes().getNamedItem("name");
            getPrinter().printActionWarnStatus("Configure", "JOSSO JAAS Login Module", "Already configured : "
                    + (jossoAppPolicyName != null ? jossoAppPolicyName.getNodeValue() : "<unknown>"));
        }/*from   ww  w  .ja  v a  2s  .co m*/
        return false;
    }

    String appendJossoAppPolicyQryMod = null;

    String loginModuleClass = "org.josso.jb42.agent.JBossSSOGatewayLoginModule";
    if (getPlatformVersion().startsWith("4.0"))
        loginModuleClass = "org.josso.jb4.agent.JBossSSOGatewayLoginModule";
    else if (getPlatformVersion().startsWith("3.2"))
        loginModuleClass = "org.josso.jb32.agent.JBossSSOGatewayLoginModule";

    if (getPlatformVersion().startsWith("5") || getPlatformVersion().startsWith("6")) {
        // Append a new JAAS Configuration ...
        appendJossoAppPolicyQryMod = "\n<xupdate:append select=\"//policy\" >" + "\n\n\n    <xupdate:comment>"
                + " ================================================== " + "</xupdate:comment>"
                + "\n    <xupdate:comment>"
                + "   JOSSO JAAS Login Configuration, automatially generated by JOSSO Installer"
                + "</xupdate:comment>" + "\n    <xupdate:element name=\"application-policy\">"
                + "    <xupdate:attribute name=\"name\">josso</xupdate:attribute>"
                + "        <authentication-jaspi>\n" + "            <login-module-stack name=\"lm-stack\">\n"
                + "            <!--login-module code=\"org.josso.jaspi.agent.SSOGatewayLoginModule\" flag=\"required\"-->\n"
                + "            <!--JBossSSOGatewayLoginModule just adds support for unauthenticatedIdentity option-->\n"
                + "            <login-module code=\"org.josso.jb5.agent.JBossSSOGatewayLoginModule\" flag=\"required\">\n"
                + "               <module-option name=\"debug\">true</module-option>\n"
                + "            </login-module>\n" + "         </login-module-stack>\n"
                + "          <auth-module code=\"org.josso.jaspi.agent.JASPISSOAuthModule\" />\n"
                + "        </authentication-jaspi>\n" + "    </xupdate:element>" + "\n    <xupdate:comment>"
                + " ================================================== \n" + "</xupdate:comment>\n\n\n\n"
                + "</xupdate:append>";
    } else {
        // Append a new JAAS Configuration ...
        appendJossoAppPolicyQryMod = "\n<xupdate:append select=\"//policy\" >" + "\n\n\n    <xupdate:comment>"
                + " ================================================== " + "</xupdate:comment>"
                + "\n    <xupdate:comment>"
                + "   JOSSO JAAS Login Configuration, automatially generated by JOSSO Installer"
                + "</xupdate:comment>" + "\n    <xupdate:element name=\"application-policy\">"
                + "    <xupdate:attribute name=\"name\">josso</xupdate:attribute>"
                + "        <authentication>\n" + "            <login-module code = \"" + loginModuleClass
                + "\" flag = \"required\">\n"
                + "                <module-option name=\"debug\">true</module-option>\n"
                + "            </login-module>\n" + "        </authentication>\n" + "    </xupdate:element>"
                + "\n    <xupdate:comment>" + " ================================================== \n"
                + "</xupdate:comment>\n\n\n\n" + "</xupdate:append>";
    }

    String appendJossoAppPolicyQryStr = XUpdateUtil.XUPDATE_START + appendJossoAppPolicyQryMod
            + XUpdateUtil.XUPDATE_END;
    log.debug("XUPDATE QUERY: \n" + appendJossoAppPolicyQryStr);

    XUpdateQuery appendJossoValveQry = new XUpdateQueryImpl();
    appendJossoValveQry.setQString(appendJossoAppPolicyQryStr);
    appendJossoValveQry.execute(loginConfigXmlDom);

    if (!(getPlatformVersion().startsWith("5") || getPlatformVersion().startsWith("6"))) {
        getPrinter().printActionOkStatus("Configured", "JOSSO JAAS Login Module ", loginModuleClass);
    }

    return true;

}

From source file:org.josso.tooling.gshell.install.installer.TomcatInstaller.java

protected boolean configureRealm(Node serverXmlDom) throws Exception {

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

    // Because we removed all realms, we always add JOSSO realm
    String usersClassNames = "org.josso.gateway.identity.service.BaseUserImpl";
    String roleClassNames = "org.josso.gateway.identity.service.BaseRoleImpl";
    String realmClass = "org.josso." + getPlatformId() + ".agent.jaas.CatalinaJAASRealm"; // TODO : Be carefull with platform ID, this could not match the agent pacakge

    // Check if josso agent valve is already present

    XPathExpression findAgentRealm = xpath
            .compile("/Server/Service/Engine/Realm[@className=\"" + realmClass + "\"]");
    NodeList agentRealms = (NodeList) findAgentRealm.evaluate(serverXmlDom, XPathConstants.NODESET);

    // If we already have a JOSSO Valve, do nothing!
    if (agentRealms != null && agentRealms.getLength() > 0) {
        for (int i = 0; i < agentRealms.getLength(); i++) {
            Node valve = agentRealms.item(i);
            Node valveClassNode = valve.getAttributes().getNamedItem("className");
            getPrinter().printActionWarnStatus("Configure", "JOSSO JASS Realm", "Already configured : "
                    + (valveClassNode != null ? valveClassNode.getNodeValue() : "<unknown>"));
        }//from  w  w  w  . ja v  a2s . co  m
        return false;
    }

    XPathExpression findRealmsExpr = xpath.compile("/Server/Service/Engine/Realm");
    NodeList realms = (NodeList) findRealmsExpr.evaluate(serverXmlDom, XPathConstants.NODESET);

    if (realms != null && realms.getLength() > 0) {
        String qryModifications = "\n\t<xupdate:remove select=\"/Server/Service/Engine/Realm\"/>";
        String qry = XUpdateUtil.XUPDATE_START + qryModifications + XUpdateUtil.XUPDATE_END;
        log.debug("XUPDATE QUERY: \n" + qry);
        XUpdateQuery xq = new XUpdateQueryImpl();
        xq.setQString(qry);
        xq.execute(serverXmlDom);

        for (int i = 0; i < realms.getLength(); i++) {
            Node realmDom = realms.item(i);
            Node className = realmDom.getAttributes().getNamedItem("className");
            getPrinter().printActionOkStatus("Removed", "Tomcat default Realm ",
                    (className != null ? className.getNodeValue() : "<unknown>"));
        }
    }

    String appendJossoRealmQryMod = "\n\t<xupdate:insert-before select=\"//Server/Service/Engine/Host[1]\" >"
            + "\n\t<xupdate:comment>" + " ================================================== "
            + "</xupdate:comment>" + "\n\t<xupdate:comment>"
            + "   JOSSO JAAS Realm, configuration automatially generated by JOSSO Installer "
            + "</xupdate:comment>" + "\n\t<xupdate:element name=\"Realm\">"
            + "<xupdate:attribute name=\"appName\">josso</xupdate:attribute>"
            + "<xupdate:attribute name=\"debug\">1</xupdate:attribute>"
            + "\n\t<xupdate:attribute name=\"className\">" + realmClass + "</xupdate:attribute>"
            + "\n\t<xupdate:attribute name=\"userClassNames\">" + usersClassNames + "</xupdate:attribute>"
            + "\n\t<xupdate:attribute name=\"roleClassNames\">" + roleClassNames + "</xupdate:attribute>"
            + "</xupdate:element>" + "\n\t<xupdate:comment>"
            + " ================================================== " + "</xupdate:comment>\n\t"
            + "</xupdate:insert-before>";

    String appendJossoRealmQryStr = XUpdateUtil.XUPDATE_START + appendJossoRealmQryMod
            + XUpdateUtil.XUPDATE_END;
    log.debug("XUPDATE QUERY: \n" + appendJossoRealmQryStr);

    XUpdateQuery appendJossoRealmQry = new XUpdateQueryImpl();
    appendJossoRealmQry.setQString(appendJossoRealmQryStr);
    appendJossoRealmQry.execute(serverXmlDom);

    getPrinter().printActionOkStatus("Configured", "JOSSO JAAS Realm ", realmClass);

    return true;

}