|
1 /* |
|
2 * Copyright (c) 2003, 2010, 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 org.xml.sax.ContentHandler; |
|
29 import org.xml.sax.DTDHandler; |
|
30 import org.xml.sax.EntityResolver; |
|
31 import org.xml.sax.ErrorHandler; |
|
32 import org.xml.sax.InputSource; |
|
33 import org.xml.sax.SAXException; |
|
34 import org.xml.sax.SAXNotRecognizedException; |
|
35 import org.xml.sax.SAXParseException; |
|
36 import org.xml.sax.XMLReader; |
|
37 import org.xml.sax.ext.LexicalHandler; |
|
38 import org.xml.sax.helpers.XMLFilterImpl; |
|
39 |
|
40 import javax.xml.bind.JAXBContext; |
|
41 import javax.xml.bind.JAXBException; |
|
42 import javax.xml.bind.Marshaller; |
|
43 import javax.xml.transform.sax.SAXSource; |
|
44 |
|
45 /** |
|
46 * JAXP {@link javax.xml.transform.Source} implementation |
|
47 * that marshals a JAXB-generated object. |
|
48 * |
|
49 * <p> |
|
50 * This utility class is useful to combine JAXB with |
|
51 * other Java/XML technologies. |
|
52 * |
|
53 * <p> |
|
54 * The following example shows how to use JAXB to marshal a document |
|
55 * for transformation by XSLT. |
|
56 * |
|
57 * <blockquote> |
|
58 * <pre> |
|
59 * MyObject o = // get JAXB content tree |
|
60 * |
|
61 * // jaxbContext is a JAXBContext object from which 'o' is created. |
|
62 * JAXBSource source = new JAXBSource( jaxbContext, o ); |
|
63 * |
|
64 * // set up XSLT transformation |
|
65 * TransformerFactory tf = TransformerFactory.newInstance(); |
|
66 * Transformer t = tf.newTransformer(new StreamSource("test.xsl")); |
|
67 * |
|
68 * // run transformation |
|
69 * t.transform(source,new StreamResult(System.out)); |
|
70 * </pre> |
|
71 * </blockquote> |
|
72 * |
|
73 * <p> |
|
74 * The fact that JAXBSource derives from SAXSource is an implementation |
|
75 * detail. Thus in general applications are strongly discouraged from |
|
76 * accessing methods defined on SAXSource. In particular, |
|
77 * the setXMLReader and setInputSource methods shall never be called. |
|
78 * The XMLReader object obtained by the getXMLReader method shall |
|
79 * be used only for parsing the InputSource object returned by |
|
80 * the getInputSource method. |
|
81 * |
|
82 * <p> |
|
83 * Similarly the InputSource object obtained by the getInputSource |
|
84 * method shall be used only for being parsed by the XMLReader object |
|
85 * returned by the getXMLReader. |
|
86 * |
|
87 * @author |
|
88 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) |
|
89 */ |
|
90 public class JAXBSource extends SAXSource { |
|
91 |
|
92 /** |
|
93 * Creates a new {@link javax.xml.transform.Source} for the given content object. |
|
94 * |
|
95 * @param context |
|
96 * JAXBContext that was used to create |
|
97 * <code>contentObject</code>. This context is used |
|
98 * to create a new instance of marshaller and must not be null. |
|
99 * @param contentObject |
|
100 * An instance of a JAXB-generated class, which will be |
|
101 * used as a {@link javax.xml.transform.Source} (by marshalling it into XML). It must |
|
102 * not be null. |
|
103 * @throws JAXBException if an error is encountered while creating the |
|
104 * JAXBSource or if either of the parameters are null. |
|
105 */ |
|
106 public JAXBSource( JAXBContext context, Object contentObject ) |
|
107 throws JAXBException { |
|
108 |
|
109 this( |
|
110 ( context == null ) ? |
|
111 assertionFailed( Messages.format( Messages.SOURCE_NULL_CONTEXT ) ) : |
|
112 context.createMarshaller(), |
|
113 |
|
114 ( contentObject == null ) ? |
|
115 assertionFailed( Messages.format( Messages.SOURCE_NULL_CONTENT ) ) : |
|
116 contentObject); |
|
117 } |
|
118 |
|
119 /** |
|
120 * Creates a new {@link javax.xml.transform.Source} for the given content object. |
|
121 * |
|
122 * @param marshaller |
|
123 * A marshaller instance that will be used to marshal |
|
124 * <code>contentObject</code> into XML. This must be |
|
125 * created from a JAXBContext that was used to build |
|
126 * <code>contentObject</code> and must not be null. |
|
127 * @param contentObject |
|
128 * An instance of a JAXB-generated class, which will be |
|
129 * used as a {@link javax.xml.transform.Source} (by marshalling it into XML). It must |
|
130 * not be null. |
|
131 * @throws JAXBException if an error is encountered while creating the |
|
132 * JAXBSource or if either of the parameters are null. |
|
133 */ |
|
134 public JAXBSource( Marshaller marshaller, Object contentObject ) |
|
135 throws JAXBException { |
|
136 |
|
137 if( marshaller == null ) |
|
138 throw new JAXBException( |
|
139 Messages.format( Messages.SOURCE_NULL_MARSHALLER ) ); |
|
140 |
|
141 if( contentObject == null ) |
|
142 throw new JAXBException( |
|
143 Messages.format( Messages.SOURCE_NULL_CONTENT ) ); |
|
144 |
|
145 this.marshaller = marshaller; |
|
146 this.contentObject = contentObject; |
|
147 |
|
148 super.setXMLReader(pseudoParser); |
|
149 // pass a dummy InputSource. We don't care |
|
150 super.setInputSource(new InputSource()); |
|
151 } |
|
152 |
|
153 private final Marshaller marshaller; |
|
154 private final Object contentObject; |
|
155 |
|
156 // this object will pretend as an XMLReader. |
|
157 // no matter what parameter is specified to the parse method, |
|
158 // it just parse the contentObject. |
|
159 private final XMLReader pseudoParser = new XMLReader() { |
|
160 public boolean getFeature(String name) throws SAXNotRecognizedException { |
|
161 if(name.equals("http://xml.org/sax/features/namespaces")) |
|
162 return true; |
|
163 if(name.equals("http://xml.org/sax/features/namespace-prefixes")) |
|
164 return false; |
|
165 throw new SAXNotRecognizedException(name); |
|
166 } |
|
167 |
|
168 public void setFeature(String name, boolean value) throws SAXNotRecognizedException { |
|
169 if(name.equals("http://xml.org/sax/features/namespaces") && value) |
|
170 return; |
|
171 if(name.equals("http://xml.org/sax/features/namespace-prefixes") && !value) |
|
172 return; |
|
173 throw new SAXNotRecognizedException(name); |
|
174 } |
|
175 |
|
176 public Object getProperty(String name) throws SAXNotRecognizedException { |
|
177 if( "http://xml.org/sax/properties/lexical-handler".equals(name) ) { |
|
178 return lexicalHandler; |
|
179 } |
|
180 throw new SAXNotRecognizedException(name); |
|
181 } |
|
182 |
|
183 public void setProperty(String name, Object value) throws SAXNotRecognizedException { |
|
184 if( "http://xml.org/sax/properties/lexical-handler".equals(name) ) { |
|
185 this.lexicalHandler = (LexicalHandler)value; |
|
186 return; |
|
187 } |
|
188 throw new SAXNotRecognizedException(name); |
|
189 } |
|
190 |
|
191 private LexicalHandler lexicalHandler; |
|
192 |
|
193 // we will store this value but never use it by ourselves. |
|
194 private EntityResolver entityResolver; |
|
195 public void setEntityResolver(EntityResolver resolver) { |
|
196 this.entityResolver = resolver; |
|
197 } |
|
198 public EntityResolver getEntityResolver() { |
|
199 return entityResolver; |
|
200 } |
|
201 |
|
202 private DTDHandler dtdHandler; |
|
203 public void setDTDHandler(DTDHandler handler) { |
|
204 this.dtdHandler = handler; |
|
205 } |
|
206 public DTDHandler getDTDHandler() { |
|
207 return dtdHandler; |
|
208 } |
|
209 |
|
210 // SAX allows ContentHandler to be changed during the parsing, |
|
211 // but JAXB doesn't. So this repeater will sit between those |
|
212 // two components. |
|
213 private XMLFilterImpl repeater = new XMLFilterImpl(); |
|
214 |
|
215 public void setContentHandler(ContentHandler handler) { |
|
216 repeater.setContentHandler(handler); |
|
217 } |
|
218 public ContentHandler getContentHandler() { |
|
219 return repeater.getContentHandler(); |
|
220 } |
|
221 |
|
222 private ErrorHandler errorHandler; |
|
223 public void setErrorHandler(ErrorHandler handler) { |
|
224 this.errorHandler = handler; |
|
225 } |
|
226 public ErrorHandler getErrorHandler() { |
|
227 return errorHandler; |
|
228 } |
|
229 |
|
230 public void parse(InputSource input) throws SAXException { |
|
231 parse(); |
|
232 } |
|
233 |
|
234 public void parse(String systemId) throws SAXException { |
|
235 parse(); |
|
236 } |
|
237 |
|
238 public void parse() throws SAXException { |
|
239 // parses a content object by using the given marshaller |
|
240 // SAX events will be sent to the repeater, and the repeater |
|
241 // will further forward it to an appropriate component. |
|
242 try { |
|
243 marshaller.marshal( contentObject, repeater ); |
|
244 } catch( JAXBException e ) { |
|
245 // wrap it to a SAXException |
|
246 SAXParseException se = |
|
247 new SAXParseException( e.getMessage(), |
|
248 null, null, -1, -1, e ); |
|
249 |
|
250 // if the consumer sets an error handler, it is our responsibility |
|
251 // to notify it. |
|
252 if(errorHandler!=null) |
|
253 errorHandler.fatalError(se); |
|
254 |
|
255 // this is a fatal error. Even if the error handler |
|
256 // returns, we will abort anyway. |
|
257 throw se; |
|
258 } |
|
259 } |
|
260 }; |
|
261 |
|
262 /** |
|
263 * Hook to throw exception from the middle of a contructor chained call |
|
264 * to this |
|
265 */ |
|
266 private static Marshaller assertionFailed( String message ) |
|
267 throws JAXBException { |
|
268 |
|
269 throw new JAXBException( message ); |
|
270 } |
|
271 } |