Example usage for java.text ParsePosition getIndex

List of usage examples for java.text ParsePosition getIndex

Introduction

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

Prototype

public int getIndex() 

Source Link

Document

Retrieve the current parse position.

Usage

From source file:com.haulmont.chile.core.datatypes.impl.NumberDatatype.java

protected Number parse(String value, NumberFormat format) throws ParseException {
    ParsePosition pos = new ParsePosition(0);
    Number res = format.parse(value.trim(), pos);
    if (pos.getIndex() != value.length()) {
        throw new ParseException(String.format("Unparseable number: \"%s\"", value), pos.getErrorIndex());
    }/*w w  w.  j a v  a2 s  . c om*/
    return res;
}

From source file:com.anrisoftware.globalpom.format.enums.EnumFormat.java

/**
 * @see #parseObject(String, ParsePosition)
 *///w w  w  .  ja v  a  2s.co m
public EnumType parse(String source, ParsePosition pos) {
    try {
        source = source.substring(pos.getIndex()).toUpperCase();
        @SuppressWarnings("unchecked")
        EnumType item = (EnumType) Enum.valueOf(enumType, source);
        pos.setErrorIndex(-1);
        pos.setIndex(source.length());
        return item;
    } catch (IllegalArgumentException e) {
        pos.setIndex(0);
        pos.setErrorIndex(0);
        return null;
    }
}

From source file:com.flexive.shared.FxFormatUtils.java

/**
 * Convert a String to Integer/* ww  w. j  a  va 2s .  co  m*/
 *
 * @param value value to convert
 * @return Integer
 */
public static Integer toInteger(String value) {
    if (StringUtils.isBlank(value))
        return null;
    final ParsePosition pos = new ParsePosition(0);
    final String _value = value.trim();
    try {
        final Number parse = FxValueRendererFactory.getNumberFormatInstance().parse(unquote(_value), pos);
        if (pos.getErrorIndex() >= 0 || pos.getIndex() != _value.length()
                || parse.doubleValue() != (double) parse.intValue() /*truncation*/)
            throw new FxConversionException("ex.conversion.value.error", FxNumber.class.getCanonicalName(),
                    value, "Failed to parse " + value).asRuntimeException();
        return parse.intValue();
    } catch (NumberFormatException e) {
        throw new FxConversionException("ex.conversion.value.error", FxLargeNumber.class.getCanonicalName(),
                value,
                "Failed to parse [" + value + "] using dec.sep. [" + FxContext.get().getDecimalSeparator()
                        + "] and grouping sep. [" + FxContext.get().getDecimalSeparator() + "]")
                                .asRuntimeException();
    }
}

From source file:com.flexive.shared.FxFormatUtils.java

/**
 * Convert a String to Long/* ww w.  ja  va2s.  com*/
 *
 * @param value value to convert
 * @return Long
 */
public static Long toLong(String value) {
    if (StringUtils.isBlank(value))
        return null;
    final ParsePosition pos = new ParsePosition(0);
    final String _value = value.trim();
    try {
        final Number parse = FxValueRendererFactory.getNumberFormatInstance().parse(unquote(_value), pos);
        if (pos.getErrorIndex() >= 0 || pos.getIndex() != _value.length()
                || parse.doubleValue() != (double) parse.longValue() /*truncation*/)
            throw new FxConversionException("ex.conversion.value.error", FxLargeNumber.class.getCanonicalName(),
                    value, "Failed to parse " + value).asRuntimeException();
        return parse.longValue();
    } catch (NumberFormatException e) {
        throw new FxConversionException("ex.conversion.value.error", FxLargeNumber.class.getCanonicalName(),
                value,
                "Failed to parse [" + value + "] using dec.sep. [" + FxContext.get().getDecimalSeparator()
                        + "] and grouping sep. [" + FxContext.get().getDecimalSeparator() + "]")
                                .asRuntimeException();
    }
}

From source file:com.flexive.shared.FxFormatUtils.java

/**
 * Convert a String to Float//from   w  ww.j  av a  2  s . co  m
 *
 * @param value value to convert
 * @return Float
 */
public static Float toFloat(String value) {
    if (StringUtils.isBlank(value))
        return null;
    final ParsePosition pos = new ParsePosition(0);
    final String _value = value.trim();
    try {
        Float res = FxValueRendererFactory.getNumberFormatInstance().parse(unquote(_value), pos).floatValue();
        if (pos.getErrorIndex() >= 0 || pos.getIndex() != _value.length())
            throw new FxConversionException("ex.conversion.value.error", FxFloat.class.getCanonicalName(),
                    value, "Failed to parse " + value).asRuntimeException();
        return res;
    } catch (NumberFormatException e) {
        throw new FxConversionException("ex.conversion.value.error", FxLargeNumber.class.getCanonicalName(),
                value,
                "Failed to parse [" + value + "] using dec.sep. [" + FxContext.get().getDecimalSeparator()
                        + "] and grouping sep. [" + FxContext.get().getDecimalSeparator() + "]")
                                .asRuntimeException();
    }
}

