Example usage for java.text ParsePosition setErrorIndex

List of usage examples for java.text ParsePosition setErrorIndex

Introduction

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

Prototype

public void setErrorIndex(int ei) 

Source Link

Document

Set the index at which a parse error occurred.

Usage

From source file:com.anrisoftware.globalpom.format.byteformat.ByteFormat.java

/**
 * @see #parse(String)/*from   w  w  w.j  a va 2 s  .c o m*/
 *
 * @param pos
 *            the index {@link ParsePosition} position from where to start
 *            parsing.
 */
public Long parse(String source, ParsePosition pos, UnitMultiplier multiplier) {
    source = source.substring(pos.getIndex());
    try {
        long value = parseValue(source);
        value /= multiplier.getValue();
        pos.setErrorIndex(-1);
        pos.setIndex(source.length());
        return value;
    } catch (ParseException e) {
        pos.setIndex(0);
        pos.setErrorIndex(0);
        return null;
    }
}

From source file:com.anrisoftware.sscontrol.ldap.dbindex.DbIndexFormat.java

/**
 * Parses the specified string to database index.
 * <p>/*from   w  w w  . j  a va  2s  . c o  m*/
 * <h2>Format</h2>
 * <p>
 * <ul>
 * <li>{@code "index[:type]"}
 * <li>{@code "index_a,index_b,...[:type_a,type_b,...]"}
 * </ul>
 * 
 * @param source
 *            the source {@link String}.
 * 
 * @param pos
 *            the index {@link ParsePosition} position from where to start
 *            parsing.
 * 
 * @return the parsed {@link DbIndex}.
 * 
 * @throws ParseException
 *             if the string is not in the correct format.
 */
public DbIndex parse(String source, ParsePosition pos) {
    try {
        source = source.substring(pos.getIndex());
        String[] split = split(source, ':');
        Set<String> names = new HashSet<String>(splitNames(split));
        Set<IndexType> types = new HashSet<IndexType>();
        if (split.length > 1) {
            parseTypes(split, types);
        }
        pos.setErrorIndex(-1);
        pos.setIndex(pos.getIndex() + source.length());
        return factory.create(names, types);
    } catch (NumberFormatException e) {
        pos.setIndex(0);
        pos.setErrorIndex(0);
        return null;
    }
}

From source file:ISO8601DateFormat.java

/**
 * @see java.text.DateFormat#parse(String, ParsePosition)
 *///from  w w  w  .j  av  a 2 s .c  o m
public Date parse(String text, ParsePosition pos) {

    int i = pos.getIndex();

    try {
        int year = Integer.valueOf(text.substring(i, i + 4)).intValue();
        i += 4;

        if (text.charAt(i) != '-') {
            throw new NumberFormatException();
        }
        i++;

        int month = Integer.valueOf(text.substring(i, i + 2)).intValue() - 1;
        i += 2;

        if (text.charAt(i) != '-') {
            throw new NumberFormatException();
        }
        i++;

        int day = Integer.valueOf(text.substring(i, i + 2)).intValue();
        i += 2;

        calendar.set(year, month, day);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0); // no parts of a second

        i = parseTZ(i, text);

    } catch (NumberFormatException ex) {
        pos.setErrorIndex(i);
        return null;
    } catch (IndexOutOfBoundsException ex) {
        pos.setErrorIndex(i);
        return null;
    } finally {
        pos.setIndex(i);
    }

    return calendar.getTime();
}

From source file:ISO8601DateTimeFormat.java

/**
 * @see DateFormat#parse(String, ParsePosition)
 *///from  w  w w  . ja v a 2s .  c  o m
