Search in sources :

Example 1 with XPathAPI

use of org.apache.xml.security.utils.XPathAPI in project santuario-java by apache.

the class TransformXPath method enginePerformTransform.

/**
 * Method enginePerformTransform
 * {@inheritDoc}
 * @param input
 *
 * @throws TransformationException
 */
protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input, OutputStream os, Transform transformObject) throws TransformationException {
    try {
        /**
         * If the actual input is an octet stream, then the application MUST
         * convert the octet stream to an XPath node-set suitable for use by
         * Canonical XML with Comments. (A subsequent application of the
         * REQUIRED Canonical XML algorithm would strip away these comments.)
         *
         * ...
         *
         * The evaluation of this expression includes all of the document's nodes
         * (including comments) in the node-set representing the octet stream.
         */
        Element xpathElement = XMLUtils.selectDsNode(transformObject.getElement().getFirstChild(), Constants._TAG_XPATH, 0);
        if (xpathElement == null) {
            Object[] exArgs = { "ds:XPath", "Transform" };
            throw new TransformationException("xml.WrongContent", exArgs);
        }
        Node xpathnode = xpathElement.getFirstChild();
        if (xpathnode == null) {
            throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "Text must be in ds:Xpath");
        }
        String str = XMLUtils.getStrFromNode(xpathnode);
        input.setNeedsToBeExpanded(needsCircumvent(str));
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPathAPI xpathAPIInstance = xpathFactory.newXPathAPI();
        input.addNodeFilter(new XPathNodeFilter(xpathElement, xpathnode, str, xpathAPIInstance));
        input.setNodeSet(true);
        return input;
    } catch (DOMException ex) {
        throw new TransformationException(ex);
    }
}
Also used : DOMException(org.w3c.dom.DOMException) XPathFactory(org.apache.xml.security.utils.XPathFactory) TransformationException(org.apache.xml.security.transforms.TransformationException) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) XPathAPI(org.apache.xml.security.utils.XPathAPI)

Example 2 with XPathAPI

use of org.apache.xml.security.utils.XPathAPI in project santuario-java by apache.

the class XPath2NodeFilter method enginePerformTransform.

/**
 * Method enginePerformTransform
 * {@inheritDoc}
 * @param input
 *
 * @throws TransformationException
 */
protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input, OutputStream os, Transform transformObject) throws TransformationException {
    try {
        List<NodeList> unionNodes = new ArrayList<>();
        List<NodeList> subtractNodes = new ArrayList<>();
        List<NodeList> intersectNodes = new ArrayList<>();
        Element[] xpathElements = XMLUtils.selectNodes(transformObject.getElement().getFirstChild(), XPath2FilterContainer.XPathFilter2NS, XPath2FilterContainer._TAG_XPATH2);
        if (xpathElements.length == 0) {
            Object[] exArgs = { Transforms.TRANSFORM_XPATH2FILTER, "XPath" };
            throw new TransformationException("xml.WrongContent", exArgs);
        }
        Document inputDoc = null;
        if (input.getSubNode() != null) {
            inputDoc = XMLUtils.getOwnerDocument(input.getSubNode());
        } else {
            inputDoc = XMLUtils.getOwnerDocument(input.getNodeSet());
        }
        for (int i = 0; i < xpathElements.length; i++) {
            Element xpathElement = xpathElements[i];
            XPath2FilterContainer xpathContainer = XPath2FilterContainer.newInstance(xpathElement, input.getSourceURI());
            String str = XMLUtils.getStrFromNode(xpathContainer.getXPathFilterTextNode());
            XPathFactory xpathFactory = XPathFactory.newInstance();
            XPathAPI xpathAPIInstance = xpathFactory.newXPathAPI();
            NodeList subtreeRoots = xpathAPIInstance.selectNodeList(inputDoc, xpathContainer.getXPathFilterTextNode(), str, xpathContainer.getElement());
            if (xpathContainer.isIntersect()) {
                intersectNodes.add(subtreeRoots);
            } else if (xpathContainer.isSubtract()) {
                subtractNodes.add(subtreeRoots);
            } else if (xpathContainer.isUnion()) {
                unionNodes.add(subtreeRoots);
            }
        }
        input.addNodeFilter(new XPath2NodeFilter(unionNodes, subtractNodes, intersectNodes));
        input.setNodeSet(true);
        return input;
    } catch (TransformerException ex) {
        throw new TransformationException(ex);
    } catch (DOMException ex) {
        throw new TransformationException(ex);
    } catch (CanonicalizationException ex) {
        throw new TransformationException(ex);
    } catch (InvalidCanonicalizerException ex) {
        throw new TransformationException(ex);
    } catch (XMLSecurityException ex) {
        throw new TransformationException(ex);
    } catch (SAXException ex) {
        throw new TransformationException(ex);
    } catch (IOException ex) {
        throw new TransformationException(ex);
    } catch (ParserConfigurationException ex) {
        throw new TransformationException(ex);
    }
}
Also used : TransformationException(org.apache.xml.security.transforms.TransformationException) CanonicalizationException(org.apache.xml.security.c14n.CanonicalizationException) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.w3c.dom.Document) XMLSecurityException(org.apache.xml.security.exceptions.XMLSecurityException) XPathAPI(org.apache.xml.security.utils.XPathAPI) SAXException(org.xml.sax.SAXException) XPathFactory(org.apache.xml.security.utils.XPathFactory) DOMException(org.w3c.dom.DOMException) InvalidCanonicalizerException(org.apache.xml.security.c14n.InvalidCanonicalizerException) XPath2FilterContainer(org.apache.xml.security.transforms.params.XPath2FilterContainer) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException)

Aggregations

TransformationException (org.apache.xml.security.transforms.TransformationException)2 XPathAPI (org.apache.xml.security.utils.XPathAPI)2 XPathFactory (org.apache.xml.security.utils.XPathFactory)2 DOMException (org.w3c.dom.DOMException)2 Element (org.w3c.dom.Element)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1 CanonicalizationException (org.apache.xml.security.c14n.CanonicalizationException)1 InvalidCanonicalizerException (org.apache.xml.security.c14n.InvalidCanonicalizerException)1 XMLSecurityException (org.apache.xml.security.exceptions.XMLSecurityException)1 XPath2FilterContainer (org.apache.xml.security.transforms.params.XPath2FilterContainer)1 Document (org.w3c.dom.Document)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1 SAXException (org.xml.sax.SAXException)1