670ca67c7cd5a56de2b7871dbbdf61a0afa22ebe
[mir.git] / source / mir / util / GeneratorFormatAdapters.java
1 package mir.util;
2
3 import java.text.DateFormat;
4 import java.text.DecimalFormat;
5 import java.text.SimpleDateFormat;
6 import java.util.AbstractMap;
7 import java.util.Date;
8 import java.util.GregorianCalendar;
9 import java.util.HashSet;
10 import java.util.List;
11 import java.util.Map;
12 import java.util.Set;
13 import java.util.TimeZone;
14
15 import mir.generator.Generator;
16 import mir.generator.GeneratorExc;
17 import mir.generator.GeneratorFailure;
18 import mir.misc.StringUtil;
19
20 public class GeneratorFormatAdapters {
21   public static class NumberFormatAdapter {
22     private Number value;
23
24     public NumberFormatAdapter(Number aValue) {
25       value = aValue;
26     }
27
28     public Generator.GeneratorFunction getFormat() {
29       return new NumberFormattingFunction();
30     }
31
32     private class NumberFormattingFunction implements Generator.GeneratorFunction {
33       public Object perform(List aParameters) throws GeneratorExc, GeneratorFailure {
34         try {
35           if (aParameters.size() != 1 || ! (aParameters.get(0)instanceof String))
36             throw new GeneratorExc("NumberFormattingFunction <format> : exactly 1 string parameter expected");
37
38           return new DecimalFormat( (String) (aParameters.get(0))).format(value);
39         }
40         catch (GeneratorExc e) {
41           throw e;
42         }
43         catch (Throwable t) {
44           throw new GeneratorFailure("NumberFormattingFunction: " + t.getMessage(), t);
45         }
46       };
47     }
48   }
49
50   public static class DateFormatAdapter {
51     private Date value;
52     private TimeZone defaultTimezone;
53     private String defaultTimezoneName;
54
55     public DateFormatAdapter(Date aValue, String aDefaultTimezone) {
56       value = aValue;
57       defaultTimezoneName = aDefaultTimezone;
58       defaultTimezone = null;
59     }
60
61     private TimeZone getDefaultTimezone() {
62       if (defaultTimezone == null) {
63         try {
64           defaultTimezone = TimeZone.getTimeZone(defaultTimezoneName);
65         }
66         catch (Throwable t) {
67         }
68
69         if (defaultTimezone==null)
70           defaultTimezone = TimeZone.getDefault();
71       }
72
73       return defaultTimezone;
74     }
75
76     public Generator.GeneratorFunction getFormat() {
77       return new DateFormattingFunction();
78     }
79
80     public Map getFormatted() {
81       return new DateToMapAdapter();
82     }
83
84     public Date getDate() {
85       return value;
86     }
87
88     private class DateFormattingFunction implements Generator.GeneratorFunction {
89       public Object perform(List aParameters) throws GeneratorExc, GeneratorFailure {
90         try {
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");
94
95           SimpleDateFormat dateFormat = new SimpleDateFormat( (String) (aParameters.get(0)));
96
97           TimeZone timezone = null;
98           if (aParameters.size() > 1) {
99             try  {
100               timezone = TimeZone.getTimeZone( (String) aParameters.get(1));
101             }
102             catch (Throwable t) {
103
104             }
105           }
106
107           if (timezone == null)
108             timezone = getDefaultTimezone();
109
110           dateFormat.setTimeZone(timezone);
111
112           return dateFormat.format(value);
113         }
114         catch (GeneratorExc e) {
115           throw e;
116         }
117         catch (Throwable t) {
118           throw new GeneratorFailure("DateFormattingFunction: " + t.getMessage(), t);
119         }
120       };
121     }
122
123     /**
124      *
125      * retained for backwards compatibility
126      *
127      * <p>Title: </p>
128      * <p>Description: </p>
129      * <p>Copyright: Copyright (c) 2003</p>
130      * <p>Company: </p>
131      * @author not attributable
132      * @version 1.0
133      */
134
135     public class DateToMapAdapter extends AbstractMap {
136       public Object get(Object aKey) {
137         if (aKey instanceof String) {
138           try {
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);
145             }
146             else {
147               DateFormat dateFormat = new SimpleDateFormat( (String) aKey);
148               dateFormat.setTimeZone(getDefaultTimezone());
149
150               return dateFormat.format(value);
151             }
152           }
153           catch (Throwable t) {
154             throw new RuntimeException("Can't format date with format " + (String) aKey + ": " + t.getMessage());
155           }
156         }
157         else
158           return null;
159       }
160
161       public Set entrySet() {
162         return new HashSet();
163       }
164     }
165
166   }
167 }