public Date parse(String text, ParsePosition pos) {

    int i = pos.getIndex();

    try {
        int year = Integer.valueOf(text.substring(i, i + 4)).intValue();
        i += 4;

        if (text.charAt(i) != '-') {
            throw new NumberFormatException();
        }
        i++;

        int month = Integer.valueOf(text.substring(i, i + 2)).intValue() - 1;
        i += 2;

        if (text.charAt(i) != '-') {
            throw new NumberFormatException();
        }
        i++;

        int day = Integer.valueOf(text.substring(i, i + 2)).intValue();
        i += 2;

        if (text.charAt(i) != 'T') {
            throw new NumberFormatException();
        }
        i++;

        int hour = Integer.valueOf(text.substring(i, i + 2)).intValue();
        i += 2;

        if (text.charAt(i) != ':') {
            throw new NumberFormatException();
        }
        i++;

        int mins = Integer.valueOf(text.substring(i, i + 2)).intValue();
        i += 2;

        int secs = 0;
        if (i < text.length() && text.charAt(i) == ':') {
            // handle seconds flexible
            i++;

            secs = Integer.valueOf(text.substring(i, i + 2)).intValue();
            i += 2;
        }

        calendar.set(year, month, day, hour, mins, secs);
        calendar.set(Calendar.MILLISECOND, 0); // no parts of a second

        i = parseTZ(i, text);

    } catch (NumberFormatException ex) {
        pos.setErrorIndex(i);
        return null;
    } catch (IndexOutOfBoundsException ex) {
        pos.setErrorIndex(i);
        return null;
    } finally {
        pos.setIndex(i);
    }

    return calendar.getTime();
}

From source file:DateFormatUtils.java

/**
 * <p>Parsing is not supported.</p>
 * //from   www . j a v  a 2s  .c om
 * @param source  the string to parse
 * @param pos  the parsing position
 * @return <code>null</code> as not supported
 */
public Object parseObject(String source, ParsePosition pos) {
    pos.setIndex(0);
    pos.setErrorIndex(0);
    return null;
}

From source file:org.apache.ws.security.util.XmlSchemaDateFormat.java

/**
 * This method was snarfed from <tt>org.apache.axis.encoding.ser.CalendarDeserializer</tt>,
 * which was written by Sam Ruby (rubys@us.ibm.com) and Rich Scheuerle (scheu@us.ibm.com).
 * Better error reporting was added./*from   w  w  w  .  j a  v  a  2  s  .co m*/
 *
 * @see DateFormat#parse(java.lang.String)
 */
public Date parse(String src, ParsePosition parse_pos) {
    Date date;

    // validate fixed portion of format
    int index = 0;
    try {
        if (src != null) {
            if ((src.charAt(0) == '+') || (src.charAt(0) == '-')) {
                src = src.substring(1);
            }

            if (src.length() < 19) {
                parse_pos.setIndex(src.length() - 1);
                handleParseError(parse_pos, "TOO_FEW_CHARS");
            }
            validateChar(src, parse_pos, index = 4, '-', "EXPECTED_DASH");
            validateChar(src, parse_pos, index = 7, '-', "EXPECTED_DASH");
            validateChar(src, parse_pos, index = 10, 'T', "EXPECTED_CAPITAL_T");
            validateChar(src, parse_pos, index = 13, ':', "EXPECTED_COLON_IN_TIME");
            validateChar(src, parse_pos, index = 16, ':', "EXPECTED_COLON_IN_TIME");
        }

        // convert what we have validated so far
        try {
            synchronized (DATEFORMAT_XSD_ZULU) {
                date = DATEFORMAT_XSD_ZULU.parse((src == null) ? null : (src.substring(0, 19) + ".000Z"));
            }
        } catch (Exception e) {
            throw new NumberFormatException(e.toString());
        }

        index = 19;

        // parse optional milliseconds
        if (src != null) {
            if ((index < src.length()) && (src.charAt(index) == '.')) {
                int milliseconds = 0;
                int start = ++index;

                while ((index < src.length()) && Character.isDigit(src.charAt(index))) {
                    index++;
                }

                String decimal = src.substring(start, index);

                if (decimal.length() == 3) {
                    milliseconds = Integer.parseInt(decimal);
                } else if (decimal.length() < 3) {
                    milliseconds = Integer.parseInt((decimal + "000").substring(0, 3));
                } else {
                    milliseconds = Integer.parseInt(decimal.substring(0, 3));

                    if (decimal.charAt(3) >= '5') {
                        ++milliseconds;
                    }
                }

                // add milliseconds to the current date
                date.setTime(date.getTime() + milliseconds);
            }

            // parse optional timezone
            if (((index + 5) < src.length()) && ((src.charAt(index) == '+') || (src.charAt(index) == '-'))) {
                validateCharIsDigit(src, parse_pos, index + 1, "EXPECTED_NUMERAL");
                validateCharIsDigit(src, parse_pos, index + 2, "EXPECTED_NUMERAL");
                validateChar(src, parse_pos, index + 3, ':', "EXPECTED_COLON_IN_TIMEZONE");
                validateCharIsDigit(src, parse_pos, index + 4, "EXPECTED_NUMERAL");
                validateCharIsDigit(src, parse_pos, index + 5, "EXPECTED_NUMERAL");

                final int hours = (((src.charAt(index + 1) - '0') * 10) + src.charAt(index + 2)) - '0';
                final int mins = (((src.charAt(index + 4) - '0') * 10) + src.charAt(index + 5)) - '0';
                int millisecs = ((hours * 60) + mins) * 60 * 1000;

                // subtract millisecs from current date to obtain GMT
                if (src.charAt(index) == '+') {
                    millisecs = -millisecs;
                }

                date.setTime(date.getTime() + millisecs);
                index += 6;
            }

            if ((index < src.length()) && (src.charAt(index) == 'Z')) {
                index++;
            }

            if (index < src.length()) {
                handleParseError(parse_pos, "TOO_MANY_CHARS");
            }
        }
    } catch (ParseException pe) {
        log.error(pe.toString(), pe);
        index = 0; // IMPORTANT: this tells DateFormat.parse() to throw a ParseException
        parse_pos.setErrorIndex(index);
        date = null;
    }
    parse_pos.setIndex(index);
    return (date);
}

