conversations-classic/src/main/java/eu/siacs/conversations/xml/XmlReader.java

126 lines
3.5 KiB
Java
Raw Normal View History

2014-10-22 16:38:44 +00:00
package eu.siacs.conversations.xml;
2015-07-20 12:26:29 +00:00
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.util.Xml;
2014-10-22 16:38:44 +00:00
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
2015-07-20 12:26:29 +00:00
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
2014-10-22 16:38:44 +00:00
2015-07-20 12:26:29 +00:00
import eu.siacs.conversations.Config;
2014-10-22 16:38:44 +00:00
public class XmlReader {
private XmlPullParser parser;
private PowerManager.WakeLock wakeLock;
private InputStream is;
public XmlReader(WakeLock wakeLock) {
this.parser = Xml.newPullParser();
try {
this.parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
2014-10-22 16:38:44 +00:00
} catch (XmlPullParserException e) {
Log.d(Config.LOGTAG, "error setting namespace feature on parser");
}
this.wakeLock = wakeLock;
}
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 XmlPullParserException, IOException {
if (wakeLock.isHeld()) {
try {
wakeLock.release();
} catch (RuntimeException re) {
Log.d(Config.LOGTAG,"runtime exception releasing wakelock before reading tag "+re.getMessage());
2014-10-22 16:38:44 +00:00
}
}
try {
while (this.is != null && parser.next() != XmlPullParser.END_DOCUMENT) {
2014-10-22 16:38:44 +00:00
wakeLock.acquire();
if (parser.getEventType() == XmlPullParser.START_TAG) {
Tag tag = Tag.start(parser.getName());
for (int i = 0; i < parser.getAttributeCount(); ++i) {
tag.setAttribute(parser.getAttributeName(i),
parser.getAttributeValue(i));
}
String xmlns = parser.getNamespace();
if (xmlns != null) {
tag.setAttribute("xmlns", xmlns);
}
return tag;
} else if (parser.getEventType() == XmlPullParser.END_TAG) {
return Tag.end(parser.getName());
2014-10-22 16:38:44 +00:00
} else if (parser.getEventType() == XmlPullParser.TEXT) {
return Tag.no(parser.getText());
2014-10-22 16:38:44 +00:00
}
}
2016-08-02 08:58:54 +00:00
} catch (Throwable throwable) {
2017-01-22 11:54:39 +00:00
throw new IOException("xml parser mishandled "+throwable.getClass().getSimpleName()+"("+throwable.getMessage()+")", throwable);
} finally {
2014-10-22 16:38:44 +00:00
if (wakeLock.isHeld()) {
try {
wakeLock.release();
} catch (RuntimeException re) {
Log.d(Config.LOGTAG,"runtime exception releasing wakelock after exception "+re.getMessage());
2014-10-22 16:38:44 +00:00
}
}
}
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");
2014-10-22 16:38:44 +00:00
}
if (nextTag.isNo()) {
element.setContent(nextTag.getName());
nextTag = this.readTag();
if (nextTag == null) {
throw new IOException("interrupted mid tag");
2014-10-22 16:38:44 +00:00
}
}
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");
2014-10-22 16:38:44 +00:00
}
}
return element;
}
}