netbout-spi-1.4.3.src.main.java.com.netbout.spi.xml.DomPrinter Maven / Gradle / Ivy
/**
* Copyright (c) 2009-2011, NetBout.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the NetBout.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.netbout.spi.xml;
import java.io.StringWriter;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
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;
/**
* Converts a DOM object to XML.
*
* @author Yegor Bugayenko ([email protected])
* @version $Id: DomPrinter.java 1717 2012-02-29 03:37:34Z guard $
*/
public final class DomPrinter {
/**
* Transformer factory.
*/
public static final TransformerFactory FACTORY =
TransformerFactory.newInstance();
/**
* The DOM element to work with.
*/
private final transient Element element;
/**
* Public ctor.
* @param document The object to convert
*/
public DomPrinter(final Document document) {
this.element = document.getDocumentElement();
}
/**
* Public ctor.
* @param elm The object to convert
*/
public DomPrinter(final Element elm) {
this.element = elm;
}
/**
* Print the XML document.
* @return The document
*/
public String print() {
final StringWriter writer = new StringWriter();
try {
final Transformer transformer = this.FACTORY.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(
new DOMSource(this.element),
new StreamResult(writer)
);
} catch (javax.xml.transform.TransformerConfigurationException ex) {
throw new IllegalStateException(ex);
} catch (javax.xml.transform.TransformerException ex) {
throw new IllegalStateException(ex);
}
return writer.toString();
}
}