1 package org.apache.slide.projector.value; 2 3 import java.io.ByteArrayInputStream ; 4 import java.io.ByteArrayOutputStream ; 5 import java.io.IOException ; 6 import java.io.InputStream ; 7 import java.util.logging.Level ; 8 import java.util.logging.Logger ; 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 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 inputStream = null; 26 27 public DocumentValue(StreamableValue streamableResource) throws IOException { 28 this(streamableResource.getInputStream()); 29 } 30 31 public DocumentValue(InputStream inputStream) { 32 this.inputStream = inputStream; 33 } 34 35 public DocumentValue(Document document) { 36 this.document = document; 37 documentCreated = true; 38 } 39 40 public InputStream getInputStream() throws IOException { 41 if ( inputStream != null ) return inputStream; 42 if ( bytes == null ) { 43 bytes = getBytes(); 44 } 45 InputStream inputStream = new ByteArrayInputStream (bytes); 46 return inputStream; 47 } 48 49 private byte[] getBytes() throws IOException { 50 ByteArrayOutputStream outputStream = new ByteArrayOutputStream (); 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 document = null; 76 } catch (IOException 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 getContentType() { 89 return XMLValue.CONTENT_TYPE; 90 } 91 92 public String getCharacterEncoding() { 93 return "UTF-8"; 94 } 95 96 public int getContentLength() { 97 if ( bytes == null ) { 98 try { 99 bytes = getBytes(); 100 } catch ( IOException e ) { 101 return -1; 103 } 104 } 105 return bytes.length; 106 } 107 108 public boolean isDocument() { 109 return true; 110 } 111 } | Popular Tags |