001 /*
002 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package javax.xml.bind.util;
026
027 import javax.xml.bind.JAXBContext;
028 import javax.xml.bind.JAXBException;
029 import javax.xml.bind.Unmarshaller;
030 import javax.xml.bind.UnmarshallerHandler;
031 import javax.xml.transform.sax.SAXResult;
032
033 /**
034 * JAXP {@link javax.xml.transform.Result} implementation
035 * that unmarshals a JAXB object.
036 *
037 * <p>
038 * This utility class is useful to combine JAXB with
039 * other Java/XML technologies.
040 *
041 * <p>
042 * The following example shows how to use JAXB to unmarshal a document
043 * resulting from an XSLT transformation.
044 *
045 * <blockquote>
046 * <pre>
047 * JAXBResult result = new JAXBResult(
048 * JAXBContext.newInstance("org.acme.foo") );
049 *
050 * // set up XSLT transformation
051 * TransformerFactory tf = TransformerFactory.newInstance();
052 * Transformer t = tf.newTransformer(new StreamSource("test.xsl"));
053 *
054 * // run transformation
055 * t.transform(new StreamSource("document.xml"),result);
056 *
057 * // obtain the unmarshalled content tree
058 * Object o = result.getResult();
059 * </pre>
060 * </blockquote>
061 *
062 * <p>
063 * The fact that JAXBResult derives from SAXResult is an implementation
064 * detail. Thus in general applications are strongly discouraged from
065 * accessing methods defined on SAXResult.
066 *
067 * <p>
068 * In particular it shall never attempt to call the setHandler,
069 * setLexicalHandler, and setSystemId methods.
070 *
071 * @author
072 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
073 */
074 public class JAXBResult extends SAXResult {
075
076 /**
077 * Creates a new instance that uses the specified
078 * JAXBContext to unmarshal.
079 *
080 * @param context The JAXBContext that will be used to create the
081 * necessary Unmarshaller. This parameter must not be null.
082 * @exception JAXBException if an error is encountered while creating the
083 * JAXBResult or if the context parameter is null.
084 */
085 public JAXBResult(JAXBContext context) throws JAXBException {
086 this ((context == null) ? assertionFailed() : context
087 .createUnmarshaller());
088 }
089
090 /**
091 * Creates a new instance that uses the specified
092 * Unmarshaller to unmarshal an object.
093 *
094 * <p>
095 * This JAXBResult object will use the specified Unmarshaller
096 * instance. It is the caller's responsibility not to use the
097 * same Unmarshaller for other purposes while it is being
098 * used by this object.
099 *
100 * <p>
101 * The primary purpose of this method is to allow the client
102 * to configure Unmarshaller. Unless you know what you are doing,
103 * it's easier and safer to pass a JAXBContext.
104 *
105 * @param _unmarshaller the unmarshaller. This parameter must not be null.
106 * @throws JAXBException if an error is encountered while creating the
107 * JAXBResult or the Unmarshaller parameter is null.
108 */
109 public JAXBResult(Unmarshaller _unmarshaller) throws JAXBException {
110 if (_unmarshaller == null)
111 throw new JAXBException(Messages
112 .format(Messages.RESULT_NULL_UNMARSHALLER));
113
114 this .unmarshallerHandler = _unmarshaller
115 .getUnmarshallerHandler();
116
117 super .setHandler(unmarshallerHandler);
118 }
119
120 /**
121 * Unmarshaller that will be used to unmarshal
122 * the input documents.
123 */
124 private final UnmarshallerHandler unmarshallerHandler;
125
126 /**
127 * Gets the unmarshalled object created by the transformation.
128 *
129 * @return
130 * Always return a non-null object.
131 *
132 * @exception IllegalStateException
133 * if this method is called before an object is unmarshalled.
134 *
135 * @exception JAXBException
136 * if there is any unmarshalling error.
137 * Note that the implementation is allowed to throw SAXException
138 * during the parsing when it finds an error.
139 */
140 public Object getResult() throws JAXBException {
141 return unmarshallerHandler.getResult();
142 }
143
144 /**
145 * Hook to throw exception from the middle of a contructor chained call
146 * to this
147 */
148 private static Unmarshaller assertionFailed() throws JAXBException {
149 throw new JAXBException(Messages
150 .format(Messages.RESULT_NULL_CONTEXT));
151 }
152 }
|