package tools.xml;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.dom.DOMSource;
import java.io.*;

/**
 * Created by IntelliJ IDEA.
 * User: Igor
 * Date: 18.12.2005
 * Time: 15:27:36
 * To change this template use File | Settings | File Templates.
 */
public class XMLIO
{
    public static Document newDocument() throws ParserConfigurationException
    {
        return DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    }

    public static Document load(InputStream is) throws ParserConfigurationException, IOException, SAXException
    {
        return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
    }

    public static Document load(String filename) throws ParserConfigurationException, IOException, SAXException
    {
        return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(filename));
    }

    public static void save(String filename, Document document) throws TransformerException, FileNotFoundException
    {
        save(new FileOutputStream(filename), document);
    }

    public static void save(OutputStream out, Document document) throws TransformerException
    {
        TransformerFactory.newInstance().newTransformer().transform(new DOMSource(document), new StreamResult(out));
    }
}
