1 package org.exoplatform.services.xml.querying.impl.xtas.object.plugins; 2 3 import org.w3c.dom.Document ; 4 import org.w3c.dom.Node ; 5 import org.w3c.dom.NodeList ; 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 ; 18 import java.util.ArrayList ; 19 20 25 public class CastorClassMarshaller implements ObjectMarshaller { 26 27 private Class mapping; 28 29 32 public void loadMapping(Object source) throws ObjectMappingException 33 { 34 if( source instanceof Class ) 35 this.mapping = (Class ) source; 37 else 38 throw new ObjectMappingException("ObjectMappingException: Data Source ("+source.getClass().getName()+") for mapping is invalid !"); 39 40 } 41 42 45 public Document marshal(Object 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 doc = Utils.createDocument(); 54 Marshaller.marshal( obj, (Node ) doc); 55 56 return doc; 57 58 } catch (Exception e) { 59 throw new ObjectMarshalException("ObjectMarshalException occured! Reason: "+e.getMessage()); 61 62 } 63 64 } 65 66 69 public Collection unmarshal(Document 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 list = new ArrayList (); 77 78 80 NodeList nl = source.getDocumentElement().getChildNodes(); 81 for (int i=0 ;i< nl.getLength(); i++) { 82 Node 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 |