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

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

Introduction

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

Prototype

short TEXT_NODE

To view the source code for com.google.gwt.dom.client Node TEXT_NODE.

Click Source Link

Document

The node is a Text node.

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);
            }//from ww w . j  a v a  2s.c om
            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.GWTDOMStructure.java

License:Open Source License

@Override
public Value property(String name) {
    if (this.element.element.getNodeType() == Node.TEXT_NODE && name.equals("value")) {
        return new GWTDOMValue(this.element.element.getNodeValue());
    }// ww w.  j  a v a2 s.  com
    ArrayList<ElementAndSubtype> es = this.elementsByName.get(name);
    if (es == null || es.size() == 0) {
        // attribute with this name?
        if (((Element) this.element.element).hasAttribute(name)) {
            return new GWTDOMValue(((Element) this.element.element).getAttribute(name));
        }
        if (name.equals("childNodes")) // special case, when xml is used as list of childNodes and not as a map like in xhtml
        {
            return new GWTDOMValue(this.childNodes);
        }
        return null;
    }
    return new GWTDOMValue(es);
}

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());/*w w  w  .  ja v  a2 s .c om*/
    }
    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;
    }/*w w  w  .ja  v a 2s.  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: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.//from  w w  w .j a  v a2s  .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:com.alkacon.geranium.client.util.PositionBean.java

License:Open Source License

/**
 * Returns a position info representing the dimensions of all visible child elements of the given panel (excluding elements with position:absolute).
 * If the panel has no visible child elements, it's outer dimensions are returned.<p>
 * //from  w ww .j  a  v a  2s.c  o  m
 * @param panel the panel
 * @param levels the levels to traverse down the DOM tree
 * @param includeSelf <code>true</code> to include the outer dimensions of the given panel
 * 
 * @return the position info
 */
public static PositionBean getInnerDimensions(Element panel, int levels, boolean includeSelf) {

    boolean first = true;
    int top = 0;
    int left = 0;
    int bottom = 0;
    int right = 0;
    // if overflow is set to hidden, use the outer dimensions
    if (!Overflow.HIDDEN.getCssName().equals(DomUtil.getCurrentStyle(panel, Style.overflow))) {
        if (!includeSelf) {
            // check for any text content
            NodeList<Node> children = panel.getChildNodes();
            for (int i = 0; i < children.getLength(); i++) {
                if ((children.getItem(i).getNodeType() == Node.TEXT_NODE)
                        && (children.getItem(i).getNodeValue().trim().length() > 0)) {
                    includeSelf = true;
                    break;
                }
            }
        }
        if (includeSelf) {
            top = panel.getAbsoluteTop();
            left = panel.getAbsoluteLeft();
            bottom = top + panel.getOffsetHeight();
            right = left + panel.getOffsetWidth();
            first = false;
        }
        Element child = panel.getFirstChildElement();
        while (child != null) {
            String tagName = child.getTagName();
            if (tagName.equalsIgnoreCase("br") || tagName.equalsIgnoreCase("tr")
                    || tagName.equalsIgnoreCase("thead") || tagName.equalsIgnoreCase("tfoot")
                    || tagName.equalsIgnoreCase("script") || tagName.equalsIgnoreCase("style")) {
                // ignore tags with no relevant position info
                child = child.getNextSiblingElement();
                continue;
            }
            String positioning = DomUtil.getCurrentStyle(child, Style.position);
            if (!Display.NONE.getCssName().equals(DomUtil.getCurrentStyle(child, Style.display))
                    && !(positioning.equalsIgnoreCase(Position.ABSOLUTE.getCssName())
                            || positioning.equalsIgnoreCase(Position.FIXED.getCssName()))) {
                PositionBean childDimensions = levels > 0 ? getInnerDimensions(child, levels - 1, true)
                        : generatePositionInfo(panel);
                if (first) {
                    first = false;
                    top = childDimensions.getTop();
                    left = childDimensions.getLeft();
                    bottom = top + childDimensions.getHeight();
                    right = left + childDimensions.getWidth();
                } else {
                    int wTop = childDimensions.getTop();
                    top = top < wTop ? top : wTop;
                    int wLeft = childDimensions.getLeft();
                    left = left < wLeft ? left : wLeft;
                    int wBottom = wTop + childDimensions.getHeight();
                    bottom = bottom > wBottom ? bottom : wBottom;
                    int wRight = wLeft + childDimensions.getWidth();
                    right = right > wRight ? right : wRight;
                }
            }
            child = child.getNextSiblingElement();
        }
    }
    if (!first) {
        PositionBean result = new PositionBean();
        result.setHeight(bottom - top);
        result.setWidth(right - left);
        result.setTop(top);
        result.setLeft(left);
        return result;
    } else {
        return generatePositionInfo(panel);
    }
}

