1 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 34 public class JAXBWriter extends JAXBSupport { 35 private XMLWriter xmlWriter; 36 37 private OutputFormat outputFormat; 38 39 49 public JAXBWriter(String contextPath) { 50 super(contextPath); 51 outputFormat = new OutputFormat(); 52 } 53 54 66 public JAXBWriter(String contextPath, OutputFormat outputFormat) { 67 super(contextPath); 68 this.outputFormat = outputFormat; 69 } 70 71 84 public JAXBWriter(String contextPath, ClassLoader classloader) { 85 super(contextPath, classloader); 86 } 87 88 102 public JAXBWriter(String contextPath, ClassLoader classloader, 103 OutputFormat outputFormat) { 104 super(contextPath, classloader); 105 this.outputFormat = outputFormat; 106 } 107 108 113 public OutputFormat getOutputFormat() { 114 return outputFormat; 115 } 116 117 127 public void setOutput(File file) throws IOException { 128 getWriter().setOutputStream(new FileOutputStream (file)); 129 } 130 131 141 public void setOutput(OutputStream outputStream) throws IOException { 142 getWriter().setOutputStream(outputStream); 143 } 144 145 153 public void setOutput(Writer writer) throws IOException { 154 getWriter().setWriter(writer); 155 } 156 157 166 public void startDocument() throws IOException , SAXException { 167 getWriter().startDocument(); 168 } 169 170 179 public void endDocument() throws IOException , SAXException { 180 getWriter().endDocument(); 181 } 182 183 195 public void write(javax.xml.bind.Element jaxbObject) throws IOException , 196 JAXBException { 197 getWriter().write(marshal(jaxbObject)); 198 } 199 200 214 public void writeClose(javax.xml.bind.Element jaxbObject) 215 throws IOException , JAXBException { 216 getWriter().writeClose(marshal(jaxbObject)); 217 } 218 219 232 public void writeOpen(javax.xml.bind.Element jaxbObject) 233 throws IOException , JAXBException { 234 getWriter().writeOpen(marshal(jaxbObject)); 235 } 236 237 246 public void writeElement(Element element) throws IOException { 247 getWriter().write(element); 248 } 249 250 260 public void writeCloseElement(Element element) throws IOException { 261 getWriter().writeClose(element); 262 } 263 264 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 327 | Popular Tags |