KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > contrib > dbroggisch > display > filters > DateFilter


1 /*
2  * Copyright (C) 2003 Diez B. Roggisch [deets@web.de]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: DateFilter.java,v 1.3 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.contrib.dbroggisch.display.filters;
21
22 import java.util.*;
23 import java.text.DateFormat JavaDoc;
24 import java.text.SimpleDateFormat JavaDoc;
25 import org.enhydra.barracuda.contrib.dbroggisch.display.filters.dtd.*;
26 import java.lang.reflect.Field JavaDoc;
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 JavaDoc formatString;
33         private DateFormat JavaDoc dateFormat;
34
35         private Locale loc;
36
37
38         public DateFilter(String JavaDoc 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 JavaDoc formatString) {
48                 this(formatString, null);
49         }
50
51         public DateFilter() {
52                 this(null);
53         }
54
55
56         public Object JavaDoc filter(Object JavaDoc obj, FilterContext context)
57                 throws FilterException
58         {
59                 if(obj == null) {
60                         return null;
61                 }
62                 if(obj instanceof java.util.Date JavaDoc) {
63                         if(dateFormat == null) {
64                                 dateFormat = new SimpleDateFormat JavaDoc(formatString, loc);
65                         }
66                         return dateFormat.format((java.util.Date JavaDoc)obj);
67                 } else {
68                         throw new FilterException("Not a java.util.Date");
69                 }
70         }
71
72
73         public Filter configure(Object JavaDoc 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 JavaDoc locale = xmlObj.getLocale();
79                 if(locale != null && !locale.trim().equals("")) {
80                         try {
81                                 Field JavaDoc f = java.util.Locale JavaDoc.class.getDeclaredField(locale);
82                                 Locale loc = (Locale)f.get(null);
83                                 String JavaDoc [] ar = new String JavaDoc[2];
84                                 if(logger.isDebugEnabled()) {
85                                         logger.debug("Setting locale " + locale);
86                                 }
87                                 this.loc = loc;
88                         }
89                         catch(NoSuchFieldException JavaDoc ex) {
90                                 logger.error(ex.getMessage());
91                                 ex.printStackTrace();
92                                 throw new FilterException("Error configuring DateFilter");
93                         }
94                         catch(IllegalAccessException JavaDoc 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