package com.thoughtworks.xstream.io.xml;

import java.io.FilterOutputStream;
import java.io.FilterWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

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

public class Dom4JDriver extends AbstractXmlDriver {

    private DocumentFactory documentFactory;
    private OutputFormat outputFormat;

    public Dom4JDriver() {
        this(new DocumentFactory(), OutputFormat.createPrettyPrint());
        outputFormat.setTrimText(false);
    }

    public Dom4JDriver(DocumentFactory documentFactory, OutputFormat outputFormat) {
        this(documentFactory, outputFormat, new XmlFriendlyReplacer());
    }

    /**
     * @since 1.2
     */
    public Dom4JDriver(DocumentFactory documentFactory, OutputFormat outputFormat, XmlFriendlyReplacer replacer) {
        super(replacer);
        this.documentFactory = documentFactory;
        this.outputFormat = outputFormat;
    }


    public DocumentFactory getDocumentFactory() {
        return documentFactory;
    }

    public void setDocumentFactory(DocumentFactory documentFactory) {
        this.documentFactory = documentFactory;
    }

    public OutputFormat getOutputFormat() {
        return outputFormat;
    }

    public void setOutputFormat(OutputFormat outputFormat) {
        this.outputFormat = outputFormat;
    }

    public HierarchicalStreamReader createReader(Reader text) {
        try {
            SAXReader reader = new SAXReader();
            Document document = reader.read(text);
            return new Dom4JReader(document, xmlFriendlyReplacer());
        } catch (DocumentException e) {
            throw new StreamException(e);
        }
    }

    public HierarchicalStreamReader createReader(InputStream in) {
        try {
            SAXReader reader = new SAXReader();
            Document document = reader.read(in);
            return new Dom4JReader(document, xmlFriendlyReplacer());
        } catch (DocumentException e) {
            throw new StreamException(e);
        }
    }

    public HierarchicalStreamWriter createWriter(final Writer out) {
        final HierarchicalStreamWriter[] writer = new HierarchicalStreamWriter[1];
        final FilterWriter filter = new FilterWriter(out){
            public void close() {
                writer[0].close();
            }
        };
        writer[0] = new Dom4JWriter(new XMLWriter(filter,  outputFormat), xmlFriendlyReplacer());
        return writer[0];
    }

    public HierarchicalStreamWriter createWriter(final OutputStream out) {
        try {
            final HierarchicalStreamWriter[] writer = new HierarchicalStreamWriter[1];
            final FilterOutputStream filter = new FilterOutputStream(out){
                public void close() {
                    writer[0].close();
                }
            };
            writer[0] = new Dom4JWriter(new XMLWriter(filter,  outputFormat), xmlFriendlyReplacer());
            return writer[0];
        } catch (IOException e) {
            throw new StreamException(e);
        }
    }
}

@@ -176,32 +176,67 @@
 o.OutputStream;%0A
+import java.io.OutputStreamWriter;%0A
 import java.io.R
@@ -2974,58 +2974,20 @@
     
-try %7B%0A            final HierarchicalStream
+final 
 Writer
-%5B%5D
  wri
@@ -3000,439 +3000,69 @@
 new 
-HierarchicalStreamWriter%5B1%5D;%0A            final FilterOutputStream filter = new FilterOutputStream(out)%7B%0A                public void close() %7B%0A                    writer%5B0%5D.close();%0A                %7D%0A            %7D;%0A            writer%5B0%5D = new Dom4JWriter(new XMLWriter(filter,  outputFormat), xmlFriendlyReplacer());%0A            return writer%5B0%5D;%0A        %7D catch (IOException e) %7B%0A            throw new StreamException(e);%0A        %7D
+OutputStreamWriter(out);%0A        return createWriter(writer);
 %0A   

Simplify writer creation.