KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.xml.sax.ContentHandler JavaDoc;
8 import org.xml.sax.DTDHandler JavaDoc;
9 import org.xml.sax.EntityResolver JavaDoc;
10 import org.xml.sax.ErrorHandler JavaDoc;
11 import org.xml.sax.InputSource JavaDoc;
12 import org.xml.sax.SAXException JavaDoc;
13 import org.xml.sax.SAXNotRecognizedException JavaDoc;
14 import org.xml.sax.SAXParseException JavaDoc;
15 import org.xml.sax.XMLReader JavaDoc;
16 import org.xml.sax.ext.LexicalHandler JavaDoc;
17 import org.xml.sax.helpers.XMLFilterImpl JavaDoc;
18
19 import javax.xml.bind.JAXBContext;
20 import javax.xml.bind.JAXBException;
21 import javax.xml.bind.Marshaller;
22 import javax.xml.transform.sax.SAXSource JavaDoc;
23
24 /**
25  * JAXP {@link javax.xml.transform.Source} implementation
26  * that marshals a JAXB-generated object.
27  *
28  * <p>
29  * This utility class is useful to combine JAXB with
30  * other Java/XML technologies.
31  *
32  * <p>
33  * The following example shows how to use JAXB to marshal a document
34  * for transformation by XSLT.
35  *
36  * <blockquote>
37  * <pre>
38  * MyObject o = // get JAXB content tree
39  *
40  * // jaxbContext is a JAXBContext object from which 'o' is created.
41  * JAXBSource source = new JAXBSource( jaxbContext, o );
42  *
43  * // set up XSLT transformation
44  * TransformerFactory tf = TransformerFactory.newInstance();
45  * Transformer t = tf.newTransformer(new StreamSource("test.xsl"));
46  *
47  * // run transformation
48  * t.transform(source,new StreamResult(System.out));
49  * </pre>
50  * </blockquote>
51  *
52  * <p>
53  * The fact that JAXBSource derives from SAXSource is an implementation
54  * detail. Thus in general applications are strongly discouraged from
55  * accessing methods defined on SAXSource. In particular,
56  * the setXMLReader and setInputSource methods shall never be called.
57  * The XMLReader object obtained by the getXMLReader method shall
58  * be used only for parsing the InputSource object returned by
59  * the getInputSource method.
60  *
61  * <p>
62  * Similarly the InputSource object obtained by the getInputSource
63  * method shall be used only for being parsed by the XMLReader object
64  * returned by the getXMLReader.
65  *
66  * @author
67  * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
68  */

