Java Code Examples for org.springframework.format.Formatter

The following examples show how to use org.springframework.format.Formatter. 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 Formatter<Number> configureFormatterFrom(NumberFormat annotation) {
	String pattern = resolveEmbeddedValue(annotation.pattern());
	if (StringUtils.hasLength(pattern)) {
		return new NumberStyleFormatter(pattern);
	}
	else {
		Style style = annotation.style();
		if (style == Style.CURRENCY) {
			return new CurrencyStyleFormatter();
		}
		else if (style == Style.PERCENT) {
			return new PercentStyleFormatter();
		}
		else {
			return new NumberStyleFormatter();
		}
	}
}
 
Example 2
private Formatter<MonetaryAmount> configureFormatterFrom(NumberFormat annotation) {
	String pattern = resolveEmbeddedValue(annotation.pattern());
	if (StringUtils.hasLength(pattern)) {
		return new PatternDecoratingFormatter(pattern);
	}
	else {
		Style style = annotation.style();
		if (style == Style.NUMBER) {
			return new NumberDecoratingFormatter(new NumberStyleFormatter());
		}
		else if (style == Style.PERCENT) {
			return new NumberDecoratingFormatter(new PercentStyleFormatter());
		}
		else {
			return new NumberDecoratingFormatter(new CurrencyStyleFormatter());
		}
	}
}
 
Example 3
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 4
private Formatter<Number> configureFormatterFrom(NumberFormat annotation) {
	String pattern = resolveEmbeddedValue(annotation.pattern());
	if (StringUtils.hasLength(pattern)) {
		return new NumberStyleFormatter(pattern);
	}
	else {
		Style style = annotation.style();
		if (style == Style.CURRENCY) {
			return new CurrencyStyleFormatter();
		}
		else if (style == Style.PERCENT) {
			return new PercentStyleFormatter();
		}
		else {
			return new NumberStyleFormatter();
		}
	}
}
 
Example 5
private Formatter<MonetaryAmount> configureFormatterFrom(NumberFormat annotation) {
	String pattern = resolveEmbeddedValue(annotation.pattern());
	if (StringUtils.hasLength(pattern)) {
		return new PatternDecoratingFormatter(pattern);
	}
	else {
		Style style = annotation.style();
		if (style == Style.NUMBER) {
			return new NumberDecoratingFormatter(new NumberStyleFormatter());
		}
		else if (style == Style.PERCENT) {
			return new NumberDecoratingFormatter(new PercentStyleFormatter());
		}
		else {
			return new NumberDecoratingFormatter(new CurrencyStyleFormatter());
		}
	}
}
 
Example 6
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 7
private Formatter<Number> configureFormatterFrom(NumberFormat annotation) {
	if (StringUtils.hasLength(annotation.pattern())) {
		return new NumberStyleFormatter(resolveEmbeddedValue(annotation.pattern()));
	}
	else {
		Style style = annotation.style();
		if (style == Style.CURRENCY) {
			return new CurrencyStyleFormatter();
		}
		else if (style == Style.PERCENT) {
			return new PercentStyleFormatter();
		}
		else {
			return new NumberStyleFormatter();
		}
	}
}
 
Example 8
private Formatter<MonetaryAmount> configureFormatterFrom(NumberFormat annotation) {
	if (StringUtils.hasLength(annotation.pattern())) {
		return new PatternDecoratingFormatter(resolveEmbeddedValue(annotation.pattern()));
	}
	else {
		Style style = annotation.style();
		if (style == Style.NUMBER) {
			return new NumberDecoratingFormatter(new NumberStyleFormatter());
		}
		else if (style == Style.PERCENT) {
			return new NumberDecoratingFormatter(new PercentStyleFormatter());
		}
		else {
			return new NumberDecoratingFormatter(new CurrencyStyleFormatter());
		}
	}
}
 
