しつこくRhinoネタ。
タスク本体。
public class JscTask extends Task {protected String srcdir = null;
public void setSrcdir(String srcdir) {
this.srcdir = srcdir;
}protected String destdir = null;
public void setDestdir(String destdir) {
this.destdir = destdir;
}protected Path classpath = null;
public Path createClasspath() {
if (classpath == null)
classpath = new Path(getProject());return classpath;
}public void setClasspathref(Reference r) {
createClasspath().setRefid(r);
}public void setClasspath(Path classpath) {
createClasspath().add(classpath);
}protected Map exts = new HashMap();
protected Map impls = new HashMap();
public void addConfiguredE(EElement e) {
if (e.getExt() != null)
exts.put(e.getName(), e.getExt());if (e.getImpl() != null)
impls.put(e.getName(), e.getImpl());}
public void addConfiguredClasspath(ClasspathElement classpath) {
if (classpath.getRefid() != null)
createClasspath().setRefid(classpath.getRefid());if (classpath.getPath() != null)
createClasspath().add(classpath.getPath());
}public void execute() throws BuildException {
if (srcdir == null)
throw new BuildException("undefined element 'srcdir'.");
else if (destdir == null)
throw new BuildException("undefined element 'destdir'.");File src = new File(srcdir);
String root = getPackage(src);
List sources = new ArrayList();
loadSources(src, sources);for (int i = 0; i < sources.size(); i++) {
File js = (File) sources.get(i);
List args = getArgs(js, root);log(js.getName() + " " + args);
run(args);
}
}protected List getArgs(File js, String root) {
List args = new ArrayList();String ext = getE(exts, js.getName());
if (ext != null) {
args.add("-extends");
args.add(ext);
}String impl = getE(impls, js.getName());
if (impl != null) {
args.add("-implements");
args.add(impl);
}String pkg = getPackage(js);
if (pkg.length() > root.length()) {
args.add("-package");
args.add(pkg.substring(root.length() + 1));
}args.add("-d");
args.add(destdir);
args.add(js.getAbsolutePath());return args;
}protected void run(List args) {
CommandlineJava cmdl = new CommandlineJava();
cmdl.setClassname("org.mozilla.javascript.tools.jsc.Main");for (int i = 0; i < args.size(); i++)
cmdl.createArgument().setValue( (String) args.get(i) );ExecuteJava exe = new ExecuteJava();
exe.setJavaCommand(cmdl.getJavaCommand());
exe.setClasspath(classpath);
exe.execute(getProject());
}protected void loadSources(File src, final List sources) {
src.listFiles(new FileFilter() {
public boolean accept(File pathname) {
if (pathname.isDirectory()) {
loadSources(pathname, sources);
return false;
} else if (!pathname.getName().endsWith(".js")) {
return false;
} else {
sources.add(pathname);
return true;
}
}
});
}protected String getE(Map e, String name) {
for (Iterator names = e.keySet().iterator(); names.hasNext();) {
String regex = (String) names.next();if (name.matches(regex))
return (String) e.get(regex);
}return null;
}protected String getPackage(File src) {
if (src.isFile())
src = src.getParentFile();return src.getAbsolutePath().replaceAll("[\\\\/]+", ".").replaceAll("^[A-Za-z]:\\.?", "").replaceAll("\\.$", "");
}}
クラスパス要素。
public class ClasspathElement {protected Reference refid = null;
public Reference getRefid() {
return refid;
}public void setRefid(Reference refid) {
this.refid = refid;
}protected Path path = null;
public Path getPath() {
return path;
}public void setPath(Path path) {
this.path = path;
}}
拡張要素。
public class EElement {protected String name = null;
public String getName() {
return name;
}public void setName(String name) {
this.name = name;
}protected String ext = null;
public String getExt() {
return ext;
}public void setExt(String ext) {
this.ext = ext;
}protected String impl = null;
public String getImpl() {
return impl;
}public void setImpl(String impl) {
this.impl = impl;
}}
<jsc srcdir="${src.dir}" destdir="${dest.dir}">
<classpath refid="classpath" />
<e name="bar\.js" ext="java.lang.Object" impl="java.io.Serializable" />
</jsc>