001: /*
002: * Copyright 2006 Ethan Nicholas. All rights reserved.
003: * Use is subject to license terms.
004: */
005: package jaxx;
006:
007: import java.io.*;
008: import java.lang.reflect.*;
009: import java.net.*;
010: import java.util.*;
011: import org.apache.tools.ant.*;
012: import org.apache.tools.ant.taskdefs.*;
013: import org.apache.tools.ant.util.*;
014:
015: import jaxx.compiler.*;
016:
017: public class JaxxcAntTask extends MatchingTask {
018: private File srcDir;
019: private File destDir;
020: private String classPath;
021: private boolean keepJavaFiles;
022: private boolean optimize;
023: private boolean runJavac = true;
024: private String javacOpts;
025: protected File[] compileList = new File[0];
026:
027: // add dt.jar to the system class path so Introspector will find the JAXXBeanInfos
028: public void init() {
029: URL resourceURL = JaxxcAntTask.class.getClassLoader()
030: .getResource("javax/swing/JButtonBeanInfo.class");
031: String resourceString = resourceURL != null ? resourceURL
032: .toString() : null;
033: if (resourceURL == null || !resourceString.startsWith("jar:"))
034: throw new BuildException(
035: "could not find dt.jar on classpath");
036: try {
037: URL jarURL = new URL(resourceString.substring("jar:"
038: .length(), resourceString.indexOf("!")));
039: URLClassLoader classLoader = (URLClassLoader) ClassLoader
040: .getSystemClassLoader();
041: Method addURL = URLClassLoader.class.getDeclaredMethod(
042: "addURL", new Class[] { URL.class });
043: addURL.setAccessible(true);
044: addURL.invoke(classLoader, new Object[] { jarURL });
045: } catch (Exception e) {
046: throw new BuildException(e);
047: }
048: }
049:
050: public void execute() {
051: checkParameters();
052: resetFileLists();
053: DirectoryScanner ds = getDirectoryScanner(srcDir);
054: String[] files = ds.getIncludedFiles();
055: scanDir(srcDir, destDir != null ? destDir : srcDir, files);
056: if (compileList.length > 0) {
057: log("Compiling " + compileList.length + " source file"
058: + (compileList.length == 1 ? "" : "s")
059: + (destDir != null ? " to " + destDir : ""));
060: compile();
061: }
062: }
063:
064: private void checkParameters() {
065: if (srcDir == null) {
066: throw new BuildException("srcdir attribute must be set!",
067: getLocation());
068: }
069:
070: if (!srcDir.isDirectory()) {
071: throw new BuildException("source directory \"" + srcDir
072: + "\" does not exist " + "or is not a directory",
073: getLocation());
074: }
075:
076: if (destDir != null && !destDir.isDirectory()) {
077: throw new BuildException("destination directory \""
078: + destDir + "\" does not exist "
079: + "or is not a directory", getLocation());
080: }
081: }
082:
083: private void compile() {
084: CompilerOptions options = new CompilerOptions();
085: if (classPath == null)
086: classPath = srcDir.getPath();
087: else
088: classPath = srcDir + File.pathSeparator + classPath;
089: options.setClassPath(classPath);
090: options.setTargetDirectory(destDir);
091: options.setKeepJavaFiles(keepJavaFiles);
092: options.setOptimize(optimize);
093: options.setRunJavac(runJavac);
094: options.setJavacOpts(javacOpts);
095: String[] files = new String[compileList.length];
096: for (int i = 0; i < compileList.length; i++)
097: if (compileList[i].getPath().startsWith(srcDir.getPath())) {
098: files[i] = compileList[i].getPath().substring(
099: srcDir.getPath().length());
100: if (files[i].startsWith(File.separator))
101: files[i] = files[i].substring(1);
102: } else
103: files[i] = compileList[i].getPath();
104: if (!JAXXCompiler.compile(srcDir, files, options))
105: throw new BuildException(
106: "Aborting due to errors reported by jaxxc");
107: }
108:
109: public void setSrcDir(File srcDir) {
110: this .srcDir = srcDir;
111: }
112:
113: public void setDestDir(File destDir) {
114: this .destDir = destDir;
115: }
116:
117: public void setClassPath(String classPath) {
118: this .classPath = classPath;
119: }
120:
121: public void setKeepJavaFiles(boolean keepJavaFiles) {
122: this .keepJavaFiles = keepJavaFiles;
123: }
124:
125: public void setOptimize(boolean optimize) {
126: this .optimize = optimize;
127: }
128:
129: public void setRunJavac(boolean runJavac) {
130: this .runJavac = runJavac;
131: }
132:
133: public void setJavacOptions(String javacOpts) {
134: this .javacOpts = javacOpts;
135: }
136:
137: /**
138: * Clear the list of files to be compiled and copied..
139: */
140: protected void resetFileLists() {
141: compileList = new File[0];
142: }
143:
144: /**
145: * Scans the directory looking for source files to be compiled.
146: * The results are returned in the class variable compileList
147: *
148: * @param srcDir The source directory
149: * @param destDir The destination directory
150: * @param files An array of filenames
151: */
152: protected void scanDir(File srcDir, File destDir, String[] files) {
153: GlobPatternMapper m = new GlobPatternMapper();
154: m.setFrom("*.jaxx");
155: m.setTo("*.class");
156: SourceFileScanner sfs = new SourceFileScanner(this );
157: File[] jaxxFiles = sfs.restrictAsFiles(files, srcDir, destDir,
158: m);
159: m.setFrom("*.css");
160: File[] cssFiles = sfs
161: .restrictAsFiles(files, srcDir, destDir, m);
162: m.setFrom("*.script");
163: File[] scriptFiles = sfs.restrictAsFiles(files, srcDir,
164: destDir, m);
165:
166: Set/*<File>*/newFiles = new HashSet/*<File>*/(Arrays
167: .asList(jaxxFiles));
168: // if we find an updated .css or .script file, check for a corresponding .jaxx file
169: for (int i = 0; i < cssFiles.length; i++) {
170: String path = cssFiles[i].getPath();
171: File jaxxFile = new File(path.substring(0, path.length()
172: - ".css".length())
173: + ".jaxx");
174: if (jaxxFile.exists())
175: newFiles.add(jaxxFile);
176: }
177: for (int i = 0; i < scriptFiles.length; i++) {
178: String path = scriptFiles[i].getPath();
179: File jaxxFile = new File(path.substring(0, path.length()
180: - ".script".length())
181: + ".jaxx");
182: if (jaxxFile.exists())
183: newFiles.add(jaxxFile);
184: }
185:
186: if (newFiles.size() > 0) {
187: File[] newCompileList = new File[compileList.length
188: + newFiles.size()];
189: System.arraycopy(compileList, 0, newCompileList, 0,
190: compileList.length);
191: System.arraycopy(newFiles.toArray(), 0, newCompileList,
192: compileList.length, newFiles.size());
193: compileList = newCompileList;
194: }
195: }
196: }
|