Java Code Examples for org.springframework.format.Parser

The following examples show how to use org.springframework.format.Parser. These examples are extracted from open source projects. 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 Project: spring-analysis-note   Source File: FormattingConversionService.java    License: 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 2
@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 3
@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 4
@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 Project: jdal   Source File: ControlBinder.java    License: 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 6
@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 7
Source Project: spring-analysis-note   Source File: JodaTimeFormatterRegistrar.java    License: 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 8
@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 9
@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 10
Source Project: java-technology-stack   Source File: JodaTimeFormatterRegistrar.java    License: 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
@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 12
@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 13
private void addFormatterForFields(FormatterRegistry registry, Printer<?> printer,
		Parser<?> parser, Class<?>... fieldTypes) {

	for (Class<?> fieldType : fieldTypes) {
		registry.addFormatterForFieldType(fieldType, printer, parser);
	}
}
 
Example 14
@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 15
private void addFormatterForFields(FormatterRegistry registry, Printer<?> printer,
		Parser<?> parser, Class<?>... fieldTypes) {

	for (Class<?> fieldType : fieldTypes) {
		registry.addFormatterForFieldType(fieldType, printer, parser);
	}
}
 
Example 16
@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 17
@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 18
/**
 * 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 19
public Parser<Date> getParser(DateTimeFormat annotation, Class<?> fieldType) {
	if (Date.class.isAssignableFrom(fieldType)) {
		return new DatePrinter(annotation);
	} else {
		return null;
	}
}
 
Example 20
@Override
public Parser<Number> getParser(NumberFormat annotation, Class<?> fieldType) {
	return configureFormatterFrom(annotation);
}
 
Example 21
@Override
public Parser<MonetaryAmount> getParser(NumberFormat annotation, Class<?> fieldType) {
	return configureFormatterFrom(annotation);
}
 
Example 22
Source Project: spring-analysis-note   Source File: FormattingConversionService.java    License: MIT License 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 Project: spring-analysis-note   Source File: FormattingConversionService.java    License: 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;
}
 
Example 24
@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
@Override
public Parser<?> getParser(DateTimeFormat annotation, Class<?> fieldType) {
	return getFormatter(annotation, fieldType);
}
 
Example 26
@Override
public Parser<Number> getParser(NumberFormat annotation, Class<?> fieldType) {
	return configureFormatterFrom(annotation);
}
 
Example 27
@Override
public Parser<MonetaryAmount> getParser(NumberFormat annotation, Class<?> fieldType) {
	return configureFormatterFrom(annotation);
}
 
Example 28
@Override
public void addFormatterForFieldType(Class<?> fieldType, Printer<?> printer, Parser<?> parser) {
	addConverter(new PrinterConverter(fieldType, printer, this));
	addConverter(new ParserConverter(fieldType, parser, this));
}
 
Example 29
public ParserConverter(Class<?> fieldType, Parser<?> parser, ConversionService conversionService) {
	this.fieldType = fieldType;
	this.parser = parser;
	this.conversionService = conversionService;
}
 
Example 30
@Override
@SuppressWarnings("unchecked")
public Parser<?> getParser(DateTimeFormat annotation, Class<?> fieldType) {
	DateTimeFormatter formatter = getFormatter(annotation, fieldType);
	return new TemporalAccessorParser((Class<? extends TemporalAccessor>) fieldType, formatter);
}