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