1 20 package org.enhydra.barracuda.contrib.dbroggisch.display.filters; 21 22 import java.util.*; 23 import java.text.DateFormat ; 24 import java.text.SimpleDateFormat ; 25 import org.enhydra.barracuda.contrib.dbroggisch.display.filters.dtd.*; 26 import java.lang.reflect.Field ; 27 import org.apache.log4j.*; 28 29 public class DateFilter implements Filter { 30 private static Logger logger = Logger.getLogger(DateFilter.class.getName()); 31 32 private String formatString; 33 private DateFormat dateFormat; 34 35 private Locale loc; 36 37 38 public DateFilter(String formatString, Locale loc) { 39 if(loc == null) { 40 loc = Locale.getDefault(); 41 } else { 42 this.loc = loc; 43 } 44 this.formatString = formatString; 45 } 46 47 public DateFilter(String formatString) { 48 this(formatString, null); 49 } 50 51 public DateFilter() { 52 this(null); 53 } 54 55 56 public Object filter(Object obj, FilterContext context) 57 throws FilterException 58 { 59 if(obj == null) { 60 return null; 61 } 62 if(obj instanceof java.util.Date ) { 63 if(dateFormat == null) { 64 dateFormat = new SimpleDateFormat (formatString, loc); 65 } 66 return dateFormat.format((java.util.Date )obj); 67 } else { 68 throw new FilterException("Not a java.util.Date"); 69 } 70 } 71 72 73 public Filter configure(Object obj) 74 throws FilterException 75 { 76 org.enhydra.barracuda.contrib.dbroggisch.display.filters.dtd.Date xmlObj = (org.enhydra.barracuda.contrib.dbroggisch.display.filters.dtd.Date)obj; 77 this.formatString = xmlObj.getValue(); 78 String locale = xmlObj.getLocale(); 79 if(locale != null && !locale.trim().equals("")) { 80 try { 81 Field f = java.util.Locale .class.getDeclaredField(locale); 82 Locale loc = (Locale)f.get(null); 83 String [] ar = new String [2]; 84 if(logger.isDebugEnabled()) { 85 logger.debug("Setting locale " + locale); 86 } 87 this.loc = loc; 88 } 89 catch(NoSuchFieldException ex) { 90 logger.error(ex.getMessage()); 91 ex.printStackTrace(); 92 throw new FilterException("Error configuring DateFilter"); 93 } 94 catch(IllegalAccessException ex) { 95 logger.error(ex.getMessage()); 96 ex.printStackTrace(); 97 throw new FilterException("Error configuring DateFilter"); 98 } 99 } 100 return this; 101 102 } 103 104 } 105 | Popular Tags |