You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ni...@apache.org on 2005/11/27 10:49:59 UTC
svn commit: r349195 - in
/jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl:
ResourceBundleResources.java ResourcesBase.java
Author: niallp
Date: Sun Nov 27 01:49:47 2005
New Revision: 349195
URL: http://svn.apache.org/viewcvs?rev=349195&view=rev
Log:
Fix for Bug 37642 - Improve ResourcesBase method inter-operability
Modified:
jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/ResourceBundleResources.java
jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/ResourcesBase.java
Modified: jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/ResourceBundleResources.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/ResourceBundleResources.java?rev=349195&r1=349194&r2=349195&view=diff
==============================================================================
--- jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/ResourceBundleResources.java (original)
+++ jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/ResourceBundleResources.java Sun Nov 27 01:49:47 2005
@@ -194,58 +194,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
- * @return content for a specified key.
- *
- * @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 {
-
- 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
- * @return content for a specified key.
- *
- * @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) {
-
- 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>
@@ -265,85 +213,25 @@
*/
public Object getObject(String key, Locale locale, TimeZone timeZone) {
- try {
- ResourceBundle bundle = getBundle(locale, timeZone);
- return (bundle.getObject(key));
-
- } catch (MissingResourceException e) {
- if (isReturnNull()) {
- return (null);
- } else {
- throw new ResourcesKeyException(key);
- }
- }
-
- }
-
-
- /**
- * <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
- * @return content for a specified key.
- *
- * @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) {
-
- 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
- * @return content for a specified key.
- *
- * @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) {
-
if (getLog().isTraceEnabled()) {
- getLog().trace("Retrieving message for key '" + key + "' for locale '"
+ getLog().trace("Retrieving message for key '" + key + "' and locale '"
+ locale + "'");
}
-
+
try {
ResourceBundle bundle = getBundle(locale, timeZone);
- String message = bundle.getString(key);
+ Object object = bundle.getObject(key);
if (getLog().isTraceEnabled()) {
- getLog().trace("Retrieved message is '" + message + "'");
+ getLog().trace("Retrieved object for key '" + key +
+ "' and locale '" + locale +
+ "' is '" + object + "'");
}
- return (message);
-
- } catch (ClassCastException e) {
- throw new ResourcesException(e);
+ return object;
} catch (MissingResourceException e) {
if (getLog().isTraceEnabled()) {
- getLog().trace("No message found");
+ getLog().trace("No message found for key '" + key +
+ "' and locale '" + locale + "'");
}
if (isReturnNull()) {
return (null);
@@ -353,7 +241,6 @@
}
}
-
// ------------------------------------------------------ Protected Methods
Modified: jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/ResourcesBase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/ResourcesBase.java?rev=349195&r1=349194&r2=349195&view=diff
==============================================================================
--- jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/ResourcesBase.java (original)
+++ jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/ResourcesBase.java Sun Nov 27 01:49:47 2005
@@ -25,8 +25,10 @@
import java.io.ByteArrayInputStream;
import java.io.InputStream;
+import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
+import java.io.IOException;
import java.util.Iterator;
import java.util.Locale;
import java.util.TimeZone;
@@ -101,6 +103,10 @@
*/
private boolean returnNull = true;
+ /**
+ * Buffer size for creating char and byte arrays.
+ */
+ private int bufferSize = 500;
// ------------------------------------------------------ Lifecycle Methods
@@ -190,6 +196,31 @@
}
+ /**
+ * <p>Return the size of the buffer to use when converting
+ * InputStream or Reader objects.</p>
+ *
+ * @return The buffer size.
+ */
+ public int getBufferSize() {
+
+ return bufferSize;
+
+ }
+
+ /**
+ * <p>Set the size of the buffer to use when converting
+ * InputStream or Reader objects.</p>
+ *
+ * @param bufferSize The buffer size.
+ */
+ public void setBufferSize(int bufferSize) {
+
+ this.bufferSize = bufferSize;
+
+ }
+
+
// ---------------------------------------------- Content Retrieval Methods
@@ -216,11 +247,27 @@
*/
public byte[] getBytes(String key, Locale locale, TimeZone timeZone) {
- String value = getString(key, locale, timeZone);
+ Object value = getObject(key, locale, timeZone);
if (value == null) {
return (null);
+ } else if (value instanceof String) {
+ return ((String)value).getBytes();
+ } else if (value instanceof Reader) {
+ char[] chars = getChars((Reader)value);
+ if (chars == null) {
+ return (byte[])checkThrow(key);
+ }
+ return new String(chars).getBytes();
+ } else if (value instanceof InputStream) {
+ byte[] bytes = getBytes((InputStream)value);
+ if (bytes == null) {
+ return (byte[])checkThrow(key);
+ }
+ return bytes;
+ } else if (value instanceof byte[]) {
+ return (byte[])value;
} else {
- return (value.getBytes());
+ return value.toString().getBytes();
}
}
@@ -249,12 +296,27 @@
*/
public InputStream getInputStream(String key, Locale locale,
TimeZone timeZone) {
-
- byte bytes[] = getBytes(key, locale, timeZone);
- if (bytes == null) {
- bytes = new byte[0];
+ Object value = getObject(key, locale, timeZone);
+ if (value == null) {
+ return (null);
+ } else if (value instanceof String) {
+ byte[] bytes = ((String)value).getBytes();
+ return new ByteArrayInputStream(bytes);
+ } else if (value instanceof Reader) {
+ char[] chars = getChars((Reader)value);
+ if (chars == null) {
+ return (InputStream)checkThrow(key);
+ }
+ byte[] bytes = (new String(chars)).getBytes();
+ return new ByteArrayInputStream(bytes);
+ } else if (value instanceof InputStream) {
+ return (InputStream)value;
+ } else if (value instanceof byte[]) {
+ return new ByteArrayInputStream((byte[])value);
+ } else {
+ byte[] bytes = value.toString().getBytes();
+ return new ByteArrayInputStream(bytes);
}
- return (new ByteArrayInputStream(bytes));
}
@@ -306,11 +368,20 @@
*/
public Reader getReader(String key, Locale locale, TimeZone timeZone) {
- String value = getString(key, locale, timeZone);
+ Object value = getObject(key, locale, timeZone);
if (value == null) {
return (null);
+ } else if (value instanceof String) {
+ return new StringReader((String)value);
+ } else if (value instanceof Reader) {
+ return (Reader)value;
+ } else if (value instanceof InputStream) {
+ return new InputStreamReader((InputStream)value);
+ } else if (value instanceof byte[]) {
+ InputStream bais = new ByteArrayInputStream((byte[])value);
+ return new InputStreamReader(bais);
} else {
- return (new StringReader(value));
+ return new StringReader(value.toString());
}
}
@@ -344,10 +415,107 @@
return (null);
} else if (value instanceof String) {
return ((String) value);
+ } else if (value instanceof Reader) {
+ char[] chars = getChars((Reader)value);
+ if (chars == null) {
+ return (String)checkThrow(key);
+ }
+ return new String(chars);
+ } else if (value instanceof InputStream) {
+ byte[] bytes = getBytes((InputStream)value);
+ if (bytes == null) {
+ return (String)checkThrow(key);
+ }
+ return new String(bytes);
+ } else if (value instanceof byte[]) {
+ return new String((byte[])value);
} else {
return (value.toString());
}
+ }
+
+
+ /**
+ * Convert a Reader to a char array.
+ */
+ private char[] getChars(Reader reader) {
+
+ char[] array = null;
+ int arrayLth = 0;
+ try {
+ while (true) {
+ char[] buffer = new char[bufferSize];
+ int bufferLth = reader.read(buffer);
+ if (bufferLth < 0) {
+ break;
+ }
+ if (array == null && bufferLth == buffer.length) {
+ array = buffer;
+ } else {
+ char[] newArray = new char[arrayLth + bufferLth];
+ if (array != null) {
+ System.arraycopy(array, 0, newArray, 0, arrayLth);
+ }
+ System.arraycopy(buffer, 0, newArray, arrayLth, bufferLth);
+ array = newArray;
+ }
+ arrayLth = array.length;
+ }
+ } catch(IOException e) {
+ throw new ResourcesException("Error reading Reader", e);
+ }
+
+ return array;
+
+ }
+
+ /**
+ * Convert an InputStream to a byte array.
+ */
+ private byte[] getBytes(InputStream inputStream) {
+
+ byte[] array = null;
+ int arrayLth = 0;
+ try {
+ while (true) {
+ byte[] buffer = new byte[bufferSize];
+ int bufferLth = inputStream.read(buffer);
+ if (bufferLth < 0) {
+ break;
+ }
+ if (array == null && bufferLth == buffer.length) {
+ array = buffer;
+ } else {
+ byte[] newArray = new byte[arrayLth + bufferLth];
+ if (array != null) {
+ System.arraycopy(array, 0, newArray, 0, arrayLth);
+ }
+ System.arraycopy(buffer, 0, newArray, arrayLth, bufferLth);
+ array = newArray;
+ }
+ arrayLth = array.length;
+ }
+ } catch(IOException e) {
+ throw new ResourcesException("Error reading InputStream", e);
+ }
+ return array;
+
+ }
+
+ /**
+ * Check whether this Resources instance is
+ * configured to return null or throw a ResourcesKeyException.
+ * @param key Identifier for the requested content
+ * @return 'null' if returnNull is 'true' otherwise throws
+ * a ResourcesKeyException.
+ */
+ private Object checkThrow(String key) {
+ if (isReturnNull()) {
+ return (null);
+ } else {
+ throw new ResourcesKeyException(key);
+ }
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org