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