Example 9
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 10
private Formatter<Number> configureFormatterFrom(NumberFormat annotation) {
	if (StringUtils.hasLength(annotation.pattern())) {
		return new NumberStyleFormatter(resolveEmbeddedValue(annotation.pattern()));
	}
	else {
		Style style = annotation.style();
		if (style == Style.CURRENCY) {
			return new CurrencyStyleFormatter();
		}
		else if (style == Style.PERCENT) {
			return new PercentStyleFormatter();
		}
		else {
			return new NumberStyleFormatter();
		}
	}
}
 
Example 11
private Formatter<MonetaryAmount> configureFormatterFrom(NumberFormat annotation) {
	if (StringUtils.hasLength(annotation.pattern())) {
		return new PatternDecoratingFormatter(resolveEmbeddedValue(annotation.pattern()));
	}
	else {
		Style style = annotation.style();
		if (style == Style.NUMBER) {
			return new NumberDecoratingFormatter(new NumberStyleFormatter());
		}
		else if (style == Style.PERCENT) {
			return new NumberDecoratingFormatter(new PercentStyleFormatter());
		}
		else {
			return new NumberDecoratingFormatter(new CurrencyStyleFormatter());
		}
	}
}
 
Example 12
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 13
Source Project: haven-platform   Source File: FormatterUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Extract handled types from all possible sources (annotation and interface).
 * @param formatter
 * @return
 */
@SuppressWarnings("unchecked")
public static Set<Key<?>> getHandledMetatypes(Formatter<?> formatter) {
    Assert.notNull(formatter, "formatter is null");
    Set<Key<?>> set = new HashSet<>();
    if(formatter instanceof SelfDescribedFormatter) {
        set.addAll(((SelfDescribedFormatter) formatter).getHandledMetatypes());
    }
    FormatterInfo formatterInfo = AnnotationUtils.findAnnotation(formatter.getClass(), FormatterInfo.class);
    if(formatterInfo != null) {
        for(MetatypeInfo metatypeInfo: formatterInfo.metatypes()) {
            set.add(MetatypeUtils.toKey(metatypeInfo));
        }
    }
    return set;
}
 
Example 14
Source Project: jdal   Source File: FormatUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Get a formatter for class and property name
 * @param clazz the class
 * @param propertyName the property name
 * @return the formatter or null if none
 */
public static Formatter<?> getFormatter(Class<?> clazz, String propertyName) {
	PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(clazz, propertyName);
	if (pd != null) {
		NumberFormat format = getAnnotation(pd, NumberFormat.class);
		if (format != null) {
			return (Formatter<?>) numberFormatFactory.getPrinter(format, pd.getPropertyType());
		}
		
		PeriodFormat periodFormat = getAnnotation(pd, PeriodFormat.class);
		if (periodFormat != null)
			return new PeriodFormatter();
	}
	
	return null;
}
 
Example 15
Source Project: spring-analysis-note   Source File: DataBinder.java    License: MIT License 5 votes vote down vote up
/**
 * Add a custom formatter for the field type specified in {@link Formatter} class,
 * applying it to the specified fields only, if any, or otherwise to all fields.
 * <p>Registers a corresponding {@link PropertyEditor} adapter underneath the covers.
 * @param formatter the formatter to add, generically declared for a specific type
 * @param fields the fields to apply the formatter to, or none if to be applied to all
 * @since 4.2
 * @see #registerCustomEditor(Class, String, PropertyEditor)
 */
public void addCustomFormatter(Formatter<?> formatter, String... fields) {
	FormatterPropertyEditorAdapter adapter = new FormatterPropertyEditorAdapter(formatter);
	Class<?> fieldType = adapter.getFieldType();
	if (ObjectUtils.isEmpty(fields)) {
		getPropertyEditorRegistry().registerCustomEditor(fieldType, adapter);
	}
	else {
		for (String field : fields) {
			getPropertyEditorRegistry().registerCustomEditor(fieldType, field, adapter);
		}
	}
}
 
