aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/server/xmpp/router/StreamError.java
blob: f731f039a5ea216e436310da97aca1a88902d759 (plain) (blame)
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
package com.juick.server.xmpp.router;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;


/**
 * Created by vitalyster on 03.02.2017.
 */
public class StreamError {

    private String condition;
    private String text;

    public StreamError() {}

    public StreamError(String condition) {
        this.condition = condition;
    }

    public static StreamError parse(XmlPullParser parser) throws IOException, XmlPullParserException {
        StreamError streamError = new StreamError();
        final int initial = parser.getDepth();
        while (true) {
            int eventType = parser.next();
            if (eventType == XmlPullParser.START_TAG && parser.getDepth() == initial + 1) {
                final String tag = parser.getName();
                final String xmlns = parser.getNamespace();
                if (tag.equals("text") && xmlns.equals(StreamNamespaces.NS_XMPP_STREAMS)) {
                    streamError.text = XmlUtils.getTagText(parser);
                } else if (xmlns.equals(StreamNamespaces.NS_XMPP_STREAMS)) {
                    streamError.condition = tag;
                } else {
                    XmlUtils.skip(parser);
                }
            } else if (eventType == XmlPullParser.END_TAG && parser.getDepth() == initial) {
                break;
            }
        }
        return streamError;
    }

    public String getCondition() {
        return condition;
    }

    @Override
    public String toString() {
        return String.format("<stream:error><%s xmlns='%s'/></stream:error>", condition, StreamNamespaces.NS_XMPP_STREAMS);
    }

    public String getText() {
        return text;
    }
}