org.springframework.format.Parser Java Examples

The following examples show how to use org.springframework.format.Parser. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FormattingConversionService.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	Annotation ann = targetType.getAnnotation(this.annotationType);
	if (ann == null) {
		throw new IllegalStateException(
				"Expected [" + this.annotationType.getName() + "] to be present on " + targetType);
	}
	AnnotationConverterKey converterKey = new AnnotationConverterKey(ann, targetType.getObjectType());
	GenericConverter converter = cachedParsers.get(converterKey);
	if (converter == null) {
		Parser<?> parser = this.annotationFormatterFactory.getParser(
				converterKey.getAnnotation(), converterKey.getFieldType());
		converter = new ParserConverter(this.fieldType, parser, FormattingConversionService.this);
		cachedParsers.put(converterKey, converter);
	}
	return converter.convert(source, sourceType, targetType);
}
 
Example #2
Source File: ControlBinder.java    From jdal with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected void doUpdate() {
	Object value = controlAccessor.getControlValue();
	
	if (controlAccessor.isTextControl()) {
		Parser<?> parser = getParser();
		if (parser != null)
			try {
				value = parser.parse((String) value, Locale.getDefault());
			} catch (ParseException e) {
				log.error("Can't parse String : " + value.toString());
			}
	}
	setValue(value);
	
}
 
Example #3
Source File: FormattingConversionService.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	Annotation ann = targetType.getAnnotation(this.annotationType);
	if (ann == null) {
		throw new IllegalStateException(
				"Expected [" + this.annotationType.getName() + "] to be present on " + targetType);
	}
	AnnotationConverterKey converterKey = new AnnotationConverterKey(ann, targetType.getObjectType());
	GenericConverter converter = cachedParsers.get(converterKey);
	if (converter == null) {
		Parser<?> parser = this.annotationFormatterFactory.getParser(
				converterKey.getAnnotation(), converterKey.getFieldType());
		converter = new ParserConverter(this.fieldType, parser, FormattingConversionService.this);
		cachedParsers.put(converterKey, converter);
	}
	return converter.convert(source, sourceType, targetType);
}
 
Example #4
Source File: FormattingConversionService.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	Annotation ann = targetType.getAnnotation(this.annotationType);
	if (ann == null) {
		throw new IllegalStateException(
				"Expected [" + this.annotationType.getName() + "] to be present on " + targetType);
	}
	AnnotationConverterKey converterKey = new AnnotationConverterKey(ann, targetType.getObjectType());
	GenericConverter converter = cachedParsers.get(converterKey);
	if (converter == null) {
		Parser<?> parser = this.annotationFormatterFactory.getParser(
				converterKey.getAnnotation(), converterKey.getFieldType());
		converter = new ParserConverter(this.fieldType, parser, FormattingConversionService.this);
		cachedParsers.put(converterKey, converter);
	}
	return converter.convert(source, sourceType, targetType);
}
 
Example #5
Source File: FormattingConversionService.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	Annotation ann = targetType.getAnnotation(this.annotationType);
	if (ann == null) {
		throw new IllegalStateException(
				"Expected [" + this.annotationType.getName() + "] to be present on " + targetType);
	}
	AnnotationConverterKey converterKey = new AnnotationConverterKey(ann, targetType.getObjectType());
	GenericConverter converter = cachedParsers.get(converterKey);
	if (converter == null) {
		Parser<?> parser = this.annotationFormatterFactory.getParser(
				converterKey.getAnnotation(), converterKey.getFieldType());
		converter = new ParserConverter(this.fieldType, parser, FormattingConversionService.this);
		cachedParsers.put(converterKey, converter);
	}
	return converter.convert(source, sourceType, targetType);
}
 
Example #6
Source File: EntityFormatAnnotationFormatterFactory.java    From springlets with Apache License 2.0 5 votes vote down vote up
@Override
public Parser<?> getParser(EntityFormat annotation, Class<?> fieldType) {

  Assert.notNull(annotation, "The EntityFormat annotation is required");
  Assert.notNull(fieldType, "The Class of the field to parse is required");

  EntityResolver<?, ?> resolver = entity2Resolver.get(fieldType);
  if (resolver == null) {
    throw new IllegalArgumentException(
        "Not found a required EntityService bean for the type: " + fieldType);
  }

  return new EntityParser<>(resolver, conversionService);
}
 
