001: /*
002: * Copyright (C) 2004, 2005, 2006 Joe Walnes.
003: * Copyright (C) 2006, 2007, 2008 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.IOException;
015: import java.io.InputStream;
016: import java.io.OutputStream;
017: import java.io.OutputStreamWriter;
018: import java.io.Reader;
019: import java.io.UnsupportedEncodingException;
020: import java.io.Writer;
021:
022: import javax.xml.parsers.DocumentBuilder;
023: import javax.xml.parsers.DocumentBuilderFactory;
024: import javax.xml.parsers.FactoryConfigurationError;
025: import javax.xml.parsers.ParserConfigurationException;
026:
027: import org.w3c.dom.Document;
028: import org.xml.sax.InputSource;
029: import org.xml.sax.SAXException;
030:
031: import com.thoughtworks.xstream.io.HierarchicalStreamReader;
032: import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
033: import com.thoughtworks.xstream.io.StreamException;
034:
035: public class DomDriver extends AbstractXmlDriver {
036:
037: private final String encoding;
038: private final DocumentBuilderFactory documentBuilderFactory;
039:
040: /**
041: * Construct a DomDriver.
042: */
043: public DomDriver() {
044: this (null);
045: }
046:
047: /**
048: * Construct a DomDriver with a specified encoding. The created DomReader will ignore any
049: * encoding attribute of the XML header though.
050: */
051: public DomDriver(String encoding) {
052: this (encoding, new XmlFriendlyReplacer());
053: }
054:
055: /**
056: * @since 1.2
057: */
058: public DomDriver(String encoding, XmlFriendlyReplacer replacer) {
059: super (replacer);
060: documentBuilderFactory = DocumentBuilderFactory.newInstance();
061: this .encoding = encoding;
062: }
063:
064: public HierarchicalStreamReader createReader(Reader xml) {
065: return createReader(new InputSource(xml));
066: }
067:
068: public HierarchicalStreamReader createReader(InputStream xml) {
069: return createReader(new InputSource(xml));
070: }
071:
072: private HierarchicalStreamReader createReader(InputSource source) {
073: try {
074: DocumentBuilder documentBuilder = documentBuilderFactory
075: .newDocumentBuilder();
076: if (encoding != null) {
077: source.setEncoding(encoding);
078: }
079: Document document = documentBuilder.parse(source);
080: return new DomReader(document, xmlFriendlyReplacer());
081: } catch (FactoryConfigurationError e) {
082: throw new StreamException(e);
083: } catch (ParserConfigurationException e) {
084: throw new StreamException(e);
085: } catch (SAXException e) {
086: throw new StreamException(e);
087: } catch (IOException e) {
088: throw new StreamException(e);
089: }
090: }
091:
092: public HierarchicalStreamWriter createWriter(Writer out) {
093: return new PrettyPrintWriter(out, xmlFriendlyReplacer());
094: }
095:
096: public HierarchicalStreamWriter createWriter(OutputStream out) {
097: try {
098: return createWriter(encoding != null ? new OutputStreamWriter(
099: out, encoding)
100: : new OutputStreamWriter(out));
101: } catch (UnsupportedEncodingException e) {
102: throw new StreamException(e);
103: }
104: }
105: }
|