ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rwald...@us.britannica.com
Subject [CONTRIB] Regular Expression Replace Task
Date Thu, 04 Jan 2001 23:05:49 GMT
I sent a version of this out on Monday, my apologies if its already in some
committer's queue, but I was afraid it might have been missed in the holiday
rush and absent any sort of [PATCH] style subject-line. (By the way, what's
the convention for marking a wholly new contribution? I've seen [PATCH] and
[CONTRIB] on a couple of lists, but I wasn't able to find a canonical form
in the site documentation.  Is it a [PATCH] even if it doesn't change any
currently committed files?)

Here's a new task that performs a regular expression substitution inline on
a file or a set of files. The substitution pattern can contain references to
subexpressions within the find pattern, which makes it possible to use
fragments of the original text in the new text. 

The syntax and implementation borrows from the replace task, for example,
given the following file:

public class BuildInfo {
   public static final String TIMESTAMP =
/*[[TIMESTAMP[[*/"20010103@1240"/*]]TIMESTAMP]]*/;
   public static final String VERSION =
/*[[VERSION[[*/"3.1.4"/*]]VERSION]]*/;

   public static void main(String[] args) {
      System.out.println("Compiled on:\t" + TIMESTAMP);
      System.out.println("Version:\t" + VERSION);
   }
}

The tasks:

   <rereplace file="BuildInfo.java" 
              find="/\*\[\[TIMESTAMP\[\[\*/.*/\*\]\]TIMESTAMP\]\]\*/"
 
replaceWith="/*[[TIMESTAMP[[*/&quot;${DSTAMP}@${TSTAMP}&quot;/*]]TIMESTAMP]]
*/"/>
   <rereplace file="BuildInfo.java" 
              find="/\*\[\[VERSION\[\[\*/.*/\*\]\]VERSION\]\]\*/"
 
replaceWith="/*[[VERSION[[*/&quot;${version}&quot;/*]]VERSION]]*/"/>

will update BuildInfo.java file with the associated properties.  This is a
superset of <replace> functionality, and I found it more useful than using
filter tokens, since the source file is compile-able as is.  (This also
helps to work around limitations in ClearCase, which doesn't support
RCS-style $Id, etc.)

This implementation relies upon the jakarta-oro regular expression package
(see http://jakarta.apache.org/oro), although it is relatively simply to
modify the code to support alternative regular expression packages.

Documentation and some examples can be found in the attached archive.

I've seen a few requests for this functionality on the list, and I've found
this task to be useful, so I thought others might also.  In theory, this
could replace the replace task altogether, but I've packaged it as optional
since it depends upon the oro library.

(The archive also includes a support class that implements an arbitrary
nested element containing text, which may be independently useful.)

Enjoy,

 Rod Waldhoff <rwaldhof@us.britannica.com>


Mime
View raw message