001 /** 002 * ========================================= 003 * LibFormula : a free Java formula library 004 * ========================================= 005 * 006 * Project Info: http://reporting.pentaho.org/libformula/ 007 * 008 * (C) Copyright 2006-2007, by Pentaho Corporation and Contributors. 009 * 010 * This library is free software; you can redistribute it and/or modify it under the terms 011 * of the GNU Lesser General Public License as published by the Free Software Foundation; 012 * either 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, but WITHOUT ANY WARRANTY; 015 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 016 * See the GNU Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public License along with this 019 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, 020 * Boston, MA 02111-1307, USA. 021 * 022 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 023 * in the United States and other countries.] 024 * 025 * 026 * ------------ 027 * $Id: DefaultLocalizationContext.java 4100 2007-11-26 21:04:25Z dkincade $ 028 * ------------ 029 * (C) Copyright 2006-2007, by Pentaho Corporation. 030 */ 031 package org.jfree.formula; 032 033 import java.text.SimpleDateFormat; 034 import java.util.ArrayList; 035 import java.util.Iterator; 036 import java.util.List; 037 import java.util.Locale; 038 import java.util.ResourceBundle; 039 import java.util.TimeZone; 040 import java.util.StringTokenizer; 041 042 import org.jfree.formula.typing.Type; 043 import org.jfree.util.Configuration; 044 045 /** 046 * Creation-Date: 03.11.2006, 14:28:12 047 * 048 * @author Thomas Morgner 049 */ 050 public class DefaultLocalizationContext implements LocalizationContext 051 { 052 private static final String CONFIG_TIMEZONE_KEY = "org.jfree.formula.timezone"; 053 054 private static final String CONFIG_LOCALE_KEY = "org.jfree.formula.locale"; 055 056 private static final String CONFIG_DATEFORMAT_KEY = "org.jfree.formula.dateformat."; 057 058 private List dateFormats; 059 060 private List datetimeFormats; 061 062 private List timeFormats; 063 064 private Locale locale; 065 066 private TimeZone timeZone; 067 068 public DefaultLocalizationContext() 069 { 070 dateFormats = new ArrayList(); 071 datetimeFormats = new ArrayList(); 072 timeFormats = new ArrayList(); 073 } 074 075 public Locale getLocale() 076 { 077 return locale; 078 } 079 080 public ResourceBundle getBundle(final String id) 081 { 082 return ResourceBundle.getBundle(id, getLocale()); 083 } 084 085 public TimeZone getTimeZone() 086 { 087 return timeZone; 088 } 089 090 public List getDateFormats(final Type type) 091 { 092 if (type.isFlagSet(Type.DATE_TYPE)) 093 { 094 return dateFormats; 095 } 096 else if (type.isFlagSet(Type.DATETIME_TYPE)) 097 { 098 return datetimeFormats; 099 } 100 else if (type.isFlagSet(Type.TIME_TYPE)) 101 { 102 return timeFormats; 103 } 104 return null; 105 } 106 107 private String[] createLocale(final String locale) 108 { 109 final StringTokenizer strtok = new StringTokenizer(locale, "_"); 110 final String[] retval = new String[3]; 111 if (strtok.hasMoreElements()) 112 { 113 retval[0] = strtok.nextToken(); 114 } 115 if (strtok.hasMoreElements()) 116 { 117 retval[1] = strtok.nextToken(); 118 } 119 else 120 { 121 retval[1] = ""; 122 } 123 if (strtok.hasMoreElements()) 124 { 125 retval[2] = strtok.nextToken(); 126 } 127 else 128 { 129 retval[2] = ""; 130 } 131 return retval; 132 } 133 134 private String[] createFormatSpec(final String text) 135 { 136 final StringTokenizer strtok = new StringTokenizer(text, "."); 137 if (strtok.countTokens() == 2) 138 { 139 final String[] retval = new String[2]; 140 retval[0] = strtok.nextToken(); 141 retval[1] = strtok.nextToken(); 142 return retval; 143 } 144 return null; 145 } 146 147 148 public void initialize(final Configuration config) 149 { 150 // setting locale 151 final String declaredLocale = config.getConfigProperty(CONFIG_LOCALE_KEY, Locale.getDefault().toString()); 152 final String[] declaredLocaleParts = createLocale(declaredLocale); 153 this.locale = new Locale(declaredLocaleParts[0], declaredLocaleParts[1], declaredLocaleParts[2]); 154 155 //setting timezone 156 final String timeZoneId = config.getConfigProperty(CONFIG_TIMEZONE_KEY, TimeZone.getDefault().getID()); 157 timeZone = TimeZone.getTimeZone(timeZoneId); 158 159 // adding custom dateformats 160 final Iterator formatKeys = config.findPropertyKeys(CONFIG_DATEFORMAT_KEY); 161 while (formatKeys.hasNext()) 162 { 163 final String formatKey = (String) formatKeys.next(); 164 // Lets grab the format string first ... 165 final String format = config.getConfigProperty(formatKey); 166 167 // The key itself holds information about the format-string type and the locale of that string. 168 final String keySpec = formatKey.substring(CONFIG_DATEFORMAT_KEY.length(), formatKey.length()); 169 final String[] formatSpec = createFormatSpec(keySpec); 170 if (formatSpec != null) 171 { 172 final String type = "type."+formatSpec[0]; 173 final String[] locale = createLocale(formatSpec[1]); 174 175 final SimpleDateFormat df = new SimpleDateFormat(format, new Locale(locale[0], locale[1], locale[2])); 176 177 if (Type.TIME_TYPE.equals(type)) 178 { 179 timeFormats.add(df); 180 } 181 else if (Type.DATE_TYPE.equals(type)) 182 { 183 dateFormats.add(df); 184 } 185 else if (Type.DATETIME_TYPE.equals(type)) 186 { 187 datetimeFormats.add(df); 188 } 189 } 190 } 191 192 // adding default dateformats using current local 193 datetimeFormats.add(SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, 194 SimpleDateFormat.SHORT, getLocale())); 195 dateFormats.add(SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT, 196 getLocale())); 197 timeFormats.add(SimpleDateFormat.getTimeInstance(SimpleDateFormat.SHORT, 198 getLocale())); 199 200 // adding default ISO8 dateformats 201 datetimeFormats.add(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.US)); 202 dateFormats.add(new SimpleDateFormat("yyyy-MM-dd", Locale.US)); 203 timeFormats.add(new SimpleDateFormat("hh:mm:ss", Locale.US)); 204 timeFormats.add(new SimpleDateFormat("hh:mm", Locale.US)); 205 } 206 }