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.anrisoftware.sscontrol.ldap.dbindex.DbIndexFormat.java

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

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

/**
 * Parses the specified string to a continued fraction.
 * <p>/*from   w  w w .j a  v a 2  s .  c om*/
 * <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.degree.DegreeSexagesimalFormat.java

/**
 * @see #parseObject(String)/*w  w  w . j a va 2s .c  o m*/
 * 
 * @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.projity.grouping.core.OutlineCode.java

public Object parseObject(String code, ParsePosition pos) {
    Object result = null;//from   www .  jav  a 2s .  c  om
    Iterator i = masks.iterator();
    String current = code.substring(pos.getIndex());
    Matcher matcher = pattern.matcher(current);
    if (matcher.matches()) {
        pos.setIndex(pos.getIndex() + matcher.end());
        return current;
    } else
        return null;
}

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

/**
 * @see #parse(String, ParsePosition)/* w w w  .ja v a  2s.c om*/
 */
public Amount<Angle> parse(String source) throws ParseException {
    ParsePosition pos = new ParsePosition(0);
    Amount<Angle> result = parse(source, pos);
    if (pos.getIndex() == 0) {
        throw log.errorParse(source, pos);
    }
    return result;
}

From source file:org.netxilia.api.impl.value.NumberParser.java

@Override
public IGenericValue parse(String text) {
    String input = preprocess(text);
    ParsePosition pp = new ParsePosition(0);

    for (NumberFormat format : this.formats) {
        pp.setIndex(0);//from  ww w.  java 2s . c  o m
        Number number = format.parse(input, pp);
        if (number != null && input.length() == pp.getIndex()) {
            return new NumberValue(number.doubleValue());
        }
    }

    return null;
}

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

/**
 * Parses the specified string to a point.
 * <p>/*from  w w  w . j a v  a 2s. c o  m*/
 * The parser expects the patterns:
 * 
 * <ul>
 * <li><code>(x, y)</code></li>
 * <li><code>(x,y)</code></li>
 * <li><code>x, y</code></li>
 * <li><code>x,y</code></li>
 * </ul>
 * 
 * @param point
 *            the {@link Point2D} that should be parsed. If {@code null} a
 *            {@link Point2D#Double} is used.
 * 
 * @return the parsed {@link Point2D}.
 * 
 * @throws ParseException
 *             if the string cannot be parsed to a point.
 * 
 * @see Point
 * @see Point2D#Double
 * @see Point2D#Float
 */
public Point2D parse(String source, Point2D point) throws ParseException {
    ParsePosition pos = new ParsePosition(0);
    Point2D result = parse(source, pos, point);
    if (pos.getIndex() == 0) {
        throw log.errorParsePoint(source, pos);
    }
    return result;
}

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

/**
 * @see #parse(String)//from  www .j  a v a2s  . 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:ISO8601DateTimeFormat.java

/**
 * @see DateFormat#parse(String, ParsePosition)
 *//*w w w .j a 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:com.anrisoftware.globalpom.format.byteformat.ByteFormat.java

/**
 * Parses the specified string to computer byte.
 * <p>//w w w  .  java 2  s .  c om
 * The format follows the pattern:
 *
 * <pre>
 * &lt;value&gt;(byte|kB,MB,...,YB|KiB,MiB,...,YiB)
 * </pre>
 *
 * <p>
 * <h2>Examples</h2>
 * <p>
 * <ul>
 * <li>{@code "64 byte"}
 * <li>{@code "1 kb"}
 * <li>{@code "1 Kib"}
 * </ul>
 *
 * @param multiplier
 *            the unit {@link UnitMultiplier}.
 *
 * @return the parsed value.
 *
 * @throws ParseException
 *             if the string cannot be parsed to a value.
 */
public long parse(String source, UnitMultiplier multiplier) throws ParseException {
    ParsePosition pos = new ParsePosition(0);
    Long result = parse(source, pos, multiplier);
    if (pos.getIndex() == 0) {
        throw log.errorParseValue(source, pos);
    }
    return result;
}