Java Code Examples for org.springframework.format.Printer

The following examples show how to use org.springframework.format.Printer. 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 = sourceType.getAnnotation(this.annotationType);
	if (ann == null) {
		throw new IllegalStateException(
				"Expected [" + this.annotationType.getName() + "] to be present on " + sourceType);
	}
	AnnotationConverterKey converterKey = new AnnotationConverterKey(ann, sourceType.getObjectType());
	GenericConverter converter = cachedPrinters.get(converterKey);
	if (converter == null) {
		Printer<?> printer = this.annotationFormatterFactory.getPrinter(
				converterKey.getAnnotation(), converterKey.getFieldType());
		converter = new PrinterConverter(this.fieldType, printer, FormattingConversionService.this);
		cachedPrinters.put(converterKey, converter);
	}
	return converter.convert(source, sourceType, targetType);
}
 
Example 2
@Override
public Printer<?> getPrinter(DateTimeFormat annotation, Class<?> fieldType) {
	DateTimeFormatter formatter = getFormatter(annotation, fieldType);

	// Efficient ISO_LOCAL_* variants for printing since they are twice as fast...
	if (formatter == DateTimeFormatter.ISO_DATE) {
		if (isLocal(fieldType)) {
			formatter = DateTimeFormatter.ISO_LOCAL_DATE;
		}
	}
	else if (formatter == DateTimeFormatter.ISO_TIME) {
		if (isLocal(fieldType)) {
			formatter = DateTimeFormatter.ISO_LOCAL_TIME;
		}
	}
	else if (formatter == DateTimeFormatter.ISO_DATE_TIME) {
		if (isLocal(fieldType)) {
			formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
		}
	}

	return new TemporalAccessorPrinter(formatter);
}
 
Example 3
@Override
@SuppressWarnings("unchecked")
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	Annotation ann = sourceType.getAnnotation(this.annotationType);
	if (ann == null) {
		throw new IllegalStateException(
				"Expected [" + this.annotationType.getName() + "] to be present on " + sourceType);
	}
	AnnotationConverterKey converterKey = new AnnotationConverterKey(ann, sourceType.getObjectType());
	GenericConverter converter = cachedPrinters.get(converterKey);
	if (converter == null) {
		Printer<?> printer = this.annotationFormatterFactory.getPrinter(
				converterKey.getAnnotation(), converterKey.getFieldType());
		converter = new PrinterConverter(this.fieldType, printer, FormattingConversionService.this);
		cachedPrinters.put(converterKey, converter);
	}
	return converter.convert(source, sourceType, targetType);
}
 
Example 4
@Override
public Printer<?> getPrinter(DateTimeFormat annotation, Class<?> fieldType) {
	DateTimeFormatter formatter = getFormatter(annotation, fieldType);

	// Efficient ISO_LOCAL_* variants for printing since they are twice as fast...
	if (formatter == DateTimeFormatter.ISO_DATE) {
		if (isLocal(fieldType)) {
			formatter = DateTimeFormatter.ISO_LOCAL_DATE;
		}
	}
	else if (formatter == DateTimeFormatter.ISO_TIME) {
		if (isLocal(fieldType)) {
			formatter = DateTimeFormatter.ISO_LOCAL_TIME;
		}
	}
	else if (formatter == DateTimeFormatter.ISO_DATE_TIME) {
		if (isLocal(fieldType)) {
			formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
		}
	}

	return new TemporalAccessorPrinter(formatter);
}
 
Example 5
@Override
@SuppressWarnings("unchecked")
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	Annotation ann = sourceType.getAnnotation(this.annotationType);
	if (ann == null) {
		throw new IllegalStateException(
				"Expected [" + this.annotationType.getName() + "] to be present on " + sourceType);
	}
	AnnotationConverterKey converterKey = new AnnotationConverterKey(ann, sourceType.getObjectType());
	GenericConverter converter = cachedPrinters.get(converterKey);
	if (converter == null) {
		Printer<?> printer = this.annotationFormatterFactory.getPrinter(
				converterKey.getAnnotation(), converterKey.getFieldType());
		converter = new PrinterConverter(this.fieldType, printer, FormattingConversionService.this);
		cachedPrinters.put(converterKey, converter);
	}
	return converter.convert(source, sourceType, targetType);
}
 
