Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/***********************************************************************************************************************
 * Copyright (c) 2008 empolis GmbH and brox IT Solutions GmbH. All rights reserved. This program and the accompanying
 * materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors: Ivan Churkin (brox IT Solutions GmbH) - initial creator
 *               Sebastian Voigt (brox IT Solutions GmbH)
 *               Andreas Weber (empolis GmbH) - switch to XML 1.1 in header to make more documents readable.
 **********************************************************************************************************************/

import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
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;

public class Main {
    /**
     * The Constant TRANSFORMER_FACTORY.
     */
    public static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance();

    /**
     * To string.
     *
     * @param document
     *          the document
     *
     * @return the string
     */
    public static String toString(final Document document) {
        if (document == null) {
            return null;
        }
        // final StringWriter xmlString = new StringWriter();
        // final OutputFormat format = new OutputFormat(document);
        // final XMLSerializer serializer = new XMLSerializer(xmlString, format);
        // serializer.serialize(document);
        // final String xml = xmlString.toString();
        // return xml;
        return toString(document, false);
    }

    /**
     * To string.
     *
     * @param document
     *          the document
     * @param xmlDeclaration
     *          the xml declaration
     *
     * @return the string
     */
    public static String toString(final Document document, final boolean xmlDeclaration) {
        if (document == null) {
            return null;
        }
        final StreamResult streamResult = new StreamResult(new StringWriter());
        transform(document, streamResult, xmlDeclaration);
        final String stringXml = streamResult.getWriter().toString();
        return stringXml;
    }

    /**
     * Transform.
     *
     * @param document
     *          the document
     * @param streamResult
     *          the stream result
     * @param xmlDeclaration
     *          the xml declaration
     */
    public static void transform(final Document document, final StreamResult streamResult,
            final boolean xmlDeclaration) {
        if (document == null) {
            throw new IllegalArgumentException("document cannot be NULL!");
        }
        if (streamResult == null) {
            throw new IllegalArgumentException("streamResult cannot be NULL!");
        }
        Transformer transformer;
        try {
            transformer = TRANSFORMER_FACTORY.newTransformer();
        } catch (final TransformerConfigurationException e) {
            throw new RuntimeException(e);
        }
        transformer.setOutputProperty(OutputKeys.INDENT, "no");
        String strXmlDeclaration;
        if (xmlDeclaration) {
            strXmlDeclaration = "no";
        } else {
            strXmlDeclaration = "yes";
        }
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, strXmlDeclaration);
        final DOMSource source = new DOMSource(document);
        try {
            transformer.transform(source, streamResult);
        } catch (final TransformerException e) {
            throw new RuntimeException(e);
        }
    }
}