KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > xml > querying > impl > xtas > object > plugins > CastorMappedMarshaller


1 package org.exoplatform.services.xml.querying.impl.xtas.object.plugins;
2
3 import java.io.InputStream JavaDoc;
4 import java.io.OutputStream JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.ByteArrayOutputStream JavaDoc;
7 import java.io.OutputStreamWriter JavaDoc;
8 import java.io.InputStreamReader JavaDoc;
9 import java.net.URL JavaDoc;
10
11 import org.exolab.castor.mapping.Mapping;
12 import org.exolab.castor.mapping.MappingException;
13 import org.exolab.castor.xml.Unmarshaller;
14 import org.exolab.castor.xml.Marshaller;
15 import org.exolab.castor.xml.MarshalException;
16 import org.exolab.castor.xml.ValidationException;
17 import org.exoplatform.services.xml.querying.Statement;
18 import org.exoplatform.services.xml.querying.impl.xtas.Query;
19 import org.exoplatform.services.xml.querying.impl.xtas.QueryType;
20 import org.exoplatform.services.xml.querying.impl.xtas.SimpleStatement;
21 import org.exoplatform.services.xml.querying.impl.xtas.UniFormConverter;
22 import org.exoplatform.services.xml.querying.impl.xtas.UniFormTree;
23 import org.exoplatform.services.xml.querying.impl.xtas.UniFormTreeFragment;
24 import org.exoplatform.services.xml.querying.impl.xtas.WellFormedUniFormTree;
25 import org.exoplatform.services.xml.querying.impl.xtas.object.ObjectMarshaller;
26 import org.exoplatform.services.xml.querying.impl.xtas.xml.Utils;
27 import org.exoplatform.services.xml.querying.object.ObjectMappingException;
28 import org.exoplatform.services.xml.querying.object.ObjectMarshalException;
29
30 import org.xml.sax.InputSource JavaDoc;
31 import org.w3c.dom.Node JavaDoc;
32 import org.w3c.dom.Document JavaDoc;
33 import java.util.Collection JavaDoc;
34 import java.util.Iterator JavaDoc;
35
36
37 /**
38  * Exolab's Castor XML implementation of ObjectMarshaller
39  * based on Castor mapping (custom mapping)
40  * @version $Id: CastorMappedMarshaller.java 566 2005-01-25 12:50:49Z kravchuk $
41  */

