KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > util > JRDateLocaleConverter


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28
29 package net.sf.jasperreports.engine.util;
30
31 import java.text.ParseException JavaDoc;
32 import java.text.SimpleDateFormat JavaDoc;
33 import java.util.Locale JavaDoc;
34 import java.util.TimeZone JavaDoc;
35
36 import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39
40 /**
41  * A Converter class dedicated for the java.util.Date type.
42  * <p/>
43  * In order to obtain a java.util.Date object from a given String, a JRJavaUtilDateConverter
44  * object should be instantiated and it's inherited convert() method should be called. The
45  * final result is provided by the JRJavaUtilDateConverter's parse() invoked method.
46  * <p/>
47  * If if any of constructor arguments is null, default values will be provided.
48  * <p/>
49  * @see org.apache.commons.beanutils.locale.converters.DateLocaleConverter
50  * @author szaharia
51  * @version $Id: JRParameter.java 1485 2006-11-14 18:23:17 +0000 (Tue, 14 Nov 2006) teodord $
52  */

53
54 public class JRDateLocaleConverter extends DateLocaleConverter
55 {
56
57     private static Log log = LogFactory.getLog(DateLocaleConverter.class);
58     
59     // holds the timezone's ID
60
private TimeZone JavaDoc timeZone = null;
61     
62     //boolean isLenient = false;
63

64     /**
65      *
66      */

67     public JRDateLocaleConverter(TimeZone JavaDoc timeZone) {
68         super();
69         
70         this.timeZone = timeZone;
71     }
72
73     /**
74      *
75      *
76     public JRDateLocaleConverter(boolean locPattern) {
77         super(Locale.getDefault(), locPattern);
78     }
79
80     /**
81      *
82      *
83     public JRDateLocaleConverter(Locale locale) {
84         super(locale, false);
85     }
86
87     /**
88      *
89      *
90     public JRDateLocaleConverter(Locale locale, boolean locPattern) {
91         super(locale, (String) null, locPattern);
92     }
93
94     /**
95      *
96      *
97     public JRDateLocaleConverter(Locale locale, String pattern) {
98         super(locale, pattern, false);
99     }
100
101     /**
102      * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
103      * that will throw a {@link org.apache.commons.beanutils.ConversionException}
104      * if a conversion error occurs.
105      *
106      * @param locale The locale
107      * @param pattern The convertion pattern
108      * @param locPattern Indicate whether the pattern is localized or not
109      *
110     public JRDateLocaleConverter(Locale locale, String pattern, boolean locPattern) {
111         super(locale, pattern, locPattern);
112     }
113
114     /**
115      *
116      *
117     public JRDateLocaleConverter(Object defaultValue) {
118         super(defaultValue, false);
119     }
120
121     /**
122      *
123      *
124     public JRDateLocaleConverter(Object defaultValue, boolean locPattern) {
125         super(defaultValue, Locale.getDefault(), locPattern);
126     }
127
128     /**
129      *
130      *
131     public JRDateLocaleConverter(Object defaultValue, Locale locale) {
132         super(defaultValue, locale, false);
133     }
134
135     /**
136      *
137      *
138     public JRDateLocaleConverter(Object defaultValue, Locale locale, boolean locPattern) {
139         super(defaultValue, locale, null, locPattern);
140     }
141
142
143     /**
144      *
145      *
146     public JRDateLocaleConverter(Object defaultValue, Locale locale, String pattern) {
147         super(defaultValue, locale, pattern, false);
148     }
149
150     /**
151      *
152      *
153     public JRDateLocaleConverter(Object defaultValue, Locale locale, String pattern, boolean locPattern) {
154         super(defaultValue, locale, pattern, locPattern);
155     }
156     */

157
158     protected Object JavaDoc parse(Object JavaDoc value, String JavaDoc pattern) throws ParseException JavaDoc {
159         SimpleDateFormat JavaDoc formatter = getFormatter(pattern, locale);
160         if (locPattern) {
161             formatter.applyLocalizedPattern(pattern);
162         }
163         else {
164             formatter.applyPattern(pattern);
165         }
166         return formatter.parse((String JavaDoc) value);
167     }
168
169     private SimpleDateFormat JavaDoc getFormatter(String JavaDoc pattern, Locale JavaDoc locale) {
170         
171         if(pattern == null) {
172             pattern = locPattern ?
173                 new SimpleDateFormat JavaDoc().toLocalizedPattern() : new SimpleDateFormat JavaDoc().toPattern();
174             log.warn("Null pattern was provided, defaulting to: " + pattern);
175         }
176         SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc(pattern, locale);
177         if(timeZone != null)
178             format.setTimeZone(timeZone);
179         format.setLenient(isLenient());
180         return format;
181     }
182
183     /**
184     *
185     *
186    public boolean isLenient() {
187        return isLenient;
188    }
189    
190    /**
191     *
192     *
193    public void setLenient(boolean lenient) {
194        isLenient = lenient;
195    }
196    */

197     
198 }
199
Popular Tags