Example 6
@Override
public Printer<?> getPrinter(DateTimeFormat annotation, Class<?> fieldType) {
	DateTimeFormatter formatter = getFormatter(annotation, fieldType);

	// Efficient ISO_LOCAL_* variants for printing since they are twice as fast...
	if (formatter == DateTimeFormatter.ISO_DATE) {
		if (isLocal(fieldType)) {
			formatter = DateTimeFormatter.ISO_LOCAL_DATE;
		}
	}
	else if (formatter == DateTimeFormatter.ISO_TIME) {
		if (isLocal(fieldType)) {
			formatter = DateTimeFormatter.ISO_LOCAL_TIME;
		}
	}
	else if (formatter == DateTimeFormatter.ISO_DATE_TIME) {
		if (isLocal(fieldType)) {
			formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
		}
	}

	return new TemporalAccessorPrinter(formatter);
}
 
Example 7
@Override
@SuppressWarnings("unchecked")
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	Annotation ann = sourceType.getAnnotation(this.annotationType);
	if (ann == null) {
		throw new IllegalStateException(
				"Expected [" + this.annotationType.getName() + "] to be present on " + sourceType);
	}
	AnnotationConverterKey converterKey = new AnnotationConverterKey(ann, sourceType.getObjectType());
	GenericConverter converter = cachedPrinters.get(converterKey);
	if (converter == null) {
		Printer<?> printer = this.annotationFormatterFactory.getPrinter(
				converterKey.getAnnotation(), converterKey.getFieldType());
		converter = new PrinterConverter(this.fieldType, printer, FormattingConversionService.this);
		cachedPrinters.put(converterKey, converter);
	}
	return converter.convert(source, sourceType, targetType);
}
 
Example 8
@Override
public Printer<?> getPrinter(DateTimeFormat annotation, Class<?> fieldType) {
	DateTimeFormatter formatter = getFormatter(annotation, fieldType);
	if (ReadablePartial.class.isAssignableFrom(fieldType)) {
		return new ReadablePartialPrinter(formatter);
	}
	else if (ReadableInstant.class.isAssignableFrom(fieldType) || Calendar.class.isAssignableFrom(fieldType)) {
		// assumes Calendar->ReadableInstant converter is registered
		return new ReadableInstantPrinter(formatter);
	}
	else {
		// assumes Date->Long converter is registered
		return new MillisecondInstantPrinter(formatter);
	}
}
 
Example 9
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 10
@Override
public Printer<?> getPrinter(SpecialInt annotation, Class<?> fieldType) {
	assertEquals("aliased", annotation.value());
	assertEquals("aliased", annotation.alias());
	return new Printer<Integer>() {
		@Override
		public String print(Integer object, Locale locale) {
			return ":" + object.toString();
		}
	};
}
 
Example 11
@Override
public Printer<?> getPrinter(DateTimeFormat annotation, Class<?> fieldType) {
	DateTimeFormatter formatter = getFormatter(annotation, fieldType);
	if (ReadablePartial.class.isAssignableFrom(fieldType)) {
		return new ReadablePartialPrinter(formatter);
	}
	else if (ReadableInstant.class.isAssignableFrom(fieldType) || Calendar.class.isAssignableFrom(fieldType)) {
		// assumes Calendar->ReadableInstant converter is registered
		return new ReadableInstantPrinter(formatter);
	}
	else {
		// assumes Date->Long converter is registered
		return new MillisecondInstantPrinter(formatter);
	}
}
 
Example 12
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 13
@Override
public Printer<?> getPrinter(SpecialInt annotation, Class<?> fieldType) {
	assertEquals("aliased", annotation.value());
	assertEquals("aliased", annotation.alias());
	return new Printer<Integer>() {
		@Override
		public String print(Integer object, Locale locale) {
			return ":" + object.toString();
		}
	};
}
 
