1 package org.apache.slide.projector.processor.core; 2 3 import java.text.DateFormat ; 4 import java.util.Locale ; 5 import java.util.Map ; 6 7 import org.apache.slide.projector.ContentType; 8 import org.apache.slide.projector.Context; 9 import org.apache.slide.projector.Processor; 10 import org.apache.slide.projector.Result; 11 import org.apache.slide.projector.Store; 12 import org.apache.slide.projector.descriptor.DateValueDescriptor; 13 import org.apache.slide.projector.descriptor.LocaleValueDescriptor; 14 import org.apache.slide.projector.descriptor.ParameterDescriptor; 15 import org.apache.slide.projector.descriptor.RequiredEnvironmentDescriptor; 16 import org.apache.slide.projector.descriptor.ResultDescriptor; 17 import org.apache.slide.projector.descriptor.ResultEntryDescriptor; 18 import org.apache.slide.projector.descriptor.StateDescriptor; 19 import org.apache.slide.projector.descriptor.StringValueDescriptor; 20 import org.apache.slide.projector.i18n.DefaultMessage; 21 import org.apache.slide.projector.i18n.LocalizedMessage; 22 import org.apache.slide.projector.i18n.ParameterMessage; 23 import org.apache.slide.projector.processor.SimpleProcessor; 24 import org.apache.slide.projector.value.DateValue; 25 import org.apache.slide.projector.value.LocaleValue; 26 import org.apache.slide.projector.value.StringValue; 27 28 public class DateFormatter implements Processor { 29 private final static String DATE_FORMAT = "dateFormat"; 30 private final static String TIME_FORMAT = "timeFormat"; 31 private final static String DATE = "date"; 32 private final static String LOCALE = "locale"; 33 private final static String HIDE = "hide"; 34 private final static String SHORT = "short"; 35 private final static String MEDIUM = "medium"; 36 private final static String LONG = "long"; 37 private final static String FULL = "full"; 38 private final static String [] STYLES = new String [] { HIDE, SHORT, MEDIUM, LONG, FULL }; 39 40 private final static ParameterDescriptor[] parameterDescriptors = new ParameterDescriptor[]{ 41 new ParameterDescriptor(DATE, new ParameterMessage("dateFormatter/parameter/date"), new DateValueDescriptor()), 42 new ParameterDescriptor(DATE_FORMAT, new ParameterMessage("dateFormatter/parameter/dateFormat"), new StringValueDescriptor(STYLES), new StringValue(SHORT)), 43 new ParameterDescriptor(TIME_FORMAT, new ParameterMessage("dateFormatter/parameter/timeFormat"), new StringValueDescriptor(STYLES), new StringValue(SHORT)), 44 }; 45 46 47 private final static ResultDescriptor resultDescriptor = new ResultDescriptor( 48 new StateDescriptor[]{ StateDescriptor.OK_DESCRIPTOR }, 49 new ResultEntryDescriptor[]{new ResultEntryDescriptor( 50 SimpleProcessor.OUTPUT, new LocalizedMessage("dateFormatter/result/output"), 51 ContentType.PLAIN_TEXT, true)}); 52 53 private final static RequiredEnvironmentDescriptor[] requiredEnvironmentDescriptors = new RequiredEnvironmentDescriptor[] { 54 new RequiredEnvironmentDescriptor(LOCALE, Store.SESSION, new DefaultMessage("message/requiredEnvironment/locale"), new LocaleValueDescriptor(), new LocaleValue(Locale.getDefault())) 55 }; 56 57 public Result process(Map parameters, Context context) throws Exception { 58 DateValue date = (DateValue)parameters.get(DATE); 59 String dateFormat = parameters.get(DATE_FORMAT).toString(); 60 String timeFormat = parameters.get(TIME_FORMAT).toString(); 61 Locale locale = ((LocaleValue)context.getStore(Store.SESSION).get(LOCALE)).getLocale(); 62 String formattedDate = getDateFormatter(dateFormat, timeFormat, locale).format(date.getDate()); 63 return new Result(StateDescriptor.OK, SimpleProcessor.OUTPUT, new StringValue(formattedDate)); 64 } 65 66 public ParameterDescriptor[] getParameterDescriptors() { 67 return parameterDescriptors; 68 } 69 70 public ResultDescriptor getResultDescriptor() { 71 return resultDescriptor; 72 } 73 74 private DateFormat getDateFormatter(String dateFormat, String timeFormat, Locale locale) { 75 if ( dateFormat.equals(HIDE) ) { 76 return DateFormat.getTimeInstance(getStyleByName(timeFormat), locale); 77 } else if ( timeFormat.equals(HIDE) ) { 78 return DateFormat.getDateInstance(getStyleByName(dateFormat), locale); 79 } else { 80 return DateFormat.getDateTimeInstance(getStyleByName(dateFormat), getStyleByName(timeFormat), locale); 81 } 82 } 83 84 private int getStyleByName(String styleName) { 85 if ( styleName.equals(SHORT) ) { 86 return DateFormat.SHORT; 87 } else if ( styleName.equals(MEDIUM) ) { 88 return DateFormat.MEDIUM; 89 } else if ( styleName.equals(LONG) ) { 90 return DateFormat.LONG; 91 } 92 return DateFormat.FULL; 93 } 94 } | Popular Tags |