aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/xml/XmlReader.java
blob: a67442f49c3b6be7660cd35f5f8bebc3c94d0d66 (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
package de.pixart.messenger.xml;

import android.util.Log;
import android.util.Xml;

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

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import de.pixart.messenger.Config;

public class XmlReader {
    private XmlPullParser parser;
    private InputStream is;

    public XmlReader() {
        this.parser = Xml.newPullParser();
        try {
            this.parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
        } catch (XmlPullParserException e) {
            Log.d(Config.LOGTAG, "error setting namespace feature on parser");
        }
    }

    public void setInputStream(InputStream inputStream) throws IOException {
        if (inputStream == null) {
            throw new IOException();
        }
        this.is = inputStream;
        try {
            parser.setInput(new InputStreamReader(this.is));
        } catch (XmlPullParserException e) {
            throw new IOException("error resetting parser");
        }
    }

    public void reset() throws IOException {
        if (this.is == null) {
            throw new IOException();
        }
        try {
            parser.setInput(new InputStreamReader(this.is));
        } catch (XmlPullParserException e) {
            throw new IOException("error resetting parser");
        }
    }

    public Tag readTag() throws IOException {
        try {
            while (this.is != null && parser.next() != XmlPullParser.END_DOCUMENT) {
                if (parser.getEventType() == XmlPullParser.START_TAG) {
                    Tag tag = Tag.start(parser.getName());
                    final String xmlns = parser.getNamespace();
                    for (int i = 0; i < parser.getAttributeCount(); ++i) {
                        final String prefix = parser.getAttributePrefix(i);
                        String name;
                        if (prefix != null && !prefix.isEmpty()) {
                            name = prefix + ":" + parser.getAttributeName(i);
                        } else {
                            name = parser.getAttributeName(i);
                        }
                        tag.setAttribute(name, parser.getAttributeValue(i));
                    }
                    if (xmlns != null) {
                        tag.setAttribute("xmlns", xmlns);
                    }
                    return tag;
                } else if (parser.getEventType() == XmlPullParser.END_TAG) {
                    return Tag.end(parser.getName());
                } else if (parser.getEventType() == XmlPullParser.TEXT) {
                    return Tag.no(parser.getText());
                }
            }

        } catch (Throwable throwable) {
            throw new IOException("xml parser mishandled " + throwable.getClass().getSimpleName() + "(" + throwable.getMessage() + ")", throwable);
        }
        return null;
    }

    public Element readElement(Tag currentTag) throws XmlPullParserException,
            IOException {
        Element element = new Element(currentTag.getName());
        element.setAttributes(currentTag.getAttributes());
        Tag nextTag = this.readTag();
        if (nextTag == null) {
            throw new IOException("interrupted mid tag");
        }
        if (nextTag.isNo()) {
            element.setContent(nextTag.getName());
            nextTag = this.readTag();
            if (nextTag == null) {
                throw new IOException("interrupted mid tag");
            }
        }
        while (!nextTag.isEnd(element.getName())) {
            if (!nextTag.isNo()) {
                Element child = this.readElement(nextTag);
                element.addChild(child);
            }
            nextTag = this.readTag();
            if (nextTag == null) {
                throw new IOException("interrupted mid tag");
            }
        }
        return element;
    }
}