|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| JAXBWriter.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 | /* | |
| 2 | * Copyright 2001-2004 (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 | * $Id: JAXBWriter.java,v 1.1 2004/08/02 18:44:08 maartenc Exp $ | |
| 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 | 0 | public JAXBWriter(String contextPath) { |
| 48 | 0 | super(contextPath); |
| 49 | 0 | 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 | 0 | public JAXBWriter(String contextPath, OutputFormat outputFormat) { |
| 61 | 0 | super(contextPath); |
| 62 | 0 | 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 | 0 | public JAXBWriter(String contextPath, ClassLoader classloader) { |
| 76 | 0 | 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 | 0 | public JAXBWriter(String contextPath, ClassLoader classloader, OutputFormat outputFormat) { |
| 90 | 0 | super(contextPath, classloader); |
| 91 | 0 | 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 | 0 | public OutputFormat getOutputFormat() { |
| 99 | 0 | 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 | 0 | public void setOutput(File file) throws IOException { |
| 109 | 0 | 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 | 0 | public void setOutput(OutputStream outputStream) throws IOException { |
| 118 | 0 | 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 | 0 | public void setOutput(Writer writer) throws IOException { |
| 128 | 0 | 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 | 0 | public void startDocument() throws IOException, SAXException { |
| 139 | 0 | 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 | 0 | public void endDocument() throws IOException, SAXException { |
| 150 | 0 | 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 | 0 | public void write(javax.xml.bind.Element jaxbObject) throws IOException, JAXBException { |
| 163 | 0 | 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 | 0 | public void writeClose(javax.xml.bind.Element jaxbObject) throws IOException, JAXBException { |
| 177 | 0 | 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 | 0 | public void writeOpen(javax.xml.bind.Element jaxbObject) throws IOException, JAXBException { |
| 190 | 0 | 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 | 0 | public void writeElement(Element element) throws IOException { |
| 200 | 0 | 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 | 0 | public void writeCloseElement(Element element) throws IOException { |
| 210 | 0 | 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 | 0 | public void writeOpenElement(Element element) throws IOException { |
| 220 | 0 | getWriter().writeOpen(element); |
| 221 | } | |
| 222 | ||
| 223 | 0 | private XMLWriter getWriter() throws IOException { |
| 224 | 0 | if (xmlWriter == null) { |
| 225 | 0 | if (this.outputFormat != null) { |
| 226 | 0 | xmlWriter = new XMLWriter(outputFormat); |
| 227 | } | |
| 228 | else { | |
| 229 | 0 | xmlWriter = new XMLWriter(); |
| 230 | } | |
| 231 | } | |
| 232 | 0 | return xmlWriter; |
| 233 | } | |
| 234 | ||
| 235 | } | |
| 236 | ||
| 237 | ||
| 238 | ||
| 239 | ||
| 240 | /* | |
| 241 | * Redistribution and use of this software and associated documentation | |
| 242 | * ("Software"), with or without modification, are permitted provided | |
| 243 | * that the following conditions are met: | |
| 244 | * | |
| 245 | * 1. Redistributions of source code must retain copyright | |
| 246 | * statements and notices. Redistributions must also contain a | |
| 247 | * copy of this document. | |
| 248 | * | |
| 249 | * 2. Redistributions in binary form must reproduce the | |
| 250 | * above copyright notice, this list of conditions and the | |
| 251 | * following disclaimer in the documentation and/or other | |
| 252 | * materials provided with the distribution. | |
| 253 | * | |
| 254 | * 3. The name "DOM4J" must not be used to endorse or promote | |
| 255 | * products derived from this Software without prior written | |
| 256 | * permission of MetaStuff, Ltd. For written permission, | |
| 257 | * please contact dom4j-info@metastuff.com. | |
| 258 | * | |
| 259 | * 4. Products derived from this Software may not be called "DOM4J" | |
| 260 | * nor may "DOM4J" appear in their names without prior written | |
| 261 | * permission of MetaStuff, Ltd. DOM4J is a registered | |
| 262 | * trademark of MetaStuff, Ltd. | |
| 263 | * | |
| 264 | * 5. Due credit should be given to the DOM4J Project - | |
| 265 | * http://www.dom4j.org | |
| 266 | * | |
| 267 | * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS | |
| 268 | * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT | |
| 269 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 270 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
| 271 | * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
| 272 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 273 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| 274 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 275 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
| 276 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 277 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
| 278 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 279 | * | |
| 280 | * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved. | |
| 281 | * | |
| 282 | * $Id: JAXBWriter.java,v 1.1 2004/08/02 18:44:08 maartenc Exp $ | |
| 283 | */ |
|
||||||||||