Example usage for com.google.gwt.dom.client NodeList getLength

List of usage examples for com.google.gwt.dom.client NodeList getLength

Introduction

In this page you can find the example usage for com.google.gwt.dom.client NodeList getLength.

Prototype

public int getLength() 

Source Link

Usage

From source file:org.vaadin.gwtgraphics.client.impl.util.VMLUtil.java

License:Apache License

private static Element getChildElementWithTagName(Element element, String name) {
    NodeList<Node> nodes = element.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.getItem(i);
        if (node.getNodeName().equals(name)) {
            return Element.as(node);
        }/*  w w w. j ava 2s  .  c o  m*/
    }
    return null;
}

From source file:org.vectomatic.dom.svg.impl.SVGParserImplOpera.java

License:Open Source License

/**
 * Fix for opera./*from w ww .  jav  a2s  .  c om*/
 * SVG Objects created by the parser and imported do not seem to recognize their
 * CSS attributes. Reapplying them seems to solve the issue.
 * The CSS style attributes which contain hrefs to svg elements are corrupted an
 * need to be fixed as well.
 * @param root
 */
private static void operaFix(Element root) {
    Stack<Element> stack = new Stack<Element>();
    stack.push(root);
    while (!stack.empty()) {
        Element element = stack.pop();
        if (SVGConstants.SVG_NAMESPACE_URI.equals(DOMHelper.getNamespaceURI(element))) {
            OMSVGAnimatedString cn = element.<SVGElement>cast().getClassName_();
            if (cn != null) {
                String value = cn.getBaseVal();
                if (value != null && value.length() > 0) {
                    cn.setBaseVal(value);
                }
            }
            if (element.hasAttribute(SVGConstants.SVG_STYLE_ATTRIBUTE)) {
                element.setAttribute(SVGConstants.SVG_STYLE_ATTRIBUTE,
                        element.getAttribute(SVGConstants.SVG_STYLE_ATTRIBUTE));
                OMSVGStyle style = element.getStyle().<OMSVGStyle>cast();
                fixProperty(style, SVGConstants.CSS_FILL_PROPERTY);
                fixProperty(style, SVGConstants.CSS_STROKE_PROPERTY);
            }
        }
        NodeList<Node> childNodes = element.getChildNodes();
        for (int i = 0, length = childNodes.getLength(); i < length; i++) {
            Node node = childNodes.getItem(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                stack.push(node.<Element>cast());
            }
        }
    }
}

From source file:org.vectomatic.dom.svg.utils.DOMHelper.java

License:Open Source License

/**
 * Returns an XPath expression which enables reaching the specified node
 * from the root node//from  ww w  . j a va2s .co m
 * @param node
 * The node to reach
 * @param root
 * The root node, or null to specify the document root
 * @return
 * An XPath expression which enables reaching the specified node
 * from the root node
 */
public static String getXPath(Node node, Node root) {
    StringBuilder builder = new StringBuilder();
    Node parentNode;
    while ((node != root) && (parentNode = node.getParentNode()) != null) {
        NodeList<Node> siblings = parentNode.getChildNodes();
        int index = 0;
        for (int i = 0, length = siblings.getLength(); i < length; i++) {
            Node sibling = siblings.getItem(i);
            if (sibling.getNodeType() == Node.ELEMENT_NODE) {
                index++;
                if (node == sibling) {
                    builder.insert(0, "/*[" + index + "]");
                    break;
                }
            }
        }
        node = parentNode;
    }
    return builder.toString();
}

From source file:org.vectomatic.svg.edit.client.engine.SVGModel.java

License:Open Source License

private SVGIconTreeNode buildTreeNode(SVGElement element, List<SVGTreeNode.IProperty> properties) {
    SVGIconTreeNode node = new SVGIconTreeNode(element, properties);
    NodeList<Node> childNodes = element.getChildNodes();
    for (int i = 0, length = childNodes.getLength(); i < length; i++) {
        Node child = childNodes.getItem(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element childElement = child.cast();
            if (SVGConstants.SVG_NAMESPACE_URI.equals(DOMHelper.getNamespaceURI(childElement))
                    && hasGraphicalElements((SVGElement) childElement.cast())) {
                node.add(buildTreeNode((SVGElement) childElement.cast(), properties));
            }/*from w  w w  .j a  v  a  2 s.  c  o m*/
        }
    }
    return node;
}

