01: /*
02: * GroovyCompiler.java March 2006
03: *
04: * Copyright (C) 2006, Niall Gallagher <niallg@users.sf.net>
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13: * GNU Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General
16: * Public License along with this library; if not, write to the
17: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18: * Boston, MA 02111-1307 USA
19: */
20:
21: package simple.page.compile;
22:
23: import org.codehaus.groovy.ant.Groovyc;
24: import org.apache.tools.ant.types.Path;
25: import org.apache.tools.ant.Project;
26: import simple.page.translate.Source;
27: import simple.page.Workspace;
28: import java.io.File;
29:
30: /**
31: * The <code>GroovyCompiler</code> is used to compile Groovy sources
32: * from within the workspace build path. This will attempt to locate all
33: * files ending with the file extension ".groovy". This can be used in
34: * conjunction with the Java compilation task as it will ignore Java
35: * source files. This will compile all sources within the build path.
36: *
37: * @author Niall Gallagher
38: */
39: final class GroovyCompiler extends Compiler {
40:
41: /**
42: * Constructor for the <code>GroovyCompiler</code> object. This is
43: * used to create a compiler implementation that can compile Groovy
44: * source files within the workspace build path.
45: *
46: * @param project this is the workspace to be used for compilation
47: */
48: public GroovyCompiler(Workspace project) throws Exception {
49: super (project);
50: }
51:
52: /**
53: * This method is used to perform the actual compilation of the
54: * source files within the workspace build path. This makes use of
55: * the Ant <code>Groovyc</code> task definition to compile Groovy
56: * source files, all sources within the build path are compiled.
57: *
58: * @param source the source file to be compiled and loaded
59: *
60: * @return this is the class definition compiled and loaded
61: */
62: public Class compile(Source source) throws Exception {
63: return compile(source, null);
64: }
65:
66: /**
67: * This method is used to perform the actual compilation of the
68: * source files within the workspace build path. This makes use of
69: * the Ant <code>Groovyc</code> task definition to compile Groovy
70: * source files, all sources within the build path are compiled.
71: *
72: * @param source the source file to be compiled and loaded
73: * @param path this is the full class path for the Groovy compiler
74: *
75: * @return this is the class definition compiled and loaded
76: */
77: public Class compile(Source source, Path path) throws Exception {
78: Project project = new Project();
79: Groovyc compiler = new Groovyc();
80: String base = root.getAbsolutePath();
81:
82: project.init();
83: compiler.setProject(project);
84: compiler.setSrcdir(new Path(project, base));
85: compiler.setDestdir(project.resolveFile(base));
86: compiler.setClasspath(path);
87: compiler.execute();
88:
89: return load(source);
90: }
91:
92: }
|