Package de.stefanfrings.utils
Class TextTemplate
java.lang.Object
de.stefanfrings.utils.TextTemplate
Text document creation with templates.
Different to Freemarker/Velocity, this class generates text documents from templates without the need of a model. The application actively writes into the document to fill variables.
This class has been designed for cases where a very simple more restricted template syntax is required.
Example template file:
Hello {username}, how are you?
{if locked}
Your account is locked.
{else locked}
Welcome on our system.
{end locked}
The following users are on-line:
Username Time
{loop user}
{user.name} {user.time}
{end user}
Example code to fill this template:
File file=new File("/temp/test.tpl");
TextTemplate t=new TextTemplate(file,"UTF-8");
t.variable("user", "Stefan");
t.condition("locked",false);
t.loop("user",2);
t.variable("user0.name,"Markus");
t.variable("user0.time,"8:30");
t.variable("user1.name,"Roland");
t.variable("user1.time,"8:45");
The code example above shows how variable within loops are numbered. Counting starts with 0. Loops can be nested, for example:
<table>
{loop row}
<tr>
{loop row.column}
<td>{row.column.value}</td>
{end row.column}
</tr>
{end row}
</table>
Example code to fill this nested loop:
t.loop("row",10);
for (int i=0; i<10; i++) {
t.loop("row"+i+".column",10);
for (int j=0; j<10; j++) {
t.variable("row"+i+".column+"j"+".value","Hello");
}
}
This creates a HTML table with 10 rows and each with 10 columns.
The fields are filled with the string "Hello".- Author:
- Stefan Frings, http://stefanfrings.de/javautils
-
Constructor Summary
ConstructorsConstructorDescriptionTextTemplate(CombinedURL url, int connTimeout, int readTimeout, String encoding) Constructor that reads a template from an URL.TextTemplate(File file, String encoding) Constructor that reads a template from a file.TextTemplate(InputStream stream, String sourceName, String encoding) Constructor that reads a template from an InputStream.TextTemplate(CharSequence template, String sourceName) Constructor that copies a template from CharSequence.TextTemplate(URL url, String encoding) Constructor that reads a template from a local URL. -
Method Summary
-
Constructor Details
-
TextTemplate
Constructor that copies a template from CharSequence.- Parameters:
template- The template as CharSequencesourceName- Symbolic name of the template, used in error messages loops cause an exception. If false, only syntax errors cause an exception.
-
TextTemplate
public TextTemplate(CombinedURL url, int connTimeout, int readTimeout, String encoding) throws IOException Constructor that reads a template from an URL.- Parameters:
url- The URL of the template fileconnTimeout- Timeout connectng to the serverreadTimeout- Timeout reading the from the serverencoding- Default encoding, used if the server does not provide this information- Throws:
IOException- In case of an I/O error.
-
TextTemplate
Constructor that reads a template from a local URL. To read from a remote URL, useTextTemplate(CombinedURL url, int connTimeout, int readTimeout, String encoding)instead.- Parameters:
url- The URL of the template fileencoding- Default encoding, used if the server does not provide this information- Throws:
IOException- In case of an I/O error.
-
TextTemplate
Constructor that reads a template from an InputStream. To read from a remote URL, useTextTemplate(CombinedURL url, int connTimeout, int readTimeout, String encoding)instead.- Parameters:
stream- The source streamsourceName- Symbolic name of the template, used in error messagesencoding- Default encoding, used if the server does not provide this information- Throws:
IOException- In case of an I/O error.
-
TextTemplate
Constructor that reads a template from a file.- Parameters:
file- The template file.encoding- Encoding of the file, null is allowed to use the default.- Throws:
IOException- In case of an I/O error.
-
-
Method Details
-
toString
Return the filled template as String. -
check
public boolean check()Check if the template contains unused tags. If there are such tags, then the function logs a warning.- Returns:
- true if there are one or more unused tags
-
variable
Replace a variable by a value. This affects tags with the syntax {name}.- Parameters:
name- Name of the variablevalue- New value, null produces in an empty string.- Returns:
- The number of variables that have been replaced
- Throws:
TemplateException- If the template contains a syntx error
-
condition
Set a condition. This affects tags with the syntax- {if name}...{end name}
- {if name}...{else name}...{end name}
- {ifnot name}...{end name}
- {ifnot name}...{else name}...{end name}
- Parameters:
name- Name of the conditionvalue- Value of the condition- Returns:
- The number of conditions that have been processed
- Throws:
TemplateException- If the template contains a syntx error
-
loop
Set repetitions of a loop. This affects tags with the syntax- {loop name}...{end name}
- {loop name}...{else name}...{end name}
- Parameters:
name- Name of the looprepetitions- The number of repetitions- Returns:
- The number of conditions that have been processed
- Throws:
TemplateException- If the template contains a syntx error
-