Example #7
Source File: JodaDateTimeFormatAnnotationFormatterFactory.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public Parser<?> getParser(DateTimeFormat annotation, Class<?> fieldType) {
	if (LocalDate.class == fieldType) {
		return new LocalDateParser(getFormatter(annotation, fieldType));
	}
	else if (LocalTime.class == fieldType) {
		return new LocalTimeParser(getFormatter(annotation, fieldType));
	}
	else if (LocalDateTime.class == fieldType) {
		return new LocalDateTimeParser(getFormatter(annotation, fieldType));
	}
	else {
		return new DateTimeParser(getFormatter(annotation, fieldType));
	}
}
 
Example #8
Source File: JodaTimeFormatterRegistrar.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void addFormatterForFields(FormatterRegistry registry, Printer<?> printer,
		Parser<?> parser, Class<?>... fieldTypes) {

	for (Class<?> fieldType : fieldTypes) {
		registry.addFormatterForFieldType(fieldType, printer, parser);
	}
}
 
Example #9
Source File: FormattingConversionServiceFactoryBeanTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public Parser<?> getParser(SpecialInt annotation, Class<?> fieldType) {
	assertEquals("aliased", annotation.value());
	assertEquals("aliased", annotation.alias());
	return new Parser<Integer>() {
		@Override
		public Integer parse(String text, Locale locale) throws ParseException {
			return Integer.parseInt(text.substring(1));
		}
	};
}
 
Example #10
Source File: JodaTimeFormatterRegistrar.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void addFormatterForFields(FormatterRegistry registry, Printer<?> printer,
		Parser<?> parser, Class<?>... fieldTypes) {

	for (Class<?> fieldType : fieldTypes) {
		registry.addFormatterForFieldType(fieldType, printer, parser);
	}
}
 
Example #11
Source File: JodaDateTimeFormatAnnotationFormatterFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public Parser<?> getParser(DateTimeFormat annotation, Class<?> fieldType) {
	if (LocalDate.class == fieldType) {
		return new LocalDateParser(getFormatter(annotation, fieldType));
	}
	else if (LocalTime.class == fieldType) {
		return new LocalTimeParser(getFormatter(annotation, fieldType));
	}
	else if (LocalDateTime.class == fieldType) {
		return new LocalDateTimeParser(getFormatter(annotation, fieldType));
	}
	else {
		return new DateTimeParser(getFormatter(annotation, fieldType));
	}
}
 
Example #12
Source File: FormattingConversionServiceFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public Parser<?> getParser(SpecialInt annotation, Class<?> fieldType) {
	return new Parser<Integer>() {
		@Override
		public Integer parse(String text, Locale locale) throws ParseException {
			return Integer.parseInt(text.substring(1));
		}
	};
}
 
Example #13
Source File: JodaDateTimeFormatAnnotationFormatterFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Parser<?> getParser(DateTimeFormat annotation, Class<?> fieldType) {
	if (LocalDate.class == fieldType) {
		return new LocalDateParser(getFormatter(annotation, fieldType));
	}
	else if (LocalTime.class == fieldType) {
		return new LocalTimeParser(getFormatter(annotation, fieldType));
	}
	else if (LocalDateTime.class == fieldType) {
		return new LocalDateTimeParser(getFormatter(annotation, fieldType));
	}
	else {
		return new DateTimeParser(getFormatter(annotation, fieldType));
	}
}
 
Example #14
Source File: EntityFormatAnnotationFormatterFactoryTest.java    From springlets with Apache License 2.0 5 votes vote down vote up
/**
 * Test method for {@link io.springlets.format.EntityFormatAnnotationFormatterFactory#getParser(io.springlets.format.EntityFormat, java.lang.Class)}.
 */
@Test
public void shouldReturnEntityParser() {
  // Exercise
  Parser<?> parser = factory.getParser(format, Object.class);

  // Verify
  assertThat(parser).isNotNull();
}
 
Example #15
Source File: FormattingConversionServiceFactoryBeanTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public Parser<?> getParser(SpecialInt annotation, Class<?> fieldType) {
	assertEquals("aliased", annotation.value());
	assertEquals("aliased", annotation.alias());
	return new Parser<Integer>() {
		@Override
		public Integer parse(String text, Locale locale) throws ParseException {
			return Integer.parseInt(text.substring(1));
		}
	};
}
 