Example 16
Source Project: spring-analysis-note   Source File: DataBinder.java    License: MIT License 5 votes vote down vote up
/**
 * Add a custom formatter, applying it to the specified field types only, if any,
 * or otherwise to all fields matching the {@link Formatter}-declared type.
 * <p>Registers a corresponding {@link PropertyEditor} adapter underneath the covers.
 * @param formatter the formatter to add (does not need to generically declare a
 * field type if field types are explicitly specified as parameters)
 * @param fieldTypes the field types to apply the formatter to, or none if to be
 * derived from the given {@link Formatter} implementation class
 * @since 4.2
 * @see #registerCustomEditor(Class, PropertyEditor)
 */
public void addCustomFormatter(Formatter<?> formatter, Class<?>... fieldTypes) {
	FormatterPropertyEditorAdapter adapter = new FormatterPropertyEditorAdapter(formatter);
	if (ObjectUtils.isEmpty(fieldTypes)) {
		getPropertyEditorRegistry().registerCustomEditor(adapter.getFieldType(), adapter);
	}
	else {
		for (Class<?> fieldType : fieldTypes) {
			getPropertyEditorRegistry().registerCustomEditor(fieldType, adapter);
		}
	}
}
 
Example 17
Source Project: spring-analysis-note   Source File: FormattingConversionService.java    License: MIT License 5 votes vote down vote up
static Class<?> getFieldType(Formatter<?> formatter) {
	Class<?> fieldType = GenericTypeResolver.resolveTypeArgument(formatter.getClass(), Formatter.class);
	if (fieldType == null && formatter instanceof DecoratingProxy) {
		fieldType = GenericTypeResolver.resolveTypeArgument(
				((DecoratingProxy) formatter).getDecoratedClass(), Formatter.class);
	}
	if (fieldType == null) {
		throw new IllegalArgumentException("Unable to extract the parameterized field type from Formatter [" +
				formatter.getClass().getName() + "]; does the class parameterize the <T> generic type?");
	}
	return fieldType;
}
 
Example 18
protected Formatter<Date> getFormatter(DateTimeFormat annotation, Class<?> fieldType) {
	DateFormatter formatter = new DateFormatter();
	String style = resolveEmbeddedValue(annotation.style());
	if (StringUtils.hasLength(style)) {
		formatter.setStylePattern(style);
	}
	formatter.setIso(annotation.iso());
	String pattern = resolveEmbeddedValue(annotation.pattern());
	if (StringUtils.hasLength(pattern)) {
		formatter.setPattern(pattern);
	}
	return formatter;
}
 
Example 19
Source Project: java-technology-stack   Source File: DataBinder.java    License: MIT License 5 votes vote down vote up
/**
 * Add a custom formatter for the field type specified in {@link Formatter} class,
 * applying it to the specified fields only, if any, or otherwise to all fields.
 * <p>Registers a corresponding {@link PropertyEditor} adapter underneath the covers.
 * @param formatter the formatter to add, generically declared for a specific type
 * @param fields the fields to apply the formatter to, or none if to be applied to all
 * @since 4.2
 * @see #registerCustomEditor(Class, String, PropertyEditor)
 */
public void addCustomFormatter(Formatter<?> formatter, String... fields) {
	FormatterPropertyEditorAdapter adapter = new FormatterPropertyEditorAdapter(formatter);
	Class<?> fieldType = adapter.getFieldType();
	if (ObjectUtils.isEmpty(fields)) {
		getPropertyEditorRegistry().registerCustomEditor(fieldType, adapter);
	}
	else {
		for (String field : fields) {
			getPropertyEditorRegistry().registerCustomEditor(fieldType, field, adapter);
		}
	}
}
 
Example 20
Source Project: java-technology-stack   Source File: DataBinder.java    License: MIT License 5 votes vote down vote up
/**
 * Add a custom formatter, applying it to the specified field types only, if any,
 * or otherwise to all fields matching the {@link Formatter}-declared type.
 * <p>Registers a corresponding {@link PropertyEditor} adapter underneath the covers.
 * @param formatter the formatter to add (does not need to generically declare a
 * field type if field types are explicitly specified as parameters)
 * @param fieldTypes the field types to apply the formatter to, or none if to be
 * derived from the given {@link Formatter} implementation class
 * @since 4.2
 * @see #registerCustomEditor(Class, PropertyEditor)
 */
