KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > xml > querying > impl > xtas > UniFormTree


1 package org.exoplatform.services.xml.querying.impl.xtas;
2
3 import java.io.ByteArrayOutputStream JavaDoc;
4 import java.io.InputStream JavaDoc;
5 import java.io.ByteArrayInputStream JavaDoc;
6
7 import javax.xml.transform.Transformer JavaDoc;
8 import javax.xml.transform.TransformerFactory JavaDoc;
9 import javax.xml.transform.Result JavaDoc;
10 import javax.xml.transform.stream.StreamSource JavaDoc;
11 import org.exoplatform.services.xml.querying.XMLData;
12
13 /**
14  * Encapsulates multi-formatting tree-like (XML) object presentation
15  * for XTAS' manipulation purposes
16  */

17 abstract public class UniFormTree implements XMLData {
18   
19   protected ByteArrayOutputStream JavaDoc thisStream;
20   protected Transformer JavaDoc transformer;
21   protected static TransformerFactory JavaDoc tFactory;
22   
23   static {
24     
25     try {
26       
27       tFactory = TransformerFactory.newInstance();
28       // TransformerException, TransformerConfigurationException
29
} catch (Exception JavaDoc e) {
30       // @todo Log system!
31
System.out.println("Can not INSTANTIATE UniFormTree Object Reason: " + e);
32     }
33     
34   }
35   
36   /**
37    * Default constructor
38    */

39   public UniFormTree()
40   {
41     thisStream = new ByteArrayOutputStream JavaDoc();
42     try {
43       
44       transformer = tFactory.newTransformer();
45       transformer.setOutputProperty(
46           javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");
47       
48       //FactoryConfigurationError,SAXException
49
} catch (Exception JavaDoc e) {
50       
51       // @todo Log system!
52
System.out.println("UniFormTree(): Can not create an Instance Reason: " + e);
53       // throw new UniFormTransformationException("Can not parse InputStream Reason: " + e);
54
}
55     
56   }
57   
58   /**
59    * Must be realized in concrete subclass
60    */

61   public abstract byte[] getAsByteArray();
62   
63   
64   public InputStream JavaDoc getAsInputStream()
65   {
66     return new ByteArrayInputStream JavaDoc ( getAsByteArray() );
67   }
68   
69   public String JavaDoc getAsString()
70   {
71     return new String JavaDoc (getAsByteArray());
72   }
73   
74   /**
75    * Is this object empty?
76    * **/

77   public boolean isEmpty()
78   {
79     return (thisStream.size() == 0);
80   }
81   
82   /**
83    * Cleans this object
84    */

85   public void close()
86   {
87     thisStream = new ByteArrayOutputStream JavaDoc();
88   }
89   
90   public String JavaDoc toString()
91   {
92     if (isEmpty())
93       return "";
94     return getAsString().trim();
95   }
96   
97   protected void convert(Result JavaDoc result)
98   {
99     
100     try {
101       
102       convert(result, false);
103       
104     } catch (Exception JavaDoc e) { }
105     
106   }
107   
108   
109   /**
110    * The main method for object transformation
111    * to some output format (for getAs... methods) .
112    */

113   protected void convert(Result JavaDoc result, boolean withException) throws Exception JavaDoc
114   {
115     
116     // if( thisStream.toByteArray().length == 0 )
117
// System.out.println("UniFormTree.convert() MUST BE INITIALIZED: EMPTY stream:"+ result.getClass().getName()+
118
// " Subclass: "+getClass().getName() );
119

120     
121     try {
122       
123       transformer.transform( new StreamSource JavaDoc( new ByteArrayInputStream JavaDoc(
124           thisStream.toByteArray() ) ), result );
125       // TransformerException, TransformerConfigurationException
126
} catch (Exception JavaDoc e) {
127       if(withException) throw e;
128       // @todo Log system!
129
System.out.println("UniFormTree.convert(): Can not convert to "+ result.getClass().getName()+" Reason: " + e +
130           " XML: <"+new String JavaDoc(thisStream.toByteArray())+"> Subclass: "+getClass().getName() );
131       e.printStackTrace();
132     }
133   }
134 }
135
Popular Tags