aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/xml/XmlReader.java
diff options
context:
space:
mode:
authorsteckbrief <steckbrief@chefmail.de>2015-05-03 22:25:46 +0200
committerlookshe <github@lookshe.org>2015-06-19 09:46:40 +0200
commit7382e3af9769f76fe4e19934a59e45a3f9858332 (patch)
treec37cdb03dfaeaccde7c8dd7c79887bf0de278f83 /src/main/java/de/thedevstack/conversationsplus/xml/XmlReader.java
parentb3b4a2902e37fb072e800f5dff0392755f5d4501 (diff)
renaming eu.siacs.conversations to de.thedevstack.conversationsplus
"renaming eu.siacs.conversations to de.thedevstack.conversationsplus" package renaming completed
Diffstat (limited to 'src/main/java/de/thedevstack/conversationsplus/xml/XmlReader.java')
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/xml/XmlReader.java141
1 files changed, 141 insertions, 0 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/xml/XmlReader.java b/src/main/java/de/thedevstack/conversationsplus/xml/XmlReader.java
new file mode 100644
index 00000000..d7f6edf5
--- /dev/null
+++ b/src/main/java/de/thedevstack/conversationsplus/xml/XmlReader.java
@@ -0,0 +1,141 @@
+package de.thedevstack.conversationsplus.xml;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+import org.xmlpull.v1.XmlPullParser;
+import org.xmlpull.v1.XmlPullParserException;
+
+import de.thedevstack.conversationsplus.Config;
+
+import android.os.PowerManager;
+import android.os.PowerManager.WakeLock;
+import android.util.Log;
+import android.util.Xml;
+
+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);
+ } 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 InputStream getInputStream() throws IOException {
+ if (this.is == null) {
+ throw new IOException();
+ }
+ return is;
+ }
+
+ 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) {
+ }
+ }
+ try {
+ while (this.is != null
+ && parser.next() != XmlPullParser.END_DOCUMENT) {
+ 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) {
+ Tag tag = Tag.end(parser.getName());
+ return tag;
+ } else if (parser.getEventType() == XmlPullParser.TEXT) {
+ Tag tag = Tag.no(parser.getText());
+ return tag;
+ }
+ }
+ if (wakeLock.isHeld()) {
+ try {
+ wakeLock.release();
+ } catch (RuntimeException re) {
+ }
+ }
+ } catch (ArrayIndexOutOfBoundsException e) {
+ throw new IOException(
+ "xml parser mishandled ArrayIndexOufOfBounds", e);
+ } catch (StringIndexOutOfBoundsException e) {
+ throw new IOException(
+ "xml parser mishandled StringIndexOufOfBounds", e);
+ } catch (NullPointerException e) {
+ throw new IOException("xml parser mishandled NullPointerException",
+ e);
+ } catch (IndexOutOfBoundsException e) {
+ throw new IOException("xml parser mishandled IndexOutOfBound", e);
+ }
+ 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("unterupted mid tag");
+ }
+ if (nextTag.isNo()) {
+ element.setContent(nextTag.getName());
+ nextTag = this.readTag();
+ if (nextTag == null) {
+ throw new IOException("unterupted 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("unterupted mid tag");
+ }
+ }
+ return element;
+ }
+}