Example usage for org.apache.commons.lang BooleanUtils toBooleanObject

List of usage examples for org.apache.commons.lang BooleanUtils toBooleanObject

Introduction

In this page you can find the example usage for org.apache.commons.lang BooleanUtils toBooleanObject.

Prototype

public static Boolean toBooleanObject(String str, String trueString, String falseString, String nullString) 

Source Link

Document

Converts a String to a Boolean throwing an exception if no match.

 BooleanUtils.toBooleanObject("true", "true", "false", "null")  = Boolean.TRUE BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE BooleanUtils.toBooleanObject("null", "true", "false", "null")  = null 

Usage

From source file:org.apache.camel.component.docker.DockerHelper.java

/**
 * Validates the URI parameters for a given {@link DockerOperation}
 *
 * @param dockerOperation//from   www  . java  2  s  .c  o m
 * @param parameters
 */
public static void validateParameters(DockerOperation dockerOperation, Map<String, Object> parameters) {
    Map<String, Class<?>> validParamMap = new HashMap<String, Class<?>>();
    validParamMap.putAll(DockerConstants.DOCKER_DEFAULT_PARAMETERS);
    validParamMap.putAll(dockerOperation.getParameters());

    for (String key : parameters.keySet()) {

        String transformedKey = DockerHelper.transformToHeaderName(key);

        // Validate URI parameter name
        if (!validParamMap.containsKey(transformedKey)) {
            throw new DockerClientException(key + " is not a valid URI parameter");
        }

        try {
            Class<?> parameterClass = validParamMap.get(transformedKey);
            Object parameterValue = parameters.get(key);

            if (parameterClass == null || parameterValue == null) {
                throw new DockerClientException("Failed to validate parameter type for property " + key);
            }

            if (Integer.class == parameterClass) {
                Integer.parseInt((String) parameterValue);
            } else if (Boolean.class == parameterClass) {
                BooleanUtils.toBooleanObject((String) parameterValue, "true", "false", "null");
            }
        } catch (Exception e) {
            throw new DockerClientException("Failed to validate parameter type for property " + key);
        }
    }

}

From source file:org.apache.camel.component.docker.DockerHelper.java

/**
 * Attempts to locate a given property name within a URI parameter or the message header.
 * A found value in a message header takes precedence over a URI parameter. Returns a
 * default value if given//from   w  w w . ja  v  a2s . c o  m
 *
 * @param name
 * @param configuration
 * @param message
 * @param clazz
 * @param defaultValue
 * @return
 */
@SuppressWarnings("unchecked")
public static <T> T getProperty(String name, DockerConfiguration configuration, Message message, Class<T> clazz,
        T defaultValue) {
    // First attempt to locate property from Message Header, then fallback to Endpoint property

    if (message != null) {
        T headerProperty = message.getHeader(name, clazz);

        if (headerProperty != null) {
            return headerProperty;
        }
    }

    Object prop = configuration.getParameters().get(transformFromHeaderName(name));

    if (prop != null) {

        if (prop.getClass().isAssignableFrom(clazz)) {
            return (T) prop;
        } else if (Integer.class == clazz) {
            return (T) Integer.valueOf((String) prop);
        } else if (Boolean.class == clazz) {
            return (T) BooleanUtils.toBooleanObject((String) prop, "true", "false", "null");
        }
    } else if (defaultValue != null) {
        return defaultValue;
    }

    return null;

}

From source file:org.apache.cocoon.forms.util.DomHelper.java

public static boolean getAttributeAsBoolean(Element element, String attributeName, boolean defaultValue) {
    String attrValue = element.getAttribute(attributeName);
    Boolean result;/*  w  w w .  jav  a2s . co  m*/
    try {
        result = BooleanUtils.toBooleanObject(attrValue, "true", "false", null);
    } catch (IllegalArgumentException iae) {
        result = null;
    }
    if (result != null) {
        return result.booleanValue();
    }
    try {
        result = BooleanUtils.toBooleanObject(attrValue, "yes", "no", null);
    } catch (IllegalArgumentException iae) {
        result = null;
    }
    if (result != null) {
        return result.booleanValue();
    }
    return defaultValue;
}

From source file:org.apache.hadoop.hive.metastore.MetastoreDirectSqlUtils.java

/**
 * Convert a boolean value returned from the RDBMS to a Java Boolean object.
 * MySQL has booleans, but e.g. Derby uses 'Y'/'N' mapping.
 *
 * @param value//  w ww. j  a va2 s .  co m
 *          column value from the database
 * @return The Boolean value of the database column value, null if the column
 *         value is null
 * @throws MetaException
 *           if the column value cannot be converted into a Boolean object
 */
static Boolean extractSqlBoolean(Object value) throws MetaException {
    if (value == null) {
        return null;
    }
    if (value instanceof Boolean) {
        return (Boolean) value;
    }
    if (value instanceof String) {
        try {
            return BooleanUtils.toBooleanObject((String) value, "Y", "N", null);
        } catch (IllegalArgumentException iae) {
            // NOOP
        }
    }
    throw new MetaException("Cannot extract boolean from column value " + value);
}

From source file:org.ojbc.processor.identificationresults.modification.IdentificationResultsModificationRequestProcessor.java

private SimpleServiceResponse retrieveResponse(Exchange exchange) throws Exception {
    SimpleServiceResponse serviceResponse = new SimpleServiceResponse();
    Document body = OJBUtils.loadXMLFromString((String) exchange.getIn().getBody());
    String status = XmlUtils.xPathStringSearch(body,
            "/irm-resp-doc:IdentificationResultsModificationResponse/irm-resp-ext:IdentificationResultsModificationIndicator");
    serviceResponse.setSuccess(BooleanUtils.toBooleanObject(status, "true", "false", "false"));

    if (!serviceResponse.getSuccess()) {
        String errorMessage = XmlUtils.xPathStringSearch(body,
                "/irm-resp-doc:IdentificationResultsModificationResponse/"
                        + "irm-rm:IdentificationResultsModificationResponseMetadata/"
                        + "irm-err-rep:IdentificationResultsModificationRequestError/irm-err-rep:ErrorText");
        serviceResponse.setErrorMessage(StringUtils.trimToEmpty(errorMessage));
    }//  w ww.  j av  a2 s .  com

    log.debug("Identification Results Modification Response: " + serviceResponse.toString());
    return serviceResponse;
}