1 package org.exoplatform.services.xml.querying.impl.xtas.object.plugins; 2 3 import java.io.InputStream ; 4 import java.io.OutputStream ; 5 import java.io.IOException ; 6 import java.io.ByteArrayOutputStream ; 7 import java.io.OutputStreamWriter ; 8 import java.io.InputStreamReader ; 9 import java.net.URL ; 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 ; 31 import org.w3c.dom.Node ; 32 import org.w3c.dom.Document ; 33 import java.util.Collection ; 34 import java.util.Iterator ; 35 36 37 42 public class CastorMappedMarshaller implements ObjectMarshaller { 43 44 private Mapping mapping; 45 46 48 49 50 public CastorMappedMarshaller() 51 { 52 } 53 54 82 public void loadMapping(Object source) throws ObjectMappingException 83 { 84 85 this.mapping = new Mapping( this.getClass().getClassLoader() ); 86 87 InputSource src; 88 89 try { 90 if( source instanceof InputSource ) { 91 src = (InputSource ) source; 92 } 93 else if( source instanceof String ) { 94 src = new InputSource ( (String ) source ); 95 } 96 else if( source instanceof URL ) { 97 src = new InputSource ( ((URL ) 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 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 119 public Document marshal(Object 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 doc = Utils.createDocument(); 128 Marshaller marshaller = new Marshaller(doc); 129 marshaller.setMapping( mapping ); 130 marshaller.marshal(obj); 131 return doc; 132 133 } catch (Exception e) { 134 135 throw new ObjectMarshalException("ObjectMarshalException occured! Reason: "+e.getMessage()); 136 137 } 138 139 } 140 141 144 public Collection unmarshal(Document 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 createMapping(InputSource source) throws ObjectMappingException 170 { 171 172 try { 173 174 WellFormedUniFormTree mapTree = new WellFormedUniFormTree (); 175 mapTree.init( source ); 176 177 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 elements = f.getAsCollection(CastorMapToElement.class).iterator(); 184 185 String 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 name = ((CastorMapToElement)elements.next()).getXml(); 189 str += "<bind-xml name=\""+name+"\"/>"; 190 str+="</field></class>"; 191 192 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 ( q.getResult().getAsInputStream() ); 199 200 } catch (Exception e) { 201 202 throw new ObjectMappingException("Exception while create mapping thrown! Reason: "+e.getMessage()); 203 204 } 205 } 206 207 } 208 | Popular Tags |