001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Sam
027: */
028:
029: package com.caucho.config.functions;
030:
031: import com.caucho.log.Log;
032: import com.caucho.server.util.CauchoSystem;
033: import com.caucho.util.Alarm;
034: import com.caucho.util.L10N;
035: import com.caucho.util.QDate;
036: import com.caucho.util.Sprintf;
037:
038: import java.util.Calendar;
039: import java.util.Date;
040: import java.util.logging.Logger;
041:
042: /**
043: * An object to store in an EL Environment to provide utility methods.
044: */
045: public class FmtFunctions {
046: static protected final Logger log = Logger
047: .getLogger(FmtFunctions.class.getName());
048: static final L10N L = new L10N(FmtFunctions.class);
049:
050: static private QDate _calendar = new QDate(true);
051:
052: public FmtFunctions() {
053: }
054:
055: /**
056: * Make a timestamp for current date and time.
057: */
058: static public String timestamp(String format) {
059: long now;
060:
061: if (CauchoSystem.isTesting())
062: now = Alarm.getCurrentTime();
063: else
064: now = System.currentTimeMillis();
065:
066: return timestamp(format, now);
067: }
068:
069: static protected String timestamp(String format, long t) {
070: return _calendar.formatLocal(t, format);
071: }
072:
073: public static String timestamp(String format, Date date) {
074: return timestamp(format, date.getTime());
075: }
076:
077: public static String timestamp(String format, Calendar date) {
078: return timestamp(format, date.getTimeInMillis());
079: }
080:
081: public static String timestamp(String format, QDate date) {
082: return date.format(format);
083: }
084:
085: public static String sprintf(String format, Object arg0,
086: Object arg1, Object arg2, Object arg3, Object arg4,
087: Object arg5, Object arg6, Object arg7, Object arg8,
088: Object arg9) {
089: return Sprintf.sprintf(format, new Object[] { arg0, arg1, arg2,
090: arg3, arg4, arg5, arg6, arg7, arg8, arg9 });
091: }
092:
093: public static String sprintf(String format, Object arg0,
094: Object arg1, Object arg2, Object arg3, Object arg4,
095: Object arg5, Object arg6, Object arg7, Object arg8) {
096: return Sprintf.sprintf(format, new Object[] { arg0, arg1, arg2,
097: arg3, arg4, arg5, arg6, arg7, arg8 });
098: }
099:
100: public static String sprintf(String format, Object arg0,
101: Object arg1, Object arg2, Object arg3, Object arg4,
102: Object arg5, Object arg6, Object arg7) {
103: return Sprintf.sprintf(format, new Object[] { arg0, arg1, arg2,
104: arg3, arg4, arg5, arg6, arg7 });
105: }
106:
107: public static String sprintf(String format, Object arg0,
108: Object arg1, Object arg2, Object arg3, Object arg4,
109: Object arg5, Object arg6) {
110: return Sprintf.sprintf(format, new Object[] { arg0, arg1, arg2,
111: arg3, arg4, arg5, arg6 });
112: }
113:
114: public static String sprintf(String format, Object arg0,
115: Object arg1, Object arg2, Object arg3, Object arg4,
116: Object arg5) {
117: return Sprintf.sprintf(format, new Object[] { arg0, arg1, arg2,
118: arg3, arg4, arg5 });
119: }
120:
121: public static String sprintf(String format, Object arg0,
122: Object arg1, Object arg2, Object arg3, Object arg4) {
123: return Sprintf.sprintf(format, new Object[] { arg0, arg1, arg2,
124: arg3, arg4 });
125: }
126:
127: public static String sprintf(String format, Object arg0,
128: Object arg1, Object arg2, Object arg3) {
129: return Sprintf.sprintf(format, new Object[] { arg0, arg1, arg2,
130: arg3 });
131: }
132:
133: public static String sprintf(String format, Object arg0,
134: Object arg1, Object arg2) {
135: return Sprintf.sprintf(format,
136: new Object[] { arg0, arg1, arg2 });
137: }
138:
139: public static String sprintf(String format, Object arg0, Object arg1) {
140: return Sprintf.sprintf(format, new Object[] { arg0, arg1 });
141: }
142:
143: public static String sprintf(String format, Object arg0) {
144: return Sprintf.sprintf(format, new Object[] { arg0 });
145: }
146: }
|