1 package org.oddjob.designer.arooa; 2 3 import java.io.ByteArrayOutputStream ; 4 5 import org.apache.log4j.Logger; 6 import org.oddjob.arooa.ArooaException; 7 import org.oddjob.arooa.ArooaHandler; 8 import org.oddjob.arooa.ArooaContext; 9 import org.oddjob.arooa.ArooaConstants; 10 import org.oddjob.arooa.Lifecycle; 11 import org.oddjob.arooa.ObjectFactory; 12 import org.oddjob.arooa.handlers.XmlHandler; 13 import org.oddjob.arooa.reflect.BeanUtilsHelper; 14 import org.oddjob.arooa.reflect.IntrospectionHelper; 15 import org.oddjob.designer.components.UnknownDC; 16 import org.xml.sax.Attributes ; 17 import org.xml.sax.SAXParseException ; 18 19 26 27 public class DesignComponentHandler extends ArooaHandler { 28 private static final Logger logger = Logger.getLogger(DesignComponentHandler.class); 29 30 private static final String UNKNOWN = "unknown"; 31 32 private static final String NAME_ATTRIBUTE = "nameattribute"; 33 34 private ByteArrayOutputStream xmlOut = new ByteArrayOutputStream (); 35 private XmlHandler xmlHandler = new XmlHandler(xmlOut); 36 37 private DualHandler dualHandler; 38 39 40 54 public void onStartElement(String uri, String tag, String qname, 55 Attributes attrs, 56 ArooaContext context) 57 throws SAXParseException { 58 xmlHandler.onStartElement(uri, tag, qname, attrs, context); 59 context.set(NAME_ATTRIBUTE, attrs.getValue("name")); 60 61 ObjectFactory factory = (ObjectFactory) context.get(ArooaConstants.COMPONENT_FACTORY); 62 Object component = null; 63 try { 64 component = factory.createObject(tag); 65 } catch (Exception e) { 66 throw new SAXParseException ("Failed to create object - \"" + qname 67 + "\"", context.getLocator(), e); 68 } 69 if (component == null) { 70 component = factory.createObject("other"); 71 } 72 if (component instanceof UnknownDC) { 74 context.set(UNKNOWN, "true"); 75 } 76 77 IntrospectionHelper ih = IntrospectionHelper.getHelper(component.getClass()); 78 79 if (context.get(UNKNOWN) == null) { 80 try { 81 HandlerUtils.setAttributes(uri, attrs, component); 82 } catch (Exception e) { 83 logger.debug("Tag [" + tag + "], failed to set attribute (" 84 + e.getMessage() + "), switching to unknown."); 85 context.set(UNKNOWN, "true"); 86 } 87 } 88 89 context.set(ArooaConstants.CURRENTLY_CONFIGURING, component); 90 } 91 92 96 public void characters(char[] buf, int start, int count, 97 ArooaContext context) 98 throws SAXParseException { 99 xmlHandler.characters(buf, start, count, context); 100 101 Object component = context.get(ArooaConstants.CURRENTLY_CONFIGURING); 102 IntrospectionHelper ih = IntrospectionHelper.getHelper(component.getClass()); 103 try { 104 ih.addText(component, new String (buf, start, count)); 105 } catch (ArooaException e) { 106 context.set(UNKNOWN, "true"); 107 } 108 } 109 110 114 public ArooaHandler onStartChild(String uri, String tag, String qname, 115 Attributes attrs, 116 ArooaContext context) 117 throws SAXParseException { 118 ArooaHandler nextXmlHandler = xmlHandler.onStartChild(uri, tag, qname, attrs, context); 119 context.set(ArooaConstants.COMPONENT_HANDLER, new DesignComponentHandler()); 120 if (context.getLocal(UNKNOWN) == null) { 121 Object component = context.get(ArooaConstants.CURRENTLY_CONFIGURING); 122 IntrospectionHelper ih = IntrospectionHelper.getHelper(component.getClass()); 123 ArooaHandler nextOtherHandler = ih.provideHandler(component, tag, context); 124 dualHandler = new DualHandler(nextXmlHandler, nextOtherHandler); 125 return dualHandler; 126 } 127 else { 128 return nextXmlHandler; 129 } 130 } 131 132 public void onEndChild(String uri, String tag, String qname, 133 ArooaContext context) 134 throws SAXParseException { 135 xmlHandler.onEndChild(uri, tag, qname, context); 136 if (dualHandler != null && !dualHandler.isHandler2OK()) { 137 context.set(UNKNOWN, "true"); 138 } 139 } 140 141 145 public void onEndElement(String uri, String tag, ArooaContext context) { 146 xmlHandler.onEndElement(uri, tag, context); 147 148 Object component = context.get(ArooaConstants.CURRENTLY_CONFIGURING); 149 if (context.get(UNKNOWN) != null) { 150 DesignIH dih = DesignIH.getHelper(component.getClass()); 151 String xml = xmlOut.toString(); 152 try { 153 dih.unknown(component, xml); 154 } 155 catch (NullPointerException e) { 156 ObjectFactory factory = (ObjectFactory) context.get(ArooaConstants.COMPONENT_FACTORY); 157 component = factory.createObject("other"); 158 dih = DesignIH.getHelper(component.getClass()); 159 dih.unknown(component, xml); 160 } 161 IntrospectionHelper ih = IntrospectionHelper.getHelper(component.getClass()); 163 String name = (String ) context.getLocal(NAME_ATTRIBUTE); 164 if (name != null) { 165 BeanUtilsHelper.setProperty(component, "name", name); 166 } 167 BeanUtilsHelper.setProperty(component, "tag", tag); 168 } 169 Lifecycle.init(component); 170 171 Object parent = context.getParent().get(ArooaConstants.CURRENTLY_CONFIGURING); 172 IntrospectionHelper pih = IntrospectionHelper.getHelper(parent.getClass()); 173 pih.storeComponent(parent, component, 174 (String ) context.getParent().get(ArooaConstants.ELEMENT_NAME)); 175 } 176 } 177 | Popular Tags |