mschachter 01/06/18 16:54:20 Modified: resources/src/java/org/apache/commons/resources MessageResources.java Added: resources/src/java/org/apache/commons/resources FileResource.java Log: - Modify MessageResources to extend AbstractResource - Add FileResource class Revision Changes Path 1.2 +24 -5 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/MessageResources.java Index: MessageResources.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/MessageResources.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- MessageResources.java 2001/05/30 16:45:49 1.1 +++ MessageResources.java 2001/06/18 23:54:20 1.2 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/MessageResources.java,v 1.1 2001/05/30 16:45:49 craigmcc Exp $ - * $Revision: 1.1 $ - * $Date: 2001/05/30 16:45:49 $ + * $Header: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/MessageResources.java,v 1.2 2001/06/18 23:54:20 mschachter Exp $ + * $Revision: 1.2 $ + * $Date: 2001/06/18 23:54:20 $ * * ==================================================================== * @@ -67,6 +67,7 @@ import java.text.MessageFormat; import java.util.HashMap; import java.util.Locale; +import java.util.TimeZone; /** @@ -89,10 +90,10 @@ * application server environments. * * @author Craig R. McClanahan - * @version $Revision: 1.1 $ $Date: 2001/05/30 16:45:49 $ + * @version $Revision: 1.2 $ $Date: 2001/06/18 23:54:20 $ */ -public abstract class MessageResources implements Serializable { +public abstract class MessageResources extends AbstractResource { // ------------------------------------------------------------- Properties @@ -183,6 +184,24 @@ // --------------------------------------------------------- Public Methods + /** + * Implements the getData method of the Resource interface + */ + public byte[] getData(String key, Locale locale, TimeZone timeZone) { + String data = getMessage(locale, key); + if (data != null) { + return data.getBytes(); + } + return null; + } + + /** + * Implements the getString method of the Resource interface + */ + public String getString(String key, Locale locale, TimeZone timeZone) { + return getMessage(locale, key); + } + /** * Returns a text message for the specified key, for the default Locale. 1.1 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/FileResource.java Index: FileResource.java =================================================================== package org.apache.commons.resources; import java.util.Locale; import java.util.TimeZone; import java.util.Iterator; import java.util.Collection; import java.util.MissingResourceException; import java.io.File; import java.io.InputStream; import java.io.IOException; import java.io.FileInputStream; import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; /** * This class represents a file data resource. */ public class FileResource extends AbstractResource { /** * The character used to separate locale and time zone id's in files */ public static String ID_SEPARATOR = "_"; /** * The logical name of this resource */ protected String name; /** * The base directory of the resource */ protected String baseDir = ""; /** * Get the name of this resource * @return A logical String representation of the name of this resource */ public String getName() { return name; } /** * Set the name of this resource * @param name The name of this resource */ public void setName(String name) { this.name = name; } /** * Sets the base directory of this resource. */ public void setBaseDir(String baseDir) { this.baseDir = baseDir; } /** * Gets the base directory for this resource */ public String getBaseDir() { return baseDir; } /** * Retrieves content based on the locale and time zone specified. * @param key The key for the content * @param locale The locale to retreive the content in, if <code>null</code>, * the default locale * @param timeZone The time zone to retrieve the content for, if <code>null</code>, * the default time zone */ public byte[] getData(String key, Locale locale, TimeZone timeZone) throws MissingResourceException { byte[] data = null; File file = null; try { file = findFile(key, locale, timeZone); InputStream stream = new FileInputStream(file); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] content = new byte[(int) file.length()]; stream.read(content); baos.write(content); data = baos.toByteArray(); } catch (IOException ioe) { throw new MissingResourceException("IOException while reading " + "file '" + file.getAbsolutePath() + "': " + ioe.getMessage(), baseDir, key); } return data; } /** * Retrieves an InputStream representing the content based on the key, locale, * and time zone specified * @param key The key for the content * @param locale The locale to retreive the content in, if <code>null</code>, * the default locale * @param timeZone The time zone to retrieve the content for, if <code>null</code>, * the default time zone */ public InputStream getStream(String key, Locale locale, TimeZone timeZone) throws MissingResourceException { InputStream stream = null; File file = findFile(key, locale, timeZone); if ((file != null) && (file.exists())) { try { stream = new FileInputStream(file); } catch (FileNotFoundException fnfe) { throw new MissingResourceException("File '" + file.getAbsolutePath() + "' not found" , baseDir, key); } catch (IOException ioe) { throw new MissingResourceException("IOException while reading " + "file '" + file.getAbsolutePath() + "': " + ioe.getMessage(), baseDir, key); } } else { throw new MissingResourceException("File '" + file.getAbsolutePath() + "' cannot be found", baseDir, key); } return stream; } /** * Locates the file specified by key, locale, and timeZone. * It searches for files in the following way:<br /> * <blockquote> * 1. <i>baseDir/key_language1_countryCode1_timeZone1.extension</i><br /> * 2. <i>baseDir/key_language1_countryCode1.extension</i><br /> * 3. <i>baseDir/key_language1.extension</i><br /> * 4. <i>baseDir/key.extension</i><br /> * </blockquote> * Where <i>extension</i> is the file extension (anything after the first * "." in a file name), and <i>baseDir</i> is the * qualified file path representing the base of the file resources, and * <i>key</i> is either a file name or a relative file path minus the * <i>extension</i>. language1, countryCode1, and timeZone1 represent * the respective codes of the locale and time zone given. language2, * countryCode, and timeZone2 represent the system default locale and timezone. * @return A file representing the data, or <code>null</code> if the file * couldn't be found */ protected File findFile(String key, Locale locale, TimeZone timeZone) { String fileName = null; String extension = ""; String language = null; String countryCode = null; String zone = null; int dotIndex = key.indexOf("."); if (dotIndex > -1) { fileName = key.substring(0, dotIndex); extension = key.substring(dotIndex+1, key.length()); } else { fileName = key; } if (locale != null) { language = locale.getLanguage(); countryCode = locale.getCountry(); } if (timeZone != null) { zone = timeZone.getID(); } return findFile(fileName, language, countryCode, zone, extension); } /** * Get a the first file that exists according to the naming conventions * listed in {@link #findFile(String,Locale,TimeZone) findFile} * @param fileName the name of the file minus the extension, should * not be <code>null</code> * @param language The lower-case, two letter language code of the locale, * or <code>null</code>. * @param countryCode The upper-case, two letter country code of the locale, * or <code>null</code>. * @param timeZone The upper-case, three letter time zone ID of the time zone, * or <code>null</code>. * @param extension The file extension, that is, anything after the "." in * a file name * @return An collection of files that follow the conventions according * to the non-null parameters given */ protected File findFile(String fileName, String language,String countryCode, String timeZone, String extension) { if (baseDir == null) { baseDir = ""; } String base = baseDir + File.separator + fileName; String[] names = new String[4]; if ((extension != null) && (!"".equals(extension))) { extension = "." + extension; } else { extension = ""; } names[3] = base + extension; if ((language != null) && (!"".equals(language))) { names[2] = base + ID_SEPARATOR + language + extension; if ((countryCode != null) && (!"".equals(countryCode))) { names[1] = base + ID_SEPARATOR + language + ID_SEPARATOR + countryCode + extension; if ((timeZone != null) && (!"".equals(timeZone))) { names[0] = base + ID_SEPARATOR + language + ID_SEPARATOR + countryCode + ID_SEPARATOR + timeZone + extension; } } } for (int i = 0; i < names.length; i++) { String name = names[i]; if ((name != null) && (!"".equals(name))) { File file = new File(name); if (file.exists()) { return file; } } } return null; } }