69 public class JAXBSource extends SAXSource JavaDoc {
70
71     /**
72      * Creates a new {@link javax.xml.transform.Source} for the given content object.
73      *
74      * @param context
75      * JAXBContext that was used to create
76      * <code>contentObject</code>. This context is used
77      * to create a new instance of marshaller and must not be null.
78      * @param contentObject
79      * An instance of a JAXB-generated class, which will be
80      * used as a {@link javax.xml.transform.Source} (by marshalling it into XML). It must
81      * not be null.
82      * @throws JAXBException if an error is encountered while creating the
83      * JAXBSource or if either of the parameters are null.
84      */

85     public JAXBSource( JAXBContext context, Object JavaDoc contentObject )
86         throws JAXBException {
87             
88         this(
89             ( context == null ) ?
90                 assertionFailed( Messages.format( Messages.SOURCE_NULL_CONTEXT ) ) :
91                 context.createMarshaller(),
92                 
93             ( contentObject == null ) ?
94                 assertionFailed( Messages.format( Messages.SOURCE_NULL_CONTENT ) ) :
95                 contentObject);
96     }
97     
98     /**
99      * Creates a new {@link javax.xml.transform.Source} for the given content object.
100      *
101      * @param marshaller
102      * A marshaller instance that will be used to marshal
103      * <code>contentObject</code> into XML. This must be
104      * created from a JAXBContext that was used to build
105      * <code>contentObject</code> and must not be null.
106      * @param contentObject
107      * An instance of a JAXB-generated class, which will be
108      * used as a {@link javax.xml.transform.Source} (by marshalling it into XML). It must
109      * not be null.
110      * @throws JAXBException if an error is encountered while creating the
111      * JAXBSource or if either of the parameters are null.
112      */

113     public JAXBSource( Marshaller marshaller, Object JavaDoc contentObject )
114         throws JAXBException {
115             
116         if( marshaller == null )
117             throw new JAXBException(
118                 Messages.format( Messages.SOURCE_NULL_MARSHALLER ) );
119                 
120         if( contentObject == null )
121             throw new JAXBException(
122                 Messages.format( Messages.SOURCE_NULL_CONTENT ) );
123             
124         this.marshaller = marshaller;
125         this.contentObject = contentObject;
126         
127         super.setXMLReader(pseudoParser);
128         // pass a dummy InputSource. We don't care
129
super.setInputSource(new InputSource JavaDoc());
130     }
131     
132     private final Marshaller marshaller;
133     private final Object JavaDoc contentObject;
134     
135     // this object will pretend as an XMLReader.
136
// no matter what parameter is specified to the parse method,
137
// it just parse the contentObject.
138
private final XMLReader JavaDoc pseudoParser = new XMLReader JavaDoc() {
139         public boolean getFeature(String JavaDoc name) throws SAXNotRecognizedException JavaDoc {
140             if(name.equals("http://xml.org/sax/features/namespaces"))
141                 return true;
142             if(name.equals("http://xml.org/sax/features/namespace-prefixes"))
143                 return false;
144             throw new SAXNotRecognizedException JavaDoc(name);
145         }
146
147         public void setFeature(String JavaDoc name, boolean value) throws SAXNotRecognizedException JavaDoc {
148             if(name.equals("http://xml.org/sax/features/namespaces") && value)
149                 return;
150             if(name.equals("http://xml.org/sax/features/namespace-prefixes") && !value)
151                 return;
152             throw new SAXNotRecognizedException JavaDoc(name);
153         }
154
155         public Object JavaDoc getProperty(String JavaDoc name) throws SAXNotRecognizedException JavaDoc {
156             if( "http://xml.org/sax/properties/lexical-handler".equals(name) ) {
157                 return lexicalHandler;
158             }
159             throw new SAXNotRecognizedException JavaDoc(name);
160         }
161
162         public void setProperty(String JavaDoc name, Object JavaDoc value) throws SAXNotRecognizedException JavaDoc {
163             if( "http://xml.org/sax/properties/lexical-handler".equals(name) ) {
164                 this.lexicalHandler = (LexicalHandler JavaDoc)value;
165                 return;
166             }
167             throw new SAXNotRecognizedException JavaDoc(name);
168         }
169
170         private LexicalHandler JavaDoc lexicalHandler;
171
172         // we will store this value but never use it by ourselves.
173
private EntityResolver JavaDoc entityResolver;
174         public void setEntityResolver(EntityResolver JavaDoc resolver) {
175             this.entityResolver = resolver;
176         }
177         public EntityResolver JavaDoc getEntityResolver() {
178             return entityResolver;
179         }
180
181         private DTDHandler JavaDoc dtdHandler;
182         public void setDTDHandler(DTDHandler JavaDoc handler) {
183             this.dtdHandler = handler;
184         }
185         public DTDHandler JavaDoc getDTDHandler() {
186             return dtdHandler;
187         }
188
189         // SAX allows ContentHandler to be changed during the parsing,
190
// but JAXB doesn't. So this repeater will sit between those
191
// two components.
192
private XMLFilterImpl JavaDoc repeater = new XMLFilterImpl JavaDoc();
193
194         public void setContentHandler(ContentHandler JavaDoc handler) {
195             repeater.setContentHandler(handler);
196         }
197         public ContentHandler JavaDoc getContentHandler() {
198             return repeater.getContentHandler();
199         }
200
201         private ErrorHandler JavaDoc errorHandler;
202         public void setErrorHandler(ErrorHandler JavaDoc handler) {
203             this.errorHandler = handler;
204         }
205         public ErrorHandler JavaDoc getErrorHandler() {
206             return errorHandler;
207         }
208
209         public void parse(InputSource JavaDoc input) throws SAXException JavaDoc {
210             parse();
211         }
212
213         public void parse(String JavaDoc systemId) throws SAXException JavaDoc {
214             parse();
215         }
216
217         public void parse() throws SAXException JavaDoc {
218             // parses a content object by using the given marshaller
219
// SAX events will be sent to the repeater, and the repeater
220
// will further forward it to an appropriate component.
221
try {
222                 marshaller.marshal( contentObject, repeater );
223             } catch( JAXBException e ) {
224                 // wrap it to a SAXException
225
SAXParseException JavaDoc se =
226                     new SAXParseException JavaDoc( e.getMessage(),
227                         null, null, -1, -1, e );
228
229                 // if the consumer sets an error handler, it is our responsibility
230
// to notify it.
231
if(errorHandler!=null)
232                     errorHandler.fatalError(se);
233
234                 // this is a fatal error. Even if the error handler
235
// returns, we will abort anyway.
236
throw se;
237             }
238         }
239     };
240
241     /**
242      * Hook to throw exception from the middle of a contructor chained call
243      * to this
244      */

245     private static Marshaller assertionFailed( String JavaDoc message )
246         throws JAXBException {
247             
248         throw new JAXBException( message );
249     }
250 }
251
Popular Tags