KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > io > DocumentSource


1 /*
2  * Copyright 2001-2005 (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
8 package org.dom4j.io;
9
10 import javax.xml.transform.sax.SAXSource JavaDoc;
11
12 import org.dom4j.Document;
13 import org.dom4j.Node;
14
15 import org.xml.sax.InputSource JavaDoc;
16 import org.xml.sax.XMLFilter JavaDoc;
17 import org.xml.sax.XMLReader JavaDoc;
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 JavaDoc {
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 JavaDoc DOM4J_FEATURE
35             = "http://org.dom4j.io.DoucmentSource/feature";
36
37     /** The XMLReader to use */
38     private XMLReader JavaDoc 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     // Properties
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     // Overloaded methods
84
// -------------------------------------------------------------------------
85

86     /**
87      * DOCUMENT ME!
88      *
89      * @return the XMLReader to be used for the JAXP {@link SAXSource}.
90      */

91     public XMLReader JavaDoc 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 JavaDoc inputSource)
106             throws UnsupportedOperationException JavaDoc {
107         if (inputSource instanceof DocumentInputSource) {
108             super.setInputSource((DocumentInputSource) inputSource);
109         } else {
110             throw new UnsupportedOperationException JavaDoc();
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 JavaDoc reader)
124             throws UnsupportedOperationException JavaDoc {
125         if (reader instanceof SAXWriter) {
126             this.xmlReader = (SAXWriter) reader;
127         } else if (reader instanceof XMLFilter JavaDoc) {
128             XMLFilter JavaDoc filter = (XMLFilter JavaDoc) reader;
129
130             while (true) {
131                 XMLReader JavaDoc parent = filter.getParent();
132
133                 if (parent instanceof XMLFilter JavaDoc) {
134                     filter = (XMLFilter JavaDoc) parent;
135                 } else {
136                     break;
137                 }
138             }
139
140             // install filter in SAXWriter....
141
filter.setParent(xmlReader);
142             xmlReader = filter;
143         } else {
144             throw new UnsupportedOperationException JavaDoc();
145         }
146     }
147 }
148
149 /*
150  * Redistribution and use of this software and associated documentation
151  * ("Software"), with or without modification, are permitted provided that the
152  * following conditions are met:
153  *
154  * 1. Redistributions of source code must retain copyright statements and
155  * notices. Redistributions must also contain a copy of this document.
156  *
157  * 2. Redistributions in binary form must reproduce the above copyright notice,
158  * this list of conditions and the following disclaimer in the documentation
159  * and/or other materials provided with the distribution.
160  *
161  * 3. The name "DOM4J" must not be used to endorse or promote products derived
162  * from this Software without prior written permission of MetaStuff, Ltd. For
163  * written permission, please contact dom4j-info@metastuff.com.
164  *
165  * 4. Products derived from this Software may not be called "DOM4J" nor may
166  * "DOM4J" appear in their names without prior written permission of MetaStuff,
167  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
168  *
169  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
170  *
171  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
172  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
173  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
174  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
175  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
176  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
177  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
178  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
179  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
180  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
181  * POSSIBILITY OF SUCH DAMAGE.
182  *
183  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
184  */

185
Popular Tags