1 package org.objectledge.i18n;
2
3 import java.util.HashMap;
4 import java.util.Locale;
5 import java.util.Map;
6
7 import org.jcontainer.dna.Configuration;
8 import org.jcontainer.dna.ConfigurationException;
9 import org.objectledge.utils.StringUtils;
10
11 /***
12 * The abstract formater component.
13 *
14 * @author <a href="mailto:dgajda@caltha.pl">Damian Gajda</a>
15 * @version $Id: AbstractFormatter.java,v 1.2 2006/04/24 09:50:49 rafal Exp $
16 */
17 public abstract class AbstractFormatter
18 {
19 /*** The i18n is used as default locale provider. */
20 protected I18n i18n;
21 /*** The locale map. */
22 protected Map<Locale,Map<String,String>> localeMap;
23 /*** The default locale patterns. */
24 protected Map<Locale,String> defaultPatterns;
25
26 /***
27 * No-arg ctor for mock object testing.
28 */
29 protected AbstractFormatter()
30 {
31 }
32
33 /***
34 * Component constructor.
35 *
36 * @param config the configuration.
37 * @param logger the logger.
38 * @param i18n used as default locale provider.
39 * @throws ConfigurationException if the component configuration is malformed.
40 */
41 public AbstractFormatter(Configuration config, I18n i18n)
42 throws ConfigurationException
43 {
44 this.i18n = i18n;
45 localeMap = new HashMap<Locale,Map<String,String>>();
46 defaultPatterns = new HashMap<Locale,String>();
47 Configuration[] locales = config.getChildren("locale");
48 for (int i = 0; i < locales.length; i++)
49 {
50 String name = locales[i].getAttribute("name");
51 String defaultPattern = locales[i].getAttribute("defaultPattern");
52 Configuration[] patterns = locales[i].getChildren("pattern");
53 Locale locale = StringUtils.getLocale(name);
54 Map<String,String> map = new HashMap<String,String>();
55 localeMap.put(locale, map);
56 defaultPatterns.put(locale, defaultPattern);
57 for (int j = 0; j < patterns.length; j++)
58 {
59 String patternName = patterns[j].getAttribute("name");
60 String patternValue = patterns[j].getAttribute("value", null);
61 if (patternValue == null)
62 {
63 patternValue = patterns[j].getValue();
64 }
65 map.put(patternName, patternValue);
66 }
67 }
68 }
69
70 /***
71 * Get the default pattern for locale.
72 *
73 * @param locale the locale.
74 * @return the default pattern name.
75 */
76 public String getDefaultPattern(Locale locale)
77 {
78 String pattern = defaultPatterns.get(locale);
79 if(pattern == null)
80 {
81 pattern = defaultPatterns.get(i18n.getDefaultLocale());
82 }
83 return pattern;
84 }
85
86 /***
87 * Retrieves the pattern value by it's name and locale.
88 * If pattern is not defined than pattern for default locale is retrieved.
89 * If pattern is not defined for default locale than <code>null</code> is returned.
90 *
91 * @param patternName the requested pattern name.
92 * @param locale the locale.
93 * @return value of the pattern.
94 */
95 protected String getPatternValue(String patternName, Locale locale)
96 {
97 Map<String,String> patterns = localeMap.get(locale);
98 if(patterns == null)
99 {
100 patterns = localeMap.get(i18n.getDefaultLocale());
101 }
102 if(patterns == null)
103 {
104 return null;
105 }
106 return patterns.get(patternName);
107 }
108
109 }