package com.thoughtworks.xstream.io.xml;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;

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

import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.StreamException;

public class DomDriver extends AbstractXmlDriver {
    
    private final String encoding;
    private final DocumentBuilderFactory documentBuilderFactory;

    public DomDriver() {
        this("UTF-8");
    }

    public DomDriver(String encoding) {
        this(encoding, new XmlFriendlyReplacer());
    }

    /**
     * @since 1.2
     */
    public DomDriver(String encoding, XmlFriendlyReplacer replacer) {
        super(replacer);
        documentBuilderFactory = DocumentBuilderFactory.newInstance();
        this.encoding = encoding;
    }
    
    public HierarchicalStreamReader createReader(Reader xml) {
        return createReader(new InputSource(xml));
    }

    public HierarchicalStreamReader createReader(InputStream xml) {
        return createReader(new InputSource(xml));
    }

    private HierarchicalStreamReader createReader(InputSource source) {
        try {
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            source.setEncoding(encoding);
            Document document = documentBuilder.parse(source);
            return new DomReader(document);
        } catch (FactoryConfigurationError e) {
            throw new StreamException(e);
        } catch (ParserConfigurationException e) {
            throw new StreamException(e);
        } catch (SAXException e) {
            throw new StreamException(e);
        } catch (IOException e) {
            throw new StreamException(e);
        }
    }

    public HierarchicalStreamWriter createWriter(Writer out) {
        return new PrettyPrintWriter(out);
    }

    public HierarchicalStreamWriter createWriter(OutputStream out) {
        try {
			return createWriter(new OutputStreamWriter(out, this.encoding));
		} catch (UnsupportedEncodingException e) {
			throw new StreamException(e);
		}
    }
}

@@ -1841,16 +1841,39 @@
 document
+, xmlFriendlyReplacer()
 );%0A     
@@ -2323,19 +2323,42 @@
 iter(out
+, xmlFriendlyReplacer()
 );%0A
-
     %7D%0A%0A 
@@ -2439,19 +2439,28 @@
   try %7B%0A
-%09%09%09
+            
 return c
@@ -2516,18 +2516,24 @@
 ding));%0A
-%09%09
+        
 %7D catch 
@@ -2571,11 +2571,20 @@
 ) %7B%0A
-%09%09%09
+            
 thro
@@ -2613,10 +2613,16 @@
 e);%0A
-%09%09
+        
 %7D%0A  

Fix DomDriver ignoring given XmlFriendlyReplace (XSTR-398).