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) {
88 public DateFormatAdapter(Date aValue, String aDefaultTimezone) {
90 defaultTimezoneName = aDefaultTimezone;
91 defaultTimezone = null;
94 private TimeZone getDefaultTimezone() {
95 if (defaultTimezone == null) {
97 defaultTimezone = TimeZone.getTimeZone(defaultTimezoneName);
102 if (defaultTimezone==null)
103 defaultTimezone = TimeZone.getDefault();
106 return defaultTimezone;
109 public Generator.GeneratorFunction getFormat() {
110 return new DateFormattingFunction();
113 public Map getFormatted() {
114 return new DateToMapAdapter();
117 public Date getDate() {
121 private class DateFormattingFunction implements Generator.GeneratorFunction {
122 public Object perform(List aParameters) throws GeneratorExc, GeneratorFailure {
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");
128 SimpleDateFormat dateFormat = new SimpleDateFormat( (String) (aParameters.get(0)));
130 TimeZone timezone = null;
131 if (aParameters.size() > 1) {
133 timezone = TimeZone.getTimeZone( (String) aParameters.get(1));
135 catch (Throwable t) {
139 if (timezone == null)
140 timezone = getDefaultTimezone();
142 dateFormat.setTimeZone(timezone);
144 return dateFormat.format(value);
146 catch (GeneratorExc e) {
149 catch (Throwable t) {
150 throw new GeneratorFailure("DateFormattingFunction: " + t.getMessage(), t);
157 * retained for backwards compatibility
160 * <p>Description: </p>
161 * <p>Copyright: Copyright (c) 2003</p>
163 * @author not attributable
167 public class DateToMapAdapter extends AbstractMap {
168 public Object get(Object aKey) {
169 if (aKey instanceof String) {
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);
179 DateFormat dateFormat = new SimpleDateFormat( (String) aKey);
180 dateFormat.setTimeZone(getDefaultTimezone());
182 return dateFormat.format(value);
185 catch (Throwable t) {
186 throw new RuntimeException("Can't format date with format " + (String) aKey + ": " + t.getMessage());
193 public Set entrySet() {
194 return new HashSet();