1 9 package javolution.xml; 10 11 import j2me.lang.IllegalStateException; 12 13 import java.io.IOException ; 14 import java.io.OutputStream ; 15 import java.io.Writer ; 16 17 import javolution.context.ObjectFactory; 18 import javolution.lang.Reusable; 19 import javolution.xml.stream.XMLStreamException; 20 import javolution.xml.stream.XMLStreamWriter; 21 import javolution.xml.stream.XMLStreamWriterImpl; 22 23 44 public class XMLObjectWriter implements Reusable { 45 46 49 private static final ObjectFactory FACTORY = new ObjectFactory() { 50 51 protected Object create() { 52 return new XMLObjectWriter(); 53 } 54 protected void cleanup(Object obj) { 55 ((XMLObjectWriter)obj).reset(); 56 } 57 }; 58 59 62 private final XMLFormat.OutputElement _xml 63 = new XMLFormat.OutputElement(); 64 65 68 private Writer _writer; 69 70 73 private OutputStream _outputStream; 74 75 78 private boolean _isFactoryProduced; 79 80 83 public XMLObjectWriter() { 84 } 85 86 92 public static XMLObjectWriter newInstance(OutputStream out) throws XMLStreamException { 93 XMLObjectWriter writer = (XMLObjectWriter) FACTORY.object(); 94 writer._isFactoryProduced = true; 95 writer.setOutput(out); 96 return writer; 97 } 98 99 106 public static XMLObjectWriter newInstance(OutputStream out, String encoding) throws XMLStreamException { 107 XMLObjectWriter writer = (XMLObjectWriter) FACTORY.object(); 108 writer._isFactoryProduced = true; 109 writer.setOutput(out, encoding); 110 return writer; 111 } 112 113 119 public static XMLObjectWriter newInstance(Writer out) throws XMLStreamException { 120 XMLObjectWriter writer = (XMLObjectWriter) FACTORY.object(); 121 writer._isFactoryProduced = true; 122 writer.setOutput(out); 123 return writer; 124 } 125 126 135 public XMLStreamWriter getStreamWriter() { 136 return _xml._writer; 137 } 138 139 146 public XMLObjectWriter setOutput(OutputStream out) throws XMLStreamException { 147 if ((_outputStream != null) || (_writer != null)) 148 throw new IllegalStateException ("Writer not closed or reset"); 149 _xml._writer.setOutput(out); 150 _outputStream = out; 151 _xml._writer.writeStartDocument(); 152 return this; 153 } 154 155 163 public XMLObjectWriter setOutput(OutputStream out, String encoding) throws XMLStreamException { 164 if ((_outputStream != null) || (_writer != null)) 165 throw new IllegalStateException ("Writer not closed or reset"); 166 _xml._writer.setOutput(out, encoding); 167 _outputStream = out; 168 _xml._writer.writeStartDocument(); 169 return this; 170 } 171 172 179 public XMLObjectWriter setOutput(Writer out) throws XMLStreamException { 180 if ((_outputStream != null) || (_writer != null)) 181 throw new IllegalStateException ("Writer not closed or reset"); 182 _xml._writer.setOutput(out); 183 _writer = out; 184 _xml._writer.writeStartDocument(); 185 return this; 186 } 187 188 194 public XMLObjectWriter setBinding(XMLBinding binding) { 195 _xml.setBinding(binding); 196 return this; 197 } 198 199 206 public XMLObjectWriter setIndentation(String indentation) { 207 _xml._writer.setIndentation(indentation); 208 return this; 209 } 210 211 218 public XMLObjectWriter setReferenceResolver(XMLReferenceResolver referenceResolver) { 219 _xml.setReferenceResolver(referenceResolver); 220 return this; 221 } 222 223 230 public void write(Object obj) throws XMLStreamException { 231 _xml.add(obj); 232 } 233 234 243 public void write(Object obj, String name) throws XMLStreamException { 244 _xml.add(obj, name); 245 } 246 247 258 public void write(Object obj, String localName, String uri) 259 throws XMLStreamException { 260 _xml.add(obj, localName, uri); 261 } 262 263 272 public void write(Object obj, String name, Class cls) 273 throws XMLStreamException { 274 _xml.add(obj, name, cls); 275 } 276 277 287 public void write(Object obj, String localName, String uri, Class cls) 288 throws XMLStreamException { 289 _xml.add(obj, localName, uri, cls); 290 } 291 292 296 public void flush() throws XMLStreamException { 297 _xml._writer.flush(); 298 } 299 300 304 public void close() throws XMLStreamException { 305 try { 306 if (_outputStream != null) { 307 _xml._writer.writeEndDocument(); 308 _xml._writer.close(); 309 _outputStream.close(); 310 reset(); 311 } else if (_writer != null) { 312 _xml._writer.writeEndDocument(); 313 _xml._writer.close(); 314 _writer.close(); 315 reset(); 316 } 317 if (_isFactoryProduced) { 318 FACTORY.recycle(this); 319 } 320 321 } catch (IOException e) { 322 throw new XMLStreamException(e); 323 } 324 } 325 326 329 public void reset() { 330 _xml.reset(); 331 _outputStream = null; 332 _writer = null; 333 } 334 } | Popular Tags |