1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/javax/xml/bind/helpers/AbstractUnmarshallerImpl.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,435 @@ 1.4 +/* 1.5 + * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package javax.xml.bind.helpers; 1.30 + 1.31 +import org.xml.sax.InputSource; 1.32 +import org.xml.sax.SAXException; 1.33 +import org.xml.sax.XMLReader; 1.34 +import org.w3c.dom.Node; 1.35 + 1.36 +import javax.xml.bind.JAXBException; 1.37 +import javax.xml.bind.PropertyException; 1.38 +import javax.xml.bind.UnmarshalException; 1.39 +import javax.xml.bind.Unmarshaller; 1.40 +import javax.xml.bind.ValidationEventHandler; 1.41 +import javax.xml.bind.JAXBElement; 1.42 +import javax.xml.bind.annotation.adapters.XmlAdapter; 1.43 +import javax.xml.bind.attachment.AttachmentUnmarshaller; 1.44 +import javax.xml.parsers.ParserConfigurationException; 1.45 +import javax.xml.parsers.SAXParserFactory; 1.46 +import javax.xml.stream.XMLEventReader; 1.47 +import javax.xml.stream.XMLStreamReader; 1.48 +import javax.xml.transform.Source; 1.49 +import javax.xml.transform.dom.DOMSource; 1.50 +import javax.xml.transform.sax.SAXSource; 1.51 +import javax.xml.transform.stream.StreamSource; 1.52 +import javax.xml.validation.Schema; 1.53 +import java.io.File; 1.54 +import java.io.Reader; 1.55 +import java.net.MalformedURLException; 1.56 +import java.net.URL; 1.57 + 1.58 +/** 1.59 + * Partial default <tt>Unmarshaller</tt> implementation. 1.60 + * 1.61 + * <p> 1.62 + * This class provides a partial default implementation for the 1.63 + * {@link javax.xml.bind.Unmarshaller}interface. 1.64 + * 1.65 + * <p> 1.66 + * A JAXB Provider has to implement five methods (getUnmarshallerHandler, 1.67 + * unmarshal(Node), unmarshal(XMLReader,InputSource), 1.68 + * unmarshal(XMLStreamReader), and unmarshal(XMLEventReader). 1.69 + * 1.70 + * @author <ul> 1.71 + * <li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li> 1.72 + * </ul> 1.73 + * @see javax.xml.bind.Unmarshaller 1.74 + * @since JAXB1.0 1.75 + */ 1.76 +public abstract class AbstractUnmarshallerImpl implements Unmarshaller 1.77 +{ 1.78 + /** handler that will be used to process errors and warnings during unmarshal */ 1.79 + private ValidationEventHandler eventHandler = 1.80 + new DefaultValidationEventHandler(); 1.81 + 1.82 + /** whether or not the unmarshaller will validate */ 1.83 + protected boolean validating = false; 1.84 + 1.85 + /** 1.86 + * XMLReader that will be used to parse a document. 1.87 + */ 1.88 + private XMLReader reader = null; 1.89 + 1.90 + /** 1.91 + * Obtains a configured XMLReader. 1.92 + * 1.93 + * This method is used when the client-specified 1.94 + * {@link SAXSource} object doesn't have XMLReader. 1.95 + * 1.96 + * {@link Unmarshaller} is not re-entrant, so we will 1.97 + * only use one instance of XMLReader. 1.98 + */ 1.99 + protected XMLReader getXMLReader() throws JAXBException { 1.100 + if(reader==null) { 1.101 + try { 1.102 + SAXParserFactory parserFactory; 1.103 + parserFactory = SAXParserFactory.newInstance(); 1.104 + parserFactory.setNamespaceAware(true); 1.105 + // there is no point in asking a validation because 1.106 + // there is no guarantee that the document will come with 1.107 + // a proper schemaLocation. 1.108 + parserFactory.setValidating(false); 1.109 + reader = parserFactory.newSAXParser().getXMLReader(); 1.110 + } catch( ParserConfigurationException e ) { 1.111 + throw new JAXBException(e); 1.112 + } catch( SAXException e ) { 1.113 + throw new JAXBException(e); 1.114 + } 1.115 + } 1.116 + return reader; 1.117 + } 1.118 + 1.119 + public Object unmarshal( Source source ) throws JAXBException { 1.120 + if( source == null ) { 1.121 + throw new IllegalArgumentException( 1.122 + Messages.format( Messages.MUST_NOT_BE_NULL, "source" ) ); 1.123 + } 1.124 + 1.125 + if(source instanceof SAXSource) 1.126 + return unmarshal( (SAXSource)source ); 1.127 + if(source instanceof StreamSource) 1.128 + return unmarshal( streamSourceToInputSource((StreamSource)source)); 1.129 + if(source instanceof DOMSource) 1.130 + return unmarshal( ((DOMSource)source).getNode() ); 1.131 + 1.132 + // we don't handle other types of Source 1.133 + throw new IllegalArgumentException(); 1.134 + } 1.135 + 1.136 + // use the client specified XMLReader contained in the SAXSource. 1.137 + private Object unmarshal( SAXSource source ) throws JAXBException { 1.138 + 1.139 + XMLReader r = source.getXMLReader(); 1.140 + if( r == null ) 1.141 + r = getXMLReader(); 1.142 + 1.143 + return unmarshal( r, source.getInputSource() ); 1.144 + } 1.145 + 1.146 + /** 1.147 + * Unmarshals an object by using the specified XMLReader and the InputSource. 1.148 + * 1.149 + * The callee should call the setErrorHandler method of the XMLReader 1.150 + * so that errors are passed to the client-specified ValidationEventHandler. 1.151 + */ 1.152 + protected abstract Object unmarshal( XMLReader reader, InputSource source ) throws JAXBException; 1.153 + 1.154 + public final Object unmarshal( InputSource source ) throws JAXBException { 1.155 + if( source == null ) { 1.156 + throw new IllegalArgumentException( 1.157 + Messages.format( Messages.MUST_NOT_BE_NULL, "source" ) ); 1.158 + } 1.159 + 1.160 + return unmarshal( getXMLReader(), source ); 1.161 + } 1.162 + 1.163 + 1.164 + private Object unmarshal( String url ) throws JAXBException { 1.165 + return unmarshal( new InputSource(url) ); 1.166 + } 1.167 + 1.168 + public final Object unmarshal( URL url ) throws JAXBException { 1.169 + if( url == null ) { 1.170 + throw new IllegalArgumentException( 1.171 + Messages.format( Messages.MUST_NOT_BE_NULL, "url" ) ); 1.172 + } 1.173 + 1.174 + return unmarshal( url.toExternalForm() ); 1.175 + } 1.176 + 1.177 + public final Object unmarshal( File f ) throws JAXBException { 1.178 + if( f == null ) { 1.179 + throw new IllegalArgumentException( 1.180 + Messages.format( Messages.MUST_NOT_BE_NULL, "file" ) ); 1.181 + } 1.182 + 1.183 + try { 1.184 + // copied from JAXP 1.185 + String path = f.getAbsolutePath(); 1.186 + if (File.separatorChar != '/') 1.187 + path = path.replace(File.separatorChar, '/'); 1.188 + if (!path.startsWith("/")) 1.189 + path = "/" + path; 1.190 + if (!path.endsWith("/") && f.isDirectory()) 1.191 + path = path + "/"; 1.192 + return unmarshal(new URL("file", "", path)); 1.193 + } catch( MalformedURLException e ) { 1.194 + throw new IllegalArgumentException(e.getMessage()); 1.195 + } 1.196 + } 1.197 + 1.198 + public final Object unmarshal( java.io.InputStream is ) 1.199 + throws JAXBException { 1.200 + 1.201 + if( is == null ) { 1.202 + throw new IllegalArgumentException( 1.203 + Messages.format( Messages.MUST_NOT_BE_NULL, "is" ) ); 1.204 + } 1.205 + 1.206 + InputSource isrc = new InputSource( is ); 1.207 + return unmarshal( isrc ); 1.208 + } 1.209 + 1.210 + public final Object unmarshal( Reader reader ) throws JAXBException { 1.211 + if( reader == null ) { 1.212 + throw new IllegalArgumentException( 1.213 + Messages.format( Messages.MUST_NOT_BE_NULL, "reader" ) ); 1.214 + } 1.215 + 1.216 + InputSource isrc = new InputSource( reader ); 1.217 + return unmarshal( isrc ); 1.218 + } 1.219 + 1.220 + 1.221 + private static InputSource streamSourceToInputSource( StreamSource ss ) { 1.222 + InputSource is = new InputSource(); 1.223 + is.setSystemId( ss.getSystemId() ); 1.224 + is.setByteStream( ss.getInputStream() ); 1.225 + is.setCharacterStream( ss.getReader() ); 1.226 + 1.227 + return is; 1.228 + } 1.229 + 1.230 + 1.231 + /** 1.232 + * Indicates whether or not the Unmarshaller is configured to validate 1.233 + * during unmarshal operations. 1.234 + * <p> 1.235 + * <i><b>Note:</b> I named this method isValidating() to stay in-line 1.236 + * with JAXP, as opposed to naming it getValidating(). </i> 1.237 + * 1.238 + * @return true if the Unmarshaller is configured to validate during 1.239 + * unmarshal operations, false otherwise 1.240 + * @throws JAXBException if an error occurs while retrieving the validating 1.241 + * flag 1.242 + */ 1.243 + public boolean isValidating() throws JAXBException { 1.244 + return validating; 1.245 + } 1.246 + 1.247 + /** 1.248 + * Allow an application to register a validation event handler. 1.249 + * <p> 1.250 + * The validation event handler will be called by the JAXB Provider if any 1.251 + * validation errors are encountered during calls to any of the 1.252 + * <tt>unmarshal</tt> methods. If the client application does not register 1.253 + * a validation event handler before invoking the unmarshal methods, then 1.254 + * all validation events will be silently ignored and may result in 1.255 + * unexpected behaviour. 1.256 + * 1.257 + * @param handler the validation event handler 1.258 + * @throws JAXBException if an error was encountered while setting the 1.259 + * event handler 1.260 + */ 1.261 + public void setEventHandler(ValidationEventHandler handler) 1.262 + throws JAXBException { 1.263 + 1.264 + if( handler == null ) { 1.265 + eventHandler = new DefaultValidationEventHandler(); 1.266 + } else { 1.267 + eventHandler = handler; 1.268 + } 1.269 + } 1.270 + 1.271 + /** 1.272 + * Specifies whether or not the Unmarshaller should validate during 1.273 + * unmarshal operations. By default, the <tt>Unmarshaller</tt> does 1.274 + * not validate. 1.275 + * <p> 1.276 + * This method may only be invoked before or after calling one of the 1.277 + * unmarshal methods. 1.278 + * 1.279 + * @param validating true if the Unmarshaller should validate during 1.280 + * unmarshal, false otherwise 1.281 + * @throws JAXBException if an error occurred while enabling or disabling 1.282 + * validation at unmarshal time 1.283 + */ 1.284 + public void setValidating(boolean validating) throws JAXBException { 1.285 + this.validating = validating; 1.286 + } 1.287 + 1.288 + /** 1.289 + * Return the current event handler or the default event handler if one 1.290 + * hasn't been set. 1.291 + * 1.292 + * @return the current ValidationEventHandler or the default event handler 1.293 + * if it hasn't been set 1.294 + * @throws JAXBException if an error was encountered while getting the 1.295 + * current event handler 1.296 + */ 1.297 + public ValidationEventHandler getEventHandler() throws JAXBException { 1.298 + return eventHandler; 1.299 + } 1.300 + 1.301 + 1.302 + /** 1.303 + * Creates an UnmarshalException from a SAXException. 1.304 + * 1.305 + * This is an utility method provided for the derived classes. 1.306 + * 1.307 + * <p> 1.308 + * When a provider-implemented ContentHandler wants to throw a 1.309 + * JAXBException, it needs to wrap the exception by a SAXException. 1.310 + * If the unmarshaller implementation blindly wrap SAXException 1.311 + * by JAXBException, such an exception will be a JAXBException 1.312 + * wrapped by a SAXException wrapped by another JAXBException. 1.313 + * This is silly. 1.314 + * 1.315 + * <p> 1.316 + * This method checks the nested exception of SAXException 1.317 + * and reduce those excessive wrapping. 1.318 + * 1.319 + * @return the resulting UnmarshalException 1.320 + */ 1.321 + protected UnmarshalException createUnmarshalException( SAXException e ) { 1.322 + // check the nested exception to see if it's an UnmarshalException 1.323 + Exception nested = e.getException(); 1.324 + if(nested instanceof UnmarshalException) 1.325 + return (UnmarshalException)nested; 1.326 + 1.327 + if(nested instanceof RuntimeException) 1.328 + // typically this is an unexpected exception, 1.329 + // just throw it rather than wrap it, so that the full stack 1.330 + // trace can be displayed. 1.331 + throw (RuntimeException)nested; 1.332 + 1.333 + 1.334 + // otherwise simply wrap it 1.335 + if(nested!=null) 1.336 + return new UnmarshalException(nested); 1.337 + else 1.338 + return new UnmarshalException(e); 1.339 + } 1.340 + 1.341 + /** 1.342 + * Default implementation of the setProperty method always 1.343 + * throws PropertyException since there are no required 1.344 + * properties. If a provider needs to handle additional 1.345 + * properties, it should override this method in a derived class. 1.346 + */ 1.347 + public void setProperty( String name, Object value ) 1.348 + throws PropertyException { 1.349 + 1.350 + if( name == null ) { 1.351 + throw new IllegalArgumentException( 1.352 + Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) ); 1.353 + } 1.354 + 1.355 + throw new PropertyException(name, value); 1.356 + } 1.357 + 1.358 + /** 1.359 + * Default implementation of the getProperty method always 1.360 + * throws PropertyException since there are no required 1.361 + * properties. If a provider needs to handle additional 1.362 + * properties, it should override this method in a derived class. 1.363 + */ 1.364 + public Object getProperty( String name ) 1.365 + throws PropertyException { 1.366 + 1.367 + if( name == null ) { 1.368 + throw new IllegalArgumentException( 1.369 + Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) ); 1.370 + } 1.371 + 1.372 + throw new PropertyException(name); 1.373 + } 1.374 + 1.375 + public Object unmarshal(XMLEventReader reader) throws JAXBException { 1.376 + 1.377 + throw new UnsupportedOperationException(); 1.378 + } 1.379 + 1.380 + public Object unmarshal(XMLStreamReader reader) throws JAXBException { 1.381 + 1.382 + throw new UnsupportedOperationException(); 1.383 + } 1.384 + 1.385 + public <T> JAXBElement<T> unmarshal(Node node, Class<T> expectedType) throws JAXBException { 1.386 + throw new UnsupportedOperationException(); 1.387 + } 1.388 + 1.389 + public <T> JAXBElement<T> unmarshal(Source source, Class<T> expectedType) throws JAXBException { 1.390 + throw new UnsupportedOperationException(); 1.391 + } 1.392 + 1.393 + public <T> JAXBElement<T> unmarshal(XMLStreamReader reader, Class<T> expectedType) throws JAXBException { 1.394 + throw new UnsupportedOperationException(); 1.395 + } 1.396 + 1.397 + public <T> JAXBElement<T> unmarshal(XMLEventReader reader, Class<T> expectedType) throws JAXBException { 1.398 + throw new UnsupportedOperationException(); 1.399 + } 1.400 + 1.401 + public void setSchema(Schema schema) { 1.402 + throw new UnsupportedOperationException(); 1.403 + } 1.404 + 1.405 + public Schema getSchema() { 1.406 + throw new UnsupportedOperationException(); 1.407 + } 1.408 + 1.409 + public void setAdapter(XmlAdapter adapter) { 1.410 + if(adapter==null) 1.411 + throw new IllegalArgumentException(); 1.412 + setAdapter((Class)adapter.getClass(),adapter); 1.413 + } 1.414 + 1.415 + public <A extends XmlAdapter> void setAdapter(Class<A> type, A adapter) { 1.416 + throw new UnsupportedOperationException(); 1.417 + } 1.418 + 1.419 + public <A extends XmlAdapter> A getAdapter(Class<A> type) { 1.420 + throw new UnsupportedOperationException(); 1.421 + } 1.422 + 1.423 + public void setAttachmentUnmarshaller(AttachmentUnmarshaller au) { 1.424 + throw new UnsupportedOperationException(); 1.425 + } 1.426 + 1.427 + public AttachmentUnmarshaller getAttachmentUnmarshaller() { 1.428 + throw new UnsupportedOperationException(); 1.429 + } 1.430 + 1.431 + public void setListener(Listener listener) { 1.432 + throw new UnsupportedOperationException(); 1.433 + } 1.434 + 1.435 + public Listener getListener() { 1.436 + throw new UnsupportedOperationException(); 1.437 + } 1.438 +}