Example 14
@Override
public Printer<?> getPrinter(DateTimeFormat annotation, Class<?> fieldType) {
	DateTimeFormatter formatter = getFormatter(annotation, fieldType);
	if (ReadablePartial.class.isAssignableFrom(fieldType)) {
		return new ReadablePartialPrinter(formatter);
	}
	else if (ReadableInstant.class.isAssignableFrom(fieldType) || Calendar.class.isAssignableFrom(fieldType)) {
		// assumes Calendar->ReadableInstant converter is registered
		return new ReadableInstantPrinter(formatter);
	}
	else {
		// assumes Date->Long converter is registered
		return new MillisecondInstantPrinter(formatter);
	}
}
 
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 Printer<?> getPrinter(DateTimeFormat annotation, Class<?> fieldType) {
	DateTimeFormatter formatter = getFormatter(annotation, fieldType);
	if (ReadablePartial.class.isAssignableFrom(fieldType)) {
		return new ReadablePartialPrinter(formatter);
	}
	else if (ReadableInstant.class.isAssignableFrom(fieldType) || Calendar.class.isAssignableFrom(fieldType)) {
		// assumes Calendar->ReadableInstant converter is registered
		return new ReadableInstantPrinter(formatter);
	}
	else {
		// assumes Date->Long converter is registered
		return new MillisecondInstantPrinter(formatter);
	}
}
 
Example 17
private void addFormatterForFields(FormatterRegistry registry, Printer<?> printer,
		Parser<?> parser, Class<?>... fieldTypes) {

	for (Class<?> fieldType : fieldTypes) {
		registry.addFormatterForFieldType(fieldType, printer, parser);
	}
}
 
Example 18
@Override
public Printer<?> getPrinter(SpecialInt annotation, Class<?> fieldType) {
	return new Printer<Integer>() {
		@Override
		public String print(Integer object, Locale locale) {
			return ":" + object.toString();
		}
	};
}
 
Example 19
@Override
public Printer<?> getPrinter(EntityFormat annotation, Class<?> fieldType) {
  // First use the message code at the field level
  EntityFormat fieldAnnotation = AnnotationUtils.getAnnotation(annotation, EntityFormat.class);

  String messageCode = fieldAnnotation.message();
  if (!StringUtils.isEmpty(messageCode)) {
    return createMessagePrinter(messageCode);
  }

  // Then the expression at the field level
  String expression = fieldAnnotation.expression();
  if (!StringUtils.isEmpty(expression)) {
    return createPrinter(expression);
  }

  // If no message code or expression is provided at the field level, try with the annotation
  // at the class level
  EntityFormat classAnnotation =
      AnnotatedElementUtils.findMergedAnnotation(fieldType, EntityFormat.class);

  if (classAnnotation != null) {
    // Then the message code at the class level
    messageCode = classAnnotation.message();
    if (!StringUtils.isEmpty(messageCode)) {
      return createMessagePrinter(messageCode);
    }

    // Then the expression at the class level
    expression = classAnnotation.expression();
  }

  return createPrinter(expression);
}
 
Example 20
/**
 * Test method for {@link io.springlets.format.EntityFormatAnnotationFormatterFactory#getPrinter(io.springlets.format.EntityFormat, java.lang.Class)}.
 */
@Test
@Ignore("Whe have to find a way to mock AnnotationUtils")
public void shouldReturnASpElPrinter() {
  // Prepare
  when(format.message()).thenReturn(null);
  when(format.expression()).thenReturn("expression");

  // Exercise
  Printer<?> printer = factory.getPrinter(format, Object.class);

  // Verify
  assertThat(printer).isInstanceOf(EntityPrinter.class);
}
 
Example 21
/**
 * Test method for {@link io.springlets.format.EntityFormatAnnotationFormatterFactory#getPrinter(io.springlets.format.EntityFormat, java.lang.Class)}.
 */
