01: package com.wutka.jox;
02:
03: import java.io.*;
04: import org.xml.sax.InputSource;
05:
06: /** An InputStream filter that reads XML into a bean.
07: *
08: * When you read an XML document, you must supply either a class
09: * or an object instance. The input stream will attempt to match
10: * XML tags to bean attributes in the class/object you supply.
11: * <p>
12: * If you supply a class, the input stream will automatically create
13: * a new object instance to hold the data.
14: * <p>
15: * The stream understands the basic Java data types and their object
16: * equivalents, plus strings and dates. Anything else must be a bean.
17: * It can also read arrays of any of the supported types or of beans
18: * if it tries to read a bean with an indexed property.
19: * <p>
20: * If there are XML fields that don't match the bean, it will ignore them.
21: * If the data types are not compatible, you will get an exception. At some
22: * point the stream will be smart enough to skip over incompatible fields.
23: *
24: * <i>Note: JOXBeanInputStream and JOXBeanReader use the same underlying class
25: * to read the XML document, so their behaviour should be identical.</i>
26: *
27: * @author Mark Wutka
28: * @version 1.0 05/08/2000
29: * @version 1.1 05/09/2000
30: */
31:
32: public class JOXBeanInputStream extends FilterInputStream {
33:
34: /** Creates a new JOXInputStream around an existing input stream.
35: * @param in The input stream to be read from
36: */
37: public JOXBeanInputStream(InputStream in) {
38: super (in);
39: }
40:
41: /** Reads an XML document into the object, matching tag names to
42: * bean property names.
43: * @param ob The object that will receive data from the XML document
44: * @throws IOException If there is an error reading the document
45: */
46: public void readObject(Object ob) throws IOException {
47: /* Create an input source around the input stream and ask the
48: input utility class to process the document */
49: JOXSAXBeanInput reader = new JOXSAXBeanInput();
50:
51: reader.readObject(ob, new InputSource(in));
52: }
53:
54: /** Reads an XML document into a new instance of the given class,
55: * matching tag names to bean property names.
56: * @param obClass The class for the object that will receive data from
57: * the XML document
58: * @return The new instance of the class with the populated data
59: * @throws IOException If there is an error reading the document
60: */
61: public Object readObject(Class obClass) throws IOException {
62: try {
63: // Create a new instance of the object
64: Object ob = obClass.newInstance();
65:
66: /* Create an input source around the input stream and ask the
67: input utility class to process the document */
68: JOXSAXBeanInput reader = new JOXSAXBeanInput();
69:
70: reader.readObject(ob, new InputSource(in));
71:
72: // Return the new instance
73: return ob;
74: } catch (InstantiationException exc) {
75: throw new IOException("Error instantiating "
76: + obClass.getName() + ": " + exc.toString());
77: } catch (IllegalAccessException exc) {
78: throw new IOException("Error instantiating "
79: + obClass.getName() + ": " + exc.toString());
80: }
81: }
82: }
|