Speed โ€‹โ€‹using #parse where the file does not exist

we use Velocity to create templates for our configuration files for several different environments, and this process works very well, but I have a quick question related to parsing a file that does not exist.

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

Thus, in the example, the default.user.properties file may not legally exist, and if it will not process the rest of the file.

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

I know that one solution is to make sure that the file is always there, but it would be nice if I didnโ€™t have to.

Thanks in advance.

+4
source share
3 answers

Just did it with spring and speed:

I am having trouble getting speed to pick up an event handler, at the end, specifying it in the servlet xml file:

 <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> <property name="resourceLoaderPath" value="WEB-INF/templates"/> <property name="velocityPropertiesMap"> <map> <entry key="eventhandler.include.class"><value>com.velocity.events.OptionalIncludeEventHandler</value></entry> </map> </property> </bean> 

It just does not agree that I put it in the properties file - it will create an instance of the class, but not register it as an event listener. very upset.

The class itself is simple, a rather egregious break with the existing speed class "org.apache.velocity.app.event.implementIncludeNotFound". The existing speed implementation checks for the existence of the file, and if not, returns a custom alternative (default: notfound.vm).

The mine is exactly the same, except that it returns null if the file does not exist, forcing the analyzer to skip this include / parse directive:

 public class OptionalIncludeEventHandler implements IncludeEventHandler, RuntimeServicesAware { private RuntimeServices rs; @Override public void setRuntimeServices(RuntimeServices rs) { this.rs = rs; } @Override public String includeEvent(String includeResourcePath, String currentResourcePath, String directiveName) { return rs.getLoaderNameForResource(includeResourcePath) != null ? includeResourcePath : null; } } 

It works like a charm.

Hope this is helpful.

+3
source

A new type of event handler is "IncludeEventHandler". This allows the developer to define a class (that implements IncludeEventHandler) that will be called each time #parse or #include is evaluated. The goal of your event handler is to check if a template exists and set an error flag for the calling code, if not. Check the documentation for more information, although I have not tested it myself.

+1
source

The solution I used was to create a utility to check for the existence of a template, i.e.

 public synchronized boolean templateExists(String templateFilename) { Boolean templateExists = this.templateExistsCache.get(templateFilename); if (templateExists != null) { return templateExists; } String absoluteFilename = this.request.getSession().getServletContext().getRealPath( "/WEB-INF/templates/" + templateFilename); File templateFile = new File(absoluteFilename); templateExists = templateFile.exists(); this.templateExistsCache.put(templateFilename, templateExists); return templateExists; } private Map<String, Boolean> templateExistsCache = new HashMap<String, Boolean>(); 

of

https://github.com/okohll/agileBase/blob/master/gtpb_server/src/com/gtwm/pb/model/manageData/ViewTools.java

+1
source

All Articles