public void addCustomFormatter(Formatter<?> formatter, Class<?>... fieldTypes) {
	FormatterPropertyEditorAdapter adapter = new FormatterPropertyEditorAdapter(formatter);
	if (ObjectUtils.isEmpty(fieldTypes)) {
		getPropertyEditorRegistry().registerCustomEditor(adapter.getFieldType(), adapter);
	}
	else {
		for (Class<?> fieldType : fieldTypes) {
			getPropertyEditorRegistry().registerCustomEditor(fieldType, adapter);
		}
	}
}
 
Example 21
static Class<?> getFieldType(Formatter<?> formatter) {
	Class<?> fieldType = GenericTypeResolver.resolveTypeArgument(formatter.getClass(), Formatter.class);
	if (fieldType == null && formatter instanceof DecoratingProxy) {
		fieldType = GenericTypeResolver.resolveTypeArgument(
				((DecoratingProxy) formatter).getDecoratedClass(), Formatter.class);
	}
	if (fieldType == null) {
		throw new IllegalArgumentException("Unable to extract the parameterized field type from Formatter [" +
				formatter.getClass().getName() + "]; does the class parameterize the <T> generic type?");
	}
	return fieldType;
}
 
Example 22
protected Formatter<Date> getFormatter(DateTimeFormat annotation, Class<?> fieldType) {
	DateFormatter formatter = new DateFormatter();
	String style = resolveEmbeddedValue(annotation.style());
	if (StringUtils.hasLength(style)) {
		formatter.setStylePattern(style);
	}
	formatter.setIso(annotation.iso());
	String pattern = resolveEmbeddedValue(annotation.pattern());
	if (StringUtils.hasLength(pattern)) {
		formatter.setPattern(pattern);
	}
	return formatter;
}
 
Example 23
Source Project: lams   Source File: DataBinder.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Add a custom formatter for the field type specified in {@link Formatter} class,
 * applying it to the specified fields only, if any, or otherwise to all fields.
 * <p>Registers a corresponding {@link PropertyEditor} adapter underneath the covers.
 * @param formatter the formatter to add, generically declared for a specific type
 * @param fields the fields to apply the formatter to, or none if to be applied to all
 * @since 4.2
 * @see #registerCustomEditor(Class, String, PropertyEditor)
 */
public void addCustomFormatter(Formatter<?> formatter, String... fields) {
	FormatterPropertyEditorAdapter adapter = new FormatterPropertyEditorAdapter(formatter);
	Class<?> fieldType = adapter.getFieldType();
	if (ObjectUtils.isEmpty(fields)) {
		getPropertyEditorRegistry().registerCustomEditor(fieldType, adapter);
	}
	else {
		for (String field : fields) {
			getPropertyEditorRegistry().registerCustomEditor(fieldType, field, adapter);
		}
	}
}
 
Example 24
Source Project: lams   Source File: DataBinder.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Add a custom formatter, applying it to the specified field types only, if any,
 * or otherwise to all fields matching the {@link Formatter}-declared type.
 * <p>Registers a corresponding {@link PropertyEditor} adapter underneath the covers.
 * @param formatter the formatter to add (does not need to generically declare a
 * field type if field types are explicitly specified as parameters)
 * @param fieldTypes the field types to apply the formatter to, or none if to be
 * derived from the given {@link Formatter} implementation class
 * @since 4.2
 * @see #registerCustomEditor(Class, PropertyEditor)
 */
public void addCustomFormatter(Formatter<?> formatter, Class<?>... fieldTypes) {
	FormatterPropertyEditorAdapter adapter = new FormatterPropertyEditorAdapter(formatter);
	if (ObjectUtils.isEmpty(fieldTypes)) {
		getPropertyEditorRegistry().registerCustomEditor(adapter.getFieldType(), adapter);
	}
	else {
		for (Class<?> fieldType : fieldTypes) {
			getPropertyEditorRegistry().registerCustomEditor(fieldType, adapter);
		}
	}
}
 
