001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 * 
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 * 
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.log4j.helpers;
019
020import org.apache.log4j.Layout;
021import org.apache.log4j.spi.LoggingEvent;
022import java.text.DateFormat;
023import java.text.SimpleDateFormat;
024import java.util.Date;
025import java.util.TimeZone;
026import java.text.FieldPosition;
027
028
029/**
030   This abstract layout takes care of all the date related options and
031   formatting work.
032   
033
034   @author Ceki Gülcü
035 */
036abstract public class DateLayout extends Layout {
037
038  /**
039     String constant designating no time information. Current value of
040     this constant is <b>NULL</b>.
041     
042  */
043  public final static String NULL_DATE_FORMAT = "NULL";
044
045  /**
046     String constant designating relative time. Current value of
047     this constant is <b>RELATIVE</b>.
048   */
049  public final static String RELATIVE_TIME_DATE_FORMAT = "RELATIVE";
050
051  protected FieldPosition pos = new FieldPosition(0);
052
053  /**
054     @deprecated Options are now handled using the JavaBeans paradigm.
055     This constant is not longer needed and will be removed in the
056     <em>near</em> term.
057  */
058  final static public String DATE_FORMAT_OPTION = "DateFormat";
059  
060  /**
061     @deprecated Options are now handled using the JavaBeans paradigm.
062     This constant is not longer needed and will be removed in the
063     <em>near</em> term.
064  */
065  final static public String TIMEZONE_OPTION = "TimeZone";  
066
067  private String timeZoneID;
068  private String dateFormatOption;  
069
070  protected DateFormat dateFormat;
071  protected Date date = new Date();
072
073  /**
074     @deprecated Use the setter method for the option directly instead
075     of the generic <code>setOption</code> method. 
076  */
077  public
078  String[] getOptionStrings() {
079    return new String[] {DATE_FORMAT_OPTION, TIMEZONE_OPTION};
080  }
081
082  /**
083     @deprecated Use the setter method for the option directly instead
084     of the generic <code>setOption</code> method. 
085  */
086  public
087  void setOption(String option, String value) {
088    if(option.equalsIgnoreCase(DATE_FORMAT_OPTION)) {
089      dateFormatOption = value.toUpperCase();
090    } else if(option.equalsIgnoreCase(TIMEZONE_OPTION)) {
091      timeZoneID = value;
092    }
093  }
094  
095
096  /**
097    The value of the <b>DateFormat</b> option should be either an
098    argument to the constructor of {@link SimpleDateFormat} or one of
099    the srings "NULL", "RELATIVE", "ABSOLUTE", "DATE" or "ISO8601.
100   */
101  public
102  void setDateFormat(String dateFormat) {
103    if (dateFormat != null) {
104        dateFormatOption = dateFormat;
105    }
106    setDateFormat(dateFormatOption, TimeZone.getDefault());
107  }
108
109  /**
110     Returns value of the <b>DateFormat</b> option.
111   */
112  public
113  String getDateFormat() {
114    return dateFormatOption;
115  }
116  
117  /**
118    The <b>TimeZoneID</b> option is a time zone ID string in the format
119    expected by the {@link TimeZone#getTimeZone} method.
120   */
121  public
122  void setTimeZone(String timeZone) {
123    this.timeZoneID = timeZone;
124  }
125  
126  /**
127     Returns value of the <b>TimeZone</b> option.
128   */
129  public
130  String getTimeZone() {
131    return timeZoneID;
132  }
133  
134  public
135  void activateOptions() {
136    setDateFormat(dateFormatOption);
137    if(timeZoneID != null && dateFormat != null) {
138      dateFormat.setTimeZone(TimeZone.getTimeZone(timeZoneID));
139    }
140  }
141
142  public
143  void dateFormat(StringBuffer buf, LoggingEvent event) {
144    if(dateFormat != null) {
145      date.setTime(event.timeStamp);
146      dateFormat.format(date, buf, this.pos);
147      buf.append(' ');
148    }
149  }
150
151  /**
152     Sets the {@link DateFormat} used to format time and date in the
153     zone determined by <code>timeZone</code>.
154   */
155  public
156  void setDateFormat(DateFormat dateFormat, TimeZone timeZone) {
157    this.dateFormat = dateFormat;    
158    this.dateFormat.setTimeZone(timeZone);
159  }
160  
161  /**
162     Sets the DateFormat used to format date and time in the time zone
163     determined by <code>timeZone</code> parameter. The {@link DateFormat} used
164     will depend on the <code>dateFormatType</code>.
165
166     <p>The recognized types are {@link #NULL_DATE_FORMAT}, {@link
167     #RELATIVE_TIME_DATE_FORMAT} {@link
168     AbsoluteTimeDateFormat#ABS_TIME_DATE_FORMAT}, {@link
169     AbsoluteTimeDateFormat#DATE_AND_TIME_DATE_FORMAT} and {@link
170     AbsoluteTimeDateFormat#ISO8601_DATE_FORMAT}. If the
171     <code>dateFormatType</code> is not one of the above, then the
172     argument is assumed to be a date pattern for {@link
173     SimpleDateFormat}.
174  */
175  public
176  void setDateFormat(String dateFormatType, TimeZone timeZone) {
177    if(dateFormatType == null) {
178      this.dateFormat = null;
179      return;
180    } 
181
182    if(dateFormatType.equalsIgnoreCase(NULL_DATE_FORMAT)) {
183      this.dateFormat = null;
184    } else if (dateFormatType.equalsIgnoreCase(RELATIVE_TIME_DATE_FORMAT)) {
185      this.dateFormat =  new RelativeTimeDateFormat();
186    } else if(dateFormatType.equalsIgnoreCase(
187                             AbsoluteTimeDateFormat.ABS_TIME_DATE_FORMAT)) {
188      this.dateFormat =  new AbsoluteTimeDateFormat(timeZone);
189    } else if(dateFormatType.equalsIgnoreCase(
190                        AbsoluteTimeDateFormat.DATE_AND_TIME_DATE_FORMAT)) {
191      this.dateFormat =  new DateTimeDateFormat(timeZone);
192    } else if(dateFormatType.equalsIgnoreCase(
193                              AbsoluteTimeDateFormat.ISO8601_DATE_FORMAT)) {
194      this.dateFormat =  new ISO8601DateFormat(timeZone);
195    } else {
196      this.dateFormat = new SimpleDateFormat(dateFormatType);
197      this.dateFormat.setTimeZone(timeZone);
198    }
199  }
200}