Separating Identifiers
Apache Velocity is a free, simple and powerful template engine written in 100% pure Java.
This requires the libraries velocity-1.7.jar, oro-2.0.8.jar, commons-lang-2.4.jar,
commons-collections-3.2.1.jar, commons-logging-1.1.jar, log4j-1.2.12.jar to be in classpath.
The following example shows separating identifiers in Velocity.
package com.bethecoder.tutorials.velocity.common;
public class MethodBean {
public String toUpperCase ( String str ) {
return str.toUpperCase () ;
}
public double pow ( double a, double b ) {
return Math.pow ( a, b ) ;
}
}
package com.bethecoder.tutorials.velocity.tests;
import java.io.StringWriter;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import com.bethecoder.tutorials.velocity.common.MethodBean;
public class SeparatingIdentifiersTest {
/**
* @param args
*/
public static void main ( String [] args ) {
/**
* Initialize engine and get template
*/
VelocityEngine ve = new VelocityEngine () ;
ve.setProperty ( RuntimeConstants.RESOURCE_LOADER, "classpath" ) ;
ve.setProperty ( "classpath.resource.loader.class" , ClasspathResourceLoader. class .getName ()) ;
Template template = ve.getTemplate ( "sep.vm" ) ;
/**
* Prepare context data
*/
VelocityContext context = new VelocityContext () ;
context.put ( "bean" , new MethodBean ()) ;
/**
* Merge data and template
*/
StringWriter swOut = new StringWriter () ;
template.merge ( context, swOut ) ;
System.out.println ( swOut ) ;
}
}
It gives the following output,
Phone bill is $4000
Phone bill is 4000USD
Phone bill is 4000INR
Phone bill is 4000INR