1e5a55bf18966efba98d12a720c1c0c3a0408e23
[mir.git] / source / mir / util / GeneratorFormatAdapters.java
1 /*
2  * Copyright (C) 2001, 2002 The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with  any library licensed under the Apache Software License,
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23  * (or with modified versions of the above that use the same license as the above),
24  * and distribute linked combinations including the two.  You must obey the
25  * GNU General Public License in all respects for all of the code used other than
26  * the above mentioned libraries.  If you modify this file, you may extend this
27  * exception to your version of the file, but you are not obligated to do so.
28  * If you do not wish to do so, delete this exception statement from your version.
29  */
30 package mir.util;
31
32 import java.text.DateFormat;
33 import java.text.DecimalFormat;
34 import java.text.SimpleDateFormat;
35 import java.util.AbstractMap;
36 import java.util.Date;
37 import java.util.GregorianCalendar;
38 import java.util.HashSet;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Set;
42 import java.util.TimeZone;
43
44 import mir.generator.Generator;
45 import mir.generator.GeneratorExc;
46 import mir.generator.GeneratorFailure;
47 import mir.misc.StringUtil;
48
49 public class GeneratorFormatAdapters {
50   public static class NumberFormatAdapter {
51     private Number value;
52
53     public NumberFormatAdapter(Number aValue) {
54       value = aValue;
55     }
56
57     public Generator.GeneratorFunction getFormat() {
58       return new NumberFormattingFunction();
59     }
60
61     private class NumberFormattingFunction implements Generator.GeneratorFunction {
62       public Object perform(List aParameters) throws GeneratorExc, GeneratorFailure {
63         try {
64           if (aParameters.size() != 1 || ! (aParameters.get(0)instanceof String))
65             throw new GeneratorExc("NumberFormattingFunction <format> : exactly 1 string parameter expected");
66
67           return new DecimalFormat( (String) (aParameters.get(0))).format(value);
68         }
69         catch (GeneratorExc e) {
70           throw e;
71         }
72         catch (Throwable t) {
73           throw new GeneratorFailure("NumberFormattingFunction: " + t.getMessage(), t);
74         }
75       };
76     }
77   }
78
79   public static class DateFormatAdapter {
80     private Date value;
81     private TimeZone defaultTimezone;
82     private String defaultTimezoneName;
83
84     public DateFormatAdapter(Date aValue, String aDefaultTimezone) {
85       value = aValue;
86       defaultTimezoneName = aDefaultTimezone;
87       defaultTimezone = null;
88     }
89
90     private TimeZone getDefaultTimezone() {
91       if (defaultTimezone == null) {
92         try {
93           defaultTimezone = TimeZone.getTimeZone(defaultTimezoneName);
94         }
95         catch (Throwable t) {
96         }
97
98         if (defaultTimezone==null)
99           defaultTimezone = TimeZone.getDefault();
100       }
101
102       return defaultTimezone;
103     }
104
105     public Generator.GeneratorFunction getFormat() {
106       return new DateFormattingFunction();
107     }
108
109     public Map getFormatted() {
110       return new DateToMapAdapter();
111     }
112
113     public Date getDate() {
114       return value;
115     }
116
117     private class DateFormattingFunction implements Generator.GeneratorFunction {
118       public Object perform(List aParameters) throws GeneratorExc, GeneratorFailure {
119         try {
120           if (aParameters.size() < 1 || aParameters.size() > 2 ||
121               !(aParameters.get(0) instanceof String) || (aParameters.size()>1 && ! (aParameters.get(1) instanceof String)))
122             throw new GeneratorExc("DateFormattingFunction <format> [timezone]: 1 or 2 string parameters expected");
123
124           SimpleDateFormat dateFormat = new SimpleDateFormat( (String) (aParameters.get(0)));
125
126           TimeZone timezone = null;
127           if (aParameters.size() > 1) {
128             try  {
129               timezone = TimeZone.getTimeZone( (String) aParameters.get(1));
130             }
131             catch (Throwable t) {
132             }
133           }
134
135           if (timezone == null)
136             timezone = getDefaultTimezone();
137
138           dateFormat.setTimeZone(timezone);
139
140           return dateFormat.format(value);
141         }
142         catch (GeneratorExc e) {
143           throw e;
144         }
145         catch (Throwable t) {
146           throw new GeneratorFailure("DateFormattingFunction: " + t.getMessage(), t);
147         }
148       };
149     }
150
151     /**
152      *
153      * retained for backwards compatibility
154      *
155      * <p>Title: </p>
156      * <p>Description: </p>
157      * <p>Copyright: Copyright (c) 2003</p>
158      * <p>Company: </p>
159      * @author not attributable
160      * @version 1.0
161      */
162
163     public class DateToMapAdapter extends AbstractMap {
164       public Object get(Object aKey) {
165         if (aKey instanceof String) {
166           try {
167             // ML: quick fix to allow for the dc encoding now...
168             if ( ( (String) aKey).equals("dc")) {
169               GregorianCalendar calendar = new GregorianCalendar();
170               calendar.setTime(value);
171               calendar.setTimeZone(getDefaultTimezone());
172               return StringUtil.date2w3DateTime(calendar);
173             }
174             else {
175               DateFormat dateFormat = new SimpleDateFormat( (String) aKey);
176               dateFormat.setTimeZone(getDefaultTimezone());
177
178               return dateFormat.format(value);
179             }
180           }
181           catch (Throwable t) {
182             throw new RuntimeException("Can't format date with format " + (String) aKey + ": " + t.getMessage());
183           }
184         }
185         else
186           return null;
187       }
188
189       public Set entrySet() {
190         return new HashSet();
191       }
192     }
193   }
194 }