1
4 package nl.justobjects.pushlet.core;
5
6 import nl.justobjects.pushlet.util.Sys;
7
8 import java.io.Serializable;
9 import java.util.HashMap;
10import java.util.Iterator;
11import java.util.Map;
12
13
19public class Event implements Protocol, Serializable {
20
21 protected Map attributes = new HashMap(3);
22
23 public Event(String anEventType) {
24 this(anEventType, null);
25 }
26
27 public Event(String anEventType, Map theAttributes) {
28
29 if (theAttributes != null) {
30 setAttrs(theAttributes);
31 }
32
33 setField(P_EVENT, anEventType);
35
36 setField(P_TIME, System.currentTimeMillis() / 1000);
38 }
39
40 public Event(Map theAttributes) {
41 if (!theAttributes.containsKey(P_EVENT)) {
42 throw new IllegalArgumentException(P_EVENT + " not found in attributes");
43 }
44 setAttrs(theAttributes);
45 }
46
47 public static Event createDataEvent(String aSubject) {
48 return createDataEvent(aSubject, null);
49 }
50
51 public static Event createDataEvent(String aSubject, Map theAttributes) {
52 Event dataEvent = new Event(E_DATA, theAttributes);
53 dataEvent.setField(P_SUBJECT, aSubject);
54 return dataEvent;
55 }
56
57 public String getEventType() {
58 return getField(P_EVENT);
59 }
60
61 public String getSubject() {
62 return getField(P_SUBJECT);
63 }
64
65 public void setField(String name, String value) {
66 attributes.put(name, value);
67 }
68
69 public void setField(String name, int value) {
70 attributes.put(name, value + "");
71 }
72
73 public void setField(String name, long value) {
74 attributes.put(name, value + "");
75 }
76
77 public String getField(String name) {
78 return (String) attributes.get(name);
79 }
80
81
84 public String getField(String name, String aDefault) {
85 String result = getField(name);
86 return result == null ? aDefault : result;
87 }
88
89 public Iterator getFieldNames() {
90 return attributes.keySet().iterator();
91 }
92
93 public String toString() {
94 return attributes.toString();
95 }
96
97
00 public String toQueryString() {
01 String queryString = "";
02 String amp = "";
03 for (Iterator iter = getFieldNames(); iter.hasNext();) {
04 String nextAttrName = (String) iter.next();
05 String nextAttrValue = getField(nextAttrName);
06 queryString = queryString + amp + nextAttrName + "=" + nextAttrValue;
07 amp = "&";
09 }
10
11 return queryString;
12 }
13
14 public String toXML(boolean strict) {
15 String xmlString = "<event ";
16 for (Iterator iter = getFieldNames(); iter.hasNext();) {
17 String nextAttrName = (String) iter.next();
18 String nextAttrValue = getField(nextAttrName);
19 xmlString = xmlString + nextAttrName + "=\"" + (strict ? Sys.forHTMLTag(nextAttrValue) : nextAttrValue) + "\" ";
20 }
21
22 xmlString += "/>";
23 return xmlString;
24 }
25
26 public String toXML() {
27 return toXML(false);
28 }
29
30 public Object clone() {
31 return new Event(attributes);
33 }
34
35
38 private void setAttrs(Map theAttributes) {
39 attributes.putAll(theAttributes);
40 }
41}
42
43
85