Tue, 06 Mar 2012 16:09:35 -0800
7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom
1 /*
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.xml.internal.ws.util.xml;
28 import com.sun.istack.internal.NotNull;
29 import com.sun.istack.internal.SAXParseException2;
30 import com.sun.istack.internal.XMLStreamReaderToContentHandler;
31 import org.xml.sax.*;
32 import org.xml.sax.ext.LexicalHandler;
33 import org.xml.sax.helpers.XMLFilterImpl;
35 import javax.xml.stream.XMLStreamConstants;
36 import javax.xml.stream.XMLStreamException;
37 import javax.xml.stream.XMLStreamReader;
38 import javax.xml.transform.sax.SAXSource;
40 /**
41 * A JAXP {@link javax.xml.transform.Source} implementation that wraps
42 * the specified {@link javax.xml.stream.XMLStreamReader} or
43 * {@link javax.xml.stream.XMLEventReader} for use by applications that
44 * expext a {@link javax.xml.transform.Source}.
45 *
46 * <p>
47 * The fact that StAXSource derives from SAXSource is an implementation
48 * detail. Thus in general applications are strongly discouraged from
49 * accessing methods defined on SAXSource. In particular:
50 *
51 * <ul>
52 * <li> The setXMLReader and setInputSource methods shall never be called.</li>
53 * <li> The XMLReader object obtained by the getXMLReader method shall
54 * be used only for parsing the InputSource object returned by
55 * the getInputSource method.</li>
56 * <li> The InputSource object obtained by the getInputSource method shall
57 * be used only for being parsed by the XMLReader object returned by
58 * the getXMLReader method.</li>
59 * </ul>
60 *
61 * <p>
62 * Example:
63 *
64 * <pre>
65 // create a StAXSource
66 XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new FileReader(args[0]));
67 Source staxSource = new StAXSource(reader);
69 // createa StreamResult
70 Result streamResult = new StreamResult(System.out);
72 // run the transform
73 TransformerFactory.newInstance().newTransformer().transform(staxSource, streamResult);
74 * </pre>
75 *
76 * @author Ryan.Shoemaker@Sun.COM
77 * @version 1.0
78 */
79 public class StAXSource extends SAXSource {
81 // StAX to SAX converter that will read from StAX and produce SAX
82 // this object will be wrapped by the XMLReader exposed to the client
83 private final XMLStreamReaderToContentHandler reader;
85 private final XMLStreamReader staxReader;
87 // SAX allows ContentHandler to be changed during the parsing,
88 // but JAXB doesn't. So this repeater will sit between those
89 // two components.
90 private XMLFilterImpl repeater = new XMLFilterImpl();
92 // this object will pretend as an XMLReader.
93 // no matter what parameter is specified to the parse method,
94 // it will just read from the StAX reader.
95 private final XMLReader pseudoParser = new XMLReader() {
96 public boolean getFeature(String name) throws SAXNotRecognizedException {
97 throw new SAXNotRecognizedException(name);
98 }
100 public void setFeature(String name, boolean value) throws SAXNotRecognizedException {
101 // Should support these two features according to XMLReader javadoc.
102 if (name.equals("http://xml.org/sax/features/namespaces") && value) {
103 // Ignore for now
104 } else if (name.equals("http://xml.org/sax/features/namespace-prefixes") && !value) {
105 // Ignore for now
106 } else {
107 throw new SAXNotRecognizedException(name);
108 }
109 }
111 public Object getProperty(String name) throws SAXNotRecognizedException {
112 if( "http://xml.org/sax/properties/lexical-handler".equals(name) ) {
113 return lexicalHandler;
114 }
115 throw new SAXNotRecognizedException(name);
116 }
118 public void setProperty(String name, Object value) throws SAXNotRecognizedException {
119 if( "http://xml.org/sax/properties/lexical-handler".equals(name) ) {
120 this.lexicalHandler = (LexicalHandler)value;
121 return;
122 }
123 throw new SAXNotRecognizedException(name);
124 }
126 private LexicalHandler lexicalHandler;
128 // we will store this value but never use it by ourselves.
129 private EntityResolver entityResolver;
130 public void setEntityResolver(EntityResolver resolver) {
131 this.entityResolver = resolver;
132 }
133 public EntityResolver getEntityResolver() {
134 return entityResolver;
135 }
137 private DTDHandler dtdHandler;
138 public void setDTDHandler(DTDHandler handler) {
139 this.dtdHandler = handler;
140 }
141 public DTDHandler getDTDHandler() {
142 return dtdHandler;
143 }
145 public void setContentHandler(ContentHandler handler) {
146 repeater.setContentHandler(handler);
147 }
148 public ContentHandler getContentHandler() {
149 return repeater.getContentHandler();
150 }
152 private ErrorHandler errorHandler;
153 public void setErrorHandler(ErrorHandler handler) {
154 this.errorHandler = handler;
155 }
156 public ErrorHandler getErrorHandler() {
157 return errorHandler;
158 }
160 public void parse(InputSource input) throws SAXException {
161 parse();
162 }
164 public void parse(String systemId) throws SAXException {
165 parse();
166 }
168 public void parse() throws SAXException {
169 // parses from a StAX reader and generates SAX events which
170 // go through the repeater and are forwarded to the appropriate
171 // component
172 try {
173 reader.bridge();
174 } catch( XMLStreamException e ) {
175 // wrap it in a SAXException
176 SAXParseException se =
177 new SAXParseException2(
178 e.getMessage(),
179 null,
180 null,
181 e.getLocation() == null ? -1 : e.getLocation().getLineNumber(),
182 e.getLocation() == null ? -1 : e.getLocation().getColumnNumber(),
183 e);
185 // if the consumer sets an error handler, it is our responsibility
186 // to notify it.
187 if(errorHandler!=null)
188 errorHandler.fatalError(se);
190 // this is a fatal error. Even if the error handler
191 // returns, we will abort anyway.
192 throw se;
194 } finally {
195 try {
196 staxReader.close();
197 } catch(XMLStreamException xe) {
198 //falls through. Not much can be done.
199 }
200 }
201 }
202 };
204 /**
205 * Creates a new {@link javax.xml.transform.Source} for the given
206 * {@link XMLStreamReader}.
207 *
208 * @param reader XMLStreamReader that will be exposed as a Source
209 * @param eagerQuit if true, when the conversion is completed, leave the cursor to the last
210 * event that was fired (such as end element)
211 * @see #StAXSource(XMLStreamReader, boolean, String[])
212 */
213 public StAXSource(XMLStreamReader reader, boolean eagerQuit) {
214 this(reader, eagerQuit, new String[0]);
215 }
217 /**
218 * Creates a new {@link javax.xml.transform.Source} for the given
219 * {@link XMLStreamReader}.
220 *
221 * The XMLStreamReader must be pointing at either a
222 * {@link javax.xml.stream.XMLStreamConstants#START_DOCUMENT} or
223 * {@link javax.xml.stream.XMLStreamConstants#START_ELEMENT} event.
224 *
225 * @param reader XMLStreamReader that will be exposed as a Source
226 * @param eagerQuit if true, when the conversion is completed, leave the cursor to the last
227 * event that was fired (such as end element)
228 * @param inscope inscope Namespaces
229 * array of the even length of the form { prefix0, uri0, prefix1, uri1, ... }
230 * @throws IllegalArgumentException iff the reader is null
231 * @throws IllegalStateException iff the reader is not pointing at either a
232 * START_DOCUMENT or START_ELEMENT event
233 */
234 public StAXSource(XMLStreamReader reader, boolean eagerQuit, @NotNull String[] inscope) {
235 if( reader == null )
236 throw new IllegalArgumentException();
237 this.staxReader = reader;
239 int eventType = reader.getEventType();
240 if (!(eventType == XMLStreamConstants.START_DOCUMENT)
241 && !(eventType == XMLStreamConstants.START_ELEMENT)) {
242 throw new IllegalStateException();
243 }
245 this.reader = new XMLStreamReaderToContentHandler(reader,repeater,eagerQuit,false,inscope);
247 super.setXMLReader(pseudoParser);
248 // pass a dummy InputSource. We don't care
249 super.setInputSource(new InputSource());
250 }
252 // /**
253 // * Creates a new {@link javax.xml.transform.Source} for the given
254 // * {@link XMLEventReader}.
255 // *
256 // * The XMLEventReader must be pointing at either a
257 // * {@link javax.xml.stream.XMLStreamConstants#START_DOCUMENT} or
258 // * {@link javax.xml.stream.XMLStreamConstants#START_ELEMENT} event.
259 // *
260 // * @param reader XMLEventReader that will be exposed as a Source
261 // * @throws IllegalArgumentException iff the reader is null
262 // * @throws IllegalStateException iff the reader is not pointing at either a
263 // * START_DOCUEMENT or START_ELEMENT event
264 // */
265 // public StAXSource(XMLEventReader reader) {
266 // if( reader == null )
267 // throw new IllegalArgumentException();
268 //
269 // // TODO: detect IllegalStateException for START_ELEMENT|DOCUMENT
270 // // bugid 5046340 - peek not implemented
271 // // XMLEvent event = staxEventReader.peek();
272 //
273 // this.reader =
274 // new XMLEventReaderToContentHandler(
275 // reader,
276 // repeater);
277 //
278 // super.setXMLReader(pseudoParser);
279 // // pass a dummy InputSource. We don't care
280 // super.setInputSource(new InputSource());
281 // }
283 }