KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > projector > value > DocumentValue


1 package org.apache.slide.projector.value;
2
3 import java.io.ByteArrayInputStream JavaDoc;
4 import java.io.ByteArrayOutputStream JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.InputStream JavaDoc;
7 import java.util.logging.Level JavaDoc;
8 import java.util.logging.Logger JavaDoc;
9
10 import org.jdom.DocType;
11 import org.jdom.Document;
12 import org.jdom.Element;
13 import org.jdom.JDOMException;
14 import org.jdom.input.SAXBuilder;
15 import org.jdom.output.Format;
16 import org.jdom.output.XMLOutputter;
17
18 public class DocumentValue extends AbstractStreamableValue implements XMLValue {
19     private static Logger JavaDoc logger = Logger.getLogger(DocumentValue.class.getName());
20
21     private final static XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat());
22     private byte[] bytes = null;
23     private Document document;
24     private boolean documentCreated = false;
25     private InputStream JavaDoc inputStream = null;
26
27     public DocumentValue(StreamableValue streamableResource) throws IOException JavaDoc {
28         this(streamableResource.getInputStream());
29     }
30
31     public DocumentValue(InputStream JavaDoc inputStream) {
32         this.inputStream = inputStream;
33     }
34
35     public DocumentValue(Document document) {
36         this.document = document;
37         documentCreated = true;
38     }
39
40     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
41         if ( inputStream != null ) return inputStream;
42         if ( bytes == null ) {
43             bytes = getBytes();
44         }
45         InputStream JavaDoc inputStream = new ByteArrayInputStream JavaDoc(bytes);
46         return inputStream;
47     }
48
49     private byte[] getBytes() throws IOException JavaDoc {
50         ByteArrayOutputStream JavaDoc outputStream = new ByteArrayOutputStream JavaDoc();
51         xmlOutputter.output(getDocument(), outputStream);
52         return outputStream.toByteArray();
53     }
54
55     public void enableMultipleStreaming() {}
56
57     public DocType getDocumentType() {
58         DocType docType = getDocument().getDocType();
59         if (docType == null) {
60             docType = new DocType(getDocument().getRootElement().getName());
61             getDocument().setDocType(docType);
62         }
63         return docType;
64     }
65
66     public Document getDocument() {
67         if ( !documentCreated ) {
68             documentCreated = true;
69             SAXBuilder saxBuilder = new SAXBuilder();
70             try {
71                 document = saxBuilder.build(inputStream);
72             } catch (JDOMException e) {
73                 logger.log(Level.SEVERE, "Could not create XML document from given input stream", e);
74                 // FIXME: Exception handling!
75
document = null;
76             } catch (IOException JavaDoc e) {
77                 logger.log(Level.SEVERE, "Could not create XML document from given input stream", e);
78                 document = null;
79             }
80         }
81         return document;
82     }
83
84     public Element getRootElement() {
85         return getDocument().getRootElement();
86     }
87
88     public String JavaDoc getContentType() {
89         return XMLValue.CONTENT_TYPE;
90     }
91
92     public String JavaDoc getCharacterEncoding() {
93         return "UTF-8";
94     }
95
96     public int getContentLength() {
97         if ( bytes == null ) {
98             try {
99                 bytes = getBytes();
100             } catch ( IOException JavaDoc e ) {
101                 // could not calculate content length
102
return -1;
103             }
104         }
105         return bytes.length;
106     }
107
108     public boolean isDocument() {
109         return true;
110     }
111 }
Popular Tags