|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ConstantStringManager.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 | /* | |
| 2 | * Copyright (c) 2001-2004, | |
| 3 | * RedVerst Group, ISP RAS http://www.ispras.ru | |
| 4 | * All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * | |
| 9 | * 1. Redistributions of source code must retain the above copyright notice, this | |
| 10 | * list of conditions and the following disclaimer. | |
| 11 | * | |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, | |
| 13 | * this list of conditions and the following disclaimer in the documentation | |
| 14 | * and/or other materials provided with the distribution. | |
| 15 | * | |
| 16 | * 3. The names "ATP", "TreeDL", "RedVerst", "ISP RAS" | |
| 17 | * may not be used to endorse or promote products derived from this software | |
| 18 | * without specific prior written permission. | |
| 19 | * | |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
| 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
| 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | |
| 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | |
| 27 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 30 | */ | |
| 31 | ||
| 32 | package com.unitesk.atp.text.localize; | |
| 33 | ||
| 34 | import java.util.HashMap; | |
| 35 | import java.util.Locale; | |
| 36 | import java.util.Map; | |
| 37 | import java.util.MissingResourceException; | |
| 38 | import java.util.ResourceBundle; | |
| 39 | ||
| 40 | /** | |
| 41 | * The default implementation of localized strings manager. | |
| 42 | * Associates object classes and bundles of localized messages. | |
| 43 | * That is for all objects of some class localized strings with the same names are equal. | |
| 44 | * | |
| 45 | * @author <A href="mailto:demakov@ispras.ru">Alexey Demakov</A> | |
| 46 | * @version $Id: ConstantStringManager.java,v 1.4 2005/05/23 05:49:37 all-x Exp $ | |
| 47 | */ | |
| 48 | public class ConstantStringManager implements StringManager | |
| 49 | { | |
| 50 | /** | |
| 51 | * Creates new localized strings manager using default locale | |
| 52 | * and class loader of this class. | |
| 53 | */ | |
| 54 | 0 | public ConstantStringManager() |
| 55 | { | |
| 56 | 0 | this.locale = Locale.getDefault(); |
| 57 | 0 | this.classLoader = getClass().getClassLoader(); |
| 58 | } | |
| 59 | ||
| 60 | /** | |
| 61 | * Creates new localized strings manager using the specified locale | |
| 62 | * and class loader of this class. | |
| 63 | * | |
| 64 | * @param locale The locale to be used by this manager. | |
| 65 | */ | |
| 66 | 0 | public ConstantStringManager( Locale locale ) |
| 67 | { | |
| 68 | 0 | this.locale = locale; |
| 69 | 0 | this.classLoader = getClass().getClassLoader(); |
| 70 | } | |
| 71 | ||
| 72 | /** | |
| 73 | * Creates new localized strings manager with the specified locale and class loader. | |
| 74 | * | |
| 75 | * @param locale The locale to be used by this manager. | |
| 76 | * @param classLoader The class loader to be used by this maanger. | |
| 77 | */ | |
| 78 | 0 | public ConstantStringManager( Locale locale, ClassLoader classLoader ) |
| 79 | { | |
| 80 | 0 | this.locale = locale; |
| 81 | 0 | this.classLoader = classLoader; |
| 82 | } | |
| 83 | ||
| 84 | /** | |
| 85 | * Loads bundle of localized strings for the specified class. | |
| 86 | * <P>This implementation uses | |
| 87 | * {@link ResourceBundle#getBundle(String,Locale,ClassLoader) | |
| 88 | * ResourceBundle.getBundle( cls.getName(), locale, classLoader)} | |
| 89 | * to obtain bundle. <code>locale</code> and <code>classLoader</code> | |
| 90 | * are specified in constructor of this manager. | |
| 91 | * <P>Inheritors can override this method to change a way of bundle loading. | |
| 92 | * | |
| 93 | * @param cls The specified class. | |
| 94 | * @return Bundle for the specified class or <code>null</code> if it is not found. | |
| 95 | */ | |
| 96 | 0 | protected ResourceBundle getBundle( Class cls ) |
| 97 | { | |
| 98 | 0 | try |
| 99 | { | |
| 100 | 0 | return ResourceBundle.getBundle( cls.getName(), locale, classLoader ); |
| 101 | } | |
| 102 | catch( MissingResourceException e ) | |
| 103 | { | |
| 104 | 0 | return null; |
| 105 | } | |
| 106 | } | |
| 107 | ||
| 108 | /** | |
| 109 | * Get localized string for the specified class. | |
| 110 | * The first time this method is called for the specified class | |
| 111 | * it uses {@link #getBundle(Class) getBundle( cls )} to obtain resource bundle. | |
| 112 | * Then {@link ResourceBundle#getString(String) gets string} <code>name</code> from this bundle | |
| 113 | * and customize it with respect to <code>obj</code>. | |
| 114 | * | |
| 115 | * @param cls The specified class. | |
| 116 | * @param name The name of localized string for this class. | |
| 117 | * @return Localized string or <code>null</code> if it is not found. | |
| 118 | */ | |
| 119 | 0 | public String getString( Class cls, String name ) |
| 120 | { | |
| 121 | 0 | String className = cls.getName(); |
| 122 | 0 | ResourceBundle rb = (ResourceBundle)class_to_bundle.get( className ); |
| 123 | 0 | if( rb == null ) |
| 124 | { | |
| 125 | 0 | rb = getBundle( cls ); |
| 126 | 0 | if( rb == null ) return null; |
| 127 | 0 | class_to_bundle.put( className, rb ); |
| 128 | } | |
| 129 | 0 | try |
| 130 | { | |
| 131 | 0 | return rb.getString( name ); |
| 132 | } | |
| 133 | catch( MissingResourceException e ) | |
| 134 | { | |
| 135 | 0 | return null; |
| 136 | } | |
| 137 | } | |
| 138 | ||
| 139 | /** | |
| 140 | * Get localized string for the specified object. | |
| 141 | * It calls {@link #getString(Class,String) getString( obj.getClass(), name )}. | |
| 142 | * This string manager is called 'constant' because it returns the same string | |
| 143 | * for all objects of the same class. | |
| 144 | * | |
| 145 | * @param name The name of localized string for this object. | |
| 146 | * @param obj The specified object. | |
| 147 | * @return Localized string or <code>null</code> if it is not found. | |
| 148 | */ | |
| 149 | 0 | public String getString( String name, Object obj ) |
| 150 | { | |
| 151 | 0 | return getString( obj.getClass(), name ); |
| 152 | } | |
| 153 | ||
| 154 | /** | |
| 155 | * Get localized string for the specified class and customize it for | |
| 156 | * the specified object. | |
| 157 | * It calls {@link #getString(Class,String) getString( cls, name )}. | |
| 158 | * This string manager is called 'constant' because it returns the same string | |
| 159 | * for all objects of the same class. | |
| 160 | * | |
| 161 | * @param cls The specified class. | |
| 162 | * @param name The name of localized string for this class. | |
| 163 | * @param obj Not used. | |
| 164 | * @return Localized string or <code>null</code> if it is not found. | |
| 165 | */ | |
| 166 | 0 | public String getString( Class cls, String name, Object obj ) |
| 167 | { | |
| 168 | 0 | return getString( cls, name ); |
| 169 | } | |
| 170 | ||
| 171 | protected Locale locale; | |
| 172 | protected ClassLoader classLoader; | |
| 173 | protected Map/*String,ResourceBundle*/ class_to_bundle | |
| 174 | = new HashMap/*String,ResourceBundle*/(); | |
| 175 | } |
|
||||||||||