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:org.xlcloud.openstack.model.climate.json.OpenStackDateDeserializer.java

private Date parseDate(String input, String[] datePatterns) throws ParseException {
    SimpleDateFormat parser = new SimpleDateFormat();
    parser.setLenient(true);/*from   w w w. j av a  2s  .c o m*/
    parser.setTimeZone(DateUtils.UTC_TIME_ZONE);
    ParsePosition position = new ParsePosition(0);
    for (String datePattern : datePatterns) {
        parser.applyPattern(datePattern);
        Date date = parser.parse(input, position);
        if (date != null && position.getIndex() == input.length()) {
            return date;
        }
    }
    throw new ParseException("Unable to parse the date: " + input, -1);
}

From source file:com.anrisoftware.globalpom.format.measurement.MeasureFormat.java

/**
 * Parses the specified string to physical measurement.
 * <p>/*ww w .ja  v a  2s .c o m*/
 * The format follows the pattern:
 * 
 * <pre>
 * &lt;value&gt;[(&lt;uncertainty&gt;);&lt;significant&gt;;&lt;decimal&gt;;&lt;unit&gt;;]
 * </pre>
 * 
 * <p>
 * <h2>Examples</h2>
 * <p>
 * <ul>
 * <li>exact value: {@code 0.0123;m/s;}
 * <li>uncertain value: {@code 5.0(0.2);1;1;m/s;}
 * </ul>
 * 
 * @return the parsed {@link Measure}.
 * 
 * @param <UnitType>
 *            the {@link Quantity} of the unit.
 * 
 * @throws ParseException
 *             if the string cannot be parsed to a value.
 * 
 * @since 1.11
 */
public <UnitType extends Quantity> Measure<UnitType> parse(String source) throws ParseException {
    ParsePosition pos = new ParsePosition(0);
    Measure<?> result = parse(source, pos);
    if (pos.getIndex() == 0) {
        throw log.errorParseValue(source, pos);
    }
    return toUnitType(result);
}

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

/**
 * @see #parse(String, ParsePosition)/*from  ww  w .  jav a 2 s .  c o  m*/
 */
public ContinuedFraction parse(String source) throws ParseException {
    ParsePosition pos = new ParsePosition(0);
    ContinuedFraction result = parse(source, pos);
    if (pos.getIndex() == 0) {
        throw log.errorParse(source, pos);
    }
    return result;
}

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   w ww  .jav a2s .c  o  m

        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);
    }
}

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 . j a  va  2  s . c  o  m*/
 * 
 * @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: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;
        }//from www  .  jav  a  2 s.  com
        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.eclipse.wb.internal.css.semantics.LengthValue.java

/**
 * Checks if current value requires unit. We need unit only if value is number.
 *//*from w  w  w .  j a  v  a2  s.c  o  m*/
public boolean requiresUnit() {
    if (hasValue()) {
        ParsePosition parsePosition = new ParsePosition(0);
        VALUE_DECIMAL_FORMAT.parse(m_value, parsePosition);
        return parsePosition.getIndex() == m_value.length();
    }
    return false;
}

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

/**
 * Parses the specified string to database index.
 * <p>//from   www .  ja v a2 s  .co 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:com.anrisoftware.globalpom.format.latlong.LatitudeFormat.java

/**
 * Parses the latitude part to a latitude/longitude coordinate.
 * //  w  w w. j a v  a 2s. c o m
 * @return the parsed {@link LatLong}.
 * 
 * @throws ParseException
 *             if the string can not be parsed.
 */
public LatLong parse(String source) throws ParseException {
    ParsePosition pos = new ParsePosition(0);
    LatLong result = parse(source, pos);
    if (pos.getIndex() == 0) {
        throw log.errorParseLatitude(source, pos);
    }
    return result;
}

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

/**
 * Parses the specified string to a point.
 * <p>// w  w  w . jav  a 2 s . c o m
 * 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;
    }
}