Java Code Examples for org.springframework.format.FormatterRegistrar

The following examples show how to use org.springframework.format.FormatterRegistrar. 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
private void registerFormatters(FormattingConversionService conversionService) {
	if (this.formatters != null) {
		for (Object formatter : this.formatters) {
			if (formatter instanceof Formatter<?>) {
				conversionService.addFormatter((Formatter<?>) formatter);
			}
			else if (formatter instanceof AnnotationFormatterFactory<?>) {
				conversionService.addFormatterForFieldAnnotation((AnnotationFormatterFactory<?>) formatter);
			}
			else {
				throw new IllegalArgumentException(
						"Custom formatters must be implementations of Formatter or AnnotationFormatterFactory");
			}
		}
	}
	if (this.formatterRegistrars != null) {
		for (FormatterRegistrar registrar : this.formatterRegistrars) {
			registrar.registerFormatters(conversionService);
		}
	}
}
 
Example 2
private void registerFormatters(FormattingConversionService conversionService) {
	if (this.formatters != null) {
		for (Object formatter : this.formatters) {
			if (formatter instanceof Formatter<?>) {
				conversionService.addFormatter((Formatter<?>) formatter);
			}
			else if (formatter instanceof AnnotationFormatterFactory<?>) {
				conversionService.addFormatterForFieldAnnotation((AnnotationFormatterFactory<?>) formatter);
			}
			else {
				throw new IllegalArgumentException(
						"Custom formatters must be implementations of Formatter or AnnotationFormatterFactory");
			}
		}
	}
	if (this.formatterRegistrars != null) {
		for (FormatterRegistrar registrar : this.formatterRegistrars) {
			registrar.registerFormatters(conversionService);
		}
	}
}
 
Example 3
private void registerFormatters() {
	if (this.formatters != null) {
		for (Object formatter : this.formatters) {
			if (formatter instanceof Formatter<?>) {
				this.conversionService.addFormatter((Formatter<?>) formatter);
			}
			else if (formatter instanceof AnnotationFormatterFactory<?>) {
				this.conversionService.addFormatterForFieldAnnotation((AnnotationFormatterFactory<?>) formatter);
			}
			else {
				throw new IllegalArgumentException(
						"Custom formatters must be implementations of Formatter or AnnotationFormatterFactory");
			}
		}
	}
	if (this.formatterRegistrars != null) {
		for (FormatterRegistrar registrar : this.formatterRegistrars) {
			registrar.registerFormatters(this.conversionService);
		}
	}
}
 
Example 4
private void registerFormatters() {
	if (this.formatters != null) {
		for (Object formatter : this.formatters) {
			if (formatter instanceof Formatter<?>) {
				this.conversionService.addFormatter((Formatter<?>) formatter);
			}
			else if (formatter instanceof AnnotationFormatterFactory<?>) {
				this.conversionService.addFormatterForFieldAnnotation((AnnotationFormatterFactory<?>) formatter);
			}
			else {
				throw new IllegalArgumentException(
						"Custom formatters must be implementations of Formatter or AnnotationFormatterFactory");
			}
		}
	}
	if (this.formatterRegistrars != null) {
		for (FormatterRegistrar registrar : this.formatterRegistrars) {
			registrar.registerFormatters(this.conversionService);
		}
	}
}
 
Example 5
@Test
public void testFormatterRegistrar() throws Exception {
	FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean();
	Set<FormatterRegistrar> registrars = new HashSet<>();
	registrars.add(new TestFormatterRegistrar());
	factory.setFormatterRegistrars(registrars);
	factory.afterPropertiesSet();
	FormattingConversionService fcs = factory.getObject();

	TestBean testBean = fcs.convert("5", TestBean.class);
	assertEquals(5, testBean.getSpecialInt());
	assertEquals("5", fcs.convert(testBean, String.class));
}
 
Example 6
@Test
public void testFormatterRegistrar() throws Exception {
	FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean();
	Set<FormatterRegistrar> registrars = new HashSet<>();
	registrars.add(new TestFormatterRegistrar());
	factory.setFormatterRegistrars(registrars);
	factory.afterPropertiesSet();
	FormattingConversionService fcs = factory.getObject();

	TestBean testBean = fcs.convert("5", TestBean.class);
	assertEquals(5, testBean.getSpecialInt());
	assertEquals("5", fcs.convert(testBean, String.class));
}
 
