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.util; ohair@286: ohair@286: import javax.xml.bind.JAXBContext; ohair@286: import javax.xml.bind.JAXBException; ohair@286: import javax.xml.bind.Unmarshaller; ohair@286: import javax.xml.bind.UnmarshallerHandler; ohair@286: import javax.xml.transform.sax.SAXResult; ohair@286: ohair@286: /** ohair@286: * JAXP {@link javax.xml.transform.Result} implementation ohair@286: * that unmarshals a JAXB object. ohair@286: * ohair@286: *

ohair@286: * This utility class is useful to combine JAXB with ohair@286: * other Java/XML technologies. ohair@286: * ohair@286: *

ohair@286: * The following example shows how to use JAXB to unmarshal a document ohair@286: * resulting from an XSLT transformation. ohair@286: * ohair@286: *

ohair@286: *
ohair@286:  *       JAXBResult result = new JAXBResult(
ohair@286:  *         JAXBContext.newInstance("org.acme.foo") );
ohair@286:  *
ohair@286:  *       // set up XSLT transformation
ohair@286:  *       TransformerFactory tf = TransformerFactory.newInstance();
ohair@286:  *       Transformer t = tf.newTransformer(new StreamSource("test.xsl"));
ohair@286:  *
ohair@286:  *       // run transformation
ohair@286:  *       t.transform(new StreamSource("document.xml"),result);
ohair@286:  *
ohair@286:  *       // obtain the unmarshalled content tree
ohair@286:  *       Object o = result.getResult();
ohair@286:  *    
ohair@286: *
ohair@286: * ohair@286: *

ohair@286: * The fact that JAXBResult derives from SAXResult is an implementation ohair@286: * detail. Thus in general applications are strongly discouraged from ohair@286: * accessing methods defined on SAXResult. ohair@286: * ohair@286: *

ohair@286: * In particular it shall never attempt to call the setHandler, ohair@286: * setLexicalHandler, and setSystemId methods. ohair@286: * ohair@286: * @author ohair@286: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) ohair@286: */ ohair@286: public class JAXBResult extends SAXResult { ohair@286: ohair@286: /** ohair@286: * Creates a new instance that uses the specified ohair@286: * JAXBContext to unmarshal. ohair@286: * ohair@286: * @param context The JAXBContext that will be used to create the ohair@286: * necessary Unmarshaller. This parameter must not be null. ohair@286: * @exception JAXBException if an error is encountered while creating the ohair@286: * JAXBResult or if the context parameter is null. ohair@286: */ ohair@286: public JAXBResult( JAXBContext context ) throws JAXBException { ohair@286: this( ( context == null ) ? assertionFailed() : context.createUnmarshaller() ); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Creates a new instance that uses the specified ohair@286: * Unmarshaller to unmarshal an object. ohair@286: * ohair@286: *

ohair@286: * This JAXBResult object will use the specified Unmarshaller ohair@286: * instance. It is the caller's responsibility not to use the ohair@286: * same Unmarshaller for other purposes while it is being ohair@286: * used by this object. ohair@286: * ohair@286: *

ohair@286: * The primary purpose of this method is to allow the client ohair@286: * to configure Unmarshaller. Unless you know what you are doing, ohair@286: * it's easier and safer to pass a JAXBContext. ohair@286: * ohair@286: * @param _unmarshaller the unmarshaller. This parameter must not be null. ohair@286: * @throws JAXBException if an error is encountered while creating the ohair@286: * JAXBResult or the Unmarshaller parameter is null. ohair@286: */ ohair@286: public JAXBResult( Unmarshaller _unmarshaller ) throws JAXBException { ohair@286: if( _unmarshaller == null ) ohair@286: throw new JAXBException( ohair@286: Messages.format( Messages.RESULT_NULL_UNMARSHALLER ) ); ohair@286: ohair@286: this.unmarshallerHandler = _unmarshaller.getUnmarshallerHandler(); ohair@286: ohair@286: super.setHandler(unmarshallerHandler); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Unmarshaller that will be used to unmarshal ohair@286: * the input documents. ohair@286: */ ohair@286: private final UnmarshallerHandler unmarshallerHandler; ohair@286: ohair@286: /** ohair@286: * Gets the unmarshalled object created by the transformation. ohair@286: * ohair@286: * @return ohair@286: * Always return a non-null object. ohair@286: * ohair@286: * @exception IllegalStateException ohair@286: * if this method is called before an object is unmarshalled. ohair@286: * ohair@286: * @exception JAXBException ohair@286: * if there is any unmarshalling error. ohair@286: * Note that the implementation is allowed to throw SAXException ohair@286: * during the parsing when it finds an error. ohair@286: */ ohair@286: public Object getResult() throws JAXBException { ohair@286: return unmarshallerHandler.getResult(); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Hook to throw exception from the middle of a contructor chained call ohair@286: * to this ohair@286: */ ohair@286: private static Unmarshaller assertionFailed() throws JAXBException { ohair@286: throw new JAXBException( Messages.format( Messages.RESULT_NULL_CONTEXT ) ); ohair@286: } ohair@286: }