ijgen.generator.engine
Class TemplateGenerator

java.lang.Object
  extended by ijgen.generator.engine.TemplateGenerator
All Implemented Interfaces:
java.io.Serializable
Direct Known Subclasses:
DbCreateTemplateGenerator, MultipleDefinitionsTemplateGenerator, SingleDefinitionTemplateGenerator

public abstract class TemplateGenerator
extends java.lang.Object
implements java.io.Serializable

Abstract class representing a generator working on a set of template files.

Author:
Detelin Zlatev
See Also:
Serialized Form

Field Summary
protected  java.util.List<ClassDefinition> classDefinitions
          Class definitions which will be passed to the abstract doGenerate() method
protected  java.lang.String customLine
          Contains the string describing that file is already customized and should not be overriden by the generator.
protected  java.lang.String fileFilter
          Holds the extension that is used for filter which templates has to be proccessed by generator.
protected  java.lang.String generatedExtension
          Specifies the extension for generated artifacts.
protected  java.lang.String outputPath
          Variable specifying the output path for the generated artifacts
 
Constructor Summary
protected TemplateGenerator()
          Default constructor.
 
Method Summary
protected abstract  void doGenerate(java.util.Map<java.lang.String,java.io.File> templates, java.util.List<ClassDefinition> classDefinitions)
          Abstract method that should be overridden by all child classes.
 void generate()
          Template method that prepares the parameters and run the actual implementation of doGenerate() with these parameters.
protected abstract  java.lang.String getCustomLine()
          Abstract method that forces successors of this class to specify the custom line which indicates that the artifact is already customized and should not be generated anymore.
protected abstract  java.lang.String getFileFilter()
          Abstract method that forces successors of this class to specify extension of the template files that has to be processed.
protected abstract  java.lang.String getGeneratedExtension()
          Abstract method that forces successors of this class to specify the file extension which will be received by the generated artifacts.
 java.lang.String getOutputPath()
          Gets the path to the directory where generated artifacts will be stored.
protected  java.util.Map<java.lang.String,java.io.File> getTemplateFiles()
          Gets cached instance of template files.
protected  org.apache.velocity.Template getVelocityTemplate(java.io.File templateFile)
          Creates a velocity template from the specified file which contains the path to the actual resource containing the template.
protected  org.apache.velocity.VelocityContext prepareContext(java.lang.String templateName)
          Prepares instance of VelocityContext and populates it with the standard attributes
protected  java.lang.String readFirstFileLine(java.lang.String filePath)
          Simple utility method that just reads the first line of the file specified as a parameter.
protected  void setClassDefinitions(java.util.List<ClassDefinition> classDefinitions)
          Sets list of model definitions that will be processed by the generator.
 void setCustomLine(java.lang.String customLine)
          Sets the string which indicates that file is customized and should not be overridden.
 void setFileFilter(java.lang.String fileFilter)
          Sets the extension of template files that should be processed by this generator.
 void setGeneratedExtension(java.lang.String generatedExtension)
          Sets the extension of files produced by this generator.
 void setGlobalSettings(GlobalSettings globalSettings)
          Sets the global settings object.
 void setOutputPath(java.lang.String outputPath)
          Sets the path to the directory where generated artifacts will be stored.
 void setTemplatesCache(java.util.Map<java.lang.String,java.io.File> templatesCache)
          Sets the cache with template files.
protected  void writeToFile(ClassDefinition classDefinition, java.lang.String layer, boolean appendPackage, byte[] content)
          Writes to a physical file produced artifact by the generator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

outputPath

protected java.lang.String outputPath
Variable specifying the output path for the generated artifacts


classDefinitions

protected java.util.List<ClassDefinition> classDefinitions
Class definitions which will be passed to the abstract doGenerate() method


customLine

protected java.lang.String customLine
Contains the string describing that file is already customized and should not be overriden by the generator.


fileFilter

protected java.lang.String fileFilter
Holds the extension that is used for filter which templates has to be proccessed by generator.


generatedExtension

protected java.lang.String generatedExtension
Specifies the extension for generated artifacts.

Constructor Detail

TemplateGenerator

protected TemplateGenerator()
Default constructor.

Method Detail

setGlobalSettings

public void setGlobalSettings(GlobalSettings globalSettings)
Sets the global settings object.

