Example usage for org.w3c.dom Node CDATA_SECTION_NODE

List of usage examples for org.w3c.dom Node CDATA_SECTION_NODE

Introduction

In this page you can find the example usage for org.w3c.dom Node CDATA_SECTION_NODE.

Prototype

short CDATA_SECTION_NODE

To view the source code for org.w3c.dom Node CDATA_SECTION_NODE.

Click Source Link

Document

The node is a CDATASection.

Usage

From source file:Main.java

public final static Class<? extends Node> toClass(final short nodeType) {
    switch (nodeType) {
    case Node.ATTRIBUTE_NODE:
        return Attr.class;
    case Node.CDATA_SECTION_NODE:
        return CDATASection.class;
    case Node.COMMENT_NODE:
        return Comment.class;
    case Node.DOCUMENT_FRAGMENT_NODE:
        return DocumentFragment.class;
    case Node.DOCUMENT_NODE:
        return Document.class;
    case Node.DOCUMENT_TYPE_NODE:
        return DocumentType.class;
    case Node.ELEMENT_NODE:
        return Element.class;
    case Node.ENTITY_NODE:
        return Entity.class;
    case Node.ENTITY_REFERENCE_NODE:
        return EntityReference.class;
    case Node.NOTATION_NODE:
        return Notation.class;
    case Node.PROCESSING_INSTRUCTION_NODE:
        return ProcessingInstruction.class;
    case Node.TEXT_NODE:
        return Text.class;
    }/*from w ww . j a  va 2  s .  c om*/
    throw new RuntimeException("Unrecognized node type " + nodeType);
}

From source file:DOMCopy.java

private static void outputloop(Node node, String indent) {
        switch (node.getNodeType()) {
        case Node.ELEMENT_NODE:
            outputElement((Element) node, indent);
            break;
        case Node.TEXT_NODE:
            outputText((Text) node, indent);
            break;
        case Node.CDATA_SECTION_NODE:
            outputCDATASection((CDATASection) node, indent);
            break;
        case Node.COMMENT_NODE:
            outputComment((Comment) node, indent);
            break;
        case Node.PROCESSING_INSTRUCTION_NODE:
            outputProcessingInstructionNode((ProcessingInstruction) node, indent);
            break;
        default:/*w  ww. ja  va  2 s .  co  m*/
            System.out.println("Unknown node type: " + node.getNodeType());
            break;
        }
    }

From source file:Main.java

/**
 * Print a Node tree recursively.//from www  .  j  a v  a2 s . co  m
 * @param node A DOM tree Node
 * @return An xml String representation of the DOM tree.
 */
public static String print(Node node) {
    if (node == null) {
        return null;
    }

    StringBuffer xml = new StringBuffer(100);
    int type = node.getNodeType();

    switch (type) {
    // print element with attributes
    case Node.ELEMENT_NODE: {
        xml.append('<');
        xml.append(node.getNodeName());

        NamedNodeMap attrs = node.getAttributes();
        int length = attrs.getLength();
        ;

        for (int i = 0; i < length; i++) {
            Attr attr = (Attr) attrs.item(i);
            xml.append(' ');
            xml.append(attr.getNodeName());
            xml.append("=\"");

            //xml.append(normalize(attr.getNodeValue()));
            xml.append(attr.getNodeValue());
            xml.append('"');
        }

        xml.append('>');

        NodeList children = node.getChildNodes();

        if (children != null) {
            int len = children.getLength();

            for (int i = 0; i < len; i++) {
                xml.append(print(children.item(i)));
            }
        }

        break;
    }

    // handle entity reference nodes
    case Node.ENTITY_REFERENCE_NODE: {
        NodeList children = node.getChildNodes();

        if (children != null) {
            int len = children.getLength();

            for (int i = 0; i < len; i++) {
                xml.append(print(children.item(i)));
            }
        }

        break;
    }

    // print cdata sections
    case Node.CDATA_SECTION_NODE: {
        xml.append("<![CDATA[");
        xml.append(node.getNodeValue());
        xml.append("]]>");

        break;
    }

    // print text
    case Node.TEXT_NODE: {
        //xml.append(normalize(node.getNodeValue()));
        xml.append(node.getNodeValue());

        break;
    }

    // print processing instruction
    case Node.PROCESSING_INSTRUCTION_NODE: {
        xml.append("<?");
        xml.append(node.getNodeName());

        String data = node.getNodeValue();

        if ((data != null) && (data.length() > 0)) {
            xml.append(' ');
            xml.append(data);
        }

        xml.append("?>");

        break;
    }
    }

    if (type == Node.ELEMENT_NODE) {
        xml.append("</");
        xml.append(node.getNodeName());
        xml.append('>');
    }

    return xml.toString();
}