42 public class CastorMappedMarshaller implements ObjectMarshaller {
43
44     private Mapping mapping;
45
46     /** @link dependency
47      * @stereotype use*/

48     /*# SyntheticalRoot lnkSyntheticalRoot; */
49
50     public CastorMappedMarshaller()
51     {
52     }
53
54     /**
55      * Loads mapping
56      * Castor Mapping supports 3 formats of incoming data:
57      * sax.InputSource, String (with URL) and URL
58      * contains mapping XML
59      * Every castor based custom mapping has structure like:
60      * <p>&lt;mapping><br>
61      * &lt;!-- Standard part for root xtas.object.plugins.SyntheticalRoot class ! --><br>
62      * &lt;class name="xtas.object.plugins.SyntheticalRoot"><br>
63      * &lt;map-to xml="synthetical-root"/><br>
64      * &lt;field name="result"<br>
65      * collection="collection"><br>
66      * &lt;!-- Put your mapping for the top-level custom beans here ! --><br>
67      * &lt;bind-xml name=&quot;<b>top-level-custom-bean-name</b>&quot;/&gt;<br>
68      * &lt;!-- --><br>
69      * &lt;/field><br>
70      * &lt;/class><br>
71      * &lt;!-- Put your beans' descriptions here --><br>
72      * &lt;class name=&quot;<b>custom-package.CustomBeanName</b>&quot;&gt;<br>
73      * &lt;map-to xml=&quot;<b>top-level- custom-bean-name</b>&quot;/&gt;<br>
74      * &lt;field name=&quot;<b>field-name</b>&quot; type=&quot;<b>field-type</b>&quot; ><br>
75      * &lt;bind-xml name=&quot;<b>field-name</b>&quot; node=&quot;<b>attribute|element</b>&quot;/&gt;<br>
76      * &lt;/field><br>
77      * ....<br>
78      * &lt;/class><br>
79      * &lt;/mapping><br>
80      * See Castor-XML documentation for details.
81      */

82     public void loadMapping(Object JavaDoc source) throws ObjectMappingException
83     {
84
85        this.mapping = new Mapping( this.getClass().getClassLoader() );
86
87        InputSource JavaDoc src;
88
89        try {
90            if( source instanceof InputSource JavaDoc ) {
91                 src = (InputSource JavaDoc) source;
92            }
93            else if( source instanceof String JavaDoc ) {
94                 src = new InputSource JavaDoc( (String JavaDoc) source );
95            }
96            else if( source instanceof URL JavaDoc ) {
97                 src = new InputSource JavaDoc( ((URL JavaDoc) source).toString() );
98            }
99            else
100              throw new ObjectMappingException("ObjectMappingException: Data Source ("+source.getClass().getName()+") for mapping is invalid !");
101
102            mapping.loadMapping( createMapping(src) );
103
104         } catch (IOException JavaDoc e) {
105
106             throw new ObjectMappingException("ObjectMappingException (I/O) thrown! Reason: "+e.getMessage());
107
108         } catch (MappingException e) {
109
110             throw new ObjectMappingException("ObjectMappingException thrown! Reason: "+e.getMessage());
111
112         }
113
114     }
115
116     /**
117      * does Castor specific marshalling
118      */

119     public Document JavaDoc marshal(Object JavaDoc obj) throws ObjectMarshalException, ObjectMappingException
120     {
121
122        if( mapping == null )
123             throw new ObjectMappingException("ObjectMarshaller's mapping can not be NULL. Call loadMapping() first ! ");
124
125        try {
126
127             Document JavaDoc doc = Utils.createDocument();
128             Marshaller marshaller = new Marshaller(doc);
129             marshaller.setMapping( mapping );
130             marshaller.marshal(obj);
131             return doc;
132
133         } catch (Exception JavaDoc e) {
134
135             throw new ObjectMarshalException("ObjectMarshalException occured! Reason: "+e.getMessage());
136
137         }
138
139     }
140
141     /**
142      * Does Castor specific unmarshalling
143      */

144     public Collection JavaDoc unmarshal(Document JavaDoc source) throws ObjectMarshalException, ObjectMappingException
145     {
146         if( mapping == null )
147             throw new ObjectMappingException("ObjectMarshaller's mapping can not be NULL. Call loadMapping() first ! ");
148
149         try {
150
151              Unmarshaller unmar = new Unmarshaller(mapping);
152              SyntheticalRoot oo = (SyntheticalRoot) unmar.unmarshal( source );
153              return oo.getObjects();
154
155         } catch (MappingException e) {
156
157             throw new ObjectMappingException(e.getMessage());
158        } catch (MarshalException e) {
159
160             throw new ObjectMarshalException(e.getMessage());
161        } catch (ValidationException e) {
162
163             throw new ObjectMarshalException(e.getMessage());
164        }
165
166
167     }
168
169     private InputSource JavaDoc createMapping(InputSource JavaDoc source) throws ObjectMappingException
170     {
171
172      try {
173
174         WellFormedUniFormTree mapTree = new WellFormedUniFormTree ();
175         mapTree.init( source );
176
177         // Get Low-lewel class (will be only one!)
178
Query q1 = new Query(SimpleStatement.select(
179               "/mapping/class/map-to"
180                  ,source.getSystemId() ));
181         q1.execute();
182         UniFormTreeFragment f = UniFormConverter.toFragment((UniFormTree)q1.getResult());
183         Iterator JavaDoc elements = f.getAsCollection(CastorMapToElement.class).iterator();
184
185         // Prepare SyntheticalRoot to add to the Mapping
186
String JavaDoc str =
187                "<class name=\"org.exoplatform.services.xml.querying.impl.xtas.object.plugins.SyntheticalRoot\"><map-to xml=\"synthetical-root\"/><field name=\"objects\" collection=\"collection\">";
188         String JavaDoc name = ((CastorMapToElement)elements.next()).getXml();
189         str += "<bind-xml name=\""+name+"\"/>";
190         str+="</field></class>";
191
192         // Add SyntheticalRoot collection
193
Statement add = new SimpleStatement( QueryType.APPEND, "mapping/class[last()]", str );
194         Query q = new Query(add);
195         q.setInputStream( mapTree.getAsInputStream() );
196         q.execute();
197
198         return new InputSource JavaDoc( q.getResult().getAsInputStream() );
199
200      } catch (Exception JavaDoc e) {
201
202         throw new ObjectMappingException("Exception while create mapping thrown! Reason: "+e.getMessage());
203
204      }
205    }
206
207 }
208
Popular Tags