ohair@286: /* mkos@397: * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package javax.xml.bind.helpers; ohair@286: ohair@286: import org.xml.sax.InputSource; ohair@286: import org.xml.sax.SAXException; ohair@286: import org.xml.sax.XMLReader; ohair@286: import org.w3c.dom.Node; ohair@286: ohair@286: import javax.xml.bind.JAXBException; ohair@286: import javax.xml.bind.PropertyException; ohair@286: import javax.xml.bind.UnmarshalException; ohair@286: import javax.xml.bind.Unmarshaller; ohair@286: import javax.xml.bind.ValidationEventHandler; ohair@286: import javax.xml.bind.JAXBElement; ohair@286: import javax.xml.bind.annotation.adapters.XmlAdapter; ohair@286: import javax.xml.bind.attachment.AttachmentUnmarshaller; ohair@286: import javax.xml.parsers.ParserConfigurationException; ohair@286: import javax.xml.parsers.SAXParserFactory; ohair@286: import javax.xml.stream.XMLEventReader; ohair@286: import javax.xml.stream.XMLStreamReader; ohair@286: import javax.xml.transform.Source; ohair@286: import javax.xml.transform.dom.DOMSource; ohair@286: import javax.xml.transform.sax.SAXSource; ohair@286: import javax.xml.transform.stream.StreamSource; ohair@286: import javax.xml.validation.Schema; ohair@286: import java.io.File; ohair@286: import java.io.Reader; ohair@286: import java.net.MalformedURLException; ohair@286: import java.net.URL; ohair@286: ohair@286: /** ohair@286: * Partial default Unmarshaller implementation. ohair@286: * ohair@286: *
ohair@286: * This class provides a partial default implementation for the ohair@286: * {@link javax.xml.bind.Unmarshaller}interface. ohair@286: * ohair@286: *
ohair@286: * A JAXB Provider has to implement five methods (getUnmarshallerHandler, ohair@286: * unmarshal(Node), unmarshal(XMLReader,InputSource), ohair@286: * unmarshal(XMLStreamReader), and unmarshal(XMLEventReader). ohair@286: * ohair@286: * @author
ohair@286: * Note: I named this method isValidating() to stay in-line ohair@286: * with JAXP, as opposed to naming it getValidating(). ohair@286: * ohair@286: * @return true if the Unmarshaller is configured to validate during ohair@286: * unmarshal operations, false otherwise ohair@286: * @throws JAXBException if an error occurs while retrieving the validating ohair@286: * flag ohair@286: */ ohair@286: public boolean isValidating() throws JAXBException { ohair@286: return validating; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Allow an application to register a validation event handler. ohair@286: *
ohair@286: * The validation event handler will be called by the JAXB Provider if any ohair@286: * validation errors are encountered during calls to any of the ohair@286: * unmarshal methods. If the client application does not register ohair@286: * a validation event handler before invoking the unmarshal methods, then ohair@286: * all validation events will be silently ignored and may result in ohair@286: * unexpected behaviour. ohair@286: * ohair@286: * @param handler the validation event handler ohair@286: * @throws JAXBException if an error was encountered while setting the ohair@286: * event handler ohair@286: */ ohair@286: public void setEventHandler(ValidationEventHandler handler) ohair@286: throws JAXBException { ohair@286: ohair@286: if( handler == null ) { ohair@286: eventHandler = new DefaultValidationEventHandler(); ohair@286: } else { ohair@286: eventHandler = handler; ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Specifies whether or not the Unmarshaller should validate during ohair@286: * unmarshal operations. By default, the Unmarshaller does ohair@286: * not validate. ohair@286: *
ohair@286: * This method may only be invoked before or after calling one of the ohair@286: * unmarshal methods. ohair@286: * ohair@286: * @param validating true if the Unmarshaller should validate during ohair@286: * unmarshal, false otherwise ohair@286: * @throws JAXBException if an error occurred while enabling or disabling ohair@286: * validation at unmarshal time ohair@286: */ ohair@286: public void setValidating(boolean validating) throws JAXBException { ohair@286: this.validating = validating; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Return the current event handler or the default event handler if one ohair@286: * hasn't been set. ohair@286: * ohair@286: * @return the current ValidationEventHandler or the default event handler ohair@286: * if it hasn't been set ohair@286: * @throws JAXBException if an error was encountered while getting the ohair@286: * current event handler ohair@286: */ ohair@286: public ValidationEventHandler getEventHandler() throws JAXBException { ohair@286: return eventHandler; ohair@286: } ohair@286: ohair@286: ohair@286: /** ohair@286: * Creates an UnmarshalException from a SAXException. ohair@286: * ohair@286: * This is an utility method provided for the derived classes. ohair@286: * ohair@286: *
ohair@286: * When a provider-implemented ContentHandler wants to throw a ohair@286: * JAXBException, it needs to wrap the exception by a SAXException. ohair@286: * If the unmarshaller implementation blindly wrap SAXException ohair@286: * by JAXBException, such an exception will be a JAXBException ohair@286: * wrapped by a SAXException wrapped by another JAXBException. ohair@286: * This is silly. ohair@286: * ohair@286: *
ohair@286: * This method checks the nested exception of SAXException
ohair@286: * and reduce those excessive wrapping.
ohair@286: *
ohair@286: * @return the resulting UnmarshalException
ohair@286: */
ohair@286: protected UnmarshalException createUnmarshalException( SAXException e ) {
ohair@286: // check the nested exception to see if it's an UnmarshalException
ohair@286: Exception nested = e.getException();
ohair@286: if(nested instanceof UnmarshalException)
ohair@286: return (UnmarshalException)nested;
ohair@286:
ohair@286: if(nested instanceof RuntimeException)
ohair@286: // typically this is an unexpected exception,
ohair@286: // just throw it rather than wrap it, so that the full stack
ohair@286: // trace can be displayed.
ohair@286: throw (RuntimeException)nested;
ohair@286:
ohair@286:
ohair@286: // otherwise simply wrap it
ohair@286: if(nested!=null)
ohair@286: return new UnmarshalException(nested);
ohair@286: else
ohair@286: return new UnmarshalException(e);
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Default implementation of the setProperty method always
ohair@286: * throws PropertyException since there are no required
ohair@286: * properties. If a provider needs to handle additional
ohair@286: * properties, it should override this method in a derived class.
ohair@286: */
ohair@286: public void setProperty( String name, Object value )
ohair@286: throws PropertyException {
ohair@286:
ohair@286: if( name == null ) {
ohair@286: throw new IllegalArgumentException(
ohair@286: Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) );
ohair@286: }
ohair@286:
ohair@286: throw new PropertyException(name, value);
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Default implementation of the getProperty method always
ohair@286: * throws PropertyException since there are no required
ohair@286: * properties. If a provider needs to handle additional
ohair@286: * properties, it should override this method in a derived class.
ohair@286: */
ohair@286: public Object getProperty( String name )
ohair@286: throws PropertyException {
ohair@286:
ohair@286: if( name == null ) {
ohair@286: throw new IllegalArgumentException(
ohair@286: Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) );
ohair@286: }
ohair@286:
ohair@286: throw new PropertyException(name);
ohair@286: }
ohair@286:
ohair@286: public Object unmarshal(XMLEventReader reader) throws JAXBException {
ohair@286:
ohair@286: throw new UnsupportedOperationException();
ohair@286: }
ohair@286:
ohair@286: public Object unmarshal(XMLStreamReader reader) throws JAXBException {
ohair@286:
ohair@286: throw new UnsupportedOperationException();
ohair@286: }
ohair@286:
ohair@286: public