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

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

Introduction

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

Prototype

@Override
    public String getNodeValue() 

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);
            }/*from  ww  w  .ja  v  a 2  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());
    }//  w ww .j av a 2 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;
    }//from  w  w w . j  av  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:com.dom_distiller.client.OpenGraphProtocolParser.java

License:Open Source License

private void findPrefixes(Element root) {
    String prefixes = "";

    // See if HTML tag has "prefix" attribute.
    if (root.hasTagName("HTML"))
        prefixes = root.getAttribute("prefix");

    // Otherwise, see if HEAD tag has "prefix" attribute.
    if (prefixes.isEmpty()) {
        NodeList<Element> heads = root.getElementsByTagName("HEAD");
        if (heads.getLength() == 1)
            prefixes = heads.getItem(0).getAttribute("prefix");
    }/*  w  w w. ja  va2s.c o  m*/

    // If there's "prefix" attribute, its value is something like
    // "og: http://ogp.me/ns# profile: http://og.me/ns/profile# article: http://ogp.me/ns/article#".
    if (!prefixes.isEmpty()) {
        Matcher matcher = sOgpNsPrefixPattern.matcher(prefixes);
        while (matcher.find()) { // There could be multiple prefixes.
            setPrefixForObjectType(matcher.group(2), matcher.group(4));
        }
    } else {
        // Still no "prefix" attribute, see if HTMl tag has "xmlns" attributes e.g.:
        // - "xmlns:og="http://ogp.me/ns#"
        // - "xmlns:profile="http://ogp.me/ns/profile#"
        // - "xmlns:article="http://ogp.me/ns/article#".
        final JsArray<Node> attributes = DomUtil.getAttributes(root);
        for (int i = 0; i < attributes.length(); i++) {
            final Node node = attributes.get(i);
            // Look for attribute name that starts with "xmlns:".
            String attributeName = node.getNodeName().toLowerCase();
            Matcher nameMatcher = sOgpNsNonPrefixNamePattern.matcher(attributeName);
            if (!nameMatcher.find())
                continue;

            // Extract OGP namespace URI from attribute value, if available.
            String attributeValue = node.getNodeValue();
            Matcher valueMatcher = sOgpNsNonPrefixValuePattern.matcher(attributeValue);
            if (valueMatcher.find()) {
                setPrefixForObjectType(nameMatcher.group(1), valueMatcher.group(1));
            }
        }
    }

    setDefaultPrefixes();
}

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

License:Open Source License

@Override
public void startElement(Element element) {
    documentStringBuilder.append("<");
    documentStringBuilder.append(element.getTagName());
    JsArray<Node> attributes = DomUtil.getAttributes(element);
    for (int i = 0; i < attributes.length(); i++) {
        Node node = attributes.get(i);
        documentStringBuilder.append(" ");
        documentStringBuilder.append(node.getNodeName());
        documentStringBuilder.append("=\"");
        documentStringBuilder.append(node.getNodeValue());
        documentStringBuilder.append("\"");
    }// w ww. j  av a2 s.  c o m
    documentStringBuilder.append(">");
}

From source file:com.google.livingstories.client.lsp.views.contentitems.QuoteContentItemView.java

License:Apache License

public QuoteContentItemView(QuoteContentItem contentItem) {
    initWidget(uiBinder.createAndBindUi(this));

    // if the rendered HTML content leads with a double-quote character, we replace the underlying
    // text with 2 non-breaking spaces.
    HTML detachedHTML = new HTML(contentItem.getContent());

    // walk through the content looking for the first text node:
    Node firstTextNode = getFirstTextNode(detachedHTML.getElement().getFirstChild());

    if (firstTextNode != null) {
        // replace check the first text node to see if there's quotiness to replace.
        String firstText = firstTextNode.getNodeValue();
        // replace leading whitespace plus a quote character with two &nbsp;s.
        // \u201C is the opening-double-quote codepoint; \u00A0 is a non-breaking space.
        String replacedText = firstText.replaceFirst("^\\s*[\"\u201C]", "\u00A0\u00A0");
        if (!replacedText.equals(firstText)) {
            firstTextNode.setNodeValue(replacedText);
        }/*  ww  w.jav  a2 s.  co m*/
    }
    content.add(new ContentRenderer(detachedHTML.getElement().getInnerHTML(), false));
}

From source file:com.google.livingstories.client.lsp.views.contentitems.QuoteContentItemView.java

