org.springframework.format.Printer Java Examples

The following examples show how to use org.springframework.format.Printer. 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: FormattingConversionService.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@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 #2
Source File: FormattingConversionService.java    From spring-analysis-note with 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 #3
Source File: Jsr310DateTimeFormatAnnotationFormatterFactory.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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 #4
Source File: FormattingConversionService.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 #5
Source File: Jsr310DateTimeFormatAnnotationFormatterFactory.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@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 #6
Source File: Jsr310DateTimeFormatAnnotationFormatterFactory.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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
Source File: FormattingConversionService.java    From java-technology-stack with 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 #8
Source File: EntityFormatAnnotationFormatterFactoryTest.java    From springlets with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #9
Source File: JodaDateTimeFormatAnnotationFormatterFactory.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 #10
Source File: JodaTimeFormatterRegistrar.java    From spring4-understanding with Apache License 2.0 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
Source File: JodaDateTimeFormatAnnotationFormatterFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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 File: JodaTimeFormatterRegistrar.java    From lams with GNU General Public License v2.0 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
Source File: FormattingConversionServiceFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public Printer<?> getPrinter(SpecialInt annotation, Class<?> fieldType) {
	return new Printer<Integer>() {
		@Override
		public String print(Integer object, Locale locale) {
			return ":" + object.toString();
		}
	};
}
 
Example #14
Source File: FormattingConversionServiceFactoryBeanTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #15
Source File: JodaTimeFormatterRegistrar.java    From java-technology-stack with 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 #16
Source File: JodaDateTimeFormatAnnotationFormatterFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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
Source File: EntityFormatAnnotationFormatterFactory.java    From springlets with Apache License 2.0 5 votes vote down vote up
@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 #18
Source File: EntityFormatAnnotationFormatterFactoryTest.java    From springlets with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #19
Source File: DateTimeFormatAnnotationFormatterFactory.java    From JDeSurvey with GNU Affero General Public License v3.0 5 votes vote down vote up
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 #20
Source File: ControllerUtils.java    From wallride with 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 #21
Source File: FormattingConversionServiceFactoryBeanTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 #22
Source File: JodaTimeFormatterRegistrar.java    From spring-analysis-note with 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 #23
Source File: JodaDateTimeFormatAnnotationFormatterFactory.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 #24
Source File: AnnotationFormatTableCellRenderer.java    From jdal with 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 File: ControlBinder.java    From jdal with 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
Source File: FormattingConversionService.java    From spring4-understanding with Apache License 2.0 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 #27
Source File: FormattingConversionService.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private Class<?> resolvePrinterObjectType(Printer<?> printer) {
	return GenericTypeResolver.resolveTypeArgument(printer.getClass(), Printer.class);
}
 
Example #28
Source File: ControlBinder.java    From jdal with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
protected Printer<Object> getPrinter() {
	return (Printer<Object>) FormatUtils.getPrinter(getModel().getClass(), propertyName);
}
 
Example #29
Source File: Jsr310DateTimeFormatAnnotationFormatterFactory.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public Printer<?> getPrinter(DateTimeFormat annotation, Class<?> fieldType) {
	DateTimeFormatter formatter = getFormatter(annotation, fieldType);
	return new TemporalAccessorPrinter(formatter);
}
 
Example #30
Source File: DateTimeFormatAnnotationFormatterFactory.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public Printer<?> getPrinter(DateTimeFormat annotation, Class<?> fieldType) {
	return getFormatter(annotation, fieldType);
}