Example usage for com.google.gwt.dom.client Node getNodeType

List of usage examples for com.google.gwt.dom.client Node getNodeType

Introduction

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

Prototype

@Override
    public short getNodeType() 

Source Link

Usage

From source file:bz.davide.dmxmljson.unmarshalling.dom.gwt.GWTDOMStructure.java

License:Open Source License

private void extractChildElementByName() {
    NodeList nl = this.element.element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.getItem(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;

            String tagName = element.getTagName().toLowerCase();
            String[] parts = extractNameAndSubtype(tagName);

            String attrName = parts[0];
            ElementAndSubtype elementAndSubtype = new ElementAndSubtype();
            elementAndSubtype.element = element;
            elementAndSubtype.subtype = parts[1];

            ArrayList<ElementAndSubtype> elements = this.elementsByName.get(attrName);
            if (elements == null) {
                elements = new ArrayList<ElementAndSubtype>();
                this.elementsByName.put(attrName, elements);
            }// w w  w. j  a  v a2 s. c o m
            elements.add(elementAndSubtype);

            ElementAndSubtype childElementAndSubtype = new ElementAndSubtype();
            childElementAndSubtype.element = element;
            childElementAndSubtype.subtype = tagName;
            // Convert first letter to upper case, because java classes start normally with upper case
            childElementAndSubtype.subtype = childElementAndSubtype.subtype.substring(0, 1).toUpperCase()
                    + childElementAndSubtype.subtype.substring(1);
            this.childNodes.add(childElementAndSubtype);
        }
        if (node.getNodeType() == Node.TEXT_NODE) {
            String txt = node.getNodeValue();
            if (txt.trim().length() > 0) {
                ElementAndSubtype childElementAndSubtype = new ElementAndSubtype();
                childElementAndSubtype.element = node;
                childElementAndSubtype.subtype = "TextNode";
                this.childNodes.add(childElementAndSubtype);
            }
        }
    }
}

From source file:bz.davide.dmxmljson.unmarshalling.dom.gwt.GWTDOMValue.java

License:Open Source License

void recursiveText(Node node, StringBuffer sb) {
    if (node.getNodeType() == Node.TEXT_NODE) {
        sb.append(node.getNodeValue());/*ww w.  j  a  v  a  2  s.  co  m*/
    }
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        NodeList<Node> nl = node.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            this.recursiveText(nl.getItem(i), sb);
        }
    }
}

From source file:cc.alcina.framework.gwt.client.util.WidgetUtils.java

License:Apache License

public static Element getElementForPositioning0(Element from) {
    assert tempPositioningText == null;
    if (!isVisibleAncestorChain(from)) {
        return null;
    }//from  w ww  .j  a v a 2 s . c o  m
    boolean hidden = isZeroOffsetDims(from);
    int kidCount = from.getChildCount();
    if (kidCount != 0 && !hidden) {
        return from;
    }
    Node parent = from.getParentNode();
    if (parent != null && parent.getFirstChild() == from && parent.getNodeType() == Node.ELEMENT_NODE
            && !isZeroOffsetDims((Element) parent)) {
        return (Element) parent;
    }
    ClientNodeIterator itr = new ClientNodeIterator(from, ClientNodeIterator.SHOW_ALL);
    Element fromContainingBlock = DomUtils.getContainingBlock(from);
    Node node = from;
    int insertTextIfOffsetMoreThanXChars = 100;
    while ((node = node.getPreviousSibling()) != null) {
        if (node.getNodeType() == Node.TEXT_NODE) {
            insertTextIfOffsetMoreThanXChars -= TextUtils.normalizeWhitespaceAndTrim(node.getNodeValue())
                    .length();
            if (insertTextIfOffsetMoreThanXChars < 0) {
                // this causes a relayout - so we try and avoid. most of the
                // time, positioning elements will contain text (or be from
                // a friendly browser), or be at the start of a block elt)
                tempPositioningText = Document.get().createTextNode("---");
                from.appendChild(tempPositioningText);
                return from;
            }
        }
    }
    // give up after 50 node iterations (big tables maybe)
    int max = 50;
    while ((node = itr.nextNode()) != null && max-- > 0) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (!isZeroOffsetDims(node.getParentElement()) && node.getNodeName().equalsIgnoreCase("img")) {
                return (Element) node;
            }
            if (!UIObject.isVisible((Element) node)) {
                itr.skipChildren();
            }
        } else {
            // text
            if (!isZeroOffsetDims(node.getParentElement())
                    // we don't want the combined ancestor of everyone...
                    && (!node.getParentElement().isOrHasChild(from) ||
                    // but we do want <p><a><b>*some-text*</b></p>
                            DomUtils.getContainingBlock(node) == fromContainingBlock)) {
                return node.getParentElement();
            }
        }
    }
    return from.getParentElement();
}

