1 package org.oddjob.arooa.handlers; 2 3 import org.oddjob.arooa.ArooaConstants; 4 import org.oddjob.arooa.ArooaContext; 5 import org.oddjob.arooa.ArooaHandler; 6 import org.oddjob.arooa.ArooaRuntime; 7 import org.oddjob.arooa.ComponentProxyResolver; 8 import org.oddjob.arooa.Lifecycle; 9 import org.oddjob.arooa.ObjectFactory; 10 import org.oddjob.arooa.SimpleObjectFactory; 11 import org.oddjob.arooa.reflect.IntrospectionHelper; 12 import org.oddjob.arooa.registry.ComponentRegistry; 13 import org.xml.sax.Attributes ; 14 import org.xml.sax.SAXParseException ; 15 16 23 24 public class DefaultComponentHandler extends TypicalElementHandler { 25 26 public static final String CURRENT_COMPONENT = "curruentcomponent"; 27 28 42 public void onStartElement(String uri, String tag, String qname, 43 Attributes attrs, 44 ArooaContext context) 45 throws SAXParseException { 46 Object component = null; 47 AttributeHelper ah = new AttributeHelper(uri, attrs); 48 49 String className = ah.remove("class"); 50 51 if (className != null) { 52 try { 53 component = SimpleObjectFactory.createObjectFromClass(className); 54 } 55 catch (Exception e) { 56 throw new SAXParseException ("Could not create object of class " 57 + className + " for tag " + tag + " - ", context.getLocator(), e); 58 } 59 } 60 if (component == null) { 61 ObjectFactory factory = (ObjectFactory)context.get(ArooaConstants.COMPONENT_FACTORY); 62 try { 63 component = factory.createObject(tag); 64 } catch (Exception e) { 65 throw new SAXParseException ("Failed to create component for \"" + qname 66 + "\" (" + e.getMessage() + ")", context.getLocator(), e); 67 } 68 } 69 if (component==null) { 70 throw new SAXParseException ("Unexpected element \"" + qname 71 + "\" " + tag, context.getLocator()); 72 } 73 74 final ComponentRegistry cr = (ComponentRegistry) context.get( 75 ArooaConstants.COMPONENT_REGISTRY); 76 final ArooaRuntime wrapper = new ArooaRuntime(component, tag, context); 77 ComponentProxyResolver resolver = (ComponentProxyResolver) context.get( 78 ArooaConstants.COMPONENT_PROXY_RESOLVER); 79 final Object theComponent = resolver == null ? component 80 : resolver.proxyFor(component); 81 82 ah.process(new AttributeHelper.Processor() { 83 public void process(String name, String value) { 84 if (name.equals("id") ) { 85 cr.register(value, theComponent); 86 try { 87 wrapper.setAttribute(name, value); 88 } 89 catch (Exception e) { 90 } 94 } 95 else { 96 wrapper.setAttribute(name, value); 97 } 98 } 99 }); 100 101 context.set(ArooaConstants.CURRENTLY_CONFIGURING, wrapper); 102 context.set(CURRENT_COMPONENT, theComponent); 103 104 if (!Lifecycle.setContext(theComponent, context)) { 106 ArooaContext parentContext = context.getParent(); 107 if (parentContext != null) { 108 ArooaRuntime parentWrapper 109 = (ArooaRuntime) parentContext.get( 110 ArooaConstants.CURRENTLY_CONFIGURING); 111 parentWrapper.link(wrapper); 112 } 113 } 114 } 115 116 117 124 public void onEndElement(String uri, String tag, ArooaContext context) { 125 ArooaContext parentContext = context.getParent(); 126 if (parentContext == null) { 127 return; 128 } 129 Object component = 130 context.get(CURRENT_COMPONENT); 131 Object parent = ((ArooaRuntime) parentContext 132 .get(ArooaConstants.CURRENTLY_CONFIGURING)).getWrappedObject(); 133 134 Lifecycle.init(component); 135 136 if (parent != null) { 137 IntrospectionHelper ih = IntrospectionHelper.getHelper(parent.getClass()); 138 ih.storeComponent(parent, component, 139 (String ) context.getParent().get(ArooaConstants.ELEMENT_NAME)); 140 } 141 } 142 } 143 144 | Popular Tags |