| 1 | /* |
| 2 | * This file is part of JON. |
| 3 | * |
| 4 | * JON is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Lesser General Public License as published by |
| 6 | * the Free Software Foundation; either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * JON is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public License |
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | * Copyright 2007 Michael Doberenz |
| 18 | */ |
| 19 | package org.fuwjax.util; |
| 20 | |
| 21 | import java.util.MissingResourceException; |
| 22 | import java.util.ResourceBundle; |
| 23 | |
| 24 | /** |
| 25 | * Allows for Enum message bundling. This class helps ease the use of Enum-based |
| 26 | * resource bundles. |
| 27 | * @author michaeldoberenz |
| 28 | * @param <E> the enum used as keys into this bundle |
| 29 | */ |
| 30 | public class EnumBundle<E extends Enum<E>>{ |
| 31 | private ResourceBundle bundle; |
| 32 | |
| 33 | /** |
| 34 | * Creates a new EnumBundle for the specified class. This method attempts to |
| 35 | * load the properties file corresponding to <code>prefix</code> and simple |
| 36 | * name of <code>cls</code> concatenated. If an appropriately named file is |
| 37 | * not found, this constructor does not throw an exception. Instead all |
| 38 | * subsequent calls to the methods of this instance will return their default |
| 39 | * values. |
| 40 | * @param prefix the prefix for the properties file name |
| 41 | * @param cls the enum providing keys into this bundle |
| 42 | */ |
| 43 | public EnumBundle(final String prefix, final Class<E> cls){ |
| 44 | try{ |
| 45 | bundle = ResourceBundle.getBundle(prefix + cls.getSimpleName()); |
| 46 | }catch(NullPointerException e){ |
| 47 | bundle = null; |
| 48 | }catch(MissingResourceException e){ |
| 49 | bundle = null; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Creates a new EnumBundle for the specified class. This method attempts to |
| 55 | * load the correspondingly named properties file from the classpath. If an |
| 56 | * appropriately named file is not found, this constructor does not throw an |
| 57 | * exception. Instead all subsequent calls to the methods of this instance |
| 58 | * will return their default values; |
| 59 | * @param cls the enum providing keys into this bundle |
| 60 | */ |
| 61 | public EnumBundle(final Class<E> cls){ |
| 62 | this("", cls); //$NON-NLS-1$ |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns the string in this bundle corresponding to the specified key's |
| 67 | * name. Returns the name if no such string exists. |
| 68 | * @param key the key whose name corresponds to an entry in the bundle. |
| 69 | * @return the string value corresponding to the specified key's name, or the |
| 70 | * key's name if no such string exists. |
| 71 | * @throws NullPointerException if key is null |
| 72 | */ |
| 73 | public String getString(final E key) throws NullPointerException{ |
| 74 | try{ |
| 75 | return bundle.getString(key.name()); |
| 76 | }catch(NullPointerException e){ |
| 77 | return key.name(); |
| 78 | }catch(MissingResourceException e){ |
| 79 | return key.name(); |
| 80 | } |
| 81 | } |
| 82 | } |