org.springframework.format.Formatter Java Examples

The following examples show how to use org.springframework.format.Formatter. 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: FormatUtils.java    From jdal with 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 #2
Source File: FormattingConversionServiceFactoryBean.java    From java-technology-stack with MIT License 6 votes vote down vote up
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
Source File: NumberFormatAnnotationFormatterFactory.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
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 #4
Source File: Jsr354NumberFormatAnnotationFormatterFactory.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
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 #5
Source File: FormattingConversionServiceFactoryBean.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
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 #6
Source File: Jsr354NumberFormatAnnotationFormatterFactory.java    From java-technology-stack with MIT License 6 votes vote down vote up
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 #7
Source File: NumberFormatAnnotationFormatterFactory.java    From java-technology-stack with MIT License 6 votes vote down vote up
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 #8
Source File: NumberFormatAnnotationFormatterFactory.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
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 #9
Source File: Jsr354NumberFormatAnnotationFormatterFactory.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
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 #10
Source File: FormattingConversionServiceFactoryBean.java    From spring-analysis-note with MIT License 6 votes vote down vote up
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 #11
Source File: FormattingConversionServiceFactoryBean.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
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 #12
Source File: FormatterUtils.java    From haven-platform with 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 #13
Source File: Jsr354NumberFormatAnnotationFormatterFactory.java    From spring-analysis-note with MIT License 6 votes vote down vote up
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 #14
Source File: NumberFormatAnnotationFormatterFactory.java    From spring-analysis-note with MIT License 6 votes vote down vote up
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 #15
Source File: FormattingConversionService.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
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 #16
Source File: DateTimeFormatAnnotationFormatterFactory.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
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 #17
Source File: DefaultMetatypeFormatterRegistry.java    From haven-platform with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <T> Formatter<T> getFormatter(Key<T> key) {
    Assert.notNull(key, "key is null");
    Formatter<?> formatter = map.get(key);
    if(formatter == null) {
        throw new RuntimeException("No registered formatters for: " + key);
    }
    return (Formatter<T>) formatter;
}
 
Example #18
Source File: DataBinder.java    From spring4-understanding with 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 #19
Source File: DataBinder.java    From spring4-understanding with 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 #20
Source File: FormatterUtils.java    From haven-platform with Apache License 2.0 5 votes vote down vote up
/**
 * Do invocation of specified formatter on text with default locale.
 * @param formatter
 * @param text allow null
 * @param <T>
 * @return parsed object or null if text is null
 */
public static <T> T parse(Formatter<T> formatter, String text) {
    if(text == null) {
        return null;
    }
    try {
        return formatter.parse(text, Locale.getDefault());
    } catch (ParseException e) {
        throw new IllegalArgumentException("Can not parse:'" + text + "'", e);
    }
}
 
Example #21
Source File: DateTimeFormatAnnotationFormatterFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
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 #22
Source File: FormattingConversionService.java    From lams with GNU General Public License v2.0 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 #23
Source File: ValidableFormatter.java    From haven-platform with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param formatter
 * @param objectValidator the callback in which do object validation after parsing and before printing
 * @param textValidator the callback in which do text validation before parsing and after printing
 */
public ValidableFormatter(Formatter<T> formatter, Callback<T> objectValidator, Callback<String> textValidator) {
    this.formatter = formatter;
    Assert.notNull(this.formatter, "formatter is null");
    this.objectValidator = objectValidator;
    this.textValidator = textValidator;
    Assert.isTrue(this.objectValidator != null || this.textValidator != null, "text and object validators is null");
}
 
Example #24
Source File: DataBinder.java    From spring-analysis-note with 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 #25
Source File: DataBinder.java    From lams with 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 #26
Source File: DataBinder.java    From lams with 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 #27
Source File: DateTimeFormatAnnotationFormatterFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
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 #28
Source File: FormattingConversionService.java    From java-technology-stack with 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 #29
Source File: DataBinder.java    From spring-analysis-note with 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 #30
Source File: DataBinder.java    From java-technology-stack with 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);
		}
	}
}