make sure that we always release wake lock even after throwing exception

This commit is contained in:
Daniel Gultsch 2016-06-22 12:22:03 +02:00
parent 34454ef2ec
commit d28d968985
1 changed files with 13 additions and 20 deletions

View File

@ -22,8 +22,7 @@ public class XmlReader {
public XmlReader(WakeLock wakeLock) { public XmlReader(WakeLock wakeLock) {
this.parser = Xml.newPullParser(); this.parser = Xml.newPullParser();
try { try {
this.parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, this.parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
true);
} catch (XmlPullParserException e) { } catch (XmlPullParserException e) {
Log.d(Config.LOGTAG, "error setting namespace feature on parser"); Log.d(Config.LOGTAG, "error setting namespace feature on parser");
} }
@ -42,13 +41,6 @@ public class XmlReader {
} }
} }
public InputStream getInputStream() throws IOException {
if (this.is == null) {
throw new IOException();
}
return is;
}
public void reset() throws IOException { public void reset() throws IOException {
if (this.is == null) { if (this.is == null) {
throw new IOException(); throw new IOException();
@ -65,11 +57,11 @@ public class XmlReader {
try { try {
wakeLock.release(); wakeLock.release();
} catch (RuntimeException re) { } catch (RuntimeException re) {
Log.d(Config.LOGTAG,"runtime exception releasing wakelock before reading tag "+re.getMessage());
} }
} }
try { try {
while (this.is != null while (this.is != null && parser.next() != XmlPullParser.END_DOCUMENT) {
&& parser.next() != XmlPullParser.END_DOCUMENT) {
wakeLock.acquire(); wakeLock.acquire();
if (parser.getEventType() == XmlPullParser.START_TAG) { if (parser.getEventType() == XmlPullParser.START_TAG) {
Tag tag = Tag.start(parser.getName()); Tag tag = Tag.start(parser.getName());
@ -83,21 +75,22 @@ public class XmlReader {
} }
return tag; return tag;
} else if (parser.getEventType() == XmlPullParser.END_TAG) { } else if (parser.getEventType() == XmlPullParser.END_TAG) {
Tag tag = Tag.end(parser.getName()); return Tag.end(parser.getName());
return tag;
} else if (parser.getEventType() == XmlPullParser.TEXT) { } else if (parser.getEventType() == XmlPullParser.TEXT) {
Tag tag = Tag.no(parser.getText()); return Tag.no(parser.getText());
return tag;
} }
} }
} catch (Exception e) {
throw new IOException("xml parser mishandled "+e.getClass().getName(), e);
} finally {
if (wakeLock.isHeld()) { if (wakeLock.isHeld()) {
try { try {
wakeLock.release(); wakeLock.release();
} catch (RuntimeException re) { } catch (RuntimeException re) {
Log.d(Config.LOGTAG,"runtime exception releasing wakelock after exception "+re.getMessage());
} }
} }
} catch (Exception e) {
throw new IOException("xml parser mishandled "+e.getClass().getName(), e);
} }
return null; return null;
} }
@ -108,13 +101,13 @@ public class XmlReader {
element.setAttributes(currentTag.getAttributes()); element.setAttributes(currentTag.getAttributes());
Tag nextTag = this.readTag(); Tag nextTag = this.readTag();
if (nextTag == null) { if (nextTag == null) {
throw new IOException("unterupted mid tag"); throw new IOException("interrupted mid tag");
} }
if (nextTag.isNo()) { if (nextTag.isNo()) {
element.setContent(nextTag.getName()); element.setContent(nextTag.getName());
nextTag = this.readTag(); nextTag = this.readTag();
if (nextTag == null) { if (nextTag == null) {
throw new IOException("unterupted mid tag"); throw new IOException("interrupted mid tag");
} }
} }
while (!nextTag.isEnd(element.getName())) { while (!nextTag.isEnd(element.getName())) {
@ -124,7 +117,7 @@ public class XmlReader {
} }
nextTag = this.readTag(); nextTag = this.readTag();
if (nextTag == null) { if (nextTag == null) {
throw new IOException("unterupted mid tag"); throw new IOException("interrupted mid tag");
} }
} }
return element; return element;