001: /*
002: * Copyright (C) 2006, 2007 XStream Committers.
003: * All rights reserved.
004: *
005: * The software in this package is published under the terms of the BSD
006: * style license a copy of which has been included with this distribution in
007: * the LICENSE.txt file.
008: *
009: * Created on 13. April 2006 by Joerg Schaible
010: */
011: package com.thoughtworks.xstream;
012:
013: import com.thoughtworks.xstream.converters.ConversionException;
014: import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
015: import com.thoughtworks.xstream.io.HierarchicalStreamReader;
016: import com.thoughtworks.xstream.io.xml.XppDriver;
017:
018: import java.io.IOException;
019: import java.io.ObjectInputStream;
020: import java.io.ObjectOutputStream;
021: import java.io.ObjectStreamException;
022: import java.io.Reader;
023: import java.io.StringReader;
024: import java.io.StringWriter;
025: import java.io.Writer;
026:
027: /**
028: * Self-contained XStream generator. The class is a utility to write XML streams that contain
029: * additionally the XStream that was used to serialize the object graph. Such a stream can
030: * be unmarshalled using this embedded XStream instance, that kept any settings.
031: *
032: * @author Jörg Schaible
033: * @since 1.2
034: */
035: public class XStreamer {
036:
037: /**
038: * Serialize an object including the XStream to a pretty-printed XML String.
039: *
040: * @throws ObjectStreamException if the XML contains non-serializable elements
041: * @throws com.thoughtworks.xstream.XStreamException if the object cannot be serialized
042: * @since 1.2
043: * @see #toXML(XStream, Object, Writer)
044: */
045: public String toXML(XStream xstream, Object obj)
046: throws ObjectStreamException {
047: Writer writer = new StringWriter();
048: try {
049: toXML(xstream, obj, writer);
050: } catch (ObjectStreamException e) {
051: throw e;
052: } catch (IOException e) {
053: throw new ConversionException(
054: "Unexpeced IO error from a StringWriter", e);
055: }
056: return writer.toString();
057: }
058:
059: /**
060: * Serialize an object including the XStream to the given Writer as pretty-printed XML.
061: * <p>
062: * Warning: XStream will serialize itself into this XML stream. To read such an XML code, you
063: * should use {@link XStreamer#fromXML(Reader)} or one of the other overloaded
064: * methods. Since a lot of internals are written into the stream, you cannot expect to use such
065: * an XML to work with another XStream version or with XStream running on different JDKs and/or
066: * versions. We have currently no JDK 1.3 support, nor will the PureReflectionConverter work
067: * with a JDK less than 1.5.
068: * </p>
069: *
070: * @throws IOException if an error occurs reading from the Writer.
071: * @throws com.thoughtworks.xstream.XStreamException if the object cannot be serialized
072: * @since 1.2
073: */
074: public void toXML(XStream xstream, Object obj, Writer out)
075: throws IOException {
076: XStream outer = new XStream();
077: ObjectOutputStream oos = outer.createObjectOutputStream(out);
078: try {
079: oos.writeObject(xstream);
080: oos.flush();
081: xstream.toXML(obj, out);
082: } finally {
083: oos.close();
084: }
085: }
086:
087: /**
088: * Deserialize a self-contained XStream with object from a String. The method will use
089: * internally an XppDriver to load the contained XStream instance.
090: *
091: * @throws ClassNotFoundException if a class in the XML stream cannot be found
092: * @throws ObjectStreamException if the XML contains non-deserializable elements
093: * @throws com.thoughtworks.xstream.XStreamException if the object cannot be deserialized
094: * @since 1.2
095: * @see #toXML(XStream, Object, Writer)
096: */
097: public Object fromXML(String xml) throws ClassNotFoundException,
098: ObjectStreamException {
099: try {
100: return fromXML(new StringReader(xml));
101: } catch (ObjectStreamException e) {
102: throw e;
103: } catch (IOException e) {
104: throw new ConversionException(
105: "Unexpeced IO error from a StringReader", e);
106: }
107: }
108:
109: /**
110: * Deserialize a self-contained XStream with object from a String.
111: *
112: * @throws ClassNotFoundException if a class in the XML stream cannot be found
113: * @throws ObjectStreamException if the XML contains non-deserializable elements
114: * @throws com.thoughtworks.xstream.XStreamException if the object cannot be deserialized
115: * @since 1.2
116: * @see #toXML(XStream, Object, Writer)
117: */
118: public Object fromXML(HierarchicalStreamDriver driver, String xml)
119: throws ClassNotFoundException, ObjectStreamException {
120: try {
121: return fromXML(driver, new StringReader(xml));
122: } catch (ObjectStreamException e) {
123: throw e;
124: } catch (IOException e) {
125: throw new ConversionException(
126: "Unexpeced IO error from a StringReader", e);
127: }
128: }
129:
130: /**
131: * Deserialize a self-contained XStream with object from an XML Reader. The method will use
132: * internally an XppDriver to load the contained XStream instance.
133: *
134: * @throws IOException if an error occurs reading from the Reader.
135: * @throws ClassNotFoundException if a class in the XML stream cannot be found
136: * @throws com.thoughtworks.xstream.XStreamException if the object cannot be deserialized
137: * @since 1.2
138: * @see #toXML(XStream, Object, Writer)
139: */
140: public Object fromXML(Reader xml) throws IOException,
141: ClassNotFoundException {
142: return fromXML(new XppDriver(), xml);
143: }
144:
145: /**
146: * Deserialize a self-contained XStream with object from an XML Reader.
147: *
148: * @throws IOException if an error occurs reading from the Reader.
149: * @throws ClassNotFoundException if a class in the XML stream cannot be found
150: * @throws com.thoughtworks.xstream.XStreamException if the object cannot be deserialized
151: * @since 1.2
152: */
153: public Object fromXML(HierarchicalStreamDriver driver, Reader xml)
154: throws IOException, ClassNotFoundException {
155: XStream outer = new XStream(driver);
156: HierarchicalStreamReader reader = driver.createReader(xml);
157: ObjectInputStream configIn = outer
158: .createObjectInputStream(reader);
159: try {
160: XStream configured = (XStream) configIn.readObject();
161: ObjectInputStream in = configured
162: .createObjectInputStream(reader);
163: try {
164: return in.readObject();
165: } finally {
166: in.close();
167: }
168: } finally {
169: configIn.close();
170: }
171: }
172:
173: }
|