org.magicwerk.brownies.jdom.DomTools Maven / Gradle / Ivy
/*
* Copyright 2010 by Thomas Mauch
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $Id$
*/
package org.magicwerk.brownies.jdom;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import org.magicwerk.brownies.core.exceptions.WrapperException;
import org.magicwerk.brownies.jdom.XmlProcessor.Validation;
import org.w3c.dom.Document;
import org.xml.sax.XMLReader;
/**
* Class for working with DOM documents.
*
* @author Thomas Mauch
* @version $Id$
*/
public class DomTools {
/**
* Read XML document from file as DOM-document.
*
* @param file file to read
* @return XML document
*/
// TODO: Is the file validated?
public static Document readDocument2(String file) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder parser = factory.newDocumentBuilder();
Document document = parser.parse(file);
return document;
}
catch (Exception e) {
throw new WrapperException(e);
}
}
public static Document readDocument(String url) {
return readDocument(url, Validation.NONE);
}
public static Document readDocument(String url, Validation validation) {
XmlSource source = new XmlSource(url).setValidation(validation);
return readDocument(source);
}
public static Document readDocument(XmlSource source) {
return readDocument(source, new XmlProcessor());
}
public static Document readDocument(XmlSource source, XmlProcessor proc) {
try {
XMLReader saxParser = proc.getXmlReader(source);
Source input = new SAXSource(saxParser, source.getInputSource());
DOMResult output = new DOMResult();
TransformerFactory xformFactory = TransformerFactory.newInstance();
Transformer idTransform = xformFactory.newTransformer();
idTransform.transform(input, output);
Document doc = (Document) output.getNode();
return doc;
}
catch (Exception e) {
throw new WrapperException(e);
}
}
/**
* Write a DOM-document to file.
* The XML document is formatted in a human readable way.
*
* @param doc document to write
* @param file file to write
*/
public static void writeDocument(Document doc, String file) {
try {
TransformerFactory xformFactory = TransformerFactory.newInstance();
Transformer idTransform = xformFactory.newTransformer();
Source input = new DOMSource(doc);
Result output = new StreamResult(file);
idTransform.transform(input, output);
}
catch (Exception e) {
// TransformerConfigurationException, TransformerException
throw new RuntimeException(e);
}
}
public static String printDocument(Document document) {
try {
TransformerFactory xformFactory = TransformerFactory.newInstance();
Transformer idTransform = xformFactory.newTransformer();
Source input = new DOMSource(document);
StringWriter sw = new StringWriter();
Result output = new StreamResult(sw);
idTransform.transform(input, output);
return sw.toString();
}
catch (Exception e) {
// TransformerConfigurationException, TransformerException
throw new RuntimeException(e);
}
}
}