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: private final static Log log = LogFactory
024: .getLog(MapMessageResources.class);
025: private Map messages;
026:
027: /**
028: * Creates an empty resources object
029: */
030: public MapMessageResources() {
031: this (null);
032: }
033:
034: /**
035: * Creates a resources object based on the specified
036: * map.
037: * @param messages the map of messages
038: */
039: public MapMessageResources(Map messages) {
040: this (messages, null, "", true);
041: }
042:
043: /**
044: * Creates a resources object based on the specified
045: * map.
046: * @param messages the map of messages
047: * @param factory the MessageResourcesFactory that created us
048: * @param config the configuration parameter
049: */
050: public MapMessageResources(Map messages,
051: MessageResourcesFactory factory, String config) {
052: super (factory, config);
053: this .messages = messages;
054: if (null == this .messages)
055: this .messages = new HashMap();
056: }
057:
058: /**
059: * Creates a resources object based on the specified
060: * map.
061: * @param messages the map of messages
062: * @param factory the MessageResourcesFactory that created us
063: * @param config the configuration parameter
064: * @param returnNull the returnNull property
065: */
066: public MapMessageResources(Map messages,
067: MessageResourcesFactory factory, String config,
068: boolean returnNull) {
069: super (factory, config, returnNull);
070: this .messages = messages;
071: if (null == this .messages)
072: this .messages = new HashMap();
073: }
074:
075: /**
076: * Returns the message for the specified key. The locale
077: * is ignored.
078: * @param locale the locale (ignored)
079: * @param key the message key
080: * @return the message
081: */
082: public String getMessage(Locale locale, String key) {
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: messages.put(key, value);
093: }
094:
095: /**
096: * Adds all messages in the specified map.
097: * @param messages the message map
098: */
099: public void putMessages(Map messages) {
100: this .messages.putAll(messages);
101: }
102:
103: /**
104: * Loads a property file and adds all messages
105: * from the file.
106: * @param propertyFileName the file name
107: */
108: public void putMessages(String propertyFileName) {
109: putMessages(new File(propertyFileName));
110: }
111:
112: /**
113: * Loads a property file and adds all messages
114: * from the file.
115: * @param propertyFile the file
116: */
117: public void putMessages(File propertyFile) {
118: try {
119: FileInputStream inputStream = new FileInputStream(
120: propertyFile);
121: Properties properties = new Properties();
122: properties.load(inputStream);
123: putMessages(properties);
124: } catch (Exception exc) {
125: log.error(exc.getMessage(), exc);
126: }
127: }
128:
129: /**
130: * Clears all messages.
131: */
132: public void clear() {
133: super.formats.clear();
134: messages.clear();
135: }
136: }
|