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
18
19
20
21
22
23
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
34 private static final String DATE_TIME_FORMAT_DEFAULT = "MM/dd/yyyy";
35
36
37
38
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
61
62
63
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
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
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 }