Search in sources :

Example 6 with InvalidExpression

use of org.apache.cxf.ws.transfer.dialect.fragment.faults.InvalidExpression in project cxf by apache.

the class FragmentDialect method insertBefore.

private void insertBefore(Document ownerDocument, Node parent, Node refChild, ValueType value) {
    for (Object o : value.getContent()) {
        if (o instanceof Node) {
            Node node = (Node) o;
            if (FragmentDialectConstants.FRAGMENT_2011_03_IRI.equals(node.getNamespaceURI()) && FragmentDialectConstants.FRAGMENT_ATTR_NODE_NAME.equals(node.getLocalName())) {
                throw new InvalidRepresentation();
            }
            Node importedNode = ownerDocument.importNode(node, true);
            if (parent.getNodeType() == Node.DOCUMENT_NODE) {
                parent.appendChild(importedNode);
            } else {
                parent.insertBefore(importedNode, refChild);
            }
        } else {
            throw new InvalidExpression();
        }
    }
}
Also used : InvalidRepresentation(org.apache.cxf.ws.transfer.shared.faults.InvalidRepresentation) Node(org.w3c.dom.Node) InvalidExpression(org.apache.cxf.ws.transfer.dialect.fragment.faults.InvalidExpression)

Example 7 with InvalidExpression

use of org.apache.cxf.ws.transfer.dialect.fragment.faults.InvalidExpression in project cxf by apache.

the class FragmentDialect method modifyRepresentationModeRemove.

/**
 * Process Put requests for Remove mode.
 * @param value Value defined in the Value element.
 * @return Representation element, which is returned as response.
 */
private Representation modifyRepresentationModeRemove(List<Node> nodeList, ValueType value) {
    if (nodeList.isEmpty()) {
        throw new InvalidExpression();
    }
    Node firstNode = nodeList.get(0);
    Document ownerDocument = firstNode.getOwnerDocument();
    // if firstNode.getOwnerDocument == null the firstNode is ownerDocument
    ownerDocument = ownerDocument == null ? (Document) firstNode : ownerDocument;
    for (Node node : nodeList) {
        removeNode(node);
    }
    Representation representation = new Representation();
    representation.setAny(ownerDocument.getDocumentElement());
    return representation;
}
Also used : Node(org.w3c.dom.Node) Representation(org.apache.cxf.ws.transfer.Representation) InvalidRepresentation(org.apache.cxf.ws.transfer.shared.faults.InvalidRepresentation) Document(org.w3c.dom.Document) InvalidExpression(org.apache.cxf.ws.transfer.dialect.fragment.faults.InvalidExpression)

Example 8 with InvalidExpression

use of org.apache.cxf.ws.transfer.dialect.fragment.faults.InvalidExpression in project cxf by apache.

the class FragmentDialect method modifyRepresentationModeInsertAfter.

/**
 * Process Put requests for InsertAfter mode.
 * @param value Value defined in the Value element.
 * @return Representation element, which is returned as response.
 */
private Representation modifyRepresentationModeInsertAfter(List<Node> nodeList, ValueType value) {
    if (nodeList.isEmpty()) {
        throw new InvalidExpression();
    }
    Node firstNode = nodeList.get(0);
    Document ownerDocument = firstNode.getOwnerDocument();
    // if firstNode.getOwnerDocument == null the firstNode is ownerDocument
    ownerDocument = ownerDocument == null ? (Document) firstNode : ownerDocument;
    Node parent = firstNode.getParentNode();
    if (parent == null && firstNode.getNodeType() != Node.DOCUMENT_NODE) {
        throw new InvalidExpression();
    }
    if (parent == null) {
        parent = firstNode;
        if (((Document) parent).getDocumentElement() != null) {
            throw new InvalidExpression();
        }
    }
    for (Node node : nodeList) {
        if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
            throw new InvalidRepresentation();
        }
        insertAfter(ownerDocument, parent, node, value);
    }
    Representation representation = new Representation();
    representation.setAny(ownerDocument.getDocumentElement());
    return representation;
}
Also used : InvalidRepresentation(org.apache.cxf.ws.transfer.shared.faults.InvalidRepresentation) Node(org.w3c.dom.Node) Representation(org.apache.cxf.ws.transfer.Representation) InvalidRepresentation(org.apache.cxf.ws.transfer.shared.faults.InvalidRepresentation) Document(org.w3c.dom.Document) InvalidExpression(org.apache.cxf.ws.transfer.dialect.fragment.faults.InvalidExpression)

