Example usage for org.w3c.dom Node ENTITY_REFERENCE_NODE

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

Introduction

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

Prototype

short ENTITY_REFERENCE_NODE

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

Click Source Link

Document

The node is an EntityReference.

Usage

From source file:org.dhatim.delivery.dom.serialize.Serializer.java

/**
 * Recursively write the DOM tree to the supplied writer.
 * @param element Element to write.//  www .j a va2  s.co m
 * @param writer Writer to use.
 * @throws IOException Exception writing to Writer.
 */
public static void recursiveDOMWrite(Element element, Writer writer) throws IOException {
    NodeList children = element.getChildNodes();

    try {
        defaultSerializer.writeElementStart(element, writer);

        if (children != null && children.getLength() > 0) {
            int childCount = children.getLength();

            for (int i = 0; i < childCount; i++) {
                Node childNode = children.item(i);

                switch (childNode.getNodeType()) {
                case Node.CDATA_SECTION_NODE: {
                    defaultSerializer.writeElementCDATA((CDATASection) childNode, writer, null);
                    break;
                }
                case Node.COMMENT_NODE: {
                    defaultSerializer.writeElementComment((Comment) childNode, writer, null);
                    break;
                }
                case Node.ELEMENT_NODE: {
                    recursiveDOMWrite((Element) childNode, writer);
                    break;
                }
                case Node.ENTITY_REFERENCE_NODE: {
                    defaultSerializer.writeElementEntityRef((EntityReference) childNode, writer, null);
                    break;
                }
                case Node.TEXT_NODE: {
                    defaultSerializer.writeElementText((Text) childNode, writer, null);
                    break;
                }
                default: {
                    defaultSerializer.writeElementNode(childNode, writer, null);
                    break;
                }
                }
            }
        }
        defaultSerializer.writeElementEnd(element, writer);
    } catch (Throwable thrown) {
        if (thrown instanceof SmooksException) {
            throw (SmooksException) thrown;
        } else {
            throw new SmooksException("Serailization Error.", thrown);
        }
    }
}

From source file:org.erdc.cobie.shared.spreadsheetml.transformation.cobietab.COBieSpreadSheet.java

License:asdf

public static void nodeToStream(Node node, PrintWriter out) {
    String workSheetName = "";
    boolean canonical = false;
    // is there anything to do?
    if (node == null) {
        return;/*from w w w .ja v  a  2s .co  m*/
    }

    int type = node.getNodeType();
    switch (type) {
    // print document
    case Node.DOCUMENT_NODE: {
        if (!canonical) {
            out.println("<?xml version=\"1.0\"?>");
        }
        // print(((Document)node).getDocumentElement());

        NodeList children = node.getChildNodes();
        for (int iChild = 0; iChild < children.getLength(); iChild++) {
            nodeToStream(children.item(iChild), out);
        }
        out.flush();
        break;
    }

    // print element with attributes
    case Node.ELEMENT_NODE: {
        out.print('<');
        out.print(node.getNodeName());
        Attr attrs[] = sortAttributes(node.getAttributes());
        for (int i = 0; i < attrs.length; i++) {
            Attr attr = attrs[i];
            if (((node.getNodeName().equalsIgnoreCase("Worksheet")
                    || node.getNodeName().equalsIgnoreCase("ss:Worksheet"))
                    && attr.getName().equalsIgnoreCase("Name")) || attr.getName().equalsIgnoreCase("ss:Name")) {
                workSheetName = normalize(attr.getNodeValue());
            }
            out.print(' ');
            out.print(attr.getNodeName());
            out.print("=\"");
            out.print(normalize(attr.getNodeValue()));
            out.print('"');
        }
        out.print('>');
        out.flush();
        NodeList children = node.getChildNodes();
        if (children != null) {
            int len = children.getLength();
            for (int i = 0; i < len; i++) {
                nodeToStream(children.item(i), out);
            }
        }
        break;
    }

    // handle entity reference nodes
    case Node.ENTITY_REFERENCE_NODE: {
        if (canonical) {
            NodeList children = node.getChildNodes();
            if (children != null) {
                int len = children.getLength();
                for (int i = 0; i < len; i++) {
                    nodeToStream(children.item(i), out);
                }
            }
        } else {
            out.print('&');
            out.print(node.getNodeName());
            out.print(';');
        }
        break;
    }

    // print cdata sections
    case Node.CDATA_SECTION_NODE: {
        if (canonical) {
            out.print(normalize(node.getNodeValue()));
        } else {
            out.print("<![CDATA[");
            out.print(node.getNodeValue());
            out.print("]]>");
        }
        break;
    }

    // print text
    case Node.TEXT_NODE: {
        out.print(normalize(node.getNodeValue()));
        break;
    }

    // print processing instruction
    case Node.PROCESSING_INSTRUCTION_NODE: {
        out.print("<?");
        out.print(node.getNodeName());
        String data = node.getNodeValue();
        if ((data != null) && (data.length() > 0)) {
            out.print(' ');
            out.print(data);
        }
        out.print("?>");
        break;
    }

    // print comment
    case Node.COMMENT_NODE: {
        out.print("<!--");
        String data = node.getNodeValue();
        if (data != null) {
            out.print(data);
        }
        out.print("-->");
        break;
    }
    }

    if (type == Node.ELEMENT_NODE) {
        if ((node.getNodeName().equalsIgnoreCase("Worksheet")
                || node.getNodeName().equalsIgnoreCase("ss:Worksheet")) && (workSheetName.length() > 0)) {
            out.print(printCOBieSheetDataValidation(workSheetName));
        }
        out.print("</");
        out.print(node.getNodeName());
        out.print('>');
    }

    out.flush();

}

