You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by cr...@apache.org on 2003/01/05 04:29:58 UTC
cvs commit: jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl CollResources.java CollResourcesFactory.java CollectionResourcesBaseTestCase.java PropertyResourcesTestCase.java ResourcesBaseTestCase.java TestResources.java
craigmcc 2003/01/04 19:29:58
Modified: resources/src/java/org/apache/commons/resources
Resources.java ResourcesException.java
resources/src/java/org/apache/commons/resources/file
FileResources.java
resources/src/java/org/apache/commons/resources/impl
ResourceBundleResources.java
ResourceBundleResourcesFactory.java
ResourcesBase.java ResourcesFactoryBase.java
resources/src/java/org/apache/commons/resources/message
PropertyMessageResources.java
resources/src/test/org/apache/commons/resources/impl
ResourcesBaseTestCase.java TestResources.java
Added: resources/src/java/org/apache/commons/resources/impl
CollectionResourcesBase.java PropertyResources.java
PropertyResourcesFactory.java
resources/src/test/org/apache/commons/resources/impl
CollResources.java CollResourcesFactory.java
CollectionResourcesBaseTestCase.java
PropertyResourcesTestCase.java
Log:
Complete the major portion of the refactoring. All that's left is:
* Updating the documentation
* Implementing XMLResources (similar in functionality to the old one
but based on the new convenience base class CollectionResourcesBase)
* (Optionally) create a JDBCResources based on the refactored architecture.
* Remove the obsolete stuff
Revision Changes Path
1.6 +24 -18 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/Resources.java
Index: Resources.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/Resources.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- Resources.java 28 Dec 2002 04:40:55 -0000 1.5
+++ Resources.java 5 Jan 2003 03:29:57 -0000 1.6
@@ -174,9 +174,11 @@
*
* @exception ResourcesException if an error occurs retrieving or
* returning the requested content
+ * @exception ResourcesKeyException if the no value for the specified
+ * key was found, and <code>isReturnNull()</code> returns
+ * <code>false</code>
*/
- public byte[] getBytes(String key, Locale locale, TimeZone timeZone)
- throws ResourcesException;
+ public byte[] getBytes(String key, Locale locale, TimeZone timeZone);
/**
@@ -192,10 +194,12 @@
*
* @exception ResourcesException if an error occurs retrieving or
* returning the requested content
+ * @exception ResourcesKeyException if the no value for the specified
+ * key was found, and <code>isReturnNull()</code> returns
+ * <code>false</code>
*/
public InputStream getInputStream(String key, Locale locale,
- TimeZone timeZone)
- throws ResourcesException;
+ TimeZone timeZone);
/**
@@ -211,11 +215,11 @@
*
* @exception ResourcesException if an error occurs retrieving or
* returning the requested content
+ * @exception ResourcesKeyException if the no value for the specified
+ * key was found, and <code>isReturnNull()</code> returns
+ * <code>false</code>
*/
- /* FIXME -- Comment out until implementations are extended to support it
- public Object getObject(String key, Locale locale, TimeZone timeZone)
- throws ResourcesException;
- */
+ public Object getObject(String key, Locale locale, TimeZone timeZone);
/**
@@ -231,9 +235,11 @@
*
* @exception ResourcesException if an error occurs retrieving or
* returning the requested content
+ * @exception ResourcesKeyException if the no value for the specified
+ * key was found, and <code>isReturnNull()</code> returns
+ * <code>false</code>
*/
- public Reader getReader(String key, Locale locale, TimeZone timeZone)
- throws ResourcesException;
+ public Reader getReader(String key, Locale locale, TimeZone timeZone);
/**
@@ -249,11 +255,11 @@
*
* @exception ResourcesException if an error occurs retrieving or
* returning the requested content
+ * @exception ResourcesKeyException if the no value for the specified
+ * key was found, and <code>isReturnNull()</code> returns
+ * <code>false</code>
*/
- public String getString(String key, Locale locale, TimeZone timeZone)
- throws ResourcesException;
-
-
+ public String getString(String key, Locale locale, TimeZone timeZone);
}
1.4 +6 -5 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/ResourcesException.java
Index: ResourcesException.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/ResourcesException.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ResourcesException.java 28 Dec 2002 04:40:55 -0000 1.3
+++ ResourcesException.java 5 Jan 2003 03:29:57 -0000 1.4
@@ -72,7 +72,8 @@
* @author Mike Schachter (mschachter@hp.com)
* @version $Revision$ $Date$
*/
-public class ResourcesException extends Exception implements Serializable {
+public class ResourcesException
+ extends RuntimeException implements Serializable {
protected String message = "";
1.6 +10 -4 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/file/FileResources.java
Index: FileResources.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/file/FileResources.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- FileResources.java 28 Jul 2002 04:24:18 -0000 1.5
+++ FileResources.java 5 Jan 2003 03:29:57 -0000 1.6
@@ -71,6 +71,7 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
+import java.util.TimeZone;
import org.apache.commons.resources.ResourcesBase;
import org.apache.commons.resources.ResourcesException;
@@ -476,6 +477,11 @@
return names;
}
+
+ public Object getObject(String key, Locale locale, TimeZone timeZone) {
+ return (null);
+ }
+
}
1.5 +23 -12 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/ResourceBundleResources.java
Index: ResourceBundleResources.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/ResourceBundleResources.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- ResourceBundleResources.java 28 Dec 2002 23:29:48 -0000 1.4
+++ ResourceBundleResources.java 5 Jan 2003 03:29:57 -0000 1.5
@@ -215,6 +215,9 @@
*
* @exception ResourcesException if an error occurs retrieving or
* returning the requested content
+ * @exception ResourcesKeyException if the no value for the specified
+ * key was found, and <code>isReturnNull()</code> returns
+ * <code>false</code>
*/
public byte[] getBytes(String key, Locale locale, TimeZone timeZone)
throws ResourcesException {
@@ -237,10 +240,12 @@
*
* @exception ResourcesException if an error occurs retrieving or
* returning the requested content
+ * @exception ResourcesKeyException if the no value for the specified
+ * key was found, and <code>isReturnNull()</code> returns
+ * <code>false</code>
*/
public InputStream getInputStream(String key, Locale locale,
- TimeZone timeZone)
- throws ResourcesException {
+ TimeZone timeZone) {
throw new UnsupportedOperationException(); // FIXME
@@ -260,9 +265,11 @@
*
* @exception ResourcesException if an error occurs retrieving or
* returning the requested content
+ * @exception ResourcesKeyException if the no value for the specified
+ * key was found, and <code>isReturnNull()</code> returns
+ * <code>false</code>
*/
- public Object getObject(String key, Locale locale, TimeZone timeZone)
- throws ResourcesException {
+ public Object getObject(String key, Locale locale, TimeZone timeZone) {
try {
ResourceBundle bundle = getBundle(locale, timeZone);
@@ -291,9 +298,11 @@
*
* @exception ResourcesException if an error occurs retrieving or
* returning the requested content
+ * @exception ResourcesKeyException if the no value for the specified
+ * key was found, and <code>isReturnNull()</code> returns
+ * <code>false</code>
*/
- public Reader getReader(String key, Locale locale, TimeZone timeZone)
- throws ResourcesException {
+ public Reader getReader(String key, Locale locale, TimeZone timeZone) {
throw new UnsupportedOperationException(); // FIXME
@@ -313,9 +322,11 @@
*
* @exception ResourcesException if an error occurs retrieving or
* returning the requested content
+ * @exception ResourcesKeyException if the no value for the specified
+ * key was found, and <code>isReturnNull()</code> returns
+ * <code>false</code>
*/
- public String getString(String key, Locale locale, TimeZone timeZone)
- throws ResourcesException {
+ public String getString(String key, Locale locale, TimeZone timeZone) {
if (log.isTraceEnabled()) {
log.trace("Retrieving message for key '" + key + "' for locale '"
1.4 +4 -24 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/ResourceBundleResourcesFactory.java
Index: ResourceBundleResourcesFactory.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/ResourceBundleResourcesFactory.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ResourceBundleResourcesFactory.java 28 Dec 2002 04:40:55 -0000 1.3
+++ ResourceBundleResourcesFactory.java 5 Jan 2003 03:29:57 -0000 1.4
@@ -89,26 +89,6 @@
// --------------------------------------------------------- Public Methods
- /**
- * <p>Create (if necessary) and return a {@link Resources} instance
- * for the specified logical name, with a default configuration.</p>
- * For convenience, the logical name is also assumed to by the
- * configuration String.</p>
- *
- * @param name Logical name of the {@link Resources} instance to
- * be returned
- *
- * @exception ResourcesException if a {@link Resources} instance
- * of the specified logical name cannot be returned.
- */
- public Resources getResources(String name)
- throws ResourcesException {
-
- return (getResources(name, name));
-
- }
-
-
// ------------------------------------------------------ Protected Methods
1.3 +97 -90 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/ResourcesBase.java
Index: ResourcesBase.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/ResourcesBase.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ResourcesBase.java 28 Dec 2002 04:40:55 -0000 1.2
+++ ResourcesBase.java 5 Jan 2003 03:29:57 -0000 1.3
@@ -67,6 +67,8 @@
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import org.apache.commons.resources.Resources;
@@ -76,6 +78,13 @@
/**
* <p>Convenience base class for {@link Resources} implementations.</p>
*
+ * <p>Default implementations of the content retrieval methods are provided
+ * for all methods except <code>getObject()</code>. The default methods for
+ * the other content retrieval methods are coded in terms of
+ * <code>getString()</code>, on the assumption that most uses of
+ * {@link Resources} are of this type. However, they can be easily
+ * overridden as needed.</p>
+ *
* @author Mike Schachter (mschachter@hp.com)
* @author Craig R. McClanahan
* @version $Revision$ $Date$
@@ -116,7 +125,7 @@
/**
* <p>The logical name of this {@link Resources} instance.</p>
*/
- protected String name = null;
+ private String name = null;
/**
@@ -124,7 +133,7 @@
* <code>null</code> (instead of throwing an exception) on invalid
* key values.</p>
*/
- protected boolean returnNull = true;
+ private boolean returnNull = true;
// ------------------------------------------------------ Lifecycle Methods
@@ -135,6 +144,8 @@
* {@link Resources} instance, before any of the <code>getXxx()</code>
* methods are called.</p>
*
+ * <p>The default implementation does nothing.</p>
+ *
* @exception ResourcesException if an error occurs during initialization
*/
public void init() throws ResourcesException {
@@ -150,6 +161,8 @@
* no further calls to any of the <code>getXxx()</code> methods are
* allowed.</p>
*
+ * <p>The default implementation does nothing.</p>
+ *
* @exception ResourcesException if an error occurs during finalization
*/
public void destroy() throws ResourcesException {
@@ -206,6 +219,9 @@
* byte array, localized based on the specified <code>locale</code>
* and/or <code>timeZone</code>.</p>
*
+ * <p>The default implementation calls <code>getString()</code> and
+ * converts the value to a byte array.</p>
+ *
* @param key Identifier for the requested content
* @param locale Locale with which to localize retrieval,
* or <code>null</code> for the default Locale
@@ -214,10 +230,21 @@
*
* @exception ResourcesException if an error occurs retrieving or
* returning the requested content
- */
- public abstract byte[] getBytes(String key, Locale locale,
- TimeZone timeZone)
- throws ResourcesException;
+ * @exception ResourcesKeyException if the no value for the specified
+ * key was found, and <code>isReturnNull()</code> returns
+ * <code>false</code>
+ */
+ public byte[] getBytes(String key, Locale locale,
+ TimeZone timeZone) {
+
+ String value = getString(key, locale, timeZone);
+ if (value == null) {
+ return (null);
+ } else {
+ return (value.getBytes());
+ }
+
+ }
/**
@@ -225,6 +252,9 @@
* InputStream, localized based on the specified <code>locale</code>
* and/or <code>timeZone</code>.</p>
*
+ * <p>The default implementation calls <code>getsBytes()</code>
+ * and returns an input stream over the resulting byte array.</p>
+ *
* @param key Identifier for the requested content
* @param locale Locale with which to localize retrieval,
* or <code>null</code> for the default Locale
@@ -233,10 +263,20 @@
*
* @exception ResourcesException if an error occurs retrieving or
* returning the requested content
- */
- public abstract InputStream getInputStream(String key, Locale locale,
- TimeZone timeZone)
- throws ResourcesException;
+ * @exception ResourcesKeyException if the no value for the specified
+ * key was found, and <code>isReturnNull()</code> returns
+ * <code>false</code>
+ */
+ public InputStream getInputStream(String key, Locale locale,
+ TimeZone timeZone) {
+
+ byte bytes[] = getBytes(key, locale, timeZone);
+ if (bytes == null) {
+ bytes = new byte[0];
+ }
+ return (new ByteArrayInputStream(bytes));
+
+ }
/**
@@ -244,6 +284,9 @@
* Object, localized based on the specified <code>locale</code>
* and/or <code>timeZone</code>.</p>
*
+ * <p>There is no default implementation of this method. Concrete
+ * subclasses must provide such an implementation.</p>
+ *
* @param key Identifier for the requested content
* @param locale Locale with which to localize retrieval,
* or <code>null</code> for the default Locale
@@ -252,10 +295,12 @@
*
* @exception ResourcesException if an error occurs retrieving or
* returning the requested content
+ * @exception ResourcesKeyException if the no value for the specified
+ * key was found, and <code>isReturnNull()</code> returns
+ * <code>false</code>
*/
public abstract Object getObject(String key, Locale locale,
- TimeZone timeZone)
- throws ResourcesException;
+ TimeZone timeZone);
/**
@@ -263,6 +308,9 @@
* Reader, localized based on the specified <code>locale</code>
* and/or <code>timeZone</code>.</p>
*
+ * <p>The default implementation calls <code>getString()</code>
+ * and returns a reader over the resulting characters.</p>
+ *
* @param key Identifier for the requested content
* @param locale Locale with which to localize retrieval,
* or <code>null</code> for the default Locale
@@ -271,10 +319,21 @@
*
* @exception ResourcesException if an error occurs retrieving or
* returning the requested content
- */
- public abstract Reader getReader(String key, Locale locale,
- TimeZone timeZone)
- throws ResourcesException;
+ * @exception ResourcesKeyException if the no value for the specified
+ * key was found, and <code>isReturnNull()</code> returns
+ * <code>false</code>
+ */
+ public Reader getReader(String key, Locale locale,
+ TimeZone timeZone) {
+
+ String value = getString(key, locale, timeZone);
+ if (value == null) {
+ return (null);
+ } else {
+ return (new StringReader(value));
+ }
+
+ }
/**
@@ -282,6 +341,9 @@
* String, localized based on the specified <code>locale</code>
* and/or <code>timeZone</code>.</p>
*
+ * <p>The default implementation calls <code>getObject()</code>
+ * and converts the result to a String if necessary.</p>
+ *
* @param key Identifier for the requested content
* @param locale Locale with which to localize retrieval,
* or <code>null</code> for the default Locale
@@ -290,78 +352,23 @@
*
* @exception ResourcesException if an error occurs retrieving or
* returning the requested content
- */
- public abstract String getString(String key, Locale locale,
- TimeZone timeZone)
- throws ResourcesException;
-
+ * @exception ResourcesKeyException if the no value for the specified
+ * key was found, and <code>isReturnNull()</code> returns
+ * <code>false</code>
+ */
+ public String getString(String key, Locale locale,
+ TimeZone timeZone) {
+
+ Object value = getObject(key, locale, timeZone);
+ if (value == null) {
+ return ((String) null);
+ } else if (value instanceof String) {
+ return ((String) value);
+ } else {
+ return (value.toString());
+ }
-
-
-
-
- /**
- * Retrieves content based on the locale and time zone specified. Note
- * that this method has the potential to cause memory problems for content
- * that is relatively large. The default implementation of this returns
- * the String value of {@link #getBytes(String,Locale,TimeZone) getBytes}
- * with the arguments given in the default system encoding.
- * @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. Resources implementations may ignore
- * the time zone argument.
- * @return A String representing the data specified by key. The encoding
- * of the String is implementation dependent.
- */
- /*
- public String getString(String key, Locale locale, TimeZone timeZone)
- throws ResourcesException {
- return new String(getBytes(key, locale, timeZone));
- }
- */
-
-
- /**
- * Retrieves an InputStream representing the content based on the key, locale,
- * and time zone specified. The default implementation constructs a
- * ByteArrayInputStream based on the bytes retrieved from
- * {@link #getBytes(String,Locale,TimeZone) getBytes}.
- * @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. Resources implementations may ignore
- * the time zone argument all together.
- */
- /*
- public InputStream getInputStream(String key, Locale locale, TimeZone timeZone)
- throws ResourcesException {
- return new ByteArrayInputStream(getBytes(key, locale, timeZone));
}
- */
- /**
- * Retrieves a Reader representing the content based on the key, locale,
- * and time zone specified. The default implementation returns a StringReader
- * with the data returned from {@link #getString(String,Locale,TimeZone) getString}.
- * @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. Resources implementations may ignore
- * the time zone argument all together.
- */
- /*
- public Reader getReader(String key, Locale locale, TimeZone timeZone)
- throws ResourcesException {
- return new StringReader(getString(key, locale, timeZone));
- }
- */
-
}
-
-
-
1.3 +9 -6 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/ResourcesFactoryBase.java
Index: ResourcesFactoryBase.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/ResourcesFactoryBase.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ResourcesFactoryBase.java 28 Dec 2002 04:40:55 -0000 1.2
+++ ResourcesFactoryBase.java 5 Jan 2003 03:29:57 -0000 1.3
@@ -133,7 +133,10 @@
/**
* <p>Create (if necessary) and return a {@link Resources} instance
- * for the specified logical name, with a default configuration.</p>
+ * for the specified logical name, with a default configuration.
+ * The default implementation of this method treats the name as the
+ * configuration String as well, and calls the
+ * <code>getResources(String,String)</code> method.
*
* @param name Logical name of the {@link Resources} instance to
* be returned
@@ -144,7 +147,7 @@
public Resources getResources(String name)
throws ResourcesException {
- return (getResources(name, null));
+ return (getResources(name, name));
}
1.1 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/CollectionResourcesBase.java
Index: CollectionResourcesBase.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/CollectionResourcesBase.java,v 1.1 2003/01/05 03:29:57 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2003/01/05 03:29:57 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.resources.impl;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import org.apache.commons.resources.Resources;
import org.apache.commons.resources.ResourcesException;
import org.apache.commons.resources.ResourcesKeyException;
/**
* <p>Abstract base classes for {@link Resources} implementations that
* store their name-value mappings for each supported <code>Locale</code>
* in a URL-accessible resource file with a common base URL. Subclasses
* need only override <code>loadLocale()</code> to manage the details of
* loading the name-value mappings for a particular Locale.</p>
*
* <p>The <code>timeZone</code> paramter, if any, is ignored by the
* default content retrieval methods.</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2003/01/05 03:29:57 $
*/
public abstract class CollectionResourcesBase extends ResourcesBase {
// ----------------------------------------------------------- Constructors
/**
* <p>Create a new {@link Resources} instance with the specified
* logical name and base URL.</p>
*
* @param name Logical name of the new instance
* @param base Base URL of the resource files that contain the per-Locale
* name-value mappings for this {@link Resources} instance
*/
public CollectionResourcesBase(String name, String base) {
super(name);
this.base = base;
}
// ----------------------------------------------------- Instance Variables
/**
* <p>The base URL for the per-Locale resources files containing the
* name-value mappings for this {@link Resources} instance.</p>
*/
protected String base = null;
/**
* <p>The default <code>Locale</code> to use when no <code>Locale</code>
* is specified by the caller.</p>
*/
protected Locale defaultLocale = Locale.getDefault();
/**
* <p>The previously calculated <code>Locale</code> lists returned
* by <code>getLocaleList()</code>, keyed by <code>Locale</code>.</p>
*/
private Map lists = new HashMap();
/**
* <p>The previously calculated name-value mappings <code>Map</code>s
* returned by <code>getLocaleMap()</code>, keyed by <code>Locale</code>.
* </p>
*/
private Map maps = new HashMap();
// ---------------------------------------------- Content Retrieval Methods
/**
* <p>Return the content for the specified <code>key</code> as an
* Object, localized based on the specified <code>locale</code>
* and/or <code>timeZone</code>.</p>
*
* @param key Identifier for the requested content
* @param locale Locale with which to localize retrieval,
* or <code>null</code> for the default Locale
* @param timeZone TimeZone with which to localize retrieval,
* or <code>null</code> for the default TimeZone
*
* @exception ResourcesException if an error occurs retrieving or
* returning the requested content
* @exception ResourcesKeyException if the no value for the specified
* key was found, and <code>isReturnNull()</code> returns
* <code>false</code>
*/
public Object getObject(String key, Locale locale,
TimeZone timeZone) {
if (locale == null) {
locale = defaultLocale;
}
// Prepare local variables we will need
Object value = null;
List list = getLocaleList(locale);
int n = list.size();
// Search through the Locale hierarchy for this resource key
for (int i = 0; i < n; i++) {
Map map = getLocaleMap((Locale) list.get(i));
if (map.containsKey(key)) {
return (map.get(key));
}
}
// No value for this key was located in the entire hierarchy
if (isReturnNull()) {
return (null);
} else {
throw new ResourcesKeyException(key);
}
}
// ------------------------------------------------------ Lifecycle Methods
/**
* <p>This method must be called when the manager of this resource
* decides that it's no longer needed. After this method is called,
* no further calls to any of the <code>getXxx()</code> methods are
* allowed.</p>
*
* @exception ResourcesException if an error occurs during finalization
*/
public void destroy() throws ResourcesException {
synchronized (lists) {
lists.clear();
}
synchronized (maps) {
maps.clear();
}
}
// ------------------------------------------------------ Protected Methods
/**
* <p>Return a <code>List</code> of Locales that should be searched
* when locating resources for the specified Locale. The returned
* list will start with the specified Locale itself, followed by Locales
* that do not specify variant, country, or language modifiers. For
* example, if you pass in a Locale for the <code>en_US_POSIX</code>
* combination, the returned list will have Locale instances for
* the following country/language/variant combinations:</p>
* <ul>
* <li><code>en_US_POSIX</code></li>
* <li><code>en_US</code></li>
* <li><code>en</code></li>
* <li>(zero-length country, language, and variant)</li>
* </ul>
*
* <p>The search order calculated by this method makes it easy for
* {@link Resources} implementations to implement hierarchical search
* strategies similar to that employed by the standard Java class
* <code>java.util.ResourceBundle</code>.</p>
*
* @param locale Locale on which to base the list calculation
*/
protected List getLocaleList(Locale locale) {
synchronized (lists) {
// Optimized lookup of any previously cached Map for this Locale
List list = (List) lists.get(locale);
if (list != null) {
return (list);
}
// Calculate, cache, and return the list for this Locale
list = new ArrayList();
String language = locale.getLanguage();
int languageLength = language.length();
String country = locale.getCountry();
int countryLength = country.length();
String variant = locale.getVariant();
int variantLength = variant.length();
list.add(locale);
if (variantLength > 0) {
list.add(new Locale(language, country));
}
if ((countryLength > 0) && (languageLength > 0)) {
list.add(new Locale(language));
}
if ((languageLength > 0) || (countryLength > 0)) {
list.add(new Locale("", "", ""));
}
lists.put(locale, list);
return (list);
}
}
/**
* <p>Return the <code>Map</code> to be used to resolve name-value
* mappings for the specified <code>Locale</code>. Caching is utilized
* to ensure that a call to <code>getLocaleMap(base,locale)</code>
* occurs only once per <code>Locale</code>.</p>
*
* @param locale Locale for which to return a name-value mappings Map
*/
protected Map getLocaleMap(Locale locale) {
synchronized (maps) {
// Optimized lookup of any previously cached Map for this Locale
Map map = (Map) maps.get(locale);
if (map != null) {
return (map);
}
// Calculate, cache, and return the map for this Locale
map = getLocaleMap(base, locale);
maps.put(locale, map);
return (map);
}
}
/**
* <p>Return a <code>Map</code> containing the name-value mappings for
* the specified base URL and requested <code>Locale</code>, if there
* are any. If there are no defined mappings for the specified
* <code>Locale</code>, return an empty <code>Map</code> instead.</p>
*
* <p>Concrete subclasses must override this method to perform the
* appropriate lookup. A typical implementation will construct an
* absolute URL based on the specified base URL and <code>Locale</code>,
* retrieve the specified resource file (if any), and parse it into
* a <code>Map</code> structure.</p>
*
* <p>Caching of previously retrieved <code>Map</code>s (if any) should
* be performed by callers of this method. Therefore, this method should
* always attempt to retrieve the specified resource and load it
* appropriately.</p>
*
* @param base Base URL of the resource files for this {@link Resources}
* instance
* @param locale <code>Locale</code> for which name-value mappings
* are requested
*/
protected abstract Map getLocaleMap(String base, Locale locale);
/**
* <p>Return the <code>Locale</code>-specific suffix for the specified
* <code>Locale</code>. If the specified <code>Locale</code> has
* zero-length language and country components, the returned suffix
* will also have zero length. Otherwise, it will contain an
* underscore character followed by the non-zero-length language,
* country, and variant properties (separated by underscore characters).
*
* @param locale <code>Locale</code> for which a suffix string
* is requested
*/
protected String getLocaleSuffix(Locale locale) {
if (locale == null) {
locale = defaultLocale;
}
String language = locale.getLanguage();
if (language == null) {
language = "";
}
String country = locale.getCountry();
if (country == null) {
country = "";
}
if ((language.length() < 1) && (country.length() < 1)) {
return ("");
}
StringBuffer sb = new StringBuffer();
if (language.length() > 0) {
sb.append('_');
sb.append(language.toLowerCase());
}
if (country.length() > 0) {
sb.append('_');
sb.append(country.toUpperCase());
}
String variant = locale.getVariant();
if ((variant != null) && (variant.length() > 0)) {
sb.append('_');
sb.append(variant);
}
return (sb.toString());
}
}
1.1 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/PropertyResources.java
Index: PropertyResources.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/PropertyResources.java,v 1.1 2003/01/05 03:29:57 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2003/01/05 03:29:57 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.resources.impl;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.resources.Resources;
/**
* <p>Concrete implementation of {@link Resources} that wraps a family
* (one per <code>Locale</code> of properties files that share a base URL
* and have name suffixes reflecting the <code>Locale</code> for which
* the document's messages apply. Resources are looked up in a hierarchy
* of properties files in a manner identical to that performed by
* <code>java.util.ResourceBundle.getBundle().</code>.</p>
*
* <p>The base URL passed to our constructor must contain the base name
* of the properties file family.
* For example, if the configuration URL is passed as
* <code>http://localhost/foo/Bar</code>, the resources for the
* <code>en_US</code> Locale would be stored under URL
* <code>http://localhost/foo/Bar_en_US.properties</code>, and the default
* resources would be stored in
* <code>http://localhost/foo/Bar.properties</code>.</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2003/01/05 03:29:57 $
*/
public class PropertyResources extends CollectionResourcesBase {
// ----------------------------------------------------------- Constructors
/**
* <p>Create a new {@link Resources} instance with the specified
* logical name and base resource URL.</p>
*
* @param name Logical name of the new instance
* @param base Base URL of the family of properties files that contain
* the resource keys and values
*/
public PropertyResources(String name, String base) {
super(name, base);
}
// ------------------------------------------------------ Protected Methods
/**
* <p>Return a <code>Map</code> containing the name-value mappings for
* the specified base URL and requested <code>Locale</code>, if there
* are any. If there are no defined mappings for the specified
* <code>Locale</code>, return an empty <code>Map</code> instead.</p>
*
* <p>Concrete subclasses must override this method to perform the
* appropriate lookup. A typical implementation will construct an
* absolute URL based on the specified base URL and <code>Locale</code>,
* retrieve the specified resource file (if any), and parse it into
* a <code>Map</code> structure.</p>
*
* <p>Caching of previously retrieved <code>Map</code>s (if any) should
* be performed by callers of this method. Therefore, this method should
* always attempt to retrieve the specified resource and load it
* appropriately.</p>
*
* @param base Base URL of the resource files for this {@link Resources}
* instance
* @param locale <code>Locale</code> for which name-value mappings
* are requested
*/
protected Map getLocaleMap(String base, Locale locale) {
Properties props = new Properties();
String name = base + getLocaleSuffix(locale) + ".properties";
InputStream stream = null;
try {
URL url = new URL(name);
stream = url.openStream();
props.load(stream);
} catch (IOException e) {
; // Swallow IOExceptions, will cause an empty Map to be returned
} finally {
if (stream != null) {
try {
stream.close();
} catch (Exception e) {
;
}
stream = null;
}
}
return (props);
}
}
1.1 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/PropertyResourcesFactory.java
Index: PropertyResourcesFactory.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/PropertyResourcesFactory.java,v 1.1 2003/01/05 03:29:57 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2003/01/05 03:29:57 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.resources.impl;
import org.apache.commons.resources.Resources;
import org.apache.commons.resources.ResourcesException;
import org.apache.commons.resources.ResourcesFactory;
/**
* <p>Concrete implementation of {@link ResourcesFactory} that creates
* {@link Resources} instances that wrap a family (one per Locale) of
* properties files that share a base URL and have name suffices reflecting
* the Locale for which the document's messages apply. Resources are
* looked up in a hierarchy of documents in a manner identical to that
* performed by <code>java.util.ResourceBundle.getBundle()</code>.</p>
*
* <p>The configuration variable passed to the <code>createResources()</code>
* method must be the URL of the base name of the properties file family.
* For example, if the configuration URL is passed as
* <code>http://localhost/foo/Bar</code>, the resources for the
* <code>en_US</code> Locale would be stored under URL
* <code>http://localhost/foo/Bar_en_US.properties</code>, and the default
* resources would be stored in
* <code>http://localhost/foo/Bar.properties</code>.</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2003/01/05 03:29:57 $
*/
public class PropertyResourcesFactory extends ResourcesFactoryBase {
// --------------------------------------------------------- Public Methods
// ------------------------------------------------------ Protected Methods
/**
* <p>Create and return a new {@link Resources} instance with the
* specified logical name, after calling its <code>init()</code>
* method and delegating the relevant properties.</p>
*
* @param name Logical name of the {@link Resources} instance to create
* @param config Configuration string for this resource (if any)
*
* @exception ResourcesException if a {@link Resources} instance
* of the specified logical name cannot be created.
*/
protected Resources createResources(String name, String config)
throws ResourcesException {
Resources resources = new PropertyResources(name, config);
resources.setReturnNull(isReturnNull());
resources.init();
return (resources);
}
}
1.3 +9 -4 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/message/PropertyMessageResources.java
Index: PropertyMessageResources.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/message/PropertyMessageResources.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- PropertyMessageResources.java 24 Oct 2001 18:42:03 -0000 1.2
+++ PropertyMessageResources.java 5 Jan 2003 03:29:57 -0000 1.3
@@ -68,6 +68,7 @@
import java.util.HashMap;
import java.util.Locale;
import java.util.Properties;
+import java.util.TimeZone;
/**
@@ -309,5 +310,9 @@
}
+
+ public Object getObject(String key, Locale locale, TimeZone timeZone) {
+ return (null);
+ }
}
1.3 +14 -8 jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/ResourcesBaseTestCase.java
Index: ResourcesBaseTestCase.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/ResourcesBaseTestCase.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ResourcesBaseTestCase.java 28 Dec 2002 04:40:56 -0000 1.2
+++ ResourcesBaseTestCase.java 5 Jan 2003 03:29:58 -0000 1.3
@@ -63,6 +63,8 @@
package org.apache.commons.resources.impl;
+import java.util.ArrayList;
+import java.util.List;
import java.util.Locale;
import junit.framework.Test;
import junit.framework.TestCase;
@@ -169,16 +171,20 @@
public void testPristine() {
assertNotNull(resources);
assertEquals("Correct name", NAME, resources.getName());
- assertTrue("Resources initialized",
- ((TestResources) resources).isInitialized());
+ if (resources instanceof TestResources) {
+ assertTrue("Resources initialized",
+ ((TestResources) resources).isInitialized());
+ }
}
// Test that releasing should destroy instances
public void testRelease() throws Exception {
factory.release();
- assertTrue("Resources destroyed",
- !((TestResources) resources).isInitialized());
+ if (resources instanceof TestResources) {
+ assertTrue("Resources destroyed",
+ !((TestResources) resources).isInitialized());
+ }
}
1.3 +6 -94 jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/TestResources.java
Index: TestResources.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/TestResources.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TestResources.java 28 Dec 2002 04:40:56 -0000 1.2
+++ TestResources.java 5 Jan 2003 03:29:58 -0000 1.3
@@ -69,6 +69,7 @@
import java.io.StringReader;
import java.util.HashMap;
import java.util.Iterator;
+import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
@@ -146,51 +147,6 @@
/**
- * <p>Return the content for the specified <code>key</code> as a
- * byte array, localized based on the specified <code>locale</code>
- * and/or <code>timeZone</code>.</p>
- *
- * @param key Identifier for the requested content
- * @param locale Locale with which to localize retrieval,
- * or <code>null</code> for the default Locale
- * @param timeZone TimeZone with which to localize retrieval,
- * or <code>null</code> for the default TimeZone
- *
- * @exception ResourcesException if an error occurs retrieving or
- * returning the requested content
- */
- public byte[] getBytes(String key, Locale locale, TimeZone timeZone)
- throws ResourcesException {
-
- throw new UnsupportedOperationException(); // FIXME
-
- }
-
-
- /**
- * <p>Return the content for the specified <code>key</code> as an
- * InputStream, localized based on the specified <code>locale</code>
- * and/or <code>timeZone</code>.</p>
- *
- * @param key Identifier for the requested content
- * @param locale Locale with which to localize retrieval,
- * or <code>null</code> for the default Locale
- * @param timeZone TimeZone with which to localize retrieval,
- * or <code>null</code> for the default TimeZone
- *
- * @exception ResourcesException if an error occurs retrieving or
- * returning the requested content
- */
- public InputStream getInputStream(String key, Locale locale,
- TimeZone timeZone)
- throws ResourcesException {
-
- throw new UnsupportedOperationException(); // FIXME
-
- }
-
-
- /**
* <p>Return the content for the specified <code>key</code> as an
* Object, localized based on the specified <code>locale</code>
* and/or <code>timeZone</code>.</p>
@@ -207,51 +163,7 @@
public Object getObject(String key, Locale locale, TimeZone timeZone)
throws ResourcesException {
- throw new UnsupportedOperationException(); // FIXME
-
- }
-
-
- /**
- * <p>Return the content for the specified <code>key</code> as a
- * Reader, localized based on the specified <code>locale</code>
- * and/or <code>timeZone</code>.</p>
- *
- * @param key Identifier for the requested content
- * @param locale Locale with which to localize retrieval,
- * or <code>null</code> for the default Locale
- * @param timeZone TimeZone with which to localize retrieval,
- * or <code>null</code> for the default TimeZone
- *
- * @exception ResourcesException if an error occurs retrieving or
- * returning the requested content
- */
- public Reader getReader(String key, Locale locale, TimeZone timeZone)
- throws ResourcesException {
-
- throw new UnsupportedOperationException(); // FIXME
-
- }
-
-
- /**
- * <p>Return the content for the specified <code>key</code> as a
- * String, localized based on the specified <code>locale</code>
- * and/or <code>timeZone</code>.</p>
- *
- * @param key Identifier for the requested content
- * @param locale Locale with which to localize retrieval,
- * or <code>null</code> for the default Locale
- * @param timeZone TimeZone with which to localize retrieval,
- * or <code>null</code> for the default TimeZone
- *
- * @exception ResourcesException if an error occurs retrieving or
- * returning the requested content
- */
- public String getString(String key, Locale locale, TimeZone timeZone)
- throws ResourcesException {
-
- String value = (String) map.get(key);
+ Object value = map.get(key);
if (value == null) {
if (isReturnNull()) {
return (null);
1.1 jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/CollResources.java
Index: CollResources.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/CollResources.java,v 1.1 2003/01/05 03:29:58 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2003/01/05 03:29:58 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.resources.impl;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.TimeZone;
import org.apache.commons.resources.Resources;
import org.apache.commons.resources.ResourcesException;
import org.apache.commons.resources.ResourcesKeyException;
/**
* <p>Concrete implementation of {@link Resources} for unit tests.</p>
*
* <p><strong>IMPLEMENTATION NOTE</strong> - The set of hard-coded resource
* maps returned by <code>getLocaleMap()</code> below must *exactly*
* match the resources stored in the <code>TestResources*.properties</code>
* and <code>TestResources*.xml</code> resource files.</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2003/01/05 03:29:58 $
*/
public class CollResources extends CollectionResourcesBase {
// ----------------------------------------------------------- Constructors
// Construct a test instance with the specified name and base URL
public CollResources(String name, String base) {
super(name, base);
}
// ----------------------------------------------------- Instance Variables
// ------------------------------------------------------ Lifecycle Methods
// ------------------------------------------------------------- Properties
// ---------------------------------------------- Content Retrieval Methods
// Concrete implementation of getLocaleMap()
protected Map getLocaleMap(String base, Locale locale) {
String language = locale.getLanguage();
String country = locale.getCountry();
String variant = locale.getVariant();
if (variant.length() > 0) {
return (new HashMap());
}
if (language.equals("en")) {
if (country.equals("US")) {
Map map = new HashMap();
map.put("test.specific", "[en_US] SPECIFIC");
return (map);
} else if (country.equals("")) {
Map map = new HashMap();
map.put("test.specific", "[en] SPECIFIC");
map.put("test.inherit", "[en] INHERIT");
return (map);
} else {
return (new HashMap());
}
} else if (language.equals("fr")) {
if (country.equals("")) {
Map map = new HashMap();
map.put("test.specific", "[fr] SPECIFIC");
map.put("test.inherit", "[fr] INHERIT");
return (map);
} else {
return (new HashMap());
}
} else if (language.equals("")) {
if (country.equals("")) {
Map map = new HashMap();
map.put("test.base", "[Base] ONLY");
map.put("test.specific", "[Base] SPECIFIC");
map.put("test.inherit", "[Base] INHERIT");
map.put("test.message", "[Base] REPLACE {0} WITH {1}");
return (map);
} else {
return (new HashMap());
}
} else {
return (new HashMap());
}
}
}
1.1 jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/CollResourcesFactory.java
Index: CollResourcesFactory.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/CollResourcesFactory.java,v 1.1 2003/01/05 03:29:58 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2003/01/05 03:29:58 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.resources.impl;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.resources.Resources;
import org.apache.commons.resources.ResourcesException;
import org.apache.commons.resources.ResourcesFactory;
/**
* <p>Concrete implementation of {@link ResourcesFactory} for unit tests.</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2003/01/05 03:29:58 $
*/
public class CollResourcesFactory extends ResourcesFactoryBase {
// --------------------------------------------------------- Public Methods
// ------------------------------------------------------ Protected Methods
/**
* <p>Create and return a new {@link Resources} instance with the
* specified logical name, after calling its <code>init()</code>
* method.</p>
*
* @param name Logical name of the {@link Resources} instance to create
* @param config Configuration string for this resource (if any)
*
* @exception ResourcesException if a {@link Resources} instance
* of the specified logical name cannot be created.
*/
protected Resources createResources(String name, String config)
throws ResourcesException {
Resources resources = new CollResources(name, config);
resources.init();
resources.setReturnNull(isReturnNull());
return (resources);
}
}
1.1 jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/CollectionResourcesBaseTestCase.java
Index: CollectionResourcesBaseTestCase.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/CollectionResourcesBaseTestCase.java,v 1.1 2003/01/05 03:29:58 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2003/01/05 03:29:58 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.resources.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.resources.Resources;
import org.apache.commons.resources.ResourcesException;
import org.apache.commons.resources.ResourcesFactory;
import org.apache.commons.resources.ResourcesKeyException;
import org.apache.commons.resources.impl.CollectionResourcesBase;
/**
* <p>Unit tests for
* <code>org.apache.commons.resources.impl.CollectionResourcesBase</code>.
* </p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2003/01/05 03:29:58 $
*/
public class CollectionResourcesBaseTestCase extends ResourcesBaseTestCase {
// ----------------------------------------------------- Instance Variables
// Base URL of the resource files for the Resources instance to be created
protected static final String BASE = "fixme";
// ----------------------------------------------------------- Constructors
public CollectionResourcesBaseTestCase(String name) {
super(name);
}
// --------------------------------------------------- Overall Test Methods
// Set up instance variables required by this test case
public void setUp() throws Exception {
factory = new CollResourcesFactory();
resources = factory.getResources(NAME, BASE);
}
// Return the tests included in this test suite
public static Test suite() {
return (new TestSuite(CollectionResourcesBaseTestCase.class));
}
// Tear down the instance variables required by this test case
public void tearDown() {
resources = null;
factory = null;
}
// ------------------------------------------------ Individual Test Methods
// Test retrieving Strings that should be inherited from parent Locales
public void testInherit() throws Exception {
Locale locale = null;
locale = new Locale("en", "US");
assertEquals("en_US inherit value",
"[en] INHERIT",
resources.getString("test.inherit", locale, null));
locale = new Locale("fr", "FR");
assertEquals("fr_FR inherit value",
"[fr] INHERIT",
resources.getString("test.inherit", locale, null));
}
// Test the getLocaleList() method
public void testLocaleList() {
Locale test = null;
List expected = null;
CollectionResourcesBase cresources =
(CollectionResourcesBase) resources;
test = new Locale("en", "US", "POSIX");
expected = new ArrayList();
expected.add(test);
expected.add(new Locale("en", "US"));
expected.add(new Locale("en"));
expected.add(new Locale(""));
checkLocaleList(test, expected, cresources.getLocaleList(test));
test = new Locale("en", "US");
expected = new ArrayList();
expected.add(test);
expected.add(new Locale("en"));
expected.add(new Locale(""));
checkLocaleList(test, expected, cresources.getLocaleList(test));
test = new Locale("en");
expected = new ArrayList();
expected.add(test);
expected.add(new Locale(""));
checkLocaleList(test, expected, cresources.getLocaleList(test));
test = new Locale("", "US", "POSIX");
expected = new ArrayList();
expected.add(test);
expected.add(new Locale("", "US"));
expected.add(new Locale(""));
checkLocaleList(test, expected, cresources.getLocaleList(test));
test = new Locale("", "US");
expected = new ArrayList();
expected.add(test);
expected.add(new Locale(""));
checkLocaleList(test, expected, cresources.getLocaleList(test));
}
// Test retrieving missing resource keys
public void testMissing() throws Exception {
Locale locale = new Locale("en");
try {
resources.setReturnNull(true);
String value = resources.getString("test.missing", locale, null);
assertTrue("Should have returned null", value == null);
} catch (ResourcesException e) {
fail("Should have returned null");
}
try {
resources.setReturnNull(false);
String value = resources.getString("test.missing", locale, null);
fail("Should have thrown ResourcesException");
} catch (ResourcesKeyException e) {
; // Expected result
}
}
// Test retrieving Locale-specific Strings explicitly
public void testSpecific() throws Exception {
Locale locale = null;
locale = new Locale("en");
assertEquals("en specific value",
"[en] SPECIFIC",
resources.getString("test.specific", locale, null));
locale = new Locale("en", "US");
assertEquals("en_US specific value",
"[en_US] SPECIFIC",
resources.getString("test.specific", locale, null));
locale = new Locale("fr");
assertEquals("fr specific value",
"[fr] SPECIFIC",
resources.getString("test.specific", locale, null));
}
// ------------------------------------------------------ Protected Methods
// Check the results of a getLocaleList() operation
protected void checkLocaleList(Locale test, List expected, List actual) {
assertEquals("[" + test + "] list length",
expected.size(),
actual.size());
for (int i = 0; i < expected.size(); i++) {
assertEquals("[" + test + "] list contents[" + i + "]",
expected.get(i),
actual.get(i));
}
}
}
1.1 jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/PropertyResourcesTestCase.java
Index: PropertyResourcesTestCase.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/PropertyResourcesTestCase.java,v 1.1 2003/01/05 03:29:58 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2003/01/05 03:29:58 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.resources.impl;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.resources.Resources;
import org.apache.commons.resources.ResourcesException;
import org.apache.commons.resources.ResourcesFactory;
import org.apache.commons.resources.ResourcesKeyException;
import org.apache.commons.resources.impl.CollectionResourcesBase;
/**
* <p>Unit tests for
* <code>org.apache.commons.resources.impl.PropertyResources</code>.
* </p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2003/01/05 03:29:58 $
*/
public class PropertyResourcesTestCase
extends CollectionResourcesBaseTestCase {
// ----------------------------------------------------- Instance Variables
// Base URL of the resource files for the Resources instance to be created
protected static String BASE = null;
static {
URL url =
PropertyResourcesTestCase.class.getResource
("/org/apache/commons/resources/impl/TestResources.properties");
String string = url.toExternalForm();
BASE = string.substring(0, string.length() - 11);
}
// ----------------------------------------------------------- Constructors
public PropertyResourcesTestCase(String name) {
super(name);
}
// --------------------------------------------------- Overall Test Methods
// Set up instance variables required by this test case
public void setUp() throws Exception {
factory = new PropertyResourcesFactory();
resources = factory.getResources(NAME, BASE);
}
// Return the tests included in this test suite
public static Test suite() {
return (new TestSuite(PropertyResourcesTestCase.class));
}
// Tear down the instance variables required by this test case
public void tearDown() {
resources = null;
factory = null;
}
// ------------------------------------------------ Individual Test Methods
// ------------------------------------------------------ Protected Methods
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>