Model creation step consist to :
To do that, RTFtemplate give you an abstract class net.sourceforge.rtf.usecases.AbstractRTFUseCase that you can implement. This class is able to :
ATTENTION, it's not adwisable to use this class implementation in your application. See section integrate RTFTemplate for more informations.
RTFTemplate distribution give you several samples with RTFTemplate using (manage image, manage page break,...). For more informations, click here.
Before implementing this class, you must create your JAVA context, on other words you must create your JAVA classes POJO (Plain Old java Object) and collection of POJO. Your classes define getters/setters on simple type (java.lang.String,...) or on another POJO. Context created will be merged with your RTF model source.
Here the JAVA code of this class and principal methods that you must/can implement : (It exists another methods to configure RTFTemplate.)
public abstract class AbstractRTFUseCase { /** * Run RTFTemplate for merging rtfSource with the context * putted with the method putContext which be must implement. * After execution of this method, files * rtfSource + ".<rtfTemplateImpl>.rtf" (RTF template implementation (vmRTFtemplate,...) and * rtfSource + ".out.rtf" (RTF final with values of the context) * will be generate. * @param rtfSource RTF source model. * @throws Exception */throws Exception */ protected final void run(String rtfSource) throws Exception { .... } /** * This method must be implement by class * wich manage your RTF model. * Put the context of your model * (eg : context("date", new Date()); ) * @param context IContext */ protected abstract void putContext(IContext context); /** * Return String XML Mergefields used in your context * and Bookmarks (for start and end loop) * @return */ public String getXMLFields() { .... } /** * Save XML fields available into file. * If force parameter is false, the file is * updated with new context (by keeping just * description) otherwise the file is * crushed with new context. * @param filename * @throws Exception */ protected void saveXmlFields(String filename, boolean force) throws Exception { .... } /** * set true if RTF with (velocity, freemarker,... macro) file must be generated * and false otherwise. * @param saveTransformedDocument */ public void saveTransformedDocument(boolean saveTransformedDocument) .... } /** * This value allow to group by content when there is PageBreak * in order to group by content. * @param groupByPerPageBreak */ protected void setGroupByPerPageBreak(int groupByPerPageBreak) { ... } }
Methods which must be implement are :
Once you have implemented those methods, you can use methods :
Here JAVA code sample of AbstractRTFUseCase implementation :
public class RTFProjectTest extends AbstractUseCase { protected void putContext(IContext context) { /** * Context of simply POJO */ context.put("date", new Date()); context.put("project", new Project("Jakarta Velocity")); /** * Context of list of POJO */ List developers = new ArrayList(); Developer developer = new Developer("Will Glass-Husain", "wglass@apache.org"); developer.addRole("Java Developer"); developer.addRole("Release Manager"); .... context.put("developers", developers ); } public static void main( String[] args ) throws Exception { RTFProjectTest usecase = new RTFProjectTest(); usecase.run("usecases/project/project_model.rtf"); usecase.saveRTFVelocity(true); // Save RTF file with velocity macro // Display the XML fields System.out.println(usecase.getXMLFields()); // Save XML fields into project_model.fields.xml file with update description usecase.saveTransformedDocument("usecases/project/project_model.fields.xml" ,false); } }
Once test was ended, XML file which contains XML fileds available (*.fields.xml) project_model.fields.xml will be generate.
This XML file can be used to design RTF model. ATTENTION this file must ve ended with fields.xml in order to that you can use with MS Word.
Name of merge fields MERGEFIELD et hyperlink field HYPERLINK must respect a syntax. Those names are stored into the XML file (*.fields.xml).
<?xml version="1.0" encoding="ISO-8859-1"?> <fields> <description><![CDATA[]]></description> <!-- Bookmark Pattern --> <bookmark> <type>START_LOOP</type> <name>START_LOOP_{i}</name> <description><![CDATA[]]></description> </bookmark> <bookmark> <type>END_LOOP</type> <name>END_LOOP_{i}</name> <description><![CDATA[]]></description> </bookmark> ... <mergefield> <list>true</list> <listInfo>$[developers].Email</listInfo> <name>$developers.Email</name> <description><![CDATA[]]></description> </mergefield> <mergefield> <list>true</list> <listInfo>$[developers].Name</listInfo> <name>$developers.Name</name> <description><![CDATA[]]></description> </mergefield> .... <mergefield> <list>false</list> <listInfo>$project.Name</listInfo> <name>$project.Name</name> <description><![CDATA[]]></description> </mergefield> <mergefield> <list>false</list> <listInfo>$date</listInfo> <name>$date</name> <description><![CDATA[]]></description> </mergefield> .... </fields>
This XML file contains XML elements :