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.nsesa.editor.gwt.core.client.ui.overlay.document.OverlayStrategySupport.java

License:EUPL

/**
 * Returns a list of all the amendable elements under this element by
 * enumerating the elements under the 'children' element of this element.
 * <p/>/*from  w ww  .ja va 2s.c  om*/
 * If there is no children element, an empty list will be returned.
 *
 * @return the list of children.
 */
public final List<Element> getChildren(Element element) {
    NodeList<Node> nodes = element.getChildNodes();
    List<Element> amendableElements = new ArrayList<Element>();
    int length = 0;
    try {
        length = nodes.getLength();
    } catch (Exception e) {
        LOG.log(Level.WARNING, "Caught exception (probably under Chrome): " + e, e);
    }
    for (int i = 0; i < length; i++) {
        if (nodes.getItem(i).getNodeType() == Node.ELEMENT_NODE) {
            Element el = nodes.getItem(i).cast();

            // do not include any of the 'property' tags
            // these elements are not included in our tree, but can be searched by their parent later on, to
            // get certain properties such as content, num, ...
            if (!asProperties.contains(el.getAttribute("type").toUpperCase())) {
                amendableElements.add(el);
            }
        }
    }
    return amendableElements;
}

From source file:org.nsesa.editor.gwt.core.client.ui.overlay.HTMLFormatter.java

License:EUPL

/**
 * This method is in facto responsible with the xml generation. For each child of the given
 * overlay widget ({@link org.nsesa.editor.gwt.core.client.ui.overlay.document.OverlayWidget#getChildOverlayWidgets()})
 * it tries to find out the corresponding browser element node and to extract from there the text content and the
 * css attributes values. The process continue then recursively for all descendants of the original overlay widget.
 *
 * @param widget The overlay widget that will be processed
 * @param depth/*from   www. j a  va2 s .  c  om*/
 * @return Xml representation of overlay widget as String
 */
public String toHTMLElement(final OverlayWidget widget, boolean withIndentation, int depth) {

    final StringBuilder sb = new StringBuilder();
    final String indent = withIndentation ? TextUtils.repeat(depth, "  ") : "";
    if (!widget.getOverlayElement().getClassName().contains(widget.getType()))
        widget.getOverlayElement().addClassName(widget.getType());
    sb.append(indent).append("<span class=\"").append(widget.getOverlayElement().getClassName())
            .append("\" data-type=\"").append(widget.getType()).append("\"").append(" data-ns=\"")
            .append(widget.getNamespaceURI()).append("\"");
    //get the attributes
    final LinkedHashMap<String, String> attrs = widget.getAttributes();
    if (!attrs.isEmpty()) {
        for (final Map.Entry<String, String> entry : attrs.entrySet()) {
            // do not add the class attribute
            if (!"class".equalsIgnoreCase(entry.getKey())) {
                if (entry.getValue() != null && entry.getValue().length() > 0) {
                    sb.append(" ").append(entry.getKey()).append("=").append("\"").append(entry.getValue())
                            .append("\"");
                }
            }
        }
    }
    // add class names
    sb.append(">");
    Element element = widget.getOverlayElement();
    NodeList<Node> nodes = element.getChildNodes();
    int length = nodes.getLength();
    if (length == 0) {
        // the root is all the time a new one
        // apply xml transformation for children
        for (final OverlayWidget child : widget.getChildOverlayWidgets()) {
            sb.append(toHTMLElement(child, false, depth + 1).trim());
        }
    } else {
        for (int i = 0; i < length; i++) {
            final short nodeType = nodes.getItem(i).getNodeType();
            Element childElement = nodes.getItem(i).cast();
            switch (nodeType) {
            case Node.ELEMENT_NODE:
                // get the amendable widget corresponding to this child and apply xml transformation
                // hopefully there is one amendable widget linked to this node
                OverlayWidget child = null;
                // try to find out a amendable widget linked to childElement
                for (OverlayWidget aw : widget.getChildOverlayWidgets()) {
                    if (aw.getOverlayElement().equals(childElement)) {
                        child = aw;
                        break;
                    }
                }
                if (child != null) {
                    sb.append(toHTMLElement(child, false, depth + 1).trim());
                } else {
                    LOG.warning("No amendable child widget found for element " + childElement.getNodeName());
                    sb.append(DOM.toString((com.google.gwt.user.client.Element) childElement.cast()));
                }
                break;
            case Node.TEXT_NODE:
                sb.append(TextUtils.escapeXML(nodes.getItem(i).getNodeValue().trim()));
                //sb.append(nodes.getItem(i).getNodeValue().trim());
                break;
            case Node.DOCUMENT_NODE:
                LOG.log(Level.WARNING, "There should be no document node here for " + element.getInnerHTML());
                break;
            }
        }
    }
    sb.append("</span>");
    return sb.toString();
}

