1 4 package org.oddjob.arooa.handlers; 5 6 import org.oddjob.arooa.ArooaRuntime; 7 import org.oddjob.arooa.ArooaContext; 8 import org.oddjob.arooa.ArooaConstants; 9 import org.oddjob.arooa.ObjectFactory; 10 import org.oddjob.arooa.SimpleObjectFactory; 11 import org.xml.sax.Attributes ; 12 import org.xml.sax.SAXParseException ; 13 14 29 public class ValueHandler extends TypicalElementHandler { 30 31 32 public static final String DEFAULT_ELEMENT_TAG = "value"; 33 34 35 private final String elementTag; 36 37 39 private ObjectFactory valueFactory; 40 41 45 public ValueHandler() { 46 this(DEFAULT_ELEMENT_TAG); 47 } 48 49 53 public ValueHandler(String elementTag) { 54 this.elementTag = elementTag; 55 } 56 57 63 public void setValueFactory(ObjectFactory valueFactory) { 64 this.valueFactory = valueFactory; 65 } 66 67 71 public void onStartElement(String uri, String tag, String qname, 72 Attributes attrs, 73 ArooaContext context) 74 throws SAXParseException { 75 Object valueObj = null; 76 AttributeHelper ah = new AttributeHelper(uri, attrs); 77 String className = ah.remove("class"); 78 if (className != null) { 79 try { 80 valueObj = SimpleObjectFactory.createObjectFromClass(className); 81 } 82 catch (Exception e) { 83 throw new SAXParseException ("Could not create object of class" 84 + className + " for type \"" + tag + "\" (" + e.getMessage() + ")", 85 context.getLocator(), e); 86 } 87 } 88 if (valueObj == null) { 89 ObjectFactory factory = valueFactory; 90 if (factory == null) { 91 factory = (ObjectFactory) context.get(ArooaConstants.VALUE_FACTORY); 92 } 93 if (factory == null) { 94 throw new SAXParseException ("No value factory for creating value of type \"" + qname 95 + "\"", context.getLocator()); 96 } 97 try { 98 valueObj = factory.createObject(tag); 99 } catch (Exception e) { 100 throw new SAXParseException ("Failed to create value for \"" + qname 101 + "\" (" + e.getMessage() + ")", context.getLocator(), e); 102 } 103 } 104 if (valueObj == null) { 105 throw new SAXParseException ("Unexpected type \"" + qname 106 + "\"", context.getLocator()); 107 } 108 109 final ArooaRuntime wrapper = new ArooaRuntime(valueObj, elementTag, context); 113 ah.process(new AttributeHelper.Processor() { 114 117 public void process(String name, String value) { 118 wrapper.setAttribute(name, value); 119 } 120 }); 121 122 ArooaRuntime parentWrapper 123 = (ArooaRuntime) context.get( 124 ArooaConstants.CURRENTLY_CONFIGURING); 125 parentWrapper.addChild(wrapper); 126 context.set(ArooaConstants.CURRENTLY_CONFIGURING, wrapper); 127 } 128 129 } 130 | Popular Tags |