|
Components |
|
| |||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
com.sas.util.Template
public class Template
The Template class serves as a generic string substitution and formatting class. A raw text string, referred to as the template, can contain variables, references, text, and formatting directives.
The Template class also uses a dictionary that contains variables in the template along with their associated values. Formatting the template replaces keys from the dictionary with their corresponding values.
The general form of a template is:
text %VAR(FLD1, FLD2, FLD3) more textwhere:VAR is the name of a variable that is defined in the dictionary supplied to the Template. Variable names must begin with %. The next character must be a valid identifier start character as defined by
Character.isJavaIdentifierStart(char ch)
. All subsequent characters must be valid identifier characters as defined byCharacter.isJavaIdentifierPart(char ch)
. Variable names are case sensitive.NOTE: The '%' is not part of the variable name (it acts as a delimiter). In order to specify a value for the variable, %PROC, one would use the following syntax where the dictionary key is specified as 'PROC' not '%PROC'. The '%' character may be changed by calling the
setMarker(char)
method, or you can used the constructor which specifies the marker character.
FLD1 is an optional field that will be evaluated and displayed if VAR is defined in the dictionary and is not null. If VAR is not defined, FLD1 will be not be displayed. FLD1 can be designated as a combination of text and variables.
FLD2 is an optional field that contains a default value that should be used if VAR is not defined in the dictionary or is null. FLD2 can be designated as a combination of text and variables.
FLD3 is an optional field that designates a class that will perform special
formatting. The class specified as FLD3 must extend java.text.Format.
The class will be instantiated and the format(Object)
method will be invoked with
the value of FLD1 (if VAR is defined) or FLD2 (if VAR is not defined or is null).
NOTES:
Commas are used for field delineation. If you need to use a comma as part of a field expression (FLD1,FLD2,FLD3), that field must be surrounded by quotes.
% is used for variable delineation. %% will be used to denote a % that is not
used for a variable name. If you change the marker character with setMarker(char)
,
you must double it to see it rendered. For example, if you call setMarker('$')
then you would use a template such as "The price is $$$price"
, with
price = 10.75 in the dictionary, to yield "$10.75"
The following are example of templates:
EXAMPLE 1:
"The woman is %status"If status = "very wealthy.", the expression will be processed to:
"The woman is very wealthy."If status is not defined, the expression will be processed to:
"The woman is"EXAMPLE 2:
"Today it is %DAY(%DAY, raining)."If the variable DAY is contained in the dictionary and set to "sunny", the string will be processed to:
"Today it is sunny."If the variable DAY is not contained in the dictionary or is null, the string will be processed to:
"Today it is raining."EXAMPLE 3:
"Hello. %person(%GREET %person?)"If person = "Mary" and GREET = "How are you", the expression will be processed to:
"Hello. How are you Mary?"If person is not defined in the dictionary, the expression will be processed to:
"Hello."If person = "Mary" and GREET is not defined, the expression will be processed to:
"Hello. Mary?"
EXAMPLE 4:
%state(, is not defined)If state is defined in the dictionary and is not null, the expression will be processed to:
"" (string of zero length)If state is not defined in the dictionary the expression will be processed to:
"is not defined"Note that the comma is necessary for correct field delineation.
EXAMPLE 5:
"The stock market is %market(%direction, closed)."If market is defined and direction="up today" the expression will be processed to:
"The stock market is up today."As this example illustrates, the VAR field (market) does not have to appear in the FLD1 expression (direction). The VAR field acts first as a true/false operator that dictates whether to evaluate FLD1 or FLD2 (FLD1 if VAR is in dictionary and not null, FLD2 otherwise). If VAR appears in the expression to be evaluated, its value is substituted in the expression.
EXAMPLE 6:
%test(%dvar,,com.sas.me.MyClass)An instance of com.sas.me.MyClass will be instantiated. If the class cannot be instantiated, an exception will be thrown. If com.sas.me.MyClass extends java.text.Format, the format(Object) method will be invoked with the value of dvar (if test is not null) or a "" (if test is null). If com.sas.myClass does not extend java.text.Format, the expression will not be evaluated any further and will not appear in the formatted string. Note that the second comma is necessary for correct field delineation.
The special characters '%', '(', and ')' can be replaced with other
characters on the Template constructoer, or by setting the
marker
,
open
,
close
properties. For example,
template.setMarker('$'); template.setOpen('{'); template.setClose('}'); template.setTemplate("The stock market is $market{$direction, closed}.");
Field Summary | |
---|---|
com.sas.collection.Dictionary |
dlist
|
boolean |
hasInitialized
|
Constructor Summary | |
---|---|
Template()
Default Constructor |
|
Template(com.sas.collection.DictionaryInterface dictionary)
Construct a Template with the object that implements the DictionaryInterface. |
|
Template(java.lang.String pattern)
Construct a Template object with the specified template |
|
Template(java.lang.String pattern,
com.sas.collection.DictionaryInterface dictionary)
Construct a Template with the specified template and object that implements the DictionaryInterface. |
|
Template(java.lang.String pattern,
com.sas.collection.DictionaryInterface dictionary,
char marker,
char open,
char close)
Construct a Template with the specified template and object that implements the DictionaryInterface. |
Method Summary | |
---|---|
void |
attachModel(com.sas.ModelInterface si)
Attaches the specified Model to this client. |
java.lang.Object |
clone()
Creates a copy of the current model |
void |
contentsChanged(com.sas.collection.ContentsChangedEvent evt)
Called when the contents of the Model have changed. |
void |
detachModel(com.sas.ModelInterface si)
Detaches the specified Model to this client. |
char |
getClose()
Return the value of the close property. |
static com.sas.beans.ExtendedBeanInfo |
getExtendedBeanInfo()
Returns the ExtendedBeanInfo for this class. |
char |
getMarker()
Return the value of the marker property. |
char |
getOpen()
Return the value of the open property. |
java.util.Vector |
getRequiredInterfaces()
Returns the required interfaces Vector for this component. |
java.lang.String |
getTemplate()
Returns the template |
java.lang.String |
getText()
Returns the text created from the template |
void |
initialize()
Creates and attaches a model if no model is currently attached |
void |
refresh()
Sets dictionary items from the model |
void |
refresh(com.sas.ModelInterface Model)
Sets dictionary items from the model |
void |
setClose(char close)
Set the close property. |
void |
setDefaultValues()
Sets the initial values to the default values. |
void |
setMarker(char marker)
Set the marker property. |
void |
setOpen(char open)
Set the open property. |
void |
setTemplate(java.lang.String string_val)
Sets the template |
void |
setText(java.lang.String s)
This method contains no code. |
Field Detail |
---|
public boolean hasInitialized
public com.sas.collection.Dictionary dlist
Constructor Detail |
---|
public Template()
public Template(java.lang.String pattern)
pattern
- specified templatepublic Template(java.lang.String pattern, com.sas.collection.DictionaryInterface dictionary) throws java.lang.IllegalArgumentException
pattern
- specified templatedictionary
- object that impletments com.sas.collection.DictionaryInterface
java.lang.IllegalArgumentException
- if the dictionary does not also
implement ModelInterface
public Template(java.lang.String pattern, com.sas.collection.DictionaryInterface dictionary, char marker, char open, char close)
pattern
- specified templatedictionary
- object that impletments com.sas.collection.DictionaryInterfacemarker
- the character to used to mark template expansion strings
in the template. The default is '%'open
- the character to use instead of the default '(' to open
expansion options after the marker/nameclose
- the character to use instead of the default ')' to close
expansion options after the marker/name
java.lang.IllegalArgumentException
- if the dictionary does not also
implement ModelInterface
public Template(com.sas.collection.DictionaryInterface dictionary) throws java.lang.IllegalArgumentException
dictionary
- object that impletments com.sas.collection.DictionaryInterface
java.lang.IllegalArgumentException
- if the dictionary does not also
implement ModelInterface
Method Detail |
---|
public static com.sas.beans.ExtendedBeanInfo getExtendedBeanInfo()
public void attachModel(com.sas.ModelInterface si)
attachModel
in interface com.sas.ViewInterface
attachModel
in class com.sas.Component
si
- Model to attach toViewInterface.attachModel(com.sas.ModelInterface)
public java.lang.Object clone() throws java.lang.CloneNotSupportedException
clone
in class com.sas.Component
java.lang.CloneNotSupportedException
- if the
object cannot be clonedpublic void contentsChanged(com.sas.collection.ContentsChangedEvent evt)
contentsChanged
in interface com.sas.collection.ContentsChangedListener
evt
- ContentsChangedEvent from the Modelpublic void detachModel(com.sas.ModelInterface si)
detachModel
in interface com.sas.ViewInterface
detachModel
in class com.sas.Component
si
- Model to detach fromViewInterface.detachModel(com.sas.ModelInterface)
public java.util.Vector getRequiredInterfaces()
getRequiredInterfaces
in interface com.sas.ViewInterface
getRequiredInterfaces
in class com.sas.Component
ViewInterface.getRequiredInterfaces()
public java.lang.String getTemplate()
public java.lang.String getText()
getText
in interface com.sas.lang.StringDataInterface
public void initialize()
initialize
in interface com.sas.ComponentInterface
initialize
in class com.sas.Component
ComponentInterface.initialize()
public void refresh()
public void refresh(com.sas.ModelInterface Model)
refresh
in interface com.sas.ViewInterface
refresh
in class com.sas.Component
Model
- attached modelViewInterface.refresh(com.sas.ModelInterface)
public void setTemplate(java.lang.String string_val)
string_val
- string containing variables and formatting directivespublic void setText(java.lang.String s)
setText
in interface com.sas.lang.StringDataInterface
public void setMarker(char marker)
marker
- the new value for the marker property.public char getMarker()
public void setClose(char close)
close
- the new value for the close property.public char getClose()
public void setOpen(char open)
open
- the new value for the open property.public char getOpen()
public void setDefaultValues()
setDefaultValues
in interface com.sas.ComponentInterface
setDefaultValues
in class com.sas.Component
ComponentInterface.setDefaultValues()
|
Components |
|
| |||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |