package com.isx.ant.plistgen; import org.apache.tools.ant.Task; import org.apache.tools.ant.Project; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.types.Path; import org.apache.tools.ant.types.Reference; import java.io.*; import java.util.*; public class PListGen extends Task { public PListGen() { } private Path sourcePath = null; private String packageList = null; public Path createSourcepath() { if (sourcePath == null) { sourcePath = new Path(project); } return sourcePath.createPath(); } public void setSourcepath(Path src) { if (sourcePath == null) { sourcePath = src; } else { sourcePath.append(src); } } public void setSourcepathRef(Reference r) { createSourcepath().setRefid(r); } public void setPackageList(String s) { packageList = s; } private void addPackageIfDirectoryWithJavaFiles(String prefix, File dir, Set packages) { if (dir.isDirectory()) { File[] f = dir.listFiles(); boolean hasJavaFile = false; for (int i = 0; i < f.length; ++i) { String thisPrefix = prefix.equals("") ? f[i].getName() : prefix + "." + f[i].getName(); addPackageIfDirectoryWithJavaFiles(thisPrefix, f[i], packages); if (!hasJavaFile) hasJavaFile = f[i].getName().endsWith(".java"); } if (prefix.equals("")) log("Root -- can't use in javadoc when dealing with packages", Project.MSG_VERBOSE); else { if (hasJavaFile) { log(prefix + " has java files, and therefore is added.", Project.MSG_VERBOSE); packages.add(prefix); } else { log(prefix + " has no java files.", Project.MSG_VERBOSE); } } } else { log(dir + " not a directory", Project.MSG_VERBOSE); } } public void execute() { log("Package file : " + packageList, Project.MSG_VERBOSE); if (packageList == null) { throw new BuildException("No packageList specified"); } log("path = " + sourcePath, Project.MSG_VERBOSE); if (sourcePath == null) { throw new BuildException("No sourcepath specified"); } File f = new File(packageList); if (f.exists()) try { boolean b = f.delete(); if (!b) throw new BuildException("Unable to delete old packageList : " + f); } catch (BuildException b){ throw b; } catch (Exception e) { throw new BuildException("Unable to delete old packageList : " + f); } Set packages = new TreeSet(); String[] roots = Path.translatePath(project, sourcePath.toString()); for (int i = 0; i < roots.length; ++i) { log("Pathroot " + i + " : " + roots[i], Project.MSG_VERBOSE); File root = new File(roots[i]); addPackageIfDirectoryWithJavaFiles("", root, packages); } PrintWriter pw; try { pw = new PrintWriter(new FileWriter(f)); Iterator i = packages.iterator(); while (i.hasNext()) { String s = (String) i.next(); pw.println(s); log("Adding package " + s); } pw.flush(); pw.close(); } catch (BuildException b) { throw b; } catch (Exception e) { throw new BuildException("Unable to write packageList : " + f, e); } } }