pushlet笔记 Event

public class Event implements Protocol, Serializable {

	protected Map attributes = new HashMap(3);

	public Event(String anEventType) {
		this(anEventType, null);
	}

	//初始化方法 设置事件的时间 类型 属性  这些数据都在map中
	public Event(String anEventType, Map theAttributes) {

		if (theAttributes != null) {
			setAttrs(theAttributes);
		}

		// Set required field event type
		setField(P_EVENT, anEventType);

		// Set time in seconds since 1970
		setField(P_TIME, System.currentTimeMillis() / 1000);
	}

	//初始化方法 设置属性  因为没有提供默认的时间类型, 所以属性中必须包含类型信息
	public Event(Map theAttributes) {
		if (!theAttributes.containsKey(P_EVENT)) {
			throw new IllegalArgumentException(P_EVENT + " not found in attributes");
		}
		setAttrs(theAttributes);
	}

	public static Event createDataEvent(String aSubject) {
		return createDataEvent(aSubject, null);
	}

	//创建数据事件  设置主题 
	public static Event createDataEvent(String aSubject, Map theAttributes) {
		Event dataEvent = new Event(E_DATA, theAttributes);
		dataEvent.setField(P_SUBJECT, aSubject);
		return dataEvent;
	}

	//拿到事件类型
	public String getEventType() {
		return getField(P_EVENT);
	}

	//拿到事件主题
	public String getSubject() {
		return getField(P_SUBJECT);
	}

	public void setField(String name, String value) {
		attributes.put(name, value);
	}

	public void setField(String name, int value) {
		attributes.put(name, value + "");
	}

	public void setField(String name, long value) {
		attributes.put(name, value + "");
	}

	public String getField(String name) {
		return (String) attributes.get(name);
	}

	/**
	 * Return field; if null return default.
	 */
	public String getField(String name, String aDefault) {
		String result = getField(name);
		return result == null ? aDefault : result;
	}

	public Iterator getFieldNames() {
		return attributes.keySet().iterator();
	}

	public String toString() {
		return attributes.toString();
	}

	/**
	 * Convert to HTTP query string.
	 * 拼出一个http  get请求  id=1&name=xxx&age=13$v=34  类似这样的
	 */
	public String toQueryString() {
		String queryString = "";
		String amp = "";
		for (Iterator iter = getFieldNames(); iter.hasNext();) {
			String nextAttrName = (String) iter.next();
			String nextAttrValue = getField(nextAttrName);
			queryString = queryString + amp + nextAttrName + "=" + nextAttrValue;
			// After first add "&".
			amp = "&";
		}

		return queryString;
	}

	//转换成xml格式  strict  true的话会把其中value的特殊字符转换成html的格式 < > \' \" \\ &
	//转成 <event  id="id"  name="name" />
	public String toXML(boolean strict) {
		String xmlString = "<event ";
		for (Iterator iter = getFieldNames(); iter.hasNext();) {
			String nextAttrName = (String) iter.next();
			String nextAttrValue = getField(nextAttrName);
			xmlString = xmlString + nextAttrName + "=\"" + (strict ? Sys.forHTMLTag(nextAttrValue) : nextAttrValue) + "\" ";
		}

		xmlString += "/>";
		return xmlString;
	}

	//默认xml不转成html特殊字符
	public String toXML() {
		return toXML(false);
	}

	public Object clone() {
		// Clone the Event by using copy constructor
		return new Event(attributes);
	}

	/**
	 * Copy given attributes into event attributes
	 */
	private void setAttrs(Map theAttributes) {
		attributes.putAll(theAttributes);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值