User guide - Model creation

Model creation

Model creation step consist to :

  • prepare JAVA context for the RTF model.
  • generate the XML file (*.fields.xml) which will be used when RTFtemplate will be integrated in your application and which can be used for design RTF model.
  • test result of merge of RTF model and JAVA context.

To do that, RTFtemplate give you an abstract class net.sourceforge.rtf.usecases.AbstractRTFUseCase that you can implement. This class is able to :

  • test quickly result of merge of RTF model and JAVA context.
  • generate the XML file (*.fields.xml) by using your JAVA context. This file contains the whole of fields available for the RTF model source. It depends on your JAVA context.

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.

AbstractRTFUseCase implementation

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.

AbstractRTFUseCase class

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 :

  • putContext : put your context in this method with WHOLE JAVA objects which must be used in the RTf model. It's very important to instanciate Collection and complex objects (objects graph). For the collection, you must add at least ONE JAVA object, in order to RTFTemplate generate loop macro (#foreach for Velocity, #list for Freemarker,...), generate correctly XML file (*.fields.xml).

Once you have implemented those methods, you can use methods :

  • run : to launch the test of your RTF model. This method wait the name of the RTF model source to use. This file is required. Even if you have not start designing your RTF model, you must create a blank RTF file to launch the test. Switch configuration (see below), this method will generate RTF file transformed (with macros of template engine), XML fields available (*.fields.xml),...
  • getXMLFields : to get XML fields available (*.fields.xml) for your model. XML String returned can be used to design your RTF model.
  • saveXmlFields : to save XML fields available (*.fields.xml) of your model into a file.
  • saveTransformedDocument : set true, if you want generate RTF model transformed (which contains macro of selected template engine). By default, AbstractRTFUseCase don't generate this file.
  • setGroupByPerPageBreak : to generate every groupByPerPageBreak√®me of item of the first list detected by RTFTemplate, a page break.
  • setApplicationContext : set the RTFTemplate Spring configuration tu use.

Implementation sample

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.

XML fields available

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 :

  • description : this element is only used by macro MS Word to display description of model.
  • bookmark : describes the pattern to use to name the start/end loop bookmark.
    • type : START_LOOP to identify start loop , END_LOOP to identify end loop.
    • name : pattern to use for the name of the bookmark. {i} must be used into the pattern. Indead bookmark have a unique name in the RTF model.
    • description : is only used by macro MS Word to display description of selected bookmark.
  • mergefield : describe the field in the model :
    • list : true if field is comming from a list (ex : JAVA object which are stored into java.util.List) and false otherwise.
    • listInfo : which is used by RTFTemplate to indentify list when object is complex (object graph).
    • name : name of field which use this syntax.
    • description : is only used by macro MS Word to display description of selected field.