Java XML Node Format renderNode(StringBuffer sb, Node node, String margin, String indent, String lab, String rab, String nl)

Here you can find the source of renderNode(StringBuffer sb, Node node, String margin, String indent, String lab, String rab, String nl)

Description

render Node

License

Open Source License

Declaration

private static void renderNode(StringBuffer sb, Node node, String margin, String indent, String lab, String rab,
            String nl) 

Method Source Code

//package com.java2s;
/*---------------------------------------------------------------
*  Copyright 2011 by the Radiological Society of North America
*
*  This source software is released under the terms of the
*  RSNA Public License (http://mirc.rsna.org/rsnapubliclicense)
*----------------------------------------------------------------*/

import org.w3c.dom.Document;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    private static void renderNode(StringBuffer sb, Node node, String margin, String indent, String lab, String rab,
            String nl) {/*from  w w  w. ja  v  a  2 s  . c  o  m*/
        if (node == null) {
            sb.append("null");
            return;
        }
        switch (node.getNodeType()) {

        case Node.DOCUMENT_NODE:
            //sb.append(margin + lab +"?xml version=\"1.0\" encoding=\"UTF-8\"?" + rab + nl);
            Node root = ((Document) node).getDocumentElement();
            renderNode(sb, root, margin, indent, lab, rab, nl);
            break;

        case Node.ELEMENT_NODE:
            String name = node.getNodeName();
            NodeList children = node.getChildNodes();
            int nChildren = children.getLength();
            NamedNodeMap attributes = node.getAttributes();
            int nAttrs = attributes.getLength();

            boolean singleShortTextChild = (nAttrs == 0) && (nChildren == 1)
                    && (children.item(0).getNodeType() == Node.TEXT_NODE)
                    && (children.item(0).getTextContent().length() < 70)
                    && (!children.item(0).getTextContent().contains("\n"));

            if (singleShortTextChild) {
                sb.append(margin + lab + name + ((nChildren == 0) ? "/" : "") + rab);
            } else if (nAttrs == 0 && !singleShortTextChild) {
                sb.append(margin + lab + name + ((nChildren == 0) ? "/" : "") + rab + nl);
            } else if (nAttrs == 1) {
                Node attr = attributes.item(0);
                sb.append(margin + lab + name + " " + attr.getNodeName() + "=\"" + escapeChars(attr.getNodeValue())
                        + "\"" + ((nChildren == 0) ? "/" : "") + rab + nl);
            } else {
                sb.append(margin + lab + name + nl);
                for (int i = 0; i < nAttrs; i++) {
                    Node attr = attributes.item(i);
                    sb.append(margin + indent + attr.getNodeName() + "=\"" + escapeChars(attr.getNodeValue()));
                    if (i < nAttrs - 1)
                        sb.append("\"" + nl);
                    else
                        sb.append("\"" + ((nChildren == 0) ? "/" : "") + rab + nl);
                }
            }
            if (singleShortTextChild) {
                String text = escapeChars(node.getTextContent());
                sb.append(text.trim());
                sb.append(lab + "/" + name + rab + nl);
            } else {
                for (int i = 0; i < nChildren; i++) {
                    renderNode(sb, children.item(i), margin + indent, indent, lab, rab, nl);
                }
            }
            if (nChildren != 0 && !singleShortTextChild)
                sb.append(margin + lab + "/" + name + rab + nl);
            break;

        case Node.TEXT_NODE:
        case Node.CDATA_SECTION_NODE:
            String text = escapeChars(node.getNodeValue());
            String[] lines = text.split("\n");
            for (int i = 0; i < lines.length; i++) {
                if (!lines[i].trim().equals(""))
                    sb.append(margin + lines[i].trim() + nl);
            }
            break;

        case Node.PROCESSING_INSTRUCTION_NODE:
            sb.append(margin + lab + "?" + node.getNodeName() + " " + escapeChars(node.getNodeValue()) + "?" + rab
                    + nl);
            break;

        case Node.ENTITY_REFERENCE_NODE:
            sb.append("&" + node.getNodeName() + ";");
            break;

        case Node.DOCUMENT_TYPE_NODE:
            // Ignore document type nodes
            break;

        case Node.COMMENT_NODE:
            sb.append(margin + lab + "!--" + node.getNodeValue() + "--" + rab + nl);
            break;
        }
        return;
    }

    /**
     * Escape the ampersand, less-than, greater-than, single and double quote
     * characters in a string, replacing with their XML entities.
     * @param theString the string to escape.
     * @return the modified string.
     */
    public static String escapeChars(String theString) {
        return theString.replace("&", "&amp;").replace(">", "&gt;").replace("<", "&lt;").replace("\"", "&quot;")
                .replace("'", "&apos;");
    }
}

Related

  1. formatNode(Node node, int indent, StringBuffer stringBuffer)
  2. print(PrintStream out, Node node)
  3. printNode(StringBuffer sBuffer, Node node)
  4. printTree(Writer ps, Node node, String indent, String lineSeparator)
  5. renderNode(StringBuffer sb, Node node)