1 28 package net.sf.jasperreports.engine.util; 29 30 import java.text.DateFormat ; 31 import java.text.DecimalFormat ; 32 import java.text.NumberFormat ; 33 import java.text.SimpleDateFormat ; 34 import java.util.Locale ; 35 import java.util.TimeZone ; 36 37 import net.sf.jasperreports.engine.JRRuntimeException; 38 39 40 44 public class DefaultFormatFactory implements FormatFactory 45 { 46 47 51 public static final String STANDARD_DATE_FORMAT_DEFAULT = "default"; 52 53 57 public static final String STANDARD_DATE_FORMAT_SHORT = "short"; 58 59 63 public static final String STANDARD_DATE_FORMAT_MEDIUM = "medium"; 64 65 69 public static final String STANDARD_DATE_FORMAT_LONG = "long"; 70 71 75 public static final String STANDARD_DATE_FORMAT_FULL = "full"; 76 77 80 public static final String STANDARD_DATE_FORMAT_HIDE = "hide"; 81 82 85 public static final String STANDARD_DATE_FORMAT_SEPARATOR = ","; 86 87 88 public DateFormat createDateFormat(String pattern, Locale locale, TimeZone 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 dateTok = sepIdx < 0 ? pattern : pattern.substring(0, sepIdx); 96 dateStyle = getDateStyle(dateTok); 97 if (dateStyle != null) 98 { 99 if (sepIdx >= 0) 100 { 101 String 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 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 129 ) 130 { 131 ((SimpleDateFormat ) format).applyPattern(pattern); 132 } 133 } 134 135 format.setTimeZone(tz); 136 137 return format; 138 } 139 140 141 protected static int[] getDateStyle(String 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 getDateFormat(int[] dateStyle, int[] timeStyle, Locale locale) 175 { 176 if (dateStyle.length == 0) 177 { 178 if (timeStyle.length == 0) 179 { 180 return new SimpleDateFormat (""); 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 createNumberFormat(String pattern, Locale locale) 202 { 203 NumberFormat 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 ) 216 { 217 ((DecimalFormat ) format).applyPattern(pattern); 218 } 219 } 220 return format; 221 } 222 223 224 public static FormatFactory createFormatFactory(String formatFactoryClassName) 225 { 226 FormatFactory formatFactory = null; 227 228 if (formatFactoryClassName != null) 229 { 230 try 231 { 232 Class formatFactoryClass = JRClassLoader.loadClassForName(formatFactoryClassName); 233 formatFactory = (FormatFactory) formatFactoryClass.newInstance(); 234 } 235 catch (ClassNotFoundException e) 236 { 237 throw new JRRuntimeException("Error loading format factory class : " + formatFactoryClassName, e); 238 } 239 catch (Exception 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 |