001    package com.mockrunner.struts;
002    
003    import java.io.File;
004    import java.io.FileInputStream;
005    import java.util.HashMap;
006    import java.util.Locale;
007    import java.util.Map;
008    import java.util.Properties;
009    
010    import org.apache.commons.logging.Log;
011    import org.apache.commons.logging.LogFactory;
012    import org.apache.struts.util.MessageResources;
013    import org.apache.struts.util.MessageResourcesFactory;
014    
015    /**
016     * This implementation of <code>MessageResources</code>
017     * takes the messages from a <code>Map</code> and can be
018     * used for testing purposes. The <code>Map</code> can
019     * also be filled with the contents of a property file.
020     * Note: This implementation ignores the specified <code>Locale</code>.
021     */
022    public class MapMessageResources extends MessageResources
023    {
024        private final static Log log = LogFactory.getLog(MapMessageResources.class);
025        private Map messages;
026        
027        /**
028         * Creates an empty resources object
029         */
030        public MapMessageResources()
031        {
032            this(null);
033        }
034        
035        /**
036         * Creates a resources object based on the specified
037         * map.
038         * @param messages the map of messages
039         */
040        public MapMessageResources(Map messages)
041        {
042            this(messages, null, "", true);  
043        }
044        
045        /**
046         * Creates a resources object based on the specified
047         * map.
048         * @param messages the map of messages
049         * @param factory the MessageResourcesFactory that created us
050         * @param config the configuration parameter
051         */
052        public MapMessageResources(Map messages, MessageResourcesFactory factory, String config)
053        {
054            super(factory, config);
055            this.messages = messages;
056            if(null == this.messages) this.messages = new HashMap();
057        }
058    
059        /**
060         * Creates a resources object based on the specified
061         * map.
062         * @param messages the map of messages
063         * @param factory the MessageResourcesFactory that created us
064         * @param config the configuration parameter
065         * @param returnNull the returnNull property
066         */
067        public MapMessageResources(Map messages, MessageResourcesFactory factory, String config, boolean returnNull)
068        {
069            super(factory, config, returnNull);
070            this.messages = messages;
071            if(null == this.messages) this.messages = new HashMap();
072        }
073    
074        /**
075         * Returns the message for the specified key. The locale
076         * is ignored.
077         * @param locale the locale (ignored)
078         * @param key the message key
079         * @return the message
080         */
081        public String getMessage(Locale locale, String key)
082        {
083            return (String)messages.get(key);
084        }
085        
086        /**
087         * Adds a message for the specified key.
088         * @param key the message key
089         * @param value the message
090         */
091        public void putMessage(String key, String value)
092        {
093            messages.put(key, value);
094        }
095        
096        /**
097         * Adds all messages in the specified map.
098         * @param messages the message map
099         */
100        public void putMessages(Map messages)
101        {
102            this.messages.putAll(messages);
103        }
104        
105        /**
106         * Loads a property file and adds all messages
107         * from the file.
108         * @param propertyFileName the file name
109         */
110        public void putMessages(String propertyFileName)
111        {
112            putMessages(new File(propertyFileName));
113        }
114        
115        /**
116         * Loads a property file and adds all messages
117         * from the file.
118         * @param propertyFile the file
119         */
120        public void putMessages(File propertyFile)
121        {
122            try
123            {
124                FileInputStream inputStream = new FileInputStream(propertyFile);
125                Properties properties = new Properties();
126                properties.load(inputStream);
127                putMessages(properties);
128            }
129            catch(Exception exc)
130            {
131                log.error(exc.getMessage(), exc);
132            }
133        }
134        
135        /**
136         * Clears all messages.
137         */
138        public void clear()
139        {
140            super.formats.clear();
141            messages.clear();
142        }
143    }