Java Examples for net.sf.saxon.dom.DOMNodeList

The following java examples will help you to understand the usage of net.sf.saxon.dom.DOMNodeList. These source code samples are taken from different open source projects.

Example 1
Project: camel-master  File: CxfConsumerPayloadXPathTest.java View source code
@Override
public void process(Exchange exchange) throws Exception {
    Object obj = exchange.getIn().getBody();
    //xpath expression results in a: net.sf.saxon.dom.DOMNodeList
    //after which it is converted to a String
    String content = XPathBuilder.xpath("//xml/text()").evaluate(context, obj, String.class);
    exchange.getOut().setBody(content);
    exchange.getOut().setHeaders(exchange.getIn().getHeaders());
}
Example 2
Project: ios-driver-master  File: XPath2Engine.java View source code
private List<Map<String, String>> findElementsByXpath(String xpath, Node from) throws XPathExpressionException {
    XPathExpression expr = xpath2.compile(xpath);
    DOMNodeList elements;
    elements = (DOMNodeList) expr.evaluate(document, XPathConstants.NODESET);
    List<Map<String, String>> res = new ArrayList<Map<String, String>>();
    for (int i = 0; i < elements.getLength(); i++) {
        Element el = (Element) elements.item(i);
        String reference = el.getAttribute("ref");
        String type = el.getNodeName();
        res.add(ImmutableMap.of("ELEMENT", reference, "type", type));
    }
    return res;
}
Example 3
Project: ode-master  File: JaxpFunctionResolver.java View source code
public Object evaluate(List args) throws XPathFunctionException {
    if (args.size() < 1 || args.size() > 2)
        throw new XPathFunctionException(new FaultException(new QName(Namespaces.ODE_EXTENSION_NS, "deleteInvalidSource"), "Invalid arguments"));
    if (__log.isDebugEnabled()) {
        __log.debug("delete call(context=" + _ectx + " args=" + args + ")");
    }
    List<Node> targetNodes = new ArrayList();
    List siblingNodes;
    Object delete = args.size() == 2 ? delete = args.get(1) : args.get(0);
    try {
        if (delete instanceof List) {
            List elmts = (List) delete;
            // allow insertions after a sequence of node items
            // if (elmts.size() != 1) throw new XPathFunctionException(
            //        new FaultException(_oxpath.getOwner().constants.qnSelectionFailure,
            //                "The bpws:delete function MUST be passed a single " +
            //                        "element node."));
            targetNodes.addAll(elmts);
        } else if (delete instanceof NodeWrapper) {
            targetNodes.add((Element) ((NodeWrapper) delete).getUnderlyingNode());
        } else if (delete instanceof Element) {
            targetNodes.add((Element) delete);
        } else if (delete instanceof SequenceExtent) {
            try {
                DOMNodeList nodeList = DOMNodeList.checkAndMake((SequenceExtent) delete);
                for (int i = 0; i < nodeList.getLength(); i++) {
                    targetNodes.add(nodeList.item(i));
                }
            } catch (XPathException e) {
                throw new XPathFunctionException(e);
            }
        } else {
            throw new XPathFunctionException("Unexpected argument type: " + delete.getClass());
        }
    } catch (IllegalArgumentException e) {
        throw new XPathFunctionException(new FaultException(_oxpath.getOwner().getConstants().getQnInvalidExpressionValue(), "Invalid argument: URI Template expected. " + delete, e));
    } catch (ClassCastException e) {
        throw new XPathFunctionException(new FaultException(_oxpath.getOwner().getConstants().getQnSelectionFailure(), "The bpws:delete function MUST be passed a valid " + "element node."));
    }
    Element parentElmt = null;
    for (Node targetNode : targetNodes) {
        if (parentElmt == null) {
            parentElmt = (Element) targetNode.getParentNode();
        } else if (!parentElmt.isSameNode((Element) targetNode.getParentNode())) {
            throw new XPathFunctionException(new FaultException(_oxpath.getOwner().getConstants().getQnSelectionFailure(), "The bpws:delete function MUST be passed nodes that have " + "the same parent."));
        }
    }
    NodeList children = parentElmt.getChildNodes();
    int[] positions = new int[targetNodes.size()];
    for (int target = 0; target < positions.length; target++) {
        for (int position = 0; position < children.getLength(); position++) {
            if (children.item(position).isSameNode(targetNodes.get(target))) {
                positions[target] = position;
            }
        }
    }
    // 2xLoops as previously the contents of the 'children' list appeared to
    // be being changed by the clonedElmt.removeChild call, meaning the position
    // offset was incorrect, a possibly better approach would be to sort the position
    // indices and iterate *backwards* but for my needs this approach suffices.
    List<Node> clonedChildrenToRemove = new ArrayList<Node>();
    final Element clonedElmt = (Element) parentElmt.cloneNode(true);
    children = clonedElmt.getChildNodes();
    for (int target = 0; target < positions.length; target++) {
        Element deleteElmt = (Element) children.item(positions[target]);
        clonedChildrenToRemove.add(deleteElmt);
    }
    for (Node deleteElmt : clonedChildrenToRemove) {
        clonedElmt.removeChild(deleteElmt);
    }
    // Saxon doesn't like clones with no children, so I'll oblige
    if (clonedElmt.getChildNodes().getLength() == 0) {
        try {
            clonedElmt.appendChild(DOMUtils.toDOMDocument(parentElmt).createTextNode(""));
        } catch (TransformerException te) {
            throw new XPathFunctionException(te);
        }
    }
    return clonedElmt;
}