Class TemplateController

java.lang.Object
de.monticore.generating.templateengine.TemplateController

public class TemplateController extends Object
Provides methods for manipulating the content of templates, mainly for calling and including of templates.
  • Field Details

    • AST

      public static final String AST
      Variable name for the current node (used in the templates)
      See Also:
    • TC

      public static final String TC
      Variable name for the current TemplateController (used in the templates)
      See Also:
    • GLEX

      public static final String GLEX
      Variable name for the GLEX object (used in the templates)
      See Also:
    • config

      protected final GeneratorSetup config
      General config variables that hold for all template executions
    • templateBlackList

      protected List<String> templateBlackList
      list that contains all the Template names that should be blocked from generating comments for
    • templatename

      protected String templatename
      Name of the current template (usually fully qualified)
    • arguments

      protected List<Object> arguments
      According to FreeMarker, templates don't have a "signature" We can mimic such a signature through method calls: The signature(...) method defines a list of variables. And the include call allows a list of arguments that are bound to these variables, when the template is executed.
    • data

      protected freemarker.template.SimpleHash data
  • Constructor Details

  • Method Details

    • getGeneratorSetup

      public GeneratorSetup getGeneratorSetup()
      getters and setters for the relevant attributes
    • getTemplatename

      public String getTemplatename()
    • setTemplatename

      protected void setTemplatename(String templatename)
    • getTemplateBlackList

      public List<String> getTemplateBlackList()
    • setTemplateBlackList

      public void setTemplateBlackList(List<String> templateBlackList)
    • include

      public StringBuilder include(List<String> templatenames, List<ASTNode> astlist)
      Execute each of the templates on each ASTNode of the list. Concatenate the results together in one big String and include that into the currently processed output. We iterate on the templates and ASTNodes. In case order is important: The iteration goes like this: for ( templates ) { for ( ASTNodes ) {...} } Inside the inner loop, it is checked whether Hookpoints are to be called. Template filename may be qualified (using "."). When it is not qualified, the filename is taken from the current package (same as the calling template).
      Parameters:
      templatenames - list of filenames, qualified or not
      astlist - where we execute the template on in an iteration
      Returns:
      produced output
    • isTemplateNoteGenerated

      public boolean isTemplateNoteGenerated(freemarker.template.Template template)
    • signature

      public void signature(List<String> parameterNames)
      Defines the signature of a template. Note that, due to technical constraints, at first, the current template is included and the arguments are passed. Second, the signature is defined.
      Parameters:
      parameterNames - the list of the parameter names (=signature)
    • signature

      public void signature(String... parameterName)
      Delegates to signature(List).
    • include

      public StringBuilder include(String templateName)
      Includes a template without an explicit ast. (ast is current ast node)
      Parameters:
      templateName -
      Returns:
    • include

      public StringBuilder include(List<String> templateNames)
      Includes a template without an explicit ast. (ast is current ast node) Execute each of the templates on the ASTNode. Concatenate the results together in one big String and include that into the currently processed output. We iterate on the templates. Template filename may be qualified (using "."). When it is not qualified, the filename is taken from the current package (same as the calling template).
      Parameters:
      templateNames - list of filenames, qualified or not
      Returns:
      produced output
    • include

      public StringBuilder include(List<String> templateNames, ASTNode ast)
      Execute each of the templates on the ASTNode. Concatenate the results together in one big String and include that into the currently processed output. We iterate on the templates. Template filename may be qualified (using "."). When it is not qualified, the filename is taken from the current package (same as the calling template).
      Parameters:
      templateNames - list of filenames, qualified or not
      ast - ast-node the template is operating on
      Returns:
      produced output
    • include

      public StringBuilder include(String templateName, List<ASTNode> astlist)
      Execute the single template on each ASTNode of the list. Concatenate the results together in one big String and include that into the currently processed output. We iterate on the ASTNodes. Template filename may be qualified (using "."). When it is not qualified, the filename is taken from the current package (same as the calling template).
      Parameters:
      templateName - filename, qualified or not
      astlist - where we execute the template on in an iteration
      Returns:
      produced output
    • include

      public StringBuilder include(String templateName, ASTNode ast)
      Include the template into the currently processed output. The template is executed on ASTNode ast. Remark: even though the name suggests to run several templates, this is the version that executes on a single template given as string. We only handle one template on one node. Template filename may be qualified (using "."). When it is not qualified, the filename is taken from the current package (same as the calling template).
      Parameters:
      templateName - name of the template to be executed, qualified or not
      ast - ast-node the template is operating on
      Returns:
      output for the file (may be part of a file only)
    • includeArgs

      public StringBuilder includeArgs(String templateName, ASTNode node, List<Object> templateArguments)
      Include the template into the currently processed output. Remark: even though the name suggests to run several templates, this is the version that executes on a single template given as string. We only handle one template on one node. Template filename may be qualified (using "."). When it is not qualified, the filename is taken from the current package (same as the calling template).
      Parameters:
      templateName - name of the template to be executed, qualified or not
      templateArguments - additional data that is passed to the called template
      Returns:
      output for the file (may be part of a file only)
    • includeArgs

      public StringBuilder includeArgs(String templateName, List<Object> templateArguments)
      Include the template into the currently processed output. Remark: even though the name suggests to run several templates, this is the version that executes on a single template given as string. We only handle one template on one node. Template filename may be qualified (using "."). When it is not qualified, the filename is taken from the current package (same as the calling template).
      Parameters:
      templateName - name of the template to be executed, qualified or not
      templateArguments - additional data that is passed to the called template
      Returns:
      output for the file (may be part of a file only)
    • includeArgs

      public StringBuilder includeArgs(String templateName, String... templateArgument)
    • include

      public void include(Object empty1, Object empty2)
      This is an unusual method. It does nothing and should not be used. It will be called from freemarker automatically, when one of the arguments (either the filename or the ast) is null. In this case an error is issued.
      Parameters:
      empty1 -
      empty2 -
    • write

      public void write(String templateName, String qualifiedFileName, ASTNode ast)
      Execute the template and put the result into a file. The template is executed on ASTNode ast. File qualifiedFileName + default file extension is opened, written and closed again!
      Parameters:
      templateName - full qualified filename
      ast - where we execute the template on
    • write

      public void write(String templateName, String qualifiedFileName, String fileExtension, ASTNode ast)
    • write

      public void write(String templateName, Path filePath, ASTNode ast)
    • writeArgs

      public void writeArgs(String templateName, String qualifiedFileName, String fileExtension, ASTNode ast, List<Object> templateArguments)
      Execute the template and put the result into a file. The template is executed on ASTNode ast. And the file qualifiedFileName + fileExtension is opened, written and closed again! If fileExtension == null, then use "". fileExtension may start with ".", otherwise one is added. So ".java" and "java" have the same effect. Template filename may be qualified (using "."). When it is not qualified, the filename is taken from the current package (same as the calling template).
      Parameters:
      templateName - full qualified filename
      ast - where we execute the template on
    • writeArgs

      public void writeArgs(String templateName, Path filePath, ASTNode ast, List<Object> templateArguments)
      Processes the template templateName with the ast and the given templateArguments and writes the content into the filePath. Note: Unless not absolute, the filePath is relative to the configured target directory (i.e.,
      Parameters:
      templateName - the template to be processes
      filePath - the file path in which the content is to be written
      ast - the ast
      templateArguments - additional template arguments (if needed).
    • processTemplate

      protected String processTemplate(String templateName, ASTNode astNode, List<Object> passedArguments)
      Include a template with additional data: We only handle one template on one node. This method allows to parameterize templates. Template filename may be qualified (using "."). When it is not qualified, the filename is taken from the current package. The resulting output may be stored or included in another generation process.
      Parameters:
      templateName - name of the template to be executed, qualified or not
      astNode - ast-node the template is operating on
      passedArguments - additional data that is passed to the included template
      Returns:
      output for the file (may be part of a file only)
    • initAliases

      protected void initAliases()
      Init template aliases if not already done. This happens once (for all main templates)
    • completeQualifiedName

      protected String completeQualifiedName(String name)
      checks if the name seems to be already qualified: if not, adds the current package (of the template it operates on)
    • getValueFromData

      protected Object getValueFromData(String name)
    • setValueToData

      protected void setValueToData(String name, Object value)
    • instantiate

      public Object instantiate(String className)
      Can be used to instantiate any Java-class with a default constructor (no args). The passed className is either in the same package as the calling template, or it needs to be qualified (dot-separated).
      Parameters:
      className - name of the class to be instantiated
      Returns:
      an object of the given className
    • instantiate

      public Object instantiate(String className, List<Object> params)
      Can be used to instantiate any Java-class with constructor of a signature fitting to the params. The passed className is either in the same package as the calling template, or it needs to be qualified (dot-separated).
      Parameters:
      className - name of the class to be instantiated
      params - parameters provided for the constructor-call
      Returns:
      an object of the given className
    • trace

      public void trace(String msg, String logName)
      See Also:
      • Log.trace(String, String)
    • debug

      public void debug(String msg, String logName)
      See Also:
      • Log.debug(String, String)
    • info

      public void info(String msg, String logName)
      See Also:
      • Log.info(String, String)
    • warn

      public void warn(String msg)
      See Also:
      • Log.warn(String)
    • error

      public void error(String msg)
      See Also:
      • Log.error(String)