1 /*
2 * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
3 *
4 * This software is open source.
5 * See the bottom of this file for the licence.
6 *
7 * $Id: DocumentSource.java,v 1.8 2004/06/25 12:34:47 maartenc Exp $
8 */
9
10 package org.dom4j.io;
11
12 import javax.xml.transform.sax.SAXSource;
13
14 import org.dom4j.Document;
15 import org.dom4j.Node;
16 import org.xml.sax.InputSource;
17 import org.xml.sax.XMLFilter;
18 import org.xml.sax.XMLReader;
19
20 /***
21 * <p><code>DocumentSource</code> implements a JAXP {@link SAXSource}
22 * for a {@link Document}.</p>
23 *
24 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
25 * @version $Revision: 1.8 $
26 */
27 public class DocumentSource extends SAXSource {
28
29 /*** If {@link javax.xml.transform.TransformerFactory#getFeature}
30 * returns <code>true</code> when passed this value as an argument
31 * then the Transformer natively supports <i>dom4j</i>.
32 */
33 public final static String DOM4J_FEATURE = "http://org.dom4j.io.DoucmentSource/feature";
34
35 /*** The XMLReader to use */
36 private XMLReader xmlReader = new SAXWriter();
37
38
39 /*** Creates a JAXP {@link SAXSource} for the given
40 * {@link Node}.
41 */
42 public DocumentSource(Node node) {
43 setDocument(node.getDocument());
44 }
45
46 /*** Creates a JAXP {@link SAXSource} for the given
47 * {@link Document}.
48 */
49 public DocumentSource(Document document) {
50 setDocument(document);
51 }
52
53
54 // Properties
55 //-------------------------------------------------------------------------
56
57 /*** @return the document which is being used as the JAXP {@link SAXSource}
58 */
59 public Document getDocument() {
60 DocumentInputSource documentInputSource
61 = (DocumentInputSource) getInputSource();
62 return documentInputSource.getDocument();
63 }
64
65 /*** Sets the document used as the JAXP {@link SAXSource}
66 */
67 public void setDocument(Document document) {
68 super.setInputSource( new DocumentInputSource(document) );
69 }
70
71
72 // Overloaded methods
73 //-------------------------------------------------------------------------
74
75 /*** @return the XMLReader to be used for the JAXP {@link SAXSource}.
76 */
77 public XMLReader getXMLReader() {
78 return xmlReader;
79 }
80
81 /*** This method is not supported as this source is always a
82 * {@link Document} instance.
83 *
84 * @throws UnsupportedOperationException as this method is unsupported
85 */
86 public void setInputSource(InputSource inputSource)
87 throws UnsupportedOperationException {
88 if ( inputSource instanceof DocumentInputSource ) {
89 super.setInputSource( (DocumentInputSource) inputSource );
90 }
91 else {
92 throw new UnsupportedOperationException();
93 }
94 }
95
96 /*** Sets the XMLReader used for the JAXP {@link SAXSource}.
97 */
98 public void setXMLReader(XMLReader reader)
99 throws UnsupportedOperationException {
100 if (reader instanceof SAXWriter) {
101 this.xmlReader = (SAXWriter) reader;
102 }
103 else if (reader instanceof XMLFilter) {
104 XMLFilter filter = (XMLFilter) reader;
105 while (true) {
106 XMLReader parent = filter.getParent();
107 if ( parent instanceof XMLFilter ) {
108 filter = (XMLFilter) parent;
109 }
110 else {
111 break;
112 }
113 }
114 // install filter in SAXWriter....
115 filter.setParent(xmlReader);
116 xmlReader = filter;
117 }
118 else {
119 throw new UnsupportedOperationException();
120 }
121 }
122
123 }
124
125
126
127
128
129
130
131 /*
132 * Redistribution and use of this software and associated documentation
133 * ("Software"), with or without modification, are permitted provided
134 * that the following conditions are met:
135 *
136 * 1. Redistributions of source code must retain copyright
137 * statements and notices. Redistributions must also contain a
138 * copy of this document.
139 *
140 * 2. Redistributions in binary form must reproduce the
141 * above copyright notice, this list of conditions and the
142 * following disclaimer in the documentation and/or other
143 * materials provided with the distribution.
144 *
145 * 3. The name "DOM4J" must not be used to endorse or promote
146 * products derived from this Software without prior written
147 * permission of MetaStuff, Ltd. For written permission,
148 * please contact dom4j-info@metastuff.com.
149 *
150 * 4. Products derived from this Software may not be called "DOM4J"
151 * nor may "DOM4J" appear in their names without prior written
152 * permission of MetaStuff, Ltd. DOM4J is a registered
153 * trademark of MetaStuff, Ltd.
154 *
155 * 5. Due credit should be given to the DOM4J Project -
156 * http://www.dom4j.org
157 *
158 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
159 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
160 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
161 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
162 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
163 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
164 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
165 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
166 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
167 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
168 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
169 * OF THE POSSIBILITY OF SUCH DAMAGE.
170 *
171 * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
172 *
173 * $Id: DocumentSource.java,v 1.8 2004/06/25 12:34:47 maartenc Exp $
174 */