KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.exoplatform.services.xml.querying.impl.xtas;
2
3 import java.io.InputStream JavaDoc;
4 import java.io.ByteArrayOutputStream JavaDoc;
5 import java.io.SequenceInputStream JavaDoc;
6 import java.io.ByteArrayInputStream JavaDoc;
7 import java.util.Collection JavaDoc;
8
9 import javax.xml.transform.stream.StreamSource JavaDoc;
10 import javax.xml.transform.stream.StreamResult JavaDoc;
11 import javax.xml.transform.dom.DOMSource JavaDoc;
12 import javax.xml.transform.dom.DOMResult JavaDoc;
13 import javax.xml.parsers.ParserConfigurationException JavaDoc;
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 JavaDoc;
25 import org.w3c.dom.Document JavaDoc;
26
27
28 /**
29  * Conceptually it is a DocumentFragment (in DOM terms)
30  * May be realised as Result Tree Fragment (in XSLT terms)
31  *
32  * @version $Id: UniFormTreeFragment.java 566 2005-01-25 12:50:49Z kravchuk $
33  */

34
35 public class UniFormTreeFragment extends UniFormTree implements XMLFragmentData {
36
37
38     public UniFormTreeFragment() //throws UniFormTransformationException
39
{
40     }
41
42     /**
43      * Initializes this with the io.InputStream
44      */

45     public void init(InputStream JavaDoc stream) throws UniFormTransformationException
46     {
47
48         // Reinitialize thisStream
49
try {
50              thisStream = new ByteArrayOutputStream JavaDoc();
51         } catch (Exception JavaDoc e) {
52             throw new UniFormTransformationException("UniformTree.Init(InputStream): Can not Transform Reason: " + e);
53         }
54
55         SequenceInputStream JavaDoc s = new SequenceInputStream JavaDoc(
56              new ByteArrayInputStream JavaDoc( "<synthetical-root>".getBytes() ), stream );
57         s = new SequenceInputStream JavaDoc(
58              s, new ByteArrayInputStream JavaDoc( "</synthetical-root>".getBytes() ) );
59
60         try {
61
62                transformer.transform( new StreamSource JavaDoc(s),
63                                       new StreamResult JavaDoc( thisStream ) );
64
65         } catch (Exception JavaDoc e) {
66
67             throw new UniFormTransformationException("UniformTree.Init(inputStream): Can not parse InputStream Reason: " + e);
68
69         }
70
71     }
72
73     /**
74      * Initializes this with the DOM NodeList
75      * DOES NOT WORK properly!
76      */

77
78     public void init(NodeList JavaDoc list) throws UniFormTransformationException
79     {
80
81         try {
82
83             thisStream.close();
84             thisStream.write("<synthetical-root>".getBytes(),0, "<synthetical-root>".length() );
85
86             ByteArrayOutputStream JavaDoc tmpStream = null;
87
88             for(int i=0; i< list.getLength(); i++) {
89
90                  transformer.transform( new DOMSource JavaDoc(list.item(i)) ,
91                                         new StreamResult JavaDoc( 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         //FactoryConfigurationError,FactoryConfigurationError,SAXException
102
} catch (Exception JavaDoc e) {
103
104
105             throw new UniFormTransformationException("UniformTree.Init(NodeList): Can not Transform Reason: " + e);
106
107         }
108     }
109
110
111     public byte[] getAsByteArray() //throws UniFormTransformationException
112
{
113        ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
114        StreamResult JavaDoc sr = new StreamResult JavaDoc(bos);
115        convert( sr );
116
117        byte[] buffer = bos.toByteArray();
118        if (buffer.length <= "<synthetical-root/>".length() )
119            return "".getBytes();
120        String JavaDoc inStr = new String JavaDoc(buffer);
121        String JavaDoc result = inStr.substring( inStr.indexOf("<synthetical-root>")+
122                        "<synthetical-root>".length(),
123                        inStr.indexOf("</synthetical-root>") );
124
125        return result.getBytes();
126     }
127
128     /**
129      * Returns nested objects As Collection
130      * after marshalling with implicit (Class reflection based) mapping
131      */

132
133     public Collection JavaDoc getAsCollection(Class JavaDoc forClass) throws ObjectMarshalException
134     {
135
136        try {
137
138            ObjectMarshaller unmarshaller = ObjectMarshallerFactory.getInstance().getMarshaller( MappingType.INTERNAL );
139            unmarshaller.loadMapping( forClass );
140
141            Document JavaDoc doc = Utils.createDocument();
142            convert( new DOMResult JavaDoc( 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 JavaDoc e) {
155
156             throw new ObjectMarshalException("UniFormTree.getAsCollection(Class forClass) Exception: " + e);
157
158         }
159
160     }
161
162     /**
163      * Returns mapping for the own dom-tree
164      * with SyntheticalRoot as root and
165      * returns nested objects As Abstract collection
166      */

167
168     public Collection JavaDoc getAsCollection(Object JavaDoc mappingSource) throws ObjectMarshalException //, UniFormTransformationException
169
{
170         try {
171
172             ObjectMarshaller unmarshaller = ObjectMarshallerFactory.getInstance().getMarshaller( MappingType.CUSTOM );
173             unmarshaller.loadMapping( mappingSource );
174
175             Document JavaDoc doc = Utils.createDocument();
176
177             convert( new DOMResult JavaDoc( 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 JavaDoc e) {
190
191             throw new ObjectMarshalException("UniFormTree.getAsCollection(Object mappingSource) Exception: " + e);
192
193         }
194     }
195
196     public NodeList JavaDoc getAsNodeList() {
197
198        try {
199
200            Document JavaDoc doc = Utils.createDocument();
201            convert( new DOMResult JavaDoc( doc ) );
202
203            return doc.getDocumentElement().getChildNodes();
204
205         } catch (ParserConfigurationException JavaDoc e) {
206             throw new RuntimeException JavaDoc("UniFormTree.getAsNodeList(): Can not create DOM Reason: " + e);
207         }
208
209     }
210
211
212     /**
213      * Is this object empty?
214      * **/

215     public boolean isEmpty()
216     {
217         return new String JavaDoc(getAsByteArray()).trim().length() == 0;
218     }
219
220     /* Debugging method... */
221     public String JavaDoc getRawContent()
222     {
223         return thisStream.toString();
224     }
225
226
227 }
228
229
230
Popular Tags