001: package org.gomba.utils.xml;
002:
003: import java.io.IOException;
004:
005: import org.xml.sax.ContentHandler;
006: import org.xml.sax.DTDHandler;
007: import org.xml.sax.EntityResolver;
008: import org.xml.sax.ErrorHandler;
009: import org.xml.sax.InputSource;
010: import org.xml.sax.SAXException;
011: import org.xml.sax.SAXNotRecognizedException;
012: import org.xml.sax.SAXNotSupportedException;
013: import org.xml.sax.XMLReader;
014:
015: /**
016: * This class implements the SAX XMLReader interface and adds a new
017: * <code>parse</code> method accepting an <code>ObjectInputSource</code>.
018: * This is useful to serialize arbitrary Java objects to XML on the fly. This is
019: * done by extending this class and overriding the <code>parse</code> method.
020: * Once you get the referece to your custom Object by calling
021: * <code>ObjectInputSource.getObject()</code>, you can play with the
022: * <code>ContentHandler</code> and generate the SAX events according to your
023: * object model.
024: *
025: * @author Flavio Tordini
026: * @version $Id: ObjectXMLReader.java,v 1.3 2004/09/16 09:23:17 flaviotordini Exp $
027: * @see ObjectInputSource
028: * @see <a
029: * href="http://java.sun.com/webservices/docs/1.1/tutorial/doc/JAXPXSLT6.html">Generating
030: * XML from an Arbitrary Data Structure </a>
031: * @see <a href="http://www.saxproject.org/">The SAX Project </a>
032: */
033: public abstract class ObjectXMLReader implements XMLReader {
034:
035: protected ContentHandler handler;
036:
037: protected ErrorHandler errorHandler;
038:
039: protected EntityResolver resolver;
040:
041: protected DTDHandler dtdHandler;
042:
043: /**
044: * @see org.xml.sax.XMLReader#getContentHandler()
045: */
046: public ContentHandler getContentHandler() {
047: return this .handler;
048: }
049:
050: /**
051: * @see org.xml.sax.XMLReader#getDTDHandler()
052: */
053: public DTDHandler getDTDHandler() {
054: return this .dtdHandler;
055: }
056:
057: /**
058: * @see org.xml.sax.XMLReader#getEntityResolver()
059: */
060: public EntityResolver getEntityResolver() {
061: return this .resolver;
062: }
063:
064: /**
065: * @see org.xml.sax.XMLReader#getErrorHandler()
066: */
067: public ErrorHandler getErrorHandler() {
068: return this .errorHandler;
069: }
070:
071: /**
072: * @see org.xml.sax.XMLReader#getFeature(java.lang.String)
073: */
074: public boolean getFeature(String name)
075: throws SAXNotRecognizedException, SAXNotSupportedException {
076: return false;
077: }
078:
079: /**
080: * @see org.xml.sax.XMLReader#getProperty(java.lang.String)
081: */
082: public Object getProperty(String name)
083: throws SAXNotRecognizedException, SAXNotSupportedException {
084: return null;
085: }
086:
087: /**
088: * @see org.xml.sax.XMLReader#parse(org.xml.sax.InputSource)
089: */
090: public void parse(InputSource input) throws IOException,
091: SAXException {
092: if (!(input instanceof ObjectInputSource)) {
093: throw new SAXException("InputSource must be of "
094: + ObjectInputSource.class);
095: }
096:
097: if (this .handler == null) {
098: throw new SAXException("No content handler");
099: }
100:
101: ObjectInputSource objectInputSource = (ObjectInputSource) input;
102: parse(objectInputSource);
103: }
104:
105: public abstract void parse(ObjectInputSource input)
106: throws IOException, SAXException;
107:
108: /**
109: * @see org.xml.sax.XMLReader#parse(java.lang.String)
110: */
111: public void parse(String systemId) throws IOException, SAXException {
112: throw new UnsupportedOperationException("Not implemented.");
113: }
114:
115: /**
116: * @see org.xml.sax.XMLReader#setContentHandler(org.xml.sax.ContentHandler)
117: */
118: public void setContentHandler(ContentHandler handler) {
119: this .handler = handler;
120: }
121:
122: /**
123: * @see org.xml.sax.XMLReader#setDTDHandler(org.xml.sax.DTDHandler)
124: */
125: public void setDTDHandler(DTDHandler dtdHandler) {
126: this .dtdHandler = dtdHandler;
127: }
128:
129: /**
130: * @see org.xml.sax.XMLReader#setEntityResolver(org.xml.sax.EntityResolver)
131: */
132: public void setEntityResolver(EntityResolver resolver) {
133: this .resolver = resolver;
134: }
135:
136: /**
137: * @see org.xml.sax.XMLReader#setErrorHandler(org.xml.sax.ErrorHandler)
138: */
139: public void setErrorHandler(ErrorHandler errorHandler) {
140: this .errorHandler = errorHandler;
141: }
142:
143: /**
144: * @see org.xml.sax.XMLReader#setFeature(java.lang.String, boolean)
145: */
146: public void setFeature(String name, boolean value)
147: throws SAXNotRecognizedException, SAXNotSupportedException {
148: // dummy
149: }
150:
151: /**
152: * @see org.xml.sax.XMLReader#setProperty(java.lang.String,
153: * java.lang.Object)
154: */
155: public void setProperty(String name, Object value)
156: throws SAXNotRecognizedException, SAXNotSupportedException {
157: // dummy
158: }
159:
160: }
|