1 /**********************************************************************
2 * XStreamAdapter.java
3 * created on 08.08.2004 by netseeker
4 * $Source: /cvsroot/ejoe/EJOE/src/de/netseeker/ejoe/adapter/XStreamAdapter.java,v $
5 * $Date: 2007/11/17 10:59:40 $
6 * $Revision: 1.37 $
7 *
8 * ====================================================================
9 *
10 * Copyright 2005-2006 netseeker aka Michael Manske
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 * ====================================================================
24 *
25 * This file is part of the ejoe framework.
26 * For more information on the author, please see
27 * <http://www.manskes.de/>.
28 *
29 *********************************************************************/
30
31 package de.netseeker.ejoe.adapter;
32
33 import java.io.InputStream;
34 import java.io.OutputStream;
35 import java.io.OutputStreamWriter;
36
37 import com.thoughtworks.xstream.XStream;
38 import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
39 import com.thoughtworks.xstream.io.xml.CompactWriter;
40
41 import de.netseeker.ejoe.EJConstants;
42
43 /***
44 * An adapter for (de)serializing objects via the great XStream library
45 *
46 * @author netseeker aka Michael Manske
47 * @since 0.3.0
48 * @link http://xstream.codehaus.org
49 */
50 public class XStreamAdapter extends BaseAdapter
51 {
52 private static final long serialVersionUID = 1L;
53
54 protected XStream _xstream = null;
55
56 private boolean _isCompact = true;
57
58 /***
59 * Creates a new instance of XStreamAdapter using compact XML
60 */
61 public XStreamAdapter()
62 {
63 this( true );
64 }
65
66 /***
67 * Creates a new instance of XStreamAdapter
68 *
69 * @param isCompact true if this Adapter should use compact xml otherwise false
70 * @see com.thoughtworks.xstream.io.xml.CompactWriter
71 */
72 public XStreamAdapter(boolean isCompact)
73 {
74 this._isCompact = isCompact;
75 this._xstream = new XStream();
76 }
77
78 /***
79 * Creates a new instance of XStreamAdapter using the given HierarchicalStreamDriver
80 */
81 public XStreamAdapter(HierarchicalStreamDriver driver)
82 {
83 this._xstream = new XStream( driver );
84 }
85
86
87
88
89
90
91 public Object read( InputStream in ) throws Exception
92 {
93 return _xstream.fromXML( in );
94 }
95
96
97
98
99
100
101 public void write( Object obj, OutputStream out ) throws Exception
102 {
103 if ( this._isCompact )
104 {
105 _xstream
106 .marshal( obj, new CompactWriter( new OutputStreamWriter( out, EJConstants.EJOE_DEFAULT_CHARSET ) ) );
107 }
108 else
109 {
110 _xstream.toXML( obj, out );
111 }
112 }
113
114
115
116
117
118
119 public void handleClassLoaderChange( ClassLoader classLoader )
120 {
121 _xstream.setClassLoader( classLoader );
122 }
123
124
125
126
127
128
129 public String getContentType()
130 {
131 return "text/xml";
132 }
133 }