1:
31: package ;
32:
33: import ;
34: import ;
35: import ;
36: import ;
37: import ;
38: import ;
39: import ;
40: import ;
41:
42: import ;
43: import ;
44:
45:
50: public class DefaultLocalizationContext implements LocalizationContext
51: {
52: private static final String CONFIG_TIMEZONE_KEY = "org.jfree.formula.timezone";
53:
54: private static final String CONFIG_LOCALE_KEY = "org.jfree.formula.locale";
55:
56: private static final String CONFIG_DATEFORMAT_KEY = "org.jfree.formula.dateformat.";
57:
58: private List dateFormats;
59:
60: private List datetimeFormats;
61:
62: private List timeFormats;
63:
64: private Locale locale;
65:
66: private TimeZone timeZone;
67:
68: public DefaultLocalizationContext()
69: {
70: dateFormats = new ArrayList();
71: datetimeFormats = new ArrayList();
72: timeFormats = new ArrayList();
73: }
74:
75: public Locale getLocale()
76: {
77: return locale;
78: }
79:
80: public ResourceBundle getBundle(final String id)
81: {
82: return ResourceBundle.getBundle(id, getLocale());
83: }
84:
85: public TimeZone getTimeZone()
86: {
87: return timeZone;
88: }
89:
90: public List getDateFormats(final Type type)
91: {
92: if (type.isFlagSet(Type.DATE_TYPE))
93: {
94: return dateFormats;
95: }
96: else if (type.isFlagSet(Type.DATETIME_TYPE))
97: {
98: return datetimeFormats;
99: }
100: else if (type.isFlagSet(Type.TIME_TYPE))
101: {
102: return timeFormats;
103: }
104: return null;
105: }
106:
107: private String[] createLocale(final String locale)
108: {
109: final StringTokenizer strtok = new StringTokenizer(locale, "_");
110: final String[] retval = new String[3];
111: if (strtok.hasMoreElements())
112: {
113: retval[0] = strtok.nextToken();
114: }
115: if (strtok.hasMoreElements())
116: {
117: retval[1] = strtok.nextToken();
118: }
119: else
120: {
121: retval[1] = "";
122: }
123: if (strtok.hasMoreElements())
124: {
125: retval[2] = strtok.nextToken();
126: }
127: else
128: {
129: retval[2] = "";
130: }
131: return retval;
132: }
133:
134: private String[] createFormatSpec(final String text)
135: {
136: final StringTokenizer strtok = new StringTokenizer(text, ".");
137: if (strtok.countTokens() == 2)
138: {
139: final String[] retval = new String[2];
140: retval[0] = strtok.nextToken();
141: retval[1] = strtok.nextToken();
142: return retval;
143: }
144: return null;
145: }
146:
147:
148: public void initialize(final Configuration config)
149: {
150:
151: final String declaredLocale = config.getConfigProperty(CONFIG_LOCALE_KEY, Locale.getDefault().toString());
152: final String[] declaredLocaleParts = createLocale(declaredLocale);
153: this.locale = new Locale(declaredLocaleParts[0], declaredLocaleParts[1], declaredLocaleParts[2]);
154:
155:
156: final String timeZoneId = config.getConfigProperty(CONFIG_TIMEZONE_KEY, TimeZone.getDefault().getID());
157: timeZone = TimeZone.getTimeZone(timeZoneId);
158:
159:
160: final Iterator formatKeys = config.findPropertyKeys(CONFIG_DATEFORMAT_KEY);
161: while (formatKeys.hasNext())
162: {
163: final String formatKey = (String) formatKeys.next();
164:
165: final String format = config.getConfigProperty(formatKey);
166:
167:
168: final String keySpec = formatKey.substring(CONFIG_DATEFORMAT_KEY.length(), formatKey.length());
169: final String[] formatSpec = createFormatSpec(keySpec);
170: if (formatSpec != null)
171: {
172: final String type = "type."+formatSpec[0];
173: final String[] locale = createLocale(formatSpec[1]);
174:
175: final SimpleDateFormat df = new SimpleDateFormat(format, new Locale(locale[0], locale[1], locale[2]));
176:
177: if (Type.TIME_TYPE.equals(type))
178: {
179: timeFormats.add(df);
180: }
181: else if (Type.DATE_TYPE.equals(type))
182: {
183: dateFormats.add(df);
184: }
185: else if (Type.DATETIME_TYPE.equals(type))
186: {
187: datetimeFormats.add(df);
188: }
189: }
190: }
191:
192:
193: datetimeFormats.add(SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT,
194: SimpleDateFormat.SHORT, getLocale()));
195: dateFormats.add(SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT,
196: getLocale()));
197: timeFormats.add(SimpleDateFormat.getTimeInstance(SimpleDateFormat.SHORT,
198: getLocale()));
199:
200:
201: datetimeFormats.add(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.US));
202: dateFormats.add(new SimpleDateFormat("yyyy-MM-dd", Locale.US));
203: timeFormats.add(new SimpleDateFormat("hh:mm:ss", Locale.US));
204: }
205: }