Search in sources :

Example 1 with DocumentTraversal

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

the class AbstractStyleSheetAdapter method styleChanged.

/**
 * @param srcModel com.ibm.sed.css.model.interfaces.ICSSModel
 * @param removed com.ibm.sed.css.model.interfaces.ICSSSelector[]
 * @param added com.ibm.sed.css.model.interfaces.ICSSSelector[]
 * @param media java.lang.String
 */
public void styleChanged(ICSSModel srcModel, ICSSSelector[] removed, ICSSSelector[] added, String media) {
    Element element = getElement();
    if (element == null)
        // might released
        return;
    Document doc = element.getOwnerDocument();
    if (doc == null)
        // error
        return;
    // to notify GEF tree
    if (doc instanceof INodeNotifier) {
        Collection adapters = ((INodeNotifier) doc).getAdapters();
        if (adapters == null)
            return;
        Iterator it = adapters.iterator();
        if (it == null)
            return;
        while (it.hasNext()) {
            INodeAdapter adapter = (INodeAdapter) it.next();
            if (adapter instanceof ICSSStyleListener) {
                ((ICSSStyleListener) adapter).styleChanged(srcModel, removed, added, media);
            }
        }
    }
    if (styleChangedNodes == null) {
        styleChangedNodes = new HashSet();
    }
    try {
        int removedSelNum = removed != null ? removed.length : 0;
        int addedSelNum = added != null ? added.length : 0;
        NodeIterator iter = ((DocumentTraversal) doc).createNodeIterator(doc, NodeFilter.SHOW_ELEMENT, null, true);
        Node node;
        while ((node = iter.nextNode()) != null) {
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element elm = (Element) node;
                boolean match = false;
                int i;
                for (i = 0; i < removedSelNum && !match; i++) {
                    match = removed[i].match(elm, null);
                }
                for (i = 0; i < addedSelNum && !match; i++) {
                    match = added[i].match(elm, null);
                }
                if (match) {
                    if (!styleChangedNodes.contains(elm))
                        styleChangedNodes.add(elm);
                // notifyStyleChanged(elm);
                }
            }
        }
    } catch (ClassCastException ex) {
    // Document doesn't implement DocumentTraversal...
    }
}
Also used : NodeIterator(org.w3c.dom.traversal.NodeIterator) INodeAdapter(org.eclipse.wst.sse.core.internal.provisional.INodeAdapter) ICSSStyleListener(org.eclipse.wst.css.core.internal.event.ICSSStyleListener) Element(org.w3c.dom.Element) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) Iterator(java.util.Iterator) NodeIterator(org.w3c.dom.traversal.NodeIterator) Collection(java.util.Collection) DocumentTraversal(org.w3c.dom.traversal.DocumentTraversal) INodeNotifier(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier) HashSet(java.util.HashSet)

Example 2 with DocumentTraversal

use of org.w3c.dom.traversal.DocumentTraversal 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)

Aggregations

Document (org.w3c.dom.Document)2 Element (org.w3c.dom.Element)2 Node (org.w3c.dom.Node)2 DocumentTraversal (org.w3c.dom.traversal.DocumentTraversal)2 NodeIterator (org.w3c.dom.traversal.NodeIterator)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Transformer (javax.xml.transform.Transformer)1 TransformerException (javax.xml.transform.TransformerException)1 TransformerFactory (javax.xml.transform.TransformerFactory)1 DOMSource (javax.xml.transform.dom.DOMSource)1 StreamResult (javax.xml.transform.stream.StreamResult)1 ICSSStyleListener (org.eclipse.wst.css.core.internal.event.ICSSStyleListener)1 ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)1