From source file:com.bfr.client.selection.impl.RangeImpl.java

License:Apache License

/**
 * If the found range is not on a text node, this finds the cooresponding
 * text node to where the selection is.  If it is on a text node, just
 * directly creates the endpoint from it.
 *
 * @param node   node returned as an endpoint of a range
 * @param offset offset returned to the endpoint of a range
 * @return A range end point with a proper (or null) text node
 *//*from  w w w  . j  a  va  2  s  . c o  m*/
private RangeEndPoint findTextPoint(Node node, int offset) {
    RangeEndPoint res;
    if (node.getNodeType() == Node.TEXT_NODE) {
        res = new RangeEndPoint((Text) node, offset);
    } else {
        // search backwards unless this is after the last node
        boolean dir = offset >= node.getChildCount();
        Node child = (node.getChildCount() == 0) ? node : node.getChild(dir ? offset - 1 : offset);
        // Get the previous/next text node
        Text text = Range.getAdjacentTextElement(child, dir);
        if (text == null) {
            // If we didn't find a text node in the preferred direction,
            // try the other direction
            dir = !dir;
            text = Range.getAdjacentTextElement(child, dir);
        }
        res = new RangeEndPoint(text, dir);
    }
    return res;
}

From source file:com.bfr.client.selection.Range.java

License:Apache License

/**
 * Returns the next adjacent text node in the given direction.  Will move
 * down the hierarchy (if traversingUp is not set), then through siblings,
 * then up (but not past topMostNode), looking for the first node
 * <p/>/* ww w . jav a 2s  .c om*/
 * This could be non-statically included in the Node class
 *
 * @param current      An element to start the search from, can be any type
 *                     of node.
 * @param topMostNode  A node that this will traverse no higher than
 * @param forward      whether to search forward or backward
 * @param traversingUp if true, will not look at the children of this element
 * @return the next (previous) text node, or null if no more
 */
public static Text getAdjacentTextElement(Node current, Node topMostNode, boolean forward,
        boolean traversingUp) {
    Text res = null;
    Node node;

    // If traversingUp, then the children have already been processed
    if (!traversingUp) {
        if (current.getChildCount() > 0) {
            node = forward ? current.getFirstChild() : current.getLastChild();

            if (node.getNodeType() == Node.TEXT_NODE) {
                res = (Text) node;
            } else {
                // Depth first traversal, the recursive call deals with
                // siblings
                res = getAdjacentTextElement(node, topMostNode, forward, false);
            }
        }
    }

    if (res == null) {
        node = forward ? current.getNextSibling() : current.getPreviousSibling();
        // Traverse siblings
        if (node != null) {
            if (node.getNodeType() == Node.TEXT_NODE) {
                res = (Text) node;
            } else {
                // Depth first traversal, the recursive call deals with
                // siblings
                res = getAdjacentTextElement(node, topMostNode, forward, false);
            }
        }
    }

    // Go up and over if still not found
    if ((res == null) && (current != topMostNode)) {
        node = current.getParentNode();
        // Stop at document (technically could stop at "html" tag)
        if ((node != null) && (node.getNodeType() != Node.DOCUMENT_NODE)) {
            res = getAdjacentTextElement(node, topMostNode, forward, true);
        }
    }

    return res;
}

From source file:com.bfr.client.selection.RangeEndPoint.java

License:Apache License

public boolean isTextNode() {
    return (m_node == null) ? false : (m_node.getNodeType() == Node.TEXT_NODE);
}

From source file:com.dom_distiller.client.DomToSaxVisitor.java

License:Open Source License

@Override
public boolean visit(Node n) {
    switch (n.getNodeType()) {
    case Node.TEXT_NODE:
        handler.textNode(Text.as(n));
        return false;
    case Node.ELEMENT_NODE:
        Element e = Element.as(n);
        handler.startElement(e);//from  w w w . java 2  s. c om
        return true;
    case Node.DOCUMENT_NODE: // Don't recurse into sub-documents.
    default: // This case is for comment nodes.
        return false;
    }
}