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.ComponentRestorer; 8 import org.oddjob.arooa.Lifecycle; 9 import org.oddjob.arooa.registry.ComponentRegistry; 10 import org.xml.sax.Attributes ; 11 import org.xml.sax.SAXParseException ; 12 13 25 26 public class SerializedComponentHandler extends ArooaHandler { 27 28 29 private final ComponentRestorer restorer; 30 31 33 private final ArooaHandler delegate; 34 35 43 public SerializedComponentHandler(ArooaHandler delegate, 44 ComponentRestorer componentRestorer) { 45 this.delegate = delegate; 46 this.restorer = componentRestorer; 47 } 48 49 63 public void onStartElement(String uri, String tag, String qname, 64 Attributes attrs, 65 ArooaContext context) 66 throws SAXParseException { 67 68 AttributeHelper ah = new AttributeHelper(uri, attrs); 69 String id = ah.remove("id"); 70 71 Object component = null; 72 try { 73 if (id == null 74 || (component = restorer.restore(id)) 75 == null) 76 { 77 delegate.onStartElement(uri, tag, qname, 78 attrs, context); 79 return; 80 } 81 } catch (Exception e) { 82 throw new SAXParseException ("Failed to load component for id \"" 83 + id + "\" " + tag, context.getLocator(), e); 84 } 85 86 if (component==null) { 87 throw new SAXParseException ("Unexpected element \"" + qname 88 + "\" " + tag, context.getLocator()); 89 } 90 91 ComponentRegistry cr = (ComponentRegistry) context.get( 92 ArooaConstants.COMPONENT_REGISTRY); 93 if (id != null) { 94 cr.register(id, component); 95 } 96 97 final ArooaRuntime wrapper = new ArooaRuntime(component, tag, context); 98 wrapper.setConfigured(true); 99 ah.remove("class"); 100 ah.process(new AttributeHelper.Processor() { 101 public void process(String name, String value) { 102 wrapper.setAttribute(name, value); 103 } 104 }); 105 106 context.set(ArooaConstants.CURRENTLY_CONFIGURING, wrapper); 107 context.set(DefaultComponentHandler.CURRENT_COMPONENT, component); 108 109 if (!Lifecycle.setContext(component, context)) { 111 ArooaRuntime parentWrapper 112 = (ArooaRuntime) context.get( 113 ArooaConstants.CURRENTLY_CONFIGURING); 114 parentWrapper.link(wrapper); 115 } 116 } 117 118 130 public void characters(char[] buf, int start, int count, 131 ArooaContext context) 132 throws SAXParseException { 133 delegate.characters(buf, start, count, context); 134 } 135 136 147 public ArooaHandler onStartChild(String uri, String tag, String qname, 148 Attributes attrs, 149 ArooaContext context) 150 throws SAXParseException { 151 return delegate.onStartChild(uri, tag, qname, attrs, context); 152 } 153 154 163 public void onEndChild(String uri, String tag, String qname, 164 ArooaContext context) 165 throws SAXParseException { 166 delegate.onEndChild(uri, tag, qname, context); 167 } 168 169 176 public void onEndElement(String uri, String tag, ArooaContext context) { 177 delegate.onEndElement(uri, tag, context); 178 } 179 180 } 181 182 | Popular Tags |