Search in sources :

Example 31 with XPathFactory

use of javax.xml.xpath.XPathFactory in project perun by CESNET.

the class MuPasswordManagerModule method parseUCO.

/**
	 * Parse UCO from XML body response and convert it to map of parameters.
	 *
	 * @param document XML document to be parsed
	 * @param requestID unique ID of a request
	 * @return Map of response params
	 * @throws InternalErrorException
	 */
private Map<String, String> parseUCO(Document document, int requestID) throws InternalErrorException {
    Map<String, String> result = new HashMap<>();
    //Prepare xpath expression
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression ucoExpr;
    try {
        ucoExpr = xpath.compile("//resp/uco/text()");
    } catch (XPathExpressionException ex) {
        throw new InternalErrorException("Error when compiling xpath query. Request ID: " + requestID, ex);
    }
    try {
        String uco = (String) ucoExpr.evaluate(document, XPathConstants.STRING);
        result.put("urn:perun:user:attribute-def:def:login-namespace:mu", uco);
    } catch (XPathExpressionException ex) {
        throw new InternalErrorException("Error when evaluate xpath query on resulting document for request ID: " + requestID, ex);
    }
    return result;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) HashMap(java.util.HashMap) XPathExpressionException(javax.xml.xpath.XPathExpressionException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 32 with XPathFactory

use of javax.xml.xpath.XPathFactory in project perun by CESNET.

the class MuPasswordManagerModule method parseResponse.

/**
	 * Parse XML response from IS MU to XML document.
	 *
	 * @param inputStream Stream to be parsed to Document
	 * @param requestID ID of request made to IS MU.
	 * @return XML document for further processing
	 * @throws InternalErrorException
	 */
private Document parseResponse(InputStream inputStream, int requestID) throws InternalErrorException {
    //Create new document factory builder
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException ex) {
        throw new InternalErrorException("Error when creating newDocumentBuilder. Request ID: " + requestID, ex);
    }
    String response = null;
    try {
        response = convertStreamToString(inputStream, "UTF-8");
    } catch (IOException ex) {
        log.error("Unable to convert InputStream to String: {}", ex);
    }
    log.trace("Request ID: " + requestID + " Response: " + response);
    Document doc;
    try {
        doc = builder.parse(new InputSource(new StringReader(response)));
    } catch (SAXParseException ex) {
        throw new InternalErrorException("Error when parsing uri by document builder. Request ID: " + requestID, ex);
    } catch (SAXException ex) {
        throw new InternalErrorException("Problem with parsing is more complex, not only invalid characters. Request ID: " + requestID, ex);
    } catch (IOException ex) {
        throw new InternalErrorException("Error when parsing uri by document builder. Problem with input or output. Request ID: " + requestID, ex);
    }
    //Prepare xpath expression
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression isErrorExpr;
    XPathExpression getErrorTextExpr;
    XPathExpression getDbErrorTextExpr;
    try {
        isErrorExpr = xpath.compile("//resp/stav/text()");
        getErrorTextExpr = xpath.compile("//resp/error/text()");
        getDbErrorTextExpr = xpath.compile("//resp/dberror/text()");
    } catch (XPathExpressionException ex) {
        throw new InternalErrorException("Error when compiling xpath query. Request ID: " + requestID, ex);
    }
    // OK or ERROR
    String responseStatus;
    try {
        responseStatus = (String) isErrorExpr.evaluate(doc, XPathConstants.STRING);
    } catch (XPathExpressionException ex) {
        throw new InternalErrorException("Error when evaluate xpath query on document to resolve response status. Request ID: " + requestID, ex);
    }
    log.trace("Request ID: " + requestID + " Response status: " + responseStatus);
    if ("OK".equals(responseStatus)) {
        return doc;
    } else {
        try {
            String error = (String) getErrorTextExpr.evaluate(doc, XPathConstants.STRING);
            if (error == null || error.isEmpty()) {
                error = (String) getDbErrorTextExpr.evaluate(doc, XPathConstants.STRING);
            }
            throw new InternalErrorException("IS MU (password manager backend) responded with error to a Request ID: " + requestID + " Error: " + error);
        } catch (XPathExpressionException ex) {
            throw new InternalErrorException("Error when evaluate xpath query on document to resolve error status. Request ID: " + requestID, ex);
        }
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XPathExpressionException(javax.xml.xpath.XPathExpressionException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) XPathFactory(javax.xml.xpath.XPathFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 33 with XPathFactory

use of javax.xml.xpath.XPathFactory in project ats-framework by Axway.

the class XmlUtilities method getByXpath.

/**
     * Get values matching passed XPath expression
     * @param node
     * @param expression XPath expression
     * @return matching values
     * @throws Exception
     */
private static String[] getByXpath(Node node, String expression) throws Exception {
    XPathFactory xPathFactory = XPathFactory.newInstance();
    XPath xPath = xPathFactory.newXPath();
    XPathExpression xPathExpression = xPath.compile(expression);
    NodeList nlist = (NodeList) xPathExpression.evaluate(node, XPathConstants.NODESET);
    int nodeListSize = nlist.getLength();
    List<String> values = new ArrayList<String>(nodeListSize);
    for (int index = 0; index < nlist.getLength(); index++) {
        Node aNode = nlist.item(index);
        values.add(aNode.getTextContent());
    }
    return values.toArray(new String[values.size()]);
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList)

Example 34 with XPathFactory

use of javax.xml.xpath.XPathFactory in project perun by CESNET.

the class AbstractPublicationSystemStrategy method getValueFromXpath.

/**
	 * Get xml Node and xpath expression to get value from node by this xpath.
	 *
	 * @param node node for getting value from
	 * @param xpathExpression expression for xpath to looking for value in node
	 * @param resultType type of resulting / expected object (string number node nodelist ...)
	 * @return object extracted from node by xpath
	 * @throws InternalErrorException
	 */
protected Object getValueFromXpath(Node node, String xpathExpression, QName resultType) throws InternalErrorException {
    //Prepare xpath expression
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression expr;
    try {
        expr = xpath.compile(xpathExpression);
    } catch (XPathExpressionException ex) {
        throw new InternalErrorException("Error when compiling xpath query.", ex);
    }
    Object result;
    try {
        result = expr.evaluate(node, resultType);
    } catch (XPathExpressionException ex) {
        throw new InternalErrorException("Error when evaluate xpath query on node.", ex);
    }
    return result;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) XPathExpressionException(javax.xml.xpath.XPathExpressionException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 35 with XPathFactory

use of javax.xml.xpath.XPathFactory in project cloudstack by apache.

the class OvmObject method xmlToList.

public List<String> xmlToList(String path, Document xmlDocument) throws Ovm3ResourceException {
    List<String> list = new ArrayList<String>();
    XPathFactory factory = javax.xml.xpath.XPathFactory.newInstance();
    XPath xPath = factory.newXPath();
    try {
        XPathExpression xPathExpression = xPath.compile(path);
        NodeList nodeList = (NodeList) xPathExpression.evaluate(xmlDocument, XPathConstants.NODESET);
        for (int ind = 0; ind < nodeList.getLength(); ind++) {
            if (!nodeList.item(ind).getTextContent().isEmpty()) {
                list.add("" + nodeList.item(ind).getTextContent());
            } else {
                list.add("" + nodeList.item(ind).getNodeValue());
            }
        }
        return list;
    } catch (XPathExpressionException e) {
        throw new Ovm3ResourceException("Problem parsing XML to List: ", e);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList)

Aggregations

XPathFactory (javax.xml.xpath.XPathFactory)75 XPath (javax.xml.xpath.XPath)59 XPathExpression (javax.xml.xpath.XPathExpression)40 Document (org.w3c.dom.Document)34 NodeList (org.w3c.dom.NodeList)34 XPathExpressionException (javax.xml.xpath.XPathExpressionException)26 DocumentBuilder (javax.xml.parsers.DocumentBuilder)24 Node (org.w3c.dom.Node)24 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)22 InputSource (org.xml.sax.InputSource)16 Test (org.junit.Test)15 IOException (java.io.IOException)13 PBXNativeTarget (com.facebook.buck.apple.xcode.xcodeproj.PBXNativeTarget)11 PBXTarget (com.facebook.buck.apple.xcode.xcodeproj.PBXTarget)11 ImmutableMap (com.google.common.collect.ImmutableMap)11 Path (java.nio.file.Path)11 SAXException (org.xml.sax.SAXException)11 PBXFileReference (com.facebook.buck.apple.xcode.xcodeproj.PBXFileReference)10 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)9 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)7