Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;

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

public class Main {
    private static String BI = " ";

    private static String prettyPrintDom(Node node, String indent, boolean isRoot, boolean escapeStrings) {
        String ret = "";
        switch (node.getNodeType()) {

        case Node.DOCUMENT_NODE:
            // recurse on each child
            NodeList nodes = node.getChildNodes();
            if (nodes != null) {
                for (int i = 0; i < nodes.getLength(); i++) {
                    ret += prettyPrintDom(nodes.item(i), indent, isRoot, escapeStrings);
                }
            }
            break;

        case Node.ELEMENT_NODE:
            String name = node.getNodeName();
            ret += indent + "<" + name;
            NamedNodeMap attributes = node.getAttributes();
            for (int i = 0; i < attributes.getLength(); i++) {
                Node current = attributes.item(i);
                ret += " " + current.getNodeName() + "=\""
                        + ((escapeStrings) ? escapeStringForXML(current.getNodeValue()) : current.getNodeValue())
                        + "\"";
            }
            ret += ">";

            // recurse on each child
            NodeList children = node.getChildNodes();
            if (children != null) {
                for (int i = 0; i < children.getLength(); i++) {
                    String tmp = prettyPrintDom(children.item(i), indent + ((isRoot) ? "" : BI), false,
                            escapeStrings);
                    if (!tmp.replaceAll("[\\s]+", "").equals(""))
                        if (tmp.endsWith("\n"))
                            if (ret.endsWith("\n"))
                                ret += tmp;
                            else
                                ret += "\n" + tmp;
                        else
                            ret += tmp;
                }
            }
            if (ret.endsWith("\n"))
                ret += indent + "</" + name + ">\n";
            else
                ret += "</" + name + ">\n";
            break;

        case Node.TEXT_NODE:
            ret += (escapeStrings) ? escapeStringForXML(node.getNodeValue()) : node.getNodeValue();
            break;

        case Node.COMMENT_NODE:
            ret += "<!-- " + node.getNodeValue() + " -->";
            break;
        }
        return ret;
    }

    public static String escapeStringForXML(String aText) {
        final StringBuilder result = new StringBuilder();
        final StringCharacterIterator iterator = new StringCharacterIterator(aText);
        char character = iterator.current();
        while (character != CharacterIterator.DONE) {
            if (character == '<') {
                result.append("&lt;");
            } else if (character == '>') {
                result.append("&gt;");
            } else if (character == '\"') {
                result.append("&quot;");
            } else if (character == '\'') {
                result.append("&#039;");
            } else if (character == '&') {
                result.append("&amp;");
            } else {
                //the char is not a special one
                //add it to the result as is
                result.append(character);
            }
            character = iterator.next();
        }
        return result.toString();
    }
}