3 import java.text.DateFormat;
4 import java.text.DecimalFormat;
5 import java.text.SimpleDateFormat;
6 import java.util.AbstractMap;
8 import java.util.GregorianCalendar;
9 import java.util.HashSet;
10 import java.util.List;
13 import java.util.TimeZone;
15 import mir.generator.Generator;
16 import mir.generator.GeneratorExc;
17 import mir.generator.GeneratorFailure;
18 import mir.misc.StringUtil;
20 public class GeneratorFormatAdapters {
21 public static class NumberFormatAdapter {
24 public NumberFormatAdapter(Number aValue) {
28 public Generator.GeneratorFunction getFormat() {
29 return new NumberFormattingFunction();
32 private class NumberFormattingFunction implements Generator.GeneratorFunction {
33 public Object perform(List aParameters) throws GeneratorExc, GeneratorFailure {
35 if (aParameters.size() != 1 || ! (aParameters.get(0)instanceof String))
36 throw new GeneratorExc("NumberFormattingFunction <format> : exactly 1 string parameter expected");
38 return new DecimalFormat( (String) (aParameters.get(0))).format(value);
40 catch (GeneratorExc e) {
44 throw new GeneratorFailure("NumberFormattingFunction: " + t.getMessage(), t);
50 public static class DateFormatAdapter {
52 private TimeZone defaultTimezone;
53 private String defaultTimezoneName;
55 public DateFormatAdapter(Date aValue, String aDefaultTimezone) {
57 defaultTimezoneName = aDefaultTimezone;
58 defaultTimezone = null;
61 private TimeZone getDefaultTimezone() {
62 if (defaultTimezone == null) {
64 defaultTimezone = TimeZone.getTimeZone(defaultTimezoneName);
69 if (defaultTimezone==null)
70 defaultTimezone = TimeZone.getDefault();
73 return defaultTimezone;
76 public Generator.GeneratorFunction getFormat() {
77 return new DateFormattingFunction();
80 public Map getFormatted() {
81 return new DateToMapAdapter();
84 public Date getDate() {
88 private class DateFormattingFunction implements Generator.GeneratorFunction {
89 public Object perform(List aParameters) throws GeneratorExc, GeneratorFailure {
91 if (aParameters.size() < 1 || aParameters.size() > 2 ||
92 !(aParameters.get(0) instanceof String) || (aParameters.size()>1 && ! (aParameters.get(1) instanceof String)))
93 throw new GeneratorExc("DateFormattingFunction <format> [timezone]: 1 or 2 string parameters expected");
95 SimpleDateFormat dateFormat = new SimpleDateFormat( (String) (aParameters.get(0)));
97 TimeZone timezone = null;
98 if (aParameters.size() > 1) {
100 timezone = TimeZone.getTimeZone( (String) aParameters.get(1));
102 catch (Throwable t) {
107 if (timezone == null)
108 timezone = getDefaultTimezone();
110 dateFormat.setTimeZone(timezone);
112 return dateFormat.format(value);
114 catch (GeneratorExc e) {
117 catch (Throwable t) {
118 throw new GeneratorFailure("DateFormattingFunction: " + t.getMessage(), t);
125 * retained for backwards compatibility
128 * <p>Description: </p>
129 * <p>Copyright: Copyright (c) 2003</p>
131 * @author not attributable
135 public class DateToMapAdapter extends AbstractMap {
136 public Object get(Object aKey) {
137 if (aKey instanceof String) {
139 // ML: quick fix to allow for the dc encoding now...
140 if ( ( (String) aKey).equals("dc")) {
141 GregorianCalendar calendar = new GregorianCalendar();
142 calendar.setTime(value);
143 calendar.setTimeZone(getDefaultTimezone());
144 return StringUtil.date2w3DateTime(calendar);
147 DateFormat dateFormat = new SimpleDateFormat( (String) aKey);
148 dateFormat.setTimeZone(getDefaultTimezone());
150 return dateFormat.format(value);
153 catch (Throwable t) {
154 throw new RuntimeException("Can't format date with format " + (String) aKey + ": " + t.getMessage());
161 public Set entrySet() {
162 return new HashSet();