Class TemplateWriter


public class TemplateWriter extends TextWriter
Writes records to a text stream using FreeMarker template. See the Write an XML file example. See the FreeMarker Manual.
  • Constructor Details

    • TemplateWriter

      public TemplateWriter(File file, boolean append)
      Creates a writer for the file that wraps values with a TemplateObjectWrapper.
    • TemplateWriter

      public TemplateWriter(File file)
      Creates a writer that overwrites the file and wraps values with a TemplateObjectWrapper.
    • TemplateWriter

      public TemplateWriter(Writer writer)
      Creates a writer that wraps values with a TemplateObjectWrapper.
    • TemplateWriter

      public TemplateWriter(File file, boolean append, boolean useTemplateObjectWrapper)
      Creates a writer for the file; useTemplateObjectWrapper chooses whether values are wrapped with a TemplateObjectWrapper before templates see them.
    • TemplateWriter

      public TemplateWriter(Writer writer, boolean useTemplateObjectWrapper)
      Creates a writer; useTemplateObjectWrapper chooses whether values are wrapped with a TemplateObjectWrapper before templates see them.
  • Method Details

    • getData

      public Map<String,Object> getData()
      Returns the data model given to the templates, holding writer, record and the current record's fields by name; entries added here are visible to the templates too.
    • getConfiguration

      public freemarker.template.Configuration getConfiguration()
      Returns the FreeMarker configuration that loads the templates named in the String template setters.
    • isAlwaysShowFooter

      public boolean isAlwaysShowFooter()
      Indicates if the footer should be written, even if no records were written (defaults to true).
    • setAlwaysShowFooter

      public TemplateWriter setAlwaysShowFooter(boolean alwaysShowFooter)
      Indicates if the footer should be written, even if no records were written (defaults to true).
    • isAlwaysShowHeader

      public boolean isAlwaysShowHeader()
      Indicates if the header should be written, even if no records are written (defaults to true).
    • setAlwaysShowHeader

      public TemplateWriter setAlwaysShowHeader(boolean alwaysShowHeader)
      Indicates if the header should be written, even if no records are written (defaults to true).
    • isUseTemplateObjectWrapper

      public boolean isUseTemplateObjectWrapper()
      Indicates if values are wrapped with a TemplateObjectWrapper before templates see them; fixed by the constructor.
    • getHeaderTemplate

      public freemarker.template.Template getHeaderTemplate()
      Returns the template written before the records or null if there is none.
    • setHeaderTemplate

      public TemplateWriter setHeaderTemplate(String headerTemplate)
      Sets the header template to the template with this name, loaded through getConfiguration().
    • setHeaderTemplate

      public TemplateWriter setHeaderTemplate(freemarker.template.Template headerTemplate)
      Sets the template written before the records; only allowed before the writer is opened.
    • getFooterTemplate

      public freemarker.template.Template getFooterTemplate()
      Returns the template written after the records or null if there is none.
    • setFooterTemplate

      public TemplateWriter setFooterTemplate(String footerTemplate)
      Sets the footer template to the template with this name, loaded through getConfiguration().
    • setFooterTemplate

      public TemplateWriter setFooterTemplate(freemarker.template.Template footerTemplate)
      Sets the template written after the records; only allowed before the writer is opened.
    • getDetailTemplate

      public freemarker.template.Template getDetailTemplate()
      Returns the template written for each record.
    • setDetailTemplate

      public TemplateWriter setDetailTemplate(String detailTemplate)
      Sets the detail template to the template with this name, loaded through getConfiguration().
    • setDetailTemplate

      public TemplateWriter setDetailTemplate(freemarker.template.Template detailTemplate)
      Sets the template written for each record; only allowed before the writer is opened.
    • open

      public void open() throws DataException
      Description copied from class: DataEndpoint
      Makes this endpoint ready for reading or writing.
      Overrides:
      open in class AbstractWriter
      Throws:
      DataException
    • close

      public void close() throws DataException
      Description copied from class: DataEndpoint
      Indicates that this endpoint has finished reading or writing.
      Overrides:
      close in class TextWriter
      Throws:
      DataException
    • writeRecord

      protected void writeRecord(Record record) throws Throwable
      Description copied from class: AbstractWriter
      Writes one row; called first for the header row (see AbstractWriter.isHeaderRow()) when field names are written, then for each record.
      Specified by:
      writeRecord in class AbstractWriter
      Throws:
      Throwable
    • addExceptionProperties

      public DataException addExceptionProperties(DataException exception)
      Description copied from class: Endpoint
      Adds this endpoint's current state to a DataException. Since this method is called whenever an exception is thrown, subclasses should override it to add their specific information.
      Overrides:
      addExceptionProperties in class TextWriter