List of usage examples for java.text ParsePosition setIndex
public void setIndex(int index)
From source file:com.anrisoftware.globalpom.format.latlong.LatitudeFormat.java
/** * Parses the the latitude part with the specified index to a * latitude/longitude coordinate.// w ww. jav a2s .co 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:com.anrisoftware.globalpom.format.point.PointFormat.java
/** * Parses the specified string to a point. * <p>// w ww . j a v a 2s . 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.projity.grouping.core.OutlineCode.java
public Object parseObject(String code, ParsePosition pos) { Object result = null;//from w w w . java2 s.co m 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.alibaba.otter.node.etl.common.db.utils.SqlTimestampConverter.java
private Date parseDate(String str, String[] parsePatterns, Locale locale) throws ParseException { if ((str == null) || (parsePatterns == null)) { throw new IllegalArgumentException("Date and Patterns must not be null"); }//from w ww.jav a2 s .c om SimpleDateFormat parser = null; ParsePosition pos = new ParsePosition(0); for (int i = 0; i < parsePatterns.length; i++) { if (i == 0) { parser = new SimpleDateFormat(parsePatterns[0], locale); } else { parser.applyPattern(parsePatterns[i]); } pos.setIndex(0); Date date = parser.parse(str, pos); if ((date != null) && (pos.getIndex() == str.length())) { return date; } } throw new ParseException("Unable to parse the date: " + str, -1); }
From source file:com.anrisoftware.globalpom.format.degree.DegreeSexagesimalFormat.java
/** * @see #parseObject(String)/*from www .j av a 2 s . co 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.anrisoftware.globalpom.format.byteformat.ByteFormat.java
/** * @see #parse(String)/*w w w. j a v a2 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:org.totschnig.myexpenses.util.Utils.java
/** * <a href="http://www.ibm.com/developerworks/java/library/j-numberformat/"> * http://www.ibm.com/developerworks/java/library/j-numberformat/</a> * * @param strFloat/*w w w.j a va 2 s. c o m*/ * parsed as float with the number format defined in the locale * @return the float retrieved from the string or null if parse did not * succeed */ public static BigDecimal validateNumber(DecimalFormat df, String strFloat) { ParsePosition pp; pp = new ParsePosition(0); pp.setIndex(0); df.setParseBigDecimal(true); BigDecimal n = (BigDecimal) df.parse(strFloat, pp); if (strFloat.length() != pp.getIndex() || n == null) { return null; } else { return n; } }
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. jav a 2 s . c o 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:com.anrisoftware.sscontrol.ldap.dbindex.DbIndexFormat.java
/** * Parses the specified string to database index. * <p>// ww w . ja v a 2 s . com * <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:org.apache.openmeetings.service.calendar.caldav.IcalUtils.java
/** * Adapted from DateUtils to support Timezones, and parse ical dates into {@link java.util.Date}. * Note: Replace FastDateFormat to java.time, when shifting to Java 8 or higher. * * @param str Date representation in String. * @param patterns Patterns to parse the date against * @param _timeZone Timezone of the Date. * @return <code>java.util.Date</code> representation of string or * <code>null</code> if the Date could not be parsed. *//*from w w w. j av a 2 s .co m*/ public Date parseDate(String str, String[] patterns, TimeZone _timeZone) { FastDateFormat parser; Locale locale = WebSession.get().getLocale(); TimeZone timeZone = str.endsWith("Z") ? TimeZone.getTimeZone("UTC") : _timeZone; ParsePosition pos = new ParsePosition(0); for (String pattern : patterns) { parser = FastDateFormat.getInstance(pattern, timeZone, locale); pos.setIndex(0); Date date = parser.parse(str, pos); if (date != null && pos.getIndex() == str.length()) { return date; } } log.error("Unable to parse the date: " + str + " at " + -1); return null; }