Example 25
static Class<?> getFieldType(Formatter<?> formatter) {
	Class<?> fieldType = GenericTypeResolver.resolveTypeArgument(formatter.getClass(), Formatter.class);
	if (fieldType == null && formatter instanceof DecoratingProxy) {
		fieldType = GenericTypeResolver.resolveTypeArgument(
				((DecoratingProxy) formatter).getDecoratedClass(), Formatter.class);
	}
	if (fieldType == null) {
		throw new IllegalArgumentException("Unable to extract the parameterized field type from Formatter [" +
				formatter.getClass().getName() + "]; does the class parameterize the <T> generic type?");
	}
	return fieldType;
}
 
Example 26
protected Formatter<Date> getFormatter(DateTimeFormat annotation, Class<?> fieldType) {
	DateFormatter formatter = new DateFormatter();
	formatter.setStylePattern(resolveEmbeddedValue(annotation.style()));
	formatter.setIso(annotation.iso());
	formatter.setPattern(resolveEmbeddedValue(annotation.pattern()));
	return formatter;
}
 
Example 27
Source Project: spring4-understanding   Source File: DataBinder.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Add a custom formatter for the field type specified in {@link Formatter} class,
 * applying it to the specified fields only, if any, or otherwise to all fields.
 * <p>Registers a corresponding {@link PropertyEditor} adapter underneath the covers.
 * @param formatter the formatter to add, generically declared for a specific type
 * @param fields the fields to apply the formatter to, or none if to be applied to all
 * @since 4.2
 * @see #registerCustomEditor(Class, String, PropertyEditor)
 */
public void addCustomFormatter(Formatter<?> formatter, String... fields) {
	FormatterPropertyEditorAdapter adapter = new FormatterPropertyEditorAdapter(formatter);
	Class<?> fieldType = adapter.getFieldType();
	if (ObjectUtils.isEmpty(fields)) {
		getPropertyEditorRegistry().registerCustomEditor(fieldType, adapter);
	}
	else {
		for (String field : fields) {
			getPropertyEditorRegistry().registerCustomEditor(fieldType, field, adapter);
		}
	}
}
 
Example 28
Source Project: spring4-understanding   Source File: DataBinder.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Add a custom formatter, applying it to the specified field types only, if any,
 * or otherwise to all fields matching the {@link Formatter}-declared type.
 * <p>Registers a corresponding {@link PropertyEditor} adapter underneath the covers.
 * @param formatter the formatter to add (does not need to generically declare a
 * field type if field types are explicitly specified as parameters)
 * @param fieldTypes the field types to apply the formatter to, or none if to be
 * derived from the given {@link Formatter} implementation class
 * @since 4.2
 * @see #registerCustomEditor(Class, PropertyEditor)
 */
public void addCustomFormatter(Formatter<?> formatter, Class<?>... fieldTypes) {
	FormatterPropertyEditorAdapter adapter = new FormatterPropertyEditorAdapter(formatter);
	if (ObjectUtils.isEmpty(fieldTypes)) {
		getPropertyEditorRegistry().registerCustomEditor(adapter.getFieldType(), adapter);
	}
	else {
		for (Class<?> fieldType : fieldTypes) {
			getPropertyEditorRegistry().registerCustomEditor(fieldType, adapter);
		}
	}
}
 
Example 29
static Class<?> getFieldType(Formatter<?> formatter) {
	Class<?> fieldType = GenericTypeResolver.resolveTypeArgument(formatter.getClass(), Formatter.class);
	if (fieldType == null) {
		throw new IllegalArgumentException("Unable to extract parameterized field type argument from Formatter [" +
				formatter.getClass().getName() + "]; does the formatter parameterize the <T> generic type?");
	}
	return fieldType;
}
 
Example 30
protected Formatter<Date> getFormatter(DateTimeFormat annotation, Class<?> fieldType) {
	DateFormatter formatter = new DateFormatter();
	formatter.setStylePattern(resolveEmbeddedValue(annotation.style()));
	formatter.setIso(annotation.iso());
	formatter.setPattern(resolveEmbeddedValue(annotation.pattern()));
	return formatter;
}