Example #16
Source File: DateTimeFormatAnnotationFormatterFactory.java    From JDeSurvey with GNU Affero General Public License v3.0 5 votes vote down vote up
public Parser<Date> getParser(DateTimeFormat annotation, Class<?> fieldType) {
	if (Date.class.isAssignableFrom(fieldType)) {
		return new DatePrinter(annotation);
	} else {
		return null;
	}
}
 
Example #17
Source File: JodaTimeFormatterRegistrar.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void addFormatterForFields(FormatterRegistry registry, Printer<?> printer,
		Parser<?> parser, Class<?>... fieldTypes) {

	for (Class<?> fieldType : fieldTypes) {
		registry.addFormatterForFieldType(fieldType, printer, parser);
	}
}
 
Example #18
Source File: JodaTimeFormatterRegistrar.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void addFormatterForFields(FormatterRegistry registry, Printer<?> printer,
		Parser<?> parser, Class<?>... fieldTypes) {

	for (Class<?> fieldType : fieldTypes) {
		registry.addFormatterForFieldType(fieldType, printer, parser);
	}
}
 
Example #19
Source File: JodaDateTimeFormatAnnotationFormatterFactory.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public Parser<?> getParser(DateTimeFormat annotation, Class<?> fieldType) {
	if (LocalDate.class == fieldType) {
		return new LocalDateParser(getFormatter(annotation, fieldType));
	}
	else if (LocalTime.class == fieldType) {
		return new LocalTimeParser(getFormatter(annotation, fieldType));
	}
	else if (LocalDateTime.class == fieldType) {
		return new LocalDateTimeParser(getFormatter(annotation, fieldType));
	}
	else {
		return new DateTimeParser(getFormatter(annotation, fieldType));
	}
}
 
Example #20
Source File: Jsr354NumberFormatAnnotationFormatterFactory.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public Parser<MonetaryAmount> getParser(NumberFormat annotation, Class<?> fieldType) {
	return configureFormatterFrom(annotation);
}
 
Example #21
Source File: FormattingConversionService.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public ParserConverter(Class<?> fieldType, Parser<?> parser, ConversionService conversionService) {
	this.fieldType = fieldType;
	this.parser = parser;
	this.conversionService = conversionService;
}
 
Example #22
Source File: FormattingConversionService.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public void addFormatterForFieldType(Class<?> fieldType, Printer<?> printer, Parser<?> parser) {
	addConverter(new PrinterConverter(fieldType, printer, this));
	addConverter(new ParserConverter(fieldType, parser, this));
}
 
Example #23
Source File: ControlBinder.java    From jdal with Apache License 2.0 4 votes vote down vote up
protected Parser<?> getParser() {
	return FormatUtils.getParser(getModel().getClass(), propertyName);
}
 
Example #24
Source File: Jsr310DateTimeFormatAnnotationFormatterFactory.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Parser<?> getParser(DateTimeFormat annotation, Class<?> fieldType) {
	DateTimeFormatter formatter = getFormatter(annotation, fieldType);
	return new TemporalAccessorParser((Class<? extends TemporalAccessor>) fieldType, formatter);
}
 
Example #25
Source File: DateTimeFormatAnnotationFormatterFactory.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public Parser<?> getParser(DateTimeFormat annotation, Class<?> fieldType) {
	return getFormatter(annotation, fieldType);
}
 
Example #26
Source File: MaskFormatAnnotationFormatterFactory.java    From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
@Override
public Parser<?> getParser(MaskFormat annotation, Class<?> fieldType) {
	return new MaskFormatter(annotation.value());
}
 
Example #27
Source File: CodeFormatAnnotationFormatterFactory.java    From wallride with Apache License 2.0 4 votes vote down vote up
@Override
public Parser<?> getParser(CodeFormat annotation, Class<?> fieldType) {
	return getFormatter(annotation, fieldType);
}
 
Example #28
Source File: PeriodFormatAnnotationFactory.java    From jdal with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Parser<?> getParser(PeriodFormat annotation, Class<?> fieldType) {
	return getFormatter(annotation, fieldType);
}
 
Example #29
Source File: NumberFormatAnnotationFormatterFactory.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public Parser<Number> getParser(NumberFormat annotation, Class<?> fieldType) {
	return configureFormatterFrom(annotation);
}
 
Example #30
Source File: FormattingConversionService.java    From java-technology-stack with MIT License 4 votes vote down vote up
public ParserConverter(Class<?> fieldType, Parser<?> parser, ConversionService conversionService) {
	this.fieldType = fieldType;
	this.parser = parser;
	this.conversionService = conversionService;
}