0001: /* ====================================================================
0002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
0003: *
0004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
0005: *
0006: * Redistribution and use in source and binary forms, with or without
0007: * modification, are permitted provided that the following conditions
0008: * are met:
0009: *
0010: * 1. Redistributions of source code must retain the above copyright
0011: * notice, this list of conditions and the following disclaimer.
0012: *
0013: * 2. Redistributions in binary form must reproduce the above copyright
0014: * notice, this list of conditions and the following disclaimer in
0015: * the documentation and/or other materials provided with the
0016: * distribution.
0017: *
0018: * 3. The end-user documentation included with the redistribution,
0019: * if any, must include the following acknowledgment:
0020: * "This product includes software developed by Jcorporate Ltd.
0021: * (http://www.jcorporate.com/)."
0022: * Alternately, this acknowledgment may appear in the software itself,
0023: * if and wherever such third-party acknowledgments normally appear.
0024: *
0025: * 4. "Jcorporate" and product names such as "Expresso" must
0026: * not be used to endorse or promote products derived from this
0027: * software without prior written permission. For written permission,
0028: * please contact info@jcorporate.com.
0029: *
0030: * 5. Products derived from this software may not be called "Expresso",
0031: * or other Jcorporate product names; nor may "Expresso" or other
0032: * Jcorporate product names appear in their name, without prior
0033: * written permission of Jcorporate Ltd.
0034: *
0035: * 6. No product derived from this software may compete in the same
0036: * market space, i.e. framework, without prior written permission
0037: * of Jcorporate Ltd. For written permission, please contact
0038: * partners@jcorporate.com.
0039: *
0040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
0041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
0043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
0044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
0046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
0047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
0048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
0049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
0050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0051: * SUCH DAMAGE.
0052: * ====================================================================
0053: *
0054: * This software consists of voluntary contributions made by many
0055: * individuals on behalf of the Jcorporate Ltd. Contributions back
0056: * to the project(s) are encouraged when you make modifications.
0057: * Please send them to support@jcorporate.com. For more information
0058: * on Jcorporate Ltd. and its products, please see
0059: * <http://www.jcorporate.com/>.
0060: *
0061: * Portions of this software are based upon other open source
0062: * products and are subject to their respective licenses.
0063: */
0064:
0065: package com.jcorporate.expresso.core.misc.upload;
0066:
0067: import java.beans.IndexedPropertyDescriptor;
0068: import java.beans.Introspector;
0069: import java.beans.PropertyDescriptor;
0070: import java.io.UnsupportedEncodingException;
0071: import java.lang.reflect.Method;
0072: import java.math.BigDecimal;
0073: import java.util.Enumeration;
0074: import java.util.Hashtable;
0075:
0076: // Turbine stuff
0077: //import org.apache.turbine.util.DateSelector;
0078:
0079: /**
0080: * BaseValueParser is a base class for classes that need to parse
0081: * name/value Parameters, for example GET/POST data or Cookies
0082: * (DefaultParameterParser and DefaultCookieParser)
0083: * <p/>
0084: * <p>It can also be used standalone, for an example see DataStreamParser.
0085: * <p/>
0086: * <p>NOTE: The name= portion of a name=value pair may be converted
0087: * to lowercase or uppercase when the object is initialized and when
0088: * new data is added. This behaviour is determined by the url.case.folding
0089: * property in TurbineResources.properties. Adding a name/value pair may
0090: * overwrite existing name=value pairs if the names match:
0091: * <p/>
0092: * <pre>
0093: * ValueParser vp = new BaseValueParser();
0094: * vp.add("ERROR",1);
0095: * vp.add("eRrOr",2);
0096: * int result = vp.getInt("ERROR");
0097: * </pre>
0098: * <p/>
0099: * In the above example, result is 2.
0100: *
0101: * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
0102: * @author <a href="mailto:jon@clearink.com">Jon S. Stevens</a>
0103: * @author <a href="mailto:sean@informage.net">Sean Legassick</a>
0104: * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
0105: * @version $Id: BaseValueParser.java,v 1.7 2004/11/18 02:03:27 lhamel Exp $
0106: */
0107: public class BaseValueParser implements ValueParser {
0108:
0109: /**
0110: * Random access storage for parameter data.
0111: */
0112: protected Hashtable parameters = new Hashtable();
0113:
0114: /**
0115: * The character encoding to use when converting to byte arrays
0116: */
0117: private String characterEncoding = "US-ASCII";
0118:
0119: /**
0120: * A static version of the convert method, which
0121: * trims the string data and applies the conversion specified in
0122: * the property given by URL_CASE_FOLDING. It returns a new
0123: * string so that it does not destroy the value data.
0124: *
0125: * @param value A String to be processed.
0126: * @return A new String converted to lowercase and trimmed.
0127: */
0128: public static String convertAndTrim(String value) {
0129: return value.trim();
0130: }
0131:
0132: /**
0133: * Default constructor
0134: */
0135: public BaseValueParser() {
0136: super ();
0137: }
0138:
0139: /**
0140: * Constructor that takes a character encoding
0141: */
0142: public BaseValueParser(String newEncoding) {
0143: super ();
0144: characterEncoding = newEncoding;
0145: }
0146:
0147: /**
0148: * Disposes the parser.
0149: */
0150: public void dispose() {
0151: clear();
0152: }
0153:
0154: /**
0155: * Clear all name/value pairs out of this object.
0156: */
0157: public void clear() {
0158: parameters.clear();
0159: }
0160:
0161: /**
0162: * Set the character encoding that will be used by this ValueParser.
0163: */
0164: public void setCharacterEncoding(String s) {
0165: characterEncoding = s;
0166: }
0167:
0168: /**
0169: * Get the character encoding that will be used by this ValueParser.
0170: */
0171: public String getCharacterEncoding() {
0172: return characterEncoding;
0173: }
0174:
0175: /**
0176: * Add a name/value pair into this object.
0177: *
0178: * @param name A String with the name.
0179: * @param value A double with the value.
0180: */
0181: public void add(String name, double value) {
0182: add(name, Double.toString(value));
0183: }
0184:
0185: /**
0186: * Add a name/value pair into this object.
0187: *
0188: * @param name A String with the name.
0189: * @param value An int with the value.
0190: */
0191: public void add(String name, int value) {
0192: add(name, Integer.toString(value));
0193: }
0194:
0195: /**
0196: * Add a name/value pair into this object.
0197: *
0198: * @param name A String with the name.
0199: * @param value An Integer with the value.
0200: */
0201: public void add(String name, Integer value) {
0202: add(name, value.toString());
0203: }
0204:
0205: /**
0206: * Add a name/value pair into this object.
0207: *
0208: * @param name A String with the name.
0209: * @param value A long with the value.
0210: */
0211: public void add(String name, long value) {
0212: add(name, Long.toString(value));
0213: }
0214:
0215: /**
0216: * Add a name/value pair into this object.
0217: *
0218: * @param name A String with the name.
0219: * @param value A long with the value.
0220: */
0221: public void add(String name, String value) {
0222: append(name, value);
0223: }
0224:
0225: /**
0226: * Add a String parameters. If there are any Strings already
0227: * associated with the name, append to the array. This is used
0228: * for handling parameters from mulitipart POST requests.
0229: *
0230: * @param name A String with the name.
0231: * @param value A String with the value.
0232: */
0233: public void append(String name, String value) {
0234: String[] items = this .getStrings(name);
0235:
0236: if (items == null) {
0237: items = new String[1];
0238: items[0] = value;
0239: parameters.put(convert(name), items);
0240: } else {
0241: String[] newItems = new String[items.length + 1];
0242: System.arraycopy(items, 0, newItems, 0, items.length);
0243: newItems[items.length] = value;
0244: parameters.put(convert(name), newItems);
0245: }
0246: }
0247:
0248: /**
0249: * Removes the named parameter from the contained hashtable. Wraps to the
0250: * contained <code>Hashtable.remove()</code>.
0251: *
0252: * @return The value that was mapped to the key (a <code>String[]</code>)
0253: * or <code>null</code> if the key was not mapped.
0254: */
0255: public Object remove(String name) {
0256: return parameters.remove(convert(name));
0257: }
0258:
0259: /**
0260: * Trims the string data and applies the conversion specified in
0261: * the property given by URL_CASE_FOLDING. It returns a new
0262: * string so that it does not destroy the value data.
0263: *
0264: * @param value A String to be processed.
0265: * @return A new String converted to lowercase and trimmed.
0266: */
0267: public String convert(String value) {
0268: return convertAndTrim(value);
0269: }
0270:
0271: /**
0272: * Determine whether a given key has been inserted. All keys are
0273: * stored in lowercase strings, so override method to account for
0274: * this.
0275: *
0276: * @param key An Object with the key to search for.
0277: * @return True if the object is found.
0278: */
0279: public boolean containsKey(Object key) {
0280: return parameters.containsKey(convert((String) key));
0281: }
0282:
0283: /*
0284:
0285: * Get an enumerator for the parameter keys. Wraps to the
0286:
0287: * contained <code>Hashtable.keys()</code>.
0288:
0289: *
0290:
0291: * @return An <code>enumerator</code> of the keys.
0292:
0293: */
0294: public Enumeration keys() {
0295: return parameters.keys();
0296: }
0297:
0298: /*
0299:
0300: * Returns all the available parameter names.
0301:
0302: *
0303:
0304: * @return A object array with the keys.
0305:
0306: */
0307: public Object[] getKeys() {
0308: return parameters.keySet().toArray();
0309: }
0310:
0311: /**
0312: * Return a boolean for the given name. If the name does not
0313: * exist, return defaultValue.
0314: *
0315: * @param name A String with the name.
0316: * @param defaultValue The default value.
0317: * @return A boolean.
0318: */
0319: public boolean getBoolean(String name, boolean defaultValue) {
0320: boolean value = defaultValue;
0321: Object object = parameters.get(convert(name));
0322:
0323: if (object != null) {
0324: String tmp = getString(name);
0325:
0326: if (tmp.equalsIgnoreCase("1")
0327: || tmp.equalsIgnoreCase("true")
0328: || tmp.equalsIgnoreCase("on")) {
0329: value = true;
0330: }
0331: if (tmp.equalsIgnoreCase("0")
0332: || tmp.equalsIgnoreCase("false")) {
0333: value = false;
0334: }
0335: }
0336:
0337: return value;
0338: }
0339:
0340: /**
0341: * Return a boolean for the given name. If the name does not
0342: * exist, return false.
0343: *
0344: * @param name A String with the name.
0345: * @return A boolean.
0346: */
0347: public boolean getBoolean(String name) {
0348: return getBoolean(name, false);
0349: }
0350:
0351: /**
0352: * Return a Boolean for the given name. If the name does not
0353: * exist, return defaultValue.
0354: *
0355: * @param name A String with the name.
0356: * @param defaultValue The default value.
0357: * @return A Boolean.
0358: */
0359: public Boolean getBool(String name, boolean defaultValue) {
0360: if (getBoolean(name, defaultValue)) {
0361: return Boolean.TRUE;
0362: } else {
0363: return Boolean.FALSE;
0364: }
0365: }
0366:
0367: /**
0368: * Return a Boolean for the given name. If the name does not
0369: * exist, return false.
0370: *
0371: * @param name A String with the name.
0372: * @return A Boolean.
0373: */
0374: public Boolean getBool(String name) {
0375: return getBool(name, false);
0376: }
0377:
0378: /**
0379: * Return a double for the given name. If the name does not
0380: * exist, return defaultValue.
0381: *
0382: * @param name A String with the name.
0383: * @param defaultValue The default value.
0384: * @return A double.
0385: */
0386: public double getDouble(String name, double defaultValue) {
0387: double value = defaultValue;
0388:
0389: try {
0390: Object object = parameters.get(convert(name));
0391:
0392: if (object != null) {
0393: value = Double.valueOf(((String[]) object)[0])
0394: .doubleValue();
0395: }
0396: } catch (NumberFormatException exception) {
0397: }
0398:
0399: return value;
0400: }
0401:
0402: /**
0403: * Return a double for the given name. If the name does not
0404: * exist, return 0.0.
0405: *
0406: * @param name A String with the name.
0407: * @return A double.
0408: */
0409: public double getDouble(String name) {
0410: return getDouble(name, 0.0);
0411: }
0412:
0413: /**
0414: * Return a float for the given name. If the name does not
0415: * exist, return defaultValue.
0416: *
0417: * @param name A String with the name.
0418: * @param defaultValue The default value.
0419: * @return A float.
0420: */
0421: public float getFloat(String name, float defaultValue) {
0422: float value = defaultValue;
0423:
0424: try {
0425: Object object = parameters.get(convert(name));
0426:
0427: if (object != null) {
0428: value = Float.valueOf(((String[]) object)[0])
0429: .floatValue();
0430: }
0431: } catch (NumberFormatException exception) {
0432: }
0433:
0434: return value;
0435: }
0436:
0437: /**
0438: * Return a float for the given name. If the name does not
0439: * exist, return 0.0.
0440: *
0441: * @param name A String with the name.
0442: * @return A float.
0443: */
0444: public float getFloat(String name) {
0445: return getFloat(name, 0.0f);
0446: }
0447:
0448: /**
0449: * Return a BigDecimal for the given name. If the name does not
0450: * exist, return 0.0.
0451: *
0452: * @param name A String with the name.
0453: * @param defaultValue The default value.
0454: * @return A BigDecimal.
0455: */
0456: public BigDecimal getBigDecimal(String name, BigDecimal defaultValue) {
0457: BigDecimal value = defaultValue;
0458:
0459: try {
0460: Object object = parameters.get(convert(name));
0461:
0462: if (object != null) {
0463: value = new BigDecimal(((String[]) object)[0]);
0464: }
0465: } catch (NumberFormatException exception) {
0466: }
0467:
0468: return value;
0469: }
0470:
0471: /**
0472: * Return a BigDecimal for the given name. If the name does not
0473: * exist, return 0.0.
0474: *
0475: * @param name A String with the name.
0476: * @return A BigDecimal.
0477: */
0478: public BigDecimal getBigDecimal(String name) {
0479: return getBigDecimal(name, new BigDecimal(0.0));
0480: }
0481:
0482: /**
0483: * Return an array of BigDecimals for the given name. If the name
0484: * does not exist, return null.
0485: *
0486: * @param name A String with the name.
0487: * @return A BigDecimal[].
0488: */
0489: public BigDecimal[] getBigDecimals(String name) {
0490: BigDecimal[] value = null;
0491: Object object = getStrings(convert(name));
0492:
0493: if (object != null) {
0494: String[] temp = (String[]) object;
0495: value = new BigDecimal[temp.length];
0496:
0497: for (int i = 0; i < temp.length; i++) {
0498: value[i] = new BigDecimal(temp[i]);
0499: }
0500: }
0501:
0502: return value;
0503: }
0504:
0505: /**
0506: * Return an int for the given name. If the name does not exist,
0507: * return defaultValue.
0508: *
0509: * @param name A String with the name.
0510: * @param defaultValue The default value.
0511: * @return An int.
0512: */
0513: public int getInt(String name, int defaultValue) {
0514: int value = defaultValue;
0515:
0516: try {
0517: Object object = parameters.get(convert(name));
0518:
0519: if (object != null) {
0520: value = Integer.valueOf(((String[]) object)[0])
0521: .intValue();
0522: }
0523: } catch (NumberFormatException exception) {
0524: }
0525:
0526: return value;
0527: }
0528:
0529: /**
0530: * Return an int for the given name. If the name does not exist,
0531: * return 0.
0532: *
0533: * @param name A String with the name.
0534: * @return An int.
0535: */
0536: public int getInt(String name) {
0537: return getInt(name, 0);
0538: }
0539:
0540: /**
0541: * Return an Integer for the given name. If the name does not
0542: * exist, return defaultValue.
0543: *
0544: * @param name A String with the name.
0545: * @param defaultValue The default value.
0546: * @return An Integer.
0547: */
0548: public Integer getInteger(String name, int defaultValue) {
0549: return new Integer(getInt(name, defaultValue));
0550: }
0551:
0552: /**
0553: * Return an Integer for the given name. If the name does not
0554: * exist, return defaultValue. You cannot pass in a null here for
0555: * the default value.
0556: *
0557: * @param name A String with the name.
0558: * @param defaultValue The default value.
0559: * @return An Integer.
0560: */
0561: public Integer getInteger(String name, Integer def) {
0562: return new Integer(getInt(name, def.intValue()));
0563: }
0564:
0565: /**
0566: * Return an Integer for the given name. If the name does not
0567: * exist, return 0.
0568: *
0569: * @param name A String with the name.
0570: * @return An Integer.
0571: */
0572: public Integer getInteger(String name) {
0573: return new Integer(getInt(name, 0));
0574: }
0575:
0576: /**
0577: * Return an array of ints for the given name. If the name does
0578: * not exist, return null.
0579: *
0580: * @param name A String with the name.
0581: * @return An int[].
0582: */
0583: public int[] getInts(String name) {
0584: int[] value = null;
0585: Object object = getStrings(convert(name));
0586:
0587: if (object != null) {
0588: String[] temp = (String[]) object;
0589: value = new int[temp.length];
0590:
0591: for (int i = 0; i < temp.length; i++) {
0592: value[i] = Integer.parseInt(temp[i]);
0593: }
0594: }
0595:
0596: return value;
0597: }
0598:
0599: /**
0600: * Return an array of Integers for the given name. If the name
0601: * does not exist, return null.
0602: *
0603: * @param name A String with the name.
0604: * @return An Integer[].
0605: */
0606: public Integer[] getIntegers(String name) {
0607: Integer[] value = null;
0608: Object object = getStrings(convert(name));
0609:
0610: if (object != null) {
0611: String[] temp = (String[]) object;
0612: value = new Integer[temp.length];
0613:
0614: for (int i = 0; i < temp.length; i++) {
0615: value[i] = Integer.valueOf(temp[i]);
0616: }
0617: }
0618:
0619: return value;
0620: }
0621:
0622: /**
0623: * Return a long for the given name. If the name does not exist,
0624: * return defaultValue.
0625: *
0626: * @param name A String with the name.
0627: * @param defaultValue The default value.
0628: * @return A long.
0629: */
0630: public long getLong(String name, long defaultValue) {
0631: long value = defaultValue;
0632:
0633: try {
0634: Object object = parameters.get(convert(name));
0635:
0636: if (object != null) {
0637: value = Long.valueOf(((String[]) object)[0])
0638: .longValue();
0639: }
0640: } catch (NumberFormatException exception) {
0641: }
0642:
0643: return value;
0644: }
0645:
0646: /**
0647: * Return a long for the given name. If the name does not exist,
0648: * return 0.
0649: *
0650: * @param name A String with the name.
0651: * @return A long.
0652: */
0653: public long getLong(String name) {
0654: return getLong(name, 0);
0655: }
0656:
0657: /**
0658: * Return an array of longs for the given name. If the name does
0659: * not exist, return null.
0660: *
0661: * @param name A String with the name.
0662: * @return A long[].
0663: */
0664: public long[] getLongs(String name) {
0665: long[] value = null;
0666: Object object = getStrings(convert(name));
0667:
0668: if (object != null) {
0669: String[] temp = (String[]) object;
0670: value = new long[temp.length];
0671:
0672: for (int i = 0; i < temp.length; i++) {
0673: value[i] = Long.parseLong(temp[i]);
0674: }
0675: }
0676:
0677: return value;
0678: }
0679:
0680: /**
0681: * Return an array of Longs for the given name. If the name does
0682: * not exist, return null.
0683: *
0684: * @param name A String with the name.
0685: * @return A Long[].
0686: */
0687: public Long[] getLongObjects(String name) {
0688: Long[] value = null;
0689: Object object = getStrings(convert(name));
0690:
0691: if (object != null) {
0692: String[] temp = (String[]) object;
0693: value = new Long[temp.length];
0694:
0695: for (int i = 0; i < temp.length; i++) {
0696: value[i] = Long.valueOf(temp[i]);
0697: }
0698: }
0699:
0700: return value;
0701: }
0702:
0703: /**
0704: * Return a byte for the given name. If the name does not exist,
0705: * return defaultValue.
0706: *
0707: * @param name A String with the name.
0708: * @param defaultValue The default value.
0709: * @return A byte.
0710: */
0711: public byte getByte(String name, byte defaultValue) {
0712: byte value = defaultValue;
0713:
0714: try {
0715: Object object = parameters.get(convert(name));
0716:
0717: if (object != null) {
0718: value = Byte.valueOf(((String[]) object)[0])
0719: .byteValue();
0720: }
0721: } catch (NumberFormatException exception) {
0722: }
0723:
0724: return value;
0725: }
0726:
0727: /**
0728: * Return a byte for the given name. If the name does not exist,
0729: * return 0.
0730: *
0731: * @param name A String with the name.
0732: * @return A byte.
0733: */
0734: public byte getByte(String name) {
0735: return getByte(name, (byte) 0);
0736: }
0737:
0738: /**
0739: * Return an array of bytes for the given name. If the name does
0740: * not exist, return null. The array is returned according to the
0741: * HttpRequest's character encoding.
0742: *
0743: * @param name A String with the name.
0744: * @return A byte[].
0745: */
0746: public byte[] getBytes(String name)
0747: throws UnsupportedEncodingException {
0748: String tempStr = getString(name);
0749:
0750: if (tempStr != null) {
0751: return tempStr.getBytes(characterEncoding);
0752: }
0753:
0754: return null;
0755: }
0756:
0757: /**
0758: * Return a String for the given name. If the name does not
0759: * exist, return null.
0760: *
0761: * @param name A String with the name.
0762: * @return A String.
0763: */
0764: public String getString(String name) {
0765: try {
0766: String value = null;
0767: Object object = parameters.get(convert(name));
0768:
0769: if (object != null) {
0770: value = ((String[]) object)[0];
0771: }
0772: if (value == null || value.equals("null")) {
0773: return null;
0774: }
0775:
0776: return value;
0777: } catch (ClassCastException e) {
0778: return null;
0779: }
0780: }
0781:
0782: /**
0783: * Return a String for the given name. If the name does not
0784: * exist, return null. It is the same as the getString() method
0785: * however has been added for simplicity when working with
0786: * template tools such as Velocity which allow you to do
0787: * something like this:
0788: * <p/>
0789: * <code>$data.Parameters.form_variable_name</code>
0790: *
0791: * @param name A String with the name.
0792: * @return A String.
0793: */
0794: public String get(String name) {
0795: return getString(name);
0796: }
0797:
0798: /**
0799: * Return a String for the given name. If the name does not
0800: * exist, return the defaultValue.
0801: *
0802: * @param name A String with the name.
0803: * @param defaultValue The default value.
0804: * @return A String.
0805: */
0806: public String getString(String name, String defaultValue) {
0807: String value = getString(name);
0808:
0809: if (value == null || value.length() == 0
0810: || value.equals("null")) {
0811: return defaultValue;
0812: } else {
0813: return value;
0814: }
0815: }
0816:
0817: /**
0818: * Set a parameter to a specific value.
0819: * <p/>
0820: * This is useful if you want your action to override the values
0821: * of the parameters for the screen to use.
0822: *
0823: * @param name The name of the parameter.
0824: * @param value The value to set.
0825: */
0826: public void setString(String name, String value) {
0827: if (value != null) {
0828: parameters.put(convert(name), new String[] { value });
0829: }
0830: }
0831:
0832: /**
0833: * Return an array of Strings for the given name. If the name
0834: * does not exist, return null.
0835: *
0836: * @param name A String with the name.
0837: * @return A String[].
0838: */
0839: public String[] getStrings(String name) {
0840: String[] value = null;
0841: Object object = parameters.get(convert(name));
0842:
0843: if (object != null) {
0844: if (object instanceof String[]) {
0845: value = (String[]) object;
0846: }
0847: }
0848:
0849: return value;
0850: }
0851:
0852: /**
0853: * Return an array of Strings for the given name. If the name
0854: * does not exist, return the defaultValue.
0855: *
0856: * @param name A String with the name.
0857: * @param defaultValue The default value.
0858: * @return A String[].
0859: */
0860: public String[] getStrings(String name, String[] defaultValue) {
0861: String[] value = getStrings(name);
0862:
0863: if (value == null || value.length == 0) {
0864: return defaultValue;
0865: } else {
0866: return value;
0867: }
0868: }
0869:
0870: /**
0871: * Set a parameter to a specific value.
0872: * <p/>
0873: * This is useful if you want your action to override the values
0874: * of the parameters for the screen to use.
0875: *
0876: * @param name The name of the parameter.
0877: * @param values The value to set.
0878: */
0879: public void setStrings(String name, String[] values) {
0880: if (values != null) {
0881: parameters.put(convert(name), values);
0882: }
0883: }
0884:
0885: /**
0886: * Return an Object for the given name. If the name does not
0887: * exist, return null.
0888: *
0889: * @param name A String with the name.
0890: * @return An Object.
0891: */
0892: public Object getObject(String name) {
0893: try {
0894: Object value = null;
0895: Object object = parameters.get(convert(name));
0896:
0897: if (object != null) {
0898: value = ((Object[]) object)[0];
0899: }
0900:
0901: return value;
0902: } catch (ClassCastException e) {
0903: return null;
0904: }
0905: }
0906:
0907: /**
0908: * Return an array of Objects for the given name. If the name
0909: * does not exist, return null.
0910: *
0911: * @param name A String with the name.
0912: * @return An Object[].
0913: */
0914: public Object[] getObjects(String name) {
0915: try {
0916: return (Object[]) parameters.get(convert(name));
0917: } catch (ClassCastException e) {
0918: return null;
0919: }
0920: }
0921:
0922: /**
0923: * Uses bean introspection to set writable properties of bean from
0924: * the parameters, where a (case-insensitive) name match between
0925: * the bean property and the parameter is looked for.
0926: *
0927: * @param bean An Object.
0928: * @throws Exception, a generic exception.
0929: */
0930: public void setProperties(Object bean) throws Exception {
0931: Class beanClass = bean.getClass();
0932: PropertyDescriptor[] props = Introspector
0933: .getBeanInfo(beanClass).getPropertyDescriptors();
0934:
0935: for (int i = 0; i < props.length; i++) {
0936: String propname = props[i].getName();
0937: Method setter = props[i].getWriteMethod();
0938:
0939: if (setter != null && (containsKey(propname))) {
0940: setProperty(bean, props[i]);
0941: }
0942: }
0943: }
0944:
0945: /**
0946: * Simple method that attempts to get a toString() representation
0947: * of this object. It doesn't do well with String[]'s though.
0948: *
0949: * @return A String.
0950: */
0951: public String toString() {
0952: StringBuffer sb = new StringBuffer();
0953:
0954: for (Enumeration e = parameters.keys(); e.hasMoreElements();) {
0955: String name = (String) e.nextElement();
0956:
0957: try {
0958: sb.append("{");
0959: sb.append(name);
0960: sb.append("=");
0961:
0962: String[] params = this .getStrings(name);
0963:
0964: if (params.length <= 1) {
0965: sb.append(params[0]);
0966: } else {
0967: for (int i = 0; i < params.length; i++) {
0968: if (i != 0) {
0969: sb.append(", ");
0970: }
0971:
0972: sb.append('[').append(params[i]).append(']');
0973: }
0974: }
0975:
0976: sb.append("}\n");
0977: } catch (Exception ee) {
0978: try {
0979: sb.append("{");
0980: sb.append(name);
0981: sb.append("=");
0982: sb.append("ERROR?");
0983: sb.append("}\n");
0984: } catch (Exception eee) {
0985: }
0986: }
0987: }
0988:
0989: return sb.toString();
0990: }
0991:
0992: /**
0993: * Set the property 'prop' in the bean to the value of the
0994: * corresponding parameters. Supports all types supported by
0995: * getXXX methods plus a few more that come for free because
0996: * primitives have to be wrapped before being passed to invoke
0997: * anyway.
0998: *
0999: * @param bean An Object.
1000: * @param prop A PropertyDescriptor.
1001: * @throws Exception, a generic exception.
1002: */
1003: private void setProperty(Object bean, PropertyDescriptor prop)
1004: throws Exception {
1005: if (prop instanceof IndexedPropertyDescriptor) {
1006: throw new Exception(prop.getName()
1007: + " is an indexed property (not supported)");
1008: }
1009:
1010: Method setter = prop.getWriteMethod();
1011:
1012: if (setter == null) {
1013: throw new Exception(prop.getName()
1014: + " is a read only property");
1015: }
1016:
1017: Class propclass = prop.getPropertyType();
1018: Object[] args = { null };
1019:
1020: if (propclass == String.class) {
1021: args[0] = getString(prop.getName());
1022: } else if (propclass == Integer.class
1023: || propclass == Integer.TYPE) {
1024: args[0] = getInteger(prop.getName());
1025: } else if (propclass == Long.class || propclass == Long.TYPE) {
1026: args[0] = new Long(getLong(prop.getName()));
1027: } else if (propclass == Boolean.class
1028: || propclass == Boolean.TYPE) {
1029: args[0] = getBool(prop.getName());
1030: } else if (propclass == Double.class
1031: || propclass == Double.TYPE) {
1032: args[0] = new Double(getDouble(prop.getName()));
1033: } else if (propclass == String[].class) {
1034: args[0] = getStrings(prop.getName());
1035: } else if (propclass == Object.class) {
1036: args[0] = getObject(prop.getName());
1037: } else if (propclass == int[].class) {
1038: args[0] = getInts(prop.getName());
1039: } else if (propclass == Integer[].class) {
1040: args[0] = getIntegers(prop.getName());
1041: } else {
1042: throw new Exception("property " + prop.getName()
1043: + " is of unsupported type " + propclass.toString());
1044: }
1045:
1046: setter.invoke(bean, args);
1047: }
1048: }
|