KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > om > impl > llom > OMTextImpl


1 /*
2  * Copyright 2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis2.om.impl.llom;
17
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.util.Date JavaDoc;
21 import java.util.Random JavaDoc;
22
23 import javax.activation.DataHandler JavaDoc;
24 import javax.xml.stream.XMLStreamException;
25 import javax.xml.stream.XMLStreamWriter;
26
27 import org.apache.axis2.attachments.Base64;
28 import org.apache.axis2.attachments.ByteArrayDataSource;
29 import org.apache.axis2.attachments.IOUtils;
30 import org.apache.axis2.om.OMAttribute;
31 import org.apache.axis2.om.OMConstants;
32 import org.apache.axis2.om.OMElement;
33 import org.apache.axis2.om.OMException;
34 import org.apache.axis2.om.OMNamespace;
35 import org.apache.axis2.om.OMNode;
36 import org.apache.axis2.om.OMOutput;
37 import org.apache.axis2.om.OMText;
38 import org.apache.axis2.om.OMXMLParserWrapper;
39 import org.apache.axis2.om.impl.llom.mtom.MTOMStAXSOAPModelBuilder;
40
41 /**
42  * @author <a HREF="mailto:thilina@opensource.lk">Thilina Gunarathne </a>
43  */

44 public class OMTextImpl extends OMNodeImpl implements OMText, OMConstants {
45
46     protected String JavaDoc value = null;
47
48     protected short textType = TEXT_NODE;
49
50     protected String JavaDoc mimeType;
51
52     protected boolean optimize = false;
53
54     protected boolean isBinary = false;
55     
56     private static Random JavaDoc rnd = new Random JavaDoc(new Date JavaDoc().getTime());
57
58     /**
59      * Field contentID for the mime part used when serialising Binary stuff as
60      * MTOM optimised
61      */

62     private String JavaDoc contentID = null;
63
64     /**
65      * Field dataHandler
66      */

67     private DataHandler JavaDoc dataHandler = null;
68
69     /**
70      * Field nameSpace used when serialising Binary stuff as MTOM optimised
71      */

72     protected OMNamespace ns = new OMNamespaceImpl(
73             "http://www.w3.org/2004/08/xop/Include", "xop");
74
75     /**
76      * Field localName used when serialising Binary stuff as MTOM optimised
77      */

78     protected String JavaDoc localName = "Include";
79
80     /**
81      * Field attributes used when serialising Binary stuff as MTOM optimised
82      */

83     protected OMAttribute attribute;
84
85     /**
86      * Constructor OMTextImpl
87      *
88      * @param s
89      */

90     public OMTextImpl(String JavaDoc s) {
91         this.value = s;
92     }
93
94     /**
95      * Constructor OMTextImpl
96      *
97      * @param parent
98      * @param text
99      */

100     public OMTextImpl(OMElement parent, String JavaDoc text) {
101         super(parent);
102         this.value = text;
103         done = true;
104     }
105
106     /**
107      * @param s -
108      * base64 encoded String representation of Binary
109      * @param mimeType
110      * of the Binary
111      */

112     public OMTextImpl(String JavaDoc s, String JavaDoc mimeType, boolean optimize) {
113         this(null,s,mimeType,optimize);
114     }
115
116     /**
117      * @param parent
118      * @param s -
119      * base64 encoded String representation of Binary
120      * @param mimeType
121      * of the Binary
122      */

123     public OMTextImpl(OMElement parent, String JavaDoc s, String JavaDoc mimeType,
124             boolean optimize) {
125         this(parent, s);
126         this.mimeType = mimeType;
127         this.optimize = optimize;
128         if (this.contentID == null && optimize==true) {
129             createContentID();
130         }
131         done = true;
132     }
133
134     /**
135      * @param dataHandler
136      * To send binary optimised content Created programatically.
137      */

138     public OMTextImpl(DataHandler JavaDoc dataHandler) {
139         this(dataHandler,true);
140
141     }
142
143     /**
144      * @param dataHandler
145      * @param optimize
146      * To send binary content. Created progrmatically.
147      */

148     public OMTextImpl(DataHandler JavaDoc dataHandler, boolean optimize) {
149         this.dataHandler = dataHandler;
150         this.isBinary = true;
151         this.optimize = optimize;
152         if (this.contentID == null && optimize==true) {
153             createContentID();
154         }
155         done = true;
156
157     }
158
159     /**
160      * @param contentID
161      * @param parent
162      * @param builder
163      * Used when the builder is encountered with a XOP:Include tag
164      * Stores a reference to the builder and the content-id. Supports
165      * deffered parsing of MIME messages
166      */

167     public OMTextImpl(String JavaDoc contentID, OMElement parent,
168             OMXMLParserWrapper builder) {
169         super(parent);
170         this.contentID = contentID;
171         this.optimize = true;
172         this.isBinary = true;
173         this.builder = builder;
174         done = true;
175         
176     }
177
178     /**
179      * We use the OMText class to hold comments, text, characterData, CData,
180      * etc., The codes are found in OMNode class
181      *
182      * @param type
183      */

184     public void setTextType(short type) {
185         if ((type == TEXT_NODE) || (type == COMMENT_NODE)
186                 || (type == CDATA_SECTION_NODE)) {
187             this.textType = type;
188         } else {
189             throw new UnsupportedOperationException JavaDoc("Attempt to set wrong type");
190         }
191     }
192
193     public int getType() throws OMException {
194         return textType;
195     }
196
197     /**
198      * @param writer
199      * @throws XMLStreamException
200      */

201     public void serializeWithCache(OMOutput omOutput) throws XMLStreamException {
202         XMLStreamWriter writer = omOutput.getXmlStreamWriter();
203         if (textType == TEXT_NODE) {
204             writer.writeCharacters(this.value);
205         } else if (textType == COMMENT_NODE) {
206             writer.writeComment(this.value);
207         } else if (textType == CDATA_SECTION_NODE) {
208             writer.writeCData(this.value);
209         }
210         OMNode nextSibling = this.getNextSibling();
211         if (nextSibling != null) {
212             nextSibling.serializeWithCache(omOutput);
213         }
214     }
215
216     /**
217      * Returns the value
218      */

219     public String JavaDoc getText() throws OMException {
220         if (this.value != null) {
221             return this.value;
222         } else {
223             try {
224                 InputStream JavaDoc inStream;
225                 inStream = this.getInputStream();
226                 byte[] data;
227                 data = new byte[inStream.available()];
228                 IOUtils.readFully(inStream, data);
229                 return Base64.encode(data);
230             } catch (Exception JavaDoc e) {
231                 throw new OMException(
232                         "Cannot read from Stream taken form the Data Handler"
233                                 + e);
234             }
235         }
236
237     }
238
239     public boolean isOptimized() {
240         return optimize;
241     }
242
243     public void doOptimize(boolean value) {
244         this.optimize = value;
245         if (this.contentID == null && value==true) {
246             getContentID();
247         }
248     }
249
250     /**
251      * @return
252      * @throws org.apache.axis2.om.OMException
253      * @throws OMException
254      */

255     public DataHandler JavaDoc getDataHandler() {
256
257         /*
258          * this should return a DataHandler containing the binary data
259          * reperesented by the Base64 strings stored in OMText
260          */

261         if (value != null) {
262             ByteArrayDataSource dataSource;
263             byte[] data = Base64.decode(value);
264             if (mimeType != null) {
265                 dataSource = new ByteArrayDataSource(data, mimeType);
266             } else {
267                 // Assumes type as application/octet-stream
268
dataSource = new ByteArrayDataSource(data);
269             }
270             DataHandler JavaDoc dataHandler = new DataHandler JavaDoc(dataSource);
271             return dataHandler;
272         } else {
273             if (dataHandler == null) {
274                 dataHandler = ((MTOMStAXSOAPModelBuilder) builder)
275                         .getDataHandler(contentID);
276             }
277             return dataHandler;
278         }
279     }
280
281     public String JavaDoc getLocalName() {
282         return localName;
283     }
284
285     public java.io.InputStream JavaDoc getInputStream() throws OMException {
286         if (isBinary == true) {
287             if (dataHandler == null) {
288                 getDataHandler();
289             }
290             InputStream JavaDoc inStream;
291             try {
292                 inStream = dataHandler.getDataSource().getInputStream();
293             } catch (IOException JavaDoc e) {
294                 throw new OMException(
295                         "Cannot get InputStream from DataHandler." + e);
296             }
297             return inStream;
298         } else {
299             throw new OMException("Unsupported Operation");
300         }
301     }
302
303     public String JavaDoc getContentID() {
304         return this.contentID;
305     }
306
307     public boolean isComplete() {
308         return true;
309     }
310
311     public void serialize(OMOutput omOutput) throws XMLStreamException {
312         boolean firstElement = false;
313
314         if (!this.isBinary) {
315             serializeWithCache(omOutput);
316         } else {
317             if (omOutput.doOptimise()) {
318                 // send binary as MTOM optimised
319
this.attribute = new OMAttributeImpl("href",
320                         new OMNamespaceImpl("", ""), "cid:"
321                                 + this.contentID.trim());
322
323                 this.serializeStartpart(omOutput);
324                 omOutput.writeOptimised(this);
325                 omOutput.getXmlStreamWriter().writeEndElement();
326             } else {
327                 omOutput.getXmlStreamWriter().writeCharacters(this.getText());
328             }
329             // TODO do we need these
330
OMNode nextSibling = this.getNextSibling();
331             if (nextSibling != null) {
332                 // serilize next sibling
333
nextSibling.serialize(omOutput);
334             } else {
335                 // TODO : See whether following part is really needed
336
if (parent == null) {
337                     return;
338                 } else if (parent.isComplete()) {
339                     return;
340                 } else {
341                     // do the special serialization
342
// Only the push serializer is left now
343
builder.next();
344                 }
345
346             }
347         }
348
349     }
350     private void createContentID()
351     {
352 // We can use a UUID, taken using Apache commons id project.
353
// TODO change to UUID
354
this.contentID = "2"+String.valueOf(rnd.nextLong())+"@schemas.xmlsoap.org";
355     }
356
357
358     /*
359      * Methods to copy from OMSerialize utils
360      */

361     private void serializeStartpart(OMOutput omOutput)
362             throws XMLStreamException {
363         String JavaDoc nameSpaceName = null;
364         String JavaDoc writer_prefix = null;
365         String JavaDoc prefix = null;
366         XMLStreamWriter writer = omOutput.getXmlStreamWriter();
367         if (this.ns != null) {
368             nameSpaceName = this.ns.getName();
369             writer_prefix = writer.getPrefix(nameSpaceName);
370             prefix = this.ns.getPrefix();
371           
372             if (nameSpaceName != null) {
373                 if (writer_prefix != null) {
374                     writer.writeStartElement(nameSpaceName,
375                             this.getLocalName());
376                 } else {
377                     if (prefix != null) {
378                         writer.writeStartElement(prefix, this.getLocalName(),
379                                 nameSpaceName);
380                         //TODO FIX ME
381
//writer.writeNamespace(prefix, nameSpaceName);
382
writer.setPrefix(prefix, nameSpaceName);
383                     } else {
384                         writer.writeStartElement(nameSpaceName,
385                                 this.getLocalName());
386                         writer.writeDefaultNamespace(nameSpaceName);
387                         writer.setDefaultNamespace(nameSpaceName);
388                     }
389                 }
390             } else {
391                 writer.writeStartElement(this.getLocalName());
392
393             }
394         } else {
395             writer.writeStartElement(this.getLocalName());
396
397         }
398
399         // add the elements attribute "href"
400

401         serializeAttribute(this.attribute, omOutput);
402
403         // add the namespace
404
serializeNamespace(this.ns, omOutput);
405
406     }
407
408     /**
409      * Method serializeAttribute
410      *
411      * @param attr
412      * @param writer
413      * @throws XMLStreamException
414      */

415     static void serializeAttribute(OMAttribute attr, OMOutput omOutput)
416             throws XMLStreamException {
417
418         XMLStreamWriter writer = omOutput.getXmlStreamWriter();
419         // first check whether the attribute is associated with a namespace
420
OMNamespace ns = attr.getNamespace();
421         String JavaDoc prefix = null;
422         String JavaDoc namespaceName = null;
423         if (ns != null) {
424
425             // add the prefix if it's availble
426
prefix = ns.getPrefix();
427             namespaceName = ns.getName();
428             if (prefix != null) {
429                 writer.writeAttribute(prefix, namespaceName, attr
430                         .getLocalName(), attr.getValue());
431             } else {
432                 writer.writeAttribute(namespaceName, attr.getLocalName(), attr
433                         .getValue());
434             }
435         } else {
436             writer.writeAttribute(attr.getLocalName(), attr.getValue());
437         }
438     }
439
440     /**
441      * Method serializeNamespace
442      *
443      * @param namespace
444      * @param writer
445      * @throws XMLStreamException
446      */

447     static void serializeNamespace(OMNamespace namespace, OMOutput omOutput)
448             throws XMLStreamException {
449         XMLStreamWriter writer = omOutput.getXmlStreamWriter();
450         if (namespace != null) {
451             String JavaDoc uri = namespace.getName();
452             //String prefix = writer.getPrefix(uri);
453
String JavaDoc ns_prefix = namespace.getPrefix();
454             //if (prefix == null) {
455
writer.writeNamespace(ns_prefix, namespace.getName());
456                 writer.setPrefix(ns_prefix, uri);
457             //}
458
}
459     }
460
461     /**
462      * Slightly different implementation of the discard method
463      *
464      * @throws OMException
465      */

466     public void discard() throws OMException {
467         if (done) {
468             this.detach();
469         } else {
470             builder.discard((OMElement) this.parent);
471         }
472     }
473
474 }
Popular Tags