com.sas.util
Class Template

com.sas.util.Template
All Implemented Interfaces:
com.sas.beans.PropertyChangeSource, com.sas.beans.VetoableChangeSource, com.sas.collection.ContentsChangedListener, com.sas.ComponentInterface, com.sas.lang.StringDataInterface, com.sas.LinkPropertiesInterface, com.sas.ModelInterface, com.sas.ViewInterface, MultipleValueEventSourceInterface, java.beans.PropertyChangeListener, java.io.ObjectInputValidation, java.io.Serializable, java.util.EventListener
Direct Known Subclasses:
SASProcedureTemplate

public class Template
implements com.sas.lang.StringDataInterface, com.sas.collection.ContentsChangedListener

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 text
 
 where:
 

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 by Character.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}.");
 

See Also:
Serialized Form

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

hasInitialized

public boolean hasInitialized

dlist

public com.sas.collection.Dictionary dlist
Constructor Detail

Template

public Template()
Default Constructor


Template

public Template(java.lang.String pattern)
Construct a Template object with the specified template

Parameters:
pattern - specified template

Template

public Template(java.lang.String pattern,
                com.sas.collection.DictionaryInterface dictionary)
         throws java.lang.IllegalArgumentException
Construct a Template with the specified template and object that implements the DictionaryInterface. The object that implements the DictionaryInterface must also support the ModelInterface.

Parameters:
pattern - specified template
dictionary - object that impletments com.sas.collection.DictionaryInterface
Throws:
java.lang.IllegalArgumentException - if the dictionary does not also implement ModelInterface

Template

public 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. The object that implements the DictionaryInterface must also support the ModelInterface.

Parameters:
pattern - specified template
dictionary - object that impletments com.sas.collection.DictionaryInterface
marker - 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/name
close - the character to use instead of the default ')' to close expansion options after the marker/name
Throws:
java.lang.IllegalArgumentException - if the dictionary does not also implement ModelInterface

Template

public Template(com.sas.collection.DictionaryInterface dictionary)
         throws java.lang.IllegalArgumentException
Construct a Template with the object that implements the DictionaryInterface. The object that implements the DictionaryInterface must also support the ModelInterface.

Parameters:
dictionary - object that impletments com.sas.collection.DictionaryInterface
Throws:
java.lang.IllegalArgumentException - if the dictionary does not also implement ModelInterface
Method Detail

getExtendedBeanInfo

public static com.sas.beans.ExtendedBeanInfo getExtendedBeanInfo()
Returns the ExtendedBeanInfo for this class.

Returns:
ExtendedBeanInfo for this class

attachModel

public void attachModel(com.sas.ModelInterface si)
Attaches the specified Model to this client.

Specified by:
attachModel in interface com.sas.ViewInterface
Overrides:
attachModel in class com.sas.Component
Parameters:
si - Model to attach to
See Also:
ViewInterface.attachModel(com.sas.ModelInterface)

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Creates a copy of the current model

Overrides:
clone in class com.sas.Component
Returns:
a clone of this object
Throws:
java.lang.CloneNotSupportedException - if the object cannot be cloned

contentsChanged

public void contentsChanged(com.sas.collection.ContentsChangedEvent evt)
Called when the contents of the Model have changed.

Specified by:
contentsChanged in interface com.sas.collection.ContentsChangedListener
Parameters:
evt - ContentsChangedEvent from the Model

detachModel

public void detachModel(com.sas.ModelInterface si)
Detaches the specified Model to this client.

Specified by:
detachModel in interface com.sas.ViewInterface
Overrides:
detachModel in class com.sas.Component
Parameters:
si - Model to detach from
See Also:
ViewInterface.detachModel(com.sas.ModelInterface)

getRequiredInterfaces

public java.util.Vector getRequiredInterfaces()
Returns the required interfaces Vector for this component.

Specified by:
getRequiredInterfaces in interface com.sas.ViewInterface
Overrides:
getRequiredInterfaces in class com.sas.Component
Returns:
the required interfaces Vector for this component.
See Also:
ViewInterface.getRequiredInterfaces()

getTemplate

public java.lang.String getTemplate()
Returns the template

Returns:
string containing variables and formatting directives

getText

public java.lang.String getText()
Returns the text created from the template

Specified by:
getText in interface com.sas.lang.StringDataInterface
Returns:
string whose variables have been replaced with their values

initialize

public void initialize()
Creates and attaches a model if no model is currently attached

Specified by:
initialize in interface com.sas.ComponentInterface
Overrides:
initialize in class com.sas.Component
See Also:
ComponentInterface.initialize()

refresh

public void refresh()
Sets dictionary items from the model


refresh

public void refresh(com.sas.ModelInterface Model)
Sets dictionary items from the model

Specified by:
refresh in interface com.sas.ViewInterface
Overrides:
refresh in class com.sas.Component
Parameters:
Model - attached model
See Also:
ViewInterface.refresh(com.sas.ModelInterface)

setTemplate

public void setTemplate(java.lang.String string_val)
Sets the template

Parameters:
string_val - string containing variables and formatting directives

setText

public void setText(java.lang.String s)
This method contains no code. It is here just to satisfy the requirements of the StringDataInterface

Specified by:
setText in interface com.sas.lang.StringDataInterface

setMarker

public void setMarker(char marker)
Set the marker property.

Parameters:
marker - the new value for the marker property.

getMarker

public char getMarker()
Return the value of the marker property.

Returns:
the value of the marker property.

setClose

public void setClose(char close)
Set the close property.

Parameters:
close - the new value for the close property.

getClose

public char getClose()
Return the value of the close property.

Returns:
the value of the close property.

setOpen

public void setOpen(char open)
Set the open property.

Parameters:
open - the new value for the open property.

getOpen

public char getOpen()
Return the value of the open property.

Returns:
the value of the open property.

setDefaultValues

public void setDefaultValues()
Sets the initial values to the default values. This method is called by the Component constructors. The setDefaultValues method of each class is responsible for setting its own initial values.

Specified by:
setDefaultValues in interface com.sas.ComponentInterface
Overrides:
setDefaultValues in class com.sas.Component
See Also:
ComponentInterface.setDefaultValues()



Copyright © 2009 SAS Institute Inc. All Rights Reserved.