You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by br...@apache.org on 2009/08/05 21:18:51 UTC
svn commit: r801367 - in /incubator/pivot/trunk/core:
src/org/apache/pivot/util/ test/org/apache/pivot/util/test/
Author: brindy
Date: Wed Aug 5 19:18:50 2009
New Revision: 801367
URL: http://svn.apache.org/viewvc?rev=801367&view=rev
Log:
PIVOT-180
Allow Resources to be constructed with a parent.
Added supporting unit test.
Added:
incubator/pivot/trunk/core/test/org/apache/pivot/util/test/ParentResourcesTest.java
incubator/pivot/trunk/core/test/org/apache/pivot/util/test/ParentResourcesTest.json
incubator/pivot/trunk/core/test/org/apache/pivot/util/test/ParentResourcesTestParent.json
incubator/pivot/trunk/core/test/org/apache/pivot/util/test/ParentResourcesTest_en.json
Modified:
incubator/pivot/trunk/core/src/org/apache/pivot/util/Resources.java
Modified: incubator/pivot/trunk/core/src/org/apache/pivot/util/Resources.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/org/apache/pivot/util/Resources.java?rev=801367&r1=801366&r2=801367&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/org/apache/pivot/util/Resources.java (original)
+++ incubator/pivot/trunk/core/src/org/apache/pivot/util/Resources.java Wed Aug 5 19:18:50 2009
@@ -29,7 +29,6 @@
import org.apache.pivot.serialization.JSONSerializer;
import org.apache.pivot.serialization.SerializationException;
-
/**
* Reads a JSON resource at {@link #baseName} using
* {@link ClassLoader#getResourceAsStream(String)}. It applies localization to
@@ -38,80 +37,121 @@
* applies a country specified resource over-writing the values in the base
* using the country specified. It then does the same for country/language
* specific.
- *
+ *
* @see java.util.ResourceBundle
- *
+ *
* @author brindy
* @author gbrown
*/
public class Resources implements Dictionary<String, Object>, Iterable<String> {
- private String baseName = null;
- private Locale locale = null;
- private Charset charset = null;
+
+ private final Resources parent;
+ private final String baseName;
+ private final Locale locale;
+ private final Charset charset;
private Map<String, Object> resourceMap = null;
- public Resources(Object baseObject)
- throws IOException, SerializationException {
+ public Resources(Object baseObject) throws IOException,
+ SerializationException {
this(baseObject.getClass());
}
- public Resources(Class<?> baseType)
- throws IOException, SerializationException {
+ public Resources(Resources parent, Object baseObject) throws IOException,
+ SerializationException {
+ this(parent, baseObject.getClass());
+ }
+
+ public Resources(Class<?> baseType) throws IOException,
+ SerializationException {
this(baseType.getName());
}
- public Resources(String baseName)
- throws IOException, SerializationException {
- this(baseName, Locale.getDefault(), Charset.defaultCharset());
+ public Resources(Resources parent, Class<?> baseType) throws IOException,
+ SerializationException {
+ this(parent, baseType.getName());
+ }
+
+ public Resources(String baseName) throws IOException,
+ SerializationException {
+ this(null, baseName, Locale.getDefault(), Charset.defaultCharset());
}
- public Resources(String baseName, Locale locale)
- throws IOException, SerializationException {
- this(baseName, locale, Charset.defaultCharset());
+ public Resources(Resources parent, String baseName) throws IOException,
+ SerializationException {
+ this(parent, baseName, Locale.getDefault(), Charset.defaultCharset());
}
- public Resources(String baseName, String charsetName)
- throws IOException, SerializationException {
+ public Resources(String baseName, Locale locale) throws IOException,
+ SerializationException {
+ this(null, baseName, locale, Charset.defaultCharset());
+ }
+
+ public Resources(Resources parent, String baseName, Locale locale)
+ throws IOException, SerializationException {
+ this(parent, baseName, locale, Charset.defaultCharset());
+ }
+
+ public Resources(String baseName, String charsetName) throws IOException,
+ SerializationException {
this(baseName, Locale.getDefault(), charsetName);
}
- public Resources(String baseName, Charset charset)
- throws IOException, SerializationException {
- this(baseName, Locale.getDefault(), charset);
+ public Resources(Resources parent, String baseName, String charsetName)
+ throws IOException, SerializationException {
+ this(parent, baseName, Locale.getDefault(), charsetName);
+ }
+
+ public Resources(String baseName, Charset charset) throws IOException,
+ SerializationException {
+ this(null, baseName, Locale.getDefault(), charset);
+ }
+
+ public Resources(Resources parent, String baseName, Charset charset)
+ throws IOException, SerializationException {
+ this(parent, baseName, Locale.getDefault(), charset);
}
public Resources(String baseName, Locale locale, String charsetName)
- throws IOException, SerializationException {
- this(baseName, locale, Charset.forName(charsetName));
+ throws IOException, SerializationException {
+ this(null, baseName, locale, Charset.forName(charsetName));
+ }
+
+ public Resources(Resources parent, String baseName, Locale locale,
+ String charsetName) throws IOException, SerializationException {
+ this(parent, baseName, locale, Charset.forName(charsetName));
}
/**
* Creates a new resource bundle.
- *
+ *
+ * @param parent
+ * The parent resource defer to if a resource cannot be found in this
+ * instance or null.
+ *
* @param baseName
* The base name of this resource as a fully qualified class name.
- *
+ *
* @param locale
* The locale to use when reading this resource.
- *
+ *
* @param charset
* The character encoding to use when reading this resource.
- *
+ *
* @throws IOException
* If there is a problem when reading the resource.
- *
+ *
* @throws SerializationException
* If there is a problem deserializing the resource from its JSON format.
- *
+ *
* @throws IllegalArgumentException
* If baseName or locale is null.
- *
+ *
* @throws MissingResourceException
* If no resource for the specified base name can be found.
*/
- public Resources(String baseName, Locale locale, Charset charset) throws IOException,
- SerializationException {
+ public Resources(Resources parent, String baseName, Locale locale,
+ Charset charset) throws IOException, SerializationException {
if (locale == null) {
throw new IllegalArgumentException("locale is null");
@@ -121,6 +161,7 @@
throw new IllegalArgumentException("charset is null.");
}
+ this.parent = parent;
this.baseName = baseName;
this.locale = locale;
this.charset = charset;
@@ -128,19 +169,21 @@
String resourceName = baseName.replace('.', '/');
resourceMap = readJSONResource(resourceName + ".json");
if (resourceMap == null) {
- throw new MissingResourceException("Can't find resource for base name "
- + baseName + ", locale " + locale, baseName, "");
+ throw new MissingResourceException(
+ "Can't find resource for base name " + baseName
+ + ", locale " + locale, baseName, "");
}
// try to find resource for the language (e.g. resourceName_en)
Map<String, Object> overrideMap = readJSONResource(resourceName + "_"
- + locale.getLanguage() + ".json");
+ + locale.getLanguage() + ".json");
if (overrideMap != null) {
applyOverrides(resourceMap, overrideMap);
}
// try to find resource for the entire locale (e.g. resourceName_en_GB)
- overrideMap = readJSONResource(resourceName + "_" + locale.toString() + ".json");
+ overrideMap = readJSONResource(resourceName + "_" + locale.toString()
+ + ".json");
if (null != overrideMap) {
applyOverrides(resourceMap, overrideMap);
}
@@ -155,94 +198,156 @@
}
public Object get(String key) {
- return JSONSerializer.get(resourceMap, key);
+ Object o = JSONSerializer.get(resourceMap, key);
+ if (o == null && parent != null) {
+ return parent.get(key);
+ }
+ return o;
}
public String getString(String key) {
- return JSONSerializer.getString(resourceMap, key);
+ String s = JSONSerializer.getString(resourceMap, key);
+ if (s == null && parent != null) {
+ return parent.getString(key);
+ }
+ return s;
}
public Number getNumber(String key) {
- return JSONSerializer.getNumber(resourceMap, key);
+ Number n = JSONSerializer.getNumber(resourceMap, key);
+ if (n == null && parent != null) {
+ return parent.getNumber(key);
+ }
+ return n;
}
public Short getShort(String key) {
- return JSONSerializer.getShort(resourceMap, key);
+ Short s = JSONSerializer.getShort(resourceMap, key);
+ if (s == null && parent != null) {
+ return parent.getShort(key);
+ }
+ return s;
}
public Integer getInteger(String key) {
- return JSONSerializer.getInteger(resourceMap, key);
+ Integer i = JSONSerializer.getInteger(resourceMap, key);
+ if (i == null && parent != null) {
+ return parent.getInteger(key);
+ }
+ return i;
}
public Long getLong(String key) {
- return JSONSerializer.getLong(resourceMap, key);
+ Long l = JSONSerializer.getLong(resourceMap, key);
+ if (l == null && parent != null) {
+ return parent.getLong(key);
+ }
+ return l;
}
public Float getFloat(String key) {
- return JSONSerializer.getFloat(resourceMap, key);
+ Float f = JSONSerializer.getFloat(resourceMap, key);
+ if (f == null && parent != null) {
+ return parent.getFloat(key);
+ }
+ return f;
}
public Double getDouble(String key) {
- return JSONSerializer.getDouble(resourceMap, key);
+ Double d = JSONSerializer.getDouble(resourceMap, key);
+ if (d == null && parent != null) {
+ return parent.getDouble(key);
+ }
+ return d;
}
public Boolean getBoolean(String key) {
- return JSONSerializer.getBoolean(resourceMap, key);
+ Boolean b = JSONSerializer.getBoolean(resourceMap, key);
+ if (b == null && parent != null) {
+ return parent.getBoolean(key);
+ }
+ return b;
}
public List<?> getList(String key) {
- return JSONSerializer.getList(resourceMap, key);
+ List<?> list = JSONSerializer.getList(resourceMap, key);
+ if (list == null && parent != null) {
+ return parent.getList(key);
+ }
+ return list;
}
public Map<String, ?> getMap(String key) {
- return JSONSerializer.getMap(resourceMap, key);
+ Map<String, ?> map = JSONSerializer.getMap(resourceMap, key);
+ if (map == null && parent != null) {
+ return parent.getMap(key);
+ }
+ return map;
+ }
+
+ public Resources getParent() {
+ return parent;
}
public Object put(String key, Object value) {
- throw new UnsupportedOperationException("Resources instances are immutable");
+ throw new UnsupportedOperationException(
+ "Resources instances are immutable");
}
public Object remove(String key) {
- throw new UnsupportedOperationException("Resources instances are immutable");
+ throw new UnsupportedOperationException(
+ "Resources instances are immutable");
}
public boolean containsKey(String key) {
- return resourceMap.containsKey(key);
+ boolean containsKey = resourceMap.containsKey(key);
+ if (!containsKey && parent != null) {
+ return parent.containsKey(key);
+ }
+ return containsKey;
}
public boolean isEmpty() {
- return resourceMap.isEmpty();
+ boolean empty = resourceMap.isEmpty();
+ if (empty && parent != null) {
+ return parent.isEmpty();
+ }
+ return empty;
}
- @SuppressWarnings({"unchecked"})
- private void applyOverrides(Map<String, Object> sourceMap,
- Map<String, Object> overridesMap) {
+ public Iterator<String> iterator() {
+ return new ImmutableIterator<String>(resourceMap.iterator());
+ }
+ @SuppressWarnings( { "unchecked" })
+ private void applyOverrides(Map<String, Object> sourceMap,
+ Map<String, Object> overridesMap) {
+
for (String key : overridesMap) {
if (sourceMap.containsKey(key)) {
Object source = sourceMap.get(key);
Object override = overridesMap.get(key);
-
+
if (source instanceof Map<?, ?>
- && override instanceof Map<?, ?>) {
+ && override instanceof Map<?, ?>) {
applyOverrides((Map<String, Object>) source,
- (Map<String, Object>) override);
+ (Map<String, Object>) override);
} else {
sourceMap.put(key, overridesMap.get(key));
}
}
}
-
+
}
@SuppressWarnings("unchecked")
- private Map<String, Object> readJSONResource(String name) throws IOException,
- SerializationException {
+ private Map<String, Object> readJSONResource(String name)
+ throws IOException, SerializationException {
InputStream in = getClass().getClassLoader().getResourceAsStream(name);
if (in == null) {
return null;
}
-
+
JSONSerializer serializer = new JSONSerializer(charset);
Map<String, Object> resourceMap = null;
try {
@@ -250,11 +355,7 @@
} finally {
in.close();
}
-
+
return resourceMap;
}
-
- public Iterator<String> iterator() {
- return new ImmutableIterator<String>(resourceMap.iterator());
- }
}
Added: incubator/pivot/trunk/core/test/org/apache/pivot/util/test/ParentResourcesTest.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/test/org/apache/pivot/util/test/ParentResourcesTest.java?rev=801367&view=auto
==============================================================================
--- incubator/pivot/trunk/core/test/org/apache/pivot/util/test/ParentResourcesTest.java (added)
+++ incubator/pivot/trunk/core/test/org/apache/pivot/util/test/ParentResourcesTest.java Wed Aug 5 19:18:50 2009
@@ -0,0 +1,176 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.pivot.util.test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.nio.charset.Charset;
+import java.util.Locale;
+
+import org.apache.pivot.collections.List;
+import org.apache.pivot.util.Resources;
+import org.junit.Before;
+import org.junit.Test;
+
+public class ParentResourcesTest {
+
+ private Resources parent;
+ private Resources main;
+
+ @Before
+ public void before() throws Exception {
+ parent = new Resources(getClass().getName() + "Parent");
+ assertNotNull("From parent", parent.getString("parentKeyString"));
+ main = new Resources(parent, getClass().getName(), Locale.ENGLISH);
+ }
+
+ @Test
+ public void testString() throws Exception {
+ assertNotNull("In main only", main.getString("mainKeyString"));
+ assertEquals("In parent", "From Parent", main.getParent().getString(
+ "someMagicString"));
+ assertEquals("In both", "From Main", main.getString("someMagicString"));
+ }
+
+ @Test
+ public void testNumber() throws Exception {
+ assertNotNull("In main only", main.getNumber("mainKeyNumber"));
+ assertEquals("In parent", new Integer(100), main.getParent().getNumber(
+ "someMagicNumber"));
+ assertEquals("In both", new Integer(200), main
+ .getNumber("someMagicNumber"));
+ }
+
+ @Test
+ public void testInteger() throws Exception {
+ assertNotNull("In main only", main.getInteger("mainKeyNumber"));
+ assertEquals("In parent", new Integer(100), main.getParent()
+ .getInteger("someMagicNumber"));
+ assertEquals("In both", new Integer(200), main
+ .getInteger("someMagicNumber"));
+ }
+
+ @Test
+ public void testLong() throws Exception {
+ assertNotNull("In main only", main.getLong("mainKeyNumber"));
+ assertEquals("In parent", new Long(100), main.getParent().getLong(
+ "someMagicNumber"));
+ assertEquals("In both", new Long(200), main.getLong("someMagicNumber"));
+ }
+
+ @Test
+ public void testShort() throws Exception {
+ assertNotNull("In main only", main.getShort("mainKeyNumber"));
+ assertEquals("In parent", new Short((short) 100), main.getParent()
+ .getShort("someMagicNumber"));
+ assertEquals("In both", new Short((short) 200), main
+ .getShort("someMagicNumber"));
+ }
+
+ @Test
+ public void testFloat() throws Exception {
+ assertNotNull("In main only", main.getFloat("mainKeyNumber"));
+ assertEquals("In parent", new Float(100), main.getParent().getFloat(
+ "someMagicNumber"));
+ assertEquals("In both", new Float(200), main
+ .getFloat("someMagicNumber"));
+ }
+
+ @Test
+ public void testDouble() throws Exception {
+ assertNotNull("In main only", main.getDouble("mainKeyNumber"));
+ assertEquals("In parent", new Double(100), main.getParent().getDouble(
+ "someMagicNumber"));
+ assertEquals("In both", new Double(200), main
+ .getDouble("someMagicNumber"));
+ }
+
+ @Test
+ public void testBoolean() throws Exception {
+ assertNotNull("In main only", main.getBoolean("mainKeyBoolean"));
+ assertEquals("In parent", false, main.getParent().getBoolean(
+ "someMagicBoolean"));
+ assertEquals("In both", true, main.getBoolean("someMagicBoolean"));
+ }
+
+ @Test
+ public void testMyMap() {
+ assertFalse("not contains noMap", main.containsKey("noMap"));
+ assertTrue("contains myMap", main.containsKey("myMap"));
+ assertFalse("empty map", main.isEmpty());
+ assertNotNull("myMap as object not null", main.get("myMap"));
+ assertNull("noMap as map is null", main.getMap("noMap"));
+ assertNotNull("myMap as map not null", main.getMap("myMap"));
+ }
+
+ @Test
+ public void testList() {
+
+ List<?> list = main.getList("aList");
+ assertNotNull("aList not null", list);
+ assertEquals("6 items", 6, list.getLength());
+
+ }
+
+ @Test
+ public void testLanguage() {
+
+ assertEquals("Language", "This is specifically English", main
+ .getString("languageKey"));
+
+ assertEquals("Language in parent",
+ "This is not a specific language and is in the parent.", parent
+ .getString("languageKey"));
+
+ }
+
+ @Test
+ public void testConstructors() throws Exception {
+
+ main = new Resources(parent, this);
+ testString();
+
+ main = new Resources(parent, getClass());
+ testString();
+
+ main = new Resources(parent, getClass().getName(), Charset
+ .defaultCharset());
+ testString();
+
+ main = new Resources(parent, getClass().getName(), Charset
+ .defaultCharset().name());
+ testString();
+
+ main = new Resources(parent, getClass().getName(), Locale.ENGLISH);
+ testString();
+
+ main = new Resources(parent, getClass().getName(), Locale.ENGLISH,
+ Charset.defaultCharset());
+ testString();
+
+ main = new Resources(parent, getClass().getName(), Locale.ENGLISH,
+ Charset.defaultCharset().name());
+ testString();
+
+ }
+
+}
Added: incubator/pivot/trunk/core/test/org/apache/pivot/util/test/ParentResourcesTest.json
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/test/org/apache/pivot/util/test/ParentResourcesTest.json?rev=801367&view=auto
==============================================================================
--- incubator/pivot/trunk/core/test/org/apache/pivot/util/test/ParentResourcesTest.json (added)
+++ incubator/pivot/trunk/core/test/org/apache/pivot/util/test/ParentResourcesTest.json Wed Aug 5 19:18:50 2009
@@ -0,0 +1,9 @@
+{
+ mainKeyString: "Hello From Main",
+ someMagicString: "From Main",
+ mainKeyNumber: 200,
+ someMagicNumber: 200,
+ mainKeyBoolean: false,
+ someMagicBoolean: true,
+ languageKey: "This is not specifically any language"
+}
\ No newline at end of file
Added: incubator/pivot/trunk/core/test/org/apache/pivot/util/test/ParentResourcesTestParent.json
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/test/org/apache/pivot/util/test/ParentResourcesTestParent.json?rev=801367&view=auto
==============================================================================
--- incubator/pivot/trunk/core/test/org/apache/pivot/util/test/ParentResourcesTestParent.json (added)
+++ incubator/pivot/trunk/core/test/org/apache/pivot/util/test/ParentResourcesTestParent.json Wed Aug 5 19:18:50 2009
@@ -0,0 +1,20 @@
+{
+ parentKeyString: "Hello from parent",
+ someMagicString: "From Parent",
+ parentKeyNumber: 100,
+ someMagicNumber: 100,
+ mainKeyBoolean: true,
+ someMagicBoolean: false,
+
+ myMap : {
+
+ x: 75,
+ y: 90
+
+ },
+
+ languageKey: "This is not a specific language and is in the parent.",
+
+ aList: [1, 2, 3, 4, 5, 6]
+
+}
\ No newline at end of file
Added: incubator/pivot/trunk/core/test/org/apache/pivot/util/test/ParentResourcesTest_en.json
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/test/org/apache/pivot/util/test/ParentResourcesTest_en.json?rev=801367&view=auto
==============================================================================
--- incubator/pivot/trunk/core/test/org/apache/pivot/util/test/ParentResourcesTest_en.json (added)
+++ incubator/pivot/trunk/core/test/org/apache/pivot/util/test/ParentResourcesTest_en.json Wed Aug 5 19:18:50 2009
@@ -0,0 +1,5 @@
+{
+
+ languageKey: "This is specifically English"
+
+}
\ No newline at end of file