View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.i18n;
35  
36  import info.magnolia.cms.util.ClasspathResourcesUtil;
37  
38  import java.io.IOException;
39  import java.io.InputStream;
40  import java.util.Iterator;
41  import java.util.Locale;
42  import java.util.MissingResourceException;
43  import java.util.PropertyResourceBundle;
44  import java.util.ResourceBundle;
45  
46  import org.apache.commons.collections.IteratorUtils;
47  import org.apache.commons.lang.StringUtils;
48  import org.apache.commons.io.IOUtils;
49  
50  
51  /**
52   * @author Philipp Bracher
53   * @version $Revision: 32667 $ ($Author: gjoseph $)
54   */
55  public class DefaultMessagesImpl extends AbstractMessagesImpl {
56  
57      protected DefaultMessagesImpl(String basename, Locale locale) {
58          super(basename, locale);
59      }
60  
61      /**
62       * Get the message from the bundle.
63       * @param key the key
64       * @return message
65       */
66      public String get(String key) {
67          if(key == null){
68              return "??????";
69          }
70          try {
71              return getBundle().getString(key);
72          }
73          catch (MissingResourceException e) {
74              return "???" + key + "???"; //$NON-NLS-1$ //$NON-NLS-2$
75          }
76      }
77  
78      /**
79       * Returns the bundle for the current basename.
80       */
81      protected ResourceBundle getBundle() {
82          if (bundle == null) {
83              InputStream stream = null;
84              try {
85                  // TODO : isnt this what ResourceBundle does? except maybe for some ClasspathResourcesUtil magic ? 
86                  final String path = "/" + StringUtils.replace(basename, ".", "/");
87                  final Locale locale = getLocale();
88                  final Locale defaultLocale = MessagesManager.getInstance().getDefaultLocale();
89  
90                  stream = ClasspathResourcesUtil.getStream(path + "_" + locale.getLanguage() + "_" + locale.getCountry() + ".properties", false);
91                  if (stream == null) {
92                      stream = ClasspathResourcesUtil.getStream(path + "_" + locale.getLanguage() + ".properties", false);
93                  }
94                  if (stream == null) {
95                      stream = ClasspathResourcesUtil.getStream(path + "_" + defaultLocale.getLanguage() + ".properties", false);
96                  }
97                  if (stream == null) {
98                      stream = ClasspathResourcesUtil.getStream(path + ".properties", false);
99                  }
100 
101                 if (stream != null) {
102                     bundle = new PropertyResourceBundle(stream);
103                 }
104                 else {
105                     bundle = ResourceBundle.getBundle(getBasename(), locale);
106                 }
107             }
108             catch (IOException e) {
109                 log.error("can't load messages for " + basename);
110             } finally {
111                 IOUtils.closeQuietly(stream);
112             }
113         }
114         return bundle;
115     }
116 
117     public void reload() throws Exception {
118         this.bundle = null;
119     }
120 
121     /**
122      * Iterate over the keys.
123      */
124     public Iterator keys() {
125         ResourceBundle bundle = this.getBundle();
126         if(bundle != null) {
127             return IteratorUtils.asIterator(bundle.getKeys());
128         }
129         return IteratorUtils.EMPTY_ITERATOR;
130     }
131 }