2
* Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
4
* This software is open source.
5
* See the bottom of this file for the licence.
8
package org.dom4j.jaxb;
11
import java.io.FileOutputStream;
12
import java.io.IOException;
13
import java.io.OutputStream;
14
import java.io.Writer;
16
import javax.xml.bind.JAXBException;
18
import org.dom4j.Element;
19
import org.dom4j.io.OutputFormat;
20
import org.dom4j.io.XMLWriter;
22
import org.xml.sax.SAXException;
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.
29
* @author Wonne Keysers (Realsoftware.be)
31
* @see org.dom4j.io.XMLWriter
32
* @see javax.xml.bind.JAXBContext
34
public class JAXBWriter extends JAXBSupport {
35
private XMLWriter xmlWriter;
37
private OutputFormat outputFormat;
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!
45
* JAXB context path to be used
47
* @see javax.xml.bind.JAXBContext
49
public JAXBWriter(String contextPath) {
51
outputFormat = new OutputFormat();
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
60
* JAXB context path to be used
62
* the DOM4J {@link org.dom4j.io.OutputFormat}to be used
64
* @see javax.xml.bind.JAXBContext
66
public JAXBWriter(String contextPath, OutputFormat outputFormat) {
68
this.outputFormat = outputFormat;
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
78
* JAXB context path to be used
80
* the classloader to be used for loading JAXB
82
* @see javax.xml.bind.JAXBContext
84
public JAXBWriter(String contextPath, ClassLoader classloader) {
85
super(contextPath, classloader);
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.
94
* JAXB context path to be used
96
* the class loader to be used to load JAXB
98
* the DOM4J {@link org.dom4j.io.OutputFormat}to be used
100
* @see javax.xml.bind.JAXBContext
102
public JAXBWriter(String contextPath, ClassLoader classloader,
103
OutputFormat outputFormat) {
104
super(contextPath, classloader);
105
this.outputFormat = outputFormat;
109
* Returns the OutputFormat that will be used when writing the XML stream.
111
* @return Returns the output format.
113
public OutputFormat getOutputFormat() {
118
* Defines to write the resulting output to the specified {@link
124
* @throws IOException
125
* when the file cannot be found
127
public void setOutput(File file) throws IOException {
128
getWriter().setOutputStream(new FileOutputStream(file));
132
* Defines to write the resulting output to the specified {@link
133
* java.io.OutputStream}
135
* @param outputStream
136
* outputStream to write to.
138
* @throws IOException
141
public void setOutput(OutputStream outputStream) throws IOException {
142
getWriter().setOutputStream(outputStream);
146
* Defines to write the resulting output to the specified {@link Writer}.
151
* @throws IOException
153
public void setOutput(Writer writer) throws IOException {
154
getWriter().setWriter(writer);
158
* Start a document by writing the initial XML declaration to the output.
159
* This must be done prior to writing any other elements.
161
* @throws IOException
162
* if an error occured while writing the output
163
* @throws SAXException
164
* thrown by the underlying SAX driver
166
public void startDocument() throws IOException, SAXException {
167
getWriter().startDocument();
171
* Stop writing the document to the output. This must be done when all other
172
* elements are finished.
174
* @throws IOException
175
* if an error occured while writing the output
176
* @throws SAXException
177
* thrown by the underlying SAX driver
179
public void endDocument() throws IOException, SAXException {
180
getWriter().endDocument();
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.
190
* @throws IOException
191
* if an error occured while writing the output
192
* @throws JAXBException
193
* when an error occured while marshalling the jaxbObject
195
public void write(javax.xml.bind.Element jaxbObject) throws IOException,
197
getWriter().write(marshal(jaxbObject));
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.
207
* the JAXB element to write
209
* @throws IOException
210
* if an error occured while writing the output
211
* @throws JAXBException
212
* when an error occured while marshalling the jaxbObject
214
public void writeClose(javax.xml.bind.Element jaxbObject)
215
throws IOException, JAXBException {
216
getWriter().writeClose(marshal(jaxbObject));
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.
225
* the JAXB element to write
227
* @throws IOException
228
* if an error occured while writing the output
229
* @throws JAXBException
230
* when an error occured while marshalling the jaxbObject
232
public void writeOpen(javax.xml.bind.Element jaxbObject)
233
throws IOException, JAXBException {
234
getWriter().writeOpen(marshal(jaxbObject));
238
* Writes the specified {@link org.dom4j.Element}to the document.
241
* the {@link org.dom4j.Element}to write
243
* @throws IOException
244
* if an error occured while writing the output
246
public void writeElement(Element element) throws IOException {
247
getWriter().write(element);
251
* Writes the closing tag of the specified {@link org.dom4j.Element}to the
255
* the {@link org.dom4j.Element}to write
257
* @throws IOException
258
* if an error occured while writing the output
260
public void writeCloseElement(Element element) throws IOException {
261
getWriter().writeClose(element);
265
* Writes the opening tag of the specified {@link org.dom4j.Element}to the
269
* the {@link org.dom4j.Element}to write
271
* @throws IOException
272
* if an error occured while writing the output
274
public void writeOpenElement(Element element) throws IOException {
275
getWriter().writeOpen(element);
278
private XMLWriter getWriter() throws IOException {
279
if (xmlWriter == null) {
280
if (this.outputFormat != null) {
281
xmlWriter = new XMLWriter(outputFormat);
283
xmlWriter = new XMLWriter();
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:
296
* 1. Redistributions of source code must retain copyright statements and
297
* notices. Redistributions must also contain a copy of this document.
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.
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.
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.
311
* 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
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.
325
* Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.