From source file:org.nsesa.editor.gwt.core.client.util.NodeUtil.java

License:EUPL

public static Text getText(final Node node, boolean includeChildren) {
    final NodeList<Node> childNodes = node.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.getItem(i);
        if (child.getNodeType() == Node.TEXT_NODE) {
            return (Text) child;
        } else if (child.getNodeType() == Node.ELEMENT_NODE) {
            return getText(child, includeChildren);

        }/*from   w  w  w  . j  a  v  a2s . co m*/
    }
    return null;
}

From source file:org.nsesa.editor.gwt.core.client.util.NodeUtil.java

License:EUPL

public static void walk(final Node node, final NodeVisitor visitor) {
    visitor.visit(node);//from www  .jav a2  s  .c o m
    final NodeList<Node> childNodes = node.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.getItem(i);
        walk(child, visitor);
    }
}

From source file:org.nuxeo.ecm.platform.annotations.gwt.client.util.Visitor.java

License:Apache License

public void visit(Node node) {
    if (startNode == node) {
        processing = true;//from   ww  w . j av  a2  s  .  c  o  m
    } else if (endNode == node) {
        processing = false;
    }
    if (processor.doBreak()) {
        return;
    }

    NodeList list = node.getChildNodes();
    if (list == null || list.getLength() == 0) {
        processIf(node);
    } else {
        int length = list.getLength();
        Node[] nodes = new Node[list.getLength()];
        for (int x = 0; x < length; x++) {
            nodes[x] = list.getItem(x);
        }
        processIf(node);
        for (int x = 0; x < length; x++) {
            visit(nodes[x]);
        }
    }

}

From source file:org.nuxeo.ecm.platform.annotations.gwt.client.util.XPathUtil.java

License:Apache License

public List<Node> getNode(String xpath, Document document) {
    List<Node> nodes = new ArrayList<Node>();
    if (xpath.startsWith("//")) {
        xpath = xpath.substring(2);/*from  w w w . j a  v a 2s .  c  o m*/
        NodeList<Element> n = document.getElementsByTagName(xpath);
        for (int x = 0; x < n.getLength(); x++) {
            nodes.add(n.getItem(x));
        }
        return nodes;
    }
    Log.debug("XPathUtil#getNode -- xpath: " + xpath);
    String[] paths = xpath.split("/");
    Node result = document;
    for (String path : paths) {
        if ("".equals(path)) {
            continue;
        }
        NodeList<Node> nodeList = result.getChildNodes();
        String name = path.substring(0, path.indexOf("["));
        int index = Integer.parseInt(path.substring(path.indexOf("[") + 1, path.indexOf("]")));
        int counter = 1;
        for (int x = 0; x < nodeList.getLength(); x++) {
            Node node = nodeList.getItem(x);
            if (node.getNodeName().equalsIgnoreCase(name)) {
                if (isIgnored(node)) {// considered as text node
                    continue;
                }
                if (counter == index) {
                    result = node;
                    break;
                }
                counter++;
            }
        }

    }
    nodes.add(result);
    Log.debug("XPathUtil#getNode -- end function: ");
    return nodes;
}

From source file:org.nuxeo.ecm.platform.annotations.gwt.client.view.AnnotatedDocument.java

License:Apache License

private static void preDecorateDocument(Document document) {
    Log.debug("Predecorate document !");
    NodeList<Element> elements = document.getElementsByTagName("img");
    for (int x = 0; x < elements.getLength(); x++) {
        Element element = elements.getItem(x);
        DivElement divElement = document.createDivElement();
        divElement.getStyle().setProperty("position", "relative");
        divElement.setClassName(AnnotationConstant.IGNORED_ELEMENT);
        String path = xPathUtil.getXPath(element);
        path = XPathUtil.toIdableName(path);
        divElement.setId(path);/* w w  w .j av  a2  s.c o  m*/
        Element nextSibling = element.getNextSiblingElement();
        Element parent = element.getParentElement();
        if (nextSibling == null) {
            parent.appendChild(divElement);
        } else {
            parent.insertBefore(divElement, nextSibling);
        }
        divElement.appendChild(element);
    }
}

From source file:org.nuxeo.ecm.platform.annotations.gwt.client.view.AnnotatedDocument.java

License:Apache License

