Example usage for org.w3c.dom Text getParentNode

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

Introduction

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

Prototype

public Node getParentNode();

Source Link

Document

The parent of this node.

Usage

From source file:DOMUtil.java

/**
 * Automatically set text in a Node. Basically we find the first
 * Text node beneath the current node and replace it with a
 * CDATASection for the incoming text. All other Text nodes are
 * removed. Throws a DOMException if it's illegal to add a Text
 * child to the particular node./*  www  .  j a va2 s  .  c om*/
 *
 * @param node the starting node for the search.
 * @param text the text to be set
 * @param allowMarkupInText whether to allow markup in text to pass through unparsed
 * @return the updated node
 * @throws DOMException if the Text object is not found
 */
public static Node setTextInNode(Node node, String text, boolean allowMarkupInText) {
    //start by setting the value in the first text node we find with a comment
    Comment comment = node.getOwnerDocument().createComment("");
    Node newNode = null;

    //csc_092701.1 - support both encoded/unencoded text
    if (allowMarkupInText)
        newNode = node.getOwnerDocument().createCDATASection(text);
    else
        newNode = node.getOwnerDocument().createTextNode(text);
    //System.out.println ("newNode: "+newNode);

    Text textComp = DOMUtil.findFirstText((Element) node);
    //System.out.println ("textComp:"+textComp);        
    if (textComp == null) {
        node.appendChild(comment);
    } else {
        Node parent = textComp.getParentNode();
        parent.replaceChild(comment, textComp);
    }

    //now remove all the rest of the text nodes
    removeAllTextNodes(node);

    //now replace the comment with the newNode
    Node parent = comment.getParentNode();
    parent.replaceChild(newNode, comment);
    //System.out.println ("parent:  "+parent);        
    //System.out.println ("result:  "+DOMUtil.findFirstText((Element) parent));        
    //DOMUtil.printStackTrace(parent.getOwnerDocument().getDocumentElement());
    return node;
}

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));
                }//  www  .j  a va 2  s.c  om
            }
        } 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.openestate.io.core.XmlUtils.java

/**
 * Replace all text values of a {@link Node} with CDATA values.
 *
 * @param doc/*from  w  ww .  j av a2  s . c o  m*/
 * the document to update
 *
 * @param node
 * the node to update
 */
public static void replaceTextWithCData(Document doc, Node node) {
    if (node instanceof Text) {
        Text text = (Text) node;
        CDATASection cdata = doc.createCDATASection(text.getTextContent());
        Element parent = (Element) text.getParentNode();
        parent.replaceChild(cdata, text);
    } else if (node instanceof Element) {
        //LOGGER.debug( "ELEMENT " + element.getTagName() );
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            //LOGGER.debug( "> " + children.item( i ).getClass().getName() );
            XmlUtils.replaceTextWithCData(doc, children.item(i));
        }
    }
}