View Javadoc
1   /*
2    * Copyright 2005 Edward Kuns
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Compiles a directory tree, recursively. This class is built around the use of
30   * the ANT JAVAC task.
31   */
32  public class AntJavaCompiler implements Compiler {
33      /** A Set of directories to ignore. */
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       * Creates a compiler for a given directory.
52       *
53       * @param baseDirectory The directory that holds the files to be compiled.
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       * Sets the Java source version the current test will be using.
65       * @param javaSourceVersion The Java Source version to be used.
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       * Compiles the content of a directory. Throws a <code>CompilationException</code>
77       * if the build fails.
78       */
79      public void compileDirectory() {
80          _compiler = makeCompiler(_baseDirectory, _outputDirectory);
81          compileDirectory(_baseDirectory, _outputDirectory);
82      }
83  
84      /**
85       * Creates and returns a Ant Javac compiler.
86       * @param srcDir Source directory for compiation.
87       * @param destDir Destination directory for compilation.
88       *
89       * @return Ant Javac compiler
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      * Compiles a directory tree. Throws a <code>CompilationException</code> if build
121      * fails.
122      *
123      * @param srcDir Source directory holding the files to be compiled.
124      * @param destDir Destination directory to put the compiled classes in.
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 }