KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > xml > ObjectReader


1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2005 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package javolution.xml;
10
11 import j2me.nio.ByteBuffer;
12
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.io.Reader;
16
17 import javolution.lang.Reusable;
18 import javolution.xml.sax.XmlSaxParserImpl;
19
20 import org.xml.sax.SAXException;
21
22 /**
23  * <p> This class restores objects which have been serialized in XML
24  * format using an {@link ObjectWriter}.</p>
25  *
26  * <p> When the XML document is parsed, each elements are recursively
27  * processed and Java objects are created using the {@link XmlFormat}
28  * of the class identified by the name of the XML element.
29  * The final object constructed (and returned) is always the root element
30  * of the XML input source.</p>
31  *
32  * <p> Processing instructions are ignored, but namespaces may be used to
33  * specify package names (java addressing scheme).</p>
34  *
35  * <p> Non-blank character data of the XML document are represented
36  * by {@link CharacterData} instances.</p>
37  *
38  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
39  * @version 3.3, May 13, 2005
40  * @see XmlSaxParserImpl
41  * @see ConstructorHandler
42  */

43 public class ObjectReader/*<T>*/ implements Reusable {
44
45     /**
46      * Holds the real-time parser used.
47      */

48     private final XmlSaxParserImpl _parser = new XmlSaxParserImpl();
49
50     /**
51      * Holds the constructor handler.
52      */

53     private final ConstructorHandler _handler = new ConstructorHandler();
54
55     /**
56      * Default constructor.
57      */

58     public ObjectReader() {
59     }
60     
61     /**
62      * Resets this object reader; objects previously read cannot be refered to,
63      * they will have to be send again.
64      */

65     public void reset() {
66         _handler.reset();
67         _parser.reset();
68     }
69
70     /**
71      * Creates an object from its XML representation read from
72      * the specified <code>Reader</code>. This method reads until the
73      * end of stream; to read multiple objects over a persistent connection
74      * {@link XmlInputStream} should be used instead.
75      *
76      * @param reader the reader containing the XML representation of the
77      * object being created.
78      * @return the object corresponding to the xml root element.
79      * @throws XmlException if the object cannot be created.
80      */

81     public Object/*T*/ read(Reader reader) throws XmlException {
82         try {
83             _parser.setContentHandler(_handler);
84             _parser.parse(reader);
85             return (Object/*T*/) _handler.getRoot();
86         } catch (SAXException e1) {
87             throw new XmlException(e1);
88         } catch (IOException e2) {
89             throw new XmlException(e2);
90         }
91     }
92
93     /**
94      * Creates an object from its XML representation read from
95      * the specified <code>InputStream</code>. This method reads until the
96      * end of stream; to read multiple objects over a persistent connection
97      * {@link XmlInputStream} should be used instead.
98      *
99      * @param in the input stream containing the XML representation of the
100      * object being created.
101      * @return the object corresponding to the xml root element.
102      * @throws XmlException if the object cannot be created.
103      */

104     public Object/*T*/ read(InputStream in) throws XmlException {
105         try {
106             _parser.setContentHandler(_handler);
107             _parser.parse(in);
108             return (Object/*T*/) _handler.getRoot();
109         } catch (SAXException e1) {
110             throw new XmlException(e1);
111         } catch (IOException e2) {
112             throw new XmlException(e2);
113         }
114     }
115
116     /**
117      * Creates an object from its XML representation read from
118      * the specified <code>ByteBuffer</code>. This method reads from
119      * the current buffer position up to the buffer's limit.
120      *
121      * @param byteBuffer the byte buffer containing the XML representation
122      * of the object being created.
123      * @return the object corresponding to the xml root element.
124      * @throws XmlException if the object cannot be created.
125      */

126     public Object/*T*/ read(ByteBuffer byteBuffer) throws XmlException {
127         try {
128             _parser.setContentHandler(_handler);
129             _parser.parse(byteBuffer);
130             return (Object/*T*/) _handler.getRoot();
131         } catch (SAXException e1) {
132             throw new XmlException(e1);
133         } catch (IOException e2) {
134             throw new XmlException(e2);
135         }
136     }
137
138 }
Popular Tags