001: package com.jeta.forms.store.xml.parser;
002:
003: import org.xml.sax.SAXException;
004:
005: import com.jeta.forms.store.jml.JMLUtils;
006: import com.jeta.forms.store.jml.dom.JMLAttributes;
007: import com.jeta.forms.store.xml.XMLUtils;
008:
009: public class PropertyHandler implements XMLHandler {
010:
011: private String m_propname;
012: private ObjectHandler m_parent;
013: private ObjectHandler m_objectHandler;
014: private String m_propValue;
015: private JMLAttributes m_attribs;
016:
017: public PropertyHandler(ObjectHandler parent) {
018: m_parent = parent;
019: }
020:
021: public void characters(char[] ch, int start, int length)
022: throws SAXException {
023: if (m_propValue == null)
024: m_propValue = XMLUtils.unescape(new String(ch, start,
025: length));
026: else
027: m_propValue = m_propValue
028: + XMLUtils.unescape(new String(ch, start, length));
029: }
030:
031: public void startElement(XMLNodeContext ctx) throws SAXException {
032: try {
033: String nodename = ctx.getQualifiedName();
034: if ("at".equalsIgnoreCase(nodename)) {
035: assert (m_propname == null);
036: m_attribs = XMLUtils.toJMLAttributes(ctx
037: .getAttributes());
038: m_propname = ctx.getAttributes().getValue("name");
039: /** check for inline object definition */
040: String object = ctx.getAttributes().getValue("object");
041: if (object != null) {
042: m_objectHandler = (InlineObjectHandler) XMLHandlerFactory
043: .getInstance().getHandler(object);
044: }
045: ctx.push(this );
046: } else if ("object".equalsIgnoreCase(nodename)) {
047: assert (m_objectHandler == null);
048: m_objectHandler = (ObjectHandler) XMLHandlerFactory
049: .getInstance().getHandler(
050: ctx.getAttributes().getValue(
051: "classname"));
052: assert (m_objectHandler != null);
053: m_objectHandler.startElement(ctx);
054: } else {
055: throw JMLUtils
056: .createSAXException("Invalid tag. Expecting <at name=\"...\">. Got instead: "
057: + nodename);
058: }
059: } catch (Exception e) {
060: throw JMLUtils.createSAXException(e);
061: }
062: }
063:
064: public void endElement(XMLNodeContext ctx) throws SAXException {
065: if ("at".equalsIgnoreCase(ctx.getQualifiedName())) {
066: String prop = m_propValue;
067: if (m_objectHandler instanceof InlineObjectHandler) {
068: /**
069: * handle case for inline objects. for example a color: <at
070: * name="foreground" object="color">0,0,255</at>
071: */
072: try {
073: InlineObjectHandler inlineHandler = (InlineObjectHandler) m_objectHandler;
074: Object object = inlineHandler.instantiateObject(
075: m_attribs, prop);
076: m_parent.setProperty(m_propname, object, m_attribs);
077: } catch (Exception e) {
078: e.printStackTrace();
079: throw new SAXException(
080: "Unable to create inline object for: "
081: + m_attribs.getValue("object")
082: + " value: " + prop);
083: }
084: } else if (m_objectHandler == null) {
085: /**
086: * this case handles standard properties that are not considered
087: * objects: Strings, Java primitives
088: */
089: m_parent.setProperty(m_propname, prop, m_attribs);
090: } else {
091: /** standard case. object property: <at name="border"><object>...</object></at> */
092: m_parent.setProperty(m_propname, m_objectHandler
093: .getObject(), m_attribs);
094: }
095:
096: ctx.pop(this );
097: } else {
098: throw JMLUtils
099: .createSAXException("Invalid tag. Expecting </at> Got instead: "
100: + ctx.getQualifiedName());
101: }
102: }
103: }
|