1.1 restoration
[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.Function getFormat() {
58       return new NumberFormattingFunction();
59     }
60
61     private class NumberFormattingFunction implements Generator.Function {
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) {
85       this(aValue, "");
86     }
87
88     public DateFormatAdapter(Date aValue, String aDefaultTimezone) {
89       value = aValue;
90       defaultTimezoneName = aDefaultTimezone;
91       defaultTimezone = null;
92     }
93
94     private TimeZone getDefaultTimezone() {
95       if (defaultTimezone == null) {
96         try {
97           defaultTimezone = TimeZone.getTimeZone(defaultTimezoneName);
98         }
99         catch (Throwable t) {
100         }
101
102         if (defaultTimezone==null)
103           defaultTimezone = TimeZone.getDefault();
104       }
105
106       return defaultTimezone;
107     }
108
109     public Generator.Function getFormat() {
110       return new DateFormattingFunction();
111     }
112
113     public Map getFormatted() {
114       return new DateToMapAdapter();
115     }
116
117     public Date getDate() {
118       return value;
119     }
120
121     private class DateFormattingFunction implements Generator.Function {
122       public Object perform(List aParameters) throws GeneratorExc, GeneratorFailure {
123         try {
124           if (aParameters.size() < 1 || aParameters.size() > 2 ||
125               !(aParameters.get(0) instanceof String) || (aParameters.size()>1 && ! (aParameters.get(1) instanceof String)))
126             throw new GeneratorExc("DateFormattingFunction <format> [timezone]: 1 or 2 string parameters expected");
127
128           SimpleDateFormat dateFormat = new SimpleDateFormat( (String) (aParameters.get(0)));
129
130           TimeZone timezone = null;
131           if (aParameters.size() > 1) {
132             try  {
133               timezone = TimeZone.getTimeZone( (String) aParameters.get(1));
134             }
135             catch (Throwable t) {
136             }
137           }
138
139           if (timezone == null)
140             timezone = getDefaultTimezone();
141
142           dateFormat.setTimeZone(timezone);
143
144           return dateFormat.format(value);
145         }
146         catch (GeneratorExc e) {
147           throw e;
148         }
149         catch (Throwable t) {
150           throw new GeneratorFailure("DateFormattingFunction: " + t.getMessage(), t);
151         }
152       };
153     }
154
155     /**
156      *
157      * retained for backwards compatibility
158      *
159      * <p>Title: </p>
160      * <p>Description: </p>
161      * <p>Copyright: Copyright (c) 2003</p>
162      * <p>Company: </p>
163      * @author not attributable
164      * @version 1.0
165      */
166
167     public class DateToMapAdapter extends AbstractMap {
168       public Object get(Object aKey) {
169         if (aKey instanceof String) {
170           try {
171             // ML: quick fix to allow for the dc encoding now...
172             if ( ( (String) aKey).equals("dc")) {
173               GregorianCalendar calendar = new GregorianCalendar();
174               calendar.setTime(value);
175               calendar.setTimeZone(getDefaultTimezone());
176               return StringUtil.date2w3DateTime(calendar);
177             }
178             else {
179               DateFormat dateFormat = new SimpleDateFormat( (String) aKey);
180               dateFormat.setTimeZone(getDefaultTimezone());
181
182               return dateFormat.format(value);
183             }
184           }
185           catch (Throwable t) {
186             throw new RuntimeException("Can't format date with format " + (String) aKey + ": " + t.getMessage());
187           }
188         }
189         else
190           return null;
191       }
192
193       public Set entrySet() {
194         return new HashSet();
195       }
196     }
197   }
198 }