1 /**********************************************************************
2 * BetwixtAdapter.java
3 * created on 11.03.2005 by netseeker
4 * $Source$
5 * $Date$
6 * $Revision$
7 *
8 * ====================================================================
9 *
10 * Copyright 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 package de.netseeker.ejoe.adapter;
31
32 import java.io.InputStream;
33 import java.io.OutputStream;
34
35 import org.apache.commons.betwixt.io.BeanReader;
36 import org.apache.commons.betwixt.io.BeanWriter;
37 import org.apache.commons.betwixt.registry.DefaultXMLBeanInfoRegistry;
38 import org.apache.commons.betwixt.registry.XMLBeanInfoRegistry;
39
40 import de.netseeker.ejoe.EJConstants;
41
42 /***
43 * SerializeAdapter implementation using Apaches Betwixt
44 *
45 * @author netseeker
46 * @since 0.3.5
47 * @link http://jakarta.apache.org/commons/betwixt/
48 */
49 /***
50 * @author netseeker
51 * @since
52 */
53 public class BetwixtAdapter extends BaseAdapter
54 {
55 private static final long serialVersionUID = 1L;
56
57 private static final String XML_DECLARATION = "<?xml version=\"1.0\" encoding=\""
58 + EJConstants.EJOE_DEFAULT_CHARSET
59 + "\" ?><!-- BETWIXT START -->";
60
61 private transient XMLBeanInfoRegistry _registry = null;
62
63 private XMLBeanInfoRegistry getRegistry()
64 {
65 if ( _registry == null )
66 {
67 _registry = new DefaultXMLBeanInfoRegistry();
68 }
69
70 return _registry;
71 }
72
73 /***
74 * @param registry
75 */
76 public void setXMLBeanInfoRegistry( XMLBeanInfoRegistry registry )
77 {
78 _registry = registry;
79 }
80
81
82
83
84
85
86 public Object read( InputStream in ) throws Exception
87 {
88 BeanReader beanReader = new BeanReader();
89 beanReader.getXMLIntrospector().setRegistry( getRegistry() );
90 beanReader.getBindingConfiguration().setMapIDs( false );
91
92 return beanReader.parse( in );
93 }
94
95
96
97
98
99
100 public void write( Object obj, OutputStream out ) throws Exception
101 {
102 BeanWriter beanWriter = new BeanWriter( out );
103 beanWriter.getXMLIntrospector().setRegistry( getRegistry() );
104 beanWriter.setWriteEmptyElements( true );
105 beanWriter.writeXmlDeclaration( XML_DECLARATION );
106 beanWriter.getBindingConfiguration().setMapIDs( false );
107 try
108 {
109 beanWriter.write( obj.getClass().getName(), obj );
110 }
111 finally
112 {
113 if ( beanWriter != null ) beanWriter.close();
114 }
115 }
116
117
118
119
120
121
122 public boolean requiresCustomEOFHandling()
123 {
124 return true;
125 }
126
127
128
129
130
131
132 public String getContentType()
133 {
134 return "text/xml";
135 }
136 }