KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > lib > groovy > GroovyScriptProcessor


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

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 JavaDoc;
23
24 import javax.xml.parsers.SAXParser JavaDoc;
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 JavaDoc;
34
35 /**
36  * This extension of the {@link org.apache.hivemind.parse.XmlResourceProcessor} is suitable for
37  * processing Groovy scripts and is used internally by {@link GroovyModuleDescriptorProvider}.
38  *
39  * @see org.apache.hivemind.lib.groovy.GroovyModuleDescriptorProvider
40  * @author Knut Wannheden
41  * @since 1.1
42  */

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 JavaDoc parser,
53             DescriptorParser contentHandler) throws SAXException JavaDoc, IOException JavaDoc
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 JavaDoc 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