1 package org.oddjob.arooa.handlers; 2 3 import org.oddjob.arooa.ArooaHandler; 4 import org.oddjob.arooa.ArooaContext; 5 import org.oddjob.arooa.ArooaConstants; 6 import org.oddjob.arooa.ArooaRuntime; 7 import org.oddjob.arooa.ObjectFactory; 8 import org.oddjob.arooa.PropertyProxyResolver; 9 import org.oddjob.arooa.RuntimeConfiguration; 10 import org.oddjob.arooa.SimpleObjectFactory; 11 import org.oddjob.arooa.reflect.IntrospectionHelper; 12 import org.xml.sax.Attributes ; 13 import org.xml.sax.SAXParseException ; 14 15 36 37 public class MappedPropertyHandler extends ArooaHandler { 38 39 53 public void onStartElement(String uri, String tag, String qname, 54 Attributes attrs, 55 ArooaContext context) 56 throws SAXParseException { 57 Object valueObj = null; 58 AttributeHelper ah = new AttributeHelper(uri, attrs); 59 60 String className = ah.remove("class"); 61 62 if (className != null) { 64 try { 65 valueObj = SimpleObjectFactory.createObjectFromClass(className); 66 } 67 catch (Exception e) { 68 throw new SAXParseException ("Could not create object of class" 69 + className + " for type \"" + tag + "\" (" 70 + e.getMessage() + ")", context.getLocator(), e); 71 } 72 } 73 74 if (valueObj == null) { 75 ObjectFactory factory = (ObjectFactory)context.get(ArooaConstants.VALUE_FACTORY); 76 try { 77 valueObj = factory.createObject(tag); 78 } catch (Exception e) { 79 } 81 } 82 83 ArooaRuntime parentWrapper = 84 (ArooaRuntime) context.get( 85 ArooaConstants.CURRENTLY_CONFIGURING); 86 Object parent = parentWrapper.getWrappedObject(); 87 88 if (valueObj == null) { 89 PropertyProxyResolver pr = (PropertyProxyResolver) context.get(ArooaConstants.PROPERTY_PROXY_RESOLVER); 90 if (pr == null) { 91 throw new IllegalStateException ("Type Manager is missing!"); 92 } 93 try { 94 valueObj = pr.proxyFor(parent, 95 (String ) context.getParent().get(ArooaConstants.ELEMENT_NAME)); 96 } catch (Exception e) { 97 throw new SAXParseException ("Failed to create value for \"" + qname 98 + "\" (" + e.getMessage() + ")", context.getLocator(), e); 99 } 100 } 101 102 if (valueObj == null) { 103 throw new SAXParseException ("Unexpected type \"" + qname 104 + "\"", context.getLocator()); 105 } 106 107 final ArooaRuntime wrapper = new ArooaRuntime(valueObj, tag, context); 108 String name = ah.remove("name"); 109 if (name != null) { 110 try { 111 wrapper.setAttribute("name", name); 112 } 113 catch (Exception e) { 114 } 116 } 117 ah.process(new AttributeHelper.Processor() { 118 public void process(String name, String value) { 119 wrapper.setAttribute(name, value); 120 } 121 }); 122 123 parentWrapper.setMappedProperty( 124 (String )context.getParent().get(ArooaConstants.ELEMENT_NAME), name, wrapper); 125 126 context.set(ArooaConstants.CURRENTLY_CONFIGURING, wrapper); 127 } 128 129 140 public ArooaHandler onStartChild(String uri, String tag, String qname, 141 Attributes attrs, 142 ArooaContext context) 143 throws SAXParseException { 144 RuntimeConfiguration currentWrapper 145 = (RuntimeConfiguration ) context.get( 146 ArooaConstants.CURRENTLY_CONFIGURING); 147 Object object = currentWrapper.getWrappedObject(); 148 149 IntrospectionHelper ih = IntrospectionHelper.getHelper(object.getClass()); 150 return ih.provideHandler(object, tag, context); 151 } 152 153 } 154 155 | Popular Tags |