|
1 /* |
|
2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package javax.xml.bind.util; |
|
27 |
|
28 import javax.xml.bind.JAXBContext; |
|
29 import javax.xml.bind.JAXBException; |
|
30 import javax.xml.bind.Unmarshaller; |
|
31 import javax.xml.bind.UnmarshallerHandler; |
|
32 import javax.xml.transform.sax.SAXResult; |
|
33 |
|
34 /** |
|
35 * JAXP {@link javax.xml.transform.Result} implementation |
|
36 * that unmarshals a JAXB object. |
|
37 * |
|
38 * <p> |
|
39 * This utility class is useful to combine JAXB with |
|
40 * other Java/XML technologies. |
|
41 * |
|
42 * <p> |
|
43 * The following example shows how to use JAXB to unmarshal a document |
|
44 * resulting from an XSLT transformation. |
|
45 * |
|
46 * <blockquote> |
|
47 * <pre> |
|
48 * JAXBResult result = new JAXBResult( |
|
49 * JAXBContext.newInstance("org.acme.foo") ); |
|
50 * |
|
51 * // set up XSLT transformation |
|
52 * TransformerFactory tf = TransformerFactory.newInstance(); |
|
53 * Transformer t = tf.newTransformer(new StreamSource("test.xsl")); |
|
54 * |
|
55 * // run transformation |
|
56 * t.transform(new StreamSource("document.xml"),result); |
|
57 * |
|
58 * // obtain the unmarshalled content tree |
|
59 * Object o = result.getResult(); |
|
60 * </pre> |
|
61 * </blockquote> |
|
62 * |
|
63 * <p> |
|
64 * The fact that JAXBResult derives from SAXResult is an implementation |
|
65 * detail. Thus in general applications are strongly discouraged from |
|
66 * accessing methods defined on SAXResult. |
|
67 * |
|
68 * <p> |
|
69 * In particular it shall never attempt to call the setHandler, |
|
70 * setLexicalHandler, and setSystemId methods. |
|
71 * |
|
72 * @author |
|
73 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) |
|
74 */ |
|
75 public class JAXBResult extends SAXResult { |
|
76 |
|
77 /** |
|
78 * Creates a new instance that uses the specified |
|
79 * JAXBContext to unmarshal. |
|
80 * |
|
81 * @param context The JAXBContext that will be used to create the |
|
82 * necessary Unmarshaller. This parameter must not be null. |
|
83 * @exception JAXBException if an error is encountered while creating the |
|
84 * JAXBResult or if the context parameter is null. |
|
85 */ |
|
86 public JAXBResult( JAXBContext context ) throws JAXBException { |
|
87 this( ( context == null ) ? assertionFailed() : context.createUnmarshaller() ); |
|
88 } |
|
89 |
|
90 /** |
|
91 * Creates a new instance that uses the specified |
|
92 * Unmarshaller to unmarshal an object. |
|
93 * |
|
94 * <p> |
|
95 * This JAXBResult object will use the specified Unmarshaller |
|
96 * instance. It is the caller's responsibility not to use the |
|
97 * same Unmarshaller for other purposes while it is being |
|
98 * used by this object. |
|
99 * |
|
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( |
|
112 Messages.format( Messages.RESULT_NULL_UNMARSHALLER ) ); |
|
113 |
|
114 this.unmarshallerHandler = _unmarshaller.getUnmarshallerHandler(); |
|
115 |
|
116 super.setHandler(unmarshallerHandler); |
|
117 } |
|
118 |
|
119 /** |
|
120 * Unmarshaller that will be used to unmarshal |
|
121 * the input documents. |
|
122 */ |
|
123 private final UnmarshallerHandler unmarshallerHandler; |
|
124 |
|
125 /** |
|
126 * Gets the unmarshalled object created by the transformation. |
|
127 * |
|
128 * @return |
|
129 * Always return a non-null object. |
|
130 * |
|
131 * @exception IllegalStateException |
|
132 * if this method is called before an object is unmarshalled. |
|
133 * |
|
134 * @exception JAXBException |
|
135 * if there is any unmarshalling error. |
|
136 * Note that the implementation is allowed to throw SAXException |
|
137 * during the parsing when it finds an error. |
|
138 */ |
|
139 public Object getResult() throws JAXBException { |
|
140 return unmarshallerHandler.getResult(); |
|
141 } |
|
142 |
|
143 /** |
|
144 * Hook to throw exception from the middle of a contructor chained call |
|
145 * to this |
|
146 */ |
|
147 private static Unmarshaller assertionFailed() throws JAXBException { |
|
148 throw new JAXBException( Messages.format( Messages.RESULT_NULL_CONTEXT ) ); |
|
149 } |
|
150 } |