summaryrefslogtreecommitdiffstats
blob: 181caa2656e3c12deda56fcd7703ba1534394229 (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
/*
 * Copyright (c) 2012, Isode Limited, London, England.
 * All rights reserved.
 */
/*
 * Copyright (c) 2010, Remko Tronçon.
 * All rights reserved.
 */
package com.isode.stroke.parser.payloadparsers;

import com.isode.stroke.elements.DiscoInfo;
import com.isode.stroke.parser.AttributeMap;
import com.isode.stroke.parser.GenericPayloadParser;


public class DiscoInfoParser extends GenericPayloadParser<DiscoInfo> {
    public DiscoInfoParser() {
        super(new DiscoInfo());
    }

    public void handleStartElement(String element, String ns, AttributeMap attributes) {
    	if (level_ == TopLevel) {
			if (attributes.getAttributeValue("node") != null) {
				getPayloadInternal().setNode(attributes.getAttributeValue("node"));
			}
		}
        if (level_ == PayloadLevel) {
		if (element .equals("identity")) {
			getPayloadInternal().addIdentity(new DiscoInfo.Identity(attributes.getAttribute("name"), attributes.getAttribute("category"), attributes.getAttribute("type"), attributes.getAttribute("lang", "http://www.w3.org/XML/1998/namespace")));
		}
		else if (element.equals("feature")) {
			getPayloadInternal().addFeature(attributes.getAttribute("var"));
		}
		else if (element.equals("x") && ns.equals("jabber:x:data")) {
			assert(formParser_ == null);
			formParser_ = new FormParser();
		}
	}
	if (formParser_ != null) {
		formParser_.handleStartElement(element, ns, attributes);
	}
	++level_;
    }

    public void handleEndElement(String element, String ns) {
        --level_;
	if (formParser_ != null) {
		formParser_.handleEndElement(element, ns);
	}
	if (level_ == PayloadLevel && formParser_ != null) {
		getPayloadInternal().addExtension(formParser_.getPayloadInternal());
		formParser_ = null;
	}
    }

    public void handleCharacterData(String data) {
        if (formParser_ != null) {
            formParser_.handleCharacterData(data);
        }
    }

    private static final int TopLevel = 0;
    private static final int PayloadLevel = 1;
    private int level_ = 0;
    private FormParser formParser_ = null;
}