1 /*
   2  * Copyright (c) 1997, 2010, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.istack.internal.localization;
  27 
  28 import java.text.MessageFormat;
  29 import java.util.HashMap;
  30 import java.util.Locale;
  31 import java.util.MissingResourceException;
  32 import java.util.ResourceBundle;
  33 
  34 /**
  35  * Localizes the {@link Localizable} into a message
  36  * by using a configured {@link Locale}.
  37  *
  38  * @author WS Development Team
  39  */
  40 public class Localizer {
  41 
  42     private final Locale _locale;
  43     private final HashMap _resourceBundles;
  44 
  45     public Localizer() {
  46         this(Locale.getDefault());
  47     }
  48 
  49     public Localizer(Locale l) {
  50         _locale = l;
  51         _resourceBundles = new HashMap();
  52     }
  53 
  54     public Locale getLocale() {
  55         return _locale;
  56     }
  57 
  58     public String localize(Localizable l) {
  59         String key = l.getKey();
  60         if (key == Localizable.NOT_LOCALIZABLE) {
  61             // this message is not localizable
  62             return (String) l.getArguments()[0];
  63         }
  64         String bundlename = l.getResourceBundleName();
  65 
  66         try {
  67             ResourceBundle bundle =
  68                 (ResourceBundle) _resourceBundles.get(bundlename);
  69 
  70             if (bundle == null) {
  71                 try {
  72                     bundle = ResourceBundle.getBundle(bundlename, _locale);
  73                 } catch (MissingResourceException e) {
  74                     // work around a bug in the com.sun.enterprise.deployment.WebBundleArchivist:
  75                     //   all files with an extension different from .class (hence all the .properties files)
  76                     //   get copied to the top level directory instead of being in the package where they
  77                     //   are defined
  78                     // so, since we can't find the bundle under its proper name, we look for it under
  79                     //   the top-level package
  80 
  81                     int i = bundlename.lastIndexOf('.');
  82                     if (i != -1) {
  83                         String alternateBundleName =
  84                             bundlename.substring(i + 1);
  85                         try {
  86                             bundle =
  87                                 ResourceBundle.getBundle(
  88                                     alternateBundleName,
  89                                     _locale);
  90                         } catch (MissingResourceException e2) {
  91                             // give up
  92                             return getDefaultMessage(l);
  93                         }
  94                     }
  95                 }
  96 
  97                 _resourceBundles.put(bundlename, bundle);
  98             }
  99 
 100             if (bundle == null) {
 101                 return getDefaultMessage(l);
 102             }
 103 
 104             if (key == null)
 105                 key = "undefined";
 106 
 107             String msg;
 108             try {
 109                 msg = bundle.getString(key);
 110             } catch (MissingResourceException e) {
 111                 // notice that this may throw a MissingResourceException of its own (caught below)
 112                 msg = bundle.getString("undefined");
 113             }
 114 
 115             // localize all arguments to the given localizable object
 116             Object[] args = l.getArguments();
 117             for (int i = 0; i < args.length; ++i) {
 118                 if (args[i] instanceof Localizable)
 119                     args[i] = localize((Localizable) args[i]);
 120             }
 121 
 122             String message = MessageFormat.format(msg, args);
 123             return message;
 124 
 125         } catch (MissingResourceException e) {
 126             return getDefaultMessage(l);
 127         }
 128 
 129     }
 130 
 131     private String getDefaultMessage(Localizable l) {
 132         String key = l.getKey();
 133         Object[] args = l.getArguments();
 134         StringBuilder sb = new StringBuilder();
 135         sb.append("[failed to localize] ");
 136         sb.append(key);
 137         if (args != null) {
 138             sb.append('(');
 139             for (int i = 0; i < args.length; ++i) {
 140                 if (i != 0)
 141                     sb.append(", ");
 142                 sb.append(String.valueOf(args[i]));
 143             }
 144             sb.append(')');
 145         }
 146         return sb.toString();
 147     }
 148 
 149 }