1 package org.oddjob.ant; 2 3 import org.apache.tools.ant.ComponentHelper; 4 import org.apache.tools.ant.Location; 5 import org.apache.tools.ant.Project; 6 import org.apache.tools.ant.Task; 7 import org.oddjob.arooa.ArooaException; 8 import org.oddjob.arooa.ArooaHandler; 9 import org.oddjob.arooa.ArooaContext; 10 import org.oddjob.arooa.ArooaConstants; 11 import org.oddjob.arooa.ArooaRuntime; 12 import org.oddjob.arooa.Lifecycle; 13 import org.oddjob.arooa.RuntimeConfiguration; 14 import org.oddjob.arooa.reflect.IntrospectionHelper; 15 import org.xml.sax.Attributes ; 16 import org.xml.sax.SAXParseException ; 17 18 25 26 public class AntJobComponentHandler extends ArooaHandler { 27 28 private Project project; 29 30 34 public AntJobComponentHandler(Project project) { 35 this.project = project; 36 } 37 38 52 public void onStartElement(String uri, String tag, String qname, 53 Attributes attrs, 54 ArooaContext context) 55 throws SAXParseException { 56 57 ArooaRuntime parentWrapper 58 = (ArooaRuntime) context.get( 59 ArooaConstants.CURRENTLY_CONFIGURING); 60 61 ComponentHelper helper = ComponentHelper.getComponentHelper( 62 project); 63 Object o = helper.createComponent(tag); 64 65 if (o == null) { 66 throw new ArooaException("Failed to create ant task or type [" + tag + "]"); 67 } 68 69 if (o instanceof Task) { 70 Task task = (Task)o; 71 task.setProject(project); 72 task.setTaskName(tag); 73 task.setTaskType(tag); 74 Location location = new Location(context.getLocator().getSystemId(), 75 context.getLocator().getLineNumber(), 76 context.getLocator().getColumnNumber()); 77 task.setLocation(location); 78 } 79 80 81 AntJobRtc wrapper 82 = new AntJobRtc(project, o, tag); 83 84 for (int i = 0; i < attrs.getLength(); i++) { 85 String attrUri = attrs.getURI(i); 86 if (attrUri != null 87 && !attrUri.equals("") 88 && !attrUri.equals(uri)) { 89 continue; } 91 String name = attrs.getLocalName(i); 92 String value = attrs.getValue(i); 93 if (name.equals("id")) { 95 project.addReference(value, o); 96 } 97 else { 98 wrapper.setAttribute(name, value); 99 } 100 } 101 102 parentWrapper.link(wrapper); 103 context.set(ArooaConstants.CURRENTLY_CONFIGURING, wrapper); 104 } 105 106 118 public void characters(char[] buf, int start, int count, 119 ArooaContext context) 120 throws SAXParseException { 121 AntJobRtc wrapper 122 = (AntJobRtc) context.get( 123 ArooaConstants.CURRENTLY_CONFIGURING); 124 wrapper.addText(buf, start, count); 125 } 126 127 138 public ArooaHandler onStartChild(String uri, String name, String qname, 139 Attributes attrs, 140 ArooaContext context) 141 throws SAXParseException { 142 return new AntElementHandler(project); 143 } 144 145 152 public void onEndElement(String uri, String tag, ArooaContext context) { 153 Object component = ((RuntimeConfiguration)context.get( 154 ArooaConstants.CURRENTLY_CONFIGURING)).getWrappedObject(); 155 Object parent = ((RuntimeConfiguration)context.getParent().get( 156 ArooaConstants.CURRENTLY_CONFIGURING)).getWrappedObject(); 157 158 Lifecycle.init(component); 159 160 if (parent != null) { 161 IntrospectionHelper ih = IntrospectionHelper.getHelper(parent.getClass()); 162 ih.storeComponent(parent, component, ""); 163 } 164 } 165 166 } 167 168 | Popular Tags |