Parameters:
globalSettings - globalSettings

setCustomLine

public final void setCustomLine(java.lang.String customLine)
Sets the string which indicates that file is customized and should not be overridden. Example: for java files this would be "//custom"

Parameters:
customLine - custom string

setFileFilter

public final void setFileFilter(java.lang.String fileFilter)
Sets the extension of template files that should be processed by this generator. Example: "jv" means that file domain.vm.jv will be processed by the generator but for example domain.vm.dbs will not be taken into consideration.

Parameters:
fileFilter -

setGeneratedExtension

public final void setGeneratedExtension(java.lang.String generatedExtension)
Sets the extension of files produced by this generator. Example: "java" means that generated files will have names *.java.

Parameters:
generatedExtension - name of the extension

setTemplatesCache

public final void setTemplatesCache(java.util.Map<java.lang.String,java.io.File> templatesCache)
Sets the cache with template files.

Parameters:
templatesCache - template files cache

getOutputPath

public final java.lang.String getOutputPath()
Gets the path to the directory where generated artifacts will be stored.

Returns:
path to the output directory

setOutputPath

public final void setOutputPath(java.lang.String outputPath)
Sets the path to the directory where generated artifacts will be stored.

Parameters:
outputPath - output directory

setClassDefinitions

protected final void setClassDefinitions(java.util.List<ClassDefinition> classDefinitions)
Sets list of model definitions that will be processed by the generator.

Parameters:
classDefinition - class definitions

doGenerate

protected abstract void doGenerate(java.util.Map<java.lang.String,java.io.File> templates,
                                   java.util.List<ClassDefinition> classDefinitions)
                            throws java.lang.Exception
Abstract method that should be overridden by all child classes. Does the actual generation stuff.

Parameters:
templates - map containing a cache with template files
classDefinitions - model definitions
Throws:
java.lang.Exception

getFileFilter

protected abstract java.lang.String getFileFilter()
Abstract method that forces successors of this class to specify extension of the template files that has to be processed.

Returns:
file extension

getCustomLine

protected abstract java.lang.String getCustomLine()
Abstract method that forces successors of this class to specify the custom line which indicates that the artifact is already customized and should not be generated anymore.

Returns:
customized line

getGeneratedExtension

protected abstract java.lang.String getGeneratedExtension()
Abstract method that forces successors of this class to specify the file extension which will be received by the generated artifacts.

Returns:
generated extension

generate

public final void generate()
Template method that prepares the parameters and run the actual implementation of doGenerate() with these parameters.


getTemplateFiles

protected final java.util.Map<java.lang.String,java.io.File> getTemplateFiles()
Gets cached instance of template files.

Returns:
template files

writeToFile

protected final void writeToFile(ClassDefinition classDefinition,
                                 java.lang.String layer,
                                 boolean appendPackage,
                                 byte[] content)
                          throws java.io.IOException
Writes to a physical file produced artifact by the generator. Write operation could be skipped if user has already customized this file.

Parameters:
classDefinition - ClassDefinition object used to produce the artifact
layer - name of the layer/template used for generation of the artifact
appendPackage - true if package has to be considered as a directory structure
content - content that has to be written in the file
Throws:
java.io.IOException - if something goes wrong in the IO operations

readFirstFileLine

protected final java.lang.String readFirstFileLine(java.lang.String filePath)
                                            throws java.io.IOException
Simple utility method that just reads the first line of the file specified as a parameter.

Parameters:
filePath - path to the file
Returns:
first line of the file or null if the file is empty or does not exists
Throws:
java.io.IOException

getVelocityTemplate

protected final org.apache.velocity.Template getVelocityTemplate(java.io.File templateFile)
                                                          throws java.lang.Exception
Creates a velocity template from the specified file which contains the path to the actual resource containing the template.

Parameters:
templateFile - File object containing the path to the velocity template file
Returns:
velocity template
Throws:
java.lang.Exception - if some problem occurs

prepareContext

protected final org.apache.velocity.VelocityContext prepareContext(java.lang.String templateName)
Prepares instance of VelocityContext and populates it with the standard attributes

Parameters:
templateName - name of the templates that is being currently processed
Returns:
prepared context instance


Copyright © 2010 Detelin Zlatev. All Rights Reserved.