001    ////$HeadURL: svn+ssh://developername@svn.wald.intevation.org/deegree/base/trunk/src/org/deegree/graphics/charts/Messages.java $
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2009 by:
005       Department of Geography, University of Bonn
006     and
007       lat/lon GmbH
008    
009     This library is free software; you can redistribute it and/or modify it under
010     the terms of the GNU Lesser General Public License as published by the Free
011     Software Foundation; either version 2.1 of the License, or (at your option)
012     any later version.
013     This library is distributed in the hope that it will be useful, but WITHOUT
014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016     details.
017     You should have received a copy of the GNU Lesser General Public License
018     along with this library; if not, write to the Free Software Foundation, Inc.,
019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020    
021     Contact information:
022    
023     lat/lon GmbH
024     Aennchenstr. 19, 53177 Bonn
025     Germany
026     http://lat-lon.de/
027    
028     Department of Geography, University of Bonn
029     Prof. Dr. Klaus Greve
030     Postfach 1147, 53001 Bonn
031     Germany
032     http://www.geographie.uni-bonn.de/deegree/
033    
034     e-mail: info@deegree.org
035    ----------------------------------------------------------------------------*/
036    
037    package org.deegree.graphics.charts;
038    
039    import java.io.IOException;
040    import java.io.InputStream;
041    import java.text.MessageFormat;
042    import java.util.HashMap;
043    import java.util.Iterator;
044    import java.util.Locale;
045    import java.util.Map;
046    import java.util.Properties;
047    
048    import org.deegree.framework.log.ILogger;
049    import org.deegree.framework.log.LoggerFactory;
050    import org.deegree.framework.util.BootLogger;
051    
052    /**
053     * i118n class for error messages
054     *
055     * @author <a href="mailto:elmasry@lat-lon.de">Moataz Elmasry</a>
056     * @author last edited by: $Author: elmasry $
057     *
058     * @version $Revision: 1.1 $, $Date: 2008-03-07 14:49:52 $
059     */
060    public class Messages {
061    
062        private static final ILogger LOG = LoggerFactory.getLogger( Messages.class );
063    
064        private static Properties defaultProps = new Properties();
065    
066        private static Map<Locale, Properties> props = new HashMap<Locale, Properties>();
067    
068        private static String lang;
069    
070        /**
071         * Initialization done at class loading time.
072         */
073        static {
074            try {
075                // load all messages from default file
076                String fileName = "configs.properties";
077                InputStream is = Messages.class.getResourceAsStream( fileName );
078                if ( is == null ) {
079                    BootLogger.log( "Error while initializing " + Messages.class.getName() + " : "
080                                    + " default message file: '" + fileName + " not found." );
081                }
082                is = Messages.class.getResourceAsStream( fileName );
083                defaultProps.load( is );
084                is.close();
085    
086                // override messages using file "/message_en.properties"
087                fileName = "/messages_en.properties";
088                overrideMessages( fileName, defaultProps );
089    
090                lang = Locale.getDefault().getLanguage();
091                if ( !"".equals( lang ) && !"en".equals( lang ) ) {
092                    // override messages using file "org/deegree/i18n/message_LANG.properties"
093                    fileName = "../../i18n/messages_" + lang + ".properties";
094                    overrideMessages( fileName, defaultProps );
095                    // override messages using file "/message_LANG.properties"
096                    fileName = "/messages_" + lang + ".properties";
097                    overrideMessages( fileName, defaultProps );
098                }
099            } catch ( IOException e ) {
100                BootLogger.logError( "Error while initializing " + Messages.class.getName() + " : " + e.getMessage(), e );
101            }
102        }
103    
104        private static void overrideMessages( String propertiesFile, Properties props )
105                                throws IOException {
106            InputStream is = Messages.class.getResourceAsStream( propertiesFile );
107            if ( is != null ) {
108                // override default messages
109                Properties overrideProps = new Properties();
110                overrideProps.load( is );
111                is.close();
112                Iterator<?> iter = overrideProps.keySet().iterator();
113                while ( iter.hasNext() ) {
114                    String key = (String) iter.next();
115                    props.put( key, overrideProps.get( key ) );
116                }
117            }
118        }
119    
120        private static String get( Properties props, String key, Object... args ) {
121            String s = (String) props.get( key );
122            if ( s != null ) {
123                return MessageFormat.format( s, args );
124            }
125    
126            return "$Message with key: " + key + " not found$";
127        }
128    
129        /**
130         * @param loc
131         *            the locale to be used
132         * @param key
133         *            to get
134         * @param arguments
135         *            to fill in the message
136         * @return the localized message
137         */
138        public static synchronized String get( Locale loc, String key, Object... arguments ) {
139            if ( loc.getLanguage().equals( lang ) ) {
140                return get( key, arguments );
141            }
142    
143            if ( !props.containsKey( loc ) ) {
144                Properties p = new Properties();
145    
146                String l = loc.getLanguage();
147    
148                if ( !"".equals( l ) ) {
149                    try {
150                        // override messages in this order:
151                        // messages_en.properties
152                        // /messages_en.properties
153                        // messages_lang.properties
154                        // /messages_lang.properties
155                        String fileName = "../../i18n/messages_en.properties";
156                        overrideMessages( fileName, p );
157                        fileName = "/messages_en.properties";
158                        overrideMessages( fileName, p );
159                        fileName = "../../i18n/messages_" + l + ".properties";
160                        overrideMessages( fileName, p );
161                        fileName = "/messages_" + l + ".properties";
162                        overrideMessages( fileName, p );
163                    } catch ( IOException e ) {
164                        LOG.logError( "Error loading language file for language '" + l + "': ", e );
165                    }
166                }
167    
168                props.put( loc, p );
169            }
170    
171            String s = get( props.get( loc ), key, arguments );
172            return s;
173        }
174    
175        /**
176         * Alias for #getMessage.
177         *
178         * @param key
179         * @param arguments
180         * @return the message
181         */
182        public static String get( String key, Object... arguments ) {
183            return getMessage( key, arguments );
184        }
185    
186        /**
187         * Returns the message assigned to the passed key. If no message is assigned, an error message will be returned that
188         * indicates the missing key.
189         *
190         * @see MessageFormat for conventions on string formatting and escape characters.
191         *
192         * @param key
193         * @param arguments
194         * @return the message assigned to the passed key
195         */
196        public static String getMessage( String key, Object... arguments ) {
197            return get( defaultProps, key, arguments );
198        }
199    
200        /**
201         * @param key
202         * @return String
203         */
204        public static String getString( String key ) {
205            return getMessage( key );
206        }
207    }