Source File FiveExample. java
package nc.jonathan.velocity;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
/**
* <P>
* Fifth learning instance
* </P>
*
* <P> the passed parameter is an object, and the object operation in the template </P>
*
* Create on 2006-4-3 14:43:17
*
* @ Author Jonathan Q. Bo
* @ Version 1.0 valocity Study
*/
Public class fiveexample {
/**
* Test Method
*/
Public void test (){
Try {
/* Initialize the runtime engine and specify the template folder location for initialization */
Properties P = new properties ();
P. setproperty ("file. Resource. loader. Path", "VM ");
Velocity. INIT (P );
/* Create the context and put the data */
VelocityContext context = new VelocityContext ();
Context. put ("exampleObject", new ExampleObject ());
/* Output target of parsed data, subclass of java. io. Writer */
FileWriter w = new FileWriter ("gen/systthexamplegen. java ");
BufferedWriter bw = new BufferedWriter (w );
/* Parse */
// Velocity. mergeTemplate ("secondtemplate. vm", context, w );
Velocity. mergeTemplate ("effecthtemplate. vm", "gb2312", context, bw );
Bw. flush ();
Bw. close ();
System. Out. println ("#... successful! ");
} Catch (resourcenotfoundexception E1 ){
System. Out. println ("# The source file does not exist! ");
E1.printstacktrace ();
} Catch (parseerrorexception E2 ){
System. Out. println ("# file parsing error! ");
E2.printstacktrace ();
} Catch (methodinvocationexception E3 ){
System. Out. println ("# method call exception! ");
E3.printstacktrace ();
} Catch (exception E4 ){
System. Out. println ("# other errors! ");
E4.printstacktrace ();
}
}
public static void main(String[] args) {
FiveExample example = new FiveExample();
example.test();
}
}
Source File ExampleObject. java
package nc.jonathan.velocity;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* <P>
* Objects used in the template
* </P>
*
* Create on 2006-4-3 18:55:33
*
* @ Author Jonathan Q. Bo
* @ Version 1.0 valocity study
*/
public class ExampleObject{
private String name;
private List friends;
public ExampleObject(){
this.name = "jonathan";
friends = new ArrayList();
for(int i = 0; i < 8; i++){
friends.add(" friend" + i);
}
}
public String getAllFriends(){
String all = name + "'s friends : ";
for(Iterator itt = friends.iterator(); itt.hasNext(); ){
all += "," + (String)itt.next();
}
return all;
}
public String rename(String newName){
this.setName(newName);
return newName;
}
public String getNewName(){
String[] names = {"Jacy","Tom","Cat","Random"};
int random = (int)(Math.random()*10%3);
return names[random];
}
public List getFriends() {
return friends;
}
public void setFriends(List friends) {
this.friends = friends;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Template File
/**********************************/
test object property : ${exampleObject.name}
test object property : $exampleObject.name
test object property: $exampleObject.getName()
/***********************************/
test object method : ${exampleObject.getAllFriends()}
test object method : $exampleObject.getAllFriends()
/***********************************/
test object method : $exampleObject.rename($exampleObject.getNewName())
Output file
/**********************************/
test object property : jonathan
test object property : jonathan
test object property: jonathan
/***********************************/
test object method : jonathan's friends : , friend0, friend1, friend2, friend3, friend4, friend5, friend6, friend7
test object method : jonathan's friends : , friend0, friend1, friend2, friend3, friend4, friend5, friend6, friend7
/***********************************/
test object method : Jacy