1package eu.siacs.conversations.xml;
2
3import android.os.PowerManager;
4import android.os.PowerManager.WakeLock;
5import android.util.Log;
6import android.util.Xml;
7
8import org.xmlpull.v1.XmlPullParser;
9import org.xmlpull.v1.XmlPullParserException;
10
11import java.io.IOException;
12import java.io.InputStream;
13import java.io.InputStreamReader;
14
15import eu.siacs.conversations.Config;
16
17public class XmlReader {
18 private XmlPullParser parser;
19 private PowerManager.WakeLock wakeLock;
20 private InputStream is;
21
22 public XmlReader(WakeLock wakeLock) {
23 this.parser = Xml.newPullParser();
24 try {
25 this.parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
26 } catch (XmlPullParserException e) {
27 Log.d(Config.LOGTAG, "error setting namespace feature on parser");
28 }
29 this.wakeLock = wakeLock;
30 }
31
32 public void setInputStream(InputStream inputStream) throws IOException {
33 if (inputStream == null) {
34 throw new IOException();
35 }
36 this.is = inputStream;
37 try {
38 parser.setInput(new InputStreamReader(this.is));
39 } catch (XmlPullParserException e) {
40 throw new IOException("error resetting parser");
41 }
42 }
43
44 public void reset() throws IOException {
45 if (this.is == null) {
46 throw new IOException();
47 }
48 try {
49 parser.setInput(new InputStreamReader(this.is));
50 } catch (XmlPullParserException e) {
51 throw new IOException("error resetting parser");
52 }
53 }
54
55 public Tag readTag() throws XmlPullParserException, IOException {
56 if (wakeLock.isHeld()) {
57 try {
58 wakeLock.release();
59 } catch (RuntimeException re) {
60 Log.d(Config.LOGTAG,"runtime exception releasing wakelock before reading tag "+re.getMessage());
61 }
62 }
63 try {
64 while (this.is != null && parser.next() != XmlPullParser.END_DOCUMENT) {
65 wakeLock.acquire();
66 if (parser.getEventType() == XmlPullParser.START_TAG) {
67 Tag tag = Tag.start(parser.getName());
68 final String xmlns = parser.getNamespace();
69 for (int i = 0; i < parser.getAttributeCount(); ++i) {
70 final String prefix = parser.getAttributePrefix(i);
71 String name;
72 if (prefix != null && !prefix.isEmpty()) {
73 name = prefix+":"+parser.getAttributeName(i);
74 } else {
75 name = parser.getAttributeName(i);
76 }
77 tag.setAttribute(name,parser.getAttributeValue(i));
78 }
79 if (xmlns != null) {
80 tag.setAttribute("xmlns", xmlns);
81 }
82 return tag;
83 } else if (parser.getEventType() == XmlPullParser.END_TAG) {
84 return Tag.end(parser.getName());
85 } else if (parser.getEventType() == XmlPullParser.TEXT) {
86 return Tag.no(parser.getText());
87 }
88 }
89
90 } catch (Throwable throwable) {
91 throw new IOException("xml parser mishandled "+throwable.getClass().getSimpleName()+"("+throwable.getMessage()+")", throwable);
92 } finally {
93 if (wakeLock.isHeld()) {
94 try {
95 wakeLock.release();
96 } catch (RuntimeException re) {
97 Log.d(Config.LOGTAG,"runtime exception releasing wakelock after exception "+re.getMessage());
98 }
99 }
100 }
101 return null;
102 }
103
104 public Element readElement(Tag currentTag) throws XmlPullParserException,
105 IOException {
106 Element element = new Element(currentTag.getName());
107 element.setAttributes(currentTag.getAttributes());
108 Tag nextTag = this.readTag();
109 if (nextTag == null) {
110 throw new IOException("interrupted mid tag");
111 }
112 if (nextTag.isNo()) {
113 element.setContent(nextTag.getName());
114 nextTag = this.readTag();
115 if (nextTag == null) {
116 throw new IOException("interrupted mid tag");
117 }
118 }
119 while (!nextTag.isEnd(element.getName())) {
120 if (!nextTag.isNo()) {
121 Element child = this.readElement(nextTag);
122 element.addChild(child);
123 }
124 nextTag = this.readTag();
125 if (nextTag == null) {
126 throw new IOException("interrupted mid tag");
127 }
128 }
129 return element;
130 }
131}