summaryrefslogtreecommitdiffstats
blob: 8fd7763095c1e375d3b493abb45d3fa5eefeac54 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * Copyright (c) 2010-2015, Isode Limited, London, England.
 * All rights reserved.
 */
package com.isode.stroke.parser;

import com.isode.stroke.elements.ProtocolHeader;
import java.util.logging.Logger;

public class XMPPParser implements XMLParserClient {
    
    private XMLParser xmlParser_;
    private XMPPParserClient client_;
    private PayloadParserFactoryCollection payloadParserFactories_;
    private int level_ = 0;
    private ElementParser currentElementParser_ = null;
    private boolean parseErrorOccurred_ = false;
    private Logger logger_ = Logger.getLogger(this.getClass().getName());
    private final int TopLevel = 0;
    private final int StreamLevel = 1;
    private final int ElementLevel = 2;

    public XMPPParser(XMPPParserClient parserClient,
            PayloadParserFactoryCollection payloadParserFactories) {
        xmlParser_ = null;
        client_ = parserClient;
        payloadParserFactories_ = payloadParserFactories;
        level_ = 0;
        currentElementParser_ = null;
        parseErrorOccurred_ = false;
        xmlParser_ = PlatformXMLParserFactory.createXMLParser(this);
    }
    
    public boolean parse(String data) {
        boolean xmlParseResult = xmlParser_.parse(data);
        return xmlParseResult && !parseErrorOccurred_;
    }
    
    public void handleStartElement(String element, String ns,
            AttributeMap attributes) {
        if (!parseErrorOccurred_) {
            if (level_ == TopLevel) {
                if (element.equals("stream") && ns.equals("http://etherx.jabber.org/streams")) {
                    ProtocolHeader header = new ProtocolHeader();
                    header.setFrom(attributes.getAttribute("from"));
                    header.setTo(attributes.getAttribute("to"));
                    header.setID(attributes.getAttribute("id"));
                    header.setVersion(attributes.getAttribute("version"));
                    client_.handleStreamStart(header);
                }
                else {
                    parseErrorOccurred_ = true;
                }
            }
            else {
                if (level_ == StreamLevel) {
                    assert(currentElementParser_ == null);
                    currentElementParser_ = createElementParser(element, ns);
                }
                currentElementParser_.handleStartElement(element, ns, attributes);
            }
        }
        ++level_;
    }
    
    public void handleEndElement(String element, String ns) {
        assert(level_ > TopLevel);
        --level_;
        if (!parseErrorOccurred_) {
            if (level_ == TopLevel) {
                assert(element.equals("stream"));
                client_.handleStreamEnd();
            }
            else {
                assert(currentElementParser_ != null);
                currentElementParser_.handleEndElement(element, ns);
                if (level_ == StreamLevel) {
                    client_.handleElement(currentElementParser_.getElement());
                    currentElementParser_ = null;
                }
            }
        }
    }
    
    public void handleCharacterData(String data) {
        if (!parseErrorOccurred_) {
            if (currentElementParser_ != null) {
                currentElementParser_.handleCharacterData(data);
            }
        //else {
        //  std::cerr << "XMPPParser: Ignoring stray character data: " << data << std::endl;
        //}
        }
    }

    private ElementParser createElementParser(String element, String xmlns) {
        if (element.equals("presence")) {
            return new PresenceParser(payloadParserFactories_);
        } else if (element.equals("iq")) {
            return new IQParser(payloadParserFactories_);
        } else if (element.equals("message")) {
            return new MessageParser(payloadParserFactories_);
        } else if (element.equals("features")
                && xmlns.equals("http://etherx.jabber.org/streams")) {
            return new StreamFeaturesParser();
        } else if (element.equals("error") && xmlns.equals("http://etherx.jabber.org/streams")) {
            return new StreamErrorParser();
        } else if (element.equals("auth")) {
            return new AuthRequestParser();
        } else if (element.equals("success")) {
            return new AuthSuccessParser();
        } else if (element.equals("failure")
                && xmlns.equals("urn:ietf:params:xml:ns:xmpp-sasl")) {
            return new AuthFailureParser();
        } else if (element.equals("challenge")
                && xmlns.equals("urn:ietf:params:xml:ns:xmpp-sasl")) {
            return new AuthChallengeParser();
        } else if (element.equals("response")
                && xmlns.equals("urn:ietf:params:xml:ns:xmpp-sasl")) {
            return new AuthResponseParser();
        } else if (element.equals("starttls")) {
            return new StartTLSParser();
        } else if (element.equals("failure")
                && xmlns.equals("urn:ietf:params:xml:ns:xmpp-tls")) {
            return new StartTLSFailureParser();
        } else if (element.equals("compress")) {
            return new CompressParser();
        } else if (element.equals("compressed")) {
            return new CompressedParser();
        } else if (element.equals("failure")
                && xmlns.equals("http://jabber.org/protocol/compress")) {
            return new CompressFailureParser();
        } else if (element.equals("proceed")) {
            return new TLSProceedParser();
        } else if (element.equals("enable") && xmlns.equals("urn:xmpp:sm:2")) {
            return new EnableStreamManagementParser();
        } else if (element.equals("enabled") && xmlns.equals("urn:xmpp:sm:2")) {
            return new StreamManagementEnabledParser();
        } else if (element.equals("failed") && xmlns.equals("urn:xmpp:sm:2")) {
            return new StreamManagementFailedParser();
        } else if (element.equals("resume") && xmlns.equals("urn:xmpp:sm:2")) {
            return new StreamResumeParser();
        } else if (element.equals("resumed") && xmlns.equals("urn:xmpp:sm:2")) {
            return new StreamResumedParser();
        } else if (element.equals("a") && xmlns.equals("urn:xmpp:sm:2")) {
            return new StanzaAckParser();
        } else if (element.equals("r") && xmlns.equals("urn:xmpp:sm:2")) {
            return new StanzaAckRequestParser();
        } else if (element.equals("handshake")) {
            return new ComponentHandshakeParser();
        }
        return new UnknownElementParser();
        
    }
}