KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DelayExtension


1 import org.jivesoftware.smack.packet.PacketExtension;
2 import java.util.Calendar JavaDoc;
3 import java.util.Date JavaDoc;
4 import java.text.DateFormat JavaDoc;
5 import java.text.SimpleDateFormat JavaDoc;
6 import java.text.ParseException JavaDoc;
7
8 /** Packet extension for Delayed Delivery jabber:x:delay.
9 * See <a HREF="http://www.jabber.org/jeps/jep-0091.html">JEP 91</a>
10 * for more info.*/

11 public final class DelayExtension implements PacketExtension{
12     
13     private static final SimpleDateFormat JavaDoc FORMAT=new SimpleDateFormat JavaDoc("yyyyMMdd'T'hh:mm:ss");
14     private static final DateFormat JavaDoc LOCAL_FORMAT=DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT);
15     private static final DateFormat JavaDoc LOCAL_TIME_FORMAT=DateFormat.getTimeInstance(DateFormat.SHORT);
16     
17     
18     private String JavaDoc from=null;
19     private String JavaDoc stamp="";
20     private String JavaDoc content=null;
21     
22     /** Creates an empty DelayExtension.*/
23     public DelayExtension(){
24         //do nowt
25
}
26     
27     /** Sets the time stamp attribute.*/
28     public void setStamp(String JavaDoc stamp){
29         if(stamp==null){
30             throw new NullPointerException JavaDoc("null stamp passed to setStamp in DelayExtension");
31         }
32         this.stamp=stamp;
33     }
34     
35     /**Sets the from attribute.*/
36     public void setFrom(String JavaDoc from){
37         this.from=from;
38     }
39     
40     /** Returns the the x tag content.
41     * EG Offline Storage.*/

42     public void setContent(String JavaDoc content){
43         this.content=content;
44     }
45     
46     /** Returns the from attribute.
47     * May return <code>null</code>.*/

48     public String JavaDoc getFrom(){
49         return from;
50     }
51     
52     /** Returns the the x tag content.
53     * EG Offline Storage.
54     * May return <code>null</code>.*/

55     public String JavaDoc getContent(){
56         return content;
57     }
58     
59     /** Returns the time stamp as a string.
60     * Format is yyyyMMddThh:mm:ss.*/

61     public String JavaDoc getStamp(){
62         return stamp;
63     }
64     
65     /** Returns the time stamp as a date object.
66     * Returns <code>null</code> if there was a problem parsing the time stamp.*/

67     public Date JavaDoc getDate(){
68         try{
69             Calendar JavaDoc cal = Calendar.getInstance();
70             // Convert the UTC time to local time.
71
cal.setTime(new Date JavaDoc(FORMAT.parse(stamp).getTime()+cal.getTimeZone().getOffset(cal.getTimeInMillis())));
72             return cal.getTime();
73         }
74         catch (ParseException JavaDoc pe){
75             return null;
76         }
77     }
78     
79     /** Returns the time stamp as a localised string.
80     Returns <code>""</code> if there was a problem parsing the time stamp.*/

81     public String JavaDoc getLocalStamp(){
82         Date JavaDoc d=getDate();
83         if(d==null){
84             return "";
85         }
86         return LOCAL_FORMAT.format(d);
87     }
88     
89     /** Returns the time part of the time stamp as a localised string.
90     Returns <code>""</code> if there was a problem parsing the time stamp.*/

91     public String JavaDoc getLocalTime(){
92         Date JavaDoc d=getDate();
93         if(d==null){
94             return "";
95         }
96         return LOCAL_TIME_FORMAT.format(d);
97     }
98     
99     
100     // Interface implementation
101

102     /** Returns 'x'.*/
103     public String JavaDoc getElementName(){
104         return "x";
105     }
106     
107     /** Returns 'jabber:x:delay'.*/
108     public String JavaDoc getNamespace(){
109         return "jabber:x:delay";
110     }
111         
112     public String JavaDoc toXML(){
113         StringBuffer JavaDoc buf=new StringBuffer JavaDoc();
114         buf.append("<x xmlns='jabber:x:delay' ");
115         buf.append("stamp='").append(stamp).append("' ");
116         if(from!=null){
117             buf.append("from='").append(from).append("' ");
118         }
119         buf.append(">");
120         if(content!=null){
121             buf.append(content);
122         }
123         buf.append("</x>");
124         return buf.toString();
125     }
126 }
Popular Tags