Source for org.jfree.formula.DefaultLocalizationContext

   1: /**
   2:  * =========================================
   3:  * LibFormula : a free Java formula library
   4:  * =========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/libformula/
   7:  *
   8:  * (C) Copyright 2006-2007, by Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either 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, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  *
  26:  * ------------
  27:  * $Id: DefaultLocalizationContext.java 2797 2007-05-14 12:35:33Z taqua $
  28:  * ------------
  29:  * (C) Copyright 2006-2007, by Pentaho Corporation.
  30:  */
  31: package org.jfree.formula;
  32: 
  33: import java.text.SimpleDateFormat;
  34: import java.util.ArrayList;
  35: import java.util.Iterator;
  36: import java.util.List;
  37: import java.util.Locale;
  38: import java.util.ResourceBundle;
  39: import java.util.TimeZone;
  40: import java.util.StringTokenizer;
  41: 
  42: import org.jfree.formula.typing.Type;
  43: import org.jfree.util.Configuration;
  44: 
  45: /**
  46:  * Creation-Date: 03.11.2006, 14:28:12
  47:  *
  48:  * @author Thomas Morgner
  49:  */
  50: public class DefaultLocalizationContext implements LocalizationContext
  51: {
  52:   private static final String CONFIG_TIMEZONE_KEY = "org.jfree.formula.timezone";
  53: 
  54:   private static final String CONFIG_LOCALE_KEY = "org.jfree.formula.locale";
  55: 
  56:   private static final String CONFIG_DATEFORMAT_KEY = "org.jfree.formula.dateformat.";
  57: 
  58:   private List dateFormats;
  59: 
  60:   private List datetimeFormats;
  61: 
  62:   private List timeFormats;
  63: 
  64:   private Locale locale;
  65: 
  66:   private TimeZone timeZone;
  67: 
  68:   public DefaultLocalizationContext()
  69:   {
  70:     dateFormats = new ArrayList();
  71:     datetimeFormats = new ArrayList();
  72:     timeFormats = new ArrayList();
  73:   }
  74: 
  75:   public Locale getLocale()
  76:   {
  77:     return locale;
  78:   }
  79: 
  80:   public ResourceBundle getBundle(final String id)
  81:   {
  82:     return ResourceBundle.getBundle(id, getLocale());
  83:   }
  84: 
  85:   public TimeZone getTimeZone()
  86:   {
  87:     return timeZone;
  88:   }
  89: 
  90:   public List getDateFormats(final Type type)
  91:   {
  92:     if (type.isFlagSet(Type.DATE_TYPE))
  93:     {
  94:       return dateFormats;
  95:     }
  96:     else if (type.isFlagSet(Type.DATETIME_TYPE))
  97:     {
  98:       return datetimeFormats;
  99:     }
 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:   }
 205: }