1 /*
   2  * Copyright (c) 1997, 2017, 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<String, ResourceBundle> _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 
  65         String bundlename = l.getResourceBundleName();
  66 
  67         try {
  68             ResourceBundle bundle =
  69                 (ResourceBundle) _resourceBundles.get(bundlename);
  70 
  71             if (bundle == null) {
  72                 bundle = l.getResourceBundle(_locale);
  73                 if (bundle != null) {
  74                     _resourceBundles.put(bundlename, bundle);
  75                 }
  76             }
  77 
  78             if (bundle == null) {
  79                 try {
  80                     bundle = ResourceBundle.getBundle(bundlename, _locale);
  81                 } catch (MissingResourceException e) {
  82                     // work around a bug in the com.sun.enterprise.deployment.WebBundleArchivist:
  83                     //   all files with an extension different from .class (hence all the .properties files)
  84                     //   get copied to the top level directory instead of being in the package where they
  85                     //   are defined
  86                     // so, since we can't find the bundle under its proper name, we look for it under
  87                     //   the top-level package
  88 
  89                     int i = bundlename.lastIndexOf('.');
  90                     if (i != -1) {
  91                         String alternateBundleName =
  92                             bundlename.substring(i + 1);
  93                         try {
  94                             bundle =
  95                                 ResourceBundle.getBundle(
  96                                     alternateBundleName,
  97                                     _locale);
  98                         } catch (MissingResourceException e2) {
  99                             //try context classloader
 100                             try {
 101                                 bundle = ResourceBundle.getBundle(bundlename, _locale, Thread.currentThread().getContextClassLoader());
 102                             } catch (MissingResourceException e3) {
 103                                 // give up
 104                                 return getDefaultMessage(l);
 105                             }
 106 
 107                         }
 108                     }
 109                 }
 110 
 111                 _resourceBundles.put(bundlename, bundle);
 112             }
 113 
 114             if (bundle == null) {
 115                 return getDefaultMessage(l);
 116             }
 117 
 118             if (key == null)
 119                 key = "undefined";
 120 
 121             String msg;
 122             try {
 123                 msg = bundle.getString(key);
 124             } catch (MissingResourceException e) {
 125                 // notice that this may throw a MissingResourceException of its own (caught below)
 126                 msg = bundle.getString("undefined");
 127             }
 128 
 129             // localize all arguments to the given localizable object
 130             Object[] args = l.getArguments();
 131             for (int i = 0; i < args.length; ++i) {
 132                 if (args[i] instanceof Localizable)
 133                     args[i] = localize((Localizable) args[i]);
 134             }
 135 
 136             String message = MessageFormat.format(msg, args);
 137             return message;
 138 
 139         } catch (MissingResourceException e) {
 140             return getDefaultMessage(l);
 141         }
 142 
 143     }
 144 
 145     private String getDefaultMessage(Localizable l) {
 146         String key = l.getKey();
 147         Object[] args = l.getArguments();
 148         StringBuilder sb = new StringBuilder();
 149         sb.append("[failed to localize] ");
 150         sb.append(key);
 151         if (args != null) {
 152             sb.append('(');
 153             for (int i = 0; i < args.length; ++i) {
 154                 if (i != 0)
 155                     sb.append(", ");
 156                 sb.append(String.valueOf(args[i]));
 157             }
 158             sb.append(')');
 159         }
 160         return sb.toString();
 161     }
 162 }