1
2
3
4
5
6
7
8
9
10 package org.dom4j.jaxb;
11
12 import java.io.File;
13 import java.io.FileOutputStream;
14 import java.io.IOException;
15 import java.io.OutputStream;
16 import java.io.Writer;
17
18 import javax.xml.bind.JAXBException;
19
20 import org.dom4j.Element;
21 import org.dom4j.io.OutputFormat;
22 import org.dom4j.io.XMLWriter;
23 import org.xml.sax.SAXException;
24
25 /***
26 * Writes {@link javax.xml.bind.Element} objects to an XML stream.
27 * {@link javax.xml.bind.Element} instances can be created using the ObjectFactory
28 * that is generated by the JAXB compiler.
29 *
30 * @see org.dom4j.io.XMLWriter
31 * @see javax.xml.bind.JAXBContext
32 * @author Wonne Keysers (Realsoftware.be)
33 */
34 public class JAXBWriter extends JAXBSupport {
35
36 private XMLWriter xmlWriter;
37 private OutputFormat outputFormat;
38
39 /***
40 * Creates a new JAXBWriter for the given JAXB context path.
41 * This is the Java package where JAXB can find the generated XML classes.
42 * This package MUST contain jaxb.properties!
43 *
44 * @param contextPath JAXB context path to be used
45 * @see javax.xml.bind.JAXBContext
46 */
47 public JAXBWriter(String contextPath) {
48 super(contextPath);
49 outputFormat = new OutputFormat();
50 }
51
52 /***
53 * Creates a new JAXBWriter for the given JAXB context path.
54 * The specied {@link org.dom4j.io.OutputFormat} will be used for writing the XML stream.
55 *
56 * @param contextPath JAXB context path to be used
57 * @param outputFormat the DOM4J {@link org.dom4j.io.OutputFormat} to be used
58 * @see javax.xml.bind.JAXBContext
59 */
60 public JAXBWriter(String contextPath, OutputFormat outputFormat) {
61 super(contextPath);
62 this.outputFormat = outputFormat;
63 }
64
65 /***
66 * Creates a new JAXBWriter for the given JAXB context path,
67 * using the specified {@link java.lang.Classloader}.
68 * (This is the Java package where JAXB can find the generated XML classes.
69 * This package MUST contain jaxb.properties!)
70 *
71 * @param contextPath JAXB context path to be used
72 * @param classloader the classloader to be used for loading JAXB
73 * @see javax.xml.bind.JAXBContext
74 */
75 public JAXBWriter(String contextPath, ClassLoader classloader) {
76 super(contextPath, classloader);
77 }
78
79 /***
80 * Creates a new JAXBWriter for the given JAXB context path,
81 * using the specified {@link java.lang.Classloader}.
82 * The specied {@link org.dom4j.io.OutputFormat} will be used while writing the XML stream.
83 *
84 * @param contextPath JAXB context path to be used
85 * @param classloader the class loader to be used to load JAXB
86 * @param outputFormat the DOM4J {@link org.dom4j.io.OutputFormat} to be used
87 * @see javax.xml.bind.JAXBContext
88 */
89 public JAXBWriter(String contextPath, ClassLoader classloader, OutputFormat outputFormat) {
90 super(contextPath, classloader);
91 this.outputFormat = outputFormat;
92 }
93
94 /***
95 * Returns the OutputFormat that will be used when writing the XML stream.
96 * @return Returns the output format.
97 */
98 public OutputFormat getOutputFormat() {
99 return outputFormat;
100 }
101
102 /***
103 * Defines to write the resulting output to the specified {@link java.io.File}.
104 *
105 * @param file file to write to
106 * @throws IOException when the file cannot be found
107 */
108 public void setOutput(File file) throws IOException {
109 getWriter().setOutputStream(new FileOutputStream(file));
110 }
111
112 /***
113 * Defines to write the resulting output to the specified {@link java.io.OutputStream}
114 *
115 * @param outputStream outputStream to write to.
116 */
117 public void setOutput(OutputStream outputStream) throws IOException {
118 getWriter().setOutputStream(outputStream);
119 }
120
121 /***
122 * Defines to write the resulting output to the specified {@link Writer}.
123 *
124 * @param writer writer to write to
125 * @throws IOException
126 */
127 public void setOutput(Writer writer) throws IOException {
128 getWriter().setWriter(writer);
129 }
130
131 /***
132 * Start a document by writing the initial XML declaration to the output.
133 * This must be done prior to writing any other elements.
134 *
135 * @throws IOException if an error occured while writing the output
136 * @throws SAXException thrown by the underlying SAX driver
137 */
138 public void startDocument() throws IOException, SAXException {
139 getWriter().startDocument();
140 }
141
142 /***
143 * Stop writing the document to the output.
144 * This must be done when all other elements are finished.
145 *
146 * @throws IOException if an error occured while writing the output
147 * @throws SAXException thrown by the underlying SAX driver
148 */
149 public void endDocument() throws IOException, SAXException {
150 getWriter().endDocument();
151 }
152
153 /***
154 * Writes the specified {@link javax.xml.bind.Element} to the document.
155 * {@link javax.xml.bind.Element} instances can be created using the ObjectFactory
156 * that is generated by the JAXB compiler.
157 *
158 * @param jaxbObject
159 * @throws IOException if an error occured while writing the output
160 * @throws JAXBException when an error occured while marshalling the jaxbObject
161 */
162 public void write(javax.xml.bind.Element jaxbObject) throws IOException, JAXBException {
163 getWriter().write(marshal(jaxbObject));
164 }
165
166 /***
167 * Writes the closing tag of the specified {@link javax.xml.bind.Element} to the document.
168 * This method can be used for writing
169 * {@link javax.xml.bind.Element} instances can be created using the ObjectFactory
170 * that is generated by the JAXB compiler.
171 *
172 * @param jaxbObject the JAXB element to write
173 * @throws IOException if an error occured while writing the output
174 * @throws JAXBException when an error occured while marshalling the jaxbObject
175 */
176 public void writeClose(javax.xml.bind.Element jaxbObject) throws IOException, JAXBException {
177 getWriter().writeClose(marshal(jaxbObject));
178 }
179
180 /***
181 * Writes the opening tag of the specified {@link javax.xml.bind.Element} to the document.
182 * {@link javax.xml.bind.Element} instances can be created using the ObjectFactory
183 * that is generated by the JAXB compiler.
184 *
185 * @param jaxbObject the JAXB element to write
186 * @throws IOException if an error occured while writing the output
187 * @throws JAXBException when an error occured while marshalling the jaxbObject
188 */
189 public void writeOpen(javax.xml.bind.Element jaxbObject) throws IOException, JAXBException {
190 getWriter().writeOpen(marshal(jaxbObject));
191 }
192
193 /***
194 * Writes the specified {@link org.dom4j.Element} to the document.
195 *
196 * @param element the {@link org.dom4j.Element} to write
197 * @throws IOException if an error occured while writing the output
198 */
199 public void writeElement(Element element) throws IOException {
200 getWriter().write(element);
201 }
202
203 /***
204 * Writes the closing tag of the specified {@link org.dom4j.Element} to the document.
205 *
206 * @param element the {@link org.dom4j.Element} to write
207 * @throws IOException if an error occured while writing the output
208 */
209 public void writeCloseElement(Element element) throws IOException {
210 getWriter().writeClose(element);
211 }
212
213 /***
214 * Writes the opening tag of the specified {@link org.dom4j.Element} to the document.
215 *
216 * @param element the {@link org.dom4j.Element} to write
217 * @throws IOException if an error occured while writing the output
218 */
219 public void writeOpenElement(Element element) throws IOException {
220 getWriter().writeOpen(element);
221 }
222
223 private XMLWriter getWriter() throws IOException {
224 if (xmlWriter == null) {
225 if (this.outputFormat != null) {
226 xmlWriter = new XMLWriter(outputFormat);
227 }
228 else {
229 xmlWriter = new XMLWriter();
230 }
231 }
232 return xmlWriter;
233 }
234
235 }
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283