View Javadoc
1   package org.apache.turbine.services.pull.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.time.ZoneId;
23  import java.time.format.DateTimeFormatter;
24  import java.time.format.DateTimeParseException;
25  import java.time.temporal.TemporalAccessor;
26  import java.util.Locale;
27  
28  import org.apache.logging.log4j.LogManager;
29  import org.apache.logging.log4j.Logger;
30  import org.apache.turbine.annotation.TurbineService;
31  import org.apache.turbine.services.ServiceManager;
32  import org.apache.turbine.services.TurbineServices;
33  import org.apache.turbine.services.localization.DateTimeFormatterInterface;
34  import org.apache.turbine.services.localization.DateTimeFormatterService;
35  import org.apache.turbine.services.pull.ApplicationTool;
36  
37  /**
38   * This pull tool is used to format {@link TemporalAccessor} and
39   * {@link #map(String, DateTimeFormatter, Locale)} (different falvors)
40   * objects into strings.
41   *
42   * The methods may throw {@link java.time.temporal.UnsupportedTemporalTypeException} or
43   * {@link DateTimeParseException}.
44   * if the source and the target format do not match appropriately.
45   *
46   */
47  public class DateTimeFormatterTool
48          implements ApplicationTool, DateTimeFormatterInterface
49  {
50  
51      @TurbineService
52      private DateTimeFormatterService dtfs;
53  
54      private static final Logger log = LogManager.getLogger(DateTimeFormatterTool.class);
55  
56      /**
57       * Initialize the application tool. The data parameter holds a different
58       * type depending on how the tool is being instantiated:
59       * <ul>
60       * <li>For global tools data will be null</li>
61       * <li>For request tools data will be of type RunData</li>
62       * <li>For session and persistent tools data will be of type User</li>
63       * </ul>
64       *
65       * the {@link #defaultFormat} from {@link #dateTimeFormatPattern} with default Locale {@link Locale#getDefault()} and
66       * Default zone: {@link ZoneId#systemDefault()}
67       *
68       * @param data initialization data
69       */
70      @Override
71      public void init(Object data)
72      {
73          log.info("Initialized DateTimeFormatterTool with service {}",
74                  dtfs);
75          if (dtfs == null)
76          {
77              ServiceManager serviceManager = TurbineServices.getInstance();
78              dtfs = (DateTimeFormatterService)serviceManager.getService(DateTimeFormatterService.SERVICE_NAME);
79          }
80          // dtfs should be already initialized
81      }
82  
83      public DateTimeFormatterService getDtfs() {
84          return dtfs;
85      }
86  
87      /**
88       * Refresh the application tool. This is
89       * necessary for development work where you
90       * probably want the tool to refresh itself
91       * if it is using configuration information
92       * that is typically cached after initialization
93       */
94      @Override
95      public void refresh()
96      {
97          // empty
98      }
99  
100     public DateTimeFormatter getDefaultFormat()
101     {
102         return getDtfs().getDefaultFormat();
103     }
104 
105     public String getDateTimeFormatPattern() {
106         return getDtfs().getDateTimeFormatPattern();
107     }
108 
109     /**
110      * Formats the given datetime as a String with the #{@link DateTimeFormatterTool#defaultFormat}.
111      * using the default date format.
112      *
113      * @param the {@link TemporalAccessor to format
114      * @return String value of the date
115      */
116     public <T extends TemporalAccessor> String format(T temporalAccessor)
117     {
118         return getDtfs().getDefaultFormat().format(temporalAccessor);
119     }
120 
121     public <T extends TemporalAccessor> String format(T temporalAccessor, String dateFormatString)
122     {
123         return getDtfs().format(temporalAccessor, dateFormatString, null);
124     }
125 
126     public <T extends TemporalAccessor> String format(T temporalAccessor, String dateFormatString, Locale locale)
127     {
128         return getDtfs().format(temporalAccessor, dateFormatString, locale);
129     }
130 
131     public String map( String src, String outgoingFormatPattern, Locale locale, String incomingFormatPattern)
132     {
133         return getDtfs().map(src, outgoingFormatPattern, locale, incomingFormatPattern);
134     }
135 
136     public String map( String src, java.time.format.DateTimeFormatter outgoingFormat, Locale locale,
137             java.time.format.DateTimeFormatter incomingFormat)
138     {
139         return getDtfs().map(src, outgoingFormat, locale, incomingFormat);
140     }
141 
142     public String mapTo( String src, DateTimeFormatter outgoingFormat )
143     {
144         return  getDtfs().map( src, outgoingFormat, null, getDtfs().getDefaultFormat() );
145     }
146 
147     public String mapFrom( String src, DateTimeFormatter incomingFormat )
148     {
149         return  getDtfs().map( src, getDtfs().getDefaultFormat(), null, incomingFormat );
150     }
151 
152     public String map( String src,  DateTimeFormatter outgoingFormat, Locale locale )
153     {
154         return  getDtfs().map( src, outgoingFormat, locale, getDtfs().getDefaultFormat() );
155     }
156 }