1
2
3
4
5
6
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 /***
20 * <p>
21 * <code>DocumentInputSource</code> implements a SAX {@link InputSource}for a
22 * {@link Document}.
23 * </p>
24 *
25 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
26 * @version $Revision: 1.8 $
27 */
28 class DocumentInputSource extends InputSource {
29 /*** The document source */
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
41
42
43 /***
44 * DOCUMENT ME!
45 *
46 * @return the document which is being used as the SAX {@link InputSource}
47 */
48 public Document getDocument() {
49 return document;
50 }
51
52 /***
53 * Sets the document used as the SAX {@link InputSource}
54 *
55 * @param document
56 * DOCUMENT ME!
57 */
58 public void setDocument(Document document) {
59 this.document = document;
60 setSystemId(document.getName());
61 }
62
63
64
65
66 /***
67 * This method is not supported as this source is always a {@linkDocument}
68 * instance.
69 *
70 * @param characterStream
71 * DOCUMENT ME!
72 *
73 * @throws UnsupportedOperationException
74 * as this method is unsupported
75 */
76 public void setCharacterStream(Reader characterStream)
77 throws UnsupportedOperationException {
78 throw new UnsupportedOperationException();
79 }
80
81 /***
82 * Note this method is quite inefficent, it turns the in memory XML tree
83 * object model into a single block of text which can then be read by other
84 * XML parsers. Should only be used with care.
85 *
86 * @return DOCUMENT ME!
87 */
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
98
99
100 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148