Example of mail merge (Dependency Example) generating different files using RTFTemplate library.
References:-
http://sourceforge.net/projects/rtftemplate/
http://rtftemplate.sourceforge.net/
- Add required JARS in class path.
Required JARS:
rtftemplate-1.0.1-b14.jar
commons-collections-3.2.1.jar
spring-beans-3.2.2.RELEASE.jar
spring-context-3.2.2.RELEASE.jar
spring-context-support-3.2.2.RELEASE.jar
spring-core-3.2.2.RELEASE.jar
spring-expression-3.2.2.RELEASE.jar
spring-jdbc-3.2.2.RELEASE.jar
spring-orm-3.2.2.RELEASE.jar
spring-tx-3.2.2.RELEASE.jar
xml-apis-1.0.b2.jar
commons-logging-1.1.1.jar
freemarker.jar
commons-digester-2.1.jar
org.apache.commons.beanutils.jar
velocity-1.7.jar
commons-lang.jar
- Download the sample files (Project_www.liveinNJ.com.rtf, Project_www.liveinSF.com.rtf, Project_www.scart.com.rtf, Project_www.workinNJ.com.rtf, Project_www.workinSF.com.rtf, Project_www.workwithme.com.rtf) and Template file (RTF_Websites.rtf) in rtf format.
- Add the required classes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.rtf.impl; | |
public class Dependency { | |
private String artifactID; | |
private String type; | |
private String version; | |
private String url; | |
public Dependency(String artifactID, String type, String version, String url) { | |
this.artifactID = artifactID; | |
this.type = type; | |
this.version = version; | |
this.url = url; | |
} | |
public String getArtifactID() { | |
return artifactID; | |
} | |
public void setArtifactID(String artifactID) { | |
this.artifactID = artifactID; | |
} | |
public String getType() { | |
return type; | |
} | |
public void setType(String type) { | |
this.type = type; | |
} | |
public String getUrl() { | |
return url; | |
} | |
public void setUrl(String url) { | |
this.url = url; | |
} | |
public String getVersion() { | |
return version; | |
} | |
public void setVersion(String version) { | |
this.version = version; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.rtf.impl; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
public class Developer { | |
private String name; | |
private String email; | |
private Collection<Role> roles; | |
private Manager manager; | |
public Developer(String name, String email) { | |
this.name = name; | |
this.email = email; | |
roles = new ArrayList<Role>(); | |
} | |
public String getEmail() { | |
return email; | |
} | |
public void setEmail(String email) { | |
this.email = email; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public Collection<Role> getRoles() { | |
return roles; | |
} | |
public void setRoles(Collection<Role> roles) { | |
this.roles = roles; | |
} | |
public void addRole(Role role) { | |
roles.add(role); | |
} | |
public void addRole(String roleName) { | |
addRole(new Role(roleName)); | |
} | |
public Manager getManager() { | |
return manager; | |
} | |
public void setManager(Manager manager) { | |
this.manager = manager; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.rtf.impl; | |
public class DeveloperGroup { | |
private Developer developer1; | |
private Developer developer2; | |
private Developer developer3; | |
public DeveloperGroup(Developer developer1, Developer developer2, | |
Developer developer3) { | |
this.developer1 = developer1; | |
this.developer2 = developer2; | |
this.developer3 = developer3; | |
} | |
public Developer getDeveloper1() { | |
return developer1; | |
} | |
public void setDeveloper1(Developer developer1) { | |
this.developer1 = developer1; | |
} | |
public Developer getDeveloper2() { | |
return developer2; | |
} | |
public void setDeveloper2(Developer developer2) { | |
this.developer2 = developer2; | |
} | |
public Developer getDeveloper3() { | |
return developer3; | |
} | |
public void setDeveloper3(Developer developer3) { | |
this.developer3 = developer3; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.rtf.impl; | |
import java.io.File; | |
import java.util.ArrayList; | |
import java.util.List; | |
import net.sourceforge.rtf.RTFTemplate; | |
import net.sourceforge.rtf.helper.RTFTemplateBuilder; | |
/** | |
* This class is to generate a multiple file with there different addresses | |
* using RTF Template. | |
* | |
* @author Suraj Bhambhani | |
* | |
*/ | |
public class GenerateMultipleRTFFiles { | |
@SuppressWarnings("unchecked") | |
public static void main(String[] args) throws Exception { | |
// Source of file to be generated. | |
String rtfSource = "src/RTF_Websites.rtf"; | |
// 1. Get default RTFtemplateBuilder | |
RTFTemplateBuilder builder = RTFTemplateBuilder.newRTFTemplateBuilder(); | |
// 2. Get RTFtemplate with default Implementation of template engine | |
RTFTemplate rtfTemplate = builder.newRTFTemplate(); | |
// 3. Set the RTF model source | |
rtfTemplate.setTemplate(new File(rtfSource)); | |
// List of Projects | |
List<Project> projects = new ArrayList<Project>(); | |
// Project with one website, website2 exists. | |
Project project1 = new Project("Be Open Source", "Suraj Bhambhani", | |
"OutOfMemory", "", "www.scart.com"); | |
// Project with two website. | |
Project project2 = new Project("Be Open Source", "Suraj Bhambhani", | |
"OutOfMemory", "www.liveinSF.com", "www.liveinNJ.com"); | |
// Project with one website, website1 exists. | |
Project project3 = new Project("Be Open Source", "Suraj Bhambhani", | |
"OutOfMemory", "www.workwithme.com", ""); | |
// Project with one website, website1 exists. | |
Project project4 = new Project("Be Open Source", "Suraj Bhambhani", | |
"OutOfMemory", "www.workinSF.com", "www.workinNJ.com"); | |
projects.add(project1); | |
projects.add(project2); | |
projects.add(project3); | |
projects.add(project4); | |
rtfTemplate.put("header_developer_name", "Name"); | |
rtfTemplate.put("header_developer_email", "Email"); | |
rtfTemplate.put("header_developer_roles", "Roles"); | |
// Dependencies | |
List<Dependency> dependencies = new ArrayList<Dependency>(); | |
Dependency dependency = new Dependency("commons-collection", "jar", | |
"1.0", "http://jakarta.apache.org/commons/collection/"); | |
dependencies.add(dependency); | |
rtfTemplate.put("dependencies", dependencies); | |
// List of developers. | |
List<Developer> developers = new ArrayList<Developer>(); | |
Developer developer; | |
developer = new Developer("Will Glass-Husain", "wglass@apache.org"); | |
developer.addRole("Java Developer"); | |
developer.addRole("Manager"); | |
rtfTemplate.put("developers", developers); | |
developers.add(developer); | |
developer = new Developer("Ma-Husain", "wglass@apache.org"); | |
developer.addRole("Java Developer"); | |
developer.addRole("Manager"); | |
developers.add(developer); | |
// Iterating over projects and generating files accordingly. | |
for (Project projectIterate : projects) { | |
// When both wesite exists. | |
if (!projectIterate.getWebsite1().equals("") | |
&& !projectIterate.getWebsite2().equals("")) { | |
withTwoWebsites(rtfTemplate, projectIterate); | |
withTwoWebsites(rtfTemplate, projectIterate); | |
} | |
// When Website2 exists and website1 not. | |
if (projectIterate.getWebsite1().equals("") | |
&& !projectIterate.getWebsite2().equals("")) { | |
withWebsite2(rtfTemplate, projectIterate); | |
} | |
// When Website1 exists and website2 not. | |
if (!projectIterate.getWebsite1().equals("") | |
&& projectIterate.getWebsite2().equals("")) { | |
withWebsite1(rtfTemplate, projectIterate); | |
} | |
System.out.println(projectIterate.getWebsite1()); | |
System.out.println(projectIterate.getWebsite2()); | |
} | |
} | |
private static void withWebsite1(RTFTemplate rtfTemplate, | |
Project projectIterate) throws Exception { | |
String name; | |
name = projectIterate.getWebsite1(); | |
generateFile(rtfTemplate, projectIterate, name); | |
} | |
private static void withWebsite2(RTFTemplate rtfTemplate, | |
Project projectIterate) throws Exception { | |
String name; | |
projectIterate.setWebsite1(projectIterate.getWebsite2()); | |
name = projectIterate.getWebsite2(); | |
generateFile(rtfTemplate, projectIterate, name); | |
} | |
private static void generateFile(RTFTemplate rtfTemplate, | |
Project projectIterate, String name) throws Exception { | |
// 4. Put the context. | |
rtfTemplate.put("project", projectIterate); | |
String rtfTarget = "Project_" + name + ".rtf"; | |
// 5. Merge the RTF source model and the context | |
rtfTemplate.merge(rtfTarget); | |
} | |
private static void withTwoWebsites(RTFTemplate rtfTemplate, | |
Project projectIterate) throws Exception { | |
String name; | |
boolean websiteCheck = true; | |
int websitBreak = 0; | |
// When two website exist. | |
while (true) { | |
name = projectIterate.getWebsite1(); | |
if (websiteCheck == false) { | |
projectIterate.setWebsite1(projectIterate.getWebsite2()); | |
name = projectIterate.getWebsite2(); | |
} | |
generateFile(rtfTemplate, projectIterate, name); | |
websitBreak++; | |
websiteCheck = false; | |
if (websitBreak == 2) | |
break; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.rtf.impl; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class Manager { | |
private List<Project> projects; | |
public Manager(String name) { | |
this.name = name; | |
this.projects = new ArrayList<Project>(); | |
} | |
private String name; | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public void addProject(Project project) { | |
projects.add(project); | |
} | |
public List<Project> getProjects() { | |
return projects; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.rtf.impl; | |
import java.io.InputStream; | |
import java.util.List; | |
public class Project { | |
private String name; | |
private InputStream logo = null; | |
private String clientName; | |
private String error; | |
public String getError() { | |
return error; | |
} | |
public void setError(String error) { | |
this.error = error; | |
} | |
private String website1; | |
private String website2; | |
public String getWebsite2() { | |
return website2; | |
} | |
public void setWebsite2(String website1) { | |
this.website2 = website1; | |
} | |
public String getWebsite1() { | |
return website1; | |
} | |
public void setWebsite1(String website) { | |
this.website1 = website; | |
} | |
public String getClientName() { | |
return clientName; | |
} | |
public void setClientName(String clientName) { | |
this.clientName = clientName; | |
} | |
public Project(String name, String clientName, String error, | |
String website, String website1) { | |
this.name = name; | |
this.clientName = clientName; | |
this.error = error; | |
this.website1 = website; | |
this.website2 = website1; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public void setLogo(InputStream logo) { | |
this.logo = logo; | |
} | |
public InputStream getLogo() { | |
return logo; | |
} | |
private List<Project> group; | |
public List<Project> getGroup() { | |
return group; | |
} | |
public void addProject(Project project) { | |
group.add(project); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.rtf.impl; | |
public class Role { | |
private String name; | |
public Role(String name) { | |
this.name = name; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" | |
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" | |
xmlns:mvc="http://www.springframework.org/schema/mvc" | |
xsi:schemaLocation="http://www.springframework.org/schema/beans | |
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd | |
http://www.springframework.org/schema/context | |
http://www.springframework.org/schema/context/spring-context-3.0.xsd | |
http://www.springframework.org/schema/tx | |
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd | |
http://www.springframework.org/schema/mvc | |
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> | |
<!-- ********************************************************************** | |
********************* RTFTEMPLTE IMPLEMENTATION ********************* | |
********************************************************************** --> | |
<!-- Defautlt RTFTemplate implementation with freemarker template engine --> | |
<bean id="ftlRTFTemplate" | |
class="net.sourceforge.rtf.RTFTemplate" | |
singleton="false" > | |
<property name="parser" ref="defaultRTFParser" /> | |
<property name="transformer" ref="ftlTransformer" /> | |
<property name="templateEngine" ref="ftl" /> | |
</bean> | |
<!-- Defautlt RTFTemplate implementation with velocity template engine --> | |
<bean id="vmRTFTemplate" | |
class="net.sourceforge.rtf.RTFTemplate" | |
singleton="false" > | |
<property name="parser" ref="defaultRTFParser" /> | |
<property name="transformer" ref="vmTransformer" /> | |
<property name="templateEngine" ref="vm" /> | |
</bean> | |
<!-- ********************************************************************** | |
********************* RTFDOCUMENT PARSER ********************* | |
********************************************************************** --> | |
<!-- Defautlt RTFDocument Parser --> | |
<bean id="defaultRTFParser" | |
class="net.sourceforge.rtf.handler.RTFDocumentHandler" | |
singleton="true" > | |
</bean> | |
<!-- ********************************************************************** | |
********************* FREEMARKER TEMPLATE ENGINE ********************* | |
********************************************************************** --> | |
<!-- Freemarker template engine --> | |
<bean id="ftl" | |
class="net.sourceforge.rtf.template.freemarker.FreemarkerTemplateEngineImpl" | |
singleton="false" > | |
<property name="freemarkerConfiguration" ref="ftlConfiguration" /> | |
</bean> | |
<!-- Freemarker Configuration --> | |
<bean id="ftlConfiguration" | |
class="freemarker.template.Configuration" | |
singleton="true" > | |
</bean> | |
<!-- Freemarker RTF Document Transformer --> | |
<bean id="ftlTransformer" | |
class="net.sourceforge.rtf.template.freemarker.RTFFreemarkerTransformerImpl" | |
singleton="true" > | |
</bean> | |
<!-- ********************************************************************** | |
********************* VELOCITY TEMPLATE ENGINE ********************* | |
********************************************************************** --> | |
<bean id="vm" | |
class="net.sourceforge.rtf.template.velocity.VelocityTemplateEngineImpl" | |
singleton="false" > | |
<property name="velocityEngine" ref="vmEngine" /> | |
</bean> | |
<!-- VelocityEngine Configuration --> | |
<bean id="vmEngine" | |
class="org.apache.velocity.app.VelocityEngine" | |
singleton="true" > | |
</bean> | |
<!-- Velocity RTF Document Transformer --> | |
<bean id="vmTransformer" | |
class="net.sourceforge.rtf.template.velocity.RTFVelocityTransformerImpl" | |
singleton="true" > | |
</bean> | |
</beans> |
References:-
http://sourceforge.net/projects/rtftemplate/
http://rtftemplate.sourceforge.net/