Java XML Data Type Converter isValidValue(String value, Class type)

Here you can find the source of isValidValue(String value, Class type)

Description

Given a string value and a Java type, returns true if the given value is a legal assignment to an Attribute having the given Java type.

License

Apache License

Parameter

Parameter Description
value a parameter
type a parameter

Declaration

public static boolean isValidValue(String value, Class<?> type) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.math.BigDecimal;
import java.util.Calendar;

import javax.xml.bind.DatatypeConverter;

public class Main {
    /**//from  ww  w.j a v a 2 s .c  o m
     * Given a string value and a Java type, returns true if the given
     * value is a legal assignment to an Attribute having the given Java type.
     * <code>null</code> or empty values are always legal.
     * @param value
     * @param type
     * @return
     */
    public static boolean isValidValue(String value, Class<?> type) {
        if (null == value || value.length() == 0)
            return true; // missing values are always legal
        if (String.class == type) {
            return true; // string values are always legal (opaque strings)
        } else if (Integer.class == type) {
            try {
                DatatypeConverter.parseInteger(value);
                return true;
            } catch (NumberFormatException e) {
            }
        } else if (Long.class == type) {
            try {
                DatatypeConverter.parseLong(value);
                return true;
            } catch (NumberFormatException e) {
            }
        } else if (Short.class == type) {
            try {
                DatatypeConverter.parseShort(value);
                return true;
            } catch (NumberFormatException e) {
            }
        } else if (BigDecimal.class == type) {
            try {
                DatatypeConverter.parseDecimal(value);
                return true;
            } catch (NumberFormatException e) {
            }
        } else if (Double.class == type) {
            try {
                DatatypeConverter.parseDouble(value);
                return true;
            } catch (NumberFormatException e) {
            }
        } else if (Float.class == type) {
            try {
                DatatypeConverter.parseFloat(value);
                return true;
            } catch (NumberFormatException e) {
            }
        } else if (Calendar.class == type) {
            try {
                DatatypeConverter.parseDateTime(value);
                return true;
            } catch (IllegalArgumentException e) {
                try {
                    DatatypeConverter.parseDate(value);
                    return true;
                } catch (IllegalArgumentException e1) {
                }
            }
        } else if (Boolean.class == type) {
            try {
                DatatypeConverter.parseBoolean(value);
                return true;
            } catch (IllegalArgumentException e) {
            }
        } else {
            throw new IllegalArgumentException("Unsupported type: " + type);
        }
        return false;
    }
}

Related

  1. getImgurContent(String clientID, byte[] image)
  2. getLong(byte[] data)
  3. getText(URL url, String user, String password)
  4. getTextAsJavaByteArrayInitializer( String text, String charsetName, int maxArraySize)
  5. getXMLLong(final long value)
  6. millis2String(long millis)
  7. mixColumns(byte[] input, boolean inv)
  8. printBytes(byte[] data)
  9. printDecimal(BigDecimal value, BigDecimal min, BigDecimal max, int precision, boolean zeroIncluded)