file

Syntax

[#file]filename expression[/#file]
or
[#file encoding="value"]filename expression[/#file]

where:

Description

The file directive defines the name of the file where the text generated by a text template will be written. The nested content of the directive can contain dynamic text expressions that will be evaluated at runtime. This directive is only applicable in text templates.

It is recommended to use a relative path here (e.g. myFolder\myFile.txt). A relative path will be resolved at runtime to an absolute path (e.g. c:\myProject\generated\myFolder\myFile.txt) using a default evaluation directory, which can be configured by the end-user of the generator. If you specify an absolute path in this directive contents, the generation will be less configurable and flexible.

Although it may appear anywhere inside a text template body, it is a good practice for reader's convenience to put the file directive right after the template start tag. If this directive is omitted, then the text template is designed to be included inside another template.

The optional encoding attribute specifies an encoding to use when writing the generated content. This is particulary useful when generating HTML or XML files. You may use any encoding supported by the underlying Java platform: http://java.sun.com/j2se/1.5.0/docs/api/java/nio/charset/Charset.html
If omitted, the platform default encoding will be used.

Examples

This will generate a simple Java file, based on the specified class name:

[#package com.mycompany.example]

[#template GenerateJavaFile(class : uml21.Class)]
[#file]${class.name}.java[/#file]
public class ${class.name} {
}
[/#template]

This will generate a simple XML file, with an UTF-8 encoding:

[#package com.mycompany.example]

[#template GenerateXMLFile()]
[#file encoding="UTF-8"]myFolder\myFile.xml[/#file]
<?xml version="1.0" encoding="UTF-8"?>
<contents>
</contents>
[/#template]