/* * Created on May 2, 2003 * */ package org.ninjasoft.ant; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Task; import java.util.*; import java.io.*; import java.util.regex.*; public class BuildStamp extends Task { private static final String PATTERN = "\\s*public\\s+static\\s+final\\s+String\\s+BUILD_DATE.*"; private static final String LINE = "public static final String BUILD_DATE = "; private String dir = null; private String stamp = null; private Pattern pattern; private boolean debug = false; /** * Run this task * @see org.apache.tools.ant.Task#execute() */ public void execute() throws BuildException { super.execute(); if (this.dir == null) throw new BuildException("dir attribute must be given"); if (this.stamp == null) throw new BuildException("stamp attribute must be given"); if (this.debug) super.log("Using RegEx \"" + PATTERN + "\""); pattern = Pattern.compile(PATTERN); if (this.debug) super.log("Starting in " + dir); Vector matches = findMatchingFiles(new File(dir), new Vector()); super.log("" + matches.size() + " file match(es) regex"); for (Iterator i = matches.iterator(); i.hasNext(); ) replace((File) i.next()); } private Vector findMatchingFiles(File f, Vector v) { // If this is a java file, scan it if (f.isFile() && f.getName().toLowerCase().endsWith(".java")) { if (this.debug) super.log("Checking " + f.getAbsolutePath()); try{ String line; LineNumberReader in = new LineNumberReader(new FileReader(f)); while ((line = in.readLine()) != null) { Matcher m = pattern.matcher(line); if (m.matches()) { if (this.debug) super.log("File " + f.getName() + " matches"); v.add(f); break; } } in.close(); }catch(Exception e){ super.log("Error reading " + f.getName() + ": " + e.getMessage()); } // If this is a directory, recuse to children }else if (f.isDirectory()) { if (this.debug) super.log("Traversing to " + f.getAbsolutePath()); File[] children = f.listFiles(); for (int i=0; i