Search in sources :

Example 1 with NodeIterator

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

the class XalanXPathSupport method executeQuery.

/* " + ERRMSG_RECOMMEND_JAXEN;*/
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.");
                } 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(org.apache.xpath.XPath) NodeIterator(org.w3c.dom.traversal.NodeIterator) TemplateModelException(freemarker.template.TemplateModelException) XNull(org.apache.xpath.objects.XNull) XNumber(org.apache.xpath.objects.XNumber) Node(org.w3c.dom.Node) XBoolean(org.apache.xpath.objects.XBoolean) SimpleScalar(freemarker.template.SimpleScalar) XNodeSet(org.apache.xpath.objects.XNodeSet) SimpleNumber(freemarker.template.SimpleNumber) XString(org.apache.xpath.objects.XString) List(java.util.List) XObject(org.apache.xpath.objects.XObject) TransformerException(javax.xml.transform.TransformerException)

Example 2 with NodeIterator

use of org.w3c.dom.traversal.NodeIterator 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 3 with NodeIterator

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

the class DocumentImpl method getElementsByTagNameNS.

/**
 */
public NodeList getElementsByTagNameNS(String uri, String tagName) {
    if (tagName == null)
        return new NodeListImpl();
    NodeIterator it = createNodeIterator(this, NodeFilter.SHOW_ALL, null, false);
    if (it == null)
        return new NodeListImpl();
    NodeListImpl elements = new NodeListImpl();
    if (uri != null && uri.length() == 1 && uri.charAt(0) == '*') {
        // do not care
        uri = null;
    }
    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;
        ElementImpl element = (ElementImpl) node;
        if (tagName != null) {
            String localName = element.getLocalName();
            if (localName == null || !localName.equals(tagName))
                continue;
        }
        if (uri != null) {
            String nsURI = element.getNamespaceURI();
            if (nsURI == null || !nsURI.equals(uri))
                continue;
        }
        elements.appendNode(element);
    }
    return elements;
}
Also used : NodeIterator(org.w3c.dom.traversal.NodeIterator) Node(org.w3c.dom.Node)

Example 4 with NodeIterator

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

the class ElementImpl method getElementsByTagNameNS.

/**
 */
public NodeList getElementsByTagNameNS(String uri, String tagName) {
    if (tagName == null)
        return new NodeListImpl();
    DocumentImpl document = (DocumentImpl) getOwnerDocument();
    if (document == null)
        return new NodeListImpl();
    NodeIterator it = document.createNodeIterator(this, NodeFilter.SHOW_ALL, null, false);
    if (it == null)
        return new NodeListImpl();
    NodeListImpl elements = new NodeListImpl();
    if (uri != null && uri.length() == 1 && uri.charAt(0) == '*') {
        // do not care
        uri = null;
    }
    if (tagName.length() == 1 && tagName.charAt(0) == '*') {
        // do not care
        tagName = null;
    }
    // skip the first node since it is the root from createNodeIterator
    it.nextNode();
    for (Node node = it.nextNode(); node != null; node = it.nextNode()) {
        if (node.getNodeType() != ELEMENT_NODE)
            continue;
        ElementImpl element = (ElementImpl) node;
        if (tagName != null) {
            String localName = element.getLocalName();
            if (localName == null || !localName.equals(tagName))
                continue;
        }
        if (uri != null) {
            String nsURI = element.getNamespaceURI();
            if (nsURI == null || !nsURI.equals(uri))
                continue;
        }
        elements.appendNode(element);
    }
    return elements;
}
Also used : NodeIterator(org.w3c.dom.traversal.NodeIterator) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode)

Example 5 with NodeIterator

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

the class ElementImpl method getElementsByTagName.

/**
 * getElementsByTagName method
 *
 * @return org.w3c.dom.NodeList
 * @param tagName
 *            java.lang.String
 */
public NodeList getElementsByTagName(String tagName) {
    if (tagName == null)
        return new NodeListImpl();
    DocumentImpl document = (DocumentImpl) getOwnerDocument();
    if (document == null)
        return new NodeListImpl();
    NodeIterator it = document.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;
    }
    // skip the first node since it is the root from createNodeIterator
    it.nextNode();
    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);
    }
    return elements;
}
Also used : NodeIterator(org.w3c.dom.traversal.NodeIterator) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode)

Aggregations

NodeIterator (org.w3c.dom.traversal.NodeIterator)11 Node (org.w3c.dom.Node)10 DocumentTraversal (org.w3c.dom.traversal.DocumentTraversal)4 TransformerException (javax.xml.transform.TransformerException)3 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)3 Document (org.w3c.dom.Document)3 Element (org.w3c.dom.Element)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 OVFNetworkTO (com.cloud.agent.api.to.deployasis.OVFNetworkTO)1 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