From source file:com.flexive.shared.FxFormatUtils.java

/**
 * Convert a String to Double/*from   www.j  a v  a  2  s  . co  m*/
 *
 * @param value value to convert
 * @return Double
 */
public static Double toDouble(String value) {
    if (StringUtils.isBlank(value))
        return null;
    final ParsePosition pos = new ParsePosition(0);
    final String _value = value.trim();
    try {
        Double res = FxValueRendererFactory.getNumberFormatInstance().parse(unquote(_value), pos).doubleValue();
        if (pos.getErrorIndex() >= 0 || pos.getIndex() != _value.length())
            throw new FxConversionException("ex.conversion.value.error", FxDouble.class.getCanonicalName(),
                    value, "Failed to parse " + value).asRuntimeException();
        return res;
    } catch (NumberFormatException e) {
        throw new FxConversionException("ex.conversion.value.error", FxLargeNumber.class.getCanonicalName(),
                value,
                "Failed to parse [" + value + "] using dec.sep. [" + FxContext.get().getDecimalSeparator()
                        + "] and grouping sep. [" + FxContext.get().getDecimalSeparator() + "]")
                                .asRuntimeException();
    }
}

From source file:er.extensions.formatters.ERXSimpleHTMLFormatter.java

/**
 * Converts an HTML string into an ASCII string
 * starting from a given parse position.
 * @param string HTML string/*from w  ww . jav  a 2  s  .com*/
 * @param p current parsing position
 * @return ASCII representation of the string
 */
@Override
public Object parseObject(String string, ParsePosition p) {
    int index = p.getIndex();
    String substring = string.substring(index);
    String result;
    try {
        result = (String) parseObject(substring);
        p.setIndex(string.length() + 1);
    } catch (java.text.ParseException e) {
        result = null;
    }
    return result;
}

From source file:com.anrisoftware.globalpom.format.locale.LocaleFormat.java

/**
 * Parses the specified string to a locale.
 * /*  w w w.  j  av  a 2  s .  c om*/
 * @return the parsed {@link Locale}.
 * 
 * @throws ParseException
 *             if the string cannot be parsed to a value.
 * 
 * @see Locale#toString()
 */
public Locale parse(String source) throws ParseException {
    ParsePosition pos = new ParsePosition(0);
    Locale result = parse(source, pos);
    if (pos.getIndex() == 0) {
        throw log.errorParse(source, pos);
    }
    return result;
}

From source file:com.anrisoftware.globalpom.version.VersionFormat.java

/**
 * @see #parseObject(String)/* w  ww. j a  va  2 s.  c o m*/
 *
 * @param pos
 *            the index {@link ParsePosition} position from where to start
 *            parsing.
 */
public Version parse(String source, ParsePosition pos) {
    try {
        source = source.substring(pos.getIndex());
        Version version = decodeVersion(source, pos);
        pos.setErrorIndex(-1);
        pos.setIndex(pos.getIndex() + source.length());
        return version;
    } catch (NumberFormatException e) {
        log.errorParseNumber(e, source);
        pos.setIndex(0);
        pos.setErrorIndex(0);
        return null;
    }
}

From source file:org.op4j.functions.FnCalendar.java

protected static Calendar fromInts(final Integer year, final Integer month, final Integer day,
        final Integer hour, final Integer minute, final Integer second, final Integer milli) throws Exception {

    /*/*from  w  w w  .  ja  v a  2  s.  co m*/
     * None of the Integers can be null 
     */
    Validate.notNull(year);
    Validate.notNull(month);
    Validate.notNull(day);
    Validate.notNull(hour);
    Validate.notNull(minute);
    Validate.notNull(second);
    Validate.notNull(milli);

    Integer safeYear = year;
    String yearAsString = year.toString();
    if ((safeYear.intValue() >= 0) && (yearAsString.length() <= 2)) {
        final SimpleDateFormat sdf = new SimpleDateFormat("yy");
        final Calendar calendar = Calendar.getInstance();

        //It uses ParsePosition to make sure the whole 
        //string has been converted into a number
        ParsePosition pp = new ParsePosition(0);
        Date date = sdf.parse(yearAsString, pp);
        if (pp.getIndex() != yearAsString.length()) {
            throw new ParseException("The whole input String does not represent a valid Date", pp.getIndex());
        }
        calendar.setTime(date);
        safeYear = Integer.valueOf(calendar.get(Calendar.YEAR));
    }

    final Calendar result = Calendar.getInstance();
    result.set(Calendar.YEAR, safeYear.intValue());
    result.set(Calendar.MONTH, month.intValue() - 1);
    result.set(Calendar.DAY_OF_MONTH, day.intValue());
    result.set(Calendar.HOUR_OF_DAY, hour.intValue());
    result.set(Calendar.MINUTE, minute.intValue());
    result.set(Calendar.SECOND, second.intValue());
    result.set(Calendar.MILLISECOND, milli.intValue());

    return result;
}