1 import org.jivesoftware.smack.packet.PacketExtension; 2 import java.util.Calendar ; 3 import java.util.Date ; 4 import java.text.DateFormat ; 5 import java.text.SimpleDateFormat ; 6 import java.text.ParseException ; 7 8 11 public final class DelayExtension implements PacketExtension{ 12 13 private static final SimpleDateFormat FORMAT=new SimpleDateFormat ("yyyyMMdd'T'hh:mm:ss"); 14 private static final DateFormat LOCAL_FORMAT=DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT); 15 private static final DateFormat LOCAL_TIME_FORMAT=DateFormat.getTimeInstance(DateFormat.SHORT); 16 17 18 private String from=null; 19 private String stamp=""; 20 private String content=null; 21 22 23 public DelayExtension(){ 24 } 26 27 28 public void setStamp(String stamp){ 29 if(stamp==null){ 30 throw new NullPointerException ("null stamp passed to setStamp in DelayExtension"); 31 } 32 this.stamp=stamp; 33 } 34 35 36 public void setFrom(String from){ 37 this.from=from; 38 } 39 40 42 public void setContent(String content){ 43 this.content=content; 44 } 45 46 48 public String getFrom(){ 49 return from; 50 } 51 52 55 public String getContent(){ 56 return content; 57 } 58 59 61 public String getStamp(){ 62 return stamp; 63 } 64 65 67 public Date getDate(){ 68 try{ 69 Calendar cal = Calendar.getInstance(); 70 cal.setTime(new Date (FORMAT.parse(stamp).getTime()+cal.getTimeZone().getOffset(cal.getTimeInMillis()))); 72 return cal.getTime(); 73 } 74 catch (ParseException pe){ 75 return null; 76 } 77 } 78 79 81 public String getLocalStamp(){ 82 Date d=getDate(); 83 if(d==null){ 84 return ""; 85 } 86 return LOCAL_FORMAT.format(d); 87 } 88 89 91 public String getLocalTime(){ 92 Date d=getDate(); 93 if(d==null){ 94 return ""; 95 } 96 return LOCAL_TIME_FORMAT.format(d); 97 } 98 99 100 102 103 public String getElementName(){ 104 return "x"; 105 } 106 107 108 public String getNamespace(){ 109 return "jabber:x:delay"; 110 } 111 112 public String toXML(){ 113 StringBuffer buf=new StringBuffer (); 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 |