src/share/jaxws_classes/javax/xml/bind/helpers/AbstractUnmarshallerImpl.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 397
b99d7e355d4b
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

ohair@286 1 /*
ohair@286 2 * Copyright (c) 2003, 2010, 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 javax.xml.bind.helpers;
ohair@286 27
ohair@286 28 import org.xml.sax.InputSource;
ohair@286 29 import org.xml.sax.SAXException;
ohair@286 30 import org.xml.sax.XMLReader;
ohair@286 31 import org.w3c.dom.Node;
ohair@286 32
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.ValidationEventHandler;
ohair@286 38 import javax.xml.bind.JAXBElement;
ohair@286 39 import javax.xml.bind.annotation.adapters.XmlAdapter;
ohair@286 40 import javax.xml.bind.attachment.AttachmentUnmarshaller;
ohair@286 41 import javax.xml.parsers.ParserConfigurationException;
ohair@286 42 import javax.xml.parsers.SAXParserFactory;
ohair@286 43 import javax.xml.stream.XMLEventReader;
ohair@286 44 import javax.xml.stream.XMLStreamReader;
ohair@286 45 import javax.xml.transform.Source;
ohair@286 46 import javax.xml.transform.dom.DOMSource;
ohair@286 47 import javax.xml.transform.sax.SAXSource;
ohair@286 48 import javax.xml.transform.stream.StreamSource;
ohair@286 49 import javax.xml.validation.Schema;
ohair@286 50 import java.io.File;
ohair@286 51 import java.io.Reader;
ohair@286 52 import java.net.MalformedURLException;
ohair@286 53 import java.net.URL;
ohair@286 54
ohair@286 55 /**
ohair@286 56 * Partial default <tt>Unmarshaller</tt> implementation.
ohair@286 57 *
ohair@286 58 * <p>
ohair@286 59 * This class provides a partial default implementation for the
ohair@286 60 * {@link javax.xml.bind.Unmarshaller}interface.
ohair@286 61 *
ohair@286 62 * <p>
ohair@286 63 * A JAXB Provider has to implement five methods (getUnmarshallerHandler,
ohair@286 64 * unmarshal(Node), unmarshal(XMLReader,InputSource),
ohair@286 65 * unmarshal(XMLStreamReader), and unmarshal(XMLEventReader).
ohair@286 66 *
ohair@286 67 * @author <ul>
ohair@286 68 * <li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li>
ohair@286 69 * </ul>
ohair@286 70 * @see javax.xml.bind.Unmarshaller
ohair@286 71 * @since JAXB1.0
ohair@286 72 */
ohair@286 73 public abstract class AbstractUnmarshallerImpl implements Unmarshaller
ohair@286 74 {
ohair@286 75 /** handler that will be used to process errors and warnings during unmarshal */
ohair@286 76 private ValidationEventHandler eventHandler =
ohair@286 77 new DefaultValidationEventHandler();
ohair@286 78
ohair@286 79 /** whether or not the unmarshaller will validate */
ohair@286 80 protected boolean validating = false;
ohair@286 81
ohair@286 82 /**
ohair@286 83 * XMLReader that will be used to parse a document.
ohair@286 84 */
ohair@286 85 private XMLReader reader = null;
ohair@286 86
ohair@286 87 /**
ohair@286 88 * Obtains a configured XMLReader.
ohair@286 89 *
ohair@286 90 * This method is used when the client-specified
ohair@286 91 * {@link SAXSource} object doesn't have XMLReader.
ohair@286 92 *
ohair@286 93 * {@link Unmarshaller} is not re-entrant, so we will
ohair@286 94 * only use one instance of XMLReader.
ohair@286 95 */
ohair@286 96 protected XMLReader getXMLReader() throws JAXBException {
ohair@286 97 if(reader==null) {
ohair@286 98 try {
ohair@286 99 SAXParserFactory parserFactory;
ohair@286 100 parserFactory = SAXParserFactory.newInstance();
ohair@286 101 parserFactory.setNamespaceAware(true);
ohair@286 102 // there is no point in asking a validation because
ohair@286 103 // there is no guarantee that the document will come with
ohair@286 104 // a proper schemaLocation.
ohair@286 105 parserFactory.setValidating(false);
ohair@286 106 reader = parserFactory.newSAXParser().getXMLReader();
ohair@286 107 } catch( ParserConfigurationException e ) {
ohair@286 108 throw new JAXBException(e);
ohair@286 109 } catch( SAXException e ) {
ohair@286 110 throw new JAXBException(e);
ohair@286 111 }
ohair@286 112 }
ohair@286 113 return reader;
ohair@286 114 }
ohair@286 115
ohair@286 116 public Object unmarshal( Source source ) throws JAXBException {
ohair@286 117 if( source == null ) {
ohair@286 118 throw new IllegalArgumentException(
ohair@286 119 Messages.format( Messages.MUST_NOT_BE_NULL, "source" ) );
ohair@286 120 }
ohair@286 121
ohair@286 122 if(source instanceof SAXSource)
ohair@286 123 return unmarshal( (SAXSource)source );
ohair@286 124 if(source instanceof StreamSource)
ohair@286 125 return unmarshal( streamSourceToInputSource((StreamSource)source));
ohair@286 126 if(source instanceof DOMSource)
ohair@286 127 return unmarshal( ((DOMSource)source).getNode() );
ohair@286 128
ohair@286 129 // we don't handle other types of Source
ohair@286 130 throw new IllegalArgumentException();
ohair@286 131 }
ohair@286 132
ohair@286 133 // use the client specified XMLReader contained in the SAXSource.
ohair@286 134 private Object unmarshal( SAXSource source ) throws JAXBException {
ohair@286 135
ohair@286 136 XMLReader r = source.getXMLReader();
ohair@286 137 if( r == null )
ohair@286 138 r = getXMLReader();
ohair@286 139
ohair@286 140 return unmarshal( r, source.getInputSource() );
ohair@286 141 }
ohair@286 142
ohair@286 143 /**
ohair@286 144 * Unmarshals an object by using the specified XMLReader and the InputSource.
ohair@286 145 *
ohair@286 146 * The callee should call the setErrorHandler method of the XMLReader
ohair@286 147 * so that errors are passed to the client-specified ValidationEventHandler.
ohair@286 148 */
ohair@286 149 protected abstract Object unmarshal( XMLReader reader, InputSource source ) throws JAXBException;
ohair@286 150
ohair@286 151 public final Object unmarshal( InputSource source ) throws JAXBException {
ohair@286 152 if( source == null ) {
ohair@286 153 throw new IllegalArgumentException(
ohair@286 154 Messages.format( Messages.MUST_NOT_BE_NULL, "source" ) );
ohair@286 155 }
ohair@286 156
ohair@286 157 return unmarshal( getXMLReader(), source );
ohair@286 158 }
ohair@286 159
ohair@286 160
ohair@286 161 private Object unmarshal( String url ) throws JAXBException {
ohair@286 162 return unmarshal( new InputSource(url) );
ohair@286 163 }
ohair@286 164
ohair@286 165 public final Object unmarshal( URL url ) throws JAXBException {
ohair@286 166 if( url == null ) {
ohair@286 167 throw new IllegalArgumentException(
ohair@286 168 Messages.format( Messages.MUST_NOT_BE_NULL, "url" ) );
ohair@286 169 }
ohair@286 170
ohair@286 171 return unmarshal( url.toExternalForm() );
ohair@286 172 }
ohair@286 173
ohair@286 174 public final Object unmarshal( File f ) throws JAXBException {
ohair@286 175 if( f == null ) {
ohair@286 176 throw new IllegalArgumentException(
ohair@286 177 Messages.format( Messages.MUST_NOT_BE_NULL, "file" ) );
ohair@286 178 }
ohair@286 179
ohair@286 180 try {
ohair@286 181 // copied from JAXP
ohair@286 182 String path = f.getAbsolutePath();
ohair@286 183 if (File.separatorChar != '/')
ohair@286 184 path = path.replace(File.separatorChar, '/');
ohair@286 185 if (!path.startsWith("/"))
ohair@286 186 path = "/" + path;
ohair@286 187 if (!path.endsWith("/") && f.isDirectory())
ohair@286 188 path = path + "/";
ohair@286 189 return unmarshal(new URL("file", "", path));
ohair@286 190 } catch( MalformedURLException e ) {
ohair@286 191 throw new IllegalArgumentException(e.getMessage());
ohair@286 192 }
ohair@286 193 }
ohair@286 194
ohair@286 195 public final Object unmarshal( java.io.InputStream is )
ohair@286 196 throws JAXBException {
ohair@286 197
ohair@286 198 if( is == null ) {
ohair@286 199 throw new IllegalArgumentException(
ohair@286 200 Messages.format( Messages.MUST_NOT_BE_NULL, "is" ) );
ohair@286 201 }
ohair@286 202
ohair@286 203 InputSource isrc = new InputSource( is );
ohair@286 204 return unmarshal( isrc );
ohair@286 205 }
ohair@286 206
ohair@286 207 public final Object unmarshal( Reader reader ) throws JAXBException {
ohair@286 208 if( reader == null ) {
ohair@286 209 throw new IllegalArgumentException(
ohair@286 210 Messages.format( Messages.MUST_NOT_BE_NULL, "reader" ) );
ohair@286 211 }
ohair@286 212
ohair@286 213 InputSource isrc = new InputSource( reader );
ohair@286 214 return unmarshal( isrc );
ohair@286 215 }
ohair@286 216
ohair@286 217
ohair@286 218 private static InputSource streamSourceToInputSource( StreamSource ss ) {
ohair@286 219 InputSource is = new InputSource();
ohair@286 220 is.setSystemId( ss.getSystemId() );
ohair@286 221 is.setByteStream( ss.getInputStream() );
ohair@286 222 is.setCharacterStream( ss.getReader() );
ohair@286 223
ohair@286 224 return is;
ohair@286 225 }
ohair@286 226
ohair@286 227
ohair@286 228 /**
ohair@286 229 * Indicates whether or not the Unmarshaller is configured to validate
ohair@286 230 * during unmarshal operations.
ohair@286 231 * <p>
ohair@286 232 * <i><b>Note:</b> I named this method isValidating() to stay in-line
ohair@286 233 * with JAXP, as opposed to naming it getValidating(). </i>
ohair@286 234 *
ohair@286 235 * @return true if the Unmarshaller is configured to validate during
ohair@286 236 * unmarshal operations, false otherwise
ohair@286 237 * @throws JAXBException if an error occurs while retrieving the validating
ohair@286 238 * flag
ohair@286 239 */
ohair@286 240 public boolean isValidating() throws JAXBException {
ohair@286 241 return validating;
ohair@286 242 }
ohair@286 243
ohair@286 244 /**
ohair@286 245 * Allow an application to register a validation event handler.
ohair@286 246 * <p>
ohair@286 247 * The validation event handler will be called by the JAXB Provider if any
ohair@286 248 * validation errors are encountered during calls to any of the
ohair@286 249 * <tt>unmarshal</tt> methods. If the client application does not register
ohair@286 250 * a validation event handler before invoking the unmarshal methods, then
ohair@286 251 * all validation events will be silently ignored and may result in
ohair@286 252 * unexpected behaviour.
ohair@286 253 *
ohair@286 254 * @param handler the validation event handler
ohair@286 255 * @throws JAXBException if an error was encountered while setting the
ohair@286 256 * event handler
ohair@286 257 */
ohair@286 258 public void setEventHandler(ValidationEventHandler handler)
ohair@286 259 throws JAXBException {
ohair@286 260
ohair@286 261 if( handler == null ) {
ohair@286 262 eventHandler = new DefaultValidationEventHandler();
ohair@286 263 } else {
ohair@286 264 eventHandler = handler;
ohair@286 265 }
ohair@286 266 }
ohair@286 267
ohair@286 268 /**
ohair@286 269 * Specifies whether or not the Unmarshaller should validate during
ohair@286 270 * unmarshal operations. By default, the <tt>Unmarshaller</tt> does
ohair@286 271 * not validate.
ohair@286 272 * <p>
ohair@286 273 * This method may only be invoked before or after calling one of the
ohair@286 274 * unmarshal methods.
ohair@286 275 *
ohair@286 276 * @param validating true if the Unmarshaller should validate during
ohair@286 277 * unmarshal, false otherwise
ohair@286 278 * @throws JAXBException if an error occurred while enabling or disabling
ohair@286 279 * validation at unmarshal time
ohair@286 280 */
ohair@286 281 public void setValidating(boolean validating) throws JAXBException {
ohair@286 282 this.validating = validating;
ohair@286 283 }
ohair@286 284
ohair@286 285 /**
ohair@286 286 * Return the current event handler or the default event handler if one
ohair@286 287 * hasn't been set.
ohair@286 288 *
ohair@286 289 * @return the current ValidationEventHandler or the default event handler
ohair@286 290 * if it hasn't been set
ohair@286 291 * @throws JAXBException if an error was encountered while getting the
ohair@286 292 * current event handler
ohair@286 293 */
ohair@286 294 public ValidationEventHandler getEventHandler() throws JAXBException {
ohair@286 295 return eventHandler;
ohair@286 296 }
ohair@286 297
ohair@286 298
ohair@286 299 /**
ohair@286 300 * Creates an UnmarshalException from a SAXException.
ohair@286 301 *
ohair@286 302 * This is an utility method provided for the derived classes.
ohair@286 303 *
ohair@286 304 * <p>
ohair@286 305 * When a provider-implemented ContentHandler wants to throw a
ohair@286 306 * JAXBException, it needs to wrap the exception by a SAXException.
ohair@286 307 * If the unmarshaller implementation blindly wrap SAXException
ohair@286 308 * by JAXBException, such an exception will be a JAXBException
ohair@286 309 * wrapped by a SAXException wrapped by another JAXBException.
ohair@286 310 * This is silly.
ohair@286 311 *
ohair@286 312 * <p>
ohair@286 313 * This method checks the nested exception of SAXException
ohair@286 314 * and reduce those excessive wrapping.
ohair@286 315 *
ohair@286 316 * @return the resulting UnmarshalException
ohair@286 317 */
ohair@286 318 protected UnmarshalException createUnmarshalException( SAXException e ) {
ohair@286 319 // check the nested exception to see if it's an UnmarshalException
ohair@286 320 Exception nested = e.getException();
ohair@286 321 if(nested instanceof UnmarshalException)
ohair@286 322 return (UnmarshalException)nested;
ohair@286 323
ohair@286 324 if(nested instanceof RuntimeException)
ohair@286 325 // typically this is an unexpected exception,
ohair@286 326 // just throw it rather than wrap it, so that the full stack
ohair@286 327 // trace can be displayed.
ohair@286 328 throw (RuntimeException)nested;
ohair@286 329
ohair@286 330
ohair@286 331 // otherwise simply wrap it
ohair@286 332 if(nested!=null)
ohair@286 333 return new UnmarshalException(nested);
ohair@286 334 else
ohair@286 335 return new UnmarshalException(e);
ohair@286 336 }
ohair@286 337
ohair@286 338 /**
ohair@286 339 * Default implementation of the setProperty method always
ohair@286 340 * throws PropertyException since there are no required
ohair@286 341 * properties. If a provider needs to handle additional
ohair@286 342 * properties, it should override this method in a derived class.
ohair@286 343 */
ohair@286 344 public void setProperty( String name, Object value )
ohair@286 345 throws PropertyException {
ohair@286 346
ohair@286 347 if( name == null ) {
ohair@286 348 throw new IllegalArgumentException(
ohair@286 349 Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) );
ohair@286 350 }
ohair@286 351
ohair@286 352 throw new PropertyException(name, value);
ohair@286 353 }
ohair@286 354
ohair@286 355 /**
ohair@286 356 * Default implementation of the getProperty method always
ohair@286 357 * throws PropertyException since there are no required
ohair@286 358 * properties. If a provider needs to handle additional
ohair@286 359 * properties, it should override this method in a derived class.
ohair@286 360 */
ohair@286 361 public Object getProperty( String name )
ohair@286 362 throws PropertyException {
ohair@286 363
ohair@286 364 if( name == null ) {
ohair@286 365 throw new IllegalArgumentException(
ohair@286 366 Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) );
ohair@286 367 }
ohair@286 368
ohair@286 369 throw new PropertyException(name);
ohair@286 370 }
ohair@286 371
ohair@286 372 public Object unmarshal(XMLEventReader reader) throws JAXBException {
ohair@286 373
ohair@286 374 throw new UnsupportedOperationException();
ohair@286 375 }
ohair@286 376
ohair@286 377 public Object unmarshal(XMLStreamReader reader) throws JAXBException {
ohair@286 378
ohair@286 379 throw new UnsupportedOperationException();
ohair@286 380 }
ohair@286 381
ohair@286 382 public <T> JAXBElement<T> unmarshal(Node node, Class<T> expectedType) throws JAXBException {
ohair@286 383 throw new UnsupportedOperationException();
ohair@286 384 }
ohair@286 385
ohair@286 386 public <T> JAXBElement<T> unmarshal(Source source, Class<T> expectedType) throws JAXBException {
ohair@286 387 throw new UnsupportedOperationException();
ohair@286 388 }
ohair@286 389
ohair@286 390 public <T> JAXBElement<T> unmarshal(XMLStreamReader reader, Class<T> expectedType) throws JAXBException {
ohair@286 391 throw new UnsupportedOperationException();
ohair@286 392 }
ohair@286 393
ohair@286 394 public <T> JAXBElement<T> unmarshal(XMLEventReader reader, Class<T> expectedType) throws JAXBException {
ohair@286 395 throw new UnsupportedOperationException();
ohair@286 396 }
ohair@286 397
ohair@286 398 public void setSchema(Schema schema) {
ohair@286 399 throw new UnsupportedOperationException();
ohair@286 400 }
ohair@286 401
ohair@286 402 public Schema getSchema() {
ohair@286 403 throw new UnsupportedOperationException();
ohair@286 404 }
ohair@286 405
ohair@286 406 public void setAdapter(XmlAdapter adapter) {
ohair@286 407 if(adapter==null)
ohair@286 408 throw new IllegalArgumentException();
ohair@286 409 setAdapter((Class)adapter.getClass(),adapter);
ohair@286 410 }
ohair@286 411
ohair@286 412 public <A extends XmlAdapter> void setAdapter(Class<A> type, A adapter) {
ohair@286 413 throw new UnsupportedOperationException();
ohair@286 414 }
ohair@286 415
ohair@286 416 public <A extends XmlAdapter> A getAdapter(Class<A> type) {
ohair@286 417 throw new UnsupportedOperationException();
ohair@286 418 }
ohair@286 419
ohair@286 420 public void setAttachmentUnmarshaller(AttachmentUnmarshaller au) {
ohair@286 421 throw new UnsupportedOperationException();
ohair@286 422 }
ohair@286 423
ohair@286 424 public AttachmentUnmarshaller getAttachmentUnmarshaller() {
ohair@286 425 throw new UnsupportedOperationException();
ohair@286 426 }
ohair@286 427
ohair@286 428 public void setListener(Listener listener) {
ohair@286 429 throw new UnsupportedOperationException();
ohair@286 430 }
ohair@286 431
ohair@286 432 public Listener getListener() {
ohair@286 433 throw new UnsupportedOperationException();
ohair@286 434 }
ohair@286 435 }

mercurial