Author: epugh Date: Mon Nov 28 14:57:22 2005 New Revision: 349543 URL: http://svn.apache.org/viewcvs?rev=349543&view=rev Log: Make the ParameterParser interface match what Turbine 2.3 uses.
Modified: jakarta/turbine/fulcrum/trunk/parser/project.xml jakarta/turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/BaseValueParser.java jakarta/turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/ValueParser.java Modified: jakarta/turbine/fulcrum/trunk/parser/project.xml URL: http://svn.apache.org/viewcvs/jakarta/turbine/fulcrum/trunk/parser/project.xml?rev=349543&r1=349542&r2=349543&view=diff ============================================================================== --- jakarta/turbine/fulcrum/trunk/parser/project.xml (original) +++ jakarta/turbine/fulcrum/trunk/parser/project.xml Mon Nov 28 14:57:22 2005 @@ -16,7 +16,13 @@ <artifactId>commons-fileupload</artifactId> <version>1.0</version> <url>http://jakarta.apache.org/commons/fileupload/index.html</url> - </dependency> + </dependency> + <dependency> + <groupId>commons-lang</groupId> + <artifactId>commons-lang</artifactId> + <version>2.0</version> + <url>http://jakarta.apache.org/commons/lang/index.html</url> + </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> Modified: jakarta/turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/BaseValueParser.java URL: http://svn.apache.org/viewcvs/jakarta/turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/BaseValueParser.java?rev=349543&r1=349542&r2=349543&view=diff ============================================================================== --- jakarta/turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/BaseValueParser.java (original) +++ jakarta/turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/BaseValueParser.java Mon Nov 28 14:57:22 2005 @@ -27,12 +27,16 @@ import java.text.DateFormat; import java.text.ParseException; import java.util.Date; -import java.util.Enumeration; import java.util.Hashtable; +import java.util.Iterator; import java.util.Set; +import org.apache.commons.lang.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.apache.fulcrum.pool.Recyclable; + /** * BaseValueParser is a base class for classes that need to parse * name/value Parameters, for example GET/POST data or Cookies @@ -63,6 +67,8 @@ */ public class BaseValueParser implements ValueParser, Recyclable { + /** Logging */ + private static Log log = LogFactory.getLog(BaseValueParser.class); public BaseValueParser(){ recycle(); @@ -228,9 +234,25 @@ } /** + * Add an array of Strings for a key. This + * is simply adding all the elements in the + * array one by one. + * + * @param name A String with the name. + * @param value A String Array. + */ + public void add(String name, String [] value) + { + for (int i = 0 ; i < value.length; i++) + { + add(name, value[i]); + } + } + + /** * Add a String parameters. If there are any Strings already * associated with the name, append to the array. This is used - * for handling parameters from mulitipart POST requests. + * for handling parameters from multipart POST requests. * * @param name A String with the name. * @param value A String with the value. @@ -255,8 +277,7 @@ /** * Removes the named parameter from the contained hashtable. Wraps to the - * contained <code>Hashtable.remove()</code>. - * + * contained <code>Map.remove()</code>. * * @return The value that was mapped to the key (a <code>String[]</code>) * or <code>null</code> if the key was not mapped. @@ -265,7 +286,6 @@ { return parameters.remove(convert(name)); } - /** * Trims the string data and applies the conversion specified in * the property given by URL_CASE_FOLDING. It returns a new @@ -278,7 +298,6 @@ { return convertAndTrim(value); } - /** * Determine whether a given key has been inserted. All keys are * stored in lowercase strings, so override method to account for @@ -289,7 +308,7 @@ */ public boolean containsKey(Object key) { - return parameters.containsKey(convert((String) key)); + return parameters.containsKey(convert((String)key)); } /** @@ -302,7 +321,7 @@ return parameters.keySet(); } - /* + /** * Returns all the available parameter names. * * @return A object array with the keys. @@ -322,25 +341,8 @@ */ public boolean getBoolean(String name, boolean defaultValue) { - boolean value = defaultValue; - Object object = parameters.get(convert(name)); - if (object != null) - { - String tmp = getString(name); - if (tmp.equalsIgnoreCase("1") - || tmp.equalsIgnoreCase("true") - || tmp.equalsIgnoreCase("on")) - { - value = true; - } - if (tmp.equalsIgnoreCase("0") - || tmp.equalsIgnoreCase("false") - || tmp.equalsIgnoreCase("off")) - { - value = false; - } - } - return value; + Boolean result = getBooleanObject(name); + return (result == null ? defaultValue : result.booleanValue()); } /** @@ -356,28 +358,62 @@ } /** - * Return a Boolean for the given name. If the name does not - * exist, return defaultValue. + * Returns a Boolean object for the given name. If the parameter + * does not exist or can not be parsed as a boolean, null is returned. + * <p> + * Valid values for true: true, on, 1, yes<br> + * Valid values for false: false, off, 0, no<br> + * <p> + * The string is compared without reguard to case. * * @param name A String with the name. - * @param defaultValue The default value. * @return A Boolean. */ - public Boolean getBool(String name, boolean defaultValue) + public Boolean getBooleanObject(String name) { - return new Boolean(getBoolean(name, defaultValue)); + Boolean result = null; + String value = getString(name); + if (StringUtils.isNotEmpty(value)) + { + if (value.equals("1") || + value.equalsIgnoreCase("true") || + value.equalsIgnoreCase("yes") || + value.equalsIgnoreCase("on")) + { + result = Boolean.TRUE; + } + else if (value.equals("0") || + value.equalsIgnoreCase("false") || + value.equalsIgnoreCase("no") || + value.equalsIgnoreCase("off")) + { + result = Boolean.FALSE; + } + else + { + logConversionFailure(name, value, "Boolean"); + } + } + return result; } /** - * Return a Boolean for the given name. If the name does not - * exist, return false. + * Returns a Boolean object for the given name. If the parameter + * does not exist or can not be parsed as a boolean, null is returned. + * <p> + * Valid values for true: true, on, 1, yes<br> + * Valid values for false: false, off, 0, no<br> + * <p> + * The string is compared without reguard to case. * * @param name A String with the name. + * @param defaultValue The default value. * @return A Boolean. */ - public Boolean getBool(String name) + public Boolean getBooleanObject(String name, Boolean defaultValue) { - return new Boolean(getBoolean(name, false)); + Boolean result = getBooleanObject(name); + return (result==null ? defaultValue : result); } /** @@ -390,19 +426,20 @@ */ public double getDouble(String name, double defaultValue) { - double value = defaultValue; - try + double result = defaultValue; + String value = getString(name); + if (StringUtils.isNotEmpty(value)) { - Object object = parameters.get(convert(name)); - if (object != null) + try { - value = Double.valueOf(((String[]) object)[0]).doubleValue(); + result = Double.valueOf(value).doubleValue(); + } + catch (NumberFormatException e) + { + logConversionFailure(name, value, "Double"); } } - catch (NumberFormatException exception) - { - } - return value; + return result; } /** @@ -418,6 +455,109 @@ } /** + * Return an array of doubles for the given name. If the name does + * not exist, return null. + * + * @param name A String with the name. + * @return A double[]. + */ + public double[] getDoubles(String name) + { + double[] result = null; + String value[] = getStrings(name); + if (value != null) + { + result = new double[value.length]; + for (int i = 0; i < value.length; i++) + { + if (StringUtils.isNotEmpty(value[i])) + { + try + { + result[i] = Double.parseDouble(value[i]); + } + catch (NumberFormatException e) + { + logConversionFailure(name, value[i], "Double"); + } + } + } + } + return result; + } + + /** + * Return a Double for the given name. If the name does not + * exist, return defaultValue. + * + * @param name A String with the name. + * @param defaultValue The default value. + * @return A double. + */ + public Double getDoubleObject(String name, Double defaultValue) + { + Double result = getDoubleObject(name); + return (result==null ? defaultValue : result); + } + + /** + * Return a Double for the given name. If the name does not + * exist, return null. + * + * @param name A String with the name. + * @return A double. + */ + public Double getDoubleObject(String name) + { + Double result = null; + String value = getString(name); + if (StringUtils.isNotEmpty(value)) + { + try + { + result = new Double(value); + } + catch(NumberFormatException e) + { + logConversionFailure(name, value, "Double"); + } + } + return result; + } + + /** + * Return an array of doubles for the given name. If the name does + * not exist, return null. + * + * @param name A String with the name. + * @return A double[]. + */ + public Double[] getDoubleObjects(String name) + { + Double[] result = null; + String value[] = getStrings(convert(name)); + if (value != null) + { + result = new Double[value.length]; + for (int i = 0; i < value.length; i++) + { + if (StringUtils.isNotEmpty(value[i])) + { + try + { + result[i] = Double.valueOf(value[i]); + } + catch (NumberFormatException e) + { + logConversionFailure(name, value[i], "Double"); + } + } + } + } + return result; + } + + /** * Return a float for the given name. If the name does not * exist, return defaultValue. * @@ -427,19 +567,20 @@ */ public float getFloat(String name, float defaultValue) { - float value = defaultValue; - try + float result = defaultValue; + String value = getString(name); + if (StringUtils.isNotEmpty(value)) { - Object object = parameters.get(convert(name)); - if (object != null) + try { - value = Float.valueOf(((String[]) object)[0]).floatValue(); + result = Float.valueOf(value).floatValue(); + } + catch (NumberFormatException e) + { + logConversionFailure(name, value, "Float"); } } - catch (NumberFormatException exception) - { - } - return value; + return result; } /** @@ -455,32 +596,132 @@ } /** - * Return a BigDecimal for the given name. If the name does not - * exist, return 0.0. + * Return an array of floats for the given name. If the name does + * not exist, return null. + * + * @param name A String with the name. + * @return A float[]. + */ + public float[] getFloats(String name) + { + float[] result = null; + String value[] = getStrings(name); + if (value != null) + { + result = new float[value.length]; + for (int i = 0; i < value.length; i++) + { + if (StringUtils.isNotEmpty(value[i])) + { + try + { + result[i] = Float.parseFloat(value[i]); + } + catch (NumberFormatException e) + { + logConversionFailure(name, value[i], "Float"); + } + } + } + } + return result; + } + + /** + * Return a Float for the given name. If the name does not + * exist, return defaultValue. * * @param name A String with the name. * @param defaultValue The default value. - * @return A BigDecimal. + * @return A Float. */ - public BigDecimal getBigDecimal(String name, BigDecimal defaultValue) + public Float getFloatObject(String name, Float defaultValue) { - BigDecimal value = defaultValue; - try + Float result = getFloatObject(name); + return (result==null ? defaultValue : result); + } + + /** + * Return a float for the given name. If the name does not + * exist, return null. + * + * @param name A String with the name. + * @return A Float. + */ + public Float getFloatObject(String name) + { + Float result = null; + String value = getString(name); + if (StringUtils.isNotEmpty(value)) { - Object object = parameters.get(convert(name)); - if (object != null) + try + { + result = new Float(value); + } + catch(NumberFormatException e) { - String temp = ((String[]) object)[0]; - if (temp.length() > 0) + logConversionFailure(name, value, "Float"); + } + } + return result; + } + + /** + * Return an array of floats for the given name. If the name does + * not exist, return null. + * + * @param name A String with the name. + * @return A float[]. + */ + public Float[] getFloatObjects(String name) + { + Float[] result = null; + String value[] = getStrings(convert(name)); + if (value != null) + { + result = new Float[value.length]; + for (int i = 0; i < value.length; i++) + { + if (StringUtils.isNotEmpty(value[i])) { - value = new BigDecimal(((String[]) object)[0]); + try + { + result[i] = Float.valueOf(value[i]); + } + catch (NumberFormatException e) + { + logConversionFailure(name, value[i], "Float"); + } } } } - catch (NumberFormatException exception) + return result; + } + + /** + * Return a BigDecimal for the given name. If the name does not + * exist, return defaultValue. + * + * @param name A String with the name. + * @param defaultValue The default value. + * @return A BigDecimal. + */ + public BigDecimal getBigDecimal(String name, BigDecimal defaultValue) + { + BigDecimal result = defaultValue; + String value = getString(name); + if (StringUtils.isNotEmpty(value)) { + try + { + result = new BigDecimal(value); + } + catch (NumberFormatException e) + { + logConversionFailure(name, value, "BigDecimal"); + } } - return value; + return result; } /** @@ -504,18 +745,27 @@ */ public BigDecimal[] getBigDecimals(String name) { - BigDecimal[] value = null; - Object object = getStrings(convert(name)); - if (object != null) + BigDecimal[] result = null; + String value[] = getStrings(name); + if (value != null) { - String[] temp = (String[]) object; - value = new BigDecimal[temp.length]; - for (int i = 0; i < temp.length; i++) + result = new BigDecimal[value.length]; + for (int i = 0; i < value.length; i++) { - value[i] = new BigDecimal(temp[i]); + if(StringUtils.isNotEmpty(value[i])) + { + try + { + result[i] = new BigDecimal(value[i]); + } + catch (NumberFormatException e) + { + logConversionFailure(name, value[i], "BigDecimal"); + } + } } } - return value; + return result; } /** @@ -528,19 +778,20 @@ */ public int getInt(String name, int defaultValue) { - int value = defaultValue; - try + int result = defaultValue; + String value = getString(name); + if (StringUtils.isNotEmpty(value)) { - Object object = parameters.get(convert(name)); - if (object != null) + try { - value = Integer.valueOf(((String[]) object)[0]).intValue(); + result = Integer.valueOf(value).intValue(); + } + catch (NumberFormatException e) + { + logConversionFailure(name, value, "Integer"); } } - catch (NumberFormatException exception) - { - } - return value; + return result; } /** @@ -556,65 +807,74 @@ } /** - * Return an Integer for the given name. If the name does not - * exist, return defaultValue. + * Return an array of ints for the given name. If the name does + * not exist, return null. * * @param name A String with the name. - * @param defaultValue The default value. - * @return An Integer. + * @return An int[]. */ - public Integer getInteger(String name, int defaultValue) + public int[] getInts(String name) { - return new Integer(getInt(name, defaultValue)); + int[] result = null; + String value[] = getStrings(name); + if (value != null) + { + result = new int[value.length]; + for (int i = 0; i < value.length; i++) + { + if (StringUtils.isNotEmpty(value[i])) + { + try + { + result[i] = Integer.parseInt(value[i]); + } + catch (NumberFormatException e) + { + logConversionFailure(name, value[i], "Integer"); + } + } + } + } + return result; } /** - * Return an Integer for the given name. If the name does not - * exist, return defaultValue. You cannot pass in a null here for - * the default value. + * Return an Integer for the given name. If the name does not exist, + * return defaultValue. * * @param name A String with the name. * @param defaultValue The default value. * @return An Integer. */ - public Integer getInteger(String name, Integer def) + public Integer getIntObject(String name, Integer defaultValue) { - return new Integer(getInt(name, def.intValue())); + Integer result = getIntObject(name); + return (result==null ? defaultValue : result); } /** - * Return an Integer for the given name. If the name does not - * exist, return 0. + * Return an Integer for the given name. If the name does not exist, + * return null. * * @param name A String with the name. * @return An Integer. */ - public Integer getInteger(String name) - { - return new Integer(getInt(name, 0)); - } - - /** - * Return an array of ints for the given name. If the name does - * not exist, return null. - * - * @param name A String with the name. - * @return An int[]. - */ - public int[] getInts(String name) + public Integer getIntObject(String name) { - int[] value = null; - Object object = getStrings(convert(name)); - if (object != null) + Integer result = null; + String value = getString(name); + if (StringUtils.isNotEmpty(value)) { - String[] temp = (String[]) object; - value = new int[temp.length]; - for (int i = 0; i < temp.length; i++) + try { - value[i] = Integer.parseInt(temp[i]); + result = new Integer(value); + } + catch(NumberFormatException e) + { + logConversionFailure(name, value, "Integer"); } } - return value; + return result; } /** @@ -624,20 +884,29 @@ * @param name A String with the name. * @return An Integer[]. */ - public Integer[] getIntegers(String name) + public Integer[] getIntObjects(String name) { - Integer[] value = null; - Object object = getStrings(convert(name)); - if (object != null) + Integer[] result = null; + String value[] = getStrings(convert(name)); + if (value != null) { - String[] temp = (String[]) object; - value = new Integer[temp.length]; - for (int i = 0; i < temp.length; i++) + result = new Integer[value.length]; + for (int i = 0; i < value.length; i++) { - value[i] = Integer.valueOf(temp[i]); + if (StringUtils.isNotEmpty(value[i])) + { + try + { + result[i] = Integer.valueOf(value[i]); + } + catch (NumberFormatException e) + { + logConversionFailure(name, value[i], "Integer"); + } + } } } - return value; + return result; } /** @@ -650,19 +919,20 @@ */ public long getLong(String name, long defaultValue) { - long value = defaultValue; - try + long result = defaultValue; + String value = getString(name); + if (StringUtils.isNotEmpty(value)) { - Object object = parameters.get(convert(name)); - if (object != null) + try { - value = Long.valueOf(((String[]) object)[0]).longValue(); + result = Long.valueOf(value).longValue(); + } + catch (NumberFormatException e) + { + logConversionFailure(name, value, "Long"); } } - catch (NumberFormatException exception) - { - } - return value; + return result; } /** @@ -686,18 +956,27 @@ */ public long[] getLongs(String name) { - long[] value = null; - Object object = getStrings(convert(name)); - if (object != null) + long[] result = null; + String value[] = getStrings(name); + if (value != null) { - String[] temp = (String[]) object; - value = new long[temp.length]; - for (int i = 0; i < temp.length; i++) + result = new long[value.length]; + for (int i = 0; i < value.length; i++) { - value[i] = Long.parseLong(temp[i]); + if (StringUtils.isNotEmpty(value[i])) + { + try + { + result[i] = Long.parseLong(value[i]); + } + catch (NumberFormatException e) + { + logConversionFailure(name, value[i], "Long"); + } + } } } - return value; + return result; } /** @@ -709,18 +988,66 @@ */ public Long[] getLongObjects(String name) { - Long[] value = null; - Object object = getStrings(convert(name)); - if (object != null) + Long[] result = null; + String value[] = getStrings(convert(name)); + if (value != null) + { + result = new Long[value.length]; + for (int i = 0; i < value.length; i++) + { + if (StringUtils.isNotEmpty(value[i])) + { + try + { + result[i] = Long.valueOf(value[i]); + } + catch (NumberFormatException e) + { + logConversionFailure(name, value[i], "Long"); + } + } + } + } + return result; + } + + /** + * Return a Long for the given name. If the name does + * not exist, return null. + * + * @param name A String with the name. + * @return A Long. + */ + public Long getLongObject(String name) + { + Long result = null; + String value = getString(name); + if (StringUtils.isNotEmpty(value)) { - String[] temp = (String[]) object; - value = new Long[temp.length]; - for (int i = 0; i < temp.length; i++) + try + { + result = new Long(value); + } + catch(NumberFormatException e) { - value[i] = Long.valueOf(temp[i]); + logConversionFailure(name, value, "Long"); } } - return value; + return result; + } + + /** + * Return a Long for the given name. If the name does + * not exist, return the default value. + * + * @param name A String with the name. + * @param defaultValue The default value. + * @return A Long. + */ + public Long getLongObject(String name, Long defaultValue) + { + Long result = getLongObject(name); + return (result==null ? defaultValue : result); } /** @@ -733,19 +1060,20 @@ */ public byte getByte(String name, byte defaultValue) { - byte value = defaultValue; - try + byte result = defaultValue; + String value = getString(name); + if (StringUtils.isNotEmpty(value)) { - Object object = parameters.get(convert(name)); - if (object != null) + try { - value = Byte.valueOf(((String[]) object)[0]).byteValue(); + result = Byte.valueOf(value).byteValue(); + } + catch (NumberFormatException e) + { + logConversionFailure(name, value, "Byte"); } } - catch (NumberFormatException exception) - { - } - return value; + return result; } /** @@ -767,14 +1095,57 @@ * * @param name A String with the name. * @return A byte[]. - * @exception UnsupportedEncodingException. + * @exception UnsupportedEncodingException + */ + public byte[] getBytes(String name) + throws UnsupportedEncodingException + { + byte result[] = null; + String value = getString(name); + if (StringUtils.isNotEmpty(value)) + { + result = value.getBytes(getCharacterEncoding()); + } + return result; + } + + /** + * Return a byte for the given name. If the name does not exist, + * return defaultValue. + * + * @param name A String with the name. + * @param defaultValue The default value. + * @return A byte. + */ + public Byte getByteObject(String name, Byte defaultValue) + { + Byte result = getByteObject(name); + return (result==null ? defaultValue : result); + } + + /** + * Return a byte for the given name. If the name does not exist, + * return 0. + * + * @param name A String with the name. + * @return A byte. */ - public byte[] getBytes(String name) throws UnsupportedEncodingException + public Byte getByteObject(String name) { - String tempStr = getString(name); - if (tempStr != null) - return tempStr.getBytes(characterEncoding); - return null; + Byte result = null; + String value = getString(name); + if (StringUtils.isNotEmpty(value)) + { + try + { + result = new Byte(value); + } + catch(NumberFormatException e) + { + logConversionFailure(name, value, "Byte"); + } + } + return result; } /** @@ -786,22 +1157,27 @@ */ public String getString(String name) { + String result = null; try { - String value = null; - Object object = parameters.get(convert(name)); - if (object != null) - value = ((String[]) object)[0]; + Object value = parameters.get(convert(name)); + if (value != null) + { + value = ((String[]) value)[0]; + } if (value == null || value.equals("null")) { return null; } - return value; + result = (String) value; } catch (ClassCastException e) { - return null; + log.fatal("Parameter (" + + name + ") wasn not stored as a String", e); } + + return result; } /** @@ -832,10 +1208,8 @@ public String getString(String name, String defaultValue) { String value = getString(name); - if (value == null || value.length() == 0 || value.equals("null")) - return defaultValue; - else - return value; + + return (StringUtils.isEmpty(value) ? defaultValue : value ); } /** @@ -850,7 +1224,7 @@ { if (value != null) { - parameters.put(convert(name), new String[] { value }); + parameters.put(convert(name), new String[]{value}); } } @@ -863,13 +1237,7 @@ */ public String[] getStrings(String name) { - String[] value = null; - Object object = parameters.get(convert(name)); - if (object != null) - { - value = ((String[]) object); - } - return value; + return (String[]) parameters.get(convert(name)); } /** @@ -883,10 +1251,9 @@ public String[] getStrings(String name, String[] defaultValue) { String[] value = getStrings(name); - if (value == null || value.length == 0) - return defaultValue; - else - return value; + + return (value == null || value.length == 0) + ? defaultValue : value; } /** @@ -914,20 +1281,7 @@ */ public Object getObject(String name) { - try - { - Object value = null; - Object object = parameters.get(convert(name)); - if (object != null) - { - value = ((Object[]) object)[0]; - } - return value; - } - catch (ClassCastException e) - { - return null; - } + return getString(name); } /** @@ -939,14 +1293,7 @@ */ public Object[] getObjects(String name) { - try - { - return (Object[]) parameters.get(convert(name)); - } - catch (ClassCastException e) - { - return null; - } + return getStrings(name); } /** @@ -1037,9 +1384,9 @@ public String toString() { StringBuffer sb = new StringBuffer(); - for (Enumeration e = parameters.keys(); e.hasMoreElements();) + for (Iterator iter = keySet().iterator(); iter.hasNext();) { - String name = (String) e.nextElement(); + String name = (String) iter.next(); try { sb.append('{'); @@ -1058,7 +1405,9 @@ { sb.append(", "); } - sb.append('[').append(params[i]).append(']'); + sb.append('[') + .append(params[i]) + .append(']'); } } sb.append("}\n"); @@ -1090,25 +1439,27 @@ * * @param bean An Object. * @param prop A PropertyDescriptor. - * @exception Exception, a generic exception. + * @exception Exception a generic exception. */ - protected void setProperty(Object bean, PropertyDescriptor prop) - throws Exception + protected void setProperty(Object bean, + PropertyDescriptor prop) + throws Exception { if (prop instanceof IndexedPropertyDescriptor) { - throw new Exception( - prop.getName() + " is an indexed property (not supported)"); + throw new Exception(prop.getName() + + " is an indexed property (not supported)"); } Method setter = prop.getWriteMethod(); if (setter == null) { - throw new Exception(prop.getName() + " is a read only property"); + throw new Exception(prop.getName() + + " is a read only property"); } Class propclass = prop.getPropertyType(); - Object[] args = { null }; + Object[] args = {null}; if (propclass == String.class) { @@ -1116,7 +1467,7 @@ } else if (propclass == Integer.class || propclass == Integer.TYPE) { - args[0] = getInteger(prop.getName()); + args[0] = getIntObject(prop.getName()); } else if (propclass == Long.class || propclass == Long.TYPE) { @@ -1124,7 +1475,7 @@ } else if (propclass == Boolean.class || propclass == Boolean.TYPE) { - args[0] = getBool(prop.getName()); + args[0] = getBooleanObject(prop.getName()); } else if (propclass == Double.class || propclass == Double.TYPE) { @@ -1148,7 +1499,7 @@ } else if (propclass == Integer[].class) { - args[0] = getIntegers(prop.getName()); + args[0] = getIntObjects(prop.getName()); } else if (propclass == Date.class) { @@ -1156,8 +1507,7 @@ } else { - throw new Exception( - "property " + throw new Exception("property " + prop.getName() + " is of unsupported type " + propclass.toString()); @@ -1165,7 +1515,8 @@ setter.invoke(bean, args); } - + + /** recylable supprot **/ /** @@ -1175,9 +1526,6 @@ - - - /** * Checks whether the object is disposed. * @@ -1206,5 +1554,21 @@ return false; } } + + /** + * Writes a log message about a conversion failure. + * + * @param paramName name of the parameter which could not be converted + * @param value value of the parameter + * @param type target data type. + */ + private void logConversionFailure(String paramName, + String value, String type) + { + log.warn("Parameter (" + paramName + + ") with value of (" + + value + ") could not be converted to a " + type); + } + } Modified: jakarta/turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/ValueParser.java URL: http://svn.apache.org/viewcvs/jakarta/turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/ValueParser.java?rev=349543&r1=349542&r2=349543&view=diff ============================================================================== --- jakarta/turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/ValueParser.java (original) +++ jakarta/turbine/fulcrum/trunk/parser/src/java/org/apache/fulcrum/parser/ValueParser.java Mon Nov 28 14:57:22 2005 @@ -22,7 +22,6 @@ import java.math.BigDecimal; import java.text.DateFormat; import java.util.Date; -import java.util.Enumeration; import java.util.Set; /** @@ -84,8 +83,7 @@ * @param name A String with the name. * @param value A double with the value. */ - public void add ( String name, - double value ); + void add(String name, double value); /** * Add a name/value pair into this object. @@ -93,8 +91,7 @@ * @param name A String with the name. * @param value An int with the value. */ - public void add ( String name, - int value ); + void add(String name, int value); /** * Add a name/value pair into this object. @@ -102,8 +99,7 @@ * @param name A String with the name. * @param value An Integer with the value. */ - public void add ( String name, - Integer value ); + void add(String name, Integer value); /** * Add a name/value pair into this object. @@ -111,8 +107,7 @@ * @param name A String with the name. * @param value A long with the value. */ - public void add ( String name, - long value ); + void add(String name, long value); /** * Add a name/value pair into this object. @@ -120,19 +115,27 @@ * @param name A String with the name. * @param value A long with the value. */ - public void add ( String name, - String value ); + void add(String name, String value); /** - * Add a String parameters. If there are any Strings already + * Add a String parameter. If there are any Strings already * associated with the name, append to the array. This is used * for handling parameters from mulitipart POST requests. * * @param name A String with the name. * @param value A String with the value. */ - public void append( String name, - String value ); + void append(String name, String value); + + /** + * Add an array of Strings for a key. This + * is simply adding all the elements in the + * array one by one. + * + * @param name A String with the name. + * @param value A String Array. + */ + void add(String name, String [] value); /** * Removes the named parameter from the contained hashtable. Wraps to the @@ -142,7 +145,7 @@ * @return The value that was mapped to the key (a <code>String[]</code>) * or <code>null</code> if the key was not mapped. */ - public Object remove(String name); + Object remove(String name); /** * Determine whether a given key has been inserted. All keys are @@ -152,7 +155,7 @@ * @param key An Object with the key to search for. * @return True if the object is found. */ - public boolean containsKey( Object key ); + boolean containsKey(Object key); /** * Gets the keys. @@ -161,12 +164,12 @@ */ Set keySet(); - /* + /** * Returns all the available parameter names. * * @return A object array with the keys. */ - public Object[] getKeys(); + Object[] getKeys(); /** * Return a boolean for the given name. If the name does not @@ -176,8 +179,7 @@ * @param defaultValue The default value. * @return A boolean. */ - public boolean getBoolean(String name, - boolean defaultValue); + boolean getBoolean(String name, boolean defaultValue); /** * Return a boolean for the given name. If the name does not @@ -186,27 +188,36 @@ * @param name A String with the name. * @return A boolean. */ - public boolean getBoolean(String name); + boolean getBoolean(String name); /** - * Return a Boolean for the given name. If the name does not - * exist, return defaultValue. + * Returns a Boolean object for the given name. If the parameter + * does not exist or can not be parsed as a boolean, null is returned. + * <p> + * Valid values for true: true, on, 1, yes<br> + * Valid values for false: false, off, 0, no<br> + * <p> + * The string is compared without reguard to case. * * @param name A String with the name. - * @param defaultValue The default value. * @return A Boolean. */ - public Boolean getBool(String name, - boolean defaultValue); + Boolean getBooleanObject(String name); /** - * Return a Boolean for the given name. If the name does not - * exist, return false. + * Returns a Boolean object for the given name. If the parameter + * does not exist or can not be parsed as a boolean, the defaultValue + * is returned. + * <p> + * Valid values for true: true, on, 1, yes<br> + * Valid values for false: false, off, 0, no<br> + * <p> + * The string is compared without reguard to case. * * @param name A String with the name. * @return A Boolean. */ - public Boolean getBool(String name); + Boolean getBooleanObject(String name, Boolean defaultValue); /** * Return a double for the given name. If the name does not @@ -216,8 +227,7 @@ * @param defaultValue The default value. * @return A double. */ - public double getDouble(String name, - double defaultValue); + double getDouble(String name, double defaultValue); /** * Return a double for the given name. If the name does not @@ -226,7 +236,44 @@ * @param name A String with the name. * @return A double. */ - public double getDouble(String name); + double getDouble(String name); + + /** + * Return an array of doubles for the given name. If the name does + * not exist, return null. + * + * @param name A String with the name. + * @return A double[]. + */ + double[] getDoubles(String name); + + /** + * Return a Double for the given name. If the name does not + * exist, return defaultValue. + * + * @param name A String with the name. + * @param defaultValue The default value. + * @return A double. + */ + Double getDoubleObject(String name, Double defaultValue); + + /** + * Return a Double for the given name. If the name does not + * exist, return null. + * + * @param name A String with the name. + * @return A double. + */ + Double getDoubleObject(String name); + + /** + * Return an array of doubles for the given name. If the name does + * not exist, return null. + * + * @param name A String with the name. + * @return A double[]. + */ + Double[] getDoubleObjects(String name); /** * Return a float for the given name. If the name does not @@ -236,8 +283,7 @@ * @param defaultValue The default value. * @return A float. */ - public float getFloat(String name, - float defaultValue); + float getFloat(String name, float defaultValue); /** * Return a float for the given name. If the name does not @@ -246,7 +292,44 @@ * @param name A String with the name. * @return A float. */ - public float getFloat(String name); + float getFloat(String name); + + /** + * Return an array of floats for the given name. If the name does + * not exist, return null. + * + * @param name A String with the name. + * @return A float[]. + */ + float[] getFloats(String name); + + /** + * Return a Float for the given name. If the name does not + * exist, return defaultValue. + * + * @param name A String with the name. + * @param defaultValue The default value. + * @return A Float. + */ + Float getFloatObject(String name, Float defaultValue); + + /** + * Return a float for the given name. If the name does not + * exist, return null. + * + * @param name A String with the name. + * @return A Float. + */ + Float getFloatObject(String name); + + /** + * Return an array of floats for the given name. If the name does + * not exist, return null. + * + * @param name A String with the name. + * @return A float[]. + */ + Float[] getFloatObjects(String name); /** * Return a BigDecimal for the given name. If the name does not @@ -256,17 +339,16 @@ * @param defaultValue The default value. * @return A BigDecimal. */ - public BigDecimal getBigDecimal(String name, - BigDecimal defaultValue); + BigDecimal getBigDecimal(String name, BigDecimal defaultValue); /** * Return a BigDecimal for the given name. If the name does not - * exist, return 0.0. + * exist, return <code>null</code>. * * @param name A String with the name. * @return A BigDecimal. */ - public BigDecimal getBigDecimal(String name); + BigDecimal getBigDecimal(String name); /** * Return an array of BigDecimals for the given name. If the name @@ -275,7 +357,7 @@ * @param name A String with the name. * @return A BigDecimal[]. */ - public BigDecimal[] getBigDecimals(String name); + BigDecimal[] getBigDecimals(String name); /** * Return an int for the given name. If the name does not exist, @@ -285,8 +367,7 @@ * @param defaultValue The default value. * @return An int. */ - public int getInt(String name, - int defaultValue ); + int getInt(String name, int defaultValue); /** * Return an int for the given name. If the name does not exist, @@ -295,39 +376,26 @@ * @param name A String with the name. * @return An int. */ - public int getInt(String name); + int getInt(String name); /** - * Return an Integer for the given name. If the name does not - * exist, return defaultValue. - * - * @param name A String with the name. - * @param defaultValue The default value. - * @return An Integer. - */ - public Integer getInteger(String name, - int defaultValue); - - /** - * Return an Integer for the given name. If the name does not - * exist, return defaultValue. You cannot pass in a null here for - * the default value. + * Return an Integer for the given name. If the name does not exist, + * return defaultValue. * * @param name A String with the name. * @param defaultValue The default value. * @return An Integer. */ - public Integer getInteger(String name, - Integer def); + Integer getIntObject(String name, Integer defaultValue); /** - * Return an Integer for the given name. If the name does not - * exist, return 0. + * Return an Integer for the given name. If the name does not exist, + * return null. * * @param name A String with the name. * @return An Integer. */ - public Integer getInteger(String name); + Integer getIntObject(String name); /** * Return an array of ints for the given name. If the name does @@ -336,7 +404,7 @@ * @param name A String with the name. * @return An int[]. */ - public int[] getInts(String name); + int[] getInts(String name); /** * Return an array of Integers for the given name. If the name @@ -345,7 +413,7 @@ * @param name A String with the name. * @return An Integer[]. */ - public Integer[] getIntegers(String name); + Integer[] getIntObjects(String name); /** * Return a long for the given name. If the name does not exist, @@ -355,8 +423,7 @@ * @param defaultValue The default value. * @return A long. */ - public long getLong(String name, - long defaultValue ); + long getLong(String name, long defaultValue); /** * Return a long for the given name. If the name does not exist, @@ -365,7 +432,26 @@ * @param name A String with the name. * @return A long. */ - public long getLong(String name); + long getLong(String name); + + /** + * Return a Long for the given name. If the name does not exist, + * return defaultValue. + * + * @param name A String with the name. + * @param defaultValue The default value. + * @return A Long. + */ + Long getLongObject(String name, Long defaultValue); + + /** + * Return a Long for the given name. If the name does not exist, + * return null. + * + * @param name A String with the name. + * @return A Long. + */ + Long getLongObject(String name); /** * Return an array of longs for the given name. If the name does @@ -374,7 +460,7 @@ * @param name A String with the name. * @return A long[]. */ - public long[] getLongs(String name); + long[] getLongs(String name); /** * Return an array of Longs for the given name. If the name does @@ -383,7 +469,7 @@ * @param name A String with the name. * @return A Long[]. */ - public Long[] getLongObjects(String name); + Long[] getLongObjects(String name); /** * Return a byte for the given name. If the name does not exist, @@ -393,8 +479,7 @@ * @param defaultValue The default value. * @return A byte. */ - public byte getByte(String name, - byte defaultValue ); + byte getByte(String name, byte defaultValue); /** * Return a byte for the given name. If the name does not exist, @@ -403,7 +488,7 @@ * @param name A String with the name. * @return A byte. */ - public byte getByte(String name); + byte getByte(String name); /** * Return an array of bytes for the given name. If the name does @@ -412,10 +497,28 @@ * * @param name A String with the name. * @return A byte[]. - * @exception UnsupportedEncodingException. + * @exception UnsupportedEncodingException + */ + byte[] getBytes(String name) throws UnsupportedEncodingException; + + /** + * Return a byte for the given name. If the name does not exist, + * return defaultValue. + * + * @param name A String with the name. + * @param defaultValue The default value. + * @return A byte. + */ + Byte getByteObject(String name, Byte defaultValue); + + /** + * Return a byte for the given name. If the name does not exist, + * return 0. + * + * @param name A String with the name. + * @return A byte. */ - public byte[] getBytes(String name) - throws UnsupportedEncodingException; + Byte getByteObject(String name); /** * Return a String for the given name. If the name does not @@ -424,7 +527,7 @@ * @param name A String with the name. * @return A String. */ - public String getString(String name); + String getString(String name); /** * Return a String for the given name. If the name does not @@ -438,7 +541,7 @@ * @param name A String with the name. * @return A String. */ - public String get (String name); + String get(String name); /** * Return a String for the given name. If the name does not @@ -448,8 +551,7 @@ * @param defaultValue The default value. * @return A String. */ - public String getString(String name, - String defaultValue); + String getString(String name, String defaultValue); /** * Set a parameter to a specific value. @@ -459,7 +561,7 @@ * @param name The name of the parameter. * @param value The value to set. */ - public void setString(String name, String value); + void setString(String name, String value); /** * Return an array of Strings for the given name. If the name @@ -468,7 +570,7 @@ * @param name A String with the name. * @return A String[]. */ - public String[] getStrings(String name); + String[] getStrings(String name); /** * Return an array of Strings for the given name. If the name @@ -478,8 +580,7 @@ * @param defaultValue The default value. * @return A String[]. */ - public String[] getStrings(String name, - String[] defaultValue); + String[] getStrings(String name, String[] defaultValue); /** * Set a parameter to a specific value. @@ -489,7 +590,7 @@ * @param name The name of the parameter. * @param values The value to set. */ - public void setStrings(String name, String[] values); + void setStrings(String name, String[] values); /** * Return an Object for the given name. If the name does not @@ -498,7 +599,7 @@ * @param name A String with the name. * @return An Object. */ - public Object getObject(String name); + Object getObject(String name); /** * Return an array of Objects for the given name. If the name @@ -507,7 +608,7 @@ * @param name A String with the name. * @return An Object[]. */ - public Object[] getObjects(String name); + Object[] getObjects(String name); /** * Returns a java.util.Date object. String is parsed by supplied @@ -519,9 +620,7 @@ * @param defaultValue The default value. * @return A Date. */ - public Date getDate(String name, - DateFormat df, - Date defaultValue); + Date getDate(String name, DateFormat df, Date defaultValue); /** * Returns a java.util.Date object. If there are DateSelector @@ -532,7 +631,7 @@ * @param name A String with the name. * @return A Date. */ - public Date getDate(String name); + Date getDate(String name); /** * Returns a java.util.Date object. String is parsed by supplied @@ -542,8 +641,7 @@ * @param df A DateFormat. * @return A Date. */ - public Date getDate(String name, - DateFormat df); + Date getDate(String name, DateFormat df); /** * Uses bean introspection to set writable properties of bean from @@ -551,10 +649,9 @@ * the bean property and the parameter is looked for. * * @param bean An Object. - * @exception Exception, a generic exception. + * @exception Exception a generic exception. */ - public void setProperties(Object bean) - throws Exception; + void setProperties(Object bean) throws Exception; /** * Simple method that attempts to get a toString() representation @@ -562,5 +659,5 @@ * * @return A String. */ - public String toString(); + String toString(); } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]