ch.elca.el4j.services.i18n
Class MessageRewriter

java.lang.Object
  extended by ch.elca.el4j.services.i18n.MessageRewriter

public class MessageRewriter
extends Object

A rewriting ressource bundle provides localized strings, which are defined using rewrite rules. The bundle is backed by a list of such rules. When asked to look up a key, the bundle uses the last matching rule.

Rule File Format

We extend the syntax of MessageFormat and will therefore refer to its productions in this syntax definition. Also, please note that rule files are white space sensitive, i.e. we explicitly mention where spaces are permitted.

Every non-black, non-comment line in a rule file defines a rewrite rule:

rule:
    pattern = MessageFormatPattern
    pattern space = space MessageFormatPattern
When asked to look up a key, the bundle finds the last rule whose pattern matches the key, and uses the rule's MessageFormatPattern to format the result.
pattern:
    qualifier
    pattern . qualifier

qualifier:
    literal
    { variableName }

variableName:
    String
A key is said to match a rule if it matches the rule's pattern. It matches the pattern if, after it is split up in its different qualifiers, every qualifier of the key matches the qualifier of the pattern. If the pattern's qualifier is a literal, the key's qualifier matches only if it is identical. If the pattern's qualifier is a variable name, this defines a variable and assigns the key's qualifier to it.

For instance, the key "ch.elca.el4j" matches the patterns "ch.elca.el4j" and "ch.{company}.el4j". In the latter case, the variable "company" contains "elca" after the matching. The key does not match "tv.elca.el4j" as the first qualifier does not match.

FormatElement: (in addition to inherited members)
    { variableName }
    { MessageFormatPattern }
where the MessageFormatPattern must not start with a digit. The former addition asks the message format to insert the value of the variable, the latter asks to insert the value associated with the key obtained by evaluating the MessageFormatPattern.

For instance, if we look up "earth.poetic" in the rule source

{planet}.name = {planet}
earth.name = home
{planet}.poetic = A piece of rock called {{planet}.name}. *
we get "A piece of rock called home.". If we look up "Mars.poetic" instead, we get "A piece of rock called Mars."

Author:
Adrian Moos (AMS)
File-location:
MessageRewriter
Last check-in date:
2010-01-05 09:38:21 +0100 (Di, 05. Jan 2010) by jonasha for revision 4068

Nested Class Summary
static class MessageRewriter.NoDefinitionException
          thrown to indicate that there is no matching rule for the requested key.
static interface MessageRewriter.Rule
          Represents a rewriting rule.
 
Constructor Summary
MessageRewriter(List<Reader> readers)
          Constructor.
 
Method Summary
 void add(List<? extends MessageRewriter.Rule> rules)
          Adds the provided rules to the list of rewriting rules used.
 void add(MessageRewriter.Rule... rules)
          Adds the provided rules to the list of rewriting rules used.
static String[] qualifiers(String s)
          returns the qualifiers in s, i.e. the list of maximal substrings not containing '.'.
 String resolve(String key)
          Like resolve(String, Object[]) with empty argument list.
 String resolve(String key, Object[] arguments)
          returns the value associated with the given key, using the supplied arguments, or throws a NoDefinitionException, if the key could not be resolved.
 StringBuffer resolveAndAppend(String key, Object[] arguments, StringBuffer target)
          resolves the value of the provided key with the provided user arguments and appends the result to target, or throws a MessageRewriter.NoDefinitionException if no rule applies to the key.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MessageRewriter

public MessageRewriter(List<Reader> readers)
Constructor.

Parameters:
readers - readers providing the rule lists to be used. The lists are locically concatenated in the order they were provided.
Method Detail

qualifiers

public static String[] qualifiers(String s)
returns the qualifiers in s, i.e. the list of maximal substrings not containing '.'.

Parameters:
s - the fully qualified name
Returns:
.

add

public void add(MessageRewriter.Rule... rules)
Adds the provided rules to the list of rewriting rules used.


add

public void add(List<? extends MessageRewriter.Rule> rules)
Adds the provided rules to the list of rewriting rules used.


resolveAndAppend

public StringBuffer resolveAndAppend(String key,
                                     Object[] arguments,
                                     StringBuffer target)
resolves the value of the provided key with the provided user arguments and appends the result to target, or throws a MessageRewriter.NoDefinitionException if no rule applies to the key.

Parameters:
key - .
arguments - the user arguments. must not be null, but may be empty.
target - .
Returns:
target

resolve

public String resolve(String key,
                      Object[] arguments)
returns the value associated with the given key, using the supplied arguments, or throws a NoDefinitionException, if the key could not be resolved.


resolve

public String resolve(String key)
Like resolve(String, Object[]) with empty argument list.



Copyright © 2005-2011 ELCA. All Rights Reserved.