1 /*
   2  * Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 /*
  24  *
  25  */
  26 
  27 package com.bar;
  28 
  29 import java.text.*;
  30 import java.util.*;
  31 import java.util.spi.*;
  32 
  33 import com.foobar.Utils;
  34 
  35 public class LocaleNameProviderImpl extends LocaleNameProvider {
  36     static Locale[] avail = {Locale.JAPANESE,
  37                              Locale.JAPAN,
  38                              new Locale("ja", "JP", "osaka"),
  39                              new Locale("ja", "JP", "kyoto"),
  40                              new Locale("xx"),
  41                              new Locale("yy", "YY", "YYYY")};
  42     static List<Locale> availList = Arrays.asList(avail);
  43     public Locale[] getAvailableLocales() {
  44         return avail;
  45     }
  46 
  47     @Override
  48     public String getDisplayLanguage(String lang, Locale target) {
  49         return getDisplayString(lang, target);
  50     }
  51 
  52     @Override
  53     public String getDisplayCountry(String ctry, Locale target) {
  54         return getDisplayString(ctry, target);
  55     }
  56 
  57     @Override
  58     public String getDisplayVariant(String vrnt, Locale target) {
  59         return getDisplayString(vrnt, target);
  60     }
  61 
  62     private String getDisplayString(String key, Locale target) {
  63         if (!Utils.supportsLocale(availList, target)) {
  64             throw new IllegalArgumentException("locale is not supported: "+target);
  65         }
  66 
  67         String ret = null;
  68 
  69         if (target.getLanguage().equals("yy") &&
  70             target.getCountry().equals("YY")) {
  71             String vrnt = target.getVariant();
  72             if (vrnt.startsWith("YYYY")) {
  73                 switch (key) {
  74                     case "yy":
  75                     case "YY":
  76                         ret = "waiwai";
  77                         break;
  78 
  79                     case "YYYY":
  80                         if (vrnt.equals("YYYY_suffix")) {
  81                             // for LocaleNameProviderTest.variantFallbackTest()
  82                             throw new RuntimeException(vrnt);
  83                         } else {
  84                             ret = "waiwai";
  85                         }
  86                         break;
  87                 }
  88             }
  89         } else {
  90             // resource bundle based (allows fallback)
  91         try {
  92             ResourceBundle rb = ResourceBundle.getBundle("com.bar.LocaleNames", target);
  93                 ret = rb.getString(key);
  94         } catch (MissingResourceException mre) {
  95         }
  96         }
  97 
  98         return ret;
  99     }
 100         }