From source file:ch.bergturbenthal.hs485.frontend.gwtfrontend.client.svg.SVGProcessor.java

License:Open Source License

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

    // Change all the attributes which are URI references
    final 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());
    final IdRefTokenizer tokenizer = GWT.create(IdRefTokenizer.class);
    while (queue.size() > 0) {
        final Element element = queue.remove(0);
        if (DOMHelper.hasAttributeNS(element, SVGConstants.XLINK_NAMESPACE_URI,
                SVGConstants.XLINK_HREF_ATTRIBUTE)) {
            final String idRef = DOMHelper.getAttributeNS(element, SVGConstants.XLINK_NAMESPACE_URI,
                    SVGConstants.XLINK_HREF_ATTRIBUTE).substring(1);
            final String normalizeIdRef = idToNormalizedId.get(idRef);
            DOMHelper.setAttributeNS(element, SVGConstants.XLINK_NAMESPACE_URI,
                    SVGConstants.XLINK_HREF_ATTRIBUTE, "#" + normalizeIdRef);
        }
        final NamedNodeMap<Attr> attrs = DOMHelper.getAttributes(element);
        for (int i = 0, length = attrs.getLength(); i < length; i++) {
            final Attr attr = attrs.item(i);
            if (attNames.contains(attr.getName())) {
                final 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());
            }
        }
        final NodeList<Node> childNodes = element.getChildNodes();
        for (int i = 0, length = childNodes.getLength(); i < length; i++) {
            final Node childNode = childNodes.getItem(i);
            if (childNode.getNodeType() == Node.ELEMENT_NODE)
                queue.add((Element) childNode.cast());
        }
    }
    return docId;
}

From source file:client.net.sf.saxon.ce.dom.HTMLDocumentWrapper.java

License:Mozilla Public License

public HTMLDocumentWrapper(Node doc, String baseURI, Configuration config, DocType newDocType) {
    super(doc, null, 0);
    //        if (doc.getNodeType() != Node.DOCUMENT_NODE) {
    //            throw new IllegalArgumentException("Node must be a DOM Document");
    //        }/*w w  w  .  j  a  va  2 s.c  om*/

    nodeKind = Type.DOCUMENT;

    if ((baseURI == null || baseURI == "") && doc.getNodeType() == Type.DOCUMENT) {
        baseURI = ((Document) doc).getURL();
        this.baseURI = (baseURI != null && baseURI != "") ? baseURI : getBaseURI((Document) doc);
    } else {
        this.baseURI = baseURI;
    }

    // affects selectID() behaviour:
    isHttpRequested = (newDocType == DocType.NONHTML);

    docWrapper = this;
    domLevel3 = true;
    setConfiguration(config);
    // crude test for XHTML by seeing if there's an identifying xmlns attribute on any
    // top-level html element
    if (newDocType != DocType.UNKNOWN) {
        this.htmlType = newDocType;
        return;
    }
    // need to determine whether HTML, XHTML or neither so as to control case of node names
    // and distinguish other XML/HTML behaviours - like SelectID
    try {
        AxisIterator iter = this.iterateAxis(Axis.CHILD);
        while (true) {
            NodeInfo n = (NodeInfo) iter.next();
            if (n == null) {
                break;
            } else if (n.getNodeKind() == Type.ELEMENT) {
                //                   String uri = n.getURI();
                //                   if (uri != null && uri.length() > 0) {
                //                      htmlType = (uri.equals(NamespaceConstant.XHTML))? DocType.XHTML : DocType.NONHTML;
                //                      return;
                //                   }
                String rawLocal = ((HTMLNodeWrapper) n).getRawLocalName().toLowerCase();
                if (rawLocal.equals("html")) {
                    NamespaceBinding[] nb = n.getDeclaredNamespaces(null);
                    htmlType = DocType.HTML;
                    for (NamespaceBinding nBinding : nb) {
                        if (nBinding.getURI().equals(NamespaceConstant.XHTML)) {
                            htmlType = DocType.XHTML;
                            break;
                        }
                    }
                } else {
                    htmlType = DocType.NONHTML;
                }
                break; // only check first element
            }
        }
    } catch (Exception e) {
    }
}

From source file:client.net.sf.saxon.ce.dom.HTMLNodeWrapper.java

License:Mozilla Public License

/**
 * Factory method to wrap a DOM node with a wrapper that implements the Saxon
 * NodeInfo interface./*  w  w  w . ja v  a 2s .co  m*/
 * @param node        The DOM node
 * @param docWrapper  The wrapper for the containing Document node     *
 * @param parent      The wrapper for the parent of the JDOM node
 * @param index       The position of this node relative to its siblings
 * @return            The new wrapper for the supplied node
 */

