Example usage for org.w3c.dom Text replaceWholeText

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

Introduction

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

Prototype

public Text replaceWholeText(String content) throws DOMException;

Source Link

Document

Replaces the text of the current node and all logically-adjacent text nodes with the specified text.

Usage

From source file:Main.java

@SuppressWarnings({ "ChainOfInstanceofChecks" })
private static void formatElementStart(/*@Nonnull*/Document document, /*@Nonnull*/Element element, /*@Nonnull*/
        String formatString) {//from ww  w.j  a v a2  s  . com
    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  ww  .  j  a v  a  2  s . c  o  m
    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 w  w.  j  a  v a2s  .  com
            }
        } 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));
        }
    }
}