1 15 package org.apache.hivemind.lib.groovy; 16 17 import groovy.lang.Binding; 18 import groovy.lang.GroovyCodeSource; 19 import groovy.lang.GroovyShell; 20 import groovy.lang.Script; 21 22 import java.io.IOException ; 23 24 import javax.xml.parsers.SAXParser ; 25 26 import org.apache.hivemind.ApplicationRuntimeException; 27 import org.apache.hivemind.ClassResolver; 28 import org.apache.hivemind.ErrorHandler; 29 import org.apache.hivemind.Resource; 30 import org.apache.hivemind.parse.DescriptorParser; 31 import org.apache.hivemind.parse.ModuleDescriptor; 32 import org.apache.hivemind.parse.XmlResourceProcessor; 33 import org.xml.sax.SAXException ; 34 35 43 class GroovyScriptProcessor extends XmlResourceProcessor 44 { 45 private GroovyShell _groovyShell; 46 47 public GroovyScriptProcessor(ClassResolver resolver, ErrorHandler errorHandler) 48 { 49 super(resolver, errorHandler); 50 } 51 52 protected ModuleDescriptor parseResource(Resource resource, SAXParser parser, 53 DescriptorParser contentHandler) throws SAXException , IOException 54 { 55 HiveMindBuilder builder = new HiveMindBuilder(contentHandler); 56 57 GroovyCodeSource source = new GroovyCodeSource(resource.getResourceURL()); 58 Script script; 59 60 try 61 { 62 script = getGroovyShell().parse(source); 63 } 64 catch (Exception e) 65 { 66 throw new ApplicationRuntimeException(e); 67 } 68 69 Binding processorBinding = new Binding(); 70 processorBinding.setVariable("processor", builder); 71 72 script.setBinding(processorBinding); 73 74 script.run(); 75 76 return contentHandler.getModuleDescriptor(); 77 } 78 79 private GroovyShell getGroovyShell() 80 { 81 if (_groovyShell == null) 82 _groovyShell = new GroovyShell(_resolver.getClassLoader()); 83 84 return _groovyShell; 85 } 86 } | Popular Tags |