summaryrefslogtreecommitdiffstats
blob: 85d0e1f25a7fa134129f27fa888cb5fb07e42983 (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
156
/*
 * 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 final XMLParser xmlParser_;
    private final XMPPParserClient client_;
    private final PayloadParserFactoryCollection payloadParserFactories_;
    private int currentDepth_ = 0;
    private ElementParser currentElementParser_ = null;
    private boolean parseErrorOccurred_ = false;
    private Logger logger_ = Logger.getLogger(this.getClass().getName());
    
    public XMPPParser(XMPPParserClient parserClient,
            PayloadParserFactoryCollection payloadParserFactories) {
        client_ = parserClient;
        payloadParserFactories_ = payloadParserFactories;
        xmlParser_ = PlatformXMLParserFactory.createXMLParser(this);
    }
    
    public boolean parse(String data) {
        parseErrorOccurred_ = false;
        boolean xmlParseResult = false;
        try {
            xmlParseResult = xmlParser_.parse(data);
        } catch (Exception e) {
            parseErrorOccurred_ = true;
            logger_.log(java.util.logging.Level.WARNING, "Data " + data + " caused:\n" + e.getMessage(), e);
        }
        if (parseErrorOccurred_ || !xmlParseResult) {
            logger_.warning(String.format("When parsing, %b and %b",
                    parseErrorOccurred_, xmlParseResult));
            if (data != null) {
                logger_.warning("xml that caused failure: " + data);
            }
        }
        return xmlParseResult && !parseErrorOccurred_;
    }
    
    public void handleStartElement(String element, String ns,
            AttributeMap attributes) {
        if (!inStream()) {
            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 (!inElement()) {
                assert currentElementParser_ == null;
                currentElementParser_ = createElementParser(element, ns);
            }
            currentElementParser_.handleStartElement(element, ns, attributes);
        }
        ++currentDepth_;
    }
    
    public void handleEndElement(String element, String ns) {
        assert (inStream());
        if (inElement()) {
            assert currentElementParser_ != null;
            currentElementParser_.handleEndElement(element, ns);
            --currentDepth_;
            if (!inElement()) {
                client_.handleElement(currentElementParser_.getElement());
                currentElementParser_ = null;
            }
        } else {
            assert (element.equals("stream"));
            --currentDepth_;
            client_.handleStreamEnd();
        }
    }
    
    public void handleCharacterData(String data) {
        if (currentElementParser_ != null) {
            currentElementParser_.handleCharacterData(data);
        }
    }
    
    private boolean inStream() {
        return currentDepth_ > 0;
    }
    
    private boolean inElement() {
        return currentDepth_ > 1;
    }
    
    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("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();
        }
        return new UnknownElementParser();
        
    }
}