KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > xml > bind > util > JAXBResult


1 /*
2  * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
3  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
4  */

5 package javax.xml.bind.util;
6
7 import javax.xml.bind.JAXBContext;
8 import javax.xml.bind.JAXBException;
9 import javax.xml.bind.Unmarshaller;
10 import javax.xml.bind.UnmarshallerHandler;
11 import javax.xml.transform.sax.SAXResult JavaDoc;
12
13 /**
14  * JAXP {@link javax.xml.transform.Result} implementation
15  * that unmarshals a JAXB object.
16  *
17  * <p>
18  * This utility class is useful to combine JAXB with
19  * other Java/XML technologies.
20  *
21  * <p>
22  * The following example shows how to use JAXB to unmarshal a document
23  * resulting from an XSLT transformation.
24  *
25  * <blockquote>
26  * <pre>
27  * JAXBResult result = new JAXBResult(
28  * JAXBContext.newInstance("org.acme.foo") );
29  *
30  * // set up XSLT transformation
31  * TransformerFactory tf = TransformerFactory.newInstance();
32  * Transformer t = tf.newTransformer(new StreamSource("test.xsl"));
33  *
34  * // run transformation
35  * t.transform(new StreamSource("document.xml"),result);
36  *
37  * // obtain the unmarshalled content tree
38  * Object o = result.getResult();
39  * </pre>
40  * </blockquote>
41  *
42  * <p>
43  * The fact that JAXBResult derives from SAXResult is an implementation
44  * detail. Thus in general applications are strongly discouraged from
45  * accessing methods defined on SAXResult.
46  *
47  * <p>
48  * In particular it shall never attempt to call the setHandler,
49  * setLexicalHandler, and setSystemId methods.
50  *
51  * @author
52  * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
53  */

54 public class JAXBResult extends SAXResult JavaDoc {
55
56     /**
57      * Creates a new instance that uses the specified
58      * JAXBContext to unmarshal.
59      *
60      * @param context The JAXBContext that will be used to create the
61      * necessary Unmarshaller. This parameter must not be null.
62      * @exception JAXBException if an error is encountered while creating the
63      * JAXBResult or if the context parameter is null.
64      */

65     public JAXBResult( JAXBContext context ) throws JAXBException {
66         this( ( context == null ) ? assertionFailed() : context.createUnmarshaller() );
67     }
68     
69     /**
70      * Creates a new instance that uses the specified
71      * Unmarshaller to unmarshal an object.
72      *
73      * <p>
74      * This JAXBResult object will use the specified Unmarshaller
75      * instance. It is the caller's responsibility not to use the
76      * same Unmarshaller for other purposes while it is being
77      * used by this object.
78      *
79      * <p>
80      * The primary purpose of this method is to allow the client
81      * to configure Unmarshaller. Unless you know what you are doing,
82      * it's easier and safer to pass a JAXBContext.
83      *
84      * @param _unmarshaller the unmarshaller. This parameter must not be null.
85      * @throws JAXBException if an error is encountered while creating the
86      * JAXBResult or the Unmarshaller parameter is null.
87      */

88     public JAXBResult( Unmarshaller _unmarshaller ) throws JAXBException {
89         if( _unmarshaller == null )
90             throw new JAXBException(
91                 Messages.format( Messages.RESULT_NULL_UNMARSHALLER ) );
92             
93         this.unmarshallerHandler = _unmarshaller.getUnmarshallerHandler();
94         
95         super.setHandler(unmarshallerHandler);
96     }
97     
98     /**
99      * Unmarshaller that will be used to unmarshal
100      * the input documents.
101      */

102     private final UnmarshallerHandler unmarshallerHandler;
103
104     /**
105      * Gets the unmarshalled object created by the transformation.
106      *
107      * @return
108      * Always return a non-null object.
109      *
110      * @exception IllegalStateException
111      * if this method is called before an object is unmarshalled.
112      *
113      * @exception JAXBException
114      * if there is any unmarshalling error.
115      * Note that the implementation is allowed to throw SAXException
116      * during the parsing when it finds an error.
117      */

118     public Object JavaDoc getResult() throws JAXBException {
119         return unmarshallerHandler.getResult();
120     }
121     
122     /**
123      * Hook to throw exception from the middle of a contructor chained call
124      * to this
125      */

126     private static Unmarshaller assertionFailed() throws JAXBException {
127         throw new JAXBException( Messages.format( Messages.RESULT_NULL_CONTEXT ) );
128     }
129 }
130
Popular Tags