annotate NGSrich_0.5.5/src/org/jdom/transform/JDOMSource.java @ 0:89ad0a9cca52 default tip

Uploaded
author pfrommolt
date Mon, 21 Nov 2011 08:12:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
1 /*--
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
2
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
3 $Id: JDOMSource.java,v 1.20 2007/11/10 05:29:02 jhunter Exp $
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
4
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
5 Copyright (C) 2001-2007 Jason Hunter & Brett McLaughlin.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
6 All rights reserved.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
7
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
8 Redistribution and use in source and binary forms, with or without
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
9 modification, are permitted provided that the following conditions
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
10 are met:
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
11
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
12 1. Redistributions of source code must retain the above copyright
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
13 notice, this list of conditions, and the following disclaimer.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
14
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
15 2. Redistributions in binary form must reproduce the above copyright
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
16 notice, this list of conditions, and the disclaimer that follows
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
17 these conditions in the documentation and/or other materials
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
18 provided with the distribution.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
19
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
20 3. The name "JDOM" must not be used to endorse or promote products
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
21 derived from this software without prior written permission. For
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
22 written permission, please contact <request_AT_jdom_DOT_org>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
23
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
24 4. Products derived from this software may not be called "JDOM", nor
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
25 may "JDOM" appear in their name, without prior written permission
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
26 from the JDOM Project Management <request_AT_jdom_DOT_org>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
27
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
28 In addition, we request (but do not require) that you include in the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
29 end-user documentation provided with the redistribution and/or in the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
30 software itself an acknowledgement equivalent to the following:
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
31 "This product includes software developed by the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
32 JDOM Project (http://www.jdom.org/)."
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
33 Alternatively, the acknowledgment may be graphical using the logos
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
34 available at http://www.jdom.org/images/logos.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
35
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
36 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
37 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
38 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
39 DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
40 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
41 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
42 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
43 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
44 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
45 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
46 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
47 SUCH DAMAGE.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
48
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
49 This software consists of voluntary contributions made by many
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
50 individuals on behalf of the JDOM Project and was originally
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
51 created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
52 Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
53 on the JDOM Project, please see <http://www.jdom.org/>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
54
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
55 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
56
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
57 package org.jdom.transform;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
58
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
59 import java.io.*;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
60 import java.util.*;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
61
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
62 import javax.xml.transform.sax.*;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
63
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
64 import org.jdom.*;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
65 import org.jdom.output.*;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
66 import org.xml.sax.*;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
67
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
68 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
69 * A holder for an XML Transformation source: a Document, Element, or list of
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
70 * nodes.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
71 * <p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
72 * The is provides input to a
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
73 * {@link javax.xml.transform.Transformer JAXP TrAX Transformer}.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
74 * <p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
75 * The following example shows how to apply an XSL Transformation
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
76 * to a JDOM document and get the transformation result in the form
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
77 * of a list of JDOM nodes:
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
78 * <pre><code>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
79 * public static List transform(Document doc, String stylesheet)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
80 * throws JDOMException {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
81 * try {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
82 * Transformer transformer = TransformerFactory.newInstance()
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
83 * .newTransformer(new StreamSource(stylesheet));
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
84 * JDOMSource in = new JDOMSource(doc);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
85 * JDOMResult out = new JDOMResult();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
86 * transformer.transform(in, out);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
87 * return out.getResult();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
88 * }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
89 * catch (TransformerException e) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
90 * throw new JDOMException("XSLT Transformation failed", e);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
91 * }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
92 * }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
93 * </code></pre>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
94 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
95 * @see org.jdom.transform.JDOMResult
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
96 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
97 * @version $Revision: 1.20 $, $Date: 2007/11/10 05:29:02 $
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
98 * @author Laurent Bihanic
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
99 * @author Jason Hunter
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
100 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
101 public class JDOMSource extends SAXSource {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
102
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
103 private static final String CVS_ID =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
104 "@(#) $RCSfile: JDOMSource.java,v $ $Revision: 1.20 $ $Date: 2007/11/10 05:29:02 $ $Name: jdom_1_1_1 $";
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
105
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
106 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
107 * If {@link javax.xml.transform.TransformerFactory#getFeature}
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
108 * returns <code>true</code> when passed this value as an
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
109 * argument, the Transformer natively supports JDOM.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
110 * <p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
111 * <strong>Note</strong>: This implementation does not override
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
112 * the {@link SAXSource#FEATURE} value defined by its superclass
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
113 * to be considered as a SAXSource by Transformer implementations
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
114 * not natively supporting JDOM.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
115 * </p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
116 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
117 public final static String JDOM_FEATURE =
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
118 "http://org.jdom.transform.JDOMSource/feature";
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
119
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
120 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
121 * The XMLReader object associated to this source or
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
122 * <code>null</code> if no XMLReader has yet been requested.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
123 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
124 * @see #getXMLReader
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
125 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
126 private XMLReader xmlReader = null;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
127
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
128 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
129 * Optional entity resolver associated to the source of
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
130 * this document or <code>null</code> if no EntityResolver
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
131 * was supplied with this JDOMSource.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
132 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
133 * @see #buildDocumentReader()
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
134 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
135 private EntityResolver resolver = null;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
136
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
137 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
138 * Creates a JDOM TrAX source wrapping a JDOM document.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
139 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
140 * @param source the JDOM document to use as source for the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
141 * transformations
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
142 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
143 * @throws IllegalArgumentException if <code>source</code> is
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
144 * <code>null</code>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
145 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
146 public JDOMSource(Document source) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
147 setDocument(source);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
148 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
149
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
150 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
151 * Creates a JDOM TrAX source wrapping a list of JDOM nodes.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
152 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
153 * @param source the JDOM nodes to use as source for the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
154 * transformations
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
155 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
156 * @throws IllegalArgumentException if <code>source</code> is
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
157 * <code>null</code>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
158 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
159 public JDOMSource(List source) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
160 setNodes(source);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
161 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
162
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
163 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
164 * Creates a JDOM TrAX source wrapping a JDOM element.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
165 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
166 * @param source the JDOM element to use as source for the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
167 * transformations
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
168 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
169 * @throws IllegalArgumentException if <code>source</code> is
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
170 * <code>null</code>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
171 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
172 public JDOMSource(Element source) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
173 List nodes = new ArrayList();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
174 nodes.add(source);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
175
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
176 setNodes(nodes);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
177 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
178
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
179 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
180 * Creates a JDOM TrAX source wrapping a JDOM element with an
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
181 * associated EntityResolver to resolve external entities.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
182 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
183 * @param source The JDOM Element to use as source for the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
184 * transformations
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
185 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
186 * @param resolver Entity resolver to use for the source
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
187 * transformation
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
188 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
189 * @throws IllegalArgumentException if<code>source</code> is
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
190 * <code>null</code>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
191 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
192 public JDOMSource(Document source, EntityResolver resolver) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
193 setDocument(source);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
194 this.resolver = resolver;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
195 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
196
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
197 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
198 * Sets the source document used by this TrAX source.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
199 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
200 * @param source the JDOM document to use as source for the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
201 * transformations
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
202 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
203 * @throws IllegalArgumentException if <code>source</code> is
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
204 * <code>null</code>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
205 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
206 * @see #getDocument
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
207 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
208 public void setDocument(Document source) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
209 super.setInputSource(new JDOMInputSource(source));
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
210 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
211
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
212 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
213 * Returns the source document used by this TrAX source.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
214 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
215 * @return the source document used by this TrAX source or
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
216 * <code>null</code> if the source is a node list.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
217 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
218 * @see #setDocument
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
219 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
220 public Document getDocument() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
221 Object src = ((JDOMInputSource)getInputSource()).getSource();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
222 Document doc = null;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
223
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
224 if (src instanceof Document) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
225 doc = (Document)src;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
226 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
227 return doc;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
228 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
229
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
230 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
231 * Sets the source node list used by this TrAX source.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
232 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
233 * @param source the JDOM nodes to use as source for the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
234 * transformations
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
235 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
236 * @throws IllegalArgumentException if <code>source</code> is
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
237 * <code>null</code>.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
238 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
239 * @see #getNodes
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
240 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
241 public void setNodes(List source) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
242 super.setInputSource(new JDOMInputSource(source));
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
243 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
244
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
245 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
246 * Returns the source node list used by this TrAX source.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
247 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
248 * @return the source node list used by this TrAX source or
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
249 * <code>null</code> if the source is a JDOM document.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
250 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
251 * @see #setDocument
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
252 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
253 public List getNodes() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
254 Object src = ((JDOMInputSource)getInputSource()).getSource();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
255 List nodes = null;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
256
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
257 if (src instanceof List) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
258 nodes = (List)src;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
259 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
260 return nodes;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
261 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
262
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
263
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
264 //-------------------------------------------------------------------------
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
265 // SAXSource overwritten methods
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
266 //-------------------------------------------------------------------------
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
267
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
268 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
269 * Sets the SAX InputSource to be used for the Source.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
270 * <p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
271 * As this implementation only supports JDOM document as data
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
272 * source, this method always throws an
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
273 * {@link UnsupportedOperationException}.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
274 * </p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
275 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
276 * @param inputSource a valid InputSource reference.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
277 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
278 * @throws UnsupportedOperationException always!
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
279 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
280 public void setInputSource(InputSource inputSource)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
281 throws UnsupportedOperationException {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
282 throw new UnsupportedOperationException();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
283 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
284
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
285 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
286 * Set the XMLReader to be used for the Source.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
287 * <p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
288 * As this implementation only supports JDOM document as data
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
289 * source, this method throws an
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
290 * {@link UnsupportedOperationException} if the provided reader
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
291 * object does not implement the SAX {@link XMLFilter}
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
292 * interface. Otherwise, the JDOM document reader will be
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
293 * attached as parent of the filter chain.</p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
294 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
295 * @param reader a valid XMLReader or XMLFilter reference.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
296 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
297 * @throws UnsupportedOperationException if <code>reader</code>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
298 * is not a SAX
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
299 * {@link XMLFilter}.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
300 * @see #getXMLReader
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
301 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
302 public void setXMLReader(XMLReader reader)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
303 throws UnsupportedOperationException {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
304 if (reader instanceof XMLFilter) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
305 // Connect the filter chain to a document reader.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
306 XMLFilter filter = (XMLFilter)reader;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
307 while (filter.getParent() instanceof XMLFilter) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
308 filter = (XMLFilter)(filter.getParent());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
309 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
310 filter.setParent(buildDocumentReader());
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
311
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
312 // Read XML data from filter chain.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
313 this.xmlReader = reader;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
314 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
315 else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
316 throw new UnsupportedOperationException();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
317 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
318 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
319
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
320 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
321 * Returns the XMLReader to be used for the Source.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
322 * <p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
323 * This implementation returns a specific XMLReader reading
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
324 * the XML data from the source JDOM document.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
325 * </p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
326 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
327 * @return an XMLReader reading the XML data from the source
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
328 * JDOM document.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
329 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
330 public XMLReader getXMLReader() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
331 if (this.xmlReader == null) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
332 this.xmlReader = buildDocumentReader();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
333 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
334 return this.xmlReader;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
335 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
336
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
337 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
338 * Build an XMLReader to be used for the source. This will
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
339 * create a new instance of DocumentReader with an
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
340 * EntityResolver instance if available.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
341 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
342 * @return XMLReader reading the XML data from the source
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
343 * JDOM document with an optional EntityResolver
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
344 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
345 private XMLReader buildDocumentReader() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
346 DocumentReader reader = new DocumentReader();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
347 if (resolver != null)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
348 reader.setEntityResolver(resolver);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
349 return reader;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
350 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
351
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
352 //=========================================================================
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
353 // JDOMInputSource nested class
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
354 //=========================================================================
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
355
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
356 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
357 * A subclass of the SAX InputSource interface that wraps a JDOM
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
358 * Document.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
359 * <p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
360 * This class is nested in JDOMSource as it is not intented to
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
361 * be used independently of its friend: DocumentReader.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
362 * </p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
363 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
364 * @see org.jdom.Document
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
365 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
366 private static class JDOMInputSource extends InputSource {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
367 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
368 * The source as a JDOM document or a list of JDOM nodes.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
369 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
370 private Object source = null;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
371
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
372 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
373 * Builds a InputSource wrapping the specified JDOM Document.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
374 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
375 * @param document the source document.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
376 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
377 public JDOMInputSource(Document document) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
378 this.source = document;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
379 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
380
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
381 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
382 * Builds a InputSource wrapping a list of JDOM nodes.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
383 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
384 * @param nodes the source JDOM nodes.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
385 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
386 public JDOMInputSource(List nodes) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
387 this.source = nodes;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
388 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
389
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
390 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
391 * Returns the source.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
392 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
393 * @return the source as a JDOM document or a list of JDOM nodes.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
394 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
395 public Object getSource() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
396 return source;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
397 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
398
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
399 //-------------------------------------------------------------------------
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
400 // InputSource overwritten methods
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
401 //-------------------------------------------------------------------------
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
402
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
403 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
404 * Sets the character stream for this input source.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
405 * <p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
406 * This implementation always throws an
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
407 * {@link UnsupportedOperationException} as the only source
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
408 * stream supported is the source JDOM document.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
409 * </p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
410 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
411 * @param characterStream a character stream containing
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
412 * an XML document.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
413 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
414 * @throws UnsupportedOperationException always!
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
415 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
416 public void setCharacterStream(Reader characterStream)
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
417 throws UnsupportedOperationException {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
418 throw new UnsupportedOperationException();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
419 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
420
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
421 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
422 * Gets the character stream for this input source.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
423 * <p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
424 * Note that this method is only provided to make this
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
425 * InputSource implementation acceptable by any XML
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
426 * parser. As it generates an in-memory string representation
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
427 * of the JDOM document, it is quite inefficient from both
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
428 * speed and memory consumption points of view.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
429 * </p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
430 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
431 * @return a Reader to a string representation of the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
432 * source JDOM document.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
433 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
434 public Reader getCharacterStream() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
435 Object src = this.getSource();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
436 Reader reader = null;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
437
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
438 if (src instanceof Document) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
439 // Get an in-memory string representation of the document
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
440 // and return a reader on it.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
441 reader = new StringReader(
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
442 new XMLOutputter().outputString((Document)src));
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
443 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
444 else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
445 if (src instanceof List) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
446 reader = new StringReader(
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
447 new XMLOutputter().outputString((List)src));
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
448 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
449 // Else: No source, no reader!
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
450 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
451 return reader;
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
452 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
453 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
454
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
455 //=========================================================================
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
456 // DocumentReader nested class
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
457 //=========================================================================
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
458
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
459 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
460 * An implementation of the SAX2 XMLReader interface that presents
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
461 * a SAX view of a JDOM Document. The actual generation of the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
462 * SAX events is delegated to JDOM's SAXOutputter.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
463 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
464 * @see org.jdom.Document
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
465 * @see org.jdom.output.SAXOutputter
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
466 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
467 private static class DocumentReader extends SAXOutputter
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
468 implements XMLReader {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
469 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
470 * Public default constructor.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
471 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
472 public DocumentReader() {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
473 super();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
474 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
475
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
476 //----------------------------------------------------------------------
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
477 // SAX XMLReader interface support
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
478 //----------------------------------------------------------------------
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
479
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
480 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
481 * Parses an XML document from a system identifier (URI).
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
482 * <p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
483 * This implementation does not support reading XML data from
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
484 * system identifiers, only from JDOM documents. Hence,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
485 * this method always throws a {@link SAXNotSupportedException}.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
486 * </p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
487 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
488 * @param systemId the system identifier (URI).
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
489 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
490 * @throws SAXNotSupportedException always!
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
491 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
492 public void parse(String systemId) throws SAXNotSupportedException {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
493 throw new SAXNotSupportedException(
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
494 "Only JDOM Documents are supported as input");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
495 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
496
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
497 /**
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
498 * Parses an XML document.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
499 * <p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
500 * The methods accepts only <code>JDOMInputSource</code>s
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
501 * instances as input sources.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
502 * </p>
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
503 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
504 * @param input the input source for the top-level of the
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
505 * XML document.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
506 *
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
507 * @throws SAXException any SAX exception,
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
508 * possibly wrapping
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
509 * another exception.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
510 * @throws SAXNotSupportedException if the input source does
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
511 * not wrap a JDOM document.
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
512 */
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
513 public void parse(InputSource input) throws SAXException {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
514 if (input instanceof JDOMInputSource) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
515 try {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
516 Object source = ((JDOMInputSource)input).getSource();
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
517 if (source instanceof Document) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
518 this.output((Document)source);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
519 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
520 else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
521 this.output((List)source);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
522 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
523 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
524 catch (JDOMException e) {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
525 throw new SAXException(e.getMessage(), e);
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
526 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
527 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
528 else {
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
529 throw new SAXNotSupportedException(
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
530 "Only JDOM Documents are supported as input");
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
531 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
532 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
533 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
534 }
89ad0a9cca52 Uploaded
pfrommolt
parents:
diff changeset
535