001: package org.apache.turbine.services.pull.util;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.util.Date;
023:
024: import org.apache.commons.lang.StringUtils;
025: import org.apache.commons.lang.time.DateFormatUtils;
026: import org.apache.turbine.Turbine;
027: import org.apache.turbine.services.pull.ApplicationTool;
028:
029: /**
030: * This pull tool is used to format date objects into strings.
031: *
032: * <p>As this is designed to be used as a gloal scope pull tool it needs to be
033: * threadsafe.
034: *
035: * <p>This is an application pull tool for the template system. You should
036: * <b>not</b> use it in a normal application.
037: *
038: * @author <a href="mailto:qmccombs@nequalsone.com">Quinton McCombs</a>
039: * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
040: * @version $Id: DateFormatter.java 535251 2007-05-04 14:19:16Z seade $
041: */
042: public class DateFormatter implements ApplicationTool {
043: /** Default date format */
044: private static final String DATE_FORMAT_DEFAULT = "MM/dd/yyyy";
045:
046: /**
047: * Property tag for the date format that is to be used for the web
048: * application.
049: */
050: private static final String DATE_FORMAT_KEY = "tool.dateTool.format";
051:
052: private String dateFormat = null;
053:
054: /**
055: * Initialize the application tool. The data parameter holds a different
056: * type depending on how the tool is being instantiated:
057: * <ul>
058: * <li>For global tools data will be null
059: * <li>For request tools data will be of type RunData
060: * <li>For session and persistent tools data will be of type User
061: *
062: * @param data initialization data
063: */
064: public void init(Object data) {
065: dateFormat = Turbine.getConfiguration().getString(
066: DATE_FORMAT_KEY, DATE_FORMAT_DEFAULT);
067: }
068:
069: /**
070: * Refresh the application tool. This is necessary for development work
071: * where you probably want the tool to refresh itself if it is using
072: * configuration information that is typically cached after initialization.
073: */
074: public void refresh() {
075: }
076:
077: /**
078: * Formats the given date as a String using the default date format.
079: * The default date format is MM/dd/yyyy
080: *
081: * @param theDate date to format
082: * @return String value of the date
083: */
084: public String format(Date theDate) {
085: return format(theDate, dateFormat);
086: }
087:
088: /**
089: * Formats the given date as a String.
090: *
091: * @param theDate date to format
092: * @param dateFormatString format string to use. See
093: * java.text.SimpleDateFormat for details.
094: * @return String value of the date
095: */
096: public String format(Date theDate, String dateFormatString) {
097: String result = null;
098:
099: if (StringUtils.isEmpty(dateFormatString) || theDate == null) {
100: result = "";
101: } else {
102: result = DateFormatUtils.format(theDate, dateFormatString);
103: }
104: return result;
105: }
106:
107: /**
108: * Formats the given date as a String.
109: *
110: * @param theDate date to format
111: * @param dateFormatString format string to use. See
112: * java.text.SimpleDateFormat for details.
113: * @return String value of the date
114: */
115: public String format(long theDate, String dateFormatString) {
116: return DateFormatUtils.format(theDate, dateFormatString);
117: }
118:
119: }
|