[prev in list] [next in list] [prev in thread] [next in thread]
List: jakarta-commons-dev
Subject: cvs commit: jakarta-commons/beanutils/src/test/org/apache/commons/beanutils ConvertUtilsTestCase.jav
From: craigmcc () apache ! org
Date: 2002-06-29 22:29:22
[Download RAW message or body]
craigmcc 2002/06/29 15:29:22
Modified: beanutils/src/java/org/apache/commons/beanutils
ConvertUtils.java
beanutils/src/test/org/apache/commons/beanutils
ConvertUtilsTestCase.java
Added: beanutils/src/java/org/apache/commons/beanutils/converters
AbstractArrayConverter.java
BooleanArrayConverter.java ByteArrayConverter.java
CharacterArrayConverter.java
DoubleArrayConverter.java FloatArrayConverter.java
IntegerArrayConverter.java LongArrayConverter.java
ShortArrayConverter.java StringArrayConverter.java
Log:
Add new standard converters (and register them) for String to primitive
array conversions (for example, String --> int[]), as well as
String --> String[]. The syntax accepted by these converters is similar
to that accepted by a Java compiler doing array initializers, with the
following adjustments:
* Only literal values of the appropriate type are recognized
(i.e. no expressions)
* Leading and trailing "{" and "}" are allowed, but optional
* Commas between elements are considered to be whitespace,
so that they are optional.
Thus, you can initialize a three-element integer array with values
1, 2, and 3 with any of the following String values.
"{ 1, 2, 3}"
"1, 2, 3"
"{ 1 2 3 }"
"1 2 3"
Quoted strings embedded in the initial String (delimited with either single
or double quotes) are recognized by the rules of the java.io.StreamTokenizer
class. This is particularly useful in the String --> String[] converter,
where you can initialize array elements to a String that includes a comma
or embedded quote character.
Added some negative and positive tests for int[] and String[] conversions;
more would be useful.
Revision Changes Path
1.9 +41 -4 \
jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/ConvertUtils.java
Index: ConvertUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/ConvertUtils.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- ConvertUtils.java 15 Jun 2002 20:39:44 -0000 1.8
+++ ConvertUtils.java 29 Jun 2002 22:29:22 -0000 1.9
@@ -72,17 +72,26 @@
import org.apache.commons.beanutils.converters.BigDecimalConverter;
import org.apache.commons.beanutils.converters.BigIntegerConverter;
import org.apache.commons.beanutils.converters.BooleanConverter;
+import org.apache.commons.beanutils.converters.BooleanArrayConverter;
import org.apache.commons.beanutils.converters.ByteConverter;
+import org.apache.commons.beanutils.converters.ByteArrayConverter;
import org.apache.commons.beanutils.converters.CharacterConverter;
+import org.apache.commons.beanutils.converters.CharacterArrayConverter;
import org.apache.commons.beanutils.converters.DoubleConverter;
+import org.apache.commons.beanutils.converters.DoubleArrayConverter;
import org.apache.commons.beanutils.converters.FloatConverter;
+import org.apache.commons.beanutils.converters.FloatArrayConverter;
import org.apache.commons.beanutils.converters.IntegerConverter;
+import org.apache.commons.beanutils.converters.IntegerArrayConverter;
import org.apache.commons.beanutils.converters.LongConverter;
+import org.apache.commons.beanutils.converters.LongArrayConverter;
import org.apache.commons.beanutils.converters.ShortConverter;
+import org.apache.commons.beanutils.converters.ShortArrayConverter;
import org.apache.commons.beanutils.converters.SqlDateConverter;
import org.apache.commons.beanutils.converters.SqlTimeConverter;
import org.apache.commons.beanutils.converters.SqlTimestampConverter;
import org.apache.commons.beanutils.converters.StringConverter;
+import org.apache.commons.beanutils.converters.StringArrayConverter;
import org.apache.commons.collections.FastHashMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -408,28 +417,56 @@
*/
public static void deregister() {
+ boolean booleanArray[] = new boolean[0];
+ byte byteArray[] = new byte[0];
+ char charArray[] = new char[0];
+ double doubleArray[] = new double[0];
+ float floatArray[] = new float[0];
+ int intArray[] = new int[0];
+ long longArray[] = new long[0];
+ short shortArray[] = new short[0];
+ String stringArray[] = new String[0];
+
converters.clear();
converters.put(BigDecimal.class, new BigDecimalConverter());
converters.put(BigInteger.class, new BigIntegerConverter());
converters.put(Boolean.TYPE, new BooleanConverter(defaultBoolean));
converters.put(Boolean.class, new BooleanConverter(defaultBoolean));
+ converters.put(booleanArray.getClass(),
+ new BooleanArrayConverter(booleanArray));
converters.put(Byte.TYPE, new ByteConverter(defaultByte));
converters.put(Byte.class, new ByteConverter(defaultByte));
+ converters.put(byteArray.getClass(),
+ new ByteArrayConverter(byteArray));
converters.put(Character.TYPE,
new CharacterConverter(defaultCharacter));
converters.put(Character.class,
new CharacterConverter(defaultCharacter));
+ converters.put(charArray.getClass(),
+ new CharacterArrayConverter(charArray));
converters.put(Double.TYPE, new DoubleConverter(defaultDouble));
converters.put(Double.class, new DoubleConverter(defaultDouble));
+ converters.put(doubleArray.getClass(),
+ new DoubleArrayConverter(doubleArray));
converters.put(Float.TYPE, new FloatConverter(defaultFloat));
converters.put(Float.class, new FloatConverter(defaultFloat));
+ converters.put(floatArray.getClass(),
+ new FloatArrayConverter(floatArray));
converters.put(Integer.TYPE, new IntegerConverter(defaultInteger));
converters.put(Integer.class, new IntegerConverter(defaultInteger));
+ converters.put(intArray.getClass(),
+ new IntegerArrayConverter(intArray));
converters.put(Long.TYPE, new LongConverter(defaultLong));
converters.put(Long.class, new LongConverter(defaultLong));
+ converters.put(longArray.getClass(),
+ new LongArrayConverter(longArray));
converters.put(Short.TYPE, new ShortConverter(defaultShort));
converters.put(Short.class, new ShortConverter(defaultShort));
+ converters.put(shortArray.getClass(),
+ new ShortArrayConverter(shortArray));
converters.put(String.class, new StringConverter());
+ converters.put(stringArray.getClass(),
+ new StringArrayConverter(stringArray));
converters.put(Date.class, new SqlDateConverter());
converters.put(Time.class, new SqlTimeConverter());
converters.put(Timestamp.class, new SqlTimestampConverter());
1.1 \
jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/AbstractArrayConverter.java
Index: AbstractArrayConverter.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/AbstractArrayConverter.java,v \
1.1 2002/06/29 22:29:22 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2002/06/29 22:29:22 $
*
* ====================================================================
*
* 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.beanutils.converters;
import java.io.IOException;
import java.io.StreamTokenizer;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
/**
* <p>Convenience base class for converters that translate the String
* representation of an array into a corresponding array of primitives
* object. This class encapsulates the functionality required to parse
* the String into a list of String elements that can later be
* individually converted to the appropriate primitive type.</p>
*
* <p>The input syntax accepted by the <code>parseElements()</code> method
* is designed to be compatible with the syntax used to initialize arrays
* in a Java source program, except that only String literal values are
* supported. For maximum flexibility, the surrounding '{' and '}'
* characters are optional, and individual elements may be separated by
* any combination of whitespace and comma characters.</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2002/06/29 22:29:22 $
* @since 1.4
*/
public abstract class AbstractArrayConverter implements Converter {
// ----------------------------------------------------- Instance Variables
/**
* The default value specified to our Constructor, if any.
*/
protected Object defaultValue = null;
/**
* Should we return the default value on conversion errors?
*/
protected boolean useDefault = true;
// --------------------------------------------------------- Public Methods
/**
* Convert the specified input object into an output object of the
* specified type. This method must be implemented by a concrete
* subclass.
*
* @param type Data type to which this value should be converted
* @param value The input value to be converted
*
* @exception ConversionException if conversion cannot be performed
* successfully
*/
public abstract Object convert(Class type, Object value);
// ------------------------------------------------------ Protected Methods
/**
* <p>Parse an incoming String of the form similar to an array initializer
* in the Java language into a <code>List</code> individual Strings
* for each element, according to the following rules.</p>
* <ul>
* <li>The string must have matching '{' and '}' delimiters around
* a comma-delimited list of values.</li>
* <li>Whitespace before and after each element is stripped.
* <li>If an element is itself delimited by matching single or double
* quotes, the usual rules for interpreting a quoted String apply.</li>
* </ul>
*
* @param svalue String value to be parsed
*
* @exception ConversionException if the syntax of <code>svalue</code>
* is not syntactically valid
* @exception NullPointerException if <code>svalue</code>
* is <code>null</code>
*/
protected List parseElements(String svalue) {
// Validate the passed argument
if (svalue == null) {
throw new NullPointerException();
}
// Trim any matching '{' and '}' delimiters
svalue = svalue.trim();
if (svalue.startsWith("{") && svalue.endsWith("}")) {
svalue = svalue.substring(1, svalue.length() - 1);
}
try {
// Set up a StreamTokenizer on the characters in this String
StreamTokenizer st =
new StreamTokenizer(new StringReader(svalue));
st.whitespaceChars(',',','); // Commas are delimiters
st.ordinaryChars('0', '9'); // Needed to turn off numeric flag
st.ordinaryChars('.', '.');
st.ordinaryChars('-', '-');
st.wordChars('0', '9'); // Needed to make part of tokens
st.wordChars('.', '.');
st.wordChars('-', '-');
// Split comma-delimited tokens into a List
ArrayList list = new ArrayList();
while (true) {
int ttype = st.nextToken();
if ((ttype == StreamTokenizer.TT_WORD) ||
(ttype > 0)) {
list.add(st.sval);
} else if (ttype == StreamTokenizer.TT_EOF) {
break;
} else {
throw new ConversionException
("Encountered token of type " + ttype);
}
}
// Return the completed list
return (list);
} catch (IOException e) {
throw new ConversionException(e);
}
}
}
1.1 \
jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/BooleanArrayConverter.java
Index: BooleanArrayConverter.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/BooleanArrayConverter.java,v \
1.1 2002/06/29 22:29:22 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2002/06/29 22:29:22 $
*
* ====================================================================
*
* 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.beanutils.converters;
import java.util.List;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
/**
* <p>Standard {@link Converter} implementation that converts an incoming
* String into a primitive array of boolean. On a conversion failure, returns
* a specified default value or throws a {@link ConversionException} depending
* on how this instance is constructed.</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2002/06/29 22:29:22 $
* @since 1.4
*/
public final class BooleanArrayConverter extends AbstractArrayConverter {
// ----------------------------------------------------------- Constructors
/**
* Create a {@link Converter} that will throw a {@link ConversionException}
* if a conversion error occurs.
*/
public BooleanArrayConverter() {
this.defaultValue = null;
this.useDefault = false;
}
/**
* Create a {@link Converter} that will return the specified default value
* if a conversion error occurs.
*
* @param defaultValue The default value to be returned
*/
public BooleanArrayConverter(Object defaultValue) {
this.defaultValue = defaultValue;
this.useDefault = true;
}
// --------------------------------------------------------- Public Methods
/**
* Convert the specified input object into an output object of the
* specified type.
*
* @param type Data type to which this value should be converted
* @param value The input value to be converted
*
* @exception ConversionException if conversion cannot be performed
* successfully
*/
public Object convert(Class type, Object value) {
// Deal with a null value
if (value == null) {
if (useDefault) {
return (defaultValue);
} else {
throw new ConversionException("No value specified");
}
}
// Parse the input value as a String into elements
// and convert to the appropriate type
try {
List list = parseElements(value.toString());
boolean results[] = new boolean[list.size()];
for (int i = 0; i < results.length; i++) {
String stringValue = (String) list.get(i);
if (stringValue.equalsIgnoreCase("yes") ||
stringValue.equalsIgnoreCase("y") ||
stringValue.equalsIgnoreCase("true") ||
stringValue.equalsIgnoreCase("on") ||
stringValue.equalsIgnoreCase("1")) {
results[i] = true;
} else if (stringValue.equalsIgnoreCase("no") ||
stringValue.equalsIgnoreCase("n") ||
stringValue.equalsIgnoreCase("false") ||
stringValue.equalsIgnoreCase("off") ||
stringValue.equalsIgnoreCase("0")) {
results[i] = false;
} else {
if (useDefault) {
return (defaultValue);
} else {
throw new ConversionException(value.toString());
}
}
}
return (results);
} catch (Exception e) {
if (useDefault) {
return (defaultValue);
} else {
throw new ConversionException(value.toString(), e);
}
}
}
}
1.1 \
jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/ByteArrayConverter.java
Index: ByteArrayConverter.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/ByteArrayConverter.java,v \
1.1 2002/06/29 22:29:22 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2002/06/29 22:29:22 $
*
* ====================================================================
*
* 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.beanutils.converters;
import java.util.List;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
/**
* <p>Standard {@link Converter} implementation that converts an incoming
* String into a primitive array of byte. On a conversion failure, returns
* a specified default value or throws a {@link ConversionException} depending
* on how this instance is constructed.</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2002/06/29 22:29:22 $
* @since 1.4
*/
public final class ByteArrayConverter extends AbstractArrayConverter {
// ----------------------------------------------------------- Constructors
/**
* Create a {@link Converter} that will throw a {@link ConversionException}
* if a conversion error occurs.
*/
public ByteArrayConverter() {
this.defaultValue = null;
this.useDefault = false;
}
/**
* Create a {@link Converter} that will return the specified default value
* if a conversion error occurs.
*
* @param defaultValue The default value to be returned
*/
public ByteArrayConverter(Object defaultValue) {
this.defaultValue = defaultValue;
this.useDefault = true;
}
// --------------------------------------------------------- Public Methods
/**
* Convert the specified input object into an output object of the
* specified type.
*
* @param type Data type to which this value should be converted
* @param value The input value to be converted
*
* @exception ConversionException if conversion cannot be performed
* successfully
*/
public Object convert(Class type, Object value) {
// Deal with a null value
if (value == null) {
if (useDefault) {
return (defaultValue);
} else {
throw new ConversionException("No value specified");
}
}
// Parse the input value as a String into elements
// and convert to the appropriate type
try {
List list = parseElements(value.toString());
byte results[] = new byte[list.size()];
for (int i = 0; i < results.length; i++) {
results[i] = Byte.parseByte((String) list.get(i));
}
return (results);
} catch (Exception e) {
if (useDefault) {
return (defaultValue);
} else {
throw new ConversionException(value.toString(), e);
}
}
}
}
1.1 \
jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/CharacterArrayConverter.java
Index: CharacterArrayConverter.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/CharacterArrayConverter.java,v \
1.1 2002/06/29 22:29:22 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2002/06/29 22:29:22 $
*
* ====================================================================
*
* 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.beanutils.converters;
import java.util.List;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
/**
* <p>Standard {@link Converter} implementation that converts an incoming
* String into a primitive array of char. On a conversion failure, returns
* a specified default value or throws a {@link ConversionException} depending
* on how this instance is constructed.</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2002/06/29 22:29:22 $
* @since 1.4
*/
public final class CharacterArrayConverter extends AbstractArrayConverter {
// ----------------------------------------------------------- Constructors
/**
* Create a {@link Converter} that will throw a {@link ConversionException}
* if a conversion error occurs.
*/
public CharacterArrayConverter() {
this.defaultValue = null;
this.useDefault = false;
}
/**
* Create a {@link Converter} that will return the specified default value
* if a conversion error occurs.
*
* @param defaultValue The default value to be returned
*/
public CharacterArrayConverter(Object defaultValue) {
this.defaultValue = defaultValue;
this.useDefault = true;
}
// --------------------------------------------------------- Public Methods
/**
* Convert the specified input object into an output object of the
* specified type.
*
* @param type Data type to which this value should be converted
* @param value The input value to be converted
*
* @exception ConversionException if conversion cannot be performed
* successfully
*/
public Object convert(Class type, Object value) {
// Deal with a null value
if (value == null) {
if (useDefault) {
return (defaultValue);
} else {
throw new ConversionException("No value specified");
}
}
// Parse the input value as a String into elements
// and convert to the appropriate type
try {
List list = parseElements(value.toString());
char results[] = new char[list.size()];
for (int i = 0; i < results.length; i++) {
results[i] = ((String) list.get(i)).charAt(0);
}
return (results);
} catch (Exception e) {
if (useDefault) {
return (defaultValue);
} else {
throw new ConversionException(value.toString(), e);
}
}
}
}
1.1 \
jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/DoubleArrayConverter.java
Index: DoubleArrayConverter.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/DoubleArrayConverter.java,v \
1.1 2002/06/29 22:29:22 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2002/06/29 22:29:22 $
*
* ====================================================================
*
* 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.beanutils.converters;
import java.util.List;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
/**
* <p>Standard {@link Converter} implementation that converts an incoming
* String into a primitive array of double. On a conversion failure, returns
* a specified default value or throws a {@link ConversionException} depending
* on how this instance is constructed.</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2002/06/29 22:29:22 $
* @since 1.4
*/
public final class DoubleArrayConverter extends AbstractArrayConverter {
// ----------------------------------------------------------- Constructors
/**
* Create a {@link Converter} that will throw a {@link ConversionException}
* if a conversion error occurs.
*/
public DoubleArrayConverter() {
this.defaultValue = null;
this.useDefault = false;
}
/**
* Create a {@link Converter} that will return the specified default value
* if a conversion error occurs.
*
* @param defaultValue The default value to be returned
*/
public DoubleArrayConverter(Object defaultValue) {
this.defaultValue = defaultValue;
this.useDefault = true;
}
// --------------------------------------------------------- Public Methods
/**
* Convert the specified input object into an output object of the
* specified type.
*
* @param type Data type to which this value should be converted
* @param value The input value to be converted
*
* @exception ConversionException if conversion cannot be performed
* successfully
*/
public Object convert(Class type, Object value) {
// Deal with a null value
if (value == null) {
if (useDefault) {
return (defaultValue);
} else {
throw new ConversionException("No value specified");
}
}
// Parse the input value as a String into elements
// and convert to the appropriate type
try {
List list = parseElements(value.toString());
double results[] = new double[list.size()];
for (int i = 0; i < results.length; i++) {
results[i] = Double.parseDouble((String) list.get(i));
}
return (results);
} catch (Exception e) {
if (useDefault) {
return (defaultValue);
} else {
throw new ConversionException(value.toString(), e);
}
}
}
}
1.1 \
jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/FloatArrayConverter.java
Index: FloatArrayConverter.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/FloatArrayConverter.java,v \
1.1 2002/06/29 22:29:22 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2002/06/29 22:29:22 $
*
* ====================================================================
*
* 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.beanutils.converters;
import java.util.List;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
/**
* <p>Standard {@link Converter} implementation that converts an incoming
* String into a primitive array of float. On a conversion failure, returns
* a specified default value or throws a {@link ConversionException} depending
* on how this instance is constructed.</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2002/06/29 22:29:22 $
* @since 1.4
*/
public final class FloatArrayConverter extends AbstractArrayConverter {
// ----------------------------------------------------------- Constructors
/**
* Create a {@link Converter} that will throw a {@link ConversionException}
* if a conversion error occurs.
*/
public FloatArrayConverter() {
this.defaultValue = null;
this.useDefault = false;
}
/**
* Create a {@link Converter} that will return the specified default value
* if a conversion error occurs.
*
* @param defaultValue The default value to be returned
*/
public FloatArrayConverter(Object defaultValue) {
this.defaultValue = defaultValue;
this.useDefault = true;
}
// --------------------------------------------------------- Public Methods
/**
* Convert the specified input object into an output object of the
* specified type.
*
* @param type Data type to which this value should be converted
* @param value The input value to be converted
*
* @exception ConversionException if conversion cannot be performed
* successfully
*/
public Object convert(Class type, Object value) {
// Deal with a null value
if (value == null) {
if (useDefault) {
return (defaultValue);
} else {
throw new ConversionException("No value specified");
}
}
// Parse the input value as a String into elements
// and convert to the appropriate type
try {
List list = parseElements(value.toString());
float results[] = new float[list.size()];
for (int i = 0; i < results.length; i++) {
results[i] = Float.parseFloat((String) list.get(i));
}
return (results);
} catch (Exception e) {
if (useDefault) {
return (defaultValue);
} else {
throw new ConversionException(value.toString(), e);
}
}
}
}
1.1 \
jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/IntegerArrayConverter.java
Index: IntegerArrayConverter.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/IntegerArrayConverter.java,v \
1.1 2002/06/29 22:29:22 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2002/06/29 22:29:22 $
*
* ====================================================================
*
* 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.beanutils.converters;
import java.util.List;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
/**
* <p>Standard {@link Converter} implementation that converts an incoming
* String into a primitive array of int. On a conversion failure, returns
* a specified default value or throws a {@link ConversionException} depending
* on how this instance is constructed.</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2002/06/29 22:29:22 $
* @since 1.4
*/
public final class IntegerArrayConverter extends AbstractArrayConverter {
// ----------------------------------------------------------- Constructors
/**
* Create a {@link Converter} that will throw a {@link ConversionException}
* if a conversion error occurs.
*/
public IntegerArrayConverter() {
this.defaultValue = null;
this.useDefault = false;
}
/**
* Create a {@link Converter} that will return the specified default value
* if a conversion error occurs.
*
* @param defaultValue The default value to be returned
*/
public IntegerArrayConverter(Object defaultValue) {
this.defaultValue = defaultValue;
this.useDefault = true;
}
// --------------------------------------------------------- Public Methods
/**
* Convert the specified input object into an output object of the
* specified type.
*
* @param type Data type to which this value should be converted
* @param value The input value to be converted
*
* @exception ConversionException if conversion cannot be performed
* successfully
*/
public Object convert(Class type, Object value) {
// Deal with a null value
if (value == null) {
if (useDefault) {
return (defaultValue);
} else {
throw new ConversionException("No value specified");
}
}
// Parse the input value as a String into elements
// and convert to the appropriate type
try {
List list = parseElements(value.toString());
int results[] = new int[list.size()];
for (int i = 0; i < results.length; i++) {
results[i] = Integer.parseInt((String) list.get(i));
}
return (results);
} catch (Exception e) {
if (useDefault) {
return (defaultValue);
} else {
throw new ConversionException(value.toString(), e);
}
}
}
}
1.1 \
jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/LongArrayConverter.java
Index: LongArrayConverter.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/LongArrayConverter.java,v \
1.1 2002/06/29 22:29:22 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2002/06/29 22:29:22 $
*
* ====================================================================
*
* 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.beanutils.converters;
import java.util.List;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
/**
* <p>Standard {@link Converter} implementation that converts an incoming
* String into a primitive array of long. On a conversion failure, returns
* a specified default value or throws a {@link ConversionException} depending
* on how this instance is constructed.</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2002/06/29 22:29:22 $
* @since 1.4
*/
public final class LongArrayConverter extends AbstractArrayConverter {
// ----------------------------------------------------------- Constructors
/**
* Create a {@link Converter} that will throw a {@link ConversionException}
* if a conversion error occurs.
*/
public LongArrayConverter() {
this.defaultValue = null;
this.useDefault = false;
}
/**
* Create a {@link Converter} that will return the specified default value
* if a conversion error occurs.
*
* @param defaultValue The default value to be returned
*/
public LongArrayConverter(Object defaultValue) {
this.defaultValue = defaultValue;
this.useDefault = true;
}
// --------------------------------------------------------- Public Methods
/**
* Convert the specified input object into an output object of the
* specified type.
*
* @param type Data type to which this value should be converted
* @param value The input value to be converted
*
* @exception ConversionException if conversion cannot be performed
* successfully
*/
public Object convert(Class type, Object value) {
// Deal with a null value
if (value == null) {
if (useDefault) {
return (defaultValue);
} else {
throw new ConversionException("No value specified");
}
}
// Parse the input value as a String into elements
// and convert to the appropriate type
try {
List list = parseElements(value.toString());
long results[] = new long[list.size()];
for (int i = 0; i < results.length; i++) {
results[i] = Long.parseLong((String) list.get(i));
}
return (results);
} catch (Exception e) {
if (useDefault) {
return (defaultValue);
} else {
throw new ConversionException(value.toString(), e);
}
}
}
}
1.1 \
jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/ShortArrayConverter.java
Index: ShortArrayConverter.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/ShortArrayConverter.java,v \
1.1 2002/06/29 22:29:22 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2002/06/29 22:29:22 $
*
* ====================================================================
*
* 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.beanutils.converters;
import java.util.List;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
/**
* <p>Standard {@link Converter} implementation that converts an incoming
* String into a primitive array of short. On a conversion failure, returns
* a specified default value or throws a {@link ConversionException} depending
* on how this instance is constructed.</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2002/06/29 22:29:22 $
* @since 1.4
*/
public final class ShortArrayConverter extends AbstractArrayConverter {
// ----------------------------------------------------------- Constructors
/**
* Create a {@link Converter} that will throw a {@link ConversionException}
* if a conversion error occurs.
*/
public ShortArrayConverter() {
this.defaultValue = null;
this.useDefault = false;
}
/**
* Create a {@link Converter} that will return the specified default value
* if a conversion error occurs.
*
* @param defaultValue The default value to be returned
*/
public ShortArrayConverter(Object defaultValue) {
this.defaultValue = defaultValue;
this.useDefault = true;
}
// --------------------------------------------------------- Public Methods
/**
* Convert the specified input object into an output object of the
* specified type.
*
* @param type Data type to which this value should be converted
* @param value The input value to be converted
*
* @exception ConversionException if conversion cannot be performed
* successfully
*/
public Object convert(Class type, Object value) {
// Deal with a null value
if (value == null) {
if (useDefault) {
return (defaultValue);
} else {
throw new ConversionException("No value specified");
}
}
// Parse the input value as a String into elements
// and convert to the appropriate type
try {
List list = parseElements(value.toString());
short results[] = new short[list.size()];
for (int i = 0; i < results.length; i++) {
results[i] = Short.parseShort((String) list.get(i));
}
return (results);
} catch (Exception e) {
if (useDefault) {
return (defaultValue);
} else {
throw new ConversionException(value.toString(), e);
}
}
}
}
1.1 \
jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/StringArrayConverter.java
Index: StringArrayConverter.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/converters/StringArrayConverter.java,v \
1.1 2002/06/29 22:29:22 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2002/06/29 22:29:22 $
*
* ====================================================================
*
* 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.beanutils.converters;
import java.util.List;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
/**
* <p>Standard {@link Converter} implementation that converts an incoming
* String into an array of String. On a conversion failure, returns
* a specified default value or throws a {@link ConversionException} depending
* on how this instance is constructed.</p>
*
* @author Craig R. McClanahan
* @version $Revision: 1.1 $ $Date: 2002/06/29 22:29:22 $
* @since 1.4
*/
public final class StringArrayConverter extends AbstractArrayConverter {
// ----------------------------------------------------------- Constructors
/**
* Create a {@link Converter} that will throw a {@link ConversionException}
* if a conversion error occurs.
*/
public StringArrayConverter() {
this.defaultValue = null;
this.useDefault = false;
}
/**
* Create a {@link Converter} that will return the specified default value
* if a conversion error occurs.
*
* @param defaultValue The default value to be returned
*/
public StringArrayConverter(Object defaultValue) {
this.defaultValue = defaultValue;
this.useDefault = true;
}
// --------------------------------------------------------- Public Methods
/**
* Convert the specified input object into an output object of the
* specified type.
*
* @param type Data type to which this value should be converted
* @param value The input value to be converted
*
* @exception ConversionException if conversion cannot be performed
* successfully
*/
public Object convert(Class type, Object value) {
// Deal with a null value
if (value == null) {
if (useDefault) {
return (defaultValue);
} else {
throw new ConversionException("No value specified");
}
}
// Parse the input value as a String into elements
// and convert to the appropriate type
try {
List list = parseElements(value.toString());
String results[] = new String[list.size()];
for (int i = 0; i < results.length; i++) {
results[i] = (String) list.get(i);
}
return (results);
} catch (Exception e) {
if (useDefault) {
return (defaultValue);
} else {
throw new ConversionException(value.toString(), e);
}
}
}
}
1.4 +172 -4 \
jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/ConvertUtilsTestCase.java
Index: ConvertUtilsTestCase.java
===================================================================
RCS file: /home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/ConvertUtilsTestCase.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ConvertUtilsTestCase.java 15 Jun 2002 21:14:34 -0000 1.3
+++ ConvertUtilsTestCase.java 29 Jun 2002 22:29:22 -0000 1.4
@@ -132,6 +132,33 @@
/**
+ * Negative String to primitive integer array tests.
+ */
+ public void testNegativeIntegerArray() {
+
+ Object value = null;
+ int intArray[] = new int[0];
+
+ value = ConvertUtils.convert((String) null, intArray.getClass());
+ checkIntegerArray(value, intArray);
+ value = ConvertUtils.convert("a", intArray.getClass());
+ checkIntegerArray(value, intArray);
+ value = ConvertUtils.convert("{ a }", intArray.getClass());
+ checkIntegerArray(value, intArray);
+ value = ConvertUtils.convert("1a3", intArray.getClass());
+ checkIntegerArray(value, intArray);
+ value = ConvertUtils.convert("{ 1a3 }", intArray.getClass());
+ checkIntegerArray(value, intArray);
+ value = ConvertUtils.convert("0,1a3", intArray.getClass());
+ checkIntegerArray(value, intArray);
+ value = ConvertUtils.convert("{ 0, 1a3 }", intArray.getClass());
+ checkIntegerArray(value, intArray);
+
+
+ }
+
+
+ /**
* Negative scalar conversion tests. These rely on the standard
* default value conversions in ConvertUtils.
*/
@@ -203,6 +230,20 @@
/**
+ * Negative String to String array tests.
+ */
+ public void testNegativeStringArray() {
+
+ Object value = null;
+ String stringArray[] = new String[0];
+
+ value = ConvertUtils.convert((String) null, stringArray.getClass());
+ checkStringArray(value, stringArray);
+
+ }
+
+
+ /**
* Test conversion of object to string for arrays.
*/
public void testObjectToStringArray() {
@@ -288,6 +329,42 @@
/**
+ * Positive String to primitive integer array tests.
+ */
+ public void testPositiveIntegerArray() {
+
+ Object value = null;
+ int intArray[] = new int[0];
+ int intArray1[] = new int[] { 0 };
+ int intArray2[] = new int[] { 0, 10 };
+
+ value = ConvertUtils.convert("{ }", intArray.getClass());
+ checkIntegerArray(value, intArray);
+
+ value = ConvertUtils.convert("0", intArray.getClass());
+ checkIntegerArray(value, intArray1);
+ value = ConvertUtils.convert(" 0 ", intArray.getClass());
+ checkIntegerArray(value, intArray1);
+ value = ConvertUtils.convert("{ 0 }", intArray.getClass());
+ checkIntegerArray(value, intArray1);
+
+ value = ConvertUtils.convert("0,10", intArray.getClass());
+ checkIntegerArray(value, intArray2);
+ value = ConvertUtils.convert("0 10", intArray.getClass());
+ checkIntegerArray(value, intArray2);
+ value = ConvertUtils.convert("{0,10}", intArray.getClass());
+ checkIntegerArray(value, intArray2);
+ value = ConvertUtils.convert("{0 10}", intArray.getClass());
+ checkIntegerArray(value, intArray2);
+ value = ConvertUtils.convert("{ 0, 10 }", intArray.getClass());
+ checkIntegerArray(value, intArray2);
+ value = ConvertUtils.convert("{ 0 10 }", intArray.getClass());
+ checkIntegerArray(value, intArray2);
+
+ }
+
+
+ /**
* Positive scalar conversion tests.
*/
public void testPositiveScalar() {
@@ -434,6 +511,97 @@
value = ConvertUtils.convert(input, Timestamp.class);
assertTrue(value instanceof Timestamp);
assertEquals(input, value.toString());
+
+ }
+
+
+ /**
+ * Positive String to String array tests.
+ */
+ public void testPositiveStringArray() {
+
+ Object value = null;
+ String stringArray[] = new String[0];
+ String stringArray1[] = new String[]
+ { "abc" };
+ String stringArray2[] = new String[]
+ { "abc", "de,f" };
+
+ value = ConvertUtils.convert("", stringArray.getClass());
+ checkStringArray(value, stringArray);
+ value = ConvertUtils.convert(" ", stringArray.getClass());
+ checkStringArray(value, stringArray);
+ value = ConvertUtils.convert("{}", stringArray.getClass());
+ checkStringArray(value, stringArray);
+ value = ConvertUtils.convert("{ }", stringArray.getClass());
+ checkStringArray(value, stringArray);
+
+ value = ConvertUtils.convert("abc", stringArray.getClass());
+ checkStringArray(value, stringArray1);
+ value = ConvertUtils.convert("{abc}", stringArray.getClass());
+ checkStringArray(value, stringArray1);
+ value = ConvertUtils.convert("\"abc\"", stringArray.getClass());
+ checkStringArray(value, stringArray1);
+ value = ConvertUtils.convert("{\"abc\"}", stringArray.getClass());
+ checkStringArray(value, stringArray1);
+ value = ConvertUtils.convert("'abc'", stringArray.getClass());
+ checkStringArray(value, stringArray1);
+ value = ConvertUtils.convert("{'abc'}", stringArray.getClass());
+ checkStringArray(value, stringArray1);
+
+ value = ConvertUtils.convert("abc 'de,f'",
+ stringArray.getClass());
+ checkStringArray(value, stringArray2);
+ value = ConvertUtils.convert("{abc, 'de,f'}",
+ stringArray.getClass());
+ checkStringArray(value, stringArray2);
+ value = ConvertUtils.convert("\"abc\",\"de,f\"",
+ stringArray.getClass());
+ checkStringArray(value, stringArray2);
+ value = ConvertUtils.convert("{\"abc\" 'de,f'}",
+ stringArray.getClass());
+ checkStringArray(value, stringArray2);
+ value = ConvertUtils.convert("'abc' 'de,f'",
+ stringArray.getClass());
+ checkStringArray(value, stringArray2);
+ value = ConvertUtils.convert("{'abc', \"de,f\"}",
+ stringArray.getClass());
+ checkStringArray(value, stringArray2);
+
+
+ }
+
+
+ // -------------------------------------------------------- Private Methods
+
+
+ private void checkIntegerArray(Object value, int intArray[]) {
+
+ assertNotNull("Returned value is not null", value);
+ assertEquals("Returned value is int[]",
+ intArray.getClass(), value.getClass());
+ int results[] = (int[]) value;
+ assertEquals("Returned array length", intArray.length, results.length);
+ for (int i = 0; i < intArray.length; i++) {
+ assertEquals("Returned array value " + i,
+ intArray[i], results[i]);
+ }
+
+ }
+
+
+ private void checkStringArray(Object value, String stringArray[]) {
+
+ assertNotNull("Returned value is not null", value);
+ assertEquals("Returned value is String[]",
+ stringArray.getClass(), value.getClass());
+ String results[] = (String[]) value;
+ assertEquals("Returned array length",
+ stringArray.length, results.length);
+ for (int i = 0; i < stringArray.length; i++) {
+ assertEquals("Returned array value " + i,
+ stringArray[i], results[i]);
+ }
}
--
To unsubscribe, e-mail: <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic