KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.exoplatform.services.xml.querying.impl.xtas.object.plugins;
2
3 import org.w3c.dom.Document JavaDoc;
4 import org.w3c.dom.Node JavaDoc;
5 import org.w3c.dom.NodeList JavaDoc;
6
7 import org.exolab.castor.xml.Unmarshaller;
8 import org.exolab.castor.xml.Marshaller;
9 import org.exolab.castor.xml.MarshalException;
10 import org.exolab.castor.xml.ValidationException;
11 import org.exoplatform.services.xml.querying.impl.xtas.object.ObjectMarshaller;
12 import org.exoplatform.services.xml.querying.impl.xtas.xml.Utils;
13 import org.exoplatform.services.xml.querying.object.ObjectMappingException;
14 import org.exoplatform.services.xml.querying.object.ObjectMarshalException;
15
16
17 import java.util.Collection JavaDoc;
18 import java.util.ArrayList JavaDoc;
19
20 /**
21  * Marshaller with mapping based on java class intercaption
22  * (internal mapping)
23  * @version $Id: CastorClassMarshaller.java 566 2005-01-25 12:50:49Z kravchuk $
24  */

25 public class CastorClassMarshaller implements ObjectMarshaller {
26
27     private Class JavaDoc mapping;
28
29     /**
30      * Loads Class that will be introspected for mapping
31      */

32     public void loadMapping(Object JavaDoc source) throws ObjectMappingException
33     {
34        if( source instanceof Class JavaDoc )
35           // Explicit mapping
36
this.mapping = (Class JavaDoc) source;
37        else
38           throw new ObjectMappingException("ObjectMappingException: Data Source ("+source.getClass().getName()+") for mapping is invalid !");
39
40     }
41
42     /**
43      * Does Castor specific marshalling
44      */

45     public Document JavaDoc marshal(Object JavaDoc obj) throws ObjectMarshalException, ObjectMappingException
46     {
47
48        if( mapping == null )
49             throw new ObjectMappingException("ObjectMarshaller's mapping can not be NULL. Call loadMapping() first ! ");
50
51        try {
52
53             Document JavaDoc doc = Utils.createDocument();
54             Marshaller.marshal( obj, (Node JavaDoc) doc);
55
56             return doc;
57
58         } catch (Exception JavaDoc e) {
59 // e.printStackTrace();
60
throw new ObjectMarshalException("ObjectMarshalException occured! Reason: "+e.getMessage());
61
62         }
63
64     }
65
66     /**
67      * Does Castor specific unmarshalling
68      */

69     public Collection JavaDoc unmarshal(Document JavaDoc source) throws ObjectMarshalException, ObjectMappingException
70     {
71         if( mapping == null )
72             throw new ObjectMappingException("ObjectMarshaller's mapping can not be NULL. Call loadMapping() first ! ");
73
74         try {
75
76              ArrayList JavaDoc list = new ArrayList JavaDoc();
77
78             // Get List of ELEMENT! Nodes casted to forClass
79

80             NodeList JavaDoc nl = source.getDocumentElement().getChildNodes();
81             for (int i=0 ;i< nl.getLength(); i++) {
82                Node JavaDoc child = nl.item(i);
83                if( child.getNodeType() == Node.ELEMENT_NODE )
84                    list.add(Unmarshaller.unmarshal( mapping, child ) );
85             }
86
87            return list;
88
89         } catch (MarshalException e) {
90
91             throw new ObjectMarshalException(e.getMessage());
92         } catch (ValidationException e) {
93
94             throw new ObjectMarshalException(e.getMessage());
95         }
96
97     }
98 }
99
Popular Tags