KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > fortress > examples > components > TranslatorImpl


1 /*
2
3  ============================================================================
4                    The Apache Software License, Version 1.1
5  ============================================================================
6
7  Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8
9  Redistribution and use in source and binary forms, with or without modifica-
10  tion, are permitted provided that the following conditions are met:
11
12  1. Redistributions of source code must retain the above copyright notice,
13     this list of conditions and the following disclaimer.
14
15  2. Redistributions in binary form must reproduce the above copyright notice,
16     this list of conditions and the following disclaimer in the documentation
17     and/or other materials provided with the distribution.
18
19  3. The end-user documentation included with the redistribution, if any, must
20     include the following acknowledgment: "This product includes software
21     developed by the Apache Software Foundation (http://www.apache.org/)."
22     Alternately, this acknowledgment may appear in the software itself, if
23     and wherever such third-party acknowledgments normally appear.
24
25  4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software Foundation"
26     must not be used to endorse or promote products derived from this software
27     without prior written permission. For written permission, please contact
28     apache@apache.org.
29
30  5. Products derived from this software may not be called "Apache", nor may
31     "Apache" appear in their name, without prior written permission of the
32     Apache Software Foundation.
33
34  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44
45  This software consists of voluntary contributions made by many individuals
46  on behalf of the Apache Software Foundation. For more information on the
47  Apache Software Foundation, please see <http://www.apache.org/>.
48
49 */

50 package org.apache.avalon.fortress.examples.components;
51
52 import java.util.HashMap JavaDoc;
53 import java.util.Map JavaDoc;
54 import java.util.Set JavaDoc;
55 import org.apache.avalon.framework.configuration.Configuration;
56 import org.apache.avalon.framework.configuration.ConfigurationException;
57 import org.apache.avalon.framework.configuration.Configurable;
58 import org.apache.excalibur.instrument.AbstractLogEnabledInstrumentable;
59 import org.apache.excalibur.instrument.CounterInstrument;
60
61 /**
62  * Simple implementation of the <code>Translator</code> component, which
63  * maintains a simple mapping of keys to translated values, created during
64  * configuration.
65  *
66  * <p>
67  * Configuration format:
68  *
69  * <pre>
70  * &lt;translations&gt;
71  * &lt;entry key="hello-world"&gt;
72  * &lt;value language="Deutsch"&gt;Hallo Welt&lt;/value&gt;
73  * &lt;value language="English"&gt;Hello World&lt;/value&gt;
74  * &lt;/entry&gt;
75  * &lt;/translations&gt;
76  * </pre>
77  * </p>
78  *
79  * @avalon.component
80  * @avalon.service type=Translator
81  * @x-avalon.info name=translator
82  * @x-avalon.lifestyle type=singleton
83  *
84  * @author <a HREF="mailto:crafterm@apache.org">Marcus Crafter</a>
85  * @version CVS $Revision: 1.8 $ $Date: 2003/05/30 20:55:10 $
86  */

87 public class TranslatorImpl extends AbstractLogEnabledInstrumentable
88     implements Translator, Configurable
89 {
90     // Instrument to count the number of translations performed
91
private CounterInstrument m_translationsInstrument;
92
93     // internal store of translation mappings
94
private Map JavaDoc m_keys = new java.util.HashMap JavaDoc();
95
96     /**
97      * Create a new TranslatorImpl.
98      */

99     public TranslatorImpl()
100     {
101         addInstrument( m_translationsInstrument = new CounterInstrument( "translations" ) );
102     }
103
104     /**
105      * Configures this component. Reads configuration information
106      * from container and appropriately sets up the internal mapping
107      * array. Configuration syntax is specified in the class header.
108      *
109      * @param config <code>Configuration</code> details
110      * @exception org.apache.avalon.framework.configuration.ConfigurationException if an error occurs
111      */

112     public void configure( Configuration config )
113         throws ConfigurationException
114     {
115         if( config != null )
116         {
117             Configuration[] entries =
118                 config.getChild( "dictionary" ).getChildren( "translation" );
119
120             for( int i = 0; i < entries.length; ++i )
121             {
122                 String JavaDoc key = entries[ i ].getAttribute( "key" );
123                 Configuration[] values = entries[ i ].getChildren( "value" );
124
125                 Map JavaDoc translations = new HashMap JavaDoc();
126
127                 for( int j = 0; j < values.length; ++j )
128                 {
129                     translations.put(
130                         values[ j ].getAttribute( "language" ),
131                         values[ j ].getValue()
132                     );
133                 }
134
135                 m_keys.put( key, translations );
136             }
137
138             if( getLogger().isDebugEnabled() )
139             {
140                 getLogger().debug(
141                     "Translator configured with " + m_keys.size() + " translations"
142                 );
143             }
144         }
145
146         else
147         {
148             if( getLogger().isWarnEnabled() )
149             {
150                 getLogger().warn( "No configuration specified" );
151             }
152         }
153     }
154
155     /**
156      * <code>getSupportedLanguages</code> returns an array of String
157      * objects detailing which languages are supported for the given
158      * key.
159      *
160      * @param key a <code>String</code> value identifying a translation
161      * @return a <code>String[]</code> array containing available language
162      * translations for the given key
163      */

164     public String JavaDoc[] getSupportedLanguages( String JavaDoc key )
165     {
166         Map JavaDoc translations = (Map JavaDoc)m_keys.get( key );
167         Set JavaDoc keys = translations.keySet();
168         return (String JavaDoc[])keys.toArray( new String JavaDoc[]{} );
169     }
170
171     /**
172      * <code>getTranslation</code> obtains a translation for a given
173      * key in a given language. The language parameter must be listed
174      * in <code>getSupportedLanguages</code>.
175      *
176      * @param key a <code>String</code> value identifying a translation
177      * @param language a <code>String</code> value identifying the language
178      * @return translated text
179      */

180     public String JavaDoc getTranslation( String JavaDoc key, String JavaDoc language )
181     {
182         // Notify the Instrument Manager
183
m_translationsInstrument.increment();
184
185         Map JavaDoc translationMap = (Map JavaDoc)m_keys.get( key );
186         return (String JavaDoc)translationMap.get( language );
187     }
188 }
189
190
Popular Tags