View Javadoc

1   /*
2   *
3   * ====================================================================
4   *
5   * Copyright 2004 The Apache Software Foundation 
6   *
7   * Licensed under the Apache License, Version 2.0 (the "License");
8   * you may not use this file except in compliance with the License.
9   * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20  package org.apache.commons.i18n;
21  
22  import java.util.ArrayList;
23  import java.util.Enumeration;
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Locale;
28  import java.util.Map;
29  import java.util.MissingResourceException;
30  import java.util.ResourceBundle;
31  import java.util.logging.Level;
32  import java.util.logging.Logger;
33  
34  /***
35   * @author Daniel Florey
36   *
37   */
38  public class ResourceBundleMessageProvider implements MessageProvider {
39      private static Logger logger = Logger.getLogger(ResourceBundleMessageProvider.class.getName());
40  
41      private static List installedResourceBundles = new ArrayList();
42  
43      public String getText(String id, String entry, Locale locale) throws MessageNotFoundException {
44          String text = null;
45          for ( Iterator i = installedResourceBundles.iterator(); i.hasNext(); ) {
46              String baseName = (String)i.next();
47              try {
48                  ResourceBundle resourceBundle = ResourceBundle.getBundle(baseName, locale);
49                  try {
50                      return resourceBundle.getString(id+"."+entry);
51                  } catch ( ClassCastException e ) {
52                      // ignore all entries that are not of type String
53                  } catch ( MissingResourceException e ) {
54                      // skip resource bundle if it is not containing the desired entry
55                  }
56              } catch ( MissingResourceException e ) {
57                  logger.log(Level.WARNING, "Could not find resource bundle with base name '"+baseName+"', uninstalling it...");
58                  i.remove();
59              }
60          }
61          throw new MessageNotFoundException("Message with id "+id+" not found");
62      }
63  
64      public Map getEntries(String id, Locale locale) {
65          String messageIdentifier = id+".";
66          Map entries = null;
67          for ( Iterator i = installedResourceBundles.iterator(); i.hasNext(); ) {
68              String baseName = (String)i.next();
69              try {
70                  ResourceBundle resourceBundle = ResourceBundle.getBundle(baseName, locale);
71                  Enumeration keys = resourceBundle.getKeys();
72                  while ( keys.hasMoreElements() ) {
73                      String key = (String)keys.nextElement();
74                      if ( key.startsWith(messageIdentifier) ) {
75                          if ( entries == null ) {
76                              entries = new HashMap(); 
77                          }
78                          entries.put(key.substring(messageIdentifier.length()), resourceBundle.getString(key));
79                      }
80                  }
81              } catch ( MissingResourceException e ) {
82                  logger.log(Level.WARNING, "Could not find resource bundle with base name '"+baseName+"', uninstalling it...");
83                  i.remove();
84              }
85          }
86          if ( entries == null ) {
87              throw new MessageNotFoundException("No entries found for message with id "+id);
88          }
89          return entries;
90      }
91      
92      public static void install(String baseName) {
93          logger.log(Level.FINE, "Installing bundle with base name '"+baseName+"'");
94          installedResourceBundles.add(baseName);
95      }
96      
97      public static void uninstall(String baseName) {
98          logger.log(Level.FINE, "Uninstalling bundle with base name '"+baseName+"'");
99          installedResourceBundles.remove(baseName);
100     }
101     
102     public static void update(String baseName) {
103         uninstall(baseName);
104         install(baseName);
105     }
106 }