Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

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

public class Main {
    private final static String CHAR_NBSP = "\u00A0";

    public static int getContentAsInt(Node n) {
        String content = getContent(n).trim();
        try {
            return Integer.parseInt(content);
        } catch (NumberFormatException e) {
            throw new IllegalStateException("Cannot get number from '" + content + "' (" + format(n, 0) + ")", e);
        }
    }

    public static String getContent(Node n) {
        assertNotNull("node == null", n);
        StringBuilder result = new StringBuilder();
        if (n.getNodeType() == Node.TEXT_NODE) {
            result.append(n.getNodeValue());
        }
        NodeList childNodes = n.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node n2 = childNodes.item(i);
            result.append(getContent(n2));
        }

        String tmp = result.toString();
        return tmp.replaceAll(CHAR_NBSP, " ");
    }

    public static String format(Node n, int indentLevel) {
        String indent = "";
        for (int i = 0; i < indentLevel; i++) {
            indent += "\t";
        }

        StringBuilder result = new StringBuilder();
        result.append(indent);
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            result.append("<");
            result.append(n.getNodeName());
            NamedNodeMap attrs = n.getAttributes();
            for (int i = 0; i < attrs.getLength(); i++) {
                Node attr = attrs.item(i);
                result.append(" ");
                result.append(attr.getNodeName());
                result.append("=\"");
                result.append(attr.getNodeValue());
                result.append("\"");
            }
            result.append(">\n");
        }

        if (n.getNodeType() == Node.TEXT_NODE) {
            String str = n.getNodeValue();
            result.append(str + "\n");
        }

        NodeList childNodes = n.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node n2 = childNodes.item(i);
            if (isEmptyTextNode(n2)) {
                continue;
            }
            result.append(format(n2, indentLevel + 1));
        }

        if (n.getNodeType() == Node.ELEMENT_NODE) {
            result.append(indent);
            result.append("</");
            result.append(n.getNodeName());
            result.append(">\n");
        }
        return result.toString();
    }

    private static void assertNotNull(String msg, Object o) {
        if (o == null) {
            throw new IllegalStateException(msg);
        }
    }

    public static List<Element> getChildNodes(Element parent, String name) {
        List<Element> result = new ArrayList<Element>();
        NodeList childNodes = parent.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node n = childNodes.item(i);
            if (name.equals(n.getNodeName())) {
                result.add((Element) n);
            }
        }
        return result;
    }

    public static String toString(Document doc) {
        try {
            StringWriter sw = new StringWriter();
            Source source = new DOMSource(doc);
            Result result = new StreamResult(sw);

            TransformerFactory tf = TransformerFactory.newInstance();
            tf.setAttribute("indent-number", new Integer(4));
            Transformer xformer = tf.newTransformer();
            xformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
            xformer.setOutputProperty(OutputKeys.INDENT, "yes");
            xformer.transform(source, result);
            return sw.toString();
        } catch (TransformerException e) {
            throw new RuntimeException(e);
        }
    }

    private static boolean isEmptyTextNode(Node node) {
        if (node.getNodeType() == Node.TEXT_NODE) {
            String str = node.getNodeValue();
            if (str == null || str.trim().length() == 0) {
                return true;
            }
        }
        return false;
    }
}