View Javadoc

1   /*
2    * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
3    *
4    * This software is open source.
5    * See the bottom of this file for the licence.
6    */
7   
8   package org.dom4j.jaxb;
9   
10  import java.io.File;
11  import java.io.FileOutputStream;
12  import java.io.IOException;
13  import java.io.OutputStream;
14  import java.io.Writer;
15  
16  import javax.xml.bind.JAXBException;
17  
18  import org.dom4j.Element;
19  import org.dom4j.io.OutputFormat;
20  import org.dom4j.io.XMLWriter;
21  
22  import org.xml.sax.SAXException;
23  
24  /***
25   * Writes {@link javax.xml.bind.Element}objects to an XML stream. {@link
26   * javax.xml.bind.Element} instances can be created using the ObjectFactory that
27   * is generated by the JAXB compiler.
28   * 
29   * @author Wonne Keysers (Realsoftware.be)
30   * 
31   * @see org.dom4j.io.XMLWriter
32   * @see javax.xml.bind.JAXBContext
33   */
34  public class JAXBWriter extends JAXBSupport {
35      private XMLWriter xmlWriter;
36  
37      private OutputFormat outputFormat;
38  
39      /***
40       * Creates a new JAXBWriter for the given JAXB context path. This is the
41       * Java package where JAXB can find the generated XML classes. This package
42       * MUST contain jaxb.properties!
43       * 
44       * @param contextPath
45       *            JAXB context path to be used
46       * 
47       * @see javax.xml.bind.JAXBContext
48       */
49      public JAXBWriter(String contextPath) {
50          super(contextPath);
51          outputFormat = new OutputFormat();
52      }
53  
54      /***
55       * Creates a new JAXBWriter for the given JAXB context path. The specied
56       * {@link org.dom4j.io.OutputFormat}will be used for writing the XML
57       * stream.
58       * 
59       * @param contextPath
60       *            JAXB context path to be used
61       * @param outputFormat
62       *            the DOM4J {@link org.dom4j.io.OutputFormat}to be used
63       * 
64       * @see javax.xml.bind.JAXBContext
65       */
66      public JAXBWriter(String contextPath, OutputFormat outputFormat) {
67          super(contextPath);
68          this.outputFormat = outputFormat;
69      }
70  
71      /***
72       * Creates a new JAXBWriter for the given JAXB context path, using the
73       * specified {@link java.lang.Classloader}. (This is the Java package where
74       * JAXB can find the generated XML classes. This package MUST contain
75       * jaxb.properties!)
76       * 
77       * @param contextPath
78       *            JAXB context path to be used
79       * @param classloader
80       *            the classloader to be used for loading JAXB
81       * 
82       * @see javax.xml.bind.JAXBContext
83       */
84      public JAXBWriter(String contextPath, ClassLoader classloader) {
85          super(contextPath, classloader);
86      }
87  
88      /***
89       * Creates a new JAXBWriter for the given JAXB context path, using the
90       * specified {@link java.lang.Classloader}. The specied {@link
91       * org.dom4j.io.OutputFormat} will be used while writing the XML stream.
92       * 
93       * @param contextPath
94       *            JAXB context path to be used
95       * @param classloader
96       *            the class loader to be used to load JAXB
97       * @param outputFormat
98       *            the DOM4J {@link org.dom4j.io.OutputFormat}to be used
99       * 
100      * @see javax.xml.bind.JAXBContext
101      */
102     public JAXBWriter(String contextPath, ClassLoader classloader,
103             OutputFormat outputFormat) {
104         super(contextPath, classloader);
105         this.outputFormat = outputFormat;
106     }
107 
108     /***
109      * Returns the OutputFormat that will be used when writing the XML stream.
110      * 
111      * @return Returns the output format.
112      */
113     public OutputFormat getOutputFormat() {
114         return outputFormat;
115     }
116 
117     /***
118      * Defines to write the resulting output to the specified {@link
119      * java.io.File}.
120      * 
121      * @param file
122      *            file to write to
123      * 
124      * @throws IOException
125      *             when the file cannot be found
126      */
127     public void setOutput(File file) throws IOException {
128         getWriter().setOutputStream(new FileOutputStream(file));
129     }
130 
131     /***
132      * Defines to write the resulting output to the specified {@link
133      * java.io.OutputStream}
134      * 
135      * @param outputStream
136      *            outputStream to write to.
137      * 
138      * @throws IOException
139      *             DOCUMENT ME!
140      */
141     public void setOutput(OutputStream outputStream) throws IOException {
142         getWriter().setOutputStream(outputStream);
143     }
144 
145     /***
146      * Defines to write the resulting output to the specified {@link Writer}.
147      * 
148      * @param writer
149      *            writer to write to
150      * 
151      * @throws IOException
152      */
153     public void setOutput(Writer writer) throws IOException {
154         getWriter().setWriter(writer);
155     }
156 
157     /***
158      * Start a document by writing the initial XML declaration to the output.
159      * This must be done prior to writing any other elements.
160      * 
161      * @throws IOException
162      *             if an error occured while writing the output
163      * @throws SAXException
164      *             thrown by the underlying SAX driver
165      */
166     public void startDocument() throws IOException, SAXException {
167         getWriter().startDocument();
168     }
169 
170     /***
171      * Stop writing the document to the output. This must be done when all other
172      * elements are finished.
173      * 
174      * @throws IOException
175      *             if an error occured while writing the output
176      * @throws SAXException
177      *             thrown by the underlying SAX driver
178      */
179     public void endDocument() throws IOException, SAXException {
180         getWriter().endDocument();
181     }
182 
183     /***
184      * Writes the specified {@link javax.xml.bind.Element}to the document.
185      * {@link javax.xml.bind.Element}instances can be created using the
186      * ObjectFactory that is generated by the JAXB compiler.
187      * 
188      * @param jaxbObject
189      * 
190      * @throws IOException
191      *             if an error occured while writing the output
192      * @throws JAXBException
193      *             when an error occured while marshalling the jaxbObject
194      */
195     public void write(javax.xml.bind.Element jaxbObject) throws IOException,
196             JAXBException {
197         getWriter().write(marshal(jaxbObject));
198     }
199 
200     /***
201      * Writes the closing tag of the specified {@link javax.xml.bind.Element}to
202      * the document. This method can be used for writing {@link
203      * javax.xml.bind.Element} instances can be created using the ObjectFactory
204      * that is generated by the JAXB compiler.
205      * 
206      * @param jaxbObject
207      *            the JAXB element to write
208      * 
209      * @throws IOException
210      *             if an error occured while writing the output
211      * @throws JAXBException
212      *             when an error occured while marshalling the jaxbObject
213      */
214     public void writeClose(javax.xml.bind.Element jaxbObject)
215             throws IOException, JAXBException {
216         getWriter().writeClose(marshal(jaxbObject));
217     }
218 
219     /***
220      * Writes the opening tag of the specified {@link javax.xml.bind.Element}to
221      * the document. {@link javax.xml.bind.Element}instances can be created
222      * using the ObjectFactory that is generated by the JAXB compiler.
223      * 
224      * @param jaxbObject
225      *            the JAXB element to write
226      * 
227      * @throws IOException
228      *             if an error occured while writing the output
229      * @throws JAXBException
230      *             when an error occured while marshalling the jaxbObject
231      */
232     public void writeOpen(javax.xml.bind.Element jaxbObject)
233             throws IOException, JAXBException {
234         getWriter().writeOpen(marshal(jaxbObject));
235     }
236 
237     /***
238      * Writes the specified {@link org.dom4j.Element}to the document.
239      * 
240      * @param element
241      *            the {@link org.dom4j.Element}to write
242      * 
243      * @throws IOException
244      *             if an error occured while writing the output
245      */
246     public void writeElement(Element element) throws IOException {
247         getWriter().write(element);
248     }
249 
250     /***
251      * Writes the closing tag of the specified {@link org.dom4j.Element}to the
252      * document.
253      * 
254      * @param element
255      *            the {@link org.dom4j.Element}to write
256      * 
257      * @throws IOException
258      *             if an error occured while writing the output
259      */
260     public void writeCloseElement(Element element) throws IOException {
261         getWriter().writeClose(element);
262     }
263 
264     /***
265      * Writes the opening tag of the specified {@link org.dom4j.Element}to the
266      * document.
267      * 
268      * @param element
269      *            the {@link org.dom4j.Element}to write
270      * 
271      * @throws IOException
272      *             if an error occured while writing the output
273      */
274     public void writeOpenElement(Element element) throws IOException {
275         getWriter().writeOpen(element);
276     }
277 
278     private XMLWriter getWriter() throws IOException {
279         if (xmlWriter == null) {
280             if (this.outputFormat != null) {
281                 xmlWriter = new XMLWriter(outputFormat);
282             } else {
283                 xmlWriter = new XMLWriter();
284             }
285         }
286 
287         return xmlWriter;
288     }
289 }
290 
291 /*
292  * Redistribution and use of this software and associated documentation
293  * ("Software"), with or without modification, are permitted provided that the
294  * following conditions are met:
295  * 
296  * 1. Redistributions of source code must retain copyright statements and
297  * notices. Redistributions must also contain a copy of this document.
298  * 
299  * 2. Redistributions in binary form must reproduce the above copyright notice,
300  * this list of conditions and the following disclaimer in the documentation
301  * and/or other materials provided with the distribution.
302  * 
303  * 3. The name "DOM4J" must not be used to endorse or promote products derived
304  * from this Software without prior written permission of MetaStuff, Ltd. For
305  * written permission, please contact dom4j-info@metastuff.com.
306  * 
307  * 4. Products derived from this Software may not be called "DOM4J" nor may
308  * "DOM4J" appear in their names without prior written permission of MetaStuff,
309  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
310  * 
311  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
312  * 
313  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
314  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
315  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
316  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
317  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
318  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
319  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
320  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
321  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
322  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
323  * POSSIBILITY OF SUCH DAMAGE.
324  * 
325  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
326  */