Coverage Report - org.webmacro.servlet.LocaleTool
 
Classes in this File Line Coverage Branch Coverage Complexity
LocaleTool
0%
0/30
0%
0/6
1.778
 
 1  
 /*
 2  
  * Copyright (C) 1998-2000 Semiotek Inc.  All Rights Reserved.
 3  
  *
 4  
  * Redistribution and use in source and binary forms, with or without
 5  
  * modification, are permitted under the terms of either of the following
 6  
  * Open Source licenses:
 7  
  *
 8  
  * The GNU General Public License, version 2, or any later version, as
 9  
  * published by the Free Software Foundation
 10  
  * (http://www.fsf.org/copyleft/gpl.html);
 11  
  *
 12  
  *  or
 13  
  *
 14  
  * The Semiotek Public License (http://webmacro.org/LICENSE.)
 15  
  *
 16  
  * This software is provided "as is", with NO WARRANTY, not even the
 17  
  * implied warranties of fitness to purpose, or merchantability. You
 18  
  * assume all risks and liabilities associated with its use.
 19  
  *
 20  
  * See www.webmacro.org for more information on the WebMacro project.
 21  
  */
 22  
 
 23  
 
 24  
 package org.webmacro.servlet;
 25  
 
 26  
 import org.webmacro.Context;
 27  
 import org.webmacro.ContextTool;
 28  
 import org.webmacro.PropertyException;
 29  
 import org.webmacro.UnsettableException;
 30  
 import org.webmacro.util.Bag;
 31  
 
 32  
 import java.lang.reflect.Field;
 33  
 import java.util.HashMap;
 34  
 import java.util.Locale;
 35  
 import java.util.NoSuchElementException;
 36  
 import java.util.StringTokenizer;
 37  
 
 38  
 /**
 39  
  * Provide Template with access to Locales.  Also gives access to the static
 40  
  * fields e.g., Locale.US
 41  
  */
 42  0
 public class LocaleTool extends ContextTool implements Bag
 43  
 {
 44  
 
 45  
     public static final String RCS = "$Id: LocaleTool.java,v 1.10 2003/07/30 04:51:17 briangoetz Exp $";
 46  
 
 47  0
     private static HashMap cache = new HashMap();
 48  
 
 49  
     public Object init (Context context)
 50  
             throws PropertyException
 51  
     {
 52  0
         return this;
 53  
     }
 54  
 
 55  
     /**
 56  
      * return the default locale for this JVM
 57  
      */
 58  
 
 59  
     final public Locale getDefault ()
 60  
     {
 61  0
         return Locale.getDefault();
 62  
     }
 63  
 
 64  
     /**
 65  
      * wrappers around the 3 constructors for Locale
 66  
      */
 67  
 
 68  
     final public Locale getLocale (String country)
 69  
     {
 70  0
         return getLocale(country, "", "");
 71  
     }
 72  
 
 73  
     final public Locale getLocale (String country, String language)
 74  
     {
 75  0
         return getLocale(country, language, "");
 76  
 
 77  
     }
 78  
 
 79  
     final public Locale getLocale (String country, String language, String variant)
 80  
     {
 81  0
         String key = country + "_" + language + "_" + variant; // language.toUpperCase() ?
 82  0
         Locale locale = (Locale) cache.get(key);
 83  0
         if (locale == null)
 84  
         {
 85  0
             locale = new Locale(country, language, "");
 86  0
             cache.put(key, locale);
 87  
         }
 88  0
         return locale;
 89  
     }
 90  
 
 91  
     /**
 92  
      * access method used by $Locale.xxxxx => LocaleTool.get("xxxxx")
 93  
      */
 94  
     final public Object get (String field)
 95  
     {
 96  0
         return buildLocale(field);
 97  
     }
 98  
 
 99  
     /**
 100  
      * access to the static members such as Locale.US, etc
 101  
      *
 102  
      * If that field doesn't exist try to construct a locale
 103  
      * from a string so templates can have $Locale.en_GB.
 104  
      *
 105  
      * This may not be the right thing to do, though, in case of typos
 106  
      *
 107  
      * e.g., $Locale.ENGLISH => Locale.ENGLISH
 108  
      * but   $Locale.ENGLSH => Locale("ENGLSH","","")
 109  
      *
 110  
      * Could argue that typos aren't caught in the latter anyway
 111  
      * (surprisingly?)
 112  
      */
 113  
 
 114  
     final public static Locale buildLocale (String field)
 115  
     {
 116  
 
 117  0
         Locale locale = (Locale) cache.get(field);
 118  0
         if (locale == null)
 119  
         {
 120  
             try
 121  
             {
 122  0
                 Field f = Locale.class.getField(field);
 123  0
                 locale = (Locale) f.get(null);
 124  
             }
 125  0
             catch (Exception ne)
 126  
             {
 127  0
                 StringTokenizer st = new StringTokenizer(field, "_");
 128  0
                 String[] parts = new String[]{"", "", ""};
 129  
                 try
 130  
                 {
 131  0
                     for (int i = 0; i < 3; i++)
 132  
                     {
 133  0
                         parts[i] = st.nextToken();
 134  
                     }
 135  
                 }
 136  0
                 catch (NoSuchElementException e)
 137  
                 {
 138  0
                 }
 139  
 //              System.out.println("Creating Locale: "
 140  
 //                  +parts[0]+"-"+parts[1]+"-"+parts[2]);
 141  0
                 locale = new Locale(parts[0], parts[1], parts[2]);
 142  0
             }
 143  
 //          System.out.println("Returning locale for "+field+" -> "+locale);
 144  0
             cache.put(field, locale);
 145  
         }
 146  0
         return locale;
 147  
     }
 148  
 
 149  
     /**
 150  
      * Unsupported
 151  
      */
 152  
     final public void put (String key, Object value)
 153  
             throws UnsettableException
 154  
     {
 155  0
         throw new UnsettableException("Cannot set a form property");
 156  
     }
 157  
 
 158  
 
 159  
     /**
 160  
      * Unsupported
 161  
      */
 162  
     final public void remove (String key)
 163  
             throws UnsettableException
 164  
     {
 165  0
         throw new UnsettableException("Cannot unset a form property");
 166  
     }
 167  
 
 168  
 }