List of usage examples for java.text ParsePosition getIndex
public int getIndex()
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 ww w . j a v a 2s . com*/ 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: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 ww w . j a va 2s . 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:org.alfresco.util.CachingDateFormat.java
/** * Parses and caches date strings.//from w w w .ja v a2s.c om * * @see java.text.DateFormat#parse(java.lang.String, * java.text.ParsePosition) */ public Date parse(String text, ParsePosition pos) { Date cached = cacheDates.get(text); if (cached == null) { Date date = super.parse(text, pos); if ((date != null) && (pos.getIndex() == text.length())) { cacheDates.put(text, date); Date clonedDate = (Date) date.clone(); return clonedDate; } else { return date; } } else { pos.setIndex(text.length()); Date clonedDate = (Date) cached.clone(); return clonedDate; } }
From source file:org.wings.SDimension.java
/** * Tries to extract unit from passed string. I.e. returtn "px" if you pass "120px". * * @param value A compound number/unit string. May be <code>null</code> * @return The unit or <code>null</code> if not possible. *//*from w ww .j a v a2s.c om*/ private String extractUnit(String value) { if (value == null) { return null; } else { ParsePosition position = new ParsePosition(0); try { new DecimalFormat().parse(value, position).intValue(); return value.substring(position.getIndex()).trim(); } catch (Exception e) { return null; } } }
From source file:net.sf.morph.transform.converters.TextToNumberConverter.java
/** * Learn whether the entire string was consumed. * @param stringWithoutIgnoredSymbolsStr * @param position//from w ww .j a va 2 s . co m * @return boolean */ protected boolean isParseSuccessful(String stringWithoutIgnoredSymbolsStr, ParsePosition position) { return position.getIndex() != 0 && position.getIndex() == stringWithoutIgnoredSymbolsStr.length(); }
From source file:Unsigned.java
/** * Parse a binary number, skipping leading whitespace. Does not throw an * exception; if no object can be parsed, index is unchanged! * /*from w w w . j a v a 2s .c om*/ * @param source * the string to parse * @param status * the string index to start at * @return The binary number as a Long object. * * @since 1.0 */ public Object parseObject(String source, ParsePosition status) { int start = status.getIndex(); boolean success = false; boolean skipWhitespace = true; StringBuffer buffer = new StringBuffer(); StringCharacterIterator iter = new StringCharacterIterator(source, start); for (char c = iter.current(); c != CharacterIterator.DONE; c = iter.next()) { if (skipWhitespace && Character.isWhitespace(c)) { // skip whitespace continue; } skipWhitespace = false; if ((c == '1') || (c == '0')) { success = true; buffer.append(c); } else { break; } } if (!success) { return (null); } // convert binary to long if (buffer.length() > 64) { // larger than a long, error return (null); } long result = 0; buffer.reverse(); int length = buffer.length(); for (int i = 0; i < length; i++) { result += (buffer.charAt(i) == '1') ? 1 << i : 0; } status.setIndex(iter.getIndex()); return (new Long(result)); }
From source file:com.examples.with.different.packagename.testcarver.NumberConverter.java
/** * Convert a String into a <code>Number</code> object. * @param sourceType TODO//from w w w . j a v a 2 s .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 + "]"; } throw new ConversionException(msg); } return parsedNumber; }
From source file:org.jboss.forge.roaster.model.impl.PropertyImpl.java
@Override public PropertySource<O> setName(final String name) { if (StringUtils.isBlank(name)) { throw new IllegalStateException("Property name cannot be null/empty/blank"); }// w ww. ja v a2 s. c om if (hasField()) { getField().setName(name); } final String oldName = this.name; final boolean visitDocTags = true; final ASTVisitor renameVisitor = new ASTVisitor(visitDocTags) { @Override public boolean visit(SimpleName node) { if (Objects.equals(oldName, node.getIdentifier())) { node.setIdentifier(name); } return super.visit(node); } @Override public boolean visit(TextElement node) { final String text = node.getText(); if (!text.contains(oldName)) { return super.visit(node); } final int matchLength = oldName.length(); final int textLength = text.length(); final StringBuilder buf = new StringBuilder(text.length()); final ParsePosition pos = new ParsePosition(0); while (pos.getIndex() < textLength) { final int index = pos.getIndex(); final char c = text.charAt(index); if (Character.isJavaIdentifierStart(c)) { final int next = index + matchLength; if (next <= textLength && Objects.equals(oldName, text.substring(index, next))) { buf.append(name); pos.setIndex(next); continue; } } buf.append(c); pos.setIndex(index + 1); } node.setText(buf.toString()); return super.visit(node); } }; if (isAccessible()) { final MethodSource<O> accessor = getAccessor(); final String prefix = accessor.getReturnType().isType(boolean.class) ? "is" : "get"; accessor.setName(methodName(prefix, name)); ((MethodDeclaration) accessor.getInternal()).accept(renameVisitor); } if (isMutable()) { final MethodSource<O> mutator = getMutator(); mutator.setName(methodName("set", name)); ((MethodDeclaration) mutator.getInternal()).accept(renameVisitor); } this.name = name; return this; }
From source file:org.mule.modules.clarizen.api.ClarizenDateConverter.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./* w w w . j av a 2s . c o m*/ * @param format The DateFormat to parse the String value. * * @return The converted Calendar object. * @throws ConversionException if the String cannot be converted. */ @SuppressWarnings("rawtypes") private Calendar parse(Class sourceType, Class targetType, String value, DateFormat 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 '" + classToString(sourceType) + "' to '" + classToString(targetType) + "'"; if (format instanceof SimpleDateFormat) { msg += " using pattern '" + ((SimpleDateFormat) format).toPattern() + "'"; } throw new ConversionException(msg); } Calendar calendar = format.getCalendar(); return calendar; }