@Test
@Ignore("Whe have to find a way to mock AnnotationUtils")
public void shouldReturnASpElMessagePrinter() {
  // Prepare
  when(format.message()).thenReturn("messageCode");
  when(format.expression()).thenReturn("expression");

  // Exercise
  Printer<?> printer = factory.getPrinter(format, Object.class);

  // Verify
  assertThat(printer).isInstanceOf(EntityMessagePrinter.class);
}
 
Example 22
public Printer<Date> getPrinter(DateTimeFormat annotation,
		Class<?> fieldType) {
	if (Date.class.isAssignableFrom(fieldType)) {
		return new DatePrinter(annotation);
	} else {
		return null;
	}
	// return configureFormatterFrom(annotation, fieldType);
}
 
Example 23
Source Project: wallride   Source File: ControllerUtils.java    License: Apache License 2.0 5 votes vote down vote up
private static String convertPropertyValueForString(Object target, PropertyDescriptor descriptor, Object propertyValue) {
	DateTimeFormat dateTimeFormat;
	try {
		dateTimeFormat = target.getClass().getDeclaredField(descriptor.getName()).getAnnotation(DateTimeFormat.class);
	} catch (NoSuchFieldException e) {
		throw new RuntimeException(e);
	}
	if (dateTimeFormat != null) {
		JodaDateTimeFormatAnnotationFormatterFactory factory = new JodaDateTimeFormatAnnotationFormatterFactory();
		Printer printer = factory.getPrinter(dateTimeFormat, descriptor.getPropertyType());
		return printer.print(propertyValue, LocaleContextHolder.getLocale());
	}
	return propertyValue.toString();
}
 
Example 24
Source Project: jdal   Source File: AnnotationFormatTableCellRenderer.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
		int row, int column) {
	
	Class<?> clazz = table.getColumnClass(column);
	if (Number.class.isAssignableFrom(clazz)) {
		TableModel tableModel = table.getModel();
		if (tableModel instanceof ListTableModel) {
			ListTableModel listTableModel = (ListTableModel) tableModel;
			Class<?> modelClass = ((ListTableModel) tableModel).getModelClass();
			String propertyName = listTableModel.getPropertyName(column);
			Printer<Object> printer = (Printer<Object>) FormatUtils.getPrinter(modelClass, propertyName);
			if (printer != null) {
				value = printer.print(value, Locale.getDefault());
				JLabel label  = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
				label.setHorizontalAlignment(JLabel.RIGHT);
				return label;
			}
		}
	}
	TableCellRenderer renderer = table.getDefaultRenderer(clazz);
	
	return renderer != null ? 
			renderer.getTableCellRendererComponent(
					table, value, isSelected, hasFocus, row, column) :
				null;
}
 
Example 25
Source Project: jdal   Source File: ControlBinder.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected void doRefresh() {
	Object value = getValue();
	
	if (value != null && controlAccessor.isTextControl()) {
		Printer<Object> printer = getPrinter();
		if (printer != null) {
			value = printer.print(value, Locale.getDefault());
		}
	}
	
	controlAccessor.setControlValue(value);
	
}
 
Example 26
@Override
public Printer<Number> getPrinter(NumberFormat annotation, Class<?> fieldType) {
	return configureFormatterFrom(annotation);
}
 
Example 27
@Override
public Printer<MonetaryAmount> getPrinter(NumberFormat annotation, Class<?> fieldType) {
	return configureFormatterFrom(annotation);
}
 
Example 28
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 29
Source Project: spring-analysis-note   Source File: FormattingConversionService.java    License: MIT License 4 votes vote down vote up
public PrinterConverter(Class<?> fieldType, Printer<?> printer, ConversionService conversionService) {
	this.fieldType = fieldType;
	this.printerObjectType = TypeDescriptor.valueOf(resolvePrinterObjectType(printer));
	this.printer = printer;
	this.conversionService = conversionService;
}
 
Example 30
Source Project: spring-analysis-note   Source File: FormattingConversionService.java    License: MIT License 4 votes vote down vote up
@Nullable
private Class<?> resolvePrinterObjectType(Printer<?> printer) {
	return GenericTypeResolver.resolveTypeArgument(printer.getClass(), Printer.class);
}