3ef833ffeac32374e679e70f93ca87cfa8e4be0e
[mir.git] / source / mir / misc / MessageMethodModel.java
1 package mir.misc;
2
3 import freemarker.template.SimpleScalar;
4 import freemarker.template.TemplateMethodModel;
5 import freemarker.template.TemplateModel;
6 import org.apache.struts.util.MessageResources;
7
8 import java.util.List;
9 import java.util.Locale;
10
11 /**
12  * A FreeMarker <code>TemplateMethodModel</code> that provides access to a
13  * Struts <code>MessageResources</code>, for use in Interantionalized templates.
14  *
15  * @author Kellan <kellan@protest.net>
16  */
17
18 public class MessageMethodModel implements TemplateMethodModel {
19
20     /**
21      * The perferred locale for this instance of MessageMethod.
22      */
23     private Locale locale;
24
25     /**
26      * The MessageResources to query, a single instance shared for
27      * the lifetime of servlet.
28      */
29     private MessageResources messages;
30
31
32     /**
33      * Construct a MessageMethod that uses the JVM's default locale.
34      *
35      * @param message The MessageResources object to query
36      */
37     public MessageMethodModel(MessageResources messages) {
38         this(null, messages);
39     }
40
41     /**
42      * Construct a MessageMethod
43      *
44      * @param locale a Locale object, persumably initialized
45      *               from users Accept-Language header field
46      *
47      * @param message The MessageResources object to query
48      */
49     public MessageMethodModel(Locale locale, MessageResources messages) {
50         this.locale = locale;
51         this.messages = messages;
52     }
53
54
55     /**
56      * Takes the first argument as a resource key, then looks up
57      * a string in the MessagesResources, based on that key, and the Locale
58      *
59      * TODO: error messages should be i18n :)
60      *
61      * @param arguments List passed in by FM, first arguement is a string used as the key
62      *                  all subsequent arguments are used as described in MessageResources
63      *                  (they are filled into the placehoders of the string being returned)
64      */
65     public TemplateModel exec(List arguments) {
66         if (arguments != null) {
67             String key = (String) arguments.get(0);
68             arguments.remove(0);
69             String mesg = messages.getMessage(locale, key, arguments.toArray());
70
71             if (mesg == null) {
72                 return new SimpleScalar(errUnknownTag+key);
73             }
74             return new SimpleScalar(mesg);
75         }
76         else {
77             return missingKeyScalar;
78         }
79     }
80
81     // i'm not real clear on how this is used - kellan :)
82     public boolean isEmpty() {
83         if (messages == null)
84             return true;
85         else
86             return false;
87     }
88
89     private static String errUnknownTag = "MESSAGE NOT FOUND: ";
90     private static String missingKey = "MESSAGE CALL WITHOUT KEY";
91     private static SimpleScalar missingKeyScalar =
92             new SimpleScalar(missingKey);
93 }