01: /*
02: * Copyright (C) 2004, 2005, 2006 Joe Walnes.
03: * Copyright (C) 2006, 2007 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 03. September 2004 by Joe Walnes
11: */
12: package com.thoughtworks.xstream.io.xml;
13:
14: import java.io.IOException;
15: import java.io.InputStream;
16: import java.io.OutputStream;
17: import java.io.OutputStreamWriter;
18: import java.io.Reader;
19: import java.io.Writer;
20:
21: import org.jdom.Document;
22: import org.jdom.JDOMException;
23: import org.jdom.input.SAXBuilder;
24:
25: import com.thoughtworks.xstream.io.HierarchicalStreamReader;
26: import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
27: import com.thoughtworks.xstream.io.StreamException;
28:
29: /**
30: * @author Laurent Bihanic
31: */
32: public class JDomDriver extends AbstractXmlDriver {
33:
34: public JDomDriver() {
35: super (new XmlFriendlyReplacer());
36: }
37:
38: /**
39: * @since 1.2
40: */
41: public JDomDriver(XmlFriendlyReplacer replacer) {
42: super (replacer);
43: }
44:
45: public HierarchicalStreamReader createReader(Reader reader) {
46: try {
47: SAXBuilder builder = new SAXBuilder();
48: Document document = builder.build(reader);
49: return new JDomReader(document, xmlFriendlyReplacer());
50: } catch (IOException e) {
51: throw new StreamException(e);
52: } catch (JDOMException e) {
53: throw new StreamException(e);
54: }
55: }
56:
57: public HierarchicalStreamReader createReader(InputStream in) {
58: try {
59: SAXBuilder builder = new SAXBuilder();
60: Document document = builder.build(in);
61: return new JDomReader(document, xmlFriendlyReplacer());
62: } catch (IOException e) {
63: throw new StreamException(e);
64: } catch (JDOMException e) {
65: throw new StreamException(e);
66: }
67: }
68:
69: public HierarchicalStreamWriter createWriter(Writer out) {
70: return new PrettyPrintWriter(out, xmlFriendlyReplacer());
71: }
72:
73: public HierarchicalStreamWriter createWriter(OutputStream out) {
74: return new PrettyPrintWriter(new OutputStreamWriter(out));
75: }
76:
77: }
|