KickJava   Java API By Example, From Geeks To Geeks.

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


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 package net.sf.jasperreports.engine.util;
29
30 import java.text.DateFormat JavaDoc;
31 import java.text.DecimalFormat JavaDoc;
32 import java.text.NumberFormat JavaDoc;
33 import java.text.SimpleDateFormat JavaDoc;
34 import java.util.Locale JavaDoc;
35 import java.util.TimeZone JavaDoc;
36
37 import net.sf.jasperreports.engine.JRRuntimeException;
38
39
40 /**
41  * @author Teodor Danciu (teodord@users.sourceforge.net)
42  * @version $Id: JRDataUtils.java 1330 2006-07-10 12:09:48 +0300 (Mon, 10 Jul 2006) lucianc $
43  */

44 public class DefaultFormatFactory implements FormatFactory
45 {
46
47     /**
48      * Used in the date pattern to specify the default style.
49      * @see java.text.DateFormat#DEFAULT
50      */

51     public static final String JavaDoc STANDARD_DATE_FORMAT_DEFAULT = "default";
52
53     /**
54      * Used in the date pattern to specify the short style.
55      * @see java.text.DateFormat#SHORT
56      */

57     public static final String JavaDoc STANDARD_DATE_FORMAT_SHORT = "short";
58
59     /**
60      * Used in the date pattern to specify the medium style.
61      * @see java.text.DateFormat#MEDIUM
62      */

63     public static final String JavaDoc STANDARD_DATE_FORMAT_MEDIUM = "medium";
64
65     /**
66      * Used in the date pattern to specify the long style.
67      * @see java.text.DateFormat#LONG
68      */

69     public static final String JavaDoc STANDARD_DATE_FORMAT_LONG = "long";
70
71     /**
72      * Used in the date pattern to specify the full style.
73      * @see java.text.DateFormat#FULL
74      */

75     public static final String JavaDoc STANDARD_DATE_FORMAT_FULL = "full";
76
77     /**
78      * Used in the date pattern to specify that the date or time should not be included.
79      */

80     public static final String JavaDoc STANDARD_DATE_FORMAT_HIDE = "hide";
81
82     /**
83      * Used in the date format pattern to separate the date and time styles.
84      */

85     public static final String JavaDoc STANDARD_DATE_FORMAT_SEPARATOR = ",";
86
87
88     public DateFormat JavaDoc createDateFormat(String JavaDoc pattern, Locale JavaDoc locale, TimeZone JavaDoc tz)
89     {
90         int[] dateStyle = null;
91         int[] timeStyle = null;
92         if (pattern != null && pattern.trim().length() > 0)
93         {
94             int sepIdx = pattern.indexOf(STANDARD_DATE_FORMAT_SEPARATOR);
95             String JavaDoc dateTok = sepIdx < 0 ? pattern : pattern.substring(0, sepIdx);
96             dateStyle = getDateStyle(dateTok);
97             if (dateStyle != null)
98             {
99                 if (sepIdx >= 0)
100                 {
101                     String JavaDoc timeTok = pattern.substring(sepIdx + STANDARD_DATE_FORMAT_SEPARATOR.length());
102                     timeStyle = getDateStyle(timeTok);
103                 }
104                 else
105                 {
106                     timeStyle = dateStyle;
107                 }
108             }
109         }
110         
111         DateFormat JavaDoc format;
112         if (dateStyle != null && timeStyle != null)
113         {
114             format = getDateFormat(dateStyle, timeStyle, locale);
115         }
116         else
117         {
118             if (locale == null)
119             {
120                 format = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
121             }
122             else
123             {
124                 format = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale);
125             }
126             if (
127                 pattern != null && pattern.trim().length() > 0
128                 && format instanceof SimpleDateFormat JavaDoc
129                 )
130             {
131                 ((SimpleDateFormat JavaDoc) format).applyPattern(pattern);
132             }
133         }
134         
135         format.setTimeZone(tz);
136         
137         return format;
138     }
139
140
141     protected static int[] getDateStyle(String JavaDoc pattern)
142     {
143         if (pattern.equalsIgnoreCase(STANDARD_DATE_FORMAT_DEFAULT))
144         {
145             return new int[]{DateFormat.DEFAULT};
146         }
147         else if (pattern.equalsIgnoreCase(STANDARD_DATE_FORMAT_SHORT))
148         {
149             return new int[]{DateFormat.SHORT};
150         }
151         else if (pattern.equalsIgnoreCase(STANDARD_DATE_FORMAT_MEDIUM))
152         {
153             return new int[]{DateFormat.MEDIUM};
154         }
155         else if (pattern.equalsIgnoreCase(STANDARD_DATE_FORMAT_LONG))
156         {
157             return new int[]{DateFormat.LONG};
158         }
159         else if (pattern.equalsIgnoreCase(STANDARD_DATE_FORMAT_FULL))
160         {
161             return new int[]{DateFormat.FULL};
162         }
163         else if (pattern.equalsIgnoreCase(STANDARD_DATE_FORMAT_HIDE))
164         {
165             return new int[0];
166         }
167         else
168         {
169             return null;
170         }
171     }
172
173     
174     protected static DateFormat JavaDoc getDateFormat(int[] dateStyle, int[] timeStyle, Locale JavaDoc locale)
175     {
176         if (dateStyle.length == 0)
177         {
178             if (timeStyle.length == 0)
179             {
180                 return new SimpleDateFormat JavaDoc("");
181             }
182
183             return locale == null ?
184                     DateFormat.getTimeInstance(timeStyle[0]) :
185                         DateFormat.getTimeInstance(timeStyle[0], locale);
186         }
187
188         if (timeStyle.length == 0)
189         {
190             return locale == null ?
191                     DateFormat.getDateInstance(dateStyle[0]) :
192                     DateFormat.getDateInstance(dateStyle[0], locale);
193         }
194
195         return locale == null ?
196                 DateFormat.getDateTimeInstance(dateStyle[0], timeStyle[0]) :
197                 DateFormat.getDateTimeInstance(dateStyle[0], timeStyle[0], locale);
198     }
199     
200     
201     public NumberFormat JavaDoc createNumberFormat(String JavaDoc pattern, Locale JavaDoc locale)
202     {
203         NumberFormat JavaDoc format = null;
204         if (pattern != null && pattern.trim().length() > 0)
205         {
206             if (locale == null)
207             {
208                 format = NumberFormat.getNumberInstance();
209             }
210             else
211             {
212                 format = NumberFormat.getNumberInstance(locale);
213             }
214             
215             if (format instanceof DecimalFormat JavaDoc)
216             {
217                 ((DecimalFormat JavaDoc) format).applyPattern(pattern);
218             }
219         }
220         return format;
221     }
222     
223     
224     public static FormatFactory createFormatFactory(String JavaDoc formatFactoryClassName)
225     {
226         FormatFactory formatFactory = null;
227         
228         if (formatFactoryClassName != null)
229         {
230             try
231             {
232                 Class JavaDoc formatFactoryClass = JRClassLoader.loadClassForName(formatFactoryClassName);
233                 formatFactory = (FormatFactory) formatFactoryClass.newInstance();
234             }
235             catch (ClassNotFoundException JavaDoc e)
236             {
237                 throw new JRRuntimeException("Error loading format factory class : " + formatFactoryClassName, e);
238             }
239             catch (Exception JavaDoc e)
240             {
241                 throw new JRRuntimeException("Error creating format factory instance : " + formatFactoryClassName, e);
242             }
243         }
244         else
245         {
246             formatFactory = new DefaultFormatFactory();
247         }
248         
249         return formatFactory;
250     }
251
252
253 }
254
Popular Tags