From source file:Main.java

public static void getNodeData(Node node, StringBuffer buf) {
    switch (node.getNodeType()) {
    case Node.DOCUMENT_FRAGMENT_NODE:
    case Node.DOCUMENT_NODE:
    case Node.ELEMENT_NODE: {
        for (Node child = node.getFirstChild(); null != child; child = child.getNextSibling()) {
            buf.append('<');
            buf.append(node.getNodeName());
            buf.append('>');
            getNodeData(child, buf);//w  w  w .j  a va2  s .  co m
            buf.append("</");
            buf.append(node.getNodeName());
            buf.append('>');
        }
    }
        break;
    case Node.TEXT_NODE:
    case Node.CDATA_SECTION_NODE:
        buf.append(node.getNodeValue());
        break;
    case Node.ATTRIBUTE_NODE:
        buf.append(node.getNodeValue());
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        // warning(XPATHErrorResources.WG_PARSING_AND_PREPARING);
        break;
    default:
        // ignore
        break;
    }
}

From source file:eu.annocultor.xconverter.impl.XmlElementForVelocity.java

public String getValue() {
    //      if (element.getFirstChild() != null && element.getFirstChild().getNodeType() == Node.CDATA_SECTION_NODE)
    //      {/*from ww  w.  jav a 2s.co m*/
    //         System.out.println("hi");
    //      }
    if (element.getFirstChild() != null && (element.getFirstChild().getNodeType() == Node.TEXT_NODE
            || element.getFirstChild().getNodeType() == Node.CDATA_SECTION_NODE)) {
        String value = element.getFirstChild().getNodeValue();
        if (value != null)
            value = value.trim();
        return value;
    }
    return null;
}

From source file:Main.java

/**
 * Performs the actual recursive dumping of a DOM tree to a given
 * <CODE>PrintStream</CODE>. Note that dump is intended to be a detailed
 * debugging aid rather than pretty to look at. 
 * //from   w w  w.j av  a2 s. com
 * @param    out            The <CODE>PrintStream</CODE> to write to.
 * @param    node         The <CODE>Node</CODE> under consideration.
 * @param    indent         The level of indentation.
 * @see      #dump(Node)
 * @see      #dump(PrintStream, Node)
 * @since   TFP 1.0
 */
