Index: C:/Java/castor-2/src/tests/main/org/exolab/castor/tests/framework/AntJavaCompiler.java =================================================================== --- C:/Java/castor-2/src/tests/main/org/exolab/castor/tests/framework/AntJavaCompiler.java (revision 0) +++ C:/Java/castor-2/src/tests/main/org/exolab/castor/tests/framework/AntJavaCompiler.java (revision 0) @@ -0,0 +1,125 @@ +/* + * Copyright 2005 Edward Kuns + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.exolab.castor.tests.framework; + +import java.io.File; +import java.util.HashSet; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.taskdefs.Javac; +import org.apache.tools.ant.types.Path; + +/** + * Compiles a directory tree, recursively. This class is built around the use of + * the ANT JAVAC task. + */ +public class AntJavaCompiler implements Compiler { + private static final HashSet IGNORE_DIRS = new HashSet(); + + static { + IGNORE_DIRS.add("CVS"); + IGNORE_DIRS.add(".svn"); + IGNORE_DIRS.add("org"); + IGNORE_DIRS.add("com"); + IGNORE_DIRS.add("net"); + } + + private final File _baseDirectory; + private final File _outputDirectory; + + private Javac _compiler; + + /** + * Creates a compiler for a given directory. + * + * @param baseDirectory The directory that holds the files to be compiled. + */ + public AntJavaCompiler(final File baseDirectory) { + if (baseDirectory == null) { + throw new IllegalArgumentException("'baseDirectory' must not be null."); + } + _baseDirectory = baseDirectory; + _outputDirectory = baseDirectory; + } + + /** + * Compiles the content of a directory. Throws a CompilationException + * if the build fails. + */ + public void compileDirectory() { + _compiler = makeCompiler(_baseDirectory, _outputDirectory); + compileDirectory(_baseDirectory, _outputDirectory); + } + + /** + * Creates and returns a Ant Javac compiler. + * + * @return Ant Javac compiler + */ + private Javac makeCompiler(final File srcDir, final File destDir) { + Project project = new Project(); + project.init(); + project.setBasedir(srcDir.getAbsolutePath()); + + Javac compiler = new Javac(); + compiler.setProject(project); + compiler.setDestdir(destDir.getAbsoluteFile()); + compiler.setOptimize(false); + compiler.setDebug(true); + compiler.setDebugLevel("lines,vars,source"); + compiler.setIncludejavaruntime(true); + if (XMLTestCase._verbose) { + compiler.setListfiles(true); + compiler.setVerbose(true); + } else { + compiler.setNowarn(true); + } + + Path classpath = compiler.createClasspath(); + classpath.setPath(System.getProperty("java.class.path")); + classpath.add(new Path(project, destDir.getAbsolutePath())); + compiler.setClasspath(classpath); + return compiler; + } + + /** + * Compiles a directory tree. Throws a CompilationException if build + * fails. + * + * @param srcDir Source directory holding the files to be compiled. + * @param destDir Destination directory to put the compiled classes in. + */ + private void compileDirectory(final File srcDir, final File destDir) { + File[] entries = srcDir.listFiles(); + for (int i = 0; i < entries.length; i++) { + File entry = entries[i]; + if (entry.isDirectory() && !IGNORE_DIRS.contains(entry.getName())) { + compileDirectory(entry, destDir); + } + } + entries = null; + + try { + Path srcPath = _compiler.createSrc(); + srcPath.setLocation(destDir); + _compiler.setSrcdir(srcPath); + _compiler.execute(); + } catch (BuildException ex) { + throw new CompilationException("Problem compiling directory " + srcDir, ex); + } + } +} Index: C:/Java/castor-2/src/tests/main/org/exolab/castor/tests/framework/Compiler.java =================================================================== --- C:/Java/castor-2/src/tests/main/org/exolab/castor/tests/framework/Compiler.java (revision 6384) +++ C:/Java/castor-2/src/tests/main/org/exolab/castor/tests/framework/Compiler.java (working copy) @@ -16,7 +16,6 @@ package org.exolab.castor.tests.framework; public interface Compiler { - /** * Compiles the content of a directory tree. * @throws CompilationException If the build fails. @@ -22,5 +21,4 @@ * @throws CompilationException If the build fails. */ void compileDirectory() throws CompilationException; - } Index: C:/Java/castor-2/src/tests/main/org/exolab/castor/tests/framework/JavaCCompiler.java =================================================================== --- C:/Java/castor-2/src/tests/main/org/exolab/castor/tests/framework/JavaCCompiler.java (revision 6384) +++ C:/Java/castor-2/src/tests/main/org/exolab/castor/tests/framework/JavaCCompiler.java (working copy) @@ -1,134 +0,0 @@ -/* - * Copyright 2005 Edward Kuns - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.exolab.castor.tests.framework; - -import java.io.File; - -import org.apache.tools.ant.BuildException; -import org.apache.tools.ant.Project; -import org.apache.tools.ant.taskdefs.Javac; -import org.apache.tools.ant.types.Path; - -/** - * Compiles a directory tree, recursively. This class is built around the use of - * the ANT JAVAC task. - */ -public class JavaCCompiler implements Compiler { - - private final File _baseDirectory; - private final File _outputDirectory; - private final Javac _compiler; - - /** - * Creates a compiler for a given directory. - * @param baseDirectory The directory that holds the files to be compiled. - */ - public JavaCCompiler(final File baseDirectory) { - if (baseDirectory == null) { - throw new IllegalArgumentException("'baseDirectory' must not be null."); - } - _baseDirectory = baseDirectory; - _outputDirectory = baseDirectory; - _compiler = makeCompiler(); - } - - /** - * Creates a compiler for a given directory, specifying a destination directory as well. - * @param baseDirectory The directory that holds the files to be compiled. - * @param destDir The destination directory. - */ - public JavaCCompiler(final File baseDirectory, final File destDir) { - if (baseDirectory == null) { - throw new IllegalArgumentException("'baseDirectory' must not be null."); - } - if (destDir == null) { - throw new IllegalArgumentException("'destDir' must not be null."); - } - _baseDirectory = baseDirectory; - _outputDirectory = destDir; - _compiler = makeCompiler(); - } - - /** - * Compiles the content of a directory. - * @throws CompilationException If the build fails. - */ - public void compileDirectory() throws CompilationException { - try { - compileDirectory(_baseDirectory, _outputDirectory); - } - catch (BuildException e) { - throw new CompilationException ("Problem compiling directory " + _baseDirectory, e); - } - } //-- compileDirectory - - /** - * Creates and returns a Ant Javac compiler. - * @return a Ant Javac compiler - */ - private Javac makeCompiler() { - Project project = new Project(); - project.init(); - project.setBasedir(_baseDirectory.getAbsolutePath()); - - Javac compiler = new Javac(); - compiler.setProject(project); - compiler.setDestdir(_outputDirectory.getAbsoluteFile()); - compiler.setOptimize(false); - compiler.setDebug(true); - compiler.setDebugLevel("lines,vars,source"); - compiler.setIncludejavaruntime(true); - if (XMLTestCase._verbose) { - compiler.setListfiles(true); - compiler.setVerbose(true); - } else { - compiler.setNowarn(true); - } - - Path classpath = compiler.createClasspath(); - classpath.setPath(System.getProperty("java.class.path")); - classpath.add(new Path(project, _outputDirectory.getAbsolutePath())); - compiler.setClasspath(classpath); - return compiler; - } - - /** - * Compiles a directory tree. - * @param srcDir Source directory (holding the files to be compiled). - * @param root - * @throws BuildException If the build failes. - */ - private void compileDirectory(File srcDir, File root) throws BuildException { - File[] entries = srcDir.listFiles(); - for (int i=0; iCompiling any necessary source files in " + _outputRootFile); - Compiler compiler = new JavaCCompiler(_outputRootFile); + Compiler compiler = new SunJavaCompiler(_outputRootFile); try { compiler.compileDirectory(); _test.setDirectoryCompiled(true); Index: C:/Java/castor-2/src/tests/main/org/exolab/castor/tests/framework/SunJavaCompiler.java =================================================================== --- C:/Java/castor-2/src/tests/main/org/exolab/castor/tests/framework/SunJavaCompiler.java (revision 0) +++ C:/Java/castor-2/src/tests/main/org/exolab/castor/tests/framework/SunJavaCompiler.java (revision 0) @@ -0,0 +1,143 @@ +/* + * Copyright 2006 Ralf Jaochim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.exolab.castor.tests.framework; + +import java.io.File; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; + +/** + * Compiles a directory tree, recursively. This class is built to use the Sun Javac + * compiler contained in tools.jar. A IllegalStateException will be thrown if tools.jar + * is not on the classpath at construction of the class and execution of the + * compileDirectory() method. + * + * @author Ralf Joachim + * @version $Revision: 5951 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $ + * @since 1.0.5 + */ +public class SunJavaCompiler implements Compiler { + private static final String COMPILE_CLASSNAME = "com.sun.tools.javac.Main"; + private static final String COMPILE_METHODNAME = "compile"; + private static final Class[] COMPILE_PARAMTYPE = new Class[] {String[].class}; + + private static final HashSet IGNORE_DIRS = new HashSet(); + + private static Method _compileMethod = null; + private static boolean _initialized = false; + + private final File _baseDirectory; + private final File _outputDirectory; + + /** + * Creates a compiler for a given directory. + * @param baseDirectory The directory that holds the files to be compiled. + */ + public SunJavaCompiler(final File baseDirectory) { + if (baseDirectory == null) { + throw new IllegalArgumentException("'baseDirectory' must not be null."); + } + _baseDirectory = baseDirectory; + _outputDirectory = baseDirectory; + + if (!_initialized) { initialize(); } + } + + private void initialize() { + IGNORE_DIRS.add("CVS"); + IGNORE_DIRS.add(".svn"); + + try { + ClassLoader loader = this.getClass().getClassLoader(); + Class cls = loader.loadClass(COMPILE_CLASSNAME); + _compileMethod = cls.getMethod(COMPILE_METHODNAME, COMPILE_PARAMTYPE); + } catch (Exception ex) { + throw new IllegalStateException("Failed to find compile method."); + } + + _initialized = true; + } + + /** + * Compiles the content of a directory. Throws a CompilationException + * if the build fails. + */ + public void compileDirectory() { + List filesList = findSourceFiles(_baseDirectory); + if (filesList.size() > 0) { + filesList.addAll(0, getCompileArguments(_baseDirectory, _outputDirectory)); + + String[] args = new String[filesList.size()]; + args = (String[]) filesList.toArray(args); + + int status; + try { + Object result = _compileMethod.invoke(null, new Object[] {args}); + status = ((Integer) result).intValue(); + } catch (Exception ex) { + throw new IllegalStateException("Failed to call compile method."); + } + + switch (status) { + case 0: break; + case 1: throw new CompilationException("Compile status: ERROR"); + case 2: throw new CompilationException("Compile status: CMDERR"); + case 3: throw new CompilationException("Compile status: SYSERR"); + case 4: throw new CompilationException("Compile status: ABNORMAL"); + default: throw new CompilationException("Compile status: Unknown"); + } + } else { + throw new CompilationException("No files to compile: " + _baseDirectory); + } + } + + private List getCompileArguments(final File srcDir, final File destDir) { + List args = new ArrayList(); + + args.add("-g"); + if (XMLTestCase._verbose) { + args.add("-verbose"); + } else { + args.add("-nowarn"); + } + args.add("-classpath"); + args.add(System.getProperty("java.class.path") + ";" + destDir.getAbsolutePath()); + args.add("-d"); + args.add(destDir.getAbsolutePath()); + args.add("-sourcepath"); + args.add(srcDir.getAbsolutePath()); + + return args; + } + + private List findSourceFiles(final File srcDir) { + List files = new ArrayList(); + + File[] entries = srcDir.listFiles(); + for (int i = 0; i < entries.length; i++) { + File entry = entries[i]; + if (entry.getName().endsWith(".java")) { + files.add(entry.getAbsolutePath()); + } else if (entry.isDirectory() && !IGNORE_DIRS.contains(entry.getName())) { + files.addAll(findSourceFiles(entry)); + } + } + + return files; + } +} Index: C:/Java/castor-2/src/tests/main/org/exolab/castor/tests/framework/TestSourceGenerator.java =================================================================== --- C:/Java/castor-2/src/tests/main/org/exolab/castor/tests/framework/TestSourceGenerator.java (revision 6384) +++ C:/Java/castor-2/src/tests/main/org/exolab/castor/tests/framework/TestSourceGenerator.java (working copy) @@ -133,7 +133,7 @@ // 2. Compile the files generated by the source generator verbose("-->Compiling the files in " + _outputRootFile); try { - Compiler compiler = new JavaCCompiler(_outputRootFile); + Compiler compiler = new SunJavaCompiler(_outputRootFile); compiler.compileDirectory(); } catch (CompilationException e) { if (!checkExceptionWasExpected(e, FailureStepType.SOURCE_COMPILATION)) { Index: C:/Java/castor-2/src/tests/xml/MasterTestSuite/sourcegenerator/generationOnly/javaLang/element/TestDescriptor.xml =================================================================== --- C:/Java/castor-2/src/tests/xml/MasterTestSuite/sourcegenerator/generationOnly/javaLang/element/TestDescriptor.xml (revision 6384) +++ C:/Java/castor-2/src/tests/xml/MasterTestSuite/sourcegenerator/generationOnly/javaLang/element/TestDescriptor.xml (working copy) @@ -1,18 +1,18 @@ - - - Tests name comflicts with java.lang.* - Edward Kuns - - Tests name conflicts with java.lang.* - - basic capability - - test.xsd - - Test Source Generation - - false - - - + + + Tests name comflicts with java.lang.* + Edward Kuns + + Tests name conflicts with java.lang.* + + basic capability + + test.xsd + + Test Source Generation + + true + + + Index: C:/Java/castor-2/src/tests/xml/MasterTestSuite/sourcegenerator/generationOnly/javaLang/simpleType/TestDescriptor.xml =================================================================== --- C:/Java/castor-2/src/tests/xml/MasterTestSuite/sourcegenerator/generationOnly/javaLang/simpleType/TestDescriptor.xml (revision 6384) +++ C:/Java/castor-2/src/tests/xml/MasterTestSuite/sourcegenerator/generationOnly/javaLang/simpleType/TestDescriptor.xml (working copy) @@ -1,18 +1,18 @@ - - - Tests name comflicts with java.lang.* - Edward Kuns - - Tests name conflicts with java.lang.* - - basic capability - - test.xsd - - Test Source Generation - - false - - - + + + Tests name comflicts with java.lang.* + Edward Kuns + + Tests name conflicts with java.lang.* + + basic capability + + test.xsd + + Test Source Generation + + true + + +