Class SETemplateProcessor
- java.lang.Object
-
- ca.cgjennings.text.TemplateProcessor
-
- ca.cgjennings.text.SETemplateProcessor
-
public class SETemplateProcessor extends TemplateProcessor
An extension of the basicTemplateProcessor
which supports additional symbols for looking up localized Strange EonsLanguage
strings. Template processors assist in the automated writing of scripts and other text files by filling in a template file using variables and simple boolean conditions.Sections of the template that are to be generated automatically are marked with symbols. A symbol consists of a special token placed between braces. The tokens begin with a specific marker character and are followed by an identifier. The identifier may consist of any characters except spaces or the closing brace. The following codes are recognized:
{@key}
Replaces the symbol with an interfaceLanguage
string with the given key.{#key}
Replaces the symbol with a gameLanguage
string with the given key.{%variable}
Replaces the symbol with the value of the specified variable. Throws an exception if the variable is undefined.{?condition} ... {/?condition}
If the condition is set totrue
, then the text between the start and end symbols will be included in the document. Otherwise, it will be left out. Throws an exception if the condition has not been set.{!condition} ... {/!condition}
If the condition is set tofalse
, then the text between the start and end symbols will be included in the document. Otherwise, it will be left out. Throws an exception if the condition has not been set.- Since:
- 3.0
- Author:
- Chris Jennings
-
-
Constructor Summary
Constructors Constructor Description SETemplateProcessor()
SETemplateProcessor(TemplateProcessor source)
SETemplateProcessor(java.util.Locale locale)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static java.lang.String
escapeJavaString(java.lang.String s)
Creates a Java string literal from a plain string.static java.lang.String
escapeScriptString(java.lang.String s)
Creates a JavaScript string literal from a plain string.java.lang.String
processFromResource(java.lang.String templateResource)
Processes a template stored in the application resources.protected java.lang.String
processSymbol(java.lang.StringBuilder buffer, java.lang.String symbol, char code, java.lang.String name, java.lang.String remainder)
This method is called to process a symbol.void
setGameLanguage(Language game)
Sets the language to be used for #-symbols.void
setInterfaceLanguage(Language ui)
Sets the language to be used for @-symbols.-
Methods inherited from class ca.cgjennings.text.TemplateProcessor
error, get, getLocale, isConditionSet, process, set, set, setAll, setCondition, setLocale
-
-
-
-
Constructor Detail
-
SETemplateProcessor
public SETemplateProcessor(TemplateProcessor source)
-
SETemplateProcessor
public SETemplateProcessor(java.util.Locale locale)
-
SETemplateProcessor
public SETemplateProcessor()
-
-
Method Detail
-
setInterfaceLanguage
public void setInterfaceLanguage(Language ui)
Sets the language to be used for @-symbols. (The default value isLanguage.getInterface()
.)- Parameters:
ui
- the UI language to set
-
setGameLanguage
public void setGameLanguage(Language game)
Sets the language to be used for #-symbols. (The default value isLanguage.getGame()
.)- Parameters:
game
- the game language to set
-
processSymbol
protected java.lang.String processSymbol(java.lang.StringBuilder buffer, java.lang.String symbol, char code, java.lang.String name, java.lang.String remainder)
Description copied from class:TemplateProcessor
This method is called to process a symbol. Subclasses may override it to extend the template processor's capabilities. The pattern for doing so is to first check the code against those codes that you wish to handle and then either handle the symbol in the subclass or return the result of the super implementation. A typical code pattern is:protected String processSymbol( StringBuilder buffer, String symbol, char code, String name, String remainder ) { switch( code ) { case '*': buffer.append( "text for a *-code with variable name: " + name ); break; // ... default: remainder = super( buffer, symbol, code, name, remainder ); } return remainder; }
- Overrides:
processSymbol
in classTemplateProcessor
- Parameters:
buffer
- a buffer that the symbol's replacement text, if any, should be appended tosymbol
- the full text of the symbol, e.g., {%variable}code
- the single-character code identifying the symbol type, e.g., %name
- the name part of the the symbol following the code, e.g., variableremainder
- the template text that follows the symbol- Returns:
- the template text that follows the symbol after the symbol is
processed (usually just
remainder
)
-
processFromResource
public java.lang.String processFromResource(java.lang.String templateResource)
Processes a template stored in the application resources. The template file must use the UTF-8 text encoding.- Parameters:
templateResource
- the resource to process- Returns:
- the completed template
-
escapeScriptString
public static java.lang.String escapeScriptString(java.lang.String s)
Creates a JavaScript string literal from a plain string. The string literal will be surrounded with 'single quotes', and newlines, tabs, backslashes, and single quotes within the string will be escaped.- Parameters:
s
- the source string- Returns:
- the string literal
-
escapeJavaString
public static java.lang.String escapeJavaString(java.lang.String s)
Creates a Java string literal from a plain string. The string literal will be surrounded with "double quotes", and newlines, tabs, backslashes, and double quotes will be escaped.- Parameters:
s
- the source string- Returns:
- the string literal
-
-