From source file:org.vectomatic.svg.edit.client.engine.SVGModel.java

License:Open Source License

/**
 * Returns true if the subtree rooted at the specified element
 * contains at least one graphical element which is not
 * part of a definition element//from  ww w.ja v  a 2  s  . com
 * @param element
 * @return
 */
public static boolean hasGraphicalElements(SVGElement element) {
    if (graphicalElementNames.contains(DOMHelper.getLocalName(element))) {
        return true;
    }
    NodeList<Node> childNodes = element.getChildNodes();
    for (int i = 0, length = childNodes.getLength(); i < length; i++) {
        Node child = childNodes.getItem(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            if (SVGConstants.SVG_NAMESPACE_URI.equals(DOMHelper.getNamespaceURI((Element) child.cast()))
                    && !definitionElementNames.contains(DOMHelper.getLocalName(child))
                    && hasGraphicalElements((SVGElement) child.cast())) {
                return true;
            }
        }
    }
    return false;
}

From source file:org.vectomatic.svg.edit.client.engine.SVGModel.java

License:Open Source License

/**
 * Completes the specified SVG subtree so that all the proper SVG elements have
 * an associated title and desc elements
 * @param root The SVG subtree to complete
 *//*from  w w  w. j av a2s  . com*/
public void createTitleDesc(SVGElement root) {
    // Perform a DFS of the tree to collect all the SVG elements
    // which can have a title and desc but do not yet have one
    List<SVGElement> incompleteElements = new ArrayList<SVGElement>();
    Stack<SVGElement> stack = new Stack<SVGElement>();
    stack.push(root);
    while (stack.size() > 0) {
        SVGElement element = stack.pop();
        if (hasTitleDesc(element) && (DOMHelper.evaluateNodeXPath(element, TITLE_PROPERTY.getXPath(),
                SVGPrefixResolver.INSTANCE) == null
                || DOMHelper.evaluateNodeXPath(element, DESC_PROPERTY.getXPath(),
                        SVGPrefixResolver.INSTANCE) == null)) {
            incompleteElements.add(element);
        }
        NodeList<Node> childNodes = element.getChildNodes();
        for (int i = 0, length = childNodes.getLength(); i < length; i++) {
            Node child = childNodes.getItem(i);
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                Element childElement = child.cast();
                if (SVGConstants.SVG_NAMESPACE_URI.equals(DOMHelper.getNamespaceURI(childElement))) {
                    stack.push((SVGElement) childElement.cast());
                }
            }
        }
    }

    // Create the missing desc and titles
    SVGDocument document = root.getOwnerDocument().cast();
    for (SVGElement element : incompleteElements) {
        SVGTitleElement title = DOMHelper.evaluateNodeXPath(element, TITLE_PROPERTY.getXPath(),
                SVGPrefixResolver.INSTANCE);
        if (title == null) {
            title = DOMHelper
                    .createElementNS(document, SVGConstants.SVG_NAMESPACE_URI, SVGConstants.SVG_TITLE_TAG)
                    .cast();
            title.appendChild(document.createTextNode(generateName(element)));
            Node firstChild = element.getFirstChild();
            if (firstChild == null) {
                element.appendChild(title);
            } else {
                element.insertBefore(title, firstChild);
            }
        }
        SVGDescElement desc = DOMHelper.evaluateNodeXPath(element, DESC_PROPERTY.getXPath(),
                SVGPrefixResolver.INSTANCE);
        if (desc == null) {
            desc = DOMHelper
                    .createElementNS(document, SVGConstants.SVG_NAMESPACE_URI, SVGConstants.SVG_DESC_TAG)
                    .cast();
            desc.appendChild(document.createTextNode(""));
            element.insertAfter(desc, title);
        }
    }
}

