2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
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;
42 import java.util.TimeZone;
44 import mir.generator.Generator;
45 import mir.generator.GeneratorExc;
46 import mir.generator.GeneratorFailure;
47 import mir.misc.StringUtil;
49 public class GeneratorFormatAdapters {
50 public static class NumberFormatAdapter {
53 public NumberFormatAdapter(Number aValue) {
57 public Generator.GeneratorFunction getFormat() {
58 return new NumberFormattingFunction();
61 private class NumberFormattingFunction implements Generator.GeneratorFunction {
62 public Object perform(List aParameters) throws GeneratorExc, GeneratorFailure {
64 if (aParameters.size() != 1 || ! (aParameters.get(0)instanceof String))
65 throw new GeneratorExc("NumberFormattingFunction <format> : exactly 1 string parameter expected");
67 return new DecimalFormat( (String) (aParameters.get(0))).format(value);
69 catch (GeneratorExc e) {
73 throw new GeneratorFailure("NumberFormattingFunction: " + t.getMessage(), t);
79 public static class DateFormatAdapter {
81 private TimeZone defaultTimezone;
82 private String defaultTimezoneName;
84 public DateFormatAdapter(Date aValue, String aDefaultTimezone) {
86 defaultTimezoneName = aDefaultTimezone;
87 defaultTimezone = null;
90 private TimeZone getDefaultTimezone() {
91 if (defaultTimezone == null) {
93 defaultTimezone = TimeZone.getTimeZone(defaultTimezoneName);
98 if (defaultTimezone==null)
99 defaultTimezone = TimeZone.getDefault();
102 return defaultTimezone;
105 public Generator.GeneratorFunction getFormat() {
106 return new DateFormattingFunction();
109 public Map getFormatted() {
110 return new DateToMapAdapter();
113 public Date getDate() {
117 private class DateFormattingFunction implements Generator.GeneratorFunction {
118 public Object perform(List aParameters) throws GeneratorExc, GeneratorFailure {
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");
124 SimpleDateFormat dateFormat = new SimpleDateFormat( (String) (aParameters.get(0)));
126 TimeZone timezone = null;
127 if (aParameters.size() > 1) {
129 timezone = TimeZone.getTimeZone( (String) aParameters.get(1));
131 catch (Throwable t) {
135 if (timezone == null)
136 timezone = getDefaultTimezone();
138 dateFormat.setTimeZone(timezone);
140 return dateFormat.format(value);
142 catch (GeneratorExc e) {
145 catch (Throwable t) {
146 throw new GeneratorFailure("DateFormattingFunction: " + t.getMessage(), t);
153 * retained for backwards compatibility
156 * <p>Description: </p>
157 * <p>Copyright: Copyright (c) 2003</p>
159 * @author not attributable
163 public class DateToMapAdapter extends AbstractMap {
164 public Object get(Object aKey) {
165 if (aKey instanceof String) {
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);
175 DateFormat dateFormat = new SimpleDateFormat( (String) aKey);
176 dateFormat.setTimeZone(getDefaultTimezone());
178 return dateFormat.format(value);
181 catch (Throwable t) {
182 throw new RuntimeException("Can't format date with format " + (String) aKey + ": " + t.getMessage());
189 public Set entrySet() {
190 return new HashSet();