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.util.Date;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.apache.commons.lang.time.DateFormatUtils;
26  import org.apache.turbine.Turbine;
27  import org.apache.turbine.services.pull.ApplicationTool;
28  
29  /***
30   * This pull tool is used to format date objects into strings.
31   *
32   * <p>As this is designed to be used as a gloal scope pull tool it needs to be
33   * threadsafe.
34   *
35   * <p>This is an application pull tool for the template system. You should
36   * <b>not</b> use it in a normal application.
37   *
38   * @author <a href="mailto:qmccombs@nequalsone.com">Quinton McCombs</a>
39   * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
40   * @version $Id: DateFormatter.java 535251 2007-05-04 14:19:16Z seade $
41   */
42  public class DateFormatter
43          implements ApplicationTool
44  {
45      /*** Default date format */
46      private static final String DATE_FORMAT_DEFAULT = "MM/dd/yyyy";
47  
48      /***
49       * Property tag for the date format that is to be used for the web
50       * application.
51       */
52      private static final String DATE_FORMAT_KEY = "tool.dateTool.format";
53  
54      private String dateFormat = null;
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
61       * <li>For request tools data will be of type RunData
62       * <li>For session and persistent tools data will be of type User
63       *
64       * @param data initialization data
65       */
66      public void init(Object data)
67      {
68          dateFormat = Turbine.getConfiguration()
69                  .getString(DATE_FORMAT_KEY, DATE_FORMAT_DEFAULT);
70      }
71  
72      /***
73       * Refresh the application tool. This is necessary for development work
74       * where you probably want the tool to refresh itself if it is using
75       * configuration information that is typically cached after initialization.
76       */
77      public void refresh()
78      {
79      }
80  
81      /***
82       * Formats the given date as a String using the default date format.
83       * The default date format is MM/dd/yyyy
84       *
85       * @param theDate date to format
86       * @return String value of the date
87       */
88      public String format(Date theDate)
89      {
90          return format(theDate, dateFormat);
91      }
92  
93      /***
94       * Formats the given date as a String.
95       *
96       * @param theDate date to format
97       * @param dateFormatString format string to use.  See
98       * java.text.SimpleDateFormat for details.
99       * @return String value of the date
100      */
101     public String format(Date theDate, String dateFormatString)
102     {
103         String result = null;
104 
105         if (StringUtils.isEmpty(dateFormatString) || theDate == null)
106         {
107             result = "";
108         }
109         else
110         {
111             result = DateFormatUtils.format(theDate, dateFormatString);
112         }
113         return result;
114     }
115 
116     /***
117      * Formats the given date as a String.
118      *
119      * @param theDate date to format
120      * @param dateFormatString format string to use.  See 
121      * java.text.SimpleDateFormat for details.
122      * @return String value of the date
123      */
124     public String format(long theDate, String dateFormatString)
125     {
126         return DateFormatUtils.format(theDate, dateFormatString);
127     }
128 
129 }