From source file:org.osaf.cosmo.eim.schema.text.DurationFormat.java

public Object parseObject(String source, ParsePosition pos) {
    if (pos.getIndex() > 0)
        return null;

    if (!PATTERN.matcher(source).matches()) {
        parseException = new ParseException("Invalid duration " + source, 0);
        pos.setErrorIndex(0);
        return null;
    }/*from w  w  w .ja  va2  s .c  o m*/

    Dur dur = new Dur(source);

    pos.setIndex(source.length());

    return dur;
}

From source file:org.osaf.cosmo.eim.schema.text.TriageStatusFormat.java

public Object parseObject(String source, ParsePosition pos) {
    if (pos.getIndex() > 0)
        return null;

    int index = 0;

    String[] chunks = source.split(" ", 3);
    if (chunks.length != 3) {
        parseException = new ParseException("Incorrect number of chunks: " + chunks.length, 0);
        pos.setErrorIndex(index);
        return null;
    }// ww  w.  j  a  v a  2s  .c o m

    TriageStatus ts = entityFactory.createTriageStatus();

    try {
        pos.setIndex(index);
        Integer code = new Integer(chunks[0]);
        // validate the code as being known
        TriageStatusUtil.label(code);
        ts.setCode(code);
        index += chunks[0].length() + 1;
    } catch (Exception e) {
        parseException = new ParseException(e.getMessage(), 0);
        pos.setErrorIndex(index);
        return null;
    }

    try {
        pos.setIndex(index);
        BigDecimal rank = new BigDecimal(chunks[1]);
        if (rank.scale() != 2)
            throw new NumberFormatException("Invalid rank value " + chunks[1]);
        ts.setRank(rank);
        index += chunks[1].length() + 1;
    } catch (NumberFormatException e) {
        parseException = new ParseException(e.getMessage(), 0);
        pos.setErrorIndex(index);
        return null;
    }

    if (chunks[2].equals(AUTOTRIAGE_ON))
        ts.setAutoTriage(Boolean.TRUE);
    else if (chunks[2].equals(AUTOTRIAGE_OFF))
        ts.setAutoTriage(Boolean.FALSE);
    else {
        parseException = new ParseException("Invalid autotriage value " + chunks[2], 0);
        pos.setErrorIndex(index);
        return null;
    }
    index += chunks[2].length();

    pos.setIndex(index);

    return ts;
}