protected HTMLNodeWrapper makeWrapper(Node node, HTMLDocumentWrapper docWrapper, HTMLNodeWrapper parent,
        int index) {
    HTMLNodeWrapper wrapper;
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
    case DOCUMENT_FRAGMENT_NODE:
        return docWrapper;
    case Node.ELEMENT_NODE:
        wrapper = new HTMLNodeWrapper(node, parent, index);
        wrapper.nodeKind = Type.ELEMENT;
        break;
    case Type.ATTRIBUTE:
        wrapper = new HTMLNodeWrapper(node, parent, index);
        wrapper.nodeKind = Type.ATTRIBUTE;
        break;
    case Node.TEXT_NODE:
        wrapper = new HTMLNodeWrapper(node, parent, index);
        wrapper.nodeKind = Type.TEXT;
        break;
    case CDATA_SECTION_NODE:
        wrapper = new HTMLNodeWrapper(node, parent, index);
        wrapper.nodeKind = Type.TEXT;
        break;
    case Type.COMMENT:
        wrapper = new HTMLNodeWrapper(node, parent, index);
        wrapper.nodeKind = Type.COMMENT;
        break;
    case Type.PROCESSING_INSTRUCTION:
        wrapper = new HTMLNodeWrapper(node, parent, index);
        wrapper.nodeKind = Type.PROCESSING_INSTRUCTION;
        break;
    default:
        short nodeType = node.getNodeType();
        throw new IllegalArgumentException(
                "Unsupported node type in DOM! " + node.getNodeType() + " instance " + node.toString());
    }
    wrapper.docWrapper = docWrapper;
    return wrapper;
}

From source file:client.net.sf.saxon.ce.dom.HTMLNodeWrapper.java

License:Mozilla Public License

private static void expandStringValue(NodeList list, StringBuffer sb) {
    final int len = list.getLength();
    for (int i = 0; i < len; i++) {
        Node child = list.getItem(i);
        switch (child.getNodeType()) {
        case Node.ELEMENT_NODE:
            expandStringValue(child.getChildNodes(), sb);
            break;
        case Type.COMMENT:
        case Type.PROCESSING_INSTRUCTION:
            break;
        default://from   w ww . j ava2 s. c  o m
            sb.append(getValue(child));
        }
    }
}

From source file:client.net.sf.saxon.ce.dom.HTMLWriter.java

License:Mozilla Public License

public void setNode(Node node) {
    if (node == null) {
        return;/*from www .  java2s.c o  m*/
    }
    currentNode = node;
    if (node.getNodeType() == Node.DOCUMENT_NODE) {
        document = (Document) node;
    } else {
        document = currentNode.getOwnerDocument();
    }
    if (mode == WriteMode.NONE) {
        Controller.APIcommand cmd = pipe.getController().getApiCommand();
        mode = (cmd == APIcommand.TRANSFORM_TO_DOCUMENT || cmd == APIcommand.TRANSFORM_TO_FRAGMENT)
                ? WriteMode.XML
                : WriteMode.HTML;
    }
}

From source file:com.ait.toolkit.editors.ckeditor.client.CKEditor.java

License:Open Source License

/**
 * Use to disable CKEditor's instance/*  ww  w.  j a va 2s. c  o m*/
 * 
 * @param disabled
 */
public void setEnabled(boolean enabled) {
    // FIXME : rework this part to remove the !
    boolean disabled = !enabled;

    if (this.disabled != disabled) {
        this.disabled = disabled;

        if (disabled) {
            ScrollPanel scroll = new ScrollPanel();
            disabledHTML = new HTML();
            disabledHTML.setStyleName("GWTCKEditor-Disabled");
            scroll.setWidget(disabledHTML);

            if (config.getWidth() != null)
                scroll.setWidth(config.getWidth());

            if (config.getHeight() != null)
                scroll.setHeight(config.getHeight());

            String htmlString = new String();

            if (replaced) {
                htmlString = getHTML();
            } else {
                htmlString = waitingText;
            }

            DivElement divElement = DivElement.as(this.getElement().getFirstChildElement());
            Node node = divElement.getFirstChild();
            while (node != null) {
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    com.google.gwt.dom.client.Element element = com.google.gwt.dom.client.Element.as(node);
                    if (element.getTagName().equalsIgnoreCase("textarea")) {
                        destroyInstance();
                        replaced = false;
                        divElement.removeChild(node);
                        ckEditorNode = node;
                    }
                }
                node = node.getNextSibling();
            }
            disabledHTML.setHTML(htmlString);
            div.appendChild(scroll.getElement());

        } else {
            if (ckEditorNode != null) {
                DivElement divElement = DivElement.as(this.getElement().getFirstChildElement());
                Node node = divElement.getFirstChild();
                while (node != null) {
                    if (node.getNodeType() == Node.ELEMENT_NODE) {
                        com.google.gwt.dom.client.Element element = com.google.gwt.dom.client.Element.as(node);
                        if (element.getTagName().equalsIgnoreCase("div")) {
                            divElement.removeChild(node);

                        }
                    }
                    node = node.getNextSibling();
                }
                div.appendChild(baseTextArea);
                initInstance();

            }
        }
    }

}

