KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > xml > transform > stax > StAXSource


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://jaxp.dev.java.net/CDDLv1.0.html.
9  * See the License for the specific language governing
10  * permissions and limitations under the License.
11  *
12  * When distributing Covered Code, include this CDDL
13  * HEADER in each file and include the License file at
14  * https://jaxp.dev.java.net/CDDLv1.0.html
15  * If applicable add the following below this CDDL HEADER
16  * with the fields enclosed by brackets "[]" replaced with
17  * your own identifying information: Portions Copyright
18  * [year] [name of copyright owner]
19  */

20
21 /*
22  * $Id: StAXSource.java,v 1.4 2006/04/24 13:42:27 ndw Exp $
23  * @(#)StAXSource.java 1.11 06/07/13
24  *
25  * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package javax.xml.transform.stax;
29
30 import javax.xml.stream.XMLEventReader;
31 import javax.xml.stream.XMLStreamConstants;
32 import javax.xml.stream.XMLStreamException;
33 import javax.xml.stream.XMLStreamReader;
34 import javax.xml.stream.events.XMLEvent;
35 import javax.xml.transform.Source JavaDoc;
36
37 /**
38  * <p>Acts as a holder for an XML {@link Source} in the
39  * form of a StAX reader,i.e.
40  * {@link XMLStreamReader} or {@link XMLEventReader}.
41  * <code>StAXSource</code> can be used in all cases that accept
42  * a <code>Source</code>, e.g. {@link javax.xml.transform.Transformer},
43  * {@link javax.xml.validation.Validator} which accept
44  * <code>Source</code> as input.
45  *
46  * <p><code>StAXSource</code>s are consumed during processing
47  * and are not reusable.</p>
48  *
49  * @author <a HREF="mailto:Neeraj.Bajaj@Sun.com">Neeraj Bajaj</a>
50  * @author <a HREF="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
51  * @version $Revision: 1.4 $, $Date: 2006/04/24 13:42:27 $
52  *
53  * @see <a HREF="http://jcp.org/en/jsr/detail?id=173">
54  * JSR 173: Streaming API for XML</a>
55  * @see XMLStreamReader
56  * @see XMLEventReader
57  *
58  * @since 1.6
59  */

