001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/i18n/Messages.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2006 by:
006     EXSE, Department of Geography, University of Bonn
007     http://www.giub.uni-bonn.de/deegree/
008     lat/lon GmbH
009     http://www.lat-lon.de
010    
011     This library is free software; you can redistribute it and/or
012     modify it under the terms of the GNU Lesser General Public
013     License as published by the Free Software Foundation; either
014     version 2.1 of the License, or (at your option) any later version.
015    
016     This library is distributed in the hope that it will be useful,
017     but WITHOUT ANY WARRANTY; without even the implied warranty of
018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
019     Lesser General Public License for more details.
020    
021     You should have received a copy of the GNU Lesser General Public
022     License along with this library; if not, write to the Free Software
023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
024    
025     Contact:
026    
027     Andreas Poth
028     lat/lon GmbH
029     Aennchenstr. 19
030     53115 Bonn
031     Germany
032     E-Mail: poth@lat-lon.de
033    
034     Prof. Dr. Klaus Greve
035     Department of Geography
036     University of Bonn
037     Meckenheimer Allee 166
038     53115 Bonn
039     Germany
040     E-Mail: greve@giub.uni-bonn.de
041    
042     ---------------------------------------------------------------------------*/
043    package org.deegree.i18n;
044    
045    import java.io.IOException;
046    import java.io.InputStream;
047    import java.text.MessageFormat;
048    import java.util.Iterator;
049    import java.util.Locale;
050    import java.util.Properties;
051    
052    import org.deegree.framework.util.BootLogger;
053    
054    /**
055     * Responsible for the access to messages that are visible to the user.
056     * <p>
057     * Messages are read from the properties file <code>messages_LANG.properties</code> (LANG is always a
058     * lowercased ISO 639 code), so internationalization is supported. If a certain property (or the
059     * property file) for the specific default language of the system is not found, the message is taken
060     * from <code>messages_en.properties</code>.
061     *
062     * @see Locale#getLanguage()
063     *
064     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
065     * @author <a href="mailto:taddei@lat-lon.de">Ugo Taddei</a>
066     * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider</a> 
067     * @author last edited by: $Author: mschneider $
068     *
069     * @version $Revision: 7740 $, $Date: 2007-07-09 16:10:16 +0200 (Mo, 09 Jul 2007) $
070     */
071    public class Messages {
072    
073        /* This definition allows Eclipse to display the content of referenced message keys. */
074        @SuppressWarnings("unused")
075        private static final String BUNDLE_NAME = "org.deegree.i18n.messages_en";
076    
077        private static Properties props = new Properties();
078    
079        /**
080         * Initialization done at class loading time.
081         */
082        static {
083            try {
084                // load all messages from default file ("org/deegree/i18n/message_en.properties")
085                String fileName = "messages_en.properties";
086                InputStream is = Messages.class.getResourceAsStream( fileName );
087                if ( is == null ) {
088                    BootLogger.log( "Error while initializing " + Messages.class.getName() + " : "
089                                    + " default message file: '" + fileName + " not found." );
090                }
091                is = Messages.class.getResourceAsStream( fileName );
092                props.load( is );
093                is.close();
094    
095                // override messages using file "/message_en.properties"
096                fileName = "/messages_en.properties";
097                overrideMessages(fileName);
098               
099                String lang = Locale.getDefault().getLanguage();
100                if (!"".equals (lang) && !"en".equals(lang)) {
101                    // override messages using file "org/deegree/i18n/message_LANG.properties"                
102                    fileName = "messages_" + lang + ".properties";
103                    overrideMessages(fileName);
104                    // override messages using file "/message_LANG.properties"
105                    fileName = "/messages_" + lang + ".properties";
106                    overrideMessages(fileName);
107                }
108            } catch ( IOException e ) {
109                BootLogger.logError( "Error while initializing " + Messages.class.getName() + " : "
110                                     + e.getMessage(), e );
111            }
112        }
113    
114        private static void overrideMessages( String propertiesFile ) throws IOException {
115            InputStream is = Messages.class.getResourceAsStream( propertiesFile );
116            if ( is != null ) {
117                // override default messages 
118                Properties overrideProps = new Properties();
119                overrideProps.load( is );
120                is.close();
121                Iterator iter = overrideProps.keySet().iterator();
122                while ( iter.hasNext() ) {
123                    String key = (String) iter.next();
124                    props.put( key, overrideProps.get( key ) );
125                }
126            }
127        }
128    
129        /**
130         * Returns the message assigned to the passed key. If no message is assigned, an error message
131         * will be returned that indicates the missing key. 
132         * 
133         * @see MessageFormat for conventions on string formatting and escape characters.
134         *  
135         * @param key
136         * @param arguments
137         * @return the message assigned to the passed key
138         */
139        public static String getMessage( String key, Object... arguments ) {
140            String s = props.getProperty( key );
141            if ( s != null  ) {
142                return MessageFormat.format( s, arguments );
143            } 
144            
145            // to avoid NPEs
146            return "$Message with key: " + key + " not found$";
147        }
148    }