|
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; |
|
} |
|
} |
|
|
|
} |