60 public class StAXSource implements Source JavaDoc {
61
62     /** If {@link javax.xml.transform.TransformerFactory#getFeature(String name)}
63      * returns true when passed this value as an argument,
64      * the Transformer supports Source input of this type.
65      */

66     public static final String JavaDoc FEATURE =
67         "http://javax.xml.transform.stax.StAXSource/feature";
68
69     /** <p><code>XMLEventReader</code> to be used for source input.</p> */
70     private XMLEventReader xmlEventReader = null;
71
72     /** <p><code>XMLStreamReader</code> to be used for source input.</p> */
73     private XMLStreamReader xmlStreamReader = null;
74
75     /** <p>System identifier of source input.</p> */
76     private String JavaDoc systemId = null;
77
78     /**
79      * <p>Creates a new instance of a <code>StAXSource</code>
80      * by supplying an {@link XMLEventReader}.</p>
81      *
82      * <p><code>XMLEventReader</code> must be a
83      * non-<code>null</code> reference.</p>
84      *
85      * <p><code>XMLEventReader</code> must be in
86      * {@link XMLStreamConstants#START_DOCUMENT} or
87      * {@link XMLStreamConstants#START_ELEMENT} state.</p>
88      *
89      * @param xmlEventReader <code>XMLEventReader</code> used to create
90      * this <code>StAXSource</code>.
91      *
92      * @throws XMLStreamException If <code>xmlEventReader</code> access
93      * throws an <code>Exception</code>.
94      * @throws IllegalArgumentException If <code>xmlEventReader</code> ==
95      * <code>null</code>.
96      * @throws IllegalStateException If <code>xmlEventReader</code>
97      * is not in <code>XMLStreamConstants.START_DOCUMENT</code> or
98      * <code>XMLStreamConstants.START_ELEMENT</code> state.
99      */

100     public StAXSource(final XMLEventReader xmlEventReader)
101         throws XMLStreamException {
102
103         if (xmlEventReader == null) {
104             throw new IllegalArgumentException JavaDoc(
105                     "StAXSource(XMLEventReader) with XMLEventReader == null");
106         }
107
108         // TODO: This is ugly ...
109
// there is no way to know the current position(event) of
110
// XMLEventReader. peek() is the only way to know the next event.
111
// The next event on the input stream should be
112
// XMLStreamConstants.START_DOCUMENT or
113
// XMLStreamConstants.START_ELEMENT.
114
XMLEvent event = xmlEventReader.peek();
115         int eventType = event.getEventType();
116         if (eventType != XMLStreamConstants.START_DOCUMENT
117                 && eventType != XMLStreamConstants.START_ELEMENT) {
118             throw new IllegalStateException JavaDoc(
119                 "StAXSource(XMLEventReader) with XMLEventReader "
120                 + "not in XMLStreamConstants.START_DOCUMENT or "
121                 + "XMLStreamConstants.START_ELEMENT state");
122         }
123
124         this.xmlEventReader = xmlEventReader;
125         systemId = event.getLocation().getSystemId();
126     }
127
128     /**
129      * <p>Creates a new instance of a <code>StAXSource</code>
130      * by supplying an {@link XMLStreamReader}.</p>
131      *
132      * <p><code>XMLStreamReader</code> must be a
133      * non-<code>null</code> reference.</p>
134      *
135      * <p><code>XMLStreamReader</code> must be in
136      * {@link XMLStreamConstants#START_DOCUMENT} or
137      * {@link XMLStreamConstants#START_ELEMENT} state.</p>
138      *
139      * @param xmlStreamReader <code>XMLStreamReader</code> used to create
140      * this <code>StAXSource</code>.
141      *
142      * @throws IllegalArgumentException If <code>xmlStreamReader</code> ==
143      * <code>null</code>.
144      * @throws IllegalStateException If <code>xmlStreamReader</code>
145      * is not in <code>XMLStreamConstants.START_DOCUMENT</code> or
146      * <code>XMLStreamConstants.START_ELEMENT</code> state.
147      */

148     public StAXSource(final XMLStreamReader xmlStreamReader) {
149
150         if (xmlStreamReader == null) {
151             throw new IllegalArgumentException JavaDoc(
152                     "StAXSource(XMLStreamReader) with XMLStreamReader == null");
153         }
154
155         int eventType = xmlStreamReader.getEventType();
156         if (eventType != XMLStreamConstants.START_DOCUMENT
157                 && eventType != XMLStreamConstants.START_ELEMENT) {
158             throw new IllegalStateException JavaDoc(
159                     "StAXSource(XMLStreamReader) with XMLStreamReader"
160                     + "not in XMLStreamConstants.START_DOCUMENT or "
161                     + "XMLStreamConstants.START_ELEMENT state");
162         }
163
164         this.xmlStreamReader = xmlStreamReader;
165         systemId = xmlStreamReader.getLocation().getSystemId();
166     }
167
168     /**
169      * <p>Get the <code>XMLEventReader</code> used by this
170      * <code>StAXSource</code>.</p>
171      *
172      * <p><code>XMLEventReader</code> will be <code>null</code>.
173      * if this <code>StAXSource</code> was created with a
174      * <code>XMLStreamReader</code>.</p>
175      *
176      * @return <code>XMLEventReader</code> used by this
177      * <code>StAXSource</code>.
178      */

179     public XMLEventReader getXMLEventReader() {
180
181         return xmlEventReader;
182     }
183
184     /**
185      * <p>Get the <code>XMLStreamReader</code> used by this
186      * <code>StAXSource</code>.</p>
187      *
188      * <p><code>XMLStreamReader</code> will be <code>null</code>
189      * if this <code>StAXSource</code> was created with a
190      * <code>XMLEventReader</code>.</p>
191      *
192      * @return <code>XMLStreamReader</code> used by this
193      * <code>StAXSource</code>.
194      */

195     public XMLStreamReader getXMLStreamReader() {
196
197         return xmlStreamReader;
198     }
199
200     /**
201      * <p>In the context of a <code>StAXSource</code>, it is not appropriate
202      * to explicitly set the system identifier.
203      * The <code>XMLStreamReader</code> or <code>XMLEventReader</code>
204      * used to construct this <code>StAXSource</code> determines the
205      * system identifier of the XML source.</p>
206      *
207      * <p>An {@link UnsupportedOperationException} is <strong>always</strong>
208      * thrown by this method.</p>
209      *
210      * @param systemId Ignored.
211      *
212      * @throws UnsupportedOperationException Is <strong>always</strong>
213      * thrown by this method.
214      */

215     public void setSystemId(final String JavaDoc systemId) {
216
217         throw new UnsupportedOperationException JavaDoc(
218                 "StAXSource#setSystemId(systemId) cannot set the "
219                 + "system identifier for a StAXSource");
220     }
221
222     /**
223      * <p>Get the system identifier used by this
224      * <code>StAXSource</code>.</p>
225      *
226      * <p>The <code>XMLStreamReader</code> or <code>XMLEventReader</code>
227      * used to construct this <code>StAXSource</code> is queried to determine
228      * the system identifier of the XML source.</p>
229      *
230      * <p>The system identifier may be <code>null</code> or
231      * an empty <code>""</code> <code>String</code>.</p>
232      *
233      * @return System identifier used by this <code>StAXSource</code>.
234      */

235     public String JavaDoc getSystemId() {
236
237         return systemId;
238     }
239 }
240
Popular Tags