Example 7
@Test
public void testFormatterRegistrar() throws Exception {
	FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean();
	Set<FormatterRegistrar> registrars = new HashSet<FormatterRegistrar>();
	registrars.add(new TestFormatterRegistrar());
	factory.setFormatterRegistrars(registrars);
	factory.afterPropertiesSet();
	FormattingConversionService fcs = factory.getObject();

	TestBean testBean = fcs.convert("5", TestBean.class);
	assertEquals(5, testBean.getSpecialInt());
	assertEquals("5", fcs.convert(testBean, String.class));
}
 
Example 8
Source Project: DCMonitor   Source File: DateTimeConverter.java    License: MIT License 5 votes vote down vote up
@Bean
FormattingConversionServiceFactoryBean conversionService() {
  FormattingConversionServiceFactoryBean bean = new FormattingConversionServiceFactoryBean();
  Set<FormatterRegistrar> set = new HashSet<FormatterRegistrar>();
  JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
  registrar.setUseIsoFormat(true);
  set.add(registrar);
  bean.setFormatterRegistrars(set);
  return bean;
}
 
Example 9
/**
 * <p>Configure the set of FormatterRegistrars to invoke to register
 * Converters and Formatters in addition to those added declaratively
 * via {@link #setConverters(Set)} and {@link #setFormatters(Set)}.
 * <p>FormatterRegistrars are useful when registering multiple related
 * converters and formatters for a formatting category, such as Date
 * formatting. All types related needed to support the formatting
 * category can be registered from one place.
 * <p>FormatterRegistrars can also be used to register Formatters
 * indexed under a specific field type different from its own &lt;T&gt;,
 * or when registering a Formatter from a Printer/Parser pair.
 * @see FormatterRegistry#addFormatterForFieldType(Class, Formatter)
 * @see FormatterRegistry#addFormatterForFieldType(Class, Printer, Parser)
 */
public void setFormatterRegistrars(Set<FormatterRegistrar> formatterRegistrars) {
	this.formatterRegistrars = formatterRegistrars;
}
 
Example 10
/**
 * <p>Configure the set of FormatterRegistrars to invoke to register
 * Converters and Formatters in addition to those added declaratively
 * via {@link #setConverters(Set)} and {@link #setFormatters(Set)}.
 * <p>FormatterRegistrars are useful when registering multiple related
 * converters and formatters for a formatting category, such as Date
 * formatting. All types related needed to support the formatting
 * category can be registered from one place.
 * <p>FormatterRegistrars can also be used to register Formatters
 * indexed under a specific field type different from its own &lt;T&gt;,
 * or when registering a Formatter from a Printer/Parser pair.
 * @see FormatterRegistry#addFormatterForFieldType(Class, Formatter)
 * @see FormatterRegistry#addFormatterForFieldType(Class, Printer, Parser)
 */
public void setFormatterRegistrars(Set<FormatterRegistrar> formatterRegistrars) {
	this.formatterRegistrars = formatterRegistrars;
}
 
Example 11
/**
 * <p>Configure the set of FormatterRegistrars to invoke to register
 * Converters and Formatters in addition to those added declaratively
 * via {@link #setConverters(Set)} and {@link #setFormatters(Set)}.
 * <p>FormatterRegistrars are useful when registering multiple related
 * converters and formatters for a formatting category, such as Date
 * formatting. All types related needed to support the formatting
 * category can be registered from one place.
 * <p>FormatterRegistrars can also be used to register Formatters
 * indexed under a specific field type different from its own &lt;T&gt;,
 * or when registering a Formatter from a Printer/Parser pair.
 * @see FormatterRegistry#addFormatterForFieldType(Class, Formatter)
 * @see FormatterRegistry#addFormatterForFieldType(Class, Printer, Parser)
 */
public void setFormatterRegistrars(Set<FormatterRegistrar> formatterRegistrars) {
	this.formatterRegistrars = formatterRegistrars;
}
 
Example 12
/**
 * <p>Configure the set of FormatterRegistrars to invoke to register
 * Converters and Formatters in addition to those added declaratively
 * via {@link #setConverters(Set)} and {@link #setFormatters(Set)}.
 * <p>FormatterRegistrars are useful when registering multiple related
 * converters and formatters for a formatting category, such as Date
 * formatting. All types related needed to support the formatting
 * category can be registered from one place.
 * <p>FormatterRegistrars can also be used to register Formatters
 * indexed under a specific field type different from its own &lt;T&gt;,
 * or when registering a Formatter from a Printer/Parser pair.
 * @see FormatterRegistry#addFormatterForFieldType(Class, Formatter)
 * @see FormatterRegistry#addFormatterForFieldType(Class, Printer, Parser)
 */
public void setFormatterRegistrars(Set<FormatterRegistrar> formatterRegistrars) {
	this.formatterRegistrars = formatterRegistrars;
}