001: package com.jeta.forms.store.xml.parser;
002:
003: import java.util.HashMap;
004:
005: import org.xml.sax.SAXException;
006:
007: import com.jeta.forms.store.jml.dom.JMLAttributes;
008: import com.jeta.forms.store.xml.XMLUtils;
009:
010: public class ObjectHandler implements XMLHandler {
011:
012: private Object m_object = null;
013: private HashMap m_properties = new HashMap();
014: /** for debugging */
015: private String m_classname;
016:
017: /**
018: * Handler that represents the properties for any superclasses of the object
019: * we are persisting.
020: */
021: private ObjectHandler m_super ClassHandler;
022:
023: public Object getProperty(String propName) {
024: return m_properties.get(propName);
025: }
026:
027: protected void setProperty(Object name, Object value,
028: JMLAttributes attribs) throws SAXException {
029: if ("version".equalsIgnoreCase(name.toString())) {
030: try {
031: int ival = Integer.parseInt(value.toString());
032: } catch (Exception e) {
033: System.out
034: .println("ObjectHandler version failed!!! Classname: "
035: + m_classname);
036: e.printStackTrace();
037: }
038: }
039:
040: if (m_properties.containsKey(name)) {
041: System.out.println("ObjectHandler already has property: "
042: + name + " oldvalue: " + m_properties.get(name));
043: }
044:
045: m_properties.put(name, value);
046: }
047:
048: public void startElement(XMLNodeContext ctx) throws SAXException {
049:
050: if ("object".equalsIgnoreCase(ctx.getQualifiedName())) {
051: if (m_classname != null) {
052: System.out
053: .println("Found non-null class name in ObjectHandler.startElement existing name: "
054: + m_classname
055: + " new name: "
056: + ctx.getAttributes().getValue(
057: "classname"));
058: assert (false);
059: }
060: assert (m_object == null);
061: m_classname = ctx.getAttributes().getValue("classname");
062: ctx.push(this );
063: try {
064: m_object = instantiateObject(XMLUtils
065: .toJMLAttributes(ctx.getAttributes()));
066: } catch (Exception e) {
067: throw new SAXException(
068: e.getMessage()
069: + " - ObjectHandler unable to instantiate object for classname: "
070: + ctx.getAttributes().getValue(
071: "classname"), e);
072: }
073: } else if ("super".equalsIgnoreCase(ctx.getQualifiedName())) {
074: assert (m_super ClassHandler == null);
075: m_super ClassHandler = new JETAPersistableHandler();
076: ctx.push(m_super ClassHandler);
077: // don't call start element for <super>
078: } else if ("at".equalsIgnoreCase(ctx.getQualifiedName())) {
079: PropertyHandler phandler = new PropertyHandler(this );
080: phandler.startElement(ctx);
081: } else {
082: throw new SAXException(
083: "Invalid tag. Expecting <object classname=\"...\">. Got instead: "
084: + ctx.getQualifiedName());
085: }
086: }
087:
088: protected Object instantiateObject(JMLAttributes attribs)
089: throws InstantiationException, IllegalAccessException,
090: ClassNotFoundException {
091: String value = attribs.getValue("classname");
092: if ("null".equalsIgnoreCase(value) || value.length() == 0)
093: return null;
094: else
095: return Class.forName(value).newInstance();
096: }
097:
098: public void endElement(XMLNodeContext ctx) throws SAXException {
099: if ("object".equalsIgnoreCase(ctx.getQualifiedName())) {
100: ctx.pop(this );
101: } else if ("super".equalsIgnoreCase(ctx.getQualifiedName())) {
102: ctx.pop(this );
103: } else {
104: throw new SAXException(
105: "Invalid tag. Expecting <object classname=\"...\">. Got instead: "
106: + ctx.getQualifiedName());
107: }
108: }
109:
110: public Object getObject() {
111: return m_object;
112: }
113:
114: public ObjectHandler getSuperClassHandler() {
115: return m_super ClassHandler == null ? this : m_super ClassHandler;
116: }
117:
118: public void characters(char[] ch, int start, int length)
119: throws SAXException {
120: // ignore
121: }
122:
123: }
|