Search in sources :

Example 6 with NodeIterator

use of org.w3c.dom.traversal.NodeIterator in project cloudstack by apache.

the class HypervisorHostHelper method removeOVFNetwork.

public static String removeOVFNetwork(final String ovfString) {
    if (ovfString == null || ovfString.isEmpty()) {
        return ovfString;
    }
    try {
        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        final Document doc = factory.newDocumentBuilder().parse(new ByteArrayInputStream(ovfString.getBytes()));
        final DocumentTraversal traversal = (DocumentTraversal) doc;
        final NodeIterator iterator = traversal.createNodeIterator(doc.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null, true);
        for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) {
            final Element e = (Element) n;
            if ("NetworkSection".equals(e.getTagName())) {
                if (e.getParentNode() != null) {
                    e.getParentNode().removeChild(e);
                }
            } else if ("rasd:Connection".equals(e.getTagName())) {
                if (e.getParentNode() != null && e.getParentNode().getParentNode() != null) {
                    e.getParentNode().getParentNode().removeChild(e.getParentNode());
                }
            }
        }
        final DOMSource domSource = new DOMSource(doc);
        final StringWriter writer = new StringWriter();
        final StreamResult result = new StreamResult(writer);
        final TransformerFactory tf = TransformerFactory.newInstance();
        final Transformer transformer = tf.newTransformer();
        transformer.transform(domSource, result);
        return writer.toString();
    } catch (SAXException | IOException | ParserConfigurationException | TransformerException e) {
        s_logger.warn("Unexpected exception caught while removing network elements from OVF:", e);
    }
    return ovfString;
}
Also used : NodeIterator(org.w3c.dom.traversal.NodeIterator) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentTraversal(org.w3c.dom.traversal.DocumentTraversal) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException)

Example 7 with NodeIterator

use of org.w3c.dom.traversal.NodeIterator in project webtools.sourceediting by eclipse.

the class DocumentImpl method internalGetElementsByTagName.

private NodeListImpl internalGetElementsByTagName(String tagName) {
    // System.out.println("getElementsByTagname: " + tagName);
    NodeIterator it = createNodeIterator(this, NodeFilter.SHOW_ALL, null, false);
    if (it == null)
        return new NodeListImpl();
    NodeListImpl elements = new NodeListImpl();
    if (tagName.length() == 1 && tagName.charAt(0) == '*') {
        // do not care
        tagName = null;
    }
    for (Node node = it.nextNode(); node != null; node = it.nextNode()) {
        if (node.getNodeType() != ELEMENT_NODE)
            continue;
        if (tagName != null) {
            ElementImpl element = (ElementImpl) node;
            if (!element.matchTagName(tagName))
                continue;
        }
        elements.appendNode(node);
    }
    if (usetagnamecache) {
        tagNameCache.addItem(tagName, elements);
    }
    return elements;
}
Also used : NodeIterator(org.w3c.dom.traversal.NodeIterator) Node(org.w3c.dom.Node)

Example 8 with NodeIterator

use of org.w3c.dom.traversal.NodeIterator in project activemq-artemis by apache.

the class XalanXPathEvaluator method evaluate.

protected boolean evaluate(InputSource inputSource) {
    try {
        DocumentBuilder dbuilder = createDocumentBuilder();
        Document doc = dbuilder.parse(inputSource);
        // An XPath expression could return a true or false value instead of a node.
        // eval() is a better way to determine the boolean value of the exp.
        // For compliance with legacy behavior where selecting an empty node returns true,
        // selectNodeIterator is attempted in case of a failure.
        CachedXPathAPI cachedXPathAPI = new CachedXPathAPI();
        XObject result = cachedXPathAPI.eval(doc, xpath);
        if (result.bool())
            return true;
        else {
            NodeIterator iterator = cachedXPathAPI.selectNodeIterator(doc, xpath);
            return (iterator.nextNode() != null);
        }
    } catch (Throwable e) {
        return false;
    }
}
Also used : NodeIterator(org.w3c.dom.traversal.NodeIterator) DocumentBuilder(javax.xml.parsers.DocumentBuilder) CachedXPathAPI(org.apache.xpath.CachedXPathAPI) Document(org.w3c.dom.Document) XObject(org.apache.xpath.objects.XObject)

Example 9 with NodeIterator

