1
2
3
4
5
6
7
8 package org.dom4j.io;
9
10 import javax.xml.transform.sax.SAXSource;
11
12 import org.dom4j.Document;
13 import org.dom4j.Node;
14
15 import org.xml.sax.InputSource;
16 import org.xml.sax.XMLFilter;
17 import org.xml.sax.XMLReader;
18
19 /***
20 * <p>
21 * <code>DocumentSource</code> implements a JAXP {@link SAXSource}for a
22 * {@linkDocument}.
23 * </p>
24 *
25 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
26 * @version $Revision: 1.10 $
27 */
28 public class DocumentSource extends SAXSource {
29 /***
30 * If {@link javax.xml.transform.TransformerFactory#getFeature}returns
31 * <code>true</code> when passed this value as an argument then the
32 * Transformer natively supports <i>dom4j </i>.
33 */
34 public static final String DOM4J_FEATURE
35 = "http://org.dom4j.io.DoucmentSource/feature";
36
37 /*** The XMLReader to use */
38 private XMLReader xmlReader = new SAXWriter();
39
40 /***
41 * Creates a JAXP {@link SAXSource}for the given {@link Node}.
42 *
43 * @param node
44 * DOCUMENT ME!
45 */
46 public DocumentSource(Node node) {
47 setDocument(node.getDocument());
48 }
49
50 /***
51 * Creates a JAXP {@link SAXSource}for the given {@link Document}.
52 *
53 * @param document
54 * DOCUMENT ME!
55 */
56 public DocumentSource(Document document) {
57 setDocument(document);
58 }
59
60
61
62
63 /***
64 * DOCUMENT ME!
65 *
66 * @return the document which is being used as the JAXP {@link SAXSource}
67 */
68 public Document getDocument() {
69 DocumentInputSource source = (DocumentInputSource) getInputSource();
70 return source.getDocument();
71 }
72
73 /***
74 * Sets the document used as the JAXP {@link SAXSource}
75 *
76 * @param document
77 * DOCUMENT ME!
78 */
79 public void setDocument(Document document) {
80 super.setInputSource(new DocumentInputSource(document));
81 }
82
83
84
85
86 /***
87 * DOCUMENT ME!
88 *
89 * @return the XMLReader to be used for the JAXP {@link SAXSource}.
90 */
91 public XMLReader getXMLReader() {
92 return xmlReader;
93 }
94
95 /***
96 * This method is not supported as this source is always a {@linkDocument}
97 * instance.
98 *
99 * @param inputSource
100 * DOCUMENT ME!
101 *
102 * @throws UnsupportedOperationException
103 * as this method is unsupported
104 */
105 public void setInputSource(InputSource inputSource)
106 throws UnsupportedOperationException {
107 if (inputSource instanceof DocumentInputSource) {
108 super.setInputSource((DocumentInputSource) inputSource);
109 } else {
110 throw new UnsupportedOperationException();
111 }
112 }
113
114 /***
115 * Sets the XMLReader used for the JAXP {@link SAXSource}.
116 *
117 * @param reader
118 * DOCUMENT ME!
119 *
120 * @throws UnsupportedOperationException
121 * DOCUMENT ME!
122 */
123 public void setXMLReader(XMLReader reader)
124 throws UnsupportedOperationException {
125 if (reader instanceof SAXWriter) {
126 this.xmlReader = (SAXWriter) reader;
127 } else if (reader instanceof XMLFilter) {
128 XMLFilter filter = (XMLFilter) reader;
129
130 while (true) {
131 XMLReader parent = filter.getParent();
132
133 if (parent instanceof XMLFilter) {
134 filter = (XMLFilter) parent;
135 } else {
136 break;
137 }
138 }
139
140
141 filter.setParent(xmlReader);
142 xmlReader = filter;
143 } else {
144 throw new UnsupportedOperationException();
145 }
146 }
147 }
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184