001/*
002 * Copyright 2002-2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.format.datetime.joda;
018
019import java.util.Calendar;
020import java.util.Collections;
021import java.util.Date;
022import java.util.HashSet;
023import java.util.Set;
024
025import org.joda.time.LocalDate;
026import org.joda.time.LocalDateTime;
027import org.joda.time.LocalTime;
028import org.joda.time.ReadableInstant;
029import org.joda.time.ReadablePartial;
030import org.joda.time.format.DateTimeFormatter;
031
032import org.springframework.context.support.EmbeddedValueResolutionSupport;
033import org.springframework.format.AnnotationFormatterFactory;
034import org.springframework.format.Parser;
035import org.springframework.format.Printer;
036import org.springframework.format.annotation.DateTimeFormat;
037
038/**
039 * Formats fields annotated with the {@link DateTimeFormat} annotation using Joda-Time.
040 *
041 * <p><b>NOTE:</b> Spring's Joda-Time support requires Joda-Time 2.x, as of Spring 4.0.
042 *
043 * @author Keith Donald
044 * @author Juergen Hoeller
045 * @since 3.0
046 * @see DateTimeFormat
047 */
048public class JodaDateTimeFormatAnnotationFormatterFactory extends EmbeddedValueResolutionSupport
049                implements AnnotationFormatterFactory<DateTimeFormat> {
050
051        private static final Set<Class<?>> FIELD_TYPES;
052
053        static {
054                // Create the set of field types that may be annotated with @DateTimeFormat.
055                // Note: the 3 ReadablePartial concrete types are registered explicitly since
056                // addFormatterForFieldType rules exist for each of these types
057                // (if we did not do this, the default byType rules for LocalDate, LocalTime,
058                // and LocalDateTime would take precedence over the annotation rule, which
059                // is not what we want)
060                Set<Class<?>> fieldTypes = new HashSet<Class<?>>(8);
061                fieldTypes.add(ReadableInstant.class);
062                fieldTypes.add(LocalDate.class);
063                fieldTypes.add(LocalTime.class);
064                fieldTypes.add(LocalDateTime.class);
065                fieldTypes.add(Date.class);
066                fieldTypes.add(Calendar.class);
067                fieldTypes.add(Long.class);
068                FIELD_TYPES = Collections.unmodifiableSet(fieldTypes);
069        }
070
071
072        @Override
073        public final Set<Class<?>> getFieldTypes() {
074                return FIELD_TYPES;
075        }
076
077        @Override
078        public Printer<?> getPrinter(DateTimeFormat annotation, Class<?> fieldType) {
079                DateTimeFormatter formatter = getFormatter(annotation, fieldType);
080                if (ReadablePartial.class.isAssignableFrom(fieldType)) {
081                        return new ReadablePartialPrinter(formatter);
082                }
083                else if (ReadableInstant.class.isAssignableFrom(fieldType) || Calendar.class.isAssignableFrom(fieldType)) {
084                        // assumes Calendar->ReadableInstant converter is registered
085                        return new ReadableInstantPrinter(formatter);
086                }
087                else {
088                        // assumes Date->Long converter is registered
089                        return new MillisecondInstantPrinter(formatter);
090                }
091        }
092
093        @Override
094        public Parser<?> getParser(DateTimeFormat annotation, Class<?> fieldType) {
095                if (LocalDate.class == fieldType) {
096                        return new LocalDateParser(getFormatter(annotation, fieldType));
097                }
098                else if (LocalTime.class == fieldType) {
099                        return new LocalTimeParser(getFormatter(annotation, fieldType));
100                }
101                else if (LocalDateTime.class == fieldType) {
102                        return new LocalDateTimeParser(getFormatter(annotation, fieldType));
103                }
104                else {
105                        return new DateTimeParser(getFormatter(annotation, fieldType));
106                }
107        }
108
109        /**
110         * Factory method used to create a {@link DateTimeFormatter}.
111         * @param annotation the format annotation for the field
112         * @param fieldType the type of field
113         * @return a {@link DateTimeFormatter} instance
114         * @since 3.2
115         */
116        protected DateTimeFormatter getFormatter(DateTimeFormat annotation, Class<?> fieldType) {
117                DateTimeFormatterFactory factory = new DateTimeFormatterFactory();
118                factory.setStyle(resolveEmbeddedValue(annotation.style()));
119                factory.setIso(annotation.iso());
120                factory.setPattern(resolveEmbeddedValue(annotation.pattern()));
121                return factory.createDateTimeFormatter();
122        }
123
124}