Date: 2004-05-03T08:31:50 Editor: JanMat�rne <[EMAIL PROTECTED]> Wiki: Ant Wiki Page: AntOddities URL: http://wiki.apache.org/ant/AntOddities
Using <replaceregexp> for implementing a PreProcessor Change Log: ------------------------------------------------------------------------------ @@ -304,3 +304,99 @@ }}} ''Marc Persuy'' + + +== Implementing a PreProcessor == +On [http://issues.apache.org/bugzilla/show_bug.cgi?id=28738 Bug-28738] is a question about preprocessor. You can do that without any external tasks (but I think the external tasks are more comfortable than this way :-) + +The example java file is the common known HelloWorld: +{{{ +public class HelloWorld { + + public static void main(String arg[]){ + //@debug start + log("debug:let say this is a debug code or logging"); + //@debug end + + System.out.println("HELLO WORLD"); + } + + //@debug start + private static void log(String msg) { + System.out.println("LOG: " + msg); + } + //@debug end +} +}}} + +We can use the <replaceregexp> for deleting the code between debug-start and -end statements: +{{{ + <replaceregexp match="//@debug start.*?//@debug end" replace="" flags="gs"> +}}} + +Important is the question mark in the match clause (.*?), so we�ve got minimal pattern matching. Otherwise all +between the first debug-start mark and the very last debug-end will be selected. And therefore we will loose important code segements :-) The flag "s" is responsible that we will get the whole file at once and the "g" that we catch all debug-statements. + +After running that command we�ll get that: +{{{ +public class HelloWorld { + + public static void main(String arg[]){ + + + System.out.println("HELLO WORLD"); + } + + +} +}}} +Maybe you will run a code beautyfier on that so you�ll delete the empty lines ... But you can see that the expected code is generated. And the class file is beautified :-) + +Here the buildfile for the example. Place that in a project root directory and the original java source in "src" subdirectory. +{{{ +<?xml version="1.0" encoding="ISO-8859-1"?> +<project> + <echo> ************* Debug Mode ************* </echo> + <delete dir="classes"/> + <mkdir dir="classes"/> + <javac srcdir="src" destdir="classes"/> + <java classname="HelloWorld" classpath="classes"/> + + <echo> ************* LIVE Mode ************* </echo> + <delete dir="classes"/> + <delete dir="src-live"/> + <mkdir dir="classes"/> + <copy todir="src-live"> + <fileset dir="src"/> + </copy> + + <!-- important is the .*? because .* matches too much --> + <replaceregexp match="//@debug start.*?//@debug end" replace="" flags="gs"> + <fileset dir="src-live"/> + </replaceregexp> + + <javac srcdir="src-live" destdir="classes"/> + <java classname="HelloWorld" classpath="classes"/> +</project> +}}} + +The output would be: +{{{ +Buildfile: build.xml + [echo] ************* Debug Mode ************* + [delete] Deleting directory C:\tmp\anttests\preprocessor\classes + [mkdir] Created dir: C:\tmp\anttests\preprocessor\classes + [javac] Compiling 1 source file to C:\tmp\anttests\preprocessor\classes + [java] LOG: debug:let say this is a debug code or logging + [java] HELLO WORLD + [echo] ************* LIVE Mode ************* + [delete] Deleting directory C:\tmp\anttests\preprocessor\classes + [delete] Deleting directory C:\tmp\anttests\preprocessor\src-live + [mkdir] Created dir: C:\tmp\anttests\preprocessor\classes + [copy] Copying 1 file to C:\tmp\anttests\preprocessor\src-live + [javac] Compiling 1 source file to C:\tmp\anttests\preprocessor\classes + [java] HELLO WORLD + +BUILD SUCCESSFUL +Total time: 3 seconds +}}} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
