Home | History | Annotate | Download | only in nls
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 /*
     19  * This implementation is based on the class of the same name in
     20  * org.apache.harmony.luni.util.
     21  */
     22 
     23 package org.apache.harmony.beans.internal.nls;
     24 
     25 import java.io.IOException;
     26 import java.io.InputStream;
     27 import java.util.logging.Logger;
     28 import java.util.Locale;
     29 import java.util.PropertyResourceBundle;
     30 import java.util.ResourceBundle;
     31 import java.util.MissingResourceException;
     32 
     33 /**
     34  * This class contains helper methods for loading resource bundles and
     35  * formatting external message strings.
     36  */
     37 public final class MsgHelp {
     38     /** name of the resource for this class */
     39     private static final String RESOURCE_NAME =
     40         "/org/apache/harmony/beans/internal/nls/messages.properties";
     41 
     42     /** the resource bundle for this class */
     43     private static final ResourceBundle THE_BUNDLE;
     44 
     45     static {
     46         ResourceBundle rb = null;
     47 
     48         try {
     49             InputStream in = MsgHelp.class.getResourceAsStream(
     50                     RESOURCE_NAME);
     51             rb = new PropertyResourceBundle(in);
     52         } catch (IOException ex) {
     53             Logger.global.warning("Couldn't read resource bundle: " +
     54                     ex);
     55         } catch (RuntimeException ex) {
     56             // Shouldn't happen, but deal at least somewhat gracefully.
     57             Logger.global.warning("Couldn't find resource bundle: " +
     58                     ex);
     59         }
     60 
     61         THE_BUNDLE = rb;
     62     }
     63 
     64     public static String getString(String msg) {
     65         if (THE_BUNDLE == null) {
     66             return msg;
     67         }
     68         try {
     69             return THE_BUNDLE.getString(msg);
     70         } catch (MissingResourceException e) {
     71             return "Missing message: " + msg;
     72         }
     73     }
     74 
     75     static public String getString(String msg, Object[] args) {
     76         String format = msg;
     77         if (THE_BUNDLE != null) {
     78             try {
     79                 format = THE_BUNDLE.getString(msg);
     80             } catch (MissingResourceException e) {
     81             }
     82         }
     83 
     84         return org.apache.harmony.luni.util.MsgHelp.format(format, args);
     85     }
     86 }
     87