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 javax.xml.transform.Transformer ; 8 import javax.xml.transform.TransformerFactory ; 9 import javax.xml.transform.Result ; 10 import javax.xml.transform.stream.StreamSource ; 11 import org.exoplatform.services.xml.querying.XMLData; 12 13 17 abstract public class UniFormTree implements XMLData { 18 19 protected ByteArrayOutputStream thisStream; 20 protected Transformer transformer; 21 protected static TransformerFactory tFactory; 22 23 static { 24 25 try { 26 27 tFactory = TransformerFactory.newInstance(); 28 } catch (Exception e) { 30 System.out.println("Can not INSTANTIATE UniFormTree Object Reason: " + e); 32 } 33 34 } 35 36 39 public UniFormTree() 40 { 41 thisStream = new ByteArrayOutputStream (); 42 try { 43 44 transformer = tFactory.newTransformer(); 45 transformer.setOutputProperty( 46 javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes"); 47 48 } catch (Exception e) { 50 51 System.out.println("UniFormTree(): Can not create an Instance Reason: " + e); 53 } 55 56 } 57 58 61 public abstract byte[] getAsByteArray(); 62 63 64 public InputStream getAsInputStream() 65 { 66 return new ByteArrayInputStream ( getAsByteArray() ); 67 } 68 69 public String getAsString() 70 { 71 return new String (getAsByteArray()); 72 } 73 74 77 public boolean isEmpty() 78 { 79 return (thisStream.size() == 0); 80 } 81 82 85 public void close() 86 { 87 thisStream = new ByteArrayOutputStream (); 88 } 89 90 public String toString() 91 { 92 if (isEmpty()) 93 return ""; 94 return getAsString().trim(); 95 } 96 97 protected void convert(Result result) 98 { 99 100 try { 101 102 convert(result, false); 103 104 } catch (Exception e) { } 105 106 } 107 108 109 113 protected void convert(Result result, boolean withException) throws Exception 114 { 115 116 120 121 try { 122 123 transformer.transform( new StreamSource ( new ByteArrayInputStream ( 124 thisStream.toByteArray() ) ), result ); 125 } catch (Exception e) { 127 if(withException) throw e; 128 System.out.println("UniFormTree.convert(): Can not convert to "+ result.getClass().getName()+" Reason: " + e + 130 " XML: <"+new String (thisStream.toByteArray())+"> Subclass: "+getClass().getName() ); 131 e.printStackTrace(); 132 } 133 } 134 } 135 | Popular Tags |