From: <fg...@us...> - 2010-09-12 15:28:48
|
Revision: 3081 http://openutils.svn.sourceforge.net/openutils/?rev=3081&view=rev Author: fgiust Date: 2010-09-12 15:28:39 +0000 (Sun, 12 Sep 2010) Log Message: ----------- quick patch for eclipse problems... Added Paths: ----------- trunk/magnolia-test-webapp/src/main/java/info/ trunk/magnolia-test-webapp/src/main/java/info/magnolia/ trunk/magnolia-test-webapp/src/main/java/info/magnolia/cms/ trunk/magnolia-test-webapp/src/main/java/info/magnolia/cms/i18n/ trunk/magnolia-test-webapp/src/main/java/info/magnolia/cms/i18n/DefaultMessagesImpl.java Added: trunk/magnolia-test-webapp/src/main/java/info/magnolia/cms/i18n/DefaultMessagesImpl.java =================================================================== --- trunk/magnolia-test-webapp/src/main/java/info/magnolia/cms/i18n/DefaultMessagesImpl.java (rev 0) +++ trunk/magnolia-test-webapp/src/main/java/info/magnolia/cms/i18n/DefaultMessagesImpl.java 2010-09-12 15:28:39 UTC (rev 3081) @@ -0,0 +1,164 @@ +// avoid NPEs when bundle can't be loaded +/** + * This file Copyright (c) 2003-2010 Magnolia International + * Ltd. (http://www.magnolia-cms.com). All rights reserved. + * + * + * This file is dual-licensed under both the Magnolia + * Network Agreement and the GNU General Public License. + * You may elect to use one or the other of these licenses. + * + * This file is distributed in the hope that it will be + * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the + * implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT. + * Redistribution, except as permitted by whichever of the GPL + * or MNA you select, is prohibited. + * + * 1. For the GPL license (GPL), you can redistribute and/or + * modify this file under the terms of the GNU General + * Public License, Version 3, as published by the Free Software + * Foundation. You should have received a copy of the GNU + * General Public License, Version 3 along with this program; + * if not, write to the Free Software Foundation, Inc., 51 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * 2. For the Magnolia Network Agreement (MNA), this file + * and the accompanying materials are made available under the + * terms of the MNA which accompanies this distribution, and + * is available at http://www.magnolia-cms.com/mna.html + * + * Any modifications to this file must keep this entire header + * intact. + * + */ +package info.magnolia.cms.i18n; + +import info.magnolia.cms.util.ClasspathResourcesUtil; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Iterator; +import java.util.Locale; +import java.util.MissingResourceException; +import java.util.PropertyResourceBundle; +import java.util.ResourceBundle; + +import org.apache.commons.collections.IteratorUtils; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang.StringUtils; + + +/** + * @author Philipp Bracher + * @version $Revision$ ($Author$) + */ +public class DefaultMessagesImpl extends AbstractMessagesImpl +{ + + protected DefaultMessagesImpl(String basename, Locale locale) + { + super(basename, locale); + } + + /** + * Get the message from the bundle. + * @param key the key + * @return message + */ + public String get(String key) + { + if (key == null || getBundle() == null) + { + return "??????"; + } + try + { + return getBundle().getString(key); + } + catch (MissingResourceException e) + { + return "???" + key + "???"; //$NON-NLS-1$ //$NON-NLS-2$ + } + } + + /** + * Returns the bundle for the current basename. + */ + protected ResourceBundle getBundle() + { + if (bundle == null) + { + InputStream stream = null; + try + { + // TODO : isnt this what ResourceBundle does? except maybe for some ClasspathResourcesUtil magic ? + final String path = "/" + StringUtils.replace(basename, ".", "/"); + final Locale locale = getLocale(); + final Locale defaultLocale = MessagesManager.getInstance().getDefaultLocale(); + + stream = ClasspathResourcesUtil.getStream(path + + "_" + + locale.getLanguage() + + "_" + + locale.getCountry() + + ".properties", false); + if (stream == null) + { + stream = ClasspathResourcesUtil.getStream(path + "_" + locale.getLanguage() + ".properties", false); + } + if (stream == null) + { + stream = ClasspathResourcesUtil.getStream( + path + "_" + defaultLocale.getLanguage() + ".properties", + false); + } + if (stream == null) + { + stream = ClasspathResourcesUtil.getStream(path + ".properties", false); + } + + if (stream != null) + { + bundle = new PropertyResourceBundle(stream); + } + else + { + bundle = ResourceBundle.getBundle(getBasename(), locale); + } + } + catch (IOException e) + { + log.error("can't load messages for " + basename); + } + finally + { + IOUtils.closeQuietly(stream); + } + } + + if (bundle == null) + { + log.warn("Cannot load bundle for {}", basename); + } + return bundle; + } + + public void reload() throws Exception + { + this.bundle = null; + } + + /** + * Iterate over the keys. + */ + public Iterator keys() + { + ResourceBundle bundle = this.getBundle(); + if (bundle != null) + { + return IteratorUtils.asIterator(bundle.getKeys()); + } + return IteratorUtils.EMPTY_ITERATOR; + } +} Property changes on: trunk/magnolia-test-webapp/src/main/java/info/magnolia/cms/i18n/DefaultMessagesImpl.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |