01: /*
02: * Copyright (C) 2004, 2005, 2006 Joe Walnes.
03: * Copyright (C) 2006, 2007, 2008 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 08. March 2004 by Joe Walnes
11: */
12: package com.thoughtworks.xstream.io.xml;
13:
14: import com.thoughtworks.xstream.core.util.XmlHeaderAwareReader;
15: import com.thoughtworks.xstream.io.HierarchicalStreamReader;
16: import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
17: import com.thoughtworks.xstream.io.StreamException;
18:
19: import java.io.IOException;
20: import java.io.InputStream;
21: import java.io.OutputStream;
22: import java.io.OutputStreamWriter;
23: import java.io.Reader;
24: import java.io.UnsupportedEncodingException;
25: import java.io.Writer;
26:
27: public class XppDriver extends AbstractXmlDriver {
28:
29: private static boolean xppLibraryPresent;
30:
31: public XppDriver() {
32: super (new XmlFriendlyReplacer());
33: }
34:
35: /**
36: * @since 1.2
37: */
38: public XppDriver(XmlFriendlyReplacer replacer) {
39: super (replacer);
40: }
41:
42: public HierarchicalStreamReader createReader(Reader xml) {
43: loadLibrary();
44: return new XppReader(xml, xmlFriendlyReplacer());
45: }
46:
47: public HierarchicalStreamReader createReader(InputStream in) {
48: try {
49: return createReader(new XmlHeaderAwareReader(in));
50: } catch (UnsupportedEncodingException e) {
51: throw new StreamException(e);
52: } catch (IOException e) {
53: throw new StreamException(e);
54: }
55: }
56:
57: private void loadLibrary() {
58: if (!xppLibraryPresent) {
59: try {
60: Class.forName("org.xmlpull.mxp1.MXParser");
61: } catch (ClassNotFoundException e) {
62: throw new IllegalArgumentException(
63: "XPP3 pull parser library not present. Specify another driver."
64: + " For example: new XStream(new DomDriver())");
65: }
66: xppLibraryPresent = true;
67: }
68: }
69:
70: public HierarchicalStreamWriter createWriter(Writer out) {
71: return new PrettyPrintWriter(out, xmlFriendlyReplacer());
72: }
73:
74: public HierarchicalStreamWriter createWriter(OutputStream out) {
75: return createWriter(new OutputStreamWriter(out));
76: }
77: }
|