1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.castor.xmlctf.compiler;
17
18 import java.io.File;
19 import java.util.HashSet;
20
21 import org.apache.tools.ant.BuildException;
22 import org.apache.tools.ant.Project;
23 import org.apache.tools.ant.taskdefs.Javac;
24 import org.apache.tools.ant.types.Path;
25 import org.castor.xmlctf.XMLTestCase;
26 import org.castor.xmlctf.util.FileServices;
27
28
29
30
31
32 public class AntJavaCompiler implements Compiler {
33
34 private static final HashSet IGNORE_DIRS = new HashSet();
35
36 static {
37 IGNORE_DIRS.add(FileServices.CVS);
38 IGNORE_DIRS.add(FileServices.SVN);
39 IGNORE_DIRS.add("org");
40 IGNORE_DIRS.add("com");
41 IGNORE_DIRS.add("net");
42 }
43
44 private final File _baseDirectory;
45 private final File _outputDirectory;
46
47 private String _javaVersion = null;
48 private Javac _compiler;
49
50
51
52
53
54
55 public AntJavaCompiler(final File baseDirectory) {
56 if (baseDirectory == null) {
57 throw new IllegalArgumentException("'baseDirectory' must not be null.");
58 }
59 _baseDirectory = baseDirectory;
60 _outputDirectory = baseDirectory;
61 }
62
63
64
65
66
67 public void setJavaSourceVersion(final float javaSourceVersion) {
68 float srcVersion = javaSourceVersion;
69 if (javaSourceVersion >= 5F && javaSourceVersion < 10F) {
70 srcVersion = 1.0F + (javaSourceVersion / 10F);
71 }
72 _javaVersion = "" + srcVersion;
73 }
74
75
76
77
78
79 public void compileDirectory() {
80 _compiler = makeCompiler(_baseDirectory, _outputDirectory);
81 compileDirectory(_baseDirectory, _outputDirectory);
82 }
83
84
85
86
87
88
89
90
91 private Javac makeCompiler(final File srcDir, final File destDir) {
92 Project project = new Project();
93 project.init();
94 project.setBasedir(srcDir.getAbsolutePath());
95
96 Javac compiler = new Javac();
97 compiler.setProject(project);
98 compiler.setDestdir(destDir.getAbsoluteFile());
99 compiler.setOptimize(false);
100 compiler.setDebug(true);
101 compiler.setDebugLevel("lines,vars,source");
102 compiler.setIncludejavaruntime(true);
103 if (XMLTestCase._verbose) {
104 compiler.setListfiles(true);
105 compiler.setVerbose(true);
106 } else {
107 compiler.setNowarn(true);
108 }
109 if (_javaVersion != null) {
110 compiler.setSource(_javaVersion);
111 }
112 Path classpath = compiler.createClasspath();
113 classpath.setPath(System.getProperty("java.class.path"));
114 classpath.add(new Path(project, destDir.getAbsolutePath()));
115 compiler.setClasspath(classpath);
116 return compiler;
117 }
118
119
120
121
122
123
124
125
126 private void compileDirectory(final File srcDir, final File destDir) {
127 File[] entries = srcDir.listFiles();
128 for (int i = 0; i < entries.length; i++) {
129 File entry = entries[i];
130 if (entry.isDirectory() && !IGNORE_DIRS.contains(entry.getName())) {
131 compileDirectory(entry, destDir);
132 }
133 }
134 entries = null;
135
136 try {
137 Path srcPath = _compiler.createSrc();
138 srcPath.setLocation(destDir);
139 _compiler.setSrcdir(srcPath);
140 _compiler.execute();
141 } catch (BuildException ex) {
142 throw new CompilationException("Problem compiling directory " + srcDir, ex);
143 }
144 }
145
146 }