public void updateSelectedAnnotation(int index) {
    Annotation annotation = annotations.get(index);
    BodyElement bodyElement = Document.get().getBody();
    if (!(annotation.getXpointer() instanceof NullRangeXPointer)) {
        NodeList<Element> spans = bodyElement.getElementsByTagName("span");
        NodeList<Element> as = bodyElement.getElementsByTagName("div");
        int scrollTop = Integer.MAX_VALUE;
        int scrollLeft = Integer.MAX_VALUE;
        for (int x = 0; x < spans.getLength(); x++) {
            Element element = spans.getItem(x);
            if (processElement(annotation, element)) {
                int[] absTopLeft = Utils.getAbsoluteTopLeft(element, Document.get());
                if (absTopLeft[0] < scrollTop) {
                    scrollTop = absTopLeft[0];
                }/*from  w w w . j av a2s . c  o  m*/
                if (absTopLeft[1] < scrollLeft) {
                    scrollLeft = absTopLeft[1];
                }
            }
        }
        for (int x = 0; x < as.getLength(); x++) {
            Element element = as.getItem(x);
            if (processElement(annotation, element)) {
                int[] absTopLeft = Utils.getAbsoluteTopLeft(element, Document.get());
                if (absTopLeft[0] < scrollTop) {
                    scrollTop = absTopLeft[0];
                }
                if (absTopLeft[1] < scrollLeft) {
                    scrollLeft = absTopLeft[1];
                }
            }
        }

        scrollLeft = scrollLeft == Integer.MAX_VALUE ? 0 : scrollLeft;
        scrollTop = scrollTop == Integer.MAX_VALUE ? 0 : scrollTop;
        Window.scrollTo(scrollLeft, scrollTop);
    }
}

From source file:org.nuxeo.ecm.platform.annotations.gwt.client.view.AnnotatedDocument.java

License:Apache License

public void hideAnnotations() {
    BodyElement bodyElement = Document.get().getBody();
    NodeList<Element> spans = bodyElement.getElementsByTagName("span");
    NodeList<Element> divs = bodyElement.getElementsByTagName("div");

    for (int x = 0; x < spans.getLength(); x++) {
        Element element = spans.getItem(x);
        CSSClassManager manager = new CSSClassManager(element);
        if (manager.isClassPresent(AnnotationConstant.DECORATE_CLASS_NAME)) {
            manager.removeClass(AnnotationConstant.DECORATE_CLASS_NAME);
            manager.addClass(AnnotationConstant.DECORATE_NOT_CLASS_NAME);
        }//from w ww  .jav a 2 s .c o m
    }

    for (int x = 0; x < divs.getLength(); x++) {
        Element element = divs.getItem(x);
        CSSClassManager manager = new CSSClassManager(element);
        if (manager.isClassPresent(AnnotationConstant.DECORATE_CLASS_NAME)) {
            manager.removeClass(AnnotationConstant.DECORATE_CLASS_NAME);
            manager.addClass(AnnotationConstant.DECORATE_NOT_CLASS_NAME);
        }
    }
    setAnnotationsShown(false);
}

From source file:org.nuxeo.ecm.platform.annotations.gwt.client.view.AnnotatedDocument.java

License:Apache License

public void showAnnotations() {
    BodyElement bodyElement = Document.get().getBody();
    NodeList<Element> spans = bodyElement.getElementsByTagName("span");
    NodeList<Element> divs = bodyElement.getElementsByTagName("div");

    for (int x = 0; x < spans.getLength(); x++) {
        Element element = spans.getItem(x);
        CSSClassManager manager = new CSSClassManager(element);
        if (manager.isClassPresent(AnnotationConstant.DECORATE_NOT_CLASS_NAME)) {
            manager.removeClass(AnnotationConstant.DECORATE_NOT_CLASS_NAME);
            manager.addClass(AnnotationConstant.DECORATE_CLASS_NAME);
        }/*from ww w  .j a v  a 2 s  . com*/
        if (manager.isClassPresent(AnnotationConstant.SELECTED_NOT_CLASS_NAME)) {
            manager.removeClass(AnnotationConstant.SELECTED_NOT_CLASS_NAME);
            manager.addClass(AnnotationConstant.SELECTED_CLASS_NAME);
        }
    }

    for (int x = 0; x < divs.getLength(); x++) {
        Element element = divs.getItem(x);
        CSSClassManager manager = new CSSClassManager(element);
        if (manager.isClassPresent(AnnotationConstant.DECORATE_NOT_CLASS_NAME)) {
            manager.removeClass(AnnotationConstant.DECORATE_NOT_CLASS_NAME);
            manager.addClass(AnnotationConstant.DECORATE_CLASS_NAME);
        }
        if (manager.isClassPresent(AnnotationConstant.SELECTED_NOT_CLASS_NAME)) {
            manager.removeClass(AnnotationConstant.SELECTED_NOT_CLASS_NAME);
            manager.addClass(AnnotationConstant.SELECTED_CLASS_NAME);
        }
    }
    setAnnotationsShown(true);
}