summaryrefslogtreecommitdiffstats
blob: 7228fc3475f8222ef6ec1e67b55be500f59095b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright (c) 2011 Jan Kaluza
 * Licensed under the Simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */
/*
 * Copyright (c) 2015 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */
/*
 * Copyright (c) 2015 Tarun Gupta.
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

package com.isode.stroke.parser.payloadparsers;

import com.isode.stroke.parser.GenericPayloadParser;
import com.isode.stroke.parser.AttributeMap;
import com.isode.stroke.elements.RosterItemExchangePayload;
import com.isode.stroke.base.NotNull;
import com.isode.stroke.jid.JID;

public class RosterItemExchangeParser extends GenericPayloadParser<RosterItemExchangePayload> {

	private final int TopLevel = 0;
	private final int PayloadLevel = 1;
	private final int ItemLevel = 2;
	private int level_ = 0;
	private boolean inItem_;
	private RosterItemExchangePayload.Item currentItem_;
	private String currentText_ = "";

	public RosterItemExchangeParser() {
		super(new RosterItemExchangePayload());
		this.level_ = TopLevel;
		this.inItem_ = false;
	}

	/**
	* @param element, NotNull.
	* @param ns.
	* @param attributes, NotNull.
	*/
	@Override
	public void handleStartElement(String element, String ns, AttributeMap attributes) {
		NotNull.exceptIfNull(element, "element");
		NotNull.exceptIfNull(attributes, "attributes");
		if (level_ == PayloadLevel) {
			if (element.equals("item")) {
				inItem_ = true;

				currentItem_ = new RosterItemExchangePayload.Item();

				currentItem_.setJID(new JID(attributes.getAttribute("jid")));
				currentItem_.setName(attributes.getAttribute("name"));

				String action = attributes.getAttribute("action");
				if (action.equals("add")) {
					currentItem_.setAction(RosterItemExchangePayload.Item.Action.Add);
				}
				else if (action.equals("modify")) {
					currentItem_.setAction(RosterItemExchangePayload.Item.Action.Modify);
				}
				else if (action.equals("delete")) {
					currentItem_.setAction(RosterItemExchangePayload.Item.Action.Delete);
				}
				else {
					// Add is default action according to XEP
					currentItem_.setAction(RosterItemExchangePayload.Item.Action.Add);
				}
			}
		}
		else if (level_ == ItemLevel) {
			if (element.equals("group")) {
				currentText_ = "";
			}
		}
		++level_;
	}

	/**
	* @param element, NotNull.
	* @param ns.
	*/
	@Override
	public void handleEndElement(String element, String ns) {
		NotNull.exceptIfNull(element, "element");
		--level_;
		if (level_ == PayloadLevel) {
			if (inItem_) {
				getPayloadInternal().addItem(currentItem_);
				inItem_ = false;
			}
		}
		else if (level_ == ItemLevel) {
			if (element.equals("group")) {
				currentItem_.addGroup(currentText_);
			}
		}
	}

	/**
	* @param data, NotNull.
	*/
	@Override
	public void handleCharacterData(String data) {
		NotNull.exceptIfNull(data, "data");
		currentText_ += data;
	}
}