List of usage examples for java.text ParsePosition getErrorIndex
public int getErrorIndex()
From source file:de.betterform.xml.xforms.ui.AbstractFormControl.java
private BigDecimal strictParse(String value, Locale locale) throws ParseException { DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(locale); format.setParseBigDecimal(true);// w ww .j av a2 s . c o m value = value.trim(); ParsePosition pos = new ParsePosition(0); BigDecimal number = (BigDecimal) format.parse(value, pos); boolean okay = pos.getIndex() == value.length() && pos.getErrorIndex() == -1; if (!okay) throw new ParseException("Could not parse '" + value + "' as a number", pos.getErrorIndex()); return number; }
From source file:com.quinsoft.opencuas.RevenueDomain.java
@Override public Object convertExternalValue(Task task, AttributeInstance attributeInstance, AttributeDef attributeDef, String contextName, Object externalValue) { // If external value is an AttributeInstance then get *its* internal value. if (externalValue instanceof AttributeInstance) externalValue = ((AttributeInstance) externalValue).getValue(); // KJS - Added 01/28/11 the following two returns. if (externalValue == null) return null; if (externalValue instanceof Number) return ((Number) externalValue).doubleValue(); if (externalValue instanceof CharSequence) { String str = externalValue.toString(); // VML uses "" as a synonym for null. if (StringUtils.isBlank(str)) return null; ParsePosition ps = new ParsePosition(0); Double d;/*from www . j ava 2 s .c o m*/ synchronized (parser) { d = parser.parse(str, ps).doubleValue(); } int idx = Math.max(ps.getErrorIndex(), ps.getIndex()); if (idx != str.length()) { throw new InvalidAttributeValueException(attributeDef, str, "Error parsing '" + str + "' at position " + (idx + 1)); } return d; } throw new InvalidAttributeValueException(attributeDef, externalValue, "Can't convert '%s' to Double", externalValue.getClass().getName()); }
From source file:javadz.beanutils.locale.converters.DateLocaleConverter.java
/** * Convert the specified locale-sensitive input object into an output object of the * specified type./* ww w . ja va 2 s . com*/ * * @param value The input object to be converted * @param pattern The pattern is used for the convertion * @return the converted Date value * * @exception org.apache.commons.beanutils.ConversionException * if conversion cannot be performed successfully * @throws ParseException if an error occurs parsing */ protected Object parse(Object value, String pattern) throws ParseException { // Handle Date if (value instanceof java.util.Date) { return value; } // Handle Calendar if (value instanceof java.util.Calendar) { return ((java.util.Calendar) value).getTime(); } if (locPattern) { pattern = convertLocalizedPattern(pattern, locale); } // Create Formatter - use default if pattern is null DateFormat formatter = pattern == null ? DateFormat.getDateInstance(DateFormat.SHORT, locale) : new SimpleDateFormat(pattern, locale); formatter.setLenient(isLenient); // Parse the Date ParsePosition pos = new ParsePosition(0); String strValue = value.toString(); Object parsedValue = formatter.parseObject(strValue, pos); if (pos.getErrorIndex() > -1) { throw new ConversionException("Error parsing date '" + value + "' at position=" + pos.getErrorIndex()); } if (pos.getIndex() < strValue.length()) { throw new ConversionException( "Date '" + value + "' contains unparsed characters from position=" + pos.getIndex()); } return parsedValue; }
From source file:jp.co.ctc_g.jfw.core.util.Dates.java
/** * ??????????// w w w . j ava2 s .c om * {@link Date}???? * ?<code>null</code>??????????? * ???<code>null</code>???? * ?{@link SimpleDateFormat}???? * ?????????? * ?????????null???? * @param source ?? * @param pattern * @return ????{@link Date} */ public static Date makeFrom(String source, String pattern) { if (Strings.isEmpty(source) || Strings.isEmpty(pattern)) return null; SimpleDateFormat sdf = new SimpleDateFormat(pattern); sdf.setLenient(true); ParsePosition pos = new ParsePosition(0); Date date = sdf.parse(source, pos); if (pos.getErrorIndex() != -1 && L.isDebugEnabled()) { Map<String, Object> replace = new HashMap<String, Object>(3); replace.put("pattern", pattern); replace.put("index", pos.getErrorIndex() + 1); replace.put("target", source); L.debug(Strings.substitute(R.getString("D-UTIL#0002"), replace)); } return date; }
From source file:com.examples.with.different.packagename.testcarver.NumberConverter.java
/** * Convert a String into a <code>Number</code> object. * @param sourceType TODO/*ww w .j a v a 2 s. co m*/ * @param targetType The type to convert the value to * @param value The String date value. * @param format The NumberFormat to parse the String value. * * @return The converted Number object. * @throws ConversionException if the String cannot be converted. */ private Number parse(Class sourceType, Class targetType, String value, NumberFormat format) { ParsePosition pos = new ParsePosition(0); Number parsedNumber = format.parse(value, pos); if (pos.getErrorIndex() >= 0 || pos.getIndex() != value.length() || parsedNumber == null) { String msg = "Error converting from '" + toString(sourceType) + "' to '" + toString(targetType) + "'"; if (format instanceof DecimalFormat) { msg += " using pattern '" + ((DecimalFormat) format).toPattern() + "'"; } if (locale != null) { msg += " for locale=[" + locale + "]"; } throw new ConversionException(msg); } return parsedNumber; }
From source file:javadz.beanutils.converters.NumberConverter.java
/** * Convert a String into a <code>Number</code> object. * @param sourceType TODO//from w w w . j av a2s. c o m * @param targetType The type to convert the value to * @param value The String date value. * @param format The NumberFormat to parse the String value. * * @return The converted Number object. * @throws ConversionException if the String cannot be converted. */ private Number parse(Class sourceType, Class targetType, String value, NumberFormat format) { ParsePosition pos = new ParsePosition(0); Number parsedNumber = format.parse(value, pos); if (pos.getErrorIndex() >= 0 || pos.getIndex() != value.length() || parsedNumber == null) { String msg = "Error converting from '" + toString(sourceType) + "' to '" + toString(targetType) + "'"; if (format instanceof DecimalFormat) { msg += " using pattern '" + ((DecimalFormat) format).toPattern() + "'"; } if (locale != null) { msg += " for locale=[" + locale + "]"; } if (log().isDebugEnabled()) { log().debug(" " + msg); } throw new ConversionException(msg); } return parsedNumber; }
From source file:com.examples.with.different.packagename.testcarver.DateTimeConverter.java
/** * Parse a String into a <code>Calendar</code> object * using the specified <code>DateFormat</code>. * * @param sourceType The type of the value being converted * @param targetType The type to convert the value to * @param value The String date value./*from w w w . ja va 2 s. co m*/ * @param format The DateFormat to parse the String value. * * @return The converted Calendar object. * @throws ConversionException if the String cannot be converted. */ private Calendar parse(Class sourceType, Class targetType, String value, DateFormat format) { logFormat("Parsing", format); format.setLenient(false); ParsePosition pos = new ParsePosition(0); Date parsedDate = format.parse(value, pos); // ignore the result (use the Calendar) if (pos.getErrorIndex() >= 0 || pos.getIndex() != value.length() || parsedDate == null) { String msg = "Error converting '" + toString(sourceType) + "' to '" + toString(targetType) + "'"; if (format instanceof SimpleDateFormat) { msg += " using pattern '" + ((SimpleDateFormat) format).toPattern() + "'"; } throw new ConversionException(msg); } Calendar calendar = format.getCalendar(); return calendar; }
From source file:javadz.beanutils.converters.DateTimeConverter.java
/** * Parse a String into a <code>Calendar</code> object * using the specified <code>DateFormat</code>. * * @param sourceType The type of the value being converted * @param targetType The type to convert the value to * @param value The String date value.// ww w .j ava 2 s .c om * @param format The DateFormat to parse the String value. * * @return The converted Calendar object. * @throws ConversionException if the String cannot be converted. */ private Calendar parse(Class sourceType, Class targetType, String value, DateFormat format) { logFormat("Parsing", format); format.setLenient(false); ParsePosition pos = new ParsePosition(0); Date parsedDate = format.parse(value, pos); // ignore the result (use the Calendar) if (pos.getErrorIndex() >= 0 || pos.getIndex() != value.length() || parsedDate == null) { String msg = "Error converting '" + toString(sourceType) + "' to '" + toString(targetType) + "'"; if (format instanceof SimpleDateFormat) { msg += " using pattern '" + ((SimpleDateFormat) format).toPattern() + "'"; } if (log().isDebugEnabled()) { log().debug(" " + msg); } throw new ConversionException(msg); } Calendar calendar = format.getCalendar(); return calendar; }
From source file:org.apache.falcon.util.DateUtil.java
/** * Parses a datetime in ISO8601 format in the process timezone. * * @param s string with the datetime to parse. * @return the corresponding {@link java.util.Date} instance for the parsed date. * @throws java.text.ParseException thrown if the given string was * not an ISO8601 value for the process timezone. */// www. j a v a 2 s. c o m public static Date parseDateFalconTZ(String s) throws ParseException { s = s.trim(); ParsePosition pos = new ParsePosition(0); Date d = getISO8601DateFormat(activeTimeZone, activeTimeMask).parse(s, pos); if (d == null) { throw new ParseException("Could not parse [" + s + "] using [" + activeTimeMask + "] mask", pos.getErrorIndex()); } if (s.length() > pos.getIndex()) { throw new ParseException("Correct datetime string is followed by invalid characters: " + s, pos.getIndex()); } return d; }
From source file:org.apache.logging.log4j.core.util.datetime.FastDateParser.java
@Override public Date parse(final String source) throws ParseException { final ParsePosition pp = new ParsePosition(0); final Date date = parse(source, pp); if (date == null) { // Add a note re supported date range if (locale.equals(JAPANESE_IMPERIAL)) { throw new ParseException("(The " + locale + " locale does not support dates before 1868 AD)\n" + "Unparseable date: \"" + source, pp.getErrorIndex()); }/*from w w w . j a v a 2s. c om*/ throw new ParseException("Unparseable date: " + source, pp.getErrorIndex()); } return date; }