use of org.w3c.dom.traversal.NodeIterator in project freemarker by apache.

the class SunInternalXalanXPathSupport method executeQuery.

public synchronized TemplateModel executeQuery(Object context, String xpathQuery) throws TemplateModelException {
    if (!(context instanceof Node)) {
        if (context != null) {
            if (isNodeList(context)) {
                int cnt = ((List) context).size();
                if (cnt != 0) {
                    throw new TemplateModelException("Cannot perform an XPath query against a node set of " + cnt + " nodes. Expecting a single node." + ERRMSG_RECOMMEND_JAXEN);
                } else {
                    throw new TemplateModelException(ERRMSG_EMPTY_NODE_SET);
                }
            } else {
                throw new TemplateModelException("Cannot perform an XPath query against a " + context.getClass().getName() + ". Expecting a single org.w3c.dom.Node.");
            }
        } else {
            throw new TemplateModelException(ERRMSG_EMPTY_NODE_SET);
        }
    }
    Node node = (Node) context;
    try {
        XPath xpath = new XPath(xpathQuery, null, customPrefixResolver, XPath.SELECT, null);
        int ctxtNode = xpathContext.getDTMHandleFromNode(node);
        XObject xresult = xpath.execute(xpathContext, ctxtNode, customPrefixResolver);
        if (xresult instanceof XNodeSet) {
            NodeListModel result = new NodeListModel(node);
            result.xpathSupport = this;
            NodeIterator nodeIterator = xresult.nodeset();
            Node n;
            do {
                n = nodeIterator.nextNode();
                if (n != null) {
                    result.add(n);
                }
            } while (n != null);
            return result.size() == 1 ? result.get(0) : result;
        }
        if (xresult instanceof XBoolean) {
            return ((XBoolean) xresult).bool() ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE;
        }
        if (xresult instanceof XNull) {
            return null;
        }
        if (xresult instanceof XString) {
            return new SimpleScalar(xresult.toString());
        }
        if (xresult instanceof XNumber) {
            return new SimpleNumber(Double.valueOf(((XNumber) xresult).num()));
        }
        throw new TemplateModelException("Cannot deal with type: " + xresult.getClass().getName());
    } catch (TransformerException te) {
        throw new TemplateModelException(te);
    }
}
Also used : XPath(com.sun.org.apache.xpath.internal.XPath) NodeIterator(org.w3c.dom.traversal.NodeIterator) TemplateModelException(freemarker.template.TemplateModelException) XNull(com.sun.org.apache.xpath.internal.objects.XNull) XNumber(com.sun.org.apache.xpath.internal.objects.XNumber) Node(org.w3c.dom.Node) XBoolean(com.sun.org.apache.xpath.internal.objects.XBoolean) SimpleScalar(freemarker.template.SimpleScalar) XNodeSet(com.sun.org.apache.xpath.internal.objects.XNodeSet) SimpleNumber(freemarker.template.SimpleNumber) XString(com.sun.org.apache.xpath.internal.objects.XString) List(java.util.List) XObject(com.sun.org.apache.xpath.internal.objects.XObject) TransformerException(javax.xml.transform.TransformerException)

Aggregations

NodeIterator (org.w3c.dom.traversal.NodeIterator)9 Node (org.w3c.dom.Node)8 TransformerException (javax.xml.transform.TransformerException)3 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)3 Document (org.w3c.dom.Document)3 SimpleNumber (freemarker.template.SimpleNumber)2 SimpleScalar (freemarker.template.SimpleScalar)2 TemplateModelException (freemarker.template.TemplateModelException)2 List (java.util.List)2 XObject (org.apache.xpath.objects.XObject)2 CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)2 Element (org.w3c.dom.Element)2 DocumentTraversal (org.w3c.dom.traversal.DocumentTraversal)2 XPath (com.sun.org.apache.xpath.internal.XPath)1 XBoolean (com.sun.org.apache.xpath.internal.objects.XBoolean)1 XNodeSet (com.sun.org.apache.xpath.internal.objects.XNodeSet)1 XNull (com.sun.org.apache.xpath.internal.objects.XNull)1 XNumber (com.sun.org.apache.xpath.internal.objects.XNumber)1 XObject (com.sun.org.apache.xpath.internal.objects.XObject)1 XString (com.sun.org.apache.xpath.internal.objects.XString)1