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.locale.LocaleFormat.java

/**
 * @see #parse(String)//from  w w w .j a v a2 s .c om
 * 
 * @param pos
 *            the index {@link ParsePosition} position from where to start
 *            parsing.
 */
public Locale parse(String source, ParsePosition pos) {
    source = source.substring(pos.getIndex());
    try {
        Locale address = parseValue(source, pos);
        pos.setErrorIndex(-1);
        pos.setIndex(source.length() + 1);
        return address;
    } catch (ParseException e) {
        pos.setIndex(0);
        pos.setErrorIndex(0);
        return null;
    }
}

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

/**
 * @see #parseObject(String)/*from www  . j a  v a 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:com.anrisoftware.fractions.format.FractionFormat.java

/**
 * Parses the specified string to a continued fraction.
 * <p>/*from  w  w  w .java  2  s. com*/
 * <h2>Format</h2>
 * <p>
 * <ul>
 * <li>{@code "[n0;n1,...,ni]"}
 * </ul>
 * 
 * @return the parsed {@link ContinuedFraction}.
 */
@Override
public Object parseObject(String source, ParsePosition pos) {
    try {
        return parse(source, pos);
    } catch (ParseException e) {
        pos.setErrorIndex(pos.getIndex() + e.getErrorOffset());
        return null;
    }
}

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

/**
 * @see #parseObject(String, ParsePosition)
 *//*from w ww.jav  a  2s .c om*/
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.anrisoftware.globalpom.format.measurement.MeasureFormat.java

/**
 * @see #parse(String)/*  w ww .  j  a  va  2 s. c  om*/
 *
 * @param pos
 *            the index {@link ParsePosition} position from where to start
 *            parsing.
 */
public Measure<?> parse(String source, ParsePosition pos) {
    source = source.substring(pos.getIndex());
    try {
        Measure<?> address = parseValue(source, pos);
        pos.setErrorIndex(-1);
        pos.setIndex(source.length());
        return address;
    } catch (ParseException e) {
        pos.setIndex(0);
        pos.setErrorIndex(0);
        return null;
    }
}

From source file:com.anrisoftware.fractions.format.FractionFormat.java

/**
 * @see #parseObject(String)//from   w  w  w.j av a  2  s.com
 * 
 * @param pos
 *            the index {@link ParsePosition} position from where to start
 *            parsing.
 * 
 * @throws ParseException
 *             if the string is not in the correct format.
 */
public ContinuedFraction parse(String source, ParsePosition pos) throws ParseException {
    try {
        source = source.substring(pos.getIndex());
        ContinuedFraction f = decodeFraction(source, pos);
        pos.setErrorIndex(-1);
        pos.setIndex(pos.getIndex() + source.length());
        return f;
    } catch (NumberFormatException e) {
        log.errorParseNumber(e, source);
        pos.setIndex(0);
        pos.setErrorIndex(0);
        return null;
    }
}

From source file:com.anrisoftware.globalpom.format.latlong.LatitudeFormat.java

/**
 * Parses the the latitude part with the specified index to a
 * latitude/longitude coordinate./*from   w  w  w  .jav a 2 s  . com*/
 * 
 * @param source
 *            the source string.
 * 
 * @param pos
 *            the index {@link ParsePosition} position from where to start
 *            parsing.
 * 
 * @return the parsed {@link LatLong}.
 * 
 * @throws ParseException
 *             if the string can not be parsed to a latitude/longitude
 *             coordinate.
 */
public LatLong parse(String source, ParsePosition pos) {
    try {
        source = source.substring(pos.getIndex());
        LatLong latlong = parse(this.latLong, source, pos);
        pos.setErrorIndex(-1);
        pos.setIndex(source.length());
        return latlong;
    } catch (ParseException e) {
        pos.setIndex(0);
        pos.setErrorIndex(0);
        return null;
    }
}

From source file:com.anrisoftware.globalpom.format.point.PointFormat.java

/**
 * Parses the specified string to a point.
 * <p>/*from  www.ja  v  a2s  .  c om*/
 * The parser expects the patterns:
 * 
 * <ul>
 * <li>{@code (x, y)}</li>
 * <li>{@code (x,y)}</li>
 * <li>{@code x, y}</li>
 * <li>{@code x,y}</li>
 * </ul>
 * 
 * @param source
 *            the source string.
 * 
 * @param pos
 *            the index {@link ParsePosition} position from where to start
 *            parsing.
 * 
 * @param point
 *            the {@link Point2D} that should be parsed. If {@code null} a
 *            {@link Point2D#Double} is used.
 * 
 * @return the parsed {@link Point2D}.
 * 
 * @see Point
 * @see Point2D#Double
 * @see Point2D#Float
 */
public Point2D parse(String source, ParsePosition pos, Point2D point) {
    source = source.substring(pos.getIndex());
    try {
        Point2D address = parsePoint(source, point);
        pos.setErrorIndex(-1);
        pos.setIndex(source.length());
        return address;
    } catch (ParseException e) {
        pos.setIndex(0);
        pos.setErrorIndex(0);
        return null;
    }
}

From source file:com.anrisoftware.globalpom.format.degree.DegreeSexagesimalFormat.java

/**
 * @see #parseObject(String)//from  w  w w .  ja v a2s  .c  om
 * 
 * @param pos
 *            the index {@link ParsePosition} position from where to start
 *            parsing.
 * 
 * @throws ParseException
 *             if the string is not in the correct format.
 */
public Amount<Angle> parse(String source, ParsePosition pos) throws ParseException {
    try {
        source = source.substring(pos.getIndex());
        Amount<Angle> result = decodeAngle(source, pos);
        pos.setErrorIndex(-1);
        pos.setIndex(pos.getIndex() + source.length());
        return result;
    } catch (NumberFormatException e) {
        log.errorParseNumber(e, source);
        pos.setIndex(0);
        pos.setErrorIndex(0);
        return null;
    }
}

From source file:com.anrisoftware.sscontrol.hosts.utils.HostFormat.java

@Override
public Object parseObject(String source, ParsePosition pos) {
    source = source.trim();//from   w ww.  j  av a2s .  c  om
    TemplateResource template;
    template = templates.getResource(PARSE_TEMPLATE_NAME, locale);
    Pattern pattern = Pattern.compile(template.getText());
    Matcher matcher = pattern.matcher(source);
    if (!matcher.matches()) {
        pos.setErrorIndex(pos.getIndex());
        return null;
    }
    String address = matcher.group(1);
    String name = matcher.group(2);
    Host host = hostFactory.create(name, address);
    String[] aliases = split(matcher.group(3), ",");
    for (int i = 0; i < aliases.length; i++) {
        String alias = aliases[i].replaceAll("[\"']", "");
        host.addAlias(alias);
    }
    pos.setIndex(matcher.end());
    return host;
}