From source file:org.kalypsodeegree.xml.XMLTools.java

/**
 * Appends a node and it's children to the given StringBuffer. Indentation is added on recursion.
 *///w w w . j ava  2s  . c  o m
public static void appendNode(final Node node, final String indent, final StringBuffer sb) {
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE: {
        sb.append("<?xml version=\"1.0\"?>\n");

        final Document doc = (Document) node;
        appendNode(doc.getDocumentElement(), "", sb);

        break;
    }

    case Node.ELEMENT_NODE: {
        final String name = node.getNodeName();
        sb.append(indent + "<" + name);

        final NamedNodeMap attributes = node.getAttributes();

        for (int i = 0; i < attributes.getLength(); i++) {
            final Node current = attributes.item(i);
            sb.append(" " + current.getNodeName() + "=\"" + current.getNodeValue() + "\"");
        }

        sb.append(">");

        // Kinder durchgehen
        final NodeList children = node.getChildNodes();

        if (children != null) {
            for (int i = 0; i < children.getLength(); i++) {
                appendNode(children.item(i), indent, sb);
            }
        }

        sb.append(indent + "</" + name + ">");

        break;
    }

    case Node.TEXT_NODE:
    case Node.CDATA_SECTION_NODE: {
        final String trimmed = node.getNodeValue().trim();

        if (!trimmed.equals("")) {
            sb.append(indent + trimmed);
        }

        break;
    }

    case Node.PROCESSING_INSTRUCTION_NODE:
        break;

    case Node.ENTITY_REFERENCE_NODE:
        break;

    case Node.DOCUMENT_TYPE_NODE:
        break;
    }
}

From source file:org.sakaiproject.tool.assessment.qti.util.XmlUtil.java

/**
 * Get a textual representation of a Node.
 * @param node The Node/*from   ww w.j a  v a  2  s. c  o  m*/
 * @return the document in a text string
 */
public static String getDOMString(Node node) {
    //String domString = "";

    StringBuilder domStringbuf = new StringBuilder();

    int type = node.getNodeType();
    switch (type) {
    // print the document element
    case Node.DOCUMENT_NODE: {
        domStringbuf.append("<?xml version=\"1.0\" ?>\n");
        domStringbuf.append(getDOMString(((Document) node).getDocumentElement()));
        break;
    }

    // print element with attributes
    case Node.ELEMENT_NODE: {
        domStringbuf.append("<");
        domStringbuf.append(node.getNodeName());
        NamedNodeMap attrs = node.getAttributes();

        for (int i = 0; i < attrs.getLength(); i++) {
            Node attr = attrs.item(i);
            //domString += (" " + attr.getNodeName().trim() +
            //              "=\"" + attr.getNodeValue().trim() +
            //              "\"");

            domStringbuf.append((" " + attr.getNodeName().trim() + "=\"" + attr.getNodeValue().trim() + "\""));
        }
        //domString = domStringbuf.toString();
        domStringbuf.append(">");

        NodeList children = node.getChildNodes();
        if (children != null) {
            int len = children.getLength();
            for (int i = 0; i < len; i++)
                domStringbuf.append(getDOMString(children.item(i)));
        }
        domStringbuf.append("</");
        domStringbuf.append(node.getNodeName());
        domStringbuf.append(">\n");

        break;
    }

    // handle entity reference nodes
    case Node.ENTITY_REFERENCE_NODE: {
        domStringbuf.append("&");
        domStringbuf.append(node.getNodeName().trim());
        domStringbuf.append(";");

        break;
    }

    // print cdata sections
    case Node.CDATA_SECTION_NODE: {
        domStringbuf.append("");
        break;
    }

    // print text
    case Node.TEXT_NODE: {
        String val = node.getNodeValue();
        if (val == null)
            val = "";
        domStringbuf.append(val);//rshastri .trim() removed SAK-1671 
        break;
    }

    // print processing instruction
    case Node.PROCESSING_INSTRUCTION_NODE: {
        domStringbuf.append("");
        break;
    }
    }

    if (type == Node.ELEMENT_NODE) {
        domStringbuf.append("\n");
    }

    String domString = domStringbuf.toString();
    return domString;
}

From source file:org.yawlfoundation.yawl.util.DOMUtil.java

/**
 * Extracts to text from the supplied node and it's children.
 *
 * @param node to extract text from.//from w w  w . j  ava2s .c o  m
 * @return String representation of the node text.
 */
public static String getNodeText(Node node) {

    if (node == null || !node.hasChildNodes())
        return "";
    StringBuilder result = new StringBuilder();

    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node subnode = list.item(i);
        if (subnode.getNodeType() == Node.TEXT_NODE) {
            result.append(subnode.getNodeValue());
        } else if (subnode.getNodeType() == Node.CDATA_SECTION_NODE) {
            result.append(subnode.getNodeValue());
        } else if (subnode.getNodeType() == Node.ENTITY_REFERENCE_NODE) {
            // Recurse into the subtree for text
            // (and ignore comments)
            result.append(getNodeText(subnode));
        }
    }
    return result.toString();
}