1 /*
   2  * Copyright (c) 2011, 2014, 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.webkit;
  27 
  28 import java.io.IOException;
  29 import java.io.InputStreamReader;
  30 import java.net.URL;
  31 import java.util.Locale;
  32 import java.util.PropertyResourceBundle;
  33 import java.util.ResourceBundle;
  34 import java.util.logging.Level;
  35 import java.util.logging.Logger;
  36 
  37 final class LocalizedStrings {
  38     private final static Logger log =
  39         Logger.getLogger(LocalizedStrings.class.getName());
  40 
  41     private final static ResourceBundle BUNDLE =
  42         ResourceBundle.getBundle("com.sun.webkit.LocalizedStrings",
  43             Locale.getDefault(), new EncodingResourceBundleControl("utf-8"));
  44 
  45     /** Private ctor to avoid unexpected instantiation */
  46     private LocalizedStrings() {}
  47 
  48     private static String getLocalizedProperty(String propName) {
  49         log.log(Level.FINE, "Get property: " + propName);
  50         String propValue = BUNDLE.getString(propName);
  51         if ((propValue != null) && (propValue.trim().length() > 0)) {
  52             log.log(Level.FINE, "Property value: " + propValue);
  53             return propValue.trim();
  54         }
  55         log.log(Level.FINE, "Unknown property value");
  56         return null;
  57     }
  58 
  59     private static final class EncodingResourceBundleControl
  60         extends ResourceBundle.Control
  61     {
  62         private final String encoding;
  63 
  64         private EncodingResourceBundleControl(String encoding) {
  65             this.encoding = encoding;
  66         }
  67 
  68         @Override
  69         public ResourceBundle newBundle(String baseName, Locale locale,
  70                                         String format, ClassLoader loader,
  71                                         boolean reload)
  72             throws IllegalAccessException, InstantiationException, IOException
  73         {
  74             String bundleName = toBundleName(baseName, locale);
  75             String resourceName = toResourceName(bundleName, "properties");
  76             URL resourceURL = loader.getResource(resourceName);
  77             if (resourceURL != null)
  78             {
  79                 try
  80                 {
  81                     return new PropertyResourceBundle(new InputStreamReader(resourceURL.openStream(), encoding));
  82                 }
  83                 catch (Exception z)
  84                 {
  85                     log.log(Level.FINE, "exception thrown during bundle initialization", z);
  86                 }
  87             }
  88 
  89             return super.newBundle(baseName, locale, format, loader, reload);
  90         }
  91     }
  92 }