View Javadoc
1   package org.apache.turbine.services.localization;
2   
3   
4   import org.apache.commons.lang3.StringUtils;
5   import org.apache.logging.log4j.LogManager;
6   import org.apache.logging.log4j.Logger;
7   import org.apache.turbine.Turbine;
8   import org.apache.turbine.services.TurbineBaseService;
9   
10  import java.time.ZoneId;
11  import java.time.format.DateTimeFormatter;
12  import java.time.format.DateTimeParseException;
13  import java.time.temporal.TemporalAccessor;
14  import java.util.Locale;
15  
16  /**
17   * This service is used to format {@link TemporalAccessor} and
18   * {@link #map(String, DateTimeFormatter, Locale)} (different falvors)
19   * objects into strings.
20   *
21   * The methods may throw {@link java.time.temporal.UnsupportedTemporalTypeException} or
22   * {@link DateTimeParseException}.
23   * if the source and the target format do not match appropriately.
24   *
25   */
26  public class DateTimeFormatterService
27          extends TurbineBaseService implements DateTimeFormatterInterface {
28  
29      public static String SERVICE_NAME = "DateTimeFormatterService";
30  
31      public static String ROLE = DateTimeFormatterService.class.getName();
32  
33      /** Default date format. find supporrted formats in {@link DateTimeFormatterService} */
34      private static final String DATE_TIME_FORMAT_DEFAULT = "MM/dd/yyyy";
35  
36      /**
37       * Property tag for the date format that is to be used for the web
38       * application.
39       */
40      private static final String DATE_TIME_FORMAT_KEY = "tool.datetimeTool.format";
41  
42      private String dateTimeFormatPattern = null;
43      
44      private DateTimeFormatter defaultFormat = null;
45  
46      @Override
47      public DateTimeFormatter getDefaultFormat()
48      {
49          return defaultFormat;
50      }
51  
52      @Override
53      public String getDateTimeFormatPattern() {
54          return dateTimeFormatPattern;
55      }
56  
57      private static final Logger log = LogManager.getLogger(DateTimeFormatterService.class);
58  
59      /**
60       * Initialize the service.
61       *
62       * the {@link #defaultFormat} from {@link #dateTimeFormatPattern} is initialized with
63       * the default Locale {@link Locale#getDefault()} and default zone: {@link ZoneId#systemDefault()}.
64       *
65       */
66      @Override
67      public void init()
68      {
69          dateTimeFormatPattern = Turbine.getConfiguration()
70                  .getString(DATE_TIME_FORMAT_KEY, DATE_TIME_FORMAT_DEFAULT);
71          defaultFormat = DateTimeFormatter.ofPattern(dateTimeFormatPattern)
72                  .withLocale(Locale.getDefault()).withZone(ZoneId.systemDefault());
73  
74          log.info("Initialized DateTimeFormatterService with pattern {}, locale {} and zone {}",
75                  dateTimeFormatPattern, defaultFormat.getLocale(), defaultFormat.getZone());
76          setInit(true);
77      }
78  
79      @Override
80      public <T extends TemporalAccessor> String format(T temporalAccessor)
81      {
82          return defaultFormat.format(temporalAccessor);
83      }
84  
85      @Override
86      public <T extends TemporalAccessor> String format(T temporalAccessor, String dateFormatString)
87      {
88          return format(temporalAccessor, dateFormatString, null);
89      }
90  
91      @Override
92      public <T extends TemporalAccessor> String format(T temporalAccessor, String dateFormatString, Locale locale)
93      {
94          String result = null;
95  
96          if (StringUtils.isEmpty(dateFormatString) || temporalAccessor == null)
97          {
98              result = "";
99          }
100         else
101         {
102             DateTimeFormatter dtf = DateTimeFormatter.ofPattern(dateFormatString);
103             if (locale != null)
104             {
105                 dtf.withLocale(locale);
106             }
107             result = dtf.format(temporalAccessor);
108         }
109         return result;
110     }
111     
112     @Override
113     public String map(String src, String outgoingFormatPattern, Locale locale, String incomingFormatPattern)
114     {
115         if (StringUtils.isEmpty(src) || outgoingFormatPattern == null)
116         {
117             return "";
118         }
119         if (incomingFormatPattern == null)
120         {
121             incomingFormatPattern = dateTimeFormatPattern;
122         }
123         if (incomingFormatPattern.equals( outgoingFormatPattern )) {
124             return "";
125         }
126         DateTimeFormatter incomingFormat = DateTimeFormatter.ofPattern(incomingFormatPattern);
127         DateTimeFormatter outgoingFormat = DateTimeFormatter.ofPattern(outgoingFormatPattern);
128         if (locale != null)
129         {
130             outgoingFormat = outgoingFormat.withLocale( locale );
131             //incomingFormat = incomingFormat.withLocale( locale );
132         }
133         return map( src, outgoingFormat, locale, incomingFormat );
134     }
135 
136     @Override
137     public String map(String src, DateTimeFormatter outgoingFormat, Locale locale,
138                       DateTimeFormatter incomingFormat)
139     {
140         if (StringUtils.isEmpty(src) || outgoingFormat == null)
141         {
142             return "";
143         }
144         if (incomingFormat == null)
145         {
146             incomingFormat = defaultFormat;
147         }
148         if (incomingFormat.equals( outgoingFormat )) {
149             return "";
150         }
151         if (locale != null)
152         {
153             outgoingFormat = outgoingFormat.withLocale( locale );
154             //incomingFormat = incomingFormat.withLocale( locale );
155         }
156         return  outgoingFormat.format(
157                 incomingFormat.parse( src ));
158     }
159 
160     @Override
161     public String mapTo(String src, DateTimeFormatter outgoingFormat)
162     {
163         return map( src, outgoingFormat, null, defaultFormat );
164     }
165 
166     @Override
167     public String mapFrom(String src, DateTimeFormatter incomingFormat)
168     {
169         return map( src, defaultFormat, null, incomingFormat );
170     }
171 
172     @Override
173     public String map(String src, DateTimeFormatter outgoingFormat, Locale locale)
174     {
175         return map( src, outgoingFormat, locale, defaultFormat );
176     }
177 }