001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028:
029: package net.sf.jasperreports.engine.util;
030:
031: import java.text.ParseException;
032: import java.text.SimpleDateFormat;
033: import java.util.Locale;
034: import java.util.TimeZone;
035:
036: import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
037: import org.apache.commons.logging.Log;
038: import org.apache.commons.logging.LogFactory;
039:
040: /**
041: * A Converter class dedicated for the java.util.Date type.
042: * <p/>
043: * In order to obtain a java.util.Date object from a given String, a JRJavaUtilDateConverter
044: * object should be instantiated and it's inherited convert() method should be called. The
045: * final result is provided by the JRJavaUtilDateConverter's parse() invoked method.
046: * <p/>
047: * If if any of constructor arguments is null, default values will be provided.
048: * <p/>
049: * @see org.apache.commons.beanutils.locale.converters.DateLocaleConverter
050: * @author szaharia
051: * @version $Id: JRDateLocaleConverter.java 1737 2007-06-04 15:18:39Z teodord $
052: */
053:
054: public class JRDateLocaleConverter extends DateLocaleConverter {
055:
056: private static Log log = LogFactory
057: .getLog(DateLocaleConverter.class);
058:
059: // holds the timezone's ID
060: private TimeZone timeZone = null;
061:
062: /**
063: *
064: */
065: public JRDateLocaleConverter(TimeZone timeZone) {
066: super ();
067:
068: this .timeZone = timeZone;
069: }
070:
071: /**
072: *
073: */
074: protected Object parse(Object value, String pattern)
075: throws ParseException {
076: SimpleDateFormat formatter = getFormatter(pattern, locale);
077: if (pattern != null) {
078: if (locPattern) {
079: formatter.applyLocalizedPattern(pattern);
080: } else {
081: formatter.applyPattern(pattern);
082: }
083: }
084: return formatter.parse((String) value);
085: }
086:
087: /**
088: *
089: */
090: private SimpleDateFormat getFormatter(String pattern, Locale locale) {
091: if (pattern == null) {
092: pattern = locPattern ? new SimpleDateFormat()
093: .toLocalizedPattern() : new SimpleDateFormat()
094: .toPattern();
095: log.warn("Null pattern was provided, defaulting to: "
096: + pattern);
097: }
098: SimpleDateFormat format = new SimpleDateFormat(pattern, locale);
099: if (timeZone != null)
100: format.setTimeZone(timeZone);
101: format.setLenient(isLenient());
102: return format;
103: }
104:
105: }
|