1 package org.exoplatform.services.xml.querying.impl.xtas; 2 3 import java.io.ByteArrayOutputStream ; 4 import java.io.InputStream ; 5 import java.io.ByteArrayInputStream ; 6 7 import org.xml.sax.XMLReader ; 8 import org.xml.sax.InputSource ; 9 import org.xml.sax.ContentHandler ; 10 import org.xml.sax.SAXException ; 11 12 import javax.xml.transform.sax.TransformerHandler ; 13 import javax.xml.transform.sax.SAXTransformerFactory ; 14 import javax.xml.transform.stream.StreamResult ; 15 import javax.xml.transform.stream.StreamSource ; 16 import javax.xml.transform.dom.DOMSource ; 17 import javax.xml.transform.dom.DOMResult ; 18 import javax.xml.transform.sax.SAXResult ; 19 import javax.xml.transform.Result ; 20 import javax.xml.transform.TransformerException ; 21 import javax.xml.transform.Transformer ; 22 import javax.xml.transform.Source ; 23 24 import javax.xml.parsers.SAXParserFactory ; 25 import javax.xml.parsers.ParserConfigurationException ; 26 27 import org.exoplatform.services.xml.querying.UniFormTransformationException; 28 import org.exoplatform.services.xml.querying.XMLWellFormedData; 29 import org.exoplatform.services.xml.querying.impl.xtas.object.MappingType; 30 import org.exoplatform.services.xml.querying.impl.xtas.object.ObjectMarshaller; 31 import org.exoplatform.services.xml.querying.impl.xtas.object.ObjectMarshallerFactory; 32 import org.exoplatform.services.xml.querying.impl.xtas.xml.DtdTracer; 33 import org.exoplatform.services.xml.querying.impl.xtas.xml.Utils; 34 import org.exoplatform.services.xml.querying.object.MarshallerCreateException; 35 import org.exoplatform.services.xml.querying.object.ObjectMappingException; 36 import org.exoplatform.services.xml.querying.object.ObjectMarshalException; 37 import org.w3c.dom.Node ; 38 import org.w3c.dom.NodeList ; 39 import org.w3c.dom.Document ; 40 41 42 48 public class WellFormedUniFormTree extends UniFormTree implements XMLWellFormedData { 49 50 private DtdTracer validationHandler; 51 protected XMLReader reader=null; 52 protected TransformerHandler handler; 53 protected static SAXTransformerFactory sFactory; 54 protected static SAXParserFactory parserFactory; 55 56 57 static { 58 59 try { 60 61 parserFactory = SAXParserFactory.newInstance(); 62 sFactory = (SAXTransformerFactory ) tFactory; 63 64 } catch (Exception e) { 65 System.out.println("Can not INSTANTIATE WellFormedUniFormTree Object Reason: " + e); 67 } 68 69 } 70 71 72 75 public WellFormedUniFormTree() throws UniFormTransformationException 76 { 77 validationHandler = new DtdTracer(); 78 79 try { 80 81 87 88 handler = sFactory.newTransformerHandler(); 89 handler.setResult(new StreamResult ( this.thisStream )); 90 91 handler.getTransformer().setOutputProperty( 92 javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes"); 93 94 reader = parserFactory.newSAXParser().getXMLReader(); 95 96 reader.setContentHandler(handler); 97 98 setValidate(false); 99 100 reader.setProperty("http://xml.org/sax/properties/lexical-handler", 101 validationHandler); 102 reader.setProperty("http://xml.org/sax/properties/declaration-handler", 103 validationHandler); 104 105 106 } catch (Exception e) { 107 throw new UniFormTransformationException (" WellFormedUniFormTree(InputStream stream) ERROR: Reason: " + e); 109 } 110 111 112 } 113 114 117 public void init(InputSource src) throws UniFormTransformationException 118 { 119 120 try { 121 122 reader.parse( src ); 123 124 } catch (Exception e) { 125 126 throw new UniFormTransformationException("UniformTree.Init(inputStream): Can not parse InputSource Reason: " + e); 127 } 128 } 129 130 133 public void init(Node node) throws UniFormTransformationException 134 { 135 136 try { 137 138 thisStream = new ByteArrayOutputStream (); 140 141 transformer.transform( new DOMSource (node) , 142 new StreamResult ( thisStream ) ); 143 144 } catch (Exception e) { 146 147 throw new UniFormTransformationException("UniformTree.Init(Node): Can not Transform Reason: " + e); 148 149 } 150 } 151 152 156 public void init(Object obj) throws ObjectMarshalException, MarshallerCreateException, ObjectMappingException 157 { 158 if ( (obj instanceof InputStream ) || (obj instanceof NodeList ) || (obj instanceof Document ) || (obj instanceof InputSource ) ) 159 throw new ObjectMappingException( "DEV-ERROR! init(Object) Parameter MUST NOT BE "+ 160 obj.getClass().getName()+" TYPE!"); 161 init( obj.getClass(), obj ); 162 } 163 164 167 public void init(Object mapping, Object obj) throws ObjectMarshalException, MarshallerCreateException, ObjectMappingException 168 { 169 thisStream = new ByteArrayOutputStream (); 171 172 173 int mappingType = MappingType.INTERNAL; 174 if( !(mapping instanceof Class ) ) 175 mappingType = MappingType.CUSTOM; 176 177 ObjectMarshaller marshaller = ObjectMarshallerFactory.getInstance().getMarshaller( mappingType ); 178 marshaller.loadMapping( mapping ); 179 180 try { 181 182 Document doc = Utils.createDocument(); 183 doc = marshaller.marshal( obj ); 184 185 transformer.transform( new DOMSource (doc) , 186 new StreamResult ( thisStream ) ); 187 } catch (Exception e) { 188 189 throw new ObjectMarshalException("UniFormTree.init(Object mapping, Object obj) Exception: " + e); 190 } 191 } 192 193 198 public void initRoot(String nodeName) throws UniFormTransformationException 199 { 200 201 try { 202 203 init ( new InputSource (new ByteArrayInputStream (("<"+nodeName+"> </"+nodeName+">").getBytes() ) ) ); 204 205 } catch (Exception e) { 206 207 throw new UniFormTransformationException("UniformTree.Init(inputStream): Can not parse InputSource Reason: " + e); 208 } 209 } 210 211 214 public byte[] getAsByteArray() 215 { 216 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 217 StreamResult sr = new StreamResult (bos); 218 convert( sr ); 219 return bos.toByteArray(); 220 } 221 222 225 public Node getAsDOM() 226 { 227 228 Document tmpDoc = null; 229 try { 230 231 tmpDoc = Utils.createDocument(); 232 233 } catch (ParserConfigurationException e) { 234 System.out.println("UniFormTree.getAsDOM(): Can not create DOM Reason: " + e); 236 } 237 238 convert( new DOMResult (tmpDoc) ); 239 return tmpDoc; 240 } 241 242 245 public void processAsSAX(ContentHandler handler) throws SAXException 246 { 247 try { 248 convert( new SAXResult (handler), true ); 249 250 } catch (Exception e) { 251 252 throw new SAXException ("WellFormedUniFormTree.processAsSAX() failed. Reason: "+e); 253 } 254 } 255 256 259 public void transformWithXSL(Result result, Source xslTemplate) throws TransformerException 260 { 261 262 try { 263 if(xslTemplate == null) 264 convert( result, true ); 265 else { 266 Transformer trans; 267 trans = tFactory.newTransformer(xslTemplate); 268 trans.transform( new StreamSource ( new ByteArrayInputStream ( 269 thisStream.toByteArray() ) ), result ); 270 } 271 272 } catch (Exception e) { 273 274 throw new TransformerException ("WellFormedUniFormTree.transformWithXSL() failed. Reason: "+e); 275 } 276 } 277 278 281 public void setOmitXmlDeclaration(boolean yesNo) 282 { 283 transformer.setOutputProperty( 284 javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, (yesNo?"yes":"no")); 285 } 286 287 290 public void setIndentOutput(boolean yesNo) 291 { 292 transformer.setOutputProperty( 293 javax.xml.transform.OutputKeys.INDENT, (yesNo?"yes":"no")); 294 } 295 296 299 public void setDTDSystemId(String systemId) 300 { 301 transformer.setOutputProperty( 302 javax.xml.transform.OutputKeys.DOCTYPE_SYSTEM, systemId); 303 } 304 305 308 public void setDTDPublicId(String publicId) 309 { 310 transformer.setOutputProperty( 311 javax.xml.transform.OutputKeys.DOCTYPE_PUBLIC, publicId); 312 } 313 314 public DtdTracer getValidationHandler() 315 { 316 return validationHandler; 317 } 318 319 322 public void setValidate(boolean yesNo) 323 { 324 try { 325 reader.setFeature("http://xml.org/sax/features/validation", yesNo); 326 327 } catch (Exception e) { 328 System.out.println("WellFormedUniFormTree.setValidate(): " + e); 330 } 331 } 332 } 333 | Popular Tags |