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/06 21:41:36 UTC
cvs commit: jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl TestResources.xml TestResources_en.xml TestResources_en_US.xml TestResources_fr.xml XMLResourcesTestCase.java
craigmcc 2003/01/06 12:41:35
Modified: resources/src/java/org/apache/commons/resources/impl
PropertyResources.java
Added: resources/src/java/org/apache/commons/resources/impl
XMLResources.java XMLResourcesFactory.java
resources/src/test/org/apache/commons/resources/impl
TestResources.xml TestResources_en.xml
TestResources_en_US.xml TestResources_fr.xml
XMLResourcesTestCase.java
Log:
Add back in XMLResources support, and the corresponding test case.
Add debug logging to PropertyResources and XMLResources.
Revision Changes Path
1.2 +53 -5 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/PropertyResources.java
Index: PropertyResources.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/PropertyResources.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- PropertyResources.java 5 Jan 2003 03:29:57 -0000 1.1
+++ PropertyResources.java 6 Jan 2003 20:41:35 -0000 1.2
@@ -63,12 +63,15 @@
package org.apache.commons.resources.impl;
+import java.io.FileNotFoundException;
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.logging.Log;
+import org.apache.commons.logging.LogFactory;
import org.apache.commons.resources.Resources;
@@ -114,6 +117,16 @@
}
+ // ----------------------------------------------------- Instance Variables
+
+
+ /**
+ * <p>The <code>Log</code> instance for this class.</p>
+ */
+ protected static final Log log =
+ LogFactory.getLog(PropertyResources.class);
+
+
// ------------------------------------------------------ Protected Methods
@@ -141,16 +154,48 @@
*/
protected Map getLocaleMap(String base, Locale locale) {
+ if (log.isDebugEnabled()) {
+ log.debug("Loading locale '" + locale + "' resources from base '" +
+ base + "'");
+ }
+
Properties props = new Properties();
String name = base + getLocaleSuffix(locale) + ".properties";
InputStream stream = null;
+
try {
+
+ // Open an input stream to the URL for this locale (if any)
+ if (log.isTraceEnabled()) {
+ log.trace("Absolute URL is '" + name + "'");
+ }
URL url = new URL(name);
stream = url.openStream();
+
+ // Parse the input stream and populate the name-value mappings map
+ if (log.isTraceEnabled()) {
+ log.trace("Parsing input resource");
+ }
props.load(stream);
+
+ } catch (FileNotFoundException e) {
+
+ // Log and swallow this exception
+ if (log.isDebugEnabled()) {
+ log.debug("No resources for locale '" + locale +
+ "' from base '" + base + "'");
+ }
+ props.clear();
+
} catch (IOException e) {
- ; // Swallow IOExceptions, will cause an empty Map to be returned
+
+ log.warn("IOException loading locale '" + locale +
+ "' from base '" + base + "'", e);
+ props.clear();
+
} finally {
+
+ // Close the input stream that was opened earlier
if (stream != null) {
try {
stream.close();
@@ -159,7 +204,10 @@
}
stream = null;
}
+
}
+
+ // Return the populated (or empty) properties
return (props);
}
1.1 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/XMLResources.java
Index: XMLResources.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/XMLResources.java,v 1.1 2003/01/06 20:41:35 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2003/01/06 20:41:35 $
*
* ====================================================================
*
* 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.FileNotFoundException;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.digester.Digester;
import org.apache.commons.digester.Rule;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.resources.Resources;
import org.xml.sax.SAXException;
/**
* <p>Concrete implementation of {@link Resources} that wraps a family
* (one per <code>Locale</code> of XML documents 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
* XML documents 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 XML document 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.xml</code>, and the default
* resources would be stored in
* <code>http://localhost/foo/Bar.xml</code>.</p>
*
* <p>The required structure of the XML documents is very simple:</p>
* <ul>
* <li>The top level element must be <code><resources></code>.</li>
* <li>Each name-value pair is represented by a nested
* <code><resource></code> element.</li>
* <li>For each <code><resource></code> element, the <code>id</code>
* attribute contains the resource key, and the body contains a
* string representation of the value.</li>
* </ul>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2003/01/06 20:41:35 $
*/
public class XMLResources 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 XMLResources(String name, String base) {
super(name, base);
}
// ----------------------------------------------------- Instance Variables
/**
* <p>The <code>Log</code> instance for this class.</p>
*/
protected static final Log log = LogFactory.getLog(XMLResources.class);
// ------------------------------------------------------ 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) {
if (log.isDebugEnabled()) {
log.debug("Loading locale '" + locale + "' resources from base '" +
base + "'");
}
Map map = new HashMap();
String name = base + getLocaleSuffix(locale) + ".xml";
InputStream stream = null;
try {
// Open an input stream to the URL for this locale (if any)
if (log.isTraceEnabled()) {
log.trace("Absolute URL is '" + name + "'");
}
URL url = new URL(name);
stream = url.openStream();
// Create and configure a new Digester instance
if (log.isTraceEnabled()) {
log.trace("Creating Digester instance");
}
Digester digester = new Digester();
digester.setNamespaceAware(false);
digester.setValidating(false);
digester.push(map);
digester.addCallMethod("resources/resource", "put", 2,
new String[] { "java.lang.Object",
"java.lang.Object" });
digester.addCallParam("resources/resource", 0, "id");
digester.addCallParam("resources/resource", 1);
// Parse the input stream and populate the name-value mappings map
if (log.isTraceEnabled()) {
log.trace("Parsing input resource");
}
digester.parse(stream);
} catch (FileNotFoundException e) {
// Log and swallow this exception
if (log.isDebugEnabled()) {
log.debug("No resources for locale '" + locale +
"' from base '" + base + "'");
}
map.clear();
} catch (IOException e) {
// Log and swallow this exception
log.warn("IOException loading locale '" + locale +
"' from base '" + base + "'", e);
map.clear();
} catch (SAXException e) {
// Log and swallow this exception
log.warn("SAXException loading locale '" + locale +
"' from base '" + base + "'", e);
map.clear();
} finally {
// Close the input stream that was opened earlier
if (stream != null) {
try {
stream.close();
} catch (Exception e) {
;
}
stream = null;
}
}
// Return the populated (or empty) map
return (map);
}
}
1.1 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/XMLResourcesFactory.java
Index: XMLResourcesFactory.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/XMLResourcesFactory.java,v 1.1 2003/01/06 20:41:35 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2003/01/06 20:41:35 $
*
* ====================================================================
*
* 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
* XML documents 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 XML document 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.xml</code>, and the default
* resources would be stored in
* <code>http://localhost/foo/Bar.xml</code>.</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2003/01/06 20:41:35 $
*/
public class XMLResourcesFactory 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 XMLResources(name, config);
resources.setReturnNull(isReturnNull());
resources.init();
return (resources);
}
}
1.1 jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/TestResources.xml
Index: TestResources.xml
===================================================================
<?xml version="1.0"?>
<!-- The resources defined here must be IDENTICAL to those defined -->
<!-- in the corresponding properties file, or unit tests will fail -->
<resources>
<resource id="test.base">[Base] ONLY</resource>
<resource id="test.specific">[Base] SPECIFIC</resource>
<resource id="test.inherit">[Base] INHERIT</resource>
<resource id="test.message">[Base] REPLACE {0} WITH {1}</resource>
</resources>
1.1 jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/TestResources_en.xml
Index: TestResources_en.xml
===================================================================
<?xml version="1.0"?>
<!-- The resources defined here must be IDENTICAL to those defined -->
<!-- in the corresponding properties file, or unit tests will fail -->
<resources>
<resource id="test.specific">[en] SPECIFIC</resource>
<resource id="test.inherit">[en] INHERIT</resource>
</resources>
1.1 jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/TestResources_en_US.xml
Index: TestResources_en_US.xml
===================================================================
<?xml version="1.0"?>
<!-- The resources defined here must be IDENTICAL to those defined -->
<!-- in the corresponding properties file, or unit tests will fail -->
<resources>
<resource id="test.specific">[en_US] SPECIFIC</resource>
</resources>
1.1 jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/TestResources_fr.xml
Index: TestResources_fr.xml
===================================================================
<?xml version="1.0"?>
<!-- The resources defined here must be IDENTICAL to those defined -->
<!-- in the corresponding properties file, or unit tests will fail -->
<resources>
<resource id="test.specific">[fr] SPECIFIC</resource>
<resource id="test.inherit">[fr] INHERIT</resource>
</resources>
1.1 jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/XMLResourcesTestCase.java
Index: XMLResourcesTestCase.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/XMLResourcesTestCase.java,v 1.1 2003/01/06 20:41:35 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2003/01/06 20:41:35 $
*
* ====================================================================
*
* 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.XMLResources</code>.
* </p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2003/01/06 20:41:35 $
*/
public class XMLResourcesTestCase
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 =
XMLResourcesTestCase.class.getResource
("/org/apache/commons/resources/impl/TestResources.properties");
String string = url.toExternalForm();
BASE = string.substring(0, string.length() - 11);
}
// ----------------------------------------------------------- Constructors
public XMLResourcesTestCase(String name) {
super(name);
}
// --------------------------------------------------- Overall Test Methods
// Set up instance variables required by this test case
public void setUp() throws Exception {
factory = new XMLResourcesFactory();
resources = factory.getResources(NAME, BASE);
}
// Return the tests included in this test suite
public static Test suite() {
return (new TestSuite(XMLResourcesTestCase.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>