summaryrefslogtreecommitdiffstats
blob: 134d65448a98c0fde9126a6a73fc737539d3f646 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright (c) 2014, Isode Limited, London, England.
* All rights reserved.
*/
/*
* Copyright (c) 2014, Remko Tronçon.
* All rights reserved.
*/

package com.isode.stroke.parser.payloadparsers;

import com.isode.stroke.elements.PubSub;
import com.isode.stroke.elements.PubSubConfigure;
import com.isode.stroke.elements.PubSubCreate;
import com.isode.stroke.elements.PubSubOptions;
import com.isode.stroke.elements.PubSubPayload;
import com.isode.stroke.elements.PubSubSubscribe;
import com.isode.stroke.parser.AttributeMap;
import com.isode.stroke.parser.GenericPayloadParser;
import com.isode.stroke.parser.PayloadParser;
import com.isode.stroke.parser.PayloadParserFactoryCollection;

public class PubSubParser extends GenericPayloadParser<PubSub> {
    
    public PubSubParser(PayloadParserFactoryCollection parsers) {
        super(new PubSub());
        parsers_ = parsers;
        level_ = 0;
    }
    
    public void handleStartElement(String element, String ns, AttributeMap attributes) {
        if (level_ == 1) {
            if (element == "items" && ns == "http://jabber.org/protocol/pubsub") {
                currentPayloadParser_ = new PubSubItemsParser(parsers_);
            }
            if (element == "create" && ns == "http://jabber.org/protocol/pubsub") {
                currentPayloadParser_ = new PubSubCreateParser(parsers_);
            }
            if (element == "publish" && ns == "http://jabber.org/protocol/pubsub") {
                currentPayloadParser_ = new PubSubPublishParser(parsers_);
            }
            if (element == "affiliations" && ns == "http://jabber.org/protocol/pubsub") {
                currentPayloadParser_ = new PubSubAffiliationsParser(parsers_);
            }
            if (element == "retract" && ns == "http://jabber.org/protocol/pubsub") {
                currentPayloadParser_ = new PubSubRetractParser(parsers_);
            }
            if (element == "options" && ns == "http://jabber.org/protocol/pubsub") {
                currentPayloadParser_ = new PubSubOptionsParser(parsers_);
            }
            if (element == "configure" && ns == "http://jabber.org/protocol/pubsub") {
                currentPayloadParser_ = new PubSubConfigureParser(parsers_);
            }
            if (element == "default" && ns == "http://jabber.org/protocol/pubsub") {
                currentPayloadParser_ = new PubSubDefaultParser(parsers_);
            }
            if (element == "subscriptions" && ns == "http://jabber.org/protocol/pubsub") {
                currentPayloadParser_ = new PubSubSubscriptionsParser(parsers_);
            }
            if (element == "subscribe" && ns == "http://jabber.org/protocol/pubsub") {
                currentPayloadParser_ = new PubSubSubscribeParser(parsers_);
            }
            if (element == "unsubscribe" && ns == "http://jabber.org/protocol/pubsub") {
                currentPayloadParser_ = new PubSubUnsubscribeParser(parsers_);
            }
            if (element == "subscription" && ns == "http://jabber.org/protocol/pubsub") {
                currentPayloadParser_ = new PubSubSubscriptionParser(parsers_);
            }
        }
        
        if (level_>=1 && currentPayloadParser_!=null) {
            currentPayloadParser_.handleStartElement(element, ns, attributes);
        }
        ++level_;
    }
    
    public void handleEndElement(String element, String ns) {
        --level_;
        if (currentPayloadParser_!=null) {
            if (level_ >= 1) {
                currentPayloadParser_.handleEndElement(element, ns);
            }
            
            if (level_ == 1) {
                if (currentPayloadParser_ != null) {
                    if (element == "options" && ns == "http://jabber.org/protocol/pubsub") {
                        optionsPayload_ = (PubSubOptions)currentPayloadParser_.getPayload();
                    }
                    else if (element == "configure" && ns == "http://jabber.org/protocol/pubsub") {
                        configurePayload_ = (PubSubConfigure)currentPayloadParser_.getPayload();
                    }
                    else {
                        getPayloadInternal().setPayload((PubSubPayload)currentPayloadParser_.getPayload());
                    }
                }
                currentPayloadParser_ = null;
            }
            
            if (level_ == 0) {
                PubSubCreate create = (PubSubCreate)getPayloadInternal().getPayload();
                if (create != null) {
                    if (configurePayload_ != null) {
                        create.setConfigure(configurePayload_);
                    }
                }
                PubSubSubscribe subscribe = (PubSubSubscribe)getPayloadInternal().getPayload();
                if (subscribe != null) {
                    if (optionsPayload_ != null) {
                        subscribe.setOptions(optionsPayload_);
                    }
                }
            }
        }
    }
    
    public void handleCharacterData(String data) {
        if (level_>1 && currentPayloadParser_!=null) {
            currentPayloadParser_.handleCharacterData(data);
        }
    }
    
    
    PayloadParserFactoryCollection parsers_;
    int level_;
    PayloadParser currentPayloadParser_;
    PubSubConfigure configurePayload_;
    PubSubOptions optionsPayload_;
    
}