View Javadoc

1   /**********************************************************************
2    * SkaringaAdapter.java
3    * created on 27.05.2007 by netseeker
4    * $Id: SkaringaAdapter.java,v 1.2 2007/11/17 10:56:38 netseeker Exp $
5    * $Log: SkaringaAdapter.java,v $
6    * Revision 1.2  2007/11/17 10:56:38  netseeker
7    * *** empty log message ***
8    *
9    * Revision 1.1  2007/05/27 22:13:09  netseeker
10   * *** empty log message ***
11   *
12   *
13   * ====================================================================
14   *
15   *  Copyright 2005-2006 netseeker aka Michael Manske
16   *
17   *  Licensed under the Apache License, Version 2.0 (the "License");
18   *  you may not use this file except in compliance with the License.
19   *  You may obtain a copy of the License at
20   *
21   *      http://www.apache.org/licenses/LICENSE-2.0
22   *
23   *  Unless required by applicable law or agreed to in writing, software
24   *  distributed under the License is distributed on an "AS IS" BASIS,
25   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26   *  See the License for the specific language governing permissions and
27   *  limitations under the License.
28   * ====================================================================
29   *
30   * This file is part of the EJOE framework.
31   * For more information on the author, please see
32   * <http://www.manskes.de/>.
33   *
34   *********************************************************************/
35  package de.netseeker.ejoe.adapter;
36  
37  import java.io.InputStream;
38  import java.io.OutputStream;
39  
40  import javax.xml.transform.stream.StreamResult;
41  import javax.xml.transform.stream.StreamSource;
42  
43  import com.skaringa.javaxml.NoImplementationException;
44  import com.skaringa.javaxml.ObjectTransformer;
45  import com.skaringa.javaxml.ObjectTransformerFactory;
46  
47  /***
48   * An adapter for (de)serializing objects via the great Skaringa library
49   * 
50   * @author netseeker aka Michael Manske
51   * @since 0.3.9.3
52   * @link http://skaringa.sourceforge.net/
53   */
54  public class SkaringaAdapter extends BaseAdapter
55  {
56      /***
57       * 
58       */
59      private static final long           serialVersionUID = 1L;
60  
61      private transient ObjectTransformer _transformer;
62  
63      private boolean                     compact          = false;
64  
65      /***
66       * Creates a new instance of SkaringaAdapter using compact XML
67       */
68      public SkaringaAdapter()
69      {
70          this( true );
71      }
72  
73      /***
74       * Creates a new instance of SkaringaAdapter
75       * 
76       * @param isCompact true if this Adapter should use compact xml otherwise false
77       */
78      public SkaringaAdapter(boolean isCompact)
79      {
80          this.compact = isCompact;
81          init();
82      }
83  
84      private void init()
85      {
86          try
87          {
88              _transformer = ObjectTransformerFactory.getInstance().getImplementation();
89              _transformer.setProperty( com.skaringa.javaxml.PropertyKeys.SORT_FIELDS, "yes" );
90              _transformer.setProperty( com.skaringa.javaxml.PropertyKeys.SKIP_UNKNOWN_FIELDS, "yes" );
91              if ( this.compact )
92              {
93                  _transformer.setProperty( javax.xml.transform.OutputKeys.INDENT, "no" );
94              }
95              else
96              {
97                  _transformer.setProperty( javax.xml.transform.OutputKeys.INDENT, "yes" );
98              }
99          }
100         catch ( NoImplementationException e )
101         {
102             RuntimeException re = new RuntimeException( e.getMessage() );
103             re.setStackTrace( e.getStackTrace() );
104             throw re;
105         }
106     }
107 
108     /*
109      * (non-Javadoc)
110      * 
111      * @see de.netseeker.ejoe.adapter.SerializeAdapter#read(java.io.InputStream)
112      */
113     public Object read( InputStream in ) throws Exception
114     {
115         return _transformer.deserialize( new StreamSource( in ) );
116     }
117 
118     /*
119      * (non-Javadoc)
120      * 
121      * @see de.netseeker.ejoe.adapter.SerializeAdapter#write(java.lang.Object, java.io.OutputStream)
122      */
123     public void write( Object obj, OutputStream out ) throws Exception
124     {
125         _transformer.serialize( obj, new StreamResult( out ) );
126     }
127 
128     /*
129      * (non-Javadoc)
130      * 
131      * @see de.netseeker.ejoe.adapter.BaseAdapter#getContentType()
132      */
133     public String getContentType()
134     {
135         return "text/xml";
136     }
137 
138     /*
139      * (non-Javadoc)
140      * 
141      * @see de.netseeker.ejoe.adapter.BaseAdapter#handleClassLoaderChange(java.lang.ClassLoader)
142      */
143     public void handleClassLoaderChange( ClassLoader classLoader )
144     {
145         this._transformer.setClassLoader( classLoader );
146     }
147 
148 }