KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > helpers > DateLayout


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.log4j.helpers;
18
19 import org.apache.log4j.Layout;
20 import org.apache.log4j.helpers.RelativeTimeDateFormat;
21 import org.apache.log4j.helpers.AbsoluteTimeDateFormat;
22 import org.apache.log4j.helpers.DateTimeDateFormat;
23 import org.apache.log4j.helpers.ISO8601DateFormat;
24 import org.apache.log4j.spi.LoggingEvent;
25 import java.text.DateFormat JavaDoc;
26 import java.text.SimpleDateFormat JavaDoc;
27 import java.util.Date JavaDoc;
28 import java.util.TimeZone JavaDoc;
29 import java.text.FieldPosition JavaDoc;
30
31
32 /**
33    This abstract layout takes care of all the date related options and
34    formatting work.
35    
36
37    @author Ceki Gülcü
38  */

39 abstract public class DateLayout extends Layout {
40
41   /**
42      String constant designating no time information. Current value of
43      this constant is <b>NULL</b>.
44      
45   */

46   public final static String JavaDoc NULL_DATE_FORMAT = "NULL";
47
48   /**
49      String constant designating relative time. Current value of
50      this constant is <b>RELATIVE</b>.
51    */

52   public final static String JavaDoc RELATIVE_TIME_DATE_FORMAT = "RELATIVE";
53
54   protected FieldPosition JavaDoc pos = new FieldPosition JavaDoc(0);
55
56   /**
57      @deprecated Options are now handled using the JavaBeans paradigm.
58      This constant is not longer needed and will be removed in the
59      <em>near</em> term.
60   */

61   final static public String JavaDoc DATE_FORMAT_OPTION = "DateFormat";
62   
63   /**
64      @deprecated Options are now handled using the JavaBeans paradigm.
65      This constant is not longer needed and will be removed in the
66      <em>near</em> term.
67   */

68   final static public String JavaDoc TIMEZONE_OPTION = "TimeZone";
69
70   private String JavaDoc timeZoneID;
71   private String JavaDoc dateFormatOption;
72
73   protected DateFormat dateFormat;
74   protected Date JavaDoc date = new Date JavaDoc();
75
76   /**
77      @deprecated Use the setter method for the option directly instead
78      of the generic <code>setOption</code> method.
79   */

80   public
81   String JavaDoc[] getOptionStrings() {
82     return new String JavaDoc[] {DATE_FORMAT_OPTION, TIMEZONE_OPTION};
83   }
84
85   /**
86      @deprecated Use the setter method for the option directly instead
87      of the generic <code>setOption</code> method.
88   */

89   public
90   void setOption(String JavaDoc option, String JavaDoc value) {
91     if(option.equalsIgnoreCase(DATE_FORMAT_OPTION)) {
92       dateFormatOption = value.toUpperCase();
93     } else if(option.equalsIgnoreCase(TIMEZONE_OPTION)) {
94       timeZoneID = value;
95     }
96   }
97   
98
99   /**
100     The value of the <b>DateFormat</b> option should be either an
101     argument to the constructor of {@link SimpleDateFormat} or one of
102     the srings "NULL", "RELATIVE", "ABSOLUTE", "DATE" or "ISO8601.
103    */

104   public
105   void setDateFormat(String JavaDoc dateFormat) {
106     if (dateFormat != null) {
107         dateFormatOption = dateFormat;
108     }
109     setDateFormat(dateFormatOption, TimeZone.getDefault());
110   }
111
112   /**
113      Returns value of the <b>DateFormat</b> option.
114    */

115   public
116   String JavaDoc getDateFormat() {
117     return dateFormatOption;
118   }
119   
120   /**
121     The <b>TimeZoneID</b> option is a time zone ID string in the format
122     expected by the {@link TimeZone#getTimeZone} method.
123    */

124   public
125   void setTimeZone(String JavaDoc timeZone) {
126     this.timeZoneID = timeZone;
127   }
128   
129   /**
130      Returns value of the <b>TimeZone</b> option.
131    */

132   public
133   String JavaDoc getTimeZone() {
134     return timeZoneID;
135   }
136   
137   public
138   void activateOptions() {
139     setDateFormat(dateFormatOption);
140     if(timeZoneID != null && dateFormat != null) {
141       dateFormat.setTimeZone(TimeZone.getTimeZone(timeZoneID));
142     }
143   }
144
145   public
146   void dateFormat(StringBuffer JavaDoc buf, LoggingEvent event) {
147     if(dateFormat != null) {
148       date.setTime(event.timeStamp);
149       dateFormat.format(date, buf, this.pos);
150       buf.append(' ');
151     }
152   }
153
154   /**
155      Sets the {@link DateFormat} used to format time and date in the
156      zone determined by <code>timeZone</code>.
157    */

158   public
159   void setDateFormat(DateFormat dateFormat, TimeZone JavaDoc timeZone) {
160     this.dateFormat = dateFormat;
161     this.dateFormat.setTimeZone(timeZone);
162   }
163   
164   /**
165      Sets the DateFormat used to format date and time in the time zone
166      determined by <code>timeZone</code> parameter. The {@link DateFormat} used
167      will depend on the <code>dateFormatType</code>.
168
169      <p>The recognized types are {@link #NULL_DATE_FORMAT}, {@link
170      #RELATIVE_TIME_DATE_FORMAT} {@link
171      AbsoluteTimeDateFormat#ABS_TIME_DATE_FORMAT}, {@link
172      AbsoluteTimeDateFormat#DATE_AND_TIME_DATE_FORMAT} and {@link
173      AbsoluteTimeDateFormat#ISO8601_DATE_FORMAT}. If the
174      <code>dateFormatType</code> is not one of the above, then the
175      argument is assumed to be a date pattern for {@link
176      SimpleDateFormat}.
177   */

178   public
179   void setDateFormat(String JavaDoc dateFormatType, TimeZone JavaDoc timeZone) {
180     if(dateFormatType == null) {
181       this.dateFormat = null;
182       return;
183     }
184
185     if(dateFormatType.equalsIgnoreCase(NULL_DATE_FORMAT)) {
186       this.dateFormat = null;
187     } else if (dateFormatType.equalsIgnoreCase(RELATIVE_TIME_DATE_FORMAT)) {
188       this.dateFormat = new RelativeTimeDateFormat();
189     } else if(dateFormatType.equalsIgnoreCase(
190                              AbsoluteTimeDateFormat.ABS_TIME_DATE_FORMAT)) {
191       this.dateFormat = new AbsoluteTimeDateFormat(timeZone);
192     } else if(dateFormatType.equalsIgnoreCase(
193                         AbsoluteTimeDateFormat.DATE_AND_TIME_DATE_FORMAT)) {
194       this.dateFormat = new DateTimeDateFormat(timeZone);
195     } else if(dateFormatType.equalsIgnoreCase(
196                               AbsoluteTimeDateFormat.ISO8601_DATE_FORMAT)) {
197       this.dateFormat = new ISO8601DateFormat(timeZone);
198     } else {
199       this.dateFormat = new SimpleDateFormat JavaDoc(dateFormatType);
200       this.dateFormat.setTimeZone(timeZone);
201     }
202   }
203 }
204
Popular Tags