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

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 397
b99d7e355d4b
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

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

mercurial