package mir.util;
-import java.util.*;
-import java.text.*;
-import mir.generator.*;
+import java.text.DateFormat;
+import java.text.DecimalFormat;
+import java.text.SimpleDateFormat;
+import java.util.AbstractMap;
+import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TimeZone;
+
+import mir.generator.Generator;
+import mir.generator.GeneratorExc;
+import mir.generator.GeneratorFailure;
+import mir.misc.StringUtil;
public class GeneratorFormatAdapters {
public static class NumberFormatAdapter {
}
public Generator.GeneratorFunction getFormat() {
- return new NumberFormattingFunction(value);
+ return new NumberFormattingFunction();
}
- private static class NumberFormattingFunction implements Generator.GeneratorFunction {
- private Number value;
-
- public NumberFormattingFunction(Number aValue) {
- value = aValue;
- }
-
+ private class NumberFormattingFunction implements Generator.GeneratorFunction {
public Object perform(List aParameters) throws GeneratorExc, GeneratorFailure {
try {
- if (aParameters.size()!=1 || !(aParameters.get(0) instanceof String) )
+ if (aParameters.size() != 1 || ! (aParameters.get(0)instanceof String))
throw new GeneratorExc("NumberFormattingFunction <format> : exactly 1 string parameter expected");
- return new DecimalFormat((String) (aParameters.get(0))).format(value);
+ return new DecimalFormat( (String) (aParameters.get(0))).format(value);
}
catch (GeneratorExc e) {
throw e;
}
}
+ public static class DateFormatAdapter {
+ private Date value;
+ private TimeZone defaultTimezone;
+ private String defaultTimezoneName;
+
+ public DateFormatAdapter(Date aValue, String aDefaultTimezone) {
+ value = aValue;
+ defaultTimezoneName = aDefaultTimezone;
+ defaultTimezone = null;
+ }
+
+ private TimeZone getDefaultTimezone() {
+ if (defaultTimezone == null) {
+ try {
+ defaultTimezone = TimeZone.getTimeZone(defaultTimezoneName);
+ }
+ catch (Throwable t) {
+ }
+
+ if (defaultTimezone==null)
+ defaultTimezone = TimeZone.getDefault();
+ }
+
+ return defaultTimezone;
+ }
+
+ public Generator.GeneratorFunction getFormat() {
+ return new DateFormattingFunction();
+ }
+
+ public Map getFormatted() {
+ return new DateToMapAdapter();
+ }
+
+ public Date getDate() {
+ return value;
+ }
+
+ private class DateFormattingFunction implements Generator.GeneratorFunction {
+ public Object perform(List aParameters) throws GeneratorExc, GeneratorFailure {
+ try {
+ if (aParameters.size() < 1 || aParameters.size() > 2 ||
+ !(aParameters.get(0) instanceof String) || (aParameters.size()>1 && ! (aParameters.get(1) instanceof String)))
+ throw new GeneratorExc("DateFormattingFunction <format> [timezone]: 1 or 2 string parameters expected");
+
+ SimpleDateFormat dateFormat = new SimpleDateFormat( (String) (aParameters.get(0)));
+
+ TimeZone timezone = null;
+ if (aParameters.size() > 1) {
+ try {
+ timezone = TimeZone.getTimeZone( (String) aParameters.get(1));
+ }
+ catch (Throwable t) {
+
+ }
+ }
+
+ if (timezone == null)
+ timezone = getDefaultTimezone();
+
+ dateFormat.setTimeZone(timezone);
+
+ return dateFormat.format(value);
+ }
+ catch (GeneratorExc e) {
+ throw e;
+ }
+ catch (Throwable t) {
+ throw new GeneratorFailure("DateFormattingFunction: " + t.getMessage(), t);
+ }
+ };
+ }
+
+ /**
+ *
+ * retained for backwards compatibility
+ *
+ * <p>Title: </p>
+ * <p>Description: </p>
+ * <p>Copyright: Copyright (c) 2003</p>
+ * <p>Company: </p>
+ * @author not attributable
+ * @version 1.0
+ */
+
+ public class DateToMapAdapter extends AbstractMap {
+ public Object get(Object aKey) {
+ if (aKey instanceof String) {
+ try {
+ // ML: quick fix to allow for the dc encoding now...
+ if ( ( (String) aKey).equals("dc")) {
+ GregorianCalendar calendar = new GregorianCalendar();
+ calendar.setTime(value);
+ calendar.setTimeZone(defaultTimezone);
+ return StringUtil.date2w3DateTime(calendar);
+ }
+ else {
+ DateFormat dateFormat = new SimpleDateFormat( (String) aKey);
+ dateFormat.setTimeZone(defaultTimezone);
+
+ return dateFormat.format(value);
+ }
+ }
+ catch (Throwable t) {
+ throw new RuntimeException("Can't format date with format " + (String) aKey + ": " + t.getMessage());
+ }
+ }
+ else
+ return null;
+ }
+
+ public Set entrySet() {
+ return new HashSet();
+ }
+ }
+
+ }
}
\ No newline at end of file