001: // Released under the Canoo Webtest license.
002: package com.canoo.webtest.util;
003:
004: import com.canoo.webtest.engine.StepExecutionException;
005: import com.canoo.webtest.steps.Step;
006: import org.apache.commons.lang.StringUtils;
007:
008: /**
009: * Date: May 4, 2005
010: * @author paulk
011: */
012: public class ConversionUtil {
013: /**
014: * Convert a string value to a boolean according to ANT defintion of true. If the value is null or empty, return the
015: * specified default value.
016: *
017: * @return parsed value. If value is null or empty, return default value.
018: */
019: public static boolean convertToBoolean(String value,
020: boolean defaultValue) {
021: if (StringUtils.isNotEmpty(value)) { // isNotBlank?
022: return "yes".equalsIgnoreCase(value)
023: || "true".equalsIgnoreCase(value)
024: || "on".equalsIgnoreCase(value);
025: }
026: return defaultValue;
027: }
028:
029: /**
030: * Convert a string value to an integer. If the value is null, return the specified default value.
031: *
032: * @return parsed value. If value is null return default value.
033: * @throws NumberFormatException
034: */
035: public static int convertToInt(String value, int defaultValue) {
036: if (value != null) {
037: return Integer.parseInt(value);
038: }
039: return defaultValue;
040: }
041:
042: /**
043: * Convert a string value to a long. If the value is null, return the specified default value.
044: *
045: * @return parsed value. If value is null return default value.
046: * @throws NumberFormatException
047: */
048: public static long convertToLong(String value, long defaultValue) {
049: if (value != null) {
050: return Long.parseLong(value);
051: }
052: return defaultValue;
053: }
054:
055: /**
056: * Convert a string value to a float. If the value is null, return the specified default value.
057: *
058: * @return parsed value. If value is null return default value.
059: * @throws NumberFormatException
060: */
061: public static float convertToFloat(String value, float defaultValue) {
062: if (value != null) {
063: return Float.parseFloat(value);
064: }
065: return defaultValue;
066: }
067:
068: /**
069: * Convert a string value to a double. If the value is null, return the specified default value.
070: *
071: * @return parsed value. If value is null return default value.
072: * @throws NumberFormatException
073: */
074: public static double convertToDouble(String value,
075: double defaultValue) {
076: if (value != null) {
077: return Double.parseDouble(value);
078: }
079: return defaultValue;
080: }
081:
082: /**
083: * Parses a value as integer.
084: * @param property the name of the property (used in the exception's message if parse fails)
085: * @param value the value to parse
086: * @return the parsed int
087: * @throws com.canoo.webtest.engine.StepExecutionException if the value can't be parsed as int
088: */
089: public static int convertToIntOrReject(final String property,
090: final String value, Step step)
091: throws StepExecutionException {
092: try {
093: return Integer.parseInt(value);
094: } catch (final NumberFormatException nfe) {
095: throw new StepExecutionException("Can't parse \"" + value
096: + "\" as an int for property \"" + property + "\"",
097: step);
098: }
099: }
100: }
|