From source file:org.vectomatic.svg.edit.client.engine.SVGProcessor.java

License:Open Source License

public static int normalizeIds(OMSVGElement srcSvg) {
    docId++;//w w  w.ja  v a2 s . com
    // Collect all the original element ids and replace them with a
    // normalized id
    int idIndex = 0;
    Map<String, Element> idToElement = new HashMap<String, Element>();
    Map<String, String> idToNormalizedId = new HashMap<String, String>();
    List<Element> queue = new ArrayList<Element>();
    queue.add(srcSvg.getElement());
    while (queue.size() > 0) {
        Element element = queue.remove(0);
        String id = element.getId();
        if (id != null) {
            idToElement.put(id, element);
            String normalizedId = "d" + docId + "_" + idIndex++;
            idToNormalizedId.put(id, normalizedId);
            element.setId(normalizedId);
        }
        NodeList<Node> childNodes = element.getChildNodes();
        for (int i = 0, length = childNodes.getLength(); i < length; i++) {
            Node childNode = childNodes.getItem(i);
            if (childNode.getNodeType() == Node.ELEMENT_NODE) {
                queue.add((Element) childNode.cast());
            }
        }
    }

    // Change all the attributes which are URI references
    Set<String> attNames = new HashSet<String>(Arrays.asList(new String[] { "clip-path", "mask", "marker-start",
            "marker-mid", "marker-end", "fill", "stroke", "filter", "cursor", "style" }));
    queue.add(srcSvg.getElement());
    IdRefTokenizer tokenizer = GWT.create(IdRefTokenizer.class);
    while (queue.size() > 0) {
        Element element = queue.remove(0);
        if (DOMHelper.hasAttributeNS(element, SVGConstants.XLINK_NAMESPACE_URI,
                SVGConstants.XLINK_HREF_ATTRIBUTE)) {
            String idRef = DOMHelper.getAttributeNS(element, SVGConstants.XLINK_NAMESPACE_URI,
                    SVGConstants.XLINK_HREF_ATTRIBUTE).substring(1);
            String normalizeIdRef = idToNormalizedId.get(idRef);
            DOMHelper.setAttributeNS(element, SVGConstants.XLINK_NAMESPACE_URI,
                    SVGConstants.XLINK_HREF_ATTRIBUTE, "#" + normalizeIdRef);
        }
        NamedNodeMap<Attr> attrs = DOMHelper.getAttributes(element);
        for (int i = 0, length = attrs.getLength(); i < length; i++) {
            Attr attr = attrs.item(i);
            if (attNames.contains(attr.getName())) {
                StringBuilder builder = new StringBuilder();
                tokenizer.tokenize(attr.getValue());
                IdRefTokenizer.IdRefToken token;
                while ((token = tokenizer.nextToken()) != null) {
                    String value = token.getValue();
                    if (token.getKind() == IdRefTokenizer.IdRefToken.DATA) {
                        builder.append(value);
                    } else {
                        value = idToNormalizedId.get(value);
                        builder.append(value == null ? token.getValue() : value);
                    }
                }
                attr.setValue(builder.toString());
            }
        }
        NodeList<Node> childNodes = element.getChildNodes();
        for (int i = 0, length = childNodes.getLength(); i < length; i++) {
            Node childNode = childNodes.getItem(i);
            if (childNode.getNodeType() == Node.ELEMENT_NODE) {
                queue.add((Element) childNode.cast());
            }
        }
    }
    return docId;
}

From source file:org.vectomatic.svg.samples.client.xpath.XPathSample.java

License:Open Source License

