1 9 package javolution.xml; 10 11 import j2me.lang.IllegalStateException; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.io.Reader ; 16 17 import javolution.context.ObjectFactory; 18 import javolution.lang.Reusable; 19 import javolution.xml.stream.XMLStreamException; 20 import javolution.xml.stream.XMLStreamReader; 21 import javolution.xml.stream.XMLStreamReaderImpl; 22 23 43 public class XMLObjectReader implements Reusable { 44 45 48 private static final ObjectFactory FACTORY = new ObjectFactory() { 49 50 protected Object create() { 51 return new XMLObjectReader(); 52 } 53 protected void cleanup(Object obj) { 54 ((XMLObjectReader)obj).reset(); 55 } 56 }; 57 58 61 private final XMLFormat.InputElement _xml = new XMLFormat.InputElement(); 62 63 66 private Reader _reader; 67 68 71 private InputStream _inputStream; 72 73 76 private boolean _isFactoryProduced; 77 78 84 public static XMLObjectReader newInstance(InputStream in) throws XMLStreamException { 85 XMLObjectReader reader = (XMLObjectReader) FACTORY.object(); 86 reader._isFactoryProduced = true; 87 reader.setInput(in); 88 return reader; 89 } 90 91 98 public static XMLObjectReader newInstance(InputStream in, String encoding) throws XMLStreamException { 99 XMLObjectReader reader = (XMLObjectReader) FACTORY.object(); 100 reader._isFactoryProduced = true; 101 reader.setInput(in, encoding); 102 return reader; 103 } 104 105 111 public static XMLObjectReader newInstance(Reader in) throws XMLStreamException { 112 XMLObjectReader reader = (XMLObjectReader) FACTORY.object(); 113 reader._isFactoryProduced = true; 114 reader.setInput(in); 115 return reader; 116 } 117 118 121 public XMLObjectReader() { 122 } 123 124 130 public XMLStreamReader getStreamReader() { 131 return _xml._reader; 132 } 133 134 142 public XMLObjectReader setInput(InputStream in) throws XMLStreamException { 143 if ((_inputStream != null) || (_reader != null)) 144 throw new IllegalStateException ("Reader not closed or reset"); 145 _xml._reader.setInput(in); 146 _inputStream = in; 147 return this; 148 } 149 150 158 public XMLObjectReader setInput(InputStream in, String encoding) 159 throws XMLStreamException { 160 if ((_inputStream != null) || (_reader != null)) 161 throw new IllegalStateException ("Reader not closed or reset"); 162 _xml._reader.setInput(in, encoding); 163 _inputStream = in; 164 return this; 165 } 166 167 174 public XMLObjectReader setInput(Reader in) throws XMLStreamException { 175 if ((_inputStream != null) || (_reader != null)) 176 throw new IllegalStateException ("Reader not closed or reset"); 177 _xml._reader.setInput(in); 178 _reader = in; 179 return this; 180 } 181 182 188 public XMLObjectReader setBinding(XMLBinding binding) { 189 _xml.setBinding(binding); 190 return this; 191 } 192 193 200 public XMLObjectReader setReferenceResolver( 201 XMLReferenceResolver referenceResolver) { 202 _xml.setReferenceResolver(referenceResolver); 203 return this; 204 } 205 206 215 public boolean hasNext() throws XMLStreamException { 216 return _xml.hasNext(); 217 } 218 219 226 public Object read() throws XMLStreamException { 227 return (Object )_xml.getNext(); 228 } 229 230 239 public Object read(String name) throws XMLStreamException { 240 return (Object ) _xml.get(name); 241 } 242 243 253 public Object read(String localName, String uri) 254 throws XMLStreamException { 255 return (Object ) _xml.get(localName, uri); 256 } 257 258 267 publicObject read(String name, Class cls) 268 throws XMLStreamException { 269 return _xml.get(name, cls); 270 } 271 272 282 publicObject read(String localName, String uri, 283 Class cls) throws XMLStreamException { 284 return _xml.get(localName, uri, cls); 285 } 286 287 291 public void close() throws XMLStreamException { 292 try { 293 if (_inputStream != null) { 294 _inputStream.close(); 295 reset(); 296 } else if (_reader != null) { 297 _reader.close(); 298 reset(); 299 } 300 if (_isFactoryProduced) { 301 FACTORY.recycle(this); 302 } 303 } catch (IOException e) { 304 throw new XMLStreamException(e); 305 } 306 } 307 308 311 public void reset() { 312 _xml.reset(); 313 _reader = null; 314 _inputStream = null; 315 } 316 } | Popular Tags |