From source file:com.alkacon.geranium.client.util.DomUtil.java

License:Open Source License

/**
 * Positions an element inside the given parent, reordering the content of the parent and returns the new position index.<p>
 * This is none absolute positioning. Use for drag and drop reordering of drop targets.<p>
 * Use <code>-1</code> for x or y to ignore one ordering orientation.<p>
 * //from w w w. j  a v a2s. c  o m
 * @param element the child element
 * @param parent the parent element
 * @param currentIndex the current index position of the element, use -1 if element is not attached to the parent yet 
 * @param x the client x position, use <code>-1</code> to ignore x position 
 * @param y the client y position, use <code>-1</code> to ignore y position
 * 
 * @return the new index position
 */
public static int positionElementInside(Element element, Element parent, int currentIndex, int x, int y) {

    if ((x == -1) && (y == -1)) {
        // this is wrong usage, do nothing
        DebugLog.getInstance().printLine("this is wrong usage, doing nothing");
        return currentIndex;
    }
    int indexCorrection = 0;
    int previousTop = 0;
    for (int index = 0; index < parent.getChildCount(); index++) {
        Node node = parent.getChild(index);
        if (node.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        Element child = (Element) node;
        if (child == element) {
            indexCorrection = 1;
        }
        String positioning = DomUtil.getCurrentStyle(child, Style.position);
        if (Position.ABSOLUTE.getCssName().equals(positioning)
                || Position.FIXED.getCssName().equals(positioning)) {
            // only not 'position:absolute' elements into account, 
            // not visible children will be excluded in the next condition
            continue;
        }
        int left = 0;
        int width = 0;
        int top = 0;
        int height = 0;
        if (y != -1) {
            // check if the mouse pointer is within the height of the element 
            top = DomUtil.getRelativeY(y, child);
            height = child.getOffsetHeight();
            if ((top <= 0) || (top >= height)) {
                previousTop = top;
                continue;
            }
        }
        if (x != -1) {
            // check if the mouse pointer is within the width of the element 
            left = DomUtil.getRelativeX(x, child);
            width = child.getOffsetWidth();
            if ((left <= 0) || (left >= width)) {
                previousTop = top;
                continue;
            }
        }

        boolean floatSort = false;
        String floating = "";
        if ((top != 0) && (top == previousTop)) {
            floating = getCurrentStyle(child, Style.floatCss);
            if ("left".equals(floating) || "right".equals(floating)) {
                floatSort = true;
            }
        }
        previousTop = top;
        if (child == element) {
            return currentIndex;
        }
        if ((y == -1) || floatSort) {
            boolean insertBefore = false;
            if (left < (width / 2)) {
                if (!(floatSort && "right".equals(floating))) {
                    insertBefore = true;
                }
            } else if (floatSort && "right".equals(floating)) {
                insertBefore = true;
            }
            if (insertBefore) {
                parent.insertBefore(element, child);
                currentIndex = index - indexCorrection;
                return currentIndex;
            } else {
                parent.insertAfter(element, child);
                currentIndex = (index + 1) - indexCorrection;
                return currentIndex;
            }
        }
        if (top < (height / 2)) {
            parent.insertBefore(element, child);
            currentIndex = index - indexCorrection;
            return currentIndex;
        } else {
            parent.insertAfter(element, child);
            currentIndex = (index + 1) - indexCorrection;
            return currentIndex;
        }

    }
    // not over any child position
    if ((currentIndex >= 0) && (element.getParentElement() == parent)) {
        // element is already attached to this parent and no new position available
        // don't do anything
        return currentIndex;
    }
    int top = DomUtil.getRelativeY(y, parent);
    int offsetHeight = parent.getOffsetHeight();
    if ((top >= (offsetHeight / 2))) {
        // over top half, insert as first child
        parent.insertFirst(element);
        currentIndex = 0;
        return currentIndex;
    }
    // over bottom half, insert as last child
    parent.appendChild(element);
    currentIndex = parent.getChildCount() - 1;
    return currentIndex;
}