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.text.MessageFormat;
23  import java.util.ArrayList;
24  import java.util.Enumeration;
25  import java.util.HashMap;
26  import java.util.Iterator;
27  import java.util.List;
28  import java.util.Locale;
29  import java.util.Map;
30  import java.util.MissingResourceException;
31  import java.util.ResourceBundle;
32  import java.util.logging.Level;
33  import java.util.logging.Logger;
34  
35  /***
36   * @author Daniel Florey
37   *
38   */
39  public class ResourceBundleMessageProvider implements MessageProvider {
40      private static Logger logger = Logger.getLogger(ResourceBundleMessageProvider.class.getName());
41  
42      private static List installedResourceBundles = new ArrayList();
43  
44      public String getText(String id, String entry, Locale locale) throws MessageNotFoundException {
45          String text = null;
46          for ( Iterator i = installedResourceBundles.iterator(); i.hasNext(); ) {
47              String baseName = (String)i.next();
48              try {
49                  ResourceBundle resourceBundle = ResourceBundle.getBundle(baseName, locale);
50                  try {
51                      return resourceBundle.getString(id+"."+entry);
52                  } catch ( ClassCastException e ) {
53                      // ignore all entries that are not of type String
54                  } catch ( MissingResourceException e ) {
55                      // skip resource bundle if it is not containing the desired entry
56                  }
57              } catch ( MissingResourceException e ) {
58                  logger.log(
59                          Level.WARNING, 
60                          MessageFormat.format(
61                                  MessageManager.INTERNAL_MESSAGES.getString(MessageManager.RESOURCE_BUNDLE_NOT_FOUND),
62                                  new String[] { baseName })); 
63                  i.remove();
64              }
65          }
66          throw new MessageNotFoundException(MessageFormat.format(
67                  MessageManager.INTERNAL_MESSAGES.getString(MessageManager.NO_MESSAGE_ENTRIES_FOUND),
68                  new String[] { id })); 
69      }
70  
71      public Map getEntries(String id, Locale locale) {
72          String messageIdentifier = id+".";
73          Map entries = null;
74          for ( Iterator i = installedResourceBundles.iterator(); i.hasNext(); ) {
75              String baseName = (String)i.next();
76              try {
77                  ResourceBundle resourceBundle = ResourceBundle.getBundle(baseName, locale);
78                  Enumeration keys = resourceBundle.getKeys();
79                  while ( keys.hasMoreElements() ) {
80                      String key = (String)keys.nextElement();
81                      if ( key.startsWith(messageIdentifier) ) {
82                          if ( entries == null ) {
83                              entries = new HashMap(); 
84                          }
85                          entries.put(key.substring(messageIdentifier.length()), resourceBundle.getString(key));
86                      }
87                  }
88              } catch ( MissingResourceException e ) {
89                  logger.log(
90                          Level.WARNING, 
91                          MessageFormat.format(
92                                  MessageManager.INTERNAL_MESSAGES.getString(MessageManager.RESOURCE_BUNDLE_NOT_FOUND),
93                                  new String[] { baseName })); 
94              }
95          }
96          if ( entries == null ) {
97              throw new MessageNotFoundException(MessageFormat.format(
98                      MessageManager.INTERNAL_MESSAGES.getString(MessageManager.NO_MESSAGE_ENTRIES_FOUND),
99                      new String[] { id })); 
100         }
101         return entries;
102     }
103     
104     public static void install(String baseName) {
105         if ( !installedResourceBundles.contains(baseName) ) 
106             installedResourceBundles.add(baseName); 
107     }
108     
109     public static void uninstall(String baseName) {
110         installedResourceBundles.remove(baseName);
111     }
112     
113     public static void update(String baseName) {
114         uninstall(baseName);
115         install(baseName);
116     }
117 }