001/*
002 * Copyright 2002-2017 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;
037import org.springframework.util.StringUtils;
038
039/**
040 * Formats fields annotated with the {@link DateTimeFormat} annotation using Joda-Time.
041 *
042 * <p><b>NOTE:</b> Spring's Joda-Time support requires Joda-Time 2.x, as of Spring 4.0.
043 *
044 * @author Keith Donald
045 * @author Juergen Hoeller
046 * @since 3.0
047 * @see DateTimeFormat
048 */
049public class JodaDateTimeFormatAnnotationFormatterFactory extends EmbeddedValueResolutionSupport
050                implements AnnotationFormatterFactory<DateTimeFormat> {
051
052        private static final Set<Class<?>> FIELD_TYPES;
053
054        static {
055                // Create the set of field types that may be annotated with @DateTimeFormat.
056                // Note: the 3 ReadablePartial concrete types are registered explicitly since
057                // addFormatterForFieldType rules exist for each of these types
058                // (if we did not do this, the default byType rules for LocalDate, LocalTime,
059                // and LocalDateTime would take precedence over the annotation rule, which
060                // is not what we want)
061                Set<Class<?>> fieldTypes = new HashSet<>(8);
062                fieldTypes.add(ReadableInstant.class);
063                fieldTypes.add(LocalDate.class);
064                fieldTypes.add(LocalTime.class);
065                fieldTypes.add(LocalDateTime.class);
066                fieldTypes.add(Date.class);
067                fieldTypes.add(Calendar.class);
068                fieldTypes.add(Long.class);
069                FIELD_TYPES = Collections.unmodifiableSet(fieldTypes);
070        }
071
072
073        @Override
074        public final Set<Class<?>> getFieldTypes() {
075                return FIELD_TYPES;
076        }
077
078        @Override
079        public Printer<?> getPrinter(DateTimeFormat annotation, Class<?> fieldType) {
080                DateTimeFormatter formatter = getFormatter(annotation, fieldType);
081                if (ReadablePartial.class.isAssignableFrom(fieldType)) {
082                        return new ReadablePartialPrinter(formatter);
083                }
084                else if (ReadableInstant.class.isAssignableFrom(fieldType) || Calendar.class.isAssignableFrom(fieldType)) {
085                        // assumes Calendar->ReadableInstant converter is registered
086                        return new ReadableInstantPrinter(formatter);
087                }
088                else {
089                        // assumes Date->Long converter is registered
090                        return new MillisecondInstantPrinter(formatter);
091                }
092        }
093
094        @Override
095        public Parser<?> getParser(DateTimeFormat annotation, Class<?> fieldType) {
096                if (LocalDate.class == fieldType) {
097                        return new LocalDateParser(getFormatter(annotation, fieldType));
098                }
099                else if (LocalTime.class == fieldType) {
100                        return new LocalTimeParser(getFormatter(annotation, fieldType));
101                }
102                else if (LocalDateTime.class == fieldType) {
103                        return new LocalDateTimeParser(getFormatter(annotation, fieldType));
104                }
105                else {
106                        return new DateTimeParser(getFormatter(annotation, fieldType));
107                }
108        }
109
110        /**
111         * Factory method used to create a {@link DateTimeFormatter}.
112         * @param annotation the format annotation for the field
113         * @param fieldType the type of field
114         * @return a {@link DateTimeFormatter} instance
115         * @since 3.2
116         */
117        protected DateTimeFormatter getFormatter(DateTimeFormat annotation, Class<?> fieldType) {
118                DateTimeFormatterFactory factory = new DateTimeFormatterFactory();
119                String style = resolveEmbeddedValue(annotation.style());
120                if (StringUtils.hasLength(style)) {
121                        factory.setStyle(style);
122                }
123                factory.setIso(annotation.iso());
124                String pattern = resolveEmbeddedValue(annotation.pattern());
125                if (StringUtils.hasLength(pattern)) {
126                        factory.setPattern(pattern);
127                }
128                return factory.createDateTimeFormatter();
129        }
130
131}