Class DefaultTemplateEngine
java.lang.Object
java.util.Observable
com.nomagic.magicreport.engine.AbstractTemplateEngine
com.nomagic.magicreport.engine.velocity.DefaultTemplateEngine
- All Implemented Interfaces:
IExtensionTemplateEngine,com.nomagic.magicreport.engine.IRuntimeInstanceVelocityEngine,ITemplateEngine
- Direct Known Subclasses:
DOCXEngine,HTMLEngine,LaTeXEngine,PPTXEngine,RTFEngine,TextEngine,XLSXEngine,XMLEngine
@OpenApiAll
public class DefaultTemplateEngine
extends AbstractTemplateEngine
implements IExtensionTemplateEngine
Provide standard template engine for MagicReport. This engine performs directives translation during
pre-processing, invoke Velocity evaluate method, and perform post-processing.
- Since:
- Mar 13, 2008
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected org.apache.velocity.VelocityContextHold velocity context to abort engine.Fields inherited from class com.nomagic.magicreport.engine.AbstractTemplateEngine
context, isAborted, preProcessFile, processFile, properties -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidabort()Call to abort Velocity context.protected voidcheckFileNotFoundException(Exception e, File file) voidprotected StringcreateComment(String message) Return a formatted comment from given message.Creates a new and emptyTemplatefor generating report by this engine.createTemplate(File input, File output) Creates a newTemplateobject.createTemplate(File input, File output, boolean ignoreLocation) Creates a newTemplateobject.voiddestroy()Called by the application orTemplateEngineFactoryto inform this engine that it should destroy any resources that it has allocated.Returns the class loader for the engine.Returns a formatter that can be used to create the object layout, and formats of stateInsertionHandler.protected StringgetResolvedSectionName(String sectionName) protected voidhandleForPage(Template template, StringBuilder inProcessBuffer) Handle the #forpage directive.protected voidhandleForRow(Template template, StringBuilder inProcessBuffer) Handle the #forrow directive.protected voidhandleImport(Template template, StringBuilder inProcessBuffer) Handle the #include directive.protected voidhandleIncludeSection(Template template, StringBuilder inProcessBuffer) Handle the #includeSection directive.protected voidhandleSectionBegin(Template template, StringBuilder inProcessBuffer) Handle the #sectionBegin directive.protected booleanprotected intCount number of line from given content string.protected voidpostProcess(Template template) Perform a post-processing after the template has been evaluated.protected voidpreProcess(Template template) Perform a pre-processing before evaluate template.voidProcess a template.voidprocess(Template template, ITemplateEngine parentEngine) Process a template.protected TemplateExceptionprocessException(Template template, ParseErrorException e) Call this method to process aParseErrorException.protected TemplateExceptionprocessVelocityException(Template template, org.apache.velocity.exception.MethodInvocationException e) Call this method to process an Velocity exception and return in new format (ParseErrorException).protected TemplateExceptionprocessVelocityException(Template template, org.apache.velocity.exception.ParseErrorException e) Call this method to process an Velocity exception and return in new format (ParseErrorException).voidsetClassLoader(ClassLoader loader) Sets the class loader for this engine.voidsetFormatter(IFormatter formatter) Set the formatter to be used with this template engine.protected voidsetLatestLocation(File input, File output) Call this method to set the latest input and output location from giveninputandoutputfile.protected voidsetLocation(File input, File output) Call this method to set the input and output location from giveninputandoutputfile.voidsetProperty(String key, Object value) Set an engine Runtime property.protected BufferedWritersetWriterForEvaluate(Template template) protected voidtranslate(Template template, StringBuilder inProcessBuffer) Process the template ininProcessBufferinto valid format.inttrimTrailSpace(int currentEndCursor, StringBuilder inProcessBuffer) Calculate and return the new cursor at the end of #hash statement after trim.Methods inherited from class com.nomagic.magicreport.engine.AbstractTemplateEngine
addContext, addInvalidReferenceHandler, addObserver, addReferenceInsertionHandler, evaluate, evaluate, getConcurrentToolException, getContext, getCurrentRuntimeInstance, getInvalidReferenceHandler, getProcessSize, getProperties, getProperty, getReferenceInsertionHandler, isMemoryMode, notifyObservers, observers, setConcurrentToolException, setContext, setCurrentRuntimeInstanceMethods inherited from class java.util.Observable
clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, setChanged
-
Field Details
-
velocityContext
protected org.apache.velocity.VelocityContext velocityContextHold velocity context to abort engine.
-
-
Constructor Details
-
DefaultTemplateEngine
protected DefaultTemplateEngine()Create an instance of engine.
-
-
Method Details
-
createTemplate
Creates a new and emptyTemplatefor generating report by this engine.- Specified by:
createTemplatein interfaceITemplateEngine- Returns:
- an instance of
Template - See Also:
-
createTemplate
Creates a newTemplateobject.- Specified by:
createTemplatein interfaceITemplateEngine- Parameters:
input- template file.output- output file.- Returns:
- an instance of
Template - Throws:
FileNotFoundException- if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened.IOException- If an I/O error occurs
-
createTemplate
Creates a newTemplateobject.- Parameters:
input- template file.output- output file.ignoreLocation- true to ignore location property for child document- Returns:
- an instance of
Template - Throws:
FileNotFoundException- if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened.IOException- If an I/O error occurs
-
checkFileNotFoundException
- Throws:
FileNotFoundException
-
isNoSpaceException
-
setLocation
Call this method to set the input and output location from giveninputandoutputfile.- Parameters:
input- input template fileoutput- output report file- See Also:
-
setLatestLocation
Call this method to set the latest input and output location from giveninputandoutputfile.- Parameters:
input- input template fileoutput- output report file
-
setProperty
Set an engine Runtime property.- Specified by:
setPropertyin interfaceITemplateEngine- Overrides:
setPropertyin classAbstractTemplateEngine- Parameters:
key- the key to be placed into this property list.value- the value corresponding to key.- See Also:
-
getFormatter
Returns a formatter that can be used to create the object layout, and formats of stateInsertionHandler.By default this class returns a
DefaultFormatter. This formatter handle common text and no Bookmark, Link and Image support. The engine should override this method and return proper formatter implementation.- Specified by:
getFormatterin interfaceITemplateEngine- Returns:
- a formatter
-
setFormatter
Set the formatter to be used with this template engine.- Parameters:
formatter- a formatter
-
preProcess
Perform a pre-processing before evaluate template. This step will translate the template into valid velocity format with additional directive recognize.- Specified by:
preProcessin classAbstractTemplateEngine- Parameters:
template- template before evaluated.- Throws:
ParseErrorException- if a syntax or other error which prevents it from being parsed.InitializationEngineException- error while initializing engineTemplateException- other error while processing the template- See Also:
-
translate
protected void translate(Template template, StringBuilder inProcessBuffer) throws ParseErrorException, IOException Process the template ininProcessBufferinto valid format.- Parameters:
template- Template source.inProcessBuffer- in process buffer.- Throws:
ParseErrorException- when error while translation, or syntax not recognized.IOException- if IO Error occurs while reading template.
-
handleForPage
protected void handleForPage(Template template, StringBuilder inProcessBuffer) throws ParseErrorException Handle the #forpage directive.- Parameters:
template- Template source.inProcessBuffer- in process buffer.- Throws:
ParseErrorException- when error while translation, or syntax not recognized.
-
handleForRow
protected void handleForRow(Template template, StringBuilder inProcessBuffer) throws ParseErrorException Handle the #forrow directive.- Parameters:
template- Template source.inProcessBuffer- in process buffer.- Throws:
ParseErrorException- when error while translation, or syntax not recognized.
-
handleSectionBegin
protected void handleSectionBegin(Template template, StringBuilder inProcessBuffer) throws ParseErrorException Handle the #sectionBegin directive.- Parameters:
template- Template source.inProcessBuffer- in process buffer.- Throws:
ParseErrorException- when error while translation, or syntax not recognized.
-
handleIncludeSection
protected void handleIncludeSection(Template template, StringBuilder inProcessBuffer) throws ParseErrorException Handle the #includeSection directive.- Parameters:
template- Template source.inProcessBuffer- in process buffer.- Throws:
ParseErrorException- when error while translation, or syntax not recognized.
-
handleImport
protected void handleImport(Template template, StringBuilder inProcessBuffer) throws ParseErrorException Handle the #include directive.- Parameters:
template- Template source.inProcessBuffer- in process buffer.- Throws:
ParseErrorException- when error while translation, or syntax not recognized.
-
trimTrailSpace
Calculate and return the new cursor at the end of #hash statement after trim.- Parameters:
currentEndCursor- current end statement cursorinProcessBuffer- in process buffer- Returns:
- new cursor at the end of #hash statement
-
createComment
Return a formatted comment from given message. The default comment format return from this method is "# {0}" where {0} is message;- Parameters:
message- message to put in document comment- Returns:
- comment
-
process
Process a template. By default this method will call velocity engine to renders the input reader using the context into the output writer.- Specified by:
processin classAbstractTemplateEngine- Parameters:
template- template being evaluated.- Throws:
ParseErrorException- if a syntax or other error which prevents it from being parsed.InitializationEngineException- error while initializing engineTemplateException- other error while processing the template- See Also:
-
process
Process a template. By default this method will call velocity engine to renders the input reader using the context into the output writer.- Specified by:
processin classAbstractTemplateEngine- Parameters:
template- template being evaluated.parentEngine- an engine to shared a runtime instance for evaluate- Throws:
ParseErrorException- if a syntax or other error which prevents it from being parsed.InitializationEngineException- error while initializing engineTemplateException- other error while processing the template- See Also:
-
setWriterForEvaluate
-
postProcess
Perform a post-processing after the template has been evaluated. This process just copy output file directly from processing location to final output.- Specified by:
postProcessin classAbstractTemplateEngine- Parameters:
template- template after evaluated.- Throws:
ParseErrorException- if a syntax or other error which prevents it from being parsed.InitializationEngineException- error while initializing engineTemplateException- other error while processing the template- See Also:
-
processVelocityException
protected TemplateException processVelocityException(Template template, org.apache.velocity.exception.MethodInvocationException e) Call this method to process an Velocity exception and return in new format (ParseErrorException). By default, we can't get a nice error message from Velocity (Velocity error message containing in plain text and not readable by non-programmer.) And velocity itself doesn't provide any mechanism to get an message from ParseErrorException. This method will format the error message, remove unstructured text from velocity message e.g. "of" (with out appending text)- Parameters:
template- Templatee- MethodInvocationException- Returns:
- TemplateException
-
processVelocityException
protected TemplateException processVelocityException(Template template, org.apache.velocity.exception.ParseErrorException e) Call this method to process an Velocity exception and return in new format (ParseErrorException). By default, we can't get a nice error message from Velocity (Velocity error message containing in plain text and not readable by non-programmer.) And velocity itself doesn't provide any mechanism to get an message from ParseErrorException. This method will format the error message, remove unstructured text from velocity message e.g. "of" (with out appending text)- Parameters:
template- Templatee- Velocity exception- Returns:
- TemplateException
-
processException
Call this method to process aParseErrorException. This method call internally bypostProcess(Template). If code was performed by Velocity (process(Template)), uses methodprocessVelocityException(Template, org.apache.velocity.exception.ParseErrorException). By default this method try to calculate line number fromParseErrorException.getSource().- Parameters:
template- Templatee- MagicReport parse error exception- Returns:
- TemplateException
-
lineCount
Count number of line from given content string.- Parameters:
content- the input string- Returns:
- number of line
-
getClassLoader
Returns the class loader for the engine. This method will return null in such implementations if this class was loaded by the application class loader.- Specified by:
getClassLoaderin interfaceIExtensionTemplateEngine- Returns:
- the class loader that loaded the class or interface represented by this object.
-
setClassLoader
Sets the class loader for this engine. The class loader must be set before a template is evaluated. This method allows the creator of the template to provide the appropriate class loader to code running in the template when loading classes and resources.- Specified by:
setClassLoaderin interfaceIExtensionTemplateEngine- Parameters:
loader- Class loader for this engine
-
destroy
public void destroy()Called by the application orTemplateEngineFactoryto inform this engine that it should destroy any resources that it has allocated.A subclass of engine should override this method if it has any operation that it wants to perform before it is destroyed.
- Specified by:
destroyin interfaceITemplateEngine- Overrides:
destroyin classAbstractTemplateEngine
-
abort
public void abort()Call to abort Velocity context.- Specified by:
abortin interfaceITemplateEngine- Overrides:
abortin classAbstractTemplateEngine
-
getResolvedSectionName
-
clearTools
public void clearTools()
-