private void visit(Node node, Element parentSpan) {
    SpanElement span = doc.createSpanElement();
    nodeToSpan.put(node, span);/*from   w  ww .  j a va 2s  .  com*/

    parentSpan.appendChild(span);
    NodeList<Node> childNodes = node.getChildNodes();
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE: {
        Element element = node.<Element>cast();
        span.addClassName(css.element());

        // Populate the span with a start tag
        span.appendChild(createMarkup("<"));
        String tagName = element.getTagName();
        int index = tagName.indexOf(":");
        if (index != -1) {
            span.appendChild(doc.createTextNode(tagName.substring(0, index)));
            span.appendChild(createMarkup(":"));
            span.appendChild(doc.createTextNode(tagName.substring(index + 1)));
        } else {
            span.appendChild(doc.createTextNode(tagName));
        }

        // Create the attribute nodes spans
        NamedNodeMap<Attr> attributes = DOMHelper.getAttributes(element);
        for (int i = 0, length = attributes.getLength(); i < length; i++) {
            span.appendChild(doc.createTextNode(" "));

            SpanElement attrSpan = doc.createSpanElement();
            attrSpan.addClassName(css.attribute());
            span.appendChild(attrSpan);
            Attr attr = attributes.item(i);
            nodeToSpan.put(attr, attrSpan);

            String attrName = attr.getName();
            index = attrName.indexOf(":");
            if (index != -1) {
                attrSpan.appendChild(doc.createTextNode(attrName.substring(0, index)));
                attrSpan.appendChild(createMarkup(":"));
                attrSpan.appendChild(doc.createTextNode(attrName.substring(index + 1)));
            } else {
                attrSpan.appendChild(doc.createTextNode(attrName));
            }
            attrSpan.appendChild(createMarkup("=\""));
            attrSpan.appendChild(doc.createTextNode(attr.getValue()));
            attrSpan.appendChild(createMarkup("\""));
        }
        span.appendChild(createMarkup(childNodes.getLength() > 0 ? ">" : "/>"));
    }
        break;
    case Node.TEXT_NODE: {
        // Populate span with text
        Text text = node.<Text>cast();
        span.addClassName(css.text());
        span.appendChild(doc.createTextNode(text.getData()));
    }
        break;
    }

    for (int i = 0, count = node.getChildCount(); i < count; i++) {
        visit(childNodes.getItem(i), span);
    }

    if (childNodes.getLength() > 0 && node.getNodeType() == Node.ELEMENT_NODE) {
        Element element = node.<Element>cast();
        span.addClassName(css.element());

        // Populate the span with a close tag
        span.appendChild(createMarkup("</"));
        String tagName = element.getTagName();
        int index = tagName.indexOf(":");
        if (index != -1) {
            span.appendChild(doc.createTextNode(tagName.substring(0, index)));
            span.appendChild(createMarkup(":"));
            span.appendChild(doc.createTextNode(tagName.substring(index + 1)));
        } else {
            span.appendChild(doc.createTextNode(tagName));
        }
        span.appendChild(createMarkup(">"));
    }
}

From source file:org.waveprotocol.wave.client.clipboard.Clipboard.java

License:Apache License

private String maybeGetAttributeFromContainer(Element srcContainer, String attribName) {
    NodeList<Element> elementsByClassName = DomHelper.getElementsByClassName(srcContainer, MAGIC_CLASSNAME);
    if (elementsByClassName != null && elementsByClassName.getLength() > 0) {
        return elementsByClassName.getItem(0).getAttribute(attribName);
    }/*  w w  w.j a  v a2  s.co  m*/
    return null;
}

From source file:org.waveprotocol.wave.client.common.util.DomHelper.java

License:Apache License

/**
 * Gets a list of descendants of e that match the given class name.
 *
 * If the browser has the native method, that will be called. Otherwise, it
 * traverses descendents of the given element and returns the list of elements
 * with matching classname./* w w  w .  ja  va2s.  c o  m*/
 *
 * @param e
 * @param className
 */
public static NodeList<Element> getElementsByClassName(Element e, String className) {
    if (QuirksConstants.SUPPORTS_GET_ELEMENTS_BY_CLASSNAME) {
        return getElementsByClassNameNative(e, className);
    } else {
        NodeList<Element> all = e.getElementsByTagName("*");
        if (all == null) {
            return null;
        }
        JsArray<Element> ret = JavaScriptObject.createArray().cast();
        for (int i = 0; i < all.getLength(); ++i) {
            Element item = all.getItem(i);
            if (className.equals(item.getClassName())) {
                ret.push(item);
            }
        }
        return ret.cast();
    }
}