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