Example 9 with InvalidExpression

use of org.apache.cxf.ws.transfer.dialect.fragment.faults.InvalidExpression in project cxf by apache.

the class FragmentDialect method modifyRepresentationModeInsertBefore.

/**
 * Process Put requests for InsertBefore mode.
 * @param value Value defined in the Value element.
 * @return Representation element, which is returned as response.
 */
private Representation modifyRepresentationModeInsertBefore(List<Node> nodeList, ValueType value) {
    if (nodeList.isEmpty()) {
        throw new InvalidExpression();
    }
    Node firstNode = nodeList.get(0);
    Document ownerDocument = firstNode.getOwnerDocument();
    // if firstNode.getOwnerDocument == null the firstNode is ownerDocument
    ownerDocument = ownerDocument == null ? (Document) firstNode : ownerDocument;
    Node parent = firstNode.getParentNode();
    if (parent == null && firstNode.getNodeType() != Node.DOCUMENT_NODE) {
        throw new InvalidExpression();
    }
    if (parent == null) {
        parent = firstNode;
        if (((Document) parent).getDocumentElement() != null) {
            throw new InvalidExpression();
        }
    }
    for (Node node : nodeList) {
        if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
            throw new InvalidRepresentation();
        }
        insertBefore(ownerDocument, parent, node, value);
    }
    Representation representation = new Representation();
    representation.setAny(ownerDocument.getDocumentElement());
    return representation;
}
Also used : InvalidRepresentation(org.apache.cxf.ws.transfer.shared.faults.InvalidRepresentation) Node(org.w3c.dom.Node) Representation(org.apache.cxf.ws.transfer.Representation) InvalidRepresentation(org.apache.cxf.ws.transfer.shared.faults.InvalidRepresentation) Document(org.w3c.dom.Document) InvalidExpression(org.apache.cxf.ws.transfer.dialect.fragment.faults.InvalidExpression)

Example 10 with InvalidExpression

use of org.apache.cxf.ws.transfer.dialect.fragment.faults.InvalidExpression in project cxf by apache.

the class FragmentDialectLanguageXPath10 method getResourceFragment.

@Override
public Object getResourceFragment(final Representation representation, ExpressionType expression) {
    String expressionStr = getXPathFromExpression(expression);
    // Evaluate XPath
    XPath xPath = xpathFactory.newXPath();
    xPath.setNamespaceContext(new NamespaceContext() {

        @Override
        public String getNamespaceURI(String prefix) {
            if (prefix != null && !prefix.isEmpty()) {
                Element resource = (Element) representation.getAny();
                return resource.getAttribute("xmlns:" + prefix);
            }
            return null;
        }

        @Override
        public String getPrefix(String string) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Iterator<String> getPrefixes(String string) {
            throw new UnsupportedOperationException();
        }
    });
    try {
        Object resource = representation.getAny();
        if (resource == null) {
            resource = DOMUtils.createDocument();
        }
        NodeList result = (NodeList) xPath.evaluate(expressionStr, resource, XPathConstants.NODESET);
        if (checkResultConstraints(result)) {
            if (result.getLength() == 0) {
                return null;
            }
            return result;
        }
        return result.item(0);
    } catch (XPathException ex) {
    // See https://www.java.net/node/681793
    }
    try {
        return xPath.evaluate(expressionStr, representation.getAny(), XPathConstants.STRING);
    } catch (XPathException ex) {
        throw new InvalidExpression();
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathException(javax.xml.xpath.XPathException) NamespaceContext(javax.xml.namespace.NamespaceContext) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Iterator(java.util.Iterator) InvalidExpression(org.apache.cxf.ws.transfer.dialect.fragment.faults.InvalidExpression)

Aggregations

InvalidExpression (org.apache.cxf.ws.transfer.dialect.fragment.faults.InvalidExpression)10 Node (org.w3c.dom.Node)8 InvalidRepresentation (org.apache.cxf.ws.transfer.shared.faults.InvalidRepresentation)7 Representation (org.apache.cxf.ws.transfer.Representation)5 Document (org.w3c.dom.Document)5 NodeList (org.w3c.dom.NodeList)2 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Matcher (java.util.regex.Matcher)1 NamespaceContext (javax.xml.namespace.NamespaceContext)1 XPath (javax.xml.xpath.XPath)1 XPathException (javax.xml.xpath.XPathException)1 Element (org.w3c.dom.Element)1