License:Apache License

public Node getFirstTextNode(Node node) {
    if (node == null) {
        return null;
    }/*  www.java 2  s.co  m*/

    if (node.getNodeType() == Node.TEXT_NODE && !node.getNodeValue().trim().isEmpty()) {
        return node;
    }
    Node childResult = getFirstTextNode(node.getFirstChild());
    return (childResult == null) ? getFirstTextNode(node.getNextSibling()) : childResult;
}

From source file:cz.cas.lib.proarc.webapp.client.ClientUtils.java

License:Open Source License

/**
 * Dumps Element content and traverse its children.
 * <p/><b>WARNING:</b> it is com.google.gwt.dom.client.Element not com.google.gwt.xml.client.Element!!!
 * /*from ww  w  .  j a v a 2s .c  om*/
 * @param elm an element to dump
 * @param indent row indentation for current level
 * @param indentIncrement increment for next level
 * @param sb dumped content
 * @return dumped content
 */
public static StringBuilder dump(Element elm, String indent, String indentIncrement, StringBuilder sb) {
    int childCount = elm.getChildCount();
    String innerText = elm.getInnerText();
    String lang = elm.getLang();
    String nodeName = elm.getNodeName();
    short nodeType = elm.getNodeType();
    String getString = elm.getString();
    String tagNameWithNS = elm.getTagName();
    String xmlLang = elm.getAttribute("xml:lang");

    sb.append(ClientUtils.format(
            "%sElement {nodeName: %s, nodeType: %s, tagNameWithNS: %s, lang: %s,"
                    + " childCount: %s, getString: %s, xmlLang: %s}\n",
            indent, nodeName, nodeType, tagNameWithNS, lang, childCount, getString, xmlLang));
    NodeList<Node> childNodes = elm.getChildNodes();
    indent += indentIncrement;
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.getItem(i);
        if (Element.is(child)) {
            dump(Element.as(child), indent, indentIncrement, sb);
        } else {
            sb.append(ClientUtils.format("%sNode: nodeType: %s, nodeName: %s, childCount: %s, nodeValue: %s\n",
                    indent, child.getNodeType(), child.getNodeName(), child.getChildCount(),
                    child.getNodeValue()));
        }
    }
    return sb;
}

From source file:de.catma.ui.client.ui.tagger.AffectedNodesFinder.java

License:Open Source License

/**
 * Recursively walks the tree depth first and creates a list of affected nodes.
 * @param curNode the (relative) root node to start with 
 *///from  w w w.j  a  v a  2s  . co  m
private void walk(Node curNode) {

    // check if we're still within the affected subtree 
    // and the current node has any taggable content
    if (!this.outOfAffectedSubtree
            && ((curNode.getNodeValue() == null) || !curNode.getNodeValue().trim().isEmpty())) {

        // all text nodes gets added, in case we get into the affected subtree with this 
        // node or one of its children 
        if (curNode.getNodeType() == Node.TEXT_NODE) {
            affectedNodes.push(curNode);
        }

        // we check for children and go down the subtrees
        if (curNode.hasChildNodes()) {
            for (int i = 0; i < curNode.getChildCount(); i++) {
                walk(curNode.getChild(i));
            }
        }
        // if we reach the outer left node
        // we're in the affacted subtree -> all parent nodes can stay on the stack
        else if (curNode.equals(outerLeftNode)) {
            this.inAffectedSubtree = true;
        }
        // if we reach the outer right node
        // we reject all the rest of the upcoming nodes
        else if (curNode.equals(outerRightNode)) {
            this.outOfAffectedSubtree = true;
        }
        // if the current node is a text node it has already been pushed onto the stack
        // and if we're not within the affected subtree, we're removing the current node from the stack
        // (not being in the affected subtree means neither the current node nor one of its 
        //  children is the outer left node)
        if (!inAffectedSubtree && (curNode.getNodeType() == Node.TEXT_NODE)) {
            affectedNodes.pop();
        }
    }
}

From source file:de.catma.ui.client.ui.tagger.DebugUtil.java

License:Open Source License

public static void printNode(Node node) {
    if (node == null) {
        println("null");
    } else {/*from   ww w.  j a va 2s .c om*/
        if (Element.is(node)) {
            Element e = Element.as(node);
            println("Element " + node.getNodeName() + "#" + e.getId());
        } else {
            println(node.getNodeName() + "[" + node.getNodeValue() + "]");
        }
    }
}