Example usage for org.w3c.dom Text getWholeText

List of usage examples for org.w3c.dom Text getWholeText

Introduction

In this page you can find the example usage for org.w3c.dom Text getWholeText.

Prototype

public String getWholeText();

Source Link

Document

Returns all text of Text nodes logically-adjacent text nodes to this node, concatenated in document order.

Usage

From source file:Main.java

@SuppressWarnings({ "ChainOfInstanceofChecks" })
private static void formatElementStart(/*@Nonnull*/Document document, /*@Nonnull*/Element element, /*@Nonnull*/
        String formatString) {//from   w  ww.  j av a2s  .  c  o  m
    Node previousSibling = element.getPreviousSibling();

    if (previousSibling == null || previousSibling instanceof Element) {
        element.getParentNode().insertBefore(document.createTextNode(formatString), element);
    } else if (previousSibling instanceof Text) {
        Text textNode = (Text) previousSibling;
        String text = textNode.getWholeText();

        if (!formatString.equals(text)) {
            textNode.replaceWholeText(trimRight(text) + formatString);
        }
    }
}

From source file:Main.java

@SuppressWarnings({ "ChainOfInstanceofChecks" })
private static void formatElementEnd(/*@Nonnull*/Document document, /*@Nonnull*/Element element, /*@Nonnull*/
        String formatString) {//from   w w  w  .ja  v  a  2 s  .com
    Node lastChild = element.getLastChild();

    if (lastChild != null) {
        if (lastChild instanceof Element) {
            element.appendChild(document.createTextNode(formatString));
        } else if (lastChild instanceof Text) {
            Text textNode = (Text) lastChild;
            String text = textNode.getWholeText();

            if (hasOnlyTextSiblings(textNode)) {
                String trimmedText = text.trim();
                if (!trimmedText.equals(text)) {
                    textNode.replaceWholeText(trimmedText);
                }
            } else {
                if (!formatString.equals(text)) {
                    textNode.replaceWholeText(trimRight(text) + formatString);
                }
            }
        }
    }
}

From source file:org.apache.hadoop.gateway.filter.rewrite.impl.xml.XmlFilterReader.java

private void processBufferedElement(Level level, UrlRewriteFilterGroupDescriptor config)
        throws XPathExpressionException {
    for (UrlRewriteFilterPathDescriptor selector : config.getSelectors()) {
        if (selector instanceof UrlRewriteFilterApplyDescriptor) {
            XPathExpression path = (XPathExpression) selector.compiledPath(XPATH_COMPILER);
            Object node = path.evaluate(level.scopeNode, XPathConstants.NODE);
            if (node != null) {
                UrlRewriteFilterApplyDescriptor apply = (UrlRewriteFilterApplyDescriptor) selector;
                if (node instanceof Element) {
                    Element element = (Element) node;
                    String value = element.getTextContent();
                    value = filterText(extractQName(element), value, apply.rule());
                    element.setTextContent(value);
                } else if (node instanceof Text) {
                    Text text = (Text) node;
                    String value = text.getWholeText();
                    value = filterText(extractQName(text.getParentNode()), value, apply.rule());
                    text.replaceWholeText(value);
                } else if (node instanceof Attr) {
                    Attr attr = (Attr) node;
                    String value = attr.getValue();
                    value = filterAttribute(extractQName(attr.getOwnerElement()), extractQName(attr), value,
                            apply.rule());
                    attr.setValue(value);
                } else {
                    throw new IllegalArgumentException(RES.unexpectedSelectedNodeType(node));
                }//from   w ww . j  a  v a 2s  .c o m
            }
        } else if (selector instanceof UrlRewriteFilterDetectDescriptor) {
            XPathExpression path = (XPathExpression) selector.compiledPath(XPATH_COMPILER);
            Object node = path.evaluate(level.scopeNode, XPathConstants.NODE);
            if (node != null) {
                UrlRewriteFilterDetectDescriptor detect = (UrlRewriteFilterDetectDescriptor) selector;
                String value = null;
                if (node instanceof Element) {
                    Element element = (Element) node;
                    value = element.getTextContent();
                } else if (node instanceof Text) {
                    Text text = (Text) node;
                    value = text.getWholeText();
                } else if (node instanceof Attr) {
                    Attr attr = (Attr) node;
                    value = attr.getValue();
                } else {
                    throw new IllegalArgumentException(RES.unexpectedSelectedNodeType(node));
                }
                if (detect.compiledValue(REGEX_COMPILER).matcher(value).matches()) {
                    processBufferedElement(level, detect);
                }
            }
        } else {
            throw new IllegalArgumentException(RES.unexpectedRewritePathSelector(selector));
        }
    }
}

From source file:org.apache.ode.bpel.engine.BpelProcess.java

public String getProcessProperty(QName property, String defaultValue) {
    Text text = (Text) getProcessProperty(property);
    if (text == null) {
        return defaultValue;
    }/* w ww.  j ava2s.  com*/
    String value = text.getWholeText();
    return (value == null) ? defaultValue : value;
}

From source file:org.cruxframework.crux.core.declarativeui.crossdevice.CrossDevices.java

/**
 * @param parentElement/*from  w  w  w .  java  2  s . c om*/
 * @return
 */
static List<Element> extractChildrenElements(Element parentElement) {
    List<Element> result = new ArrayList<Element>();
    NodeList childNodes = parentElement.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        switch (node.getNodeType()) {
        case Node.COMMENT_NODE:
            //ignore node
            break;
        case Node.TEXT_NODE:
            Text textNode = (Text) node;
            if (textNode.getWholeText().trim().length() > 0) {
                return null;
            }
            break;
        case Node.ELEMENT_NODE:
            result.add((Element) node);
            break;
        default:
            return null;
        }
    }

    return result;
}