1 package org.exoplatform.services.xml.querying.impl.xtas; 2 3 import java.io.InputStream ; 4 import java.io.ByteArrayOutputStream ; 5 import java.io.SequenceInputStream ; 6 import java.io.ByteArrayInputStream ; 7 import java.util.Collection ; 8 9 import javax.xml.transform.stream.StreamSource ; 10 import javax.xml.transform.stream.StreamResult ; 11 import javax.xml.transform.dom.DOMSource ; 12 import javax.xml.transform.dom.DOMResult ; 13 import javax.xml.parsers.ParserConfigurationException ; 14 15 import org.exoplatform.services.xml.querying.UniFormTransformationException; 16 import org.exoplatform.services.xml.querying.XMLFragmentData; 17 import org.exoplatform.services.xml.querying.impl.xtas.object.MappingType; 18 import org.exoplatform.services.xml.querying.impl.xtas.object.ObjectMarshaller; 19 import org.exoplatform.services.xml.querying.impl.xtas.object.ObjectMarshallerFactory; 20 import org.exoplatform.services.xml.querying.impl.xtas.xml.Utils; 21 import org.exoplatform.services.xml.querying.object.MarshallerCreateException; 22 import org.exoplatform.services.xml.querying.object.ObjectMappingException; 23 import org.exoplatform.services.xml.querying.object.ObjectMarshalException; 24 import org.w3c.dom.NodeList ; 25 import org.w3c.dom.Document ; 26 27 28 34 35 public class UniFormTreeFragment extends UniFormTree implements XMLFragmentData { 36 37 38 public UniFormTreeFragment() { 40 } 41 42 45 public void init(InputStream stream) throws UniFormTransformationException 46 { 47 48 try { 50 thisStream = new ByteArrayOutputStream (); 51 } catch (Exception e) { 52 throw new UniFormTransformationException("UniformTree.Init(InputStream): Can not Transform Reason: " + e); 53 } 54 55 SequenceInputStream s = new SequenceInputStream ( 56 new ByteArrayInputStream ( "<synthetical-root>".getBytes() ), stream ); 57 s = new SequenceInputStream ( 58 s, new ByteArrayInputStream ( "</synthetical-root>".getBytes() ) ); 59 60 try { 61 62 transformer.transform( new StreamSource (s), 63 new StreamResult ( thisStream ) ); 64 65 } catch (Exception e) { 66 67 throw new UniFormTransformationException("UniformTree.Init(inputStream): Can not parse InputStream Reason: " + e); 68 69 } 70 71 } 72 73 77 78 public void init(NodeList list) throws UniFormTransformationException 79 { 80 81 try { 82 83 thisStream.close(); 84 thisStream.write("<synthetical-root>".getBytes(),0, "<synthetical-root>".length() ); 85 86 ByteArrayOutputStream tmpStream = null; 87 88 for(int i=0; i< list.getLength(); i++) { 89 90 transformer.transform( new DOMSource (list.item(i)) , 91 new StreamResult ( tmpStream ) ); 92 93 94 if (tmpStream!=null) 95 tmpStream.writeTo(thisStream); 96 97 } 98 99 thisStream.write("</synthetical-root>".getBytes(),0, "</synthetical-root>".length() ); 100 101 } catch (Exception e) { 103 104 105 throw new UniFormTransformationException("UniformTree.Init(NodeList): Can not Transform Reason: " + e); 106 107 } 108 } 109 110 111 public byte[] getAsByteArray() { 113 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 114 StreamResult sr = new StreamResult (bos); 115 convert( sr ); 116 117 byte[] buffer = bos.toByteArray(); 118 if (buffer.length <= "<synthetical-root/>".length() ) 119 return "".getBytes(); 120 String inStr = new String (buffer); 121 String result = inStr.substring( inStr.indexOf("<synthetical-root>")+ 122 "<synthetical-root>".length(), 123 inStr.indexOf("</synthetical-root>") ); 124 125 return result.getBytes(); 126 } 127 128 132 133 public Collection getAsCollection(Class forClass) throws ObjectMarshalException 134 { 135 136 try { 137 138 ObjectMarshaller unmarshaller = ObjectMarshallerFactory.getInstance().getMarshaller( MappingType.INTERNAL ); 139 unmarshaller.loadMapping( forClass ); 140 141 Document doc = Utils.createDocument(); 142 convert( new DOMResult ( doc ) ); 143 144 return unmarshaller.unmarshal( doc ); 145 146 } catch (ObjectMappingException e) { 147 148 throw new ObjectMarshalException("Mappling exception thrown ! Reason: " + e); 149 150 } catch (MarshallerCreateException e) { 151 152 throw new ObjectMarshalException("Can not create marshaller ! Reason: " + e); 153 154 } catch (Exception e) { 155 156 throw new ObjectMarshalException("UniFormTree.getAsCollection(Class forClass) Exception: " + e); 157 158 } 159 160 } 161 162 167 168 public Collection getAsCollection(Object mappingSource) throws ObjectMarshalException { 170 try { 171 172 ObjectMarshaller unmarshaller = ObjectMarshallerFactory.getInstance().getMarshaller( MappingType.CUSTOM ); 173 unmarshaller.loadMapping( mappingSource ); 174 175 Document doc = Utils.createDocument(); 176 177 convert( new DOMResult ( doc ) ); 178 179 return unmarshaller.unmarshal( doc ); 180 181 } catch (ObjectMappingException e) { 182 183 throw new ObjectMarshalException("Mappling exception thrown ! Reason: " + e); 184 185 } catch (MarshallerCreateException e) { 186 187 throw new ObjectMarshalException("Can not create marshaller ! Reason: " + e); 188 189 } catch (Exception e) { 190 191 throw new ObjectMarshalException("UniFormTree.getAsCollection(Object mappingSource) Exception: " + e); 192 193 } 194 } 195 196 public NodeList getAsNodeList() { 197 198 try { 199 200 Document doc = Utils.createDocument(); 201 convert( new DOMResult ( doc ) ); 202 203 return doc.getDocumentElement().getChildNodes(); 204 205 } catch (ParserConfigurationException e) { 206 throw new RuntimeException ("UniFormTree.getAsNodeList(): Can not create DOM Reason: " + e); 207 } 208 209 } 210 211 212 215 public boolean isEmpty() 216 { 217 return new String (getAsByteArray()).trim().length() == 0; 218 } 219 220 221 public String getRawContent() 222 { 223 return thisStream.toString(); 224 } 225 226 227 } 228 229 230 | Popular Tags |