1 7 8 package org.dom4j.io; 9 10 import java.io.IOException ; 11 import java.io.Reader ; 12 import java.io.StringReader ; 13 import java.io.StringWriter ; 14 15 import org.dom4j.Document; 16 17 import org.xml.sax.InputSource ; 18 19 28 class DocumentInputSource extends InputSource { 29 30 private Document document; 31 32 public DocumentInputSource() { 33 } 34 35 public DocumentInputSource(Document document) { 36 this.document = document; 37 setSystemId(document.getName()); 38 } 39 40 43 48 public Document getDocument() { 49 return document; 50 } 51 52 58 public void setDocument(Document document) { 59 this.document = document; 60 setSystemId(document.getName()); 61 } 62 63 66 76 public void setCharacterStream(Reader characterStream) 77 throws UnsupportedOperationException { 78 throw new UnsupportedOperationException (); 79 } 80 81 88 public Reader getCharacterStream() { 89 try { 90 StringWriter out = new StringWriter (); 91 XMLWriter writer = new XMLWriter(out); 92 writer.write(document); 93 writer.flush(); 94 95 return new StringReader (out.toString()); 96 } catch (final IOException e) { 97 return new Reader () { 101 public int read(char[] ch, int offset, int length) 102 throws IOException { 103 throw e; 104 } 105 106 public void close() throws IOException { 107 } 108 }; 109 } 110 } 111 } 112 113 149 | Popular Tags |