01: /*
02: * Created on Dec 3, 2004
03: */
04: package uk.org.ponder.messageutil;
05:
06: import uk.org.ponder.beanutil.BeanLocator;
07: import uk.org.ponder.beanutil.BeanResolver;
08:
09: /**
10: * Core interface supporting lookup of localised messages. Very similar to
11: * Spring's MessageSource, only with a greater flexibility of lookup, and the
12: * dependency on the Locale has been factored off into a request-scope
13: * dependency.
14: * <p/>
15: * MessageLocator also supports use as either a BeanLocator (hence messages may
16: * be addressed directly via EL as <code>#{messageLocator.message-key}</code>)
17: * or as BeanResolver (hence messages may be automatically fetched during
18: * component fixup stage in RSF by declaring the <code>resolver</code> field
19: * in a <code>UIBound</code> to be targetted at the EL
20: * <code>#{messageLocator}</code>.
21: *
22: * @author Antranig Basman (antranig@caret.cam.ac.uk)
23: *
24: */
25: public abstract class MessageLocator implements BeanLocator,
26: BeanResolver {
27: /**
28: * @see #getMessage(String, Object[])
29: * @param code An array of potential message codes to be looked up, in order
30: * of priority. Each code will be looked up in turn and the resolved message
31: * from the first which is found will be returned, if any. This strategy
32: * matches that of Spring's MessageSourceResolvable.
33: */
34: public abstract String getMessage(String[] code, Object[] args);
35:
36: /** Resolve a defaultible message which takes no arguments
37: * @see #getMessage(String[], Object[]) **/
38: public String getMessage(String[] code) {
39: return getMessage(code, null);
40: }
41:
42: public String getMessage(String code) {
43: return getMessage(code, (Object[]) null);
44: }
45:
46: /**
47: * @param code the code to lookup up, such as 'calculator.noRateSet'
48: * @param args Array of arguments that will be filled in for params within the
49: * message (params look like "{0}", "{1,date}", "{2,time}" within a
50: * message), or <code>null</code> if none.
51: * @return the resolved message, or a default message if not found.
52: */
53: public String getMessage(String code, Object[] args) {
54: return getMessage(new String[] { code }, args);
55: }
56:
57: public String getMessage(String[] code, Object param) {
58: return getMessage(code, new Object[] { param });
59: }
60:
61: public String getMessage(String code, Object param) {
62: return getMessage(new String[] { code }, new Object[] { param });
63: }
64:
65: /** @see BeanLocator#locateBean(String)
66: */
67: public Object locateBean(String path) {
68: return getMessage(new String[] { path }, null);
69: }
70:
71: /** @see BeanResolver#resolveBean(Object)
72: */
73:
74: public String resolveBean(Object bean) {
75: return getMessage((String) bean, null);
76: }
77:
78: }
|