net.sf.freecol.common.model
Class StringTemplate

java.lang.Object
  extended by net.sf.freecol.common.model.FreeColObject
      extended by net.sf.freecol.common.model.StringTemplate
Direct Known Subclasses:
HistoryEvent, ModelMessage

public class StringTemplate
extends FreeColObject

The StringTemplate represents a non-localized string that can be localized by looking up its value in a message bundle or similar Map. The StringTemplate may contain variables (keys) delimited by the '%' character, such as "%amount%" that will be replaced with a string or a StringTemplate. If the StringTemplate contains replacement values but no keys, then it is considered a "label" StringTemplate, and its value will be used to join the replacement values.

Version:
1.0

Nested Class Summary
static class StringTemplate.TemplateType
          The type of this StringTemplate, either NAME, a proper name that must not be localized (e.g.
 
Field Summary
private  java.lang.String defaultId
          An alternative key to use if the Id is not contained in the message bundle.
private  java.util.List<java.lang.String> keys
          The keys to replace within the string template.
private  java.util.List<StringTemplate> replacements
          The values with which to replace the keys in the string template.
private  StringTemplate.TemplateType templateType
          The TemplateType of this StringTemplate.
 
Fields inherited from class net.sf.freecol.common.model.FreeColObject
ARRAY_SIZE, ID_ATTRIBUTE, ID_ATTRIBUTE_TAG, INFINITY, logger, NO_ID, PARTIAL_ATTRIBUTE, UNDEFINED, VALUE_TAG
 
Constructor Summary
protected StringTemplate()
           
protected StringTemplate(java.lang.String template, StringTemplate.TemplateType templateType)
          Creates a new Template instance.
  StringTemplate(java.lang.String id, StringTemplate template)
           
 
Method Summary
 StringTemplate add(java.lang.String value)
          Add a replacement value without a key to the StringTemplate.
 StringTemplate add(java.lang.String key, java.lang.String value)
          Add a new key and replacement to the StringTemplate.
 StringTemplate addAmount(java.lang.String key, java.lang.Number amount)
          Add a key and an integer value to replace it to this StringTemplate.
 StringTemplate addName(java.lang.String value)
          Add a replacement value without a key to the StringTemplate.
 StringTemplate addName(java.lang.String key, FreeColObject object)
          Add a new key and replacement to the StringTemplate.
 StringTemplate addName(java.lang.String key, java.lang.String value)
          Add a new key and replacement to the StringTemplate.
 StringTemplate addStringTemplate(java.lang.String key, StringTemplate template)
          Add a key and a StringTemplate to replace it to this StringTemplate.
 StringTemplate addStringTemplate(StringTemplate template)
          Add a StringTemplate to this LABEL StringTemplate.
 boolean equals(java.lang.Object o)
           
 java.lang.String getDefaultId()
          Get the DefaultId value.
 java.util.List<java.lang.String> getKeys()
          Get the Keys value.
 StringTemplate getReplacement(java.lang.String key)
          Return the replacement value for a given key, or null if there is none.
 java.util.List<StringTemplate> getReplacements()
          Get the Replacements value.
 StringTemplate.TemplateType getTemplateType()
          Get the TemplateType value.
static java.lang.String getXMLElementTagName()
          Returns the tag name of the root element representing this object.
 int hashCode()
           
static StringTemplate key(java.lang.String value)
           
static StringTemplate label(java.lang.String value)
           
static StringTemplate name(java.lang.String value)
           
protected  void readAttributes(javax.xml.stream.XMLStreamReader in)
          Reads the attributes of this object from an XML stream.
protected  void readChildren(javax.xml.stream.XMLStreamReader in)
          Reads the children of this object from an XML stream.
private  void readOldFormat(java.lang.String[] data)
           
 StringTemplate setDefaultId(java.lang.String newDefaultId)
          Set the DefaultId value.
static StringTemplate template(java.lang.String value)
           
 java.lang.String toString()
           
protected  void toXMLImpl(javax.xml.stream.XMLStreamWriter out)
          This method writes an XML-representation of this object to the given stream.
protected  void writeAttributes(javax.xml.stream.XMLStreamWriter out)
          Write the attributes of this object to a stream.
protected  void writeChildren(javax.xml.stream.XMLStreamWriter out)
          Write the children of this object to a stream.
 
Methods inherited from class net.sf.freecol.common.model.FreeColObject
addPropertyChangeListener, addPropertyChangeListener, dumpObject, fireIndexedPropertyChange, fireIndexedPropertyChange, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAttribute, getAttribute, getAttribute, getAttribute, getAttribute, getId, getPropertyChangeListeners, getPropertyChangeListeners, getSpecification, hasAbility, hasAttribute, hasListeners, readAttributes, readChild, readChild, readChildren, readFromArrayElement, readFromArrayElement, readFromListElement, readFromXML, readFromXMLElement, readFromXMLImpl, readFromXMLImpl, readFromXMLPartialImpl, removePropertyChangeListener, removePropertyChangeListener, save, save, setId, setSpecification, toXML, toXML, toXML, toXMLElement, toXMLElement, toXMLElement, toXMLElement, toXMLElementPartial, toXMLPartialImpl, writeAttribute
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

templateType

private StringTemplate.TemplateType templateType
The TemplateType of this StringTemplate. Defaults to KEY.


defaultId

private java.lang.String defaultId
An alternative key to use if the Id is not contained in the message bundle.


keys

private java.util.List<java.lang.String> keys
The keys to replace within the string template.


replacements

private java.util.List<StringTemplate> replacements
The values with which to replace the keys in the string template.

Constructor Detail

StringTemplate

protected StringTemplate()

StringTemplate

public StringTemplate(java.lang.String id,
                      StringTemplate template)

StringTemplate

protected StringTemplate(java.lang.String template,
                         StringTemplate.TemplateType templateType)
Creates a new Template instance.

Parameters:
template - a String value
templateType - a TemplateType value
Method Detail

getDefaultId

public final java.lang.String getDefaultId()
Get the DefaultId value.

Returns:
a String value

setDefaultId

public StringTemplate setDefaultId(java.lang.String newDefaultId)
Set the DefaultId value.

Parameters:
newDefaultId - The new DefaultId value.
Returns:
a StringTemplate value

name

public static StringTemplate name(java.lang.String value)

key

public static StringTemplate key(java.lang.String value)

template

public static StringTemplate template(java.lang.String value)

label

public static StringTemplate label(java.lang.String value)

getTemplateType

public final StringTemplate.TemplateType getTemplateType()
Get the TemplateType value.

Returns:
a TemplateType value

getKeys

public final java.util.List<java.lang.String> getKeys()
Get the Keys value.

Returns:
a List value

getReplacements

public final java.util.List<StringTemplate> getReplacements()
Get the Replacements value.

Returns:
a List value

getReplacement

public final StringTemplate getReplacement(java.lang.String key)
Return the replacement value for a given key, or null if there is none.

Parameters:
key - a String value
Returns:
a String value

add

public StringTemplate add(java.lang.String key,
                          java.lang.String value)
Add a new key and replacement to the StringTemplate. This is only possible if the StringTemplate is of type TEMPLATE.

Parameters:
key - a String value
value - a String value
Returns:
a StringTemplate value

add

public StringTemplate add(java.lang.String value)
Add a replacement value without a key to the StringTemplate. This is only possible if the StringTemplate is of type LABEL.

Parameters:
value - a String value
Returns:
a StringTemplate value

addName

public StringTemplate addName(java.lang.String key,
                              java.lang.String value)
Add a new key and replacement to the StringTemplate. The replacement must be a proper name. This is only possible if the StringTemplate is of type TEMPLATE.

Parameters:
key - a String value
value - a String value
Returns:
a StringTemplate value

addName

public StringTemplate addName(java.lang.String key,
                              FreeColObject object)
Add a new key and replacement to the StringTemplate. The replacement must be a proper name. This is only possible if the StringTemplate is of type TEMPLATE.

Parameters:
key - a String value
object - a FreeColObject value
Returns:
a StringTemplate value

addName

public StringTemplate addName(java.lang.String value)
Add a replacement value without a key to the StringTemplate. The replacement must be a proper name. This is only possible if the StringTemplate is of type LABEL.

Parameters:
value - a String value
Returns:
a StringTemplate value

addAmount

public StringTemplate addAmount(java.lang.String key,
                                java.lang.Number amount)
Add a key and an integer value to replace it to this StringTemplate.

Parameters:
key - a String value
amount - a Number value
Returns:
a StringTemplate value

addStringTemplate

public StringTemplate addStringTemplate(java.lang.String key,
                                        StringTemplate template)
Add a key and a StringTemplate to replace it to this StringTemplate.

Parameters:
key - a String value
template - a StringTemplate value
Returns:
a StringTemplate value

addStringTemplate

public StringTemplate addStringTemplate(StringTemplate template)
Add a StringTemplate to this LABEL StringTemplate.

Parameters:
template - a StringTemplate value
Returns:
a StringTemplate value

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

equals

public boolean equals(java.lang.Object o)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

toXMLImpl

protected void toXMLImpl(javax.xml.stream.XMLStreamWriter out)
                  throws javax.xml.stream.XMLStreamException
This method writes an XML-representation of this object to the given stream.

Specified by:
toXMLImpl in class FreeColObject
Parameters:
out - The target stream.
Throws:
javax.xml.stream.XMLStreamException - if there are any problems writing to the stream.

writeAttributes

protected void writeAttributes(javax.xml.stream.XMLStreamWriter out)
                        throws javax.xml.stream.XMLStreamException
Write the attributes of this object to a stream.

Overrides:
writeAttributes in class FreeColObject
Parameters:
out - The target stream.
Throws:
javax.xml.stream.XMLStreamException - if there are any problems writing to the stream.

writeChildren

protected void writeChildren(javax.xml.stream.XMLStreamWriter out)
                      throws javax.xml.stream.XMLStreamException
Write the children of this object to a stream.

Overrides:
writeChildren in class FreeColObject
Parameters:
out - The target stream.
Throws:
javax.xml.stream.XMLStreamException - if there are any problems writing to the stream.

readAttributes

protected void readAttributes(javax.xml.stream.XMLStreamReader in)
                       throws javax.xml.stream.XMLStreamException
Reads the attributes of this object from an XML stream.

Overrides:
readAttributes in class FreeColObject
Parameters:
in - The XML input stream.
Throws:
javax.xml.stream.XMLStreamException - if a problem was encountered during parsing.

readChildren

protected void readChildren(javax.xml.stream.XMLStreamReader in)
                     throws javax.xml.stream.XMLStreamException
Reads the children of this object from an XML stream.

Overrides:
readChildren in class FreeColObject
Parameters:
in - The XML input stream.
Throws:
javax.xml.stream.XMLStreamException - if a problem was encountered during parsing.

readOldFormat

private void readOldFormat(java.lang.String[] data)

getXMLElementTagName

public static java.lang.String getXMLElementTagName()
Returns the tag name of the root element representing this object.

Returns:
"stringTemplate".