|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| PatternStringManager.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.Locale; | |
| 35 | ||
| 36 | import com.unitesk.atp.text.filters.StringTextReceiver; | |
| 37 | import com.unitesk.atp.text.generation.BasicGenerator; | |
| 38 | import com.unitesk.atp.text.generation.Generator; | |
| 39 | ||
| 40 | /** | |
| 41 | * This {@link StringManager string manager} treats localized strings as | |
| 42 | * {@link com.unitesk.atp.text.generation.Generator#txt(String) patterns}. | |
| 43 | * Pattern parameters are printed using the same resource name. | |
| 44 | * If resource not found {@link #toString()} is used. | |
| 45 | * | |
| 46 | * @author <A href="mailto:demakov@ispras.ru">Alexey Demakov</A> | |
| 47 | * @version $Id: PatternStringManager.java,v 1.3 2006/02/07 15:44:41 all-x Exp $ | |
| 48 | */ | |
| 49 | public class PatternStringManager extends ConstantStringManager | |
| 50 | { | |
| 51 | /** | |
| 52 | * Creates new localized strings manager using default locale | |
| 53 | * and class loader of this class. | |
| 54 | */ | |
| 55 | 0 | public PatternStringManager() |
| 56 | { | |
| 57 | 0 | super(); |
| 58 | ||
| 59 | 0 | initGenerator(); |
| 60 | } | |
| 61 | ||
| 62 | /** | |
| 63 | * Creates new localized strings manager using the specified locale | |
| 64 | * and class loader of this class. | |
| 65 | * | |
| 66 | * @param locale The locale to be used by this manager. | |
| 67 | */ | |
| 68 | 0 | public PatternStringManager( Locale locale ) |
| 69 | { | |
| 70 | 0 | super( locale ); |
| 71 | ||
| 72 | 0 | initGenerator(); |
| 73 | } | |
| 74 | ||
| 75 | /** | |
| 76 | * Creates new localized strings manager with the specified locale and class loader. | |
| 77 | * | |
| 78 | * @param locale The locale to be used by this manager. | |
| 79 | * @param classLoader The class loader to be used by this maanger. | |
| 80 | */ | |
| 81 | 0 | public PatternStringManager( Locale locale, ClassLoader classLoader ) |
| 82 | { | |
| 83 | 0 | super( locale, classLoader ); |
| 84 | ||
| 85 | 0 | initGenerator(); |
| 86 | } | |
| 87 | ||
| 88 | /** | |
| 89 | * Get localized string for the specified class and object. | |
| 90 | * <P>This method calls {@link ConstantStringManager#getString(Class,String,Object) | |
| 91 | * super implementation}, treats returned value as | |
| 92 | * {@link com.unitesk.atp.text.generation.Generator#txt(String) pattern} and substitute | |
| 93 | * its parameters by attributes of the specified object. | |
| 94 | * | |
| 95 | * @param cls The specified class. | |
| 96 | * @param name The name of localized string for this class. | |
| 97 | * @param obj The specified object. | |
| 98 | * @return Localized string. | |
| 99 | */ | |
| 100 | 0 | public String getString( Class cls, String name, Object obj ) |
| 101 | { | |
| 102 | 0 | setResource( name ); |
| 103 | 0 | return substitute( getPattern( cls, name ), obj ); |
| 104 | } | |
| 105 | ||
| 106 | /** | |
| 107 | * Get localized string for the specified object. | |
| 108 | * <P>This method calls {@link ConstantStringManager#getString(String,Object) | |
| 109 | * super implementation}, treats returned value as | |
| 110 | * {@link com.unitesk.atp.text.generation.Generator#txt(String) pattern} and substitute | |
| 111 | * its parameters by attributes of the specified object. | |
| 112 | * | |
| 113 | * @param name The name of localized string for this object. | |
| 114 | * @param obj The specified object. | |
| 115 | * @return Localized string or <code>null</code> if it is not found. | |
| 116 | */ | |
| 117 | 0 | public String getString( String name, Object obj ) |
| 118 | { | |
| 119 | 0 | setResource( name ); |
| 120 | 0 | return substitute( getPattern( name, obj ), obj ); |
| 121 | } | |
| 122 | ||
| 123 | 0 | public String getPattern( String name, Object obj ) |
| 124 | { | |
| 125 | 0 | return super.getString( name, obj ); |
| 126 | } | |
| 127 | ||
| 128 | 0 | public String getPattern( Class cls, String name ) |
| 129 | { | |
| 130 | 0 | return super.getString( cls, name ); |
| 131 | } | |
| 132 | ||
| 133 | 0 | protected synchronized String substitute( String pattern, Object obj ) |
| 134 | { | |
| 135 | 0 | if( pattern == null ) return null; |
| 136 | ||
| 137 | 0 | generator.setVariable( Generator.DEFAULT_NAME, obj ); |
| 138 | 0 | generator.txt( pattern ); |
| 139 | 0 | return res.getText(); |
| 140 | } | |
| 141 | ||
| 142 | 0 | private void initGenerator() |
| 143 | { | |
| 144 | 0 | generator.setFunction( Generator.DEFAULT_NAME, df ); |
| 145 | } | |
| 146 | ||
| 147 | 0 | protected void setResource( String resource ) |
| 148 | { | |
| 149 | 0 | df.setResource( resource ); |
| 150 | } | |
| 151 | ||
| 152 | 0 | protected void setResourceClass( Class resourceClass ) |
| 153 | { | |
| 154 | 0 | df.setResourceClass( resourceClass ); |
| 155 | } | |
| 156 | ||
| 157 | private StringTextReceiver res = new StringTextReceiver(); | |
| 158 | private BasicGenerator generator = new BasicGenerator( res ); | |
| 159 | private PatternDefaultFunction df = new PatternDefaultFunction( this ); | |
| 160 | } |
|
||||||||||