01: /*
02: * Copyright (C) 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 12. April 2006 by Joerg Schaible
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 nu.xom.Builder;
22: import nu.xom.Document;
23: import nu.xom.ParsingException;
24: import nu.xom.ValidityException;
25:
26: import com.thoughtworks.xstream.io.HierarchicalStreamReader;
27: import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
28: import com.thoughtworks.xstream.io.StreamException;
29:
30: public class XomDriver extends AbstractXmlDriver {
31:
32: private final Builder builder;
33:
34: public XomDriver() {
35: this (new Builder());
36: }
37:
38: public XomDriver(Builder builder) {
39: this (builder, new XmlFriendlyReplacer());
40: }
41:
42: /**
43: * @since 1.2
44: */
45: public XomDriver(XmlFriendlyReplacer replacer) {
46: this (new Builder(), replacer);
47: }
48:
49: /**
50: * @since 1.2
51: */
52: public XomDriver(Builder builder, XmlFriendlyReplacer replacer) {
53: super (replacer);
54: this .builder = builder;
55: }
56:
57: protected Builder getBuilder() {
58: return this .builder;
59: }
60:
61: public HierarchicalStreamReader createReader(Reader text) {
62: try {
63: Document document = builder.build(text);
64: return new XomReader(document, xmlFriendlyReplacer());
65: } catch (ValidityException e) {
66: throw new StreamException(e);
67: } catch (ParsingException e) {
68: throw new StreamException(e);
69: } catch (IOException e) {
70: throw new StreamException(e);
71: }
72: }
73:
74: public HierarchicalStreamReader createReader(InputStream in) {
75: try {
76: Document document = builder.build(in);
77: return new XomReader(document, xmlFriendlyReplacer());
78: } catch (ValidityException e) {
79: throw new StreamException(e);
80: } catch (ParsingException e) {
81: throw new StreamException(e);
82: } catch (IOException e) {
83: throw new StreamException(e);
84: }
85: }
86:
87: public HierarchicalStreamWriter createWriter(final Writer out) {
88: return new PrettyPrintWriter(out, xmlFriendlyReplacer());
89: }
90:
91: public HierarchicalStreamWriter createWriter(final OutputStream out) {
92: return new PrettyPrintWriter(new OutputStreamWriter(out),
93: xmlFriendlyReplacer());
94: }
95: }
|