Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.io.StringReader;
import java.io.StringWriter;
import java.net.HttpURLConnection;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

public class Main {
    /**
     * Reads a Document from an HttpURLConnection.
     *
     * @param connection
     * @return
     * @throws IOException
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws TransformerException
     */
    public static Document readDocument(HttpURLConnection connection)
            throws IOException, SAXException, ParserConfigurationException, TransformerException {
        String xmlString = readString(connection);
        return toDocument(xmlString);
    }

    /**
     * Reads an XML document from an input stream.
     *
     * @param inputStream
     * @return Document
     * @throws ParserConfigurationException if we failed to creaate a document build
     * @throws IOException                  if there was an input error reading the stream
     * @throws SAXException                 if we couldn't parse the XML
     */
    public static final Document readDocument(InputStream inputStream)
            throws ParserConfigurationException, IOException, SAXException {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        return builder.parse(inputStream);
    }

    /**
     * Reads an XML String from an HttpURLConnection.
     *
     * @param connection
     * @return
     * @throws IOException
     */
    public static String readString(HttpURLConnection connection) throws IOException {
        StringBuffer buffer = new StringBuffer();
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }
        reader.close();
        return buffer.toString();
    }

    /**
     * Converts an XML String to a Document.
     *
     * @param string
     * @return
     * @throws TransformerException
     * @throws ParserConfigurationException
     */
    public static Document toDocument(String string) throws TransformerException, ParserConfigurationException {

        // if there is a byte order mark, strip it off.
        // otherwise, we get a org.xml.sax.SAXParseException: Content is not allowed in prolog
        if (string.startsWith("\uFEFF")) {
            string = string.substring(1);
        }

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();

        // StringReader source
        Source source = new StreamSource(new StringReader(string));

        // Document result
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document document = builder.newDocument();
        Result result = new DOMResult(document);

        transformer.transform(source, result);
        return document;
    }

    /**
     * Converts an XML node (or document) to an XML String.
     *
     * @param node
     * @return
     * @throws TransformerException
     */
    public static String toString(Node node) throws TransformerException {

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();

        // Node source
        DOMSource source = new DOMSource(node);

        // StringWriter result
        StringWriter stringWriter = new StringWriter();
        Result result = new StreamResult(stringWriter);

        transformer.transform(source, result);
        return stringWriter.toString();
    }
}