Example usage for java.text ParsePosition getErrorIndex

List of usage examples for java.text ParsePosition getErrorIndex

Introduction

In this page you can find the example usage for java.text ParsePosition getErrorIndex.

Prototype

public int getErrorIndex() 

Source Link

Document

Retrieve the index at which an error occurred, or -1 if the error index has not been set.

Usage

From source file:org.lingcloud.molva.ocl.util.GenericTypeValidator.java

/**
 * Checks if the value can safely be converted to a long primitive.
 * //from  w  w w.j av a2  s.co m
 * @param value
 *            The value validation is being performed on.
 * @param locale
 *            The locale to use to parse the number (system default if null)
 * @return the converted Long value.
 */
public static Long formatLong(String value, Locale locale) {
    Long result = null;

    if (value != null) {
        NumberFormat formatter = null;
        if (locale != null) {
            formatter = NumberFormat.getNumberInstance(locale);
        } else {
            formatter = NumberFormat.getNumberInstance(Locale.getDefault());
        }
        formatter.setParseIntegerOnly(true);
        ParsePosition pos = new ParsePosition(0);
        Number num = formatter.parse(value, pos);

        // If there was no error and we used the whole string
        if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) {
            if (num.doubleValue() >= Long.MIN_VALUE && num.doubleValue() <= Long.MAX_VALUE) {
                result = Long.valueOf(num.longValue());
            }
        }
    }

    return result;
}

From source file:org.mule.modules.clarizen.api.ClarizenDateConverter.java

/**
 * Parse a String into a <code>Calendar</code> object
 * using the specified <code>DateFormat</code>.
 *
 * @param sourceType The type of the value being converted
 * @param targetType The type to convert the value to
 * @param value The String date value.//from ww  w  .  j  av  a  2  s.c  o  m
 * @param format The DateFormat to parse the String value.
 *
 * @return The converted Calendar object.
 * @throws ConversionException if the String cannot be converted.
 */
@SuppressWarnings("rawtypes")
private Calendar parse(Class sourceType, Class targetType, String value, DateFormat format) {
    format.setLenient(false);
    ParsePosition pos = new ParsePosition(0);
    Date parsedDate = format.parse(value, pos); // ignore the result (use the Calendar)
    if (pos.getErrorIndex() >= 0 || pos.getIndex() != value.length() || parsedDate == null) {
        String msg = "Error converting '" + classToString(sourceType) + "' to '" + classToString(targetType)
                + "'";
        if (format instanceof SimpleDateFormat) {
            msg += " using pattern '" + ((SimpleDateFormat) format).toPattern() + "'";
        }
        throw new ConversionException(msg);
    }
    Calendar calendar = format.getCalendar();
    return calendar;
}

From source file:org.pentaho.di.job.entries.zipfile.JobEntryZipFile.java

private int determineDepth(String depthString) throws KettleException {
    DecimalFormat df = new DecimalFormat("0");
    ParsePosition pp = new ParsePosition(0);
    df.setParseIntegerOnly(true);/*from  ww  w.j  a  va2s . c  o  m*/
    try {
        Number n = df.parse(depthString, pp);
        if (n == null) {
            return 1; // default
        }
        if (pp.getErrorIndex() == 0) {
            throw new KettleException("Unable to convert stored depth '" + depthString
                    + "' to depth at position " + pp.getErrorIndex());
        }
        return n.intValue();
    } catch (Exception e) {
        throw new KettleException("Unable to convert stored depth '" + depthString + "' to depth", e);
    }
}

From source file:org.polymap.kaps.ui.MyNumberValidator.java

public Object transform2Model(Object fieldValue) throws Exception {
    if (fieldValue == null) {
        return null;
    } else if (fieldValue instanceof String) {
        if (((String) fieldValue).isEmpty()) {
            return null;
        }/* w  w  w .j av  a  2 s.  c  om*/
        ParsePosition pp = new ParsePosition(0);
        Number result = nf.parse((String) fieldValue, pp);

        if (pp.getErrorIndex() > -1 || pp.getIndex() < ((String) fieldValue).length()) {
            throw new ParseException("field value: " + fieldValue + " for targetClass " + targetClass.getName(),
                    pp.getErrorIndex());
        }

        log.debug("value: " + fieldValue + " -> " + result.doubleValue());

        // XXX check max digits

        if (Float.class.isAssignableFrom(targetClass)) {
            return Float.valueOf(result.floatValue());
        } else if (Double.class.isAssignableFrom(targetClass)) {
            return Double.valueOf(result.doubleValue());
        } else if (Integer.class.isAssignableFrom(targetClass)) {
            return Integer.valueOf(result.intValue());
        } else if (Long.class.isAssignableFrom(targetClass)) {
            return Long.valueOf(result.longValue());
        } else {
            throw new RuntimeException("Unsupported target type: " + targetClass);
        }
    } else {
        throw new RuntimeException("Unhandled field value type: " + fieldValue);
    }
}

From source file:org.polymap.rhei.field.NumberValidator.java

public M transform2Model(String fieldValue) throws Exception {
    if (fieldValue == null) {
        return null;
    } else if (fieldValue instanceof String) {
        ParsePosition pp = new ParsePosition(0);
        Number result = nf.parse((String) fieldValue, pp);

        if (pp.getErrorIndex() > -1 || pp.getIndex() < ((String) fieldValue).length()) {
            throw new ParseException("field value: " + fieldValue, pp.getErrorIndex());
        }//from   www  . j  a v  a2  s  .c  om

        log.debug("value: " + fieldValue + " -> " + result.doubleValue());

        // XXX check max digits

        if (Float.class.isAssignableFrom(targetClass)) {
            return (M) Float.valueOf(result.floatValue());
        } else if (Double.class.isAssignableFrom(targetClass)) {
            return (M) Double.valueOf(result.floatValue());
        } else if (Integer.class.isAssignableFrom(targetClass)) {
            return (M) Integer.valueOf(result.intValue());
        } else if (Long.class.isAssignableFrom(targetClass)) {
            return (M) Long.valueOf(result.longValue());
        } else {
            throw new RuntimeException("Unsupported target type: " + targetClass);
        }
    } else {
        throw new RuntimeException("Unhandled field value type: " + fieldValue);
    }
}