views:

92

answers:

3

Hi,

Does anyone know of a Java preprocessor library? I'm searching for something like m4. I could just invoke m4 from Java and capture the result, but I don't want to depend on m4 being installed in the systems where the application runs. A standalone Java API that is similar to m4 would be great.

Thanks.

edit: I think I didn't explain very clearly what I am doing. The thing is that I am writing a Java application that processes source files written in another (custom) language. Basically it's a translator between two languages, and the source language supports macros.

A: 

Depending on what you need to do, a simple "find / replace" filter is sometimes enough. In my case, the problem is to support multiple versions of Java (1.4, 5, 6) using the same source code. Within the source code, I use remarks to enable / disable blocks, as in:

import java.sql.Clob;
//## Java 1.6 begin ##
import java.sql.NClob;
//## Java 1.6 end ##

To disable the Java 6 block, replace:

  • "//## Java 1.6 begin ##" with "/*## Java 1.6 begin ##"
  • "//## Java 1.6 end ##" with "//## Java 1.6 end ##*/".

So it becomes a block comment.

I wrote a Java tool to do that. It's just one class, without dependencies: http://code.google.com/p/h2database/source/browse/trunk/h2/src/tools/org/h2/build/code/SwitchSource.java - but you can use any kind of text find / replace tool.

The advantage of this approach is: it's still normal source code you can edit and compile without having to run the pre-compiler. You only need a tool to "switch" the code.

Thomas Mueller
Wouldnt creatng a strategy with at least 2 implementations make more sense and be more correct ?
mP
I'm not sure I understood... what do you mean with 'strategy'? Do you want another implementation for SwitchSource.java? Why should I write another one?
Thomas Mueller
I see your solution, but my situation is more than just enabling/disabling code sequences. I have to do macro replacement for macros with parameters, etc.
Gabi
A: 

Annotation Processing may be useful.

emory
Thanks for the pointer, but my source language is not Java unfortunately. It's a custom language and I wrote an ANTLR based parser for it, but the support for macros is kind of hacked in and I would like to rewrite it in a nice way.
Gabi
A: 

Velocity perhaps?

Maurice Perry