src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/unmarshaller/UnmarshallerImpl.java

Sun, 31 Aug 2014 16:14:36 +0400

author
aefimov
date
Sun, 31 Aug 2014 16:14:36 +0400
changeset 650
121e938cb9c3
parent 368
0989ad8c0860
child 637
9c07ef4934dd
permissions
-rw-r--r--

8036981: JAXB not preserving formatting for xsd:any Mixed content
Reviewed-by: lancea, mkos

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

mercurial