001: /*
002: * Copyright (C) 2004, 2005, 2006 Joe Walnes.
003: * Copyright (C) 2006, 2007 XStream Committers.
004: * All rights reserved.
005: *
006: * The software in this package is published under the terms of the BSD
007: * style license a copy of which has been included with this distribution in
008: * the LICENSE.txt file.
009: *
010: * Created on 07. March 2004 by Joe Walnes
011: */
012: package com.thoughtworks.xstream.io.xml;
013:
014: import java.io.FilterWriter;
015: import java.io.InputStream;
016: import java.io.OutputStream;
017: import java.io.OutputStreamWriter;
018: import java.io.Reader;
019: import java.io.Writer;
020:
021: import org.dom4j.Document;
022: import org.dom4j.DocumentException;
023: import org.dom4j.DocumentFactory;
024: import org.dom4j.io.OutputFormat;
025: import org.dom4j.io.SAXReader;
026: import org.dom4j.io.XMLWriter;
027:
028: import com.thoughtworks.xstream.io.HierarchicalStreamReader;
029: import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
030: import com.thoughtworks.xstream.io.StreamException;
031:
032: public class Dom4JDriver extends AbstractXmlDriver {
033:
034: private DocumentFactory documentFactory;
035: private OutputFormat outputFormat;
036:
037: public Dom4JDriver() {
038: this (new DocumentFactory(), OutputFormat.createPrettyPrint());
039: outputFormat.setTrimText(false);
040: }
041:
042: public Dom4JDriver(DocumentFactory documentFactory,
043: OutputFormat outputFormat) {
044: this (documentFactory, outputFormat, new XmlFriendlyReplacer());
045: }
046:
047: /**
048: * @since 1.2
049: */
050: public Dom4JDriver(DocumentFactory documentFactory,
051: OutputFormat outputFormat, XmlFriendlyReplacer replacer) {
052: super (replacer);
053: this .documentFactory = documentFactory;
054: this .outputFormat = outputFormat;
055: }
056:
057: public DocumentFactory getDocumentFactory() {
058: return documentFactory;
059: }
060:
061: public void setDocumentFactory(DocumentFactory documentFactory) {
062: this .documentFactory = documentFactory;
063: }
064:
065: public OutputFormat getOutputFormat() {
066: return outputFormat;
067: }
068:
069: public void setOutputFormat(OutputFormat outputFormat) {
070: this .outputFormat = outputFormat;
071: }
072:
073: public HierarchicalStreamReader createReader(Reader text) {
074: try {
075: SAXReader reader = new SAXReader();
076: Document document = reader.read(text);
077: return new Dom4JReader(document, xmlFriendlyReplacer());
078: } catch (DocumentException e) {
079: throw new StreamException(e);
080: }
081: }
082:
083: public HierarchicalStreamReader createReader(InputStream in) {
084: try {
085: SAXReader reader = new SAXReader();
086: Document document = reader.read(in);
087: return new Dom4JReader(document, xmlFriendlyReplacer());
088: } catch (DocumentException e) {
089: throw new StreamException(e);
090: }
091: }
092:
093: public HierarchicalStreamWriter createWriter(final Writer out) {
094: final HierarchicalStreamWriter[] writer = new HierarchicalStreamWriter[1];
095: final FilterWriter filter = new FilterWriter(out) {
096: public void close() {
097: writer[0].close();
098: }
099: };
100: writer[0] = new Dom4JXmlWriter(new XMLWriter(filter,
101: outputFormat), xmlFriendlyReplacer());
102: return writer[0];
103: }
104:
105: public HierarchicalStreamWriter createWriter(final OutputStream out) {
106: final Writer writer = new OutputStreamWriter(out);
107: return createWriter(writer);
108: }
109: }
|