views:

329

answers:

1

Hi, we use Velocity to template our configuration files for several different environments and this process works really well but I have a quick question relating parsing a file that doesn't exist.

My question is: How do you check if a file exists before parsing it?

So in the example the file default.user.properties might legitimately not exist and if it doesn't the rest of the file doesn't get parsed.

#parse("./default.user.properties")

I know one solution is to make sure the file is always there but it would be nice if I didn't have to.

Thanks in advance.

+1  A: 

A new type of event handler "IncludeEventHandler". This allows you, the developer, to define a class (implementing IncludeEventHandler) which will be called every time a #parse or #include is evaluated. The purpose of your event handler would be to check if the template exists and set an error flag for the calling code if not. Check the documentation for more information though I haven't tested it myself

GustlyWind
Thanks for the answer the org.apache.velocity.app.event.IncludeEventHandler looks interesting although a bit more effort than I was hoping for, :)It is also complicated as we merge/template the files all from ant so it is not clear how I will plug in the new implementation of IncludeEventHandler. But I will follow this up and post my findings.
Shawn