FormattingConversionService.java 8.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
/*
 * Copyright 2002-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.ui.format.support;

import java.lang.annotation.Annotation;
import java.text.ParseException;
import java.util.Set;

import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.GenericTypeResolver;
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConverterRegistry;
import org.springframework.core.convert.support.ConditionalGenericConverter;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.core.convert.support.GenericConverter;
import org.springframework.ui.format.AnnotationFormatterFactory;
import org.springframework.ui.format.Formatter;
import org.springframework.ui.format.FormatterRegistry;
import org.springframework.ui.format.Parser;
import org.springframework.ui.format.Printer;

/**
 * A ConversionService implementation designed to be configured as a {@link FormatterRegistry}..
 * @author Keith Donald
 * @since 3.0
 */
public class FormattingConversionService implements FormatterRegistry, ConversionService {

	private GenericConversionService conversionService = new GenericConversionService();

	/**
	 * Creates a new FormattingConversionService, initially with no Formatters registered.
	 * A {@link DefaultConversionService} is configured as the parent conversion service to support primitive type conversion.
	 */
	public FormattingConversionService() {
		this.conversionService.setParent(new DefaultConversionService());
	}

	/**
	 * Creates a new FormattingConversionService, initially with no Formatters registered.
	 * The conversion logic contained in the parent is merged with this service.
	 */
	public FormattingConversionService(ConversionService parent) {
		this.conversionService.setParent(parent);
	}

	// implementing FormattingRegistry

	public void addFormatterForFieldType(Class<?> fieldType, Printer<?> printer, Parser<?> parser) {
		this.conversionService.addGenericConverter(fieldType, String.class, new PrinterConverter(printer, this.conversionService));
		this.conversionService.addGenericConverter(String.class, fieldType, new ParserConverter(parser, this.conversionService));
	}

	public void addFormatterForFieldType(Class<?> fieldType, Formatter<?> formatter) {
		this.conversionService.addGenericConverter(fieldType, String.class, new PrinterConverter(formatter, this.conversionService));
		this.conversionService.addGenericConverter(String.class, fieldType, new ParserConverter(formatter, this.conversionService));
	}

	@SuppressWarnings("unchecked")
	public void addFormatterForFieldAnnotation(final AnnotationFormatterFactory annotationFormatterFactory) {
		final Class<? extends Annotation> annotationType = resolveAnnotationType(annotationFormatterFactory);
		if (annotationType == null) {
			throw new IllegalArgumentException(
					"Unable to extract parameterized Annotation type argument from AnnotationFormatterFactory ["
							+ annotationFormatterFactory.getClass().getName()
							+ "]; does the factory parameterize the <A extends Annotation> generic type?");
		}		
		Set<Class<?>> fieldTypes = annotationFormatterFactory.getFieldTypes();
K
polish  
Keith Donald 已提交
85
		for (final Class<?> fieldType : fieldTypes) {
86 87 88 89 90 91 92
			this.conversionService.addGenericConverter(fieldType, String.class, new ConditionalGenericConverter() {
				public boolean matches(TypeDescriptor sourceFieldType, TypeDescriptor targetFieldType) {
					return sourceFieldType.getAnnotation(annotationType) != null;
				}
				public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
					Printer<?> printer = annotationFormatterFactory.getPrinter(sourceType.getAnnotation(annotationType), targetType.getType());
					return new PrinterConverter(printer, conversionService).convert(source, sourceType, targetType);
K
polish  
Keith Donald 已提交
93 94 95 96
				}
				public String toString() {
					return "@" + annotationType.getName() + " " + fieldType.getName() + " -> " + String.class.getName();
				}
97 98 99 100 101 102 103 104
			});
			this.conversionService.addGenericConverter(String.class, fieldType, new ConditionalGenericConverter() {
				public boolean matches(TypeDescriptor sourceFieldType, TypeDescriptor targetFieldType) {
					return targetFieldType.getAnnotation(annotationType) != null;
				}
				public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
					Parser<?> parser = annotationFormatterFactory.getParser(targetType.getAnnotation(annotationType), targetType.getType());
					return new ParserConverter(parser, conversionService).convert(source, sourceType, targetType);		
K
polish  
Keith Donald 已提交
105 106 107
				}
				public String toString() {
					return String.class.getName() + " -> @" + annotationType.getName() + " " + fieldType.getName();
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
				}				
			});
		}
	}

	public ConverterRegistry getConverterRegistry() {
		return this.conversionService;
	}

	// implementing ConverisonService

	public boolean canConvert(Class<?> sourceType, Class<?> targetType) {
		return canConvert(TypeDescriptor.valueOf(sourceType), TypeDescriptor.valueOf(targetType));
	}

	@SuppressWarnings("unchecked")
	public <T> T convert(Object source, Class<T> targetType) {
		return (T) convert(source, TypeDescriptor.forObject(source), TypeDescriptor.valueOf(targetType));
	}

	public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) {
		return this.conversionService.canConvert(sourceType, targetType);
	}

	public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
		return this.conversionService.convert(source, sourceType, targetType);
	}

K
polish  
Keith Donald 已提交
136 137 138 139
	public String toString() {
		return this.conversionService.toString();
	}
	
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
	// internal helpers

	@SuppressWarnings("unchecked")
	private Class<? extends Annotation> resolveAnnotationType(AnnotationFormatterFactory<?> annotationFormatterFactory) {
		return (Class<? extends Annotation>) GenericTypeResolver.resolveTypeArgument(annotationFormatterFactory.getClass(), AnnotationFormatterFactory.class);
	}
	
	private static class PrinterConverter implements GenericConverter {

		private TypeDescriptor printerObjectType;

		@SuppressWarnings("unchecked")
		private Printer printer;

		private ConversionService conversionService;

		public PrinterConverter(Printer<?> printer, ConversionService conversionService) {
			this.printerObjectType = TypeDescriptor.valueOf(resolvePrinterObjectType(printer));
			this.printer = printer;
			this.conversionService = conversionService;
		}

		@SuppressWarnings("unchecked")
		public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
			if (!sourceType.isAssignableTo(this.printerObjectType)) {
				source = this.conversionService.convert(source, sourceType, this.printerObjectType);
			}
			return source != null ? this.printer.print(source, LocaleContextHolder.getLocale()) : "";
		}
		
		private Class<?> resolvePrinterObjectType(Printer<?> printer) {
			return GenericTypeResolver.resolveTypeArgument(printer.getClass(), Printer.class);
		}
	}

	private static class ParserConverter implements GenericConverter {

		@SuppressWarnings("unchecked")
		private Parser parser;

		private ConversionService conversionService;

		public ParserConverter(Parser<?> parser, ConversionService conversionService) {
			this.parser = parser;
			this.conversionService = conversionService;
		}

		public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
			String submittedValue = (String) source;
			if (submittedValue.isEmpty()) {
				return null;
			}
			Object parsedValue;
			try {
				parsedValue = this.parser.parse(submittedValue, LocaleContextHolder.getLocale());
			} catch (ParseException e) {
				throw new ConversionFailedException(sourceType, targetType, source, e);
			}
			TypeDescriptor parsedObjectType = TypeDescriptor.valueOf(parsedValue.getClass());
			if (!parsedObjectType.isAssignableTo(targetType)) {
				try {
					parsedValue = this.conversionService.convert(parsedValue, parsedObjectType, targetType);
				} catch (ConversionFailedException e) {
					throw new ConversionFailedException(sourceType, targetType, source, e);
				}
			}
			return parsedValue;
		}
	}
	
}