01: // DateAttribute.java
02: // $Id: DateAttribute.java,v 1.5 2002/06/26 17:27:43 ylafon Exp $
03: // (c) COPYRIGHT MIT and INRIA, 1996.
04: // Please first read the full copyright statement in file COPYRIGHT.html
05:
06: package org.w3c.tools.resources;
07:
08: import java.text.SimpleDateFormat;
09: import java.text.ParseException;
10: import java.util.Date;
11: import java.util.TimeZone;
12:
13: public class DateAttribute extends LongAttribute {
14:
15: String cachedPickledValue = null;
16: Long cachedPickledLong = null;
17:
18: public DateAttribute(String name, Object def, int flags) {
19: super (name, (Long) def, flags);
20: this .type = "java.util.Date".intern();
21: }
22:
23: public DateAttribute() {
24: super ();
25: }
26:
27: /**
28: * Get a DateFormat compliant with RFC 822 updated by RFC 1123.
29: * @return a SimpleDateFormat instance.
30: */
31: private SimpleDateFormat getDateFormatter() {
32: SimpleDateFormat formatter = new SimpleDateFormat(
33: "EEE, dd MMM yyyy HH:mm:ss zzz");
34: formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
35: return formatter;
36: }
37:
38: /**
39: * Pickle an integer to the given output stream.
40: * @param obj The object to pickle.
41: * @exception IOException If some IO error occured.
42: */
43:
44: public String pickle(Object obj) {
45: if (obj == cachedPickledLong) {
46: if (cachedPickledValue == null) {
47: SimpleDateFormat formatter = getDateFormatter();
48: String s = formatter.format(new Date(((Long) obj)
49: .longValue()));
50: cachedPickledValue = s;
51: return s;
52: }
53: return cachedPickledValue;
54: }
55: SimpleDateFormat formatter = getDateFormatter();
56: return formatter.format(new Date(((Long) obj).longValue()));
57: }
58:
59: /**
60: * Unpickle an integer from the given input stream.
61: * @param value the string representation of this integer
62: * @return An instance of Integer.
63: * @exception IOException If some IO error occured.
64: */
65:
66: public Object unpickle(String value) {
67: try {
68: SimpleDateFormat formatter = getDateFormatter();
69: Long l = new Long((formatter.parse(value)).getTime());
70: cachedPickledLong = l;
71: return l;
72: } catch (ParseException ex) {
73: return new Long(-1);
74: }
75: }
76:
77: }
|