Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2012, 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.bind.v2.runtime.unmarshaller; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.io.IOException; |
aoqi@0 | 29 | import java.io.InputStream; |
aoqi@0 | 30 | |
aoqi@0 | 31 | import javax.xml.bind.JAXBContext; |
aoqi@0 | 32 | import javax.xml.bind.JAXBElement; |
aoqi@0 | 33 | import javax.xml.bind.JAXBException; |
aoqi@0 | 34 | import javax.xml.bind.PropertyException; |
aoqi@0 | 35 | import javax.xml.bind.UnmarshalException; |
aoqi@0 | 36 | import javax.xml.bind.Unmarshaller; |
aoqi@0 | 37 | import javax.xml.bind.UnmarshallerHandler; |
aoqi@0 | 38 | import javax.xml.bind.ValidationEvent; |
aoqi@0 | 39 | import javax.xml.bind.ValidationEventHandler; |
aoqi@0 | 40 | import javax.xml.bind.annotation.adapters.XmlAdapter; |
aoqi@0 | 41 | import javax.xml.bind.attachment.AttachmentUnmarshaller; |
aoqi@0 | 42 | import javax.xml.bind.helpers.AbstractUnmarshallerImpl; |
aoqi@0 | 43 | import javax.xml.stream.XMLEventReader; |
aoqi@0 | 44 | import javax.xml.stream.XMLStreamConstants; |
aoqi@0 | 45 | import javax.xml.stream.XMLStreamException; |
aoqi@0 | 46 | import javax.xml.stream.XMLStreamReader; |
aoqi@0 | 47 | import javax.xml.stream.events.XMLEvent; |
aoqi@0 | 48 | import javax.xml.transform.Source; |
aoqi@0 | 49 | import javax.xml.transform.dom.DOMSource; |
aoqi@0 | 50 | import javax.xml.transform.sax.SAXSource; |
aoqi@0 | 51 | import javax.xml.transform.stream.StreamSource; |
aoqi@0 | 52 | import javax.xml.validation.Schema; |
aoqi@0 | 53 | |
aoqi@0 | 54 | import com.sun.xml.internal.bind.IDResolver; |
aoqi@0 | 55 | import com.sun.xml.internal.bind.api.ClassResolver; |
aoqi@0 | 56 | import com.sun.xml.internal.bind.unmarshaller.DOMScanner; |
aoqi@0 | 57 | import com.sun.xml.internal.bind.unmarshaller.InfosetScanner; |
aoqi@0 | 58 | import com.sun.xml.internal.bind.unmarshaller.Messages; |
aoqi@0 | 59 | import com.sun.xml.internal.bind.v2.ClassFactory; |
aoqi@0 | 60 | import com.sun.xml.internal.bind.v2.runtime.AssociationMap; |
aoqi@0 | 61 | import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl; |
aoqi@0 | 62 | import com.sun.xml.internal.bind.v2.runtime.JaxBeanInfo; |
aoqi@0 | 63 | import com.sun.xml.internal.bind.v2.util.XmlFactory; |
aoqi@0 | 64 | |
aoqi@0 | 65 | import java.io.Closeable; |
aoqi@0 | 66 | import javax.xml.parsers.ParserConfigurationException; |
aoqi@0 | 67 | import javax.xml.parsers.SAXParserFactory; |
aoqi@0 | 68 | import org.w3c.dom.Document; |
aoqi@0 | 69 | import org.w3c.dom.Element; |
aoqi@0 | 70 | import org.w3c.dom.Node; |
aoqi@0 | 71 | import org.xml.sax.InputSource; |
aoqi@0 | 72 | import org.xml.sax.SAXException; |
aoqi@0 | 73 | import org.xml.sax.XMLReader; |
aoqi@0 | 74 | import org.xml.sax.helpers.DefaultHandler; |
aoqi@0 | 75 | |
aoqi@0 | 76 | /** |
aoqi@0 | 77 | * Default Unmarshaller implementation. |
aoqi@0 | 78 | * |
aoqi@0 | 79 | * <p> |
aoqi@0 | 80 | * This class can be extended by the generated code to provide |
aoqi@0 | 81 | * type-safe unmarshall methods. |
aoqi@0 | 82 | * |
aoqi@0 | 83 | * @author |
aoqi@0 | 84 | * <a href="mailto:kohsuke.kawaguchi@sun.com">Kohsuke KAWAGUCHI</a> |
aoqi@0 | 85 | */ |
aoqi@0 | 86 | public final class UnmarshallerImpl extends AbstractUnmarshallerImpl implements ValidationEventHandler, Closeable |
aoqi@0 | 87 | { |
aoqi@0 | 88 | /** Owning {@link JAXBContext} */ |
aoqi@0 | 89 | protected final JAXBContextImpl context; |
aoqi@0 | 90 | |
aoqi@0 | 91 | /** |
aoqi@0 | 92 | * schema which will be used to validate during calls to unmarshal |
aoqi@0 | 93 | */ |
aoqi@0 | 94 | private Schema schema; |
aoqi@0 | 95 | |
aoqi@0 | 96 | public final UnmarshallingContext coordinator; |
aoqi@0 | 97 | |
aoqi@0 | 98 | /** Unmarshaller.Listener */ |
aoqi@0 | 99 | private Listener externalListener; |
aoqi@0 | 100 | |
aoqi@0 | 101 | /** |
aoqi@0 | 102 | * The attachment unmarshaller used to support MTOM and swaRef. |
aoqi@0 | 103 | */ |
aoqi@0 | 104 | private AttachmentUnmarshaller attachmentUnmarshaller; |
aoqi@0 | 105 | private IDResolver idResolver = new DefaultIDResolver(); |
aoqi@0 | 106 | |
aoqi@0 | 107 | public UnmarshallerImpl( JAXBContextImpl context, AssociationMap assoc ) { |
aoqi@0 | 108 | this.context = context; |
aoqi@0 | 109 | this.coordinator = new UnmarshallingContext( this, assoc ); |
aoqi@0 | 110 | |
aoqi@0 | 111 | try { |
aoqi@0 | 112 | setEventHandler(this); |
aoqi@0 | 113 | } catch (JAXBException e) { |
aoqi@0 | 114 | throw new AssertionError(e); // impossible |
aoqi@0 | 115 | } |
aoqi@0 | 116 | } |
aoqi@0 | 117 | |
aoqi@0 | 118 | public UnmarshallerHandler getUnmarshallerHandler() { |
aoqi@0 | 119 | return getUnmarshallerHandler(true,null); |
aoqi@0 | 120 | } |
aoqi@0 | 121 | |
aoqi@0 | 122 | private XMLReader reader = null; |
aoqi@0 | 123 | |
aoqi@0 | 124 | /** |
aoqi@0 | 125 | * Obtains a configured XMLReader. |
aoqi@0 | 126 | * |
aoqi@0 | 127 | * This method is used when the client-specified |
aoqi@0 | 128 | * {@link SAXSource} object doesn't have XMLReader. |
aoqi@0 | 129 | * |
aoqi@0 | 130 | * {@link Unmarshaller} is not re-entrant, so we will |
aoqi@0 | 131 | * only use one instance of XMLReader. |
aoqi@0 | 132 | * |
aoqi@0 | 133 | * Overriden in order to fix potential security issue. |
aoqi@0 | 134 | */ |
aoqi@0 | 135 | @Override |
aoqi@0 | 136 | protected XMLReader getXMLReader() throws JAXBException { |
aoqi@0 | 137 | if (reader == null) { |
aoqi@0 | 138 | try { |
aoqi@0 | 139 | SAXParserFactory parserFactory = XmlFactory.createParserFactory(context.disableSecurityProcessing); |
aoqi@0 | 140 | // there is no point in asking a validation because |
aoqi@0 | 141 | // there is no guarantee that the document will come with |
aoqi@0 | 142 | // a proper schemaLocation. |
aoqi@0 | 143 | parserFactory.setValidating(false); |
aoqi@0 | 144 | reader = parserFactory.newSAXParser().getXMLReader(); |
aoqi@0 | 145 | } catch (ParserConfigurationException e) { |
aoqi@0 | 146 | throw new JAXBException(e); |
aoqi@0 | 147 | } catch (SAXException e) { |
aoqi@0 | 148 | throw new JAXBException(e); |
aoqi@0 | 149 | } |
aoqi@0 | 150 | } |
aoqi@0 | 151 | return reader; |
aoqi@0 | 152 | } |
aoqi@0 | 153 | |
aoqi@0 | 154 | private SAXConnector getUnmarshallerHandler( boolean intern, JaxBeanInfo expectedType ) { |
aoqi@0 | 155 | XmlVisitor h = createUnmarshallerHandler(null, false, expectedType); |
aoqi@0 | 156 | if (intern) { |
aoqi@0 | 157 | h = new InterningXmlVisitor(h); |
aoqi@0 | 158 | } |
aoqi@0 | 159 | return new SAXConnector(h,null); |
aoqi@0 | 160 | } |
aoqi@0 | 161 | |
aoqi@0 | 162 | /** |
aoqi@0 | 163 | * Creates and configures a new unmarshalling pipe line. |
aoqi@0 | 164 | * Depending on the setting, we put a validator as a filter. |
aoqi@0 | 165 | * |
aoqi@0 | 166 | * @return |
aoqi@0 | 167 | * A component that implements both {@link UnmarshallerHandler} |
aoqi@0 | 168 | * and {@link ValidationEventHandler}. All the parsing errors |
aoqi@0 | 169 | * should be reported to this error handler for the unmarshalling |
aoqi@0 | 170 | * process to work correctly. |
aoqi@0 | 171 | * |
aoqi@0 | 172 | * Also, returned handler expects all the XML names to be interned. |
aoqi@0 | 173 | * |
aoqi@0 | 174 | */ |
aoqi@0 | 175 | public final XmlVisitor createUnmarshallerHandler(InfosetScanner scanner, boolean inplace, JaxBeanInfo expectedType ) { |
aoqi@0 | 176 | |
aoqi@0 | 177 | coordinator.reset(scanner,inplace,expectedType,idResolver); |
aoqi@0 | 178 | XmlVisitor unmarshaller = coordinator; |
aoqi@0 | 179 | |
aoqi@0 | 180 | // delegate to JAXP 1.3 for validation if the client provided a schema |
aoqi@0 | 181 | if (schema != null) { |
aoqi@0 | 182 | unmarshaller = new ValidatingUnmarshaller(schema,unmarshaller); |
aoqi@0 | 183 | } |
aoqi@0 | 184 | |
aoqi@0 | 185 | if(attachmentUnmarshaller!=null && attachmentUnmarshaller.isXOPPackage()) { |
aoqi@0 | 186 | unmarshaller = new MTOMDecorator(this,unmarshaller,attachmentUnmarshaller); |
aoqi@0 | 187 | } |
aoqi@0 | 188 | |
aoqi@0 | 189 | return unmarshaller; |
aoqi@0 | 190 | } |
aoqi@0 | 191 | |
aoqi@0 | 192 | private static final DefaultHandler dummyHandler = new DefaultHandler(); |
aoqi@0 | 193 | |
aoqi@0 | 194 | public static boolean needsInterning( XMLReader reader ) { |
aoqi@0 | 195 | // attempt to set it to true, which could fail |
aoqi@0 | 196 | try { |
aoqi@0 | 197 | reader.setFeature("http://xml.org/sax/features/string-interning",true); |
aoqi@0 | 198 | } catch (SAXException e) { |
aoqi@0 | 199 | // if it fails that's fine. we'll work around on our side |
aoqi@0 | 200 | } |
aoqi@0 | 201 | |
aoqi@0 | 202 | try { |
aoqi@0 | 203 | if (reader.getFeature("http://xml.org/sax/features/string-interning")) { |
aoqi@0 | 204 | return false; // no need for intern |
aoqi@0 | 205 | } |
aoqi@0 | 206 | } catch (SAXException e) { |
aoqi@0 | 207 | // unrecognized/unsupported |
aoqi@0 | 208 | } |
aoqi@0 | 209 | // otherwise we need intern |
aoqi@0 | 210 | return true; |
aoqi@0 | 211 | } |
aoqi@0 | 212 | |
aoqi@0 | 213 | protected Object unmarshal( XMLReader reader, InputSource source ) throws JAXBException { |
aoqi@0 | 214 | return unmarshal0(reader,source,null); |
aoqi@0 | 215 | } |
aoqi@0 | 216 | |
aoqi@0 | 217 | protected <T> JAXBElement<T> unmarshal( XMLReader reader, InputSource source, Class<T> expectedType ) throws JAXBException { |
aoqi@0 | 218 | if(expectedType==null) { |
aoqi@0 | 219 | throw new IllegalArgumentException(); |
aoqi@0 | 220 | } |
aoqi@0 | 221 | return (JAXBElement)unmarshal0(reader,source,getBeanInfo(expectedType)); |
aoqi@0 | 222 | } |
aoqi@0 | 223 | |
aoqi@0 | 224 | private Object unmarshal0( XMLReader reader, InputSource source, JaxBeanInfo expectedType ) throws JAXBException { |
aoqi@0 | 225 | |
aoqi@0 | 226 | SAXConnector connector = getUnmarshallerHandler(needsInterning(reader),expectedType); |
aoqi@0 | 227 | |
aoqi@0 | 228 | reader.setContentHandler(connector); |
aoqi@0 | 229 | // saxErrorHandler will be set by the getUnmarshallerHandler method. |
aoqi@0 | 230 | // configure XMLReader so that the error will be sent to it. |
aoqi@0 | 231 | // This is essential for the UnmarshallerHandler to be able to abort |
aoqi@0 | 232 | // unmarshalling when an error is found. |
aoqi@0 | 233 | // |
aoqi@0 | 234 | // Note that when this XMLReader is provided by the client code, |
aoqi@0 | 235 | // it might be already configured to call a client error handler. |
aoqi@0 | 236 | // This will clobber such handler, if any. |
aoqi@0 | 237 | // |
aoqi@0 | 238 | // Ryan noted that we might want to report errors to such a client |
aoqi@0 | 239 | // error handler as well. |
aoqi@0 | 240 | reader.setErrorHandler(coordinator); |
aoqi@0 | 241 | |
aoqi@0 | 242 | try { |
aoqi@0 | 243 | reader.parse(source); |
aoqi@0 | 244 | } catch( IOException e ) { |
aoqi@0 | 245 | coordinator.clearStates(); |
aoqi@0 | 246 | throw new UnmarshalException(e); |
aoqi@0 | 247 | } catch( SAXException e ) { |
aoqi@0 | 248 | coordinator.clearStates(); |
aoqi@0 | 249 | throw createUnmarshalException(e); |
aoqi@0 | 250 | } |
aoqi@0 | 251 | |
aoqi@0 | 252 | Object result = connector.getResult(); |
aoqi@0 | 253 | |
aoqi@0 | 254 | // avoid keeping unnecessary references too long to let the GC |
aoqi@0 | 255 | // reclaim more memory. |
aoqi@0 | 256 | // setting null upsets some parsers, so use a dummy instance instead. |
aoqi@0 | 257 | reader.setContentHandler(dummyHandler); |
aoqi@0 | 258 | reader.setErrorHandler(dummyHandler); |
aoqi@0 | 259 | |
aoqi@0 | 260 | return result; |
aoqi@0 | 261 | } |
aoqi@0 | 262 | |
aoqi@0 | 263 | @Override |
aoqi@0 | 264 | public <T> JAXBElement<T> unmarshal( Source source, Class<T> expectedType ) throws JAXBException { |
aoqi@0 | 265 | if (source instanceof SAXSource) { |
aoqi@0 | 266 | SAXSource ss = (SAXSource) source; |
aoqi@0 | 267 | |
aoqi@0 | 268 | XMLReader locReader = ss.getXMLReader(); |
aoqi@0 | 269 | if (locReader == null) { |
aoqi@0 | 270 | locReader = getXMLReader(); |
aoqi@0 | 271 | } |
aoqi@0 | 272 | |
aoqi@0 | 273 | return unmarshal(locReader, ss.getInputSource(), expectedType); |
aoqi@0 | 274 | } |
aoqi@0 | 275 | if (source instanceof StreamSource) { |
aoqi@0 | 276 | return unmarshal(getXMLReader(), streamSourceToInputSource((StreamSource) source), expectedType); |
aoqi@0 | 277 | } |
aoqi@0 | 278 | if (source instanceof DOMSource) { |
aoqi@0 | 279 | return unmarshal(((DOMSource) source).getNode(), expectedType); |
aoqi@0 | 280 | } |
aoqi@0 | 281 | |
aoqi@0 | 282 | // we don't handle other types of Source |
aoqi@0 | 283 | throw new IllegalArgumentException(); |
aoqi@0 | 284 | } |
aoqi@0 | 285 | |
aoqi@0 | 286 | public Object unmarshal0( Source source, JaxBeanInfo expectedType ) throws JAXBException { |
aoqi@0 | 287 | if (source instanceof SAXSource) { |
aoqi@0 | 288 | SAXSource ss = (SAXSource) source; |
aoqi@0 | 289 | |
aoqi@0 | 290 | XMLReader locReader = ss.getXMLReader(); |
aoqi@0 | 291 | if (locReader == null) { |
aoqi@0 | 292 | locReader = getXMLReader(); |
aoqi@0 | 293 | } |
aoqi@0 | 294 | |
aoqi@0 | 295 | return unmarshal0(locReader, ss.getInputSource(), expectedType); |
aoqi@0 | 296 | } |
aoqi@0 | 297 | if (source instanceof StreamSource) { |
aoqi@0 | 298 | return unmarshal0(getXMLReader(), streamSourceToInputSource((StreamSource) source), expectedType); |
aoqi@0 | 299 | } |
aoqi@0 | 300 | if (source instanceof DOMSource) { |
aoqi@0 | 301 | return unmarshal0(((DOMSource) source).getNode(), expectedType); |
aoqi@0 | 302 | } |
aoqi@0 | 303 | |
aoqi@0 | 304 | // we don't handle other types of Source |
aoqi@0 | 305 | throw new IllegalArgumentException(); |
aoqi@0 | 306 | } |
aoqi@0 | 307 | |
aoqi@0 | 308 | |
aoqi@0 | 309 | @Override |
aoqi@0 | 310 | public final ValidationEventHandler getEventHandler() { |
aoqi@0 | 311 | try { |
aoqi@0 | 312 | return super.getEventHandler(); |
aoqi@0 | 313 | } catch (JAXBException e) { |
aoqi@0 | 314 | // impossible |
aoqi@0 | 315 | throw new AssertionError(); |
aoqi@0 | 316 | } |
aoqi@0 | 317 | } |
aoqi@0 | 318 | |
aoqi@0 | 319 | /** |
aoqi@0 | 320 | * Returns true if an event handler is installed. |
aoqi@0 | 321 | * <p> |
aoqi@0 | 322 | * The default handler ignores any errors, and for that this method returns false. |
aoqi@0 | 323 | */ |
aoqi@0 | 324 | public final boolean hasEventHandler() { |
aoqi@0 | 325 | return getEventHandler()!=this; |
aoqi@0 | 326 | } |
aoqi@0 | 327 | |
aoqi@0 | 328 | @Override |
aoqi@0 | 329 | public <T> JAXBElement<T> unmarshal(Node node, Class<T> expectedType) throws JAXBException { |
aoqi@0 | 330 | if (expectedType == null) { |
aoqi@0 | 331 | throw new IllegalArgumentException(); |
aoqi@0 | 332 | } |
aoqi@0 | 333 | return (JAXBElement)unmarshal0(node,getBeanInfo(expectedType)); |
aoqi@0 | 334 | } |
aoqi@0 | 335 | |
aoqi@0 | 336 | public final Object unmarshal( Node node ) throws JAXBException { |
aoqi@0 | 337 | return unmarshal0(node,null); |
aoqi@0 | 338 | } |
aoqi@0 | 339 | |
aoqi@0 | 340 | // just to make the the test harness happy by making this method accessible |
aoqi@0 | 341 | @Deprecated |
aoqi@0 | 342 | public final Object unmarshal( SAXSource source ) throws JAXBException { |
aoqi@0 | 343 | return super.unmarshal(source); |
aoqi@0 | 344 | } |
aoqi@0 | 345 | |
aoqi@0 | 346 | public final Object unmarshal0( Node node, JaxBeanInfo expectedType ) throws JAXBException { |
aoqi@0 | 347 | try { |
aoqi@0 | 348 | final DOMScanner scanner = new DOMScanner(); |
aoqi@0 | 349 | |
aoqi@0 | 350 | InterningXmlVisitor handler = new InterningXmlVisitor(createUnmarshallerHandler(null,false,expectedType)); |
aoqi@0 | 351 | scanner.setContentHandler(new SAXConnector(handler,scanner)); |
aoqi@0 | 352 | |
aoqi@0 | 353 | if(node.getNodeType() == Node.ELEMENT_NODE) { |
aoqi@0 | 354 | scanner.scan((Element)node); |
aoqi@0 | 355 | } else if(node.getNodeType() == Node.DOCUMENT_NODE) { |
aoqi@0 | 356 | scanner.scan((Document)node); |
aoqi@0 | 357 | } else { |
aoqi@0 | 358 | throw new IllegalArgumentException("Unexpected node type: "+node); |
aoqi@0 | 359 | } |
aoqi@0 | 360 | |
aoqi@0 | 361 | Object retVal = handler.getContext().getResult(); |
aoqi@0 | 362 | handler.getContext().clearResult(); |
aoqi@0 | 363 | return retVal; |
aoqi@0 | 364 | } catch( SAXException e ) { |
aoqi@0 | 365 | throw createUnmarshalException(e); |
aoqi@0 | 366 | } |
aoqi@0 | 367 | } |
aoqi@0 | 368 | |
aoqi@0 | 369 | @Override |
aoqi@0 | 370 | public Object unmarshal(XMLStreamReader reader) throws JAXBException { |
aoqi@0 | 371 | return unmarshal0(reader,null); |
aoqi@0 | 372 | } |
aoqi@0 | 373 | |
aoqi@0 | 374 | @Override |
aoqi@0 | 375 | public <T> JAXBElement<T> unmarshal(XMLStreamReader reader, Class<T> expectedType) throws JAXBException { |
aoqi@0 | 376 | if (expectedType==null) { |
aoqi@0 | 377 | throw new IllegalArgumentException(); |
aoqi@0 | 378 | } |
aoqi@0 | 379 | return (JAXBElement)unmarshal0(reader,getBeanInfo(expectedType)); |
aoqi@0 | 380 | } |
aoqi@0 | 381 | |
aoqi@0 | 382 | public Object unmarshal0(XMLStreamReader reader, JaxBeanInfo expectedType) throws JAXBException { |
aoqi@0 | 383 | if (reader == null) { |
aoqi@0 | 384 | throw new IllegalArgumentException( |
aoqi@0 | 385 | Messages.format(Messages.NULL_READER)); |
aoqi@0 | 386 | } |
aoqi@0 | 387 | |
aoqi@0 | 388 | int eventType = reader.getEventType(); |
aoqi@0 | 389 | if (eventType != XMLStreamConstants.START_ELEMENT |
aoqi@0 | 390 | && eventType != XMLStreamConstants.START_DOCUMENT) { |
aoqi@0 | 391 | // TODO: convert eventType into event name |
aoqi@0 | 392 | throw new IllegalStateException( |
aoqi@0 | 393 | Messages.format(Messages.ILLEGAL_READER_STATE,eventType)); |
aoqi@0 | 394 | } |
aoqi@0 | 395 | |
aoqi@0 | 396 | XmlVisitor h = createUnmarshallerHandler(null,false,expectedType); |
aoqi@0 | 397 | StAXConnector connector=StAXStreamConnector.create(reader,h); |
aoqi@0 | 398 | |
aoqi@0 | 399 | try { |
aoqi@0 | 400 | connector.bridge(); |
aoqi@0 | 401 | } catch (XMLStreamException e) { |
aoqi@0 | 402 | throw handleStreamException(e); |
aoqi@0 | 403 | } |
aoqi@0 | 404 | |
aoqi@0 | 405 | Object retVal = h.getContext().getResult(); |
aoqi@0 | 406 | h.getContext().clearResult(); |
aoqi@0 | 407 | return retVal; |
aoqi@0 | 408 | } |
aoqi@0 | 409 | |
aoqi@0 | 410 | @Override |
aoqi@0 | 411 | public <T> JAXBElement<T> unmarshal(XMLEventReader reader, Class<T> expectedType) throws JAXBException { |
aoqi@0 | 412 | if(expectedType==null) { |
aoqi@0 | 413 | throw new IllegalArgumentException(); |
aoqi@0 | 414 | } |
aoqi@0 | 415 | return (JAXBElement)unmarshal0(reader,getBeanInfo(expectedType)); |
aoqi@0 | 416 | } |
aoqi@0 | 417 | |
aoqi@0 | 418 | @Override |
aoqi@0 | 419 | public Object unmarshal(XMLEventReader reader) throws JAXBException { |
aoqi@0 | 420 | return unmarshal0(reader,null); |
aoqi@0 | 421 | } |
aoqi@0 | 422 | |
aoqi@0 | 423 | private Object unmarshal0(XMLEventReader reader,JaxBeanInfo expectedType) throws JAXBException { |
aoqi@0 | 424 | if (reader == null) { |
aoqi@0 | 425 | throw new IllegalArgumentException( |
aoqi@0 | 426 | Messages.format(Messages.NULL_READER)); |
aoqi@0 | 427 | } |
aoqi@0 | 428 | |
aoqi@0 | 429 | try { |
aoqi@0 | 430 | XMLEvent event = reader.peek(); |
aoqi@0 | 431 | |
aoqi@0 | 432 | if (!event.isStartElement() && !event.isStartDocument()) { |
aoqi@0 | 433 | // TODO: convert event into event name |
aoqi@0 | 434 | throw new IllegalStateException( |
aoqi@0 | 435 | Messages.format( |
aoqi@0 | 436 | Messages.ILLEGAL_READER_STATE,event.getEventType())); |
aoqi@0 | 437 | } |
aoqi@0 | 438 | |
aoqi@0 | 439 | // Quick hack until SJSXP fixes 6270116 |
aoqi@0 | 440 | boolean isZephyr = reader.getClass().getName().equals("com.sun.xml.internal.stream.XMLReaderImpl"); |
aoqi@0 | 441 | XmlVisitor h = createUnmarshallerHandler(null,false,expectedType); |
aoqi@0 | 442 | if(!isZephyr) { |
aoqi@0 | 443 | h = new InterningXmlVisitor(h); |
aoqi@0 | 444 | } |
aoqi@0 | 445 | new StAXEventConnector(reader,h).bridge(); |
aoqi@0 | 446 | return h.getContext().getResult(); |
aoqi@0 | 447 | } catch (XMLStreamException e) { |
aoqi@0 | 448 | throw handleStreamException(e); |
aoqi@0 | 449 | } |
aoqi@0 | 450 | } |
aoqi@0 | 451 | |
aoqi@0 | 452 | public Object unmarshal0( InputStream input, JaxBeanInfo expectedType ) throws JAXBException { |
aoqi@0 | 453 | return unmarshal0(getXMLReader(),new InputSource(input),expectedType); |
aoqi@0 | 454 | } |
aoqi@0 | 455 | |
aoqi@0 | 456 | private static JAXBException handleStreamException(XMLStreamException e) { |
aoqi@0 | 457 | // StAXStreamConnector wraps SAXException to XMLStreamException. |
aoqi@0 | 458 | // XMLStreamException doesn't print its nested stack trace when it prints |
aoqi@0 | 459 | // its stack trace, so if we wrap XMLStreamException in JAXBException, |
aoqi@0 | 460 | // it becomes harder to find out the real problem. |
aoqi@0 | 461 | // So we unwrap them here. But we don't want to unwrap too eagerly, because |
aoqi@0 | 462 | // that could throw away some meaningful exception information. |
aoqi@0 | 463 | Throwable ne = e.getNestedException(); |
aoqi@0 | 464 | if(ne instanceof JAXBException) { |
aoqi@0 | 465 | return (JAXBException)ne; |
aoqi@0 | 466 | } |
aoqi@0 | 467 | if(ne instanceof SAXException) { |
aoqi@0 | 468 | return new UnmarshalException(ne); |
aoqi@0 | 469 | } |
aoqi@0 | 470 | return new UnmarshalException(e); |
aoqi@0 | 471 | } |
aoqi@0 | 472 | |
aoqi@0 | 473 | @Override |
aoqi@0 | 474 | public Object getProperty(String name) throws PropertyException { |
aoqi@0 | 475 | if(name.equals(IDResolver.class.getName())) { |
aoqi@0 | 476 | return idResolver; |
aoqi@0 | 477 | } |
aoqi@0 | 478 | return super.getProperty(name); |
aoqi@0 | 479 | } |
aoqi@0 | 480 | |
aoqi@0 | 481 | @Override |
aoqi@0 | 482 | public void setProperty(String name, Object value) throws PropertyException { |
aoqi@0 | 483 | if(name.equals(FACTORY)) { |
aoqi@0 | 484 | coordinator.setFactories(value); |
aoqi@0 | 485 | return; |
aoqi@0 | 486 | } |
aoqi@0 | 487 | if(name.equals(IDResolver.class.getName())) { |
aoqi@0 | 488 | idResolver = (IDResolver)value; |
aoqi@0 | 489 | return; |
aoqi@0 | 490 | } |
aoqi@0 | 491 | if(name.equals(ClassResolver.class.getName())) { |
aoqi@0 | 492 | coordinator.classResolver = (ClassResolver)value; |
aoqi@0 | 493 | return; |
aoqi@0 | 494 | } |
aoqi@0 | 495 | if(name.equals(ClassLoader.class.getName())) { |
aoqi@0 | 496 | coordinator.classLoader = (ClassLoader)value; |
aoqi@0 | 497 | return; |
aoqi@0 | 498 | } |
aoqi@0 | 499 | super.setProperty(name, value); |
aoqi@0 | 500 | } |
aoqi@0 | 501 | |
aoqi@0 | 502 | public static final String FACTORY = "com.sun.xml.internal.bind.ObjectFactory"; |
aoqi@0 | 503 | |
aoqi@0 | 504 | @Override |
aoqi@0 | 505 | public void setSchema(Schema schema) { |
aoqi@0 | 506 | this.schema = schema; |
aoqi@0 | 507 | } |
aoqi@0 | 508 | |
aoqi@0 | 509 | @Override |
aoqi@0 | 510 | public Schema getSchema() { |
aoqi@0 | 511 | return schema; |
aoqi@0 | 512 | } |
aoqi@0 | 513 | |
aoqi@0 | 514 | @Override |
aoqi@0 | 515 | public AttachmentUnmarshaller getAttachmentUnmarshaller() { |
aoqi@0 | 516 | return attachmentUnmarshaller; |
aoqi@0 | 517 | } |
aoqi@0 | 518 | |
aoqi@0 | 519 | @Override |
aoqi@0 | 520 | public void setAttachmentUnmarshaller(AttachmentUnmarshaller au) { |
aoqi@0 | 521 | this.attachmentUnmarshaller = au; |
aoqi@0 | 522 | } |
aoqi@0 | 523 | |
aoqi@0 | 524 | /** |
aoqi@0 | 525 | * @deprecated since 2.0 |
aoqi@0 | 526 | */ |
aoqi@0 | 527 | @Override |
aoqi@0 | 528 | public boolean isValidating() { |
aoqi@0 | 529 | throw new UnsupportedOperationException(); |
aoqi@0 | 530 | } |
aoqi@0 | 531 | |
aoqi@0 | 532 | /** |
aoqi@0 | 533 | * @deprecated since 2.0 |
aoqi@0 | 534 | */ |
aoqi@0 | 535 | @Override |
aoqi@0 | 536 | public void setValidating(boolean validating) { |
aoqi@0 | 537 | throw new UnsupportedOperationException(); |
aoqi@0 | 538 | } |
aoqi@0 | 539 | |
aoqi@0 | 540 | @Override |
aoqi@0 | 541 | public <A extends XmlAdapter> void setAdapter(Class<A> type, A adapter) { |
aoqi@0 | 542 | if (type==null) { |
aoqi@0 | 543 | throw new IllegalArgumentException(); |
aoqi@0 | 544 | } |
aoqi@0 | 545 | coordinator.putAdapter(type,adapter); |
aoqi@0 | 546 | } |
aoqi@0 | 547 | |
aoqi@0 | 548 | @Override |
aoqi@0 | 549 | public <A extends XmlAdapter> A getAdapter(Class<A> type) { |
aoqi@0 | 550 | if(type==null) { |
aoqi@0 | 551 | throw new IllegalArgumentException(); |
aoqi@0 | 552 | } |
aoqi@0 | 553 | if(coordinator.containsAdapter(type)) { |
aoqi@0 | 554 | return coordinator.getAdapter(type); |
aoqi@0 | 555 | } else { |
aoqi@0 | 556 | return null; |
aoqi@0 | 557 | } |
aoqi@0 | 558 | } |
aoqi@0 | 559 | |
aoqi@0 | 560 | // opening up for public use |
aoqi@0 | 561 | @Override |
aoqi@0 | 562 | public UnmarshalException createUnmarshalException( SAXException e ) { |
aoqi@0 | 563 | return super.createUnmarshalException(e); |
aoqi@0 | 564 | } |
aoqi@0 | 565 | |
aoqi@0 | 566 | |
aoqi@0 | 567 | /** |
aoqi@0 | 568 | * Default error handling behavior for {@link Unmarshaller}. |
aoqi@0 | 569 | */ |
aoqi@0 | 570 | public boolean handleEvent(ValidationEvent event) { |
aoqi@0 | 571 | return event.getSeverity()!=ValidationEvent.FATAL_ERROR; |
aoqi@0 | 572 | } |
aoqi@0 | 573 | |
aoqi@0 | 574 | private static InputSource streamSourceToInputSource( StreamSource ss ) { |
aoqi@0 | 575 | InputSource is = new InputSource(); |
aoqi@0 | 576 | is.setSystemId( ss.getSystemId() ); |
aoqi@0 | 577 | is.setByteStream( ss.getInputStream() ); |
aoqi@0 | 578 | is.setCharacterStream( ss.getReader() ); |
aoqi@0 | 579 | |
aoqi@0 | 580 | return is; |
aoqi@0 | 581 | } |
aoqi@0 | 582 | |
aoqi@0 | 583 | public <T> JaxBeanInfo<T> getBeanInfo(Class<T> clazz) throws JAXBException { |
aoqi@0 | 584 | return context.getBeanInfo(clazz,true); |
aoqi@0 | 585 | } |
aoqi@0 | 586 | |
aoqi@0 | 587 | @Override |
aoqi@0 | 588 | public Listener getListener() { |
aoqi@0 | 589 | return externalListener; |
aoqi@0 | 590 | } |
aoqi@0 | 591 | |
aoqi@0 | 592 | @Override |
aoqi@0 | 593 | public void setListener(Listener listener) { |
aoqi@0 | 594 | externalListener = listener; |
aoqi@0 | 595 | } |
aoqi@0 | 596 | |
aoqi@0 | 597 | public UnmarshallingContext getContext() { |
aoqi@0 | 598 | return coordinator; |
aoqi@0 | 599 | } |
aoqi@0 | 600 | |
aoqi@0 | 601 | @Override |
aoqi@0 | 602 | @SuppressWarnings("FinalizeDeclaration") |
aoqi@0 | 603 | protected void finalize() throws Throwable { |
aoqi@0 | 604 | try { |
aoqi@0 | 605 | ClassFactory.cleanCache(); |
aoqi@0 | 606 | } finally { |
aoqi@0 | 607 | super.finalize(); |
aoqi@0 | 608 | } |
aoqi@0 | 609 | } |
aoqi@0 | 610 | |
aoqi@0 | 611 | /** |
aoqi@0 | 612 | * Must be called from same thread which created the UnmarshallerImpl instance. |
aoqi@0 | 613 | * @throws IOException |
aoqi@0 | 614 | */ |
aoqi@0 | 615 | public void close() throws IOException { |
aoqi@0 | 616 | ClassFactory.cleanCache(); |
aoqi@0 | 617 | } |
aoqi@0 | 618 | |
aoqi@0 | 619 | } |