private static void doDump(PrintStream out, final Node node, int indent) {
    if (node != null) {
        for (int index = 0; index < indent; ++index)
            out.write(' ');

        switch (node.getNodeType()) {
        case Node.DOCUMENT_NODE: {
            Document document = (Document) node;

            out.println("DOCUMENT:");

            doDump(out, document.getDoctype(), indent + 1);
            doDump(out, document.getDocumentElement(), indent + 1);
            break;
        }

        case Node.DOCUMENT_TYPE_NODE: {
            DocumentType type = (DocumentType) node;

            out.println("DOCTYPE: [" + "name=" + format(type.getName()) + "," + "publicId="
                    + format(type.getPublicId()) + "," + "systemId=" + format(type.getSystemId()) + "]");
            break;
        }

        case Node.ELEMENT_NODE: {
            Element element = (Element) node;

            out.println("ELEMENT: [" + "ns=" + format(element.getNamespaceURI()) + "," + "name="
                    + format(element.getLocalName()) + "]");

            NamedNodeMap attrs = element.getAttributes();

            for (int index = 0; index < attrs.getLength(); ++index)
                doDump(out, attrs.item(index), indent + 1);

            for (Node child = element.getFirstChild(); child != null;) {
                doDump(out, child, indent + 1);
                child = child.getNextSibling();
            }
            break;
        }
        case Node.ATTRIBUTE_NODE: {
            Attr attr = (Attr) node;

            out.println("ATTRIBUTE: [" + "ns=" + format(attr.getNamespaceURI()) + "," + "prefix="
                    + format(attr.getPrefix()) + "," + "name=" + format(attr.getLocalName()) + "," + "value="
                    + format(attr.getNodeValue()) + "]");
            break;
        }

        case Node.TEXT_NODE: {
            Text text = (Text) node;

            out.println("TEXT: [" + format(text.getNodeValue()) + "]");

            for (Node child = text.getFirstChild(); child != null;) {
                doDump(out, child, indent + 1);
                child = child.getNextSibling();
            }
            break;
        }

        case Node.CDATA_SECTION_NODE: {
            CDATASection data = (CDATASection) node;

            out.println("CDATA: [" + format(data.getNodeValue()) + "]");
            break;
        }

        case Node.COMMENT_NODE: {
            Comment comm = (Comment) node;

            out.println("COMMENT: [" + format(comm.getNodeValue()) + "]");
            break;
        }

        default:
            out.println("UNKNOWN: [type=" + node.getNodeType() + "]");
            break;
        }
    }
}

From source file:DOMUtils.java

/**
 * Concat all the text and cdata node children of this elem and return
 * the resulting text.//from   w  w  w.  j a  v  a  2  s.c o m
 *
 * @param parentEl the element whose cdata/text node values are to
 *                 be combined.
 * @return the concatanated string.
 */
static public String getChildCharacterData(Element parentEl) {
    if (parentEl == null) {
        return null;
    }
    Node tempNode = parentEl.getFirstChild();
    StringBuffer strBuf = new StringBuffer();
    CharacterData charData;

    while (tempNode != null) {
        switch (tempNode.getNodeType()) {
        case Node.TEXT_NODE:
        case Node.CDATA_SECTION_NODE:
            charData = (CharacterData) tempNode;
            strBuf.append(charData.getData());
            break;
        }
        tempNode = tempNode.getNextSibling();
    }
    return strBuf.toString();
}

From source file:ApplyXPathDOM.java

/** Decide if the node is text, and so must be handled specially */
static boolean isTextNode(Node n) {
    if (n == null)
        return false;
    short nodeType = n.getNodeType();
    return nodeType == Node.CDATA_SECTION_NODE || nodeType == Node.TEXT_NODE;
}

From source file:DomPrintUtil.java

private boolean checkNewLine(Node target) {
    if (indent && target.hasChildNodes()) {
        short type = target.getFirstChild().getNodeType();
        if (type == Node.TEXT_NODE || type == Node.CDATA_SECTION_NODE) {
            return false;
        }//from   w ww .j  ava2 s.  c o m
        return true;
    }
    return false;
}

From source file:com.adaptris.core.XStreamMarshallerTest.java

public void testXStreamCdata() throws Exception {
    XStreamMarshaller xm = createMarshaller();
    XStreamCDataWrapper wrapper = new XStreamCDataWrapper();
    //    String id = wrapper.getRawValue();
    String xml = xm.marshal(wrapper);
    Document xmlDoc = XmlHelper.createDocument(xml);
    XPath xpath = new XPath();
    Node rawNode = xpath.selectSingleNode(xmlDoc, "/xstream-cdata-wrapper/raw-value");
    Node parentRawNode = xpath.selectSingleNode(xmlDoc, "/xstream-cdata-wrapper/parent-raw-value");

    // How else to do this...
    // We know that what we have is an element, and the first child should be the raw CDATA...
    ///*  ww w.  j  av a  2 s.c o  m*/
    assertEquals(Node.CDATA_SECTION_NODE, ((Element) rawNode).getFirstChild().getNodeType());
    assertEquals(Node.CDATA_SECTION_NODE, ((Element) parentRawNode).getFirstChild().getNodeType());
    XStreamCDataWrapper roundTrip = (XStreamCDataWrapper) xm.unmarshal(xml);
    assertRoundtripEquality(wrapper, roundTrip);
}