1 16 package org.apache.cocoon.forms.datatype.convertor; 17 18 import org.xml.sax.ContentHandler ; 19 import org.xml.sax.SAXException ; 20 import org.apache.cocoon.forms.FormsConstants; 21 import org.apache.cocoon.xml.AttributesImpl; 22 23 import java.util.Locale ; 24 import java.util.Date ; 25 import java.text.DateFormat ; 26 import java.text.ParseException ; 27 import java.text.SimpleDateFormat ; 28 29 44 public class FormattingDateConvertor implements Convertor { 45 46 private int style; 47 48 private String variant; 49 50 private LocaleMap localizedPatterns; 51 52 private String nonLocalizedPattern; 53 54 public static final String DATE = "date"; 55 public static final String TIME = "time"; 56 public static final String DATE_TIME = "datetime"; 57 58 public FormattingDateConvertor() { 59 this.style = DateFormat.SHORT; 60 this.variant = DATE; 61 this.localizedPatterns = new LocaleMap(); 62 } 63 64 public ConversionResult convertFromString(String value, Locale locale, Convertor.FormatCache formatCache) { 65 SimpleDateFormat dateFormat = getDateFormat(locale, formatCache); 66 try { 67 return new ConversionResult(dateFormat.parse(value)); 68 } catch (ParseException e) { 69 return ConversionResult.create("date." + this.variant); 70 } 71 } 72 73 public String convertToString(Object value, Locale locale, Convertor.FormatCache formatCache) { 74 SimpleDateFormat dateFormat = getDateFormat(locale, formatCache); 75 return dateFormat.format((Date )value); 76 } 77 78 private final SimpleDateFormat getDateFormat(Locale locale, Convertor.FormatCache formatCache) { 79 SimpleDateFormat dateFormat = null; 80 if (formatCache != null) 81 dateFormat = (SimpleDateFormat )formatCache.get(); 82 if (dateFormat == null) { 83 dateFormat = getDateFormat(locale); 84 if (formatCache != null) 85 formatCache.store(dateFormat); 86 } 87 return dateFormat; 88 } 89 90 protected SimpleDateFormat getDateFormat(Locale locale) { 91 SimpleDateFormat dateFormat = null; 92 93 if (this.variant.equals(DATE)) { 94 dateFormat = (SimpleDateFormat )DateFormat.getDateInstance(style, locale); 96 } else if (this.variant.equals(TIME)) { 97 dateFormat = (SimpleDateFormat )DateFormat.getTimeInstance(style, locale); 99 } else if (this.variant.equals(DATE_TIME)) { 100 dateFormat = (SimpleDateFormat )DateFormat.getDateTimeInstance(style, style, locale); 102 } 103 104 String pattern = (String )localizedPatterns.get(locale); 105 106 if (pattern != null) 107 dateFormat.applyPattern(pattern); 112 else if (nonLocalizedPattern != null) 113 dateFormat.applyPattern(nonLocalizedPattern); 114 115 return dateFormat; 116 } 117 118 public Class getTypeClass() { 119 return Date .class; 120 } 121 122 126 public void setStyle(int style) { 127 this.style = style; 128 } 129 130 public void setVariant(String variant) { 131 if (DATE.equals(variant) || TIME.equals(variant) || DATE_TIME.equals(variant)) { 132 this.variant = variant; 133 } else { 134 throw new IllegalArgumentException ("Invalid value for variant parameter."); 135 } 136 } 137 138 public void addFormattingPattern(Locale locale, String pattern) { 139 localizedPatterns.put(locale, pattern); 140 } 141 142 public void setNonLocalizedPattern(String pattern) { 143 this.nonLocalizedPattern = pattern; 144 } 145 146 private static final String CONVERTOR_EL = "convertor"; 147 148 public void generateSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException { 149 String pattern = getDateFormat(locale).toPattern(); 150 151 if (pattern != null) { 152 AttributesImpl attrs = new AttributesImpl(); 153 attrs.addCDATAAttribute("pattern", pattern); 154 attrs.addCDATAAttribute("variant", this.variant); 155 contentHandler.startElement(FormsConstants.INSTANCE_NS, CONVERTOR_EL, FormsConstants.INSTANCE_PREFIX_COLON + CONVERTOR_EL, attrs); 156 contentHandler.endElement(FormsConstants.INSTANCE_NS, CONVERTOR_EL, FormsConstants.INSTANCE_PREFIX_COLON + CONVERTOR_EL); 157 } 158 } 159 } 160 | Popular Tags |