2014-02-28 17:46:01 +00:00
|
|
|
package eu.siacs.conversations.xml;
|
2014-01-30 15:42:35 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
|
|
|
import org.xmlpull.v1.XmlPullParser;
|
|
|
|
import org.xmlpull.v1.XmlPullParserException;
|
|
|
|
|
|
|
|
import android.os.PowerManager;
|
|
|
|
import android.os.PowerManager.WakeLock;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.util.Xml;
|
|
|
|
|
|
|
|
public class XmlReader {
|
|
|
|
private static final String LOGTAG = "xmppService";
|
|
|
|
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(LOGTAG,"error setting namespace feature on parser");
|
|
|
|
}
|
|
|
|
this.wakeLock = wakeLock;
|
|
|
|
}
|
|
|
|
|
2014-08-15 11:34:55 +00:00
|
|
|
public void setInputStream(InputStream inputStream) throws IOException {
|
|
|
|
if (inputStream==null) {
|
|
|
|
throw new IOException();
|
|
|
|
}
|
2014-01-30 15:42:35 +00:00
|
|
|
this.is = inputStream;
|
|
|
|
try {
|
|
|
|
parser.setInput(new InputStreamReader(this.is));
|
|
|
|
} catch (XmlPullParserException e) {
|
2014-08-15 11:34:55 +00:00
|
|
|
throw new IOException("error resetting parser");
|
2014-01-30 15:42:35 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-03 16:16:14 +00:00
|
|
|
|
2014-08-15 11:34:55 +00:00
|
|
|
public InputStream getInputStream() throws IOException {
|
|
|
|
if (this.is==null) {
|
|
|
|
throw new IOException();
|
|
|
|
}
|
2014-04-03 16:16:14 +00:00
|
|
|
return is;
|
|
|
|
}
|
|
|
|
|
2014-08-15 11:34:55 +00:00
|
|
|
public void reset() throws IOException {
|
|
|
|
if (this.is==null) {
|
|
|
|
throw new IOException();
|
|
|
|
}
|
2014-01-30 15:42:35 +00:00
|
|
|
try {
|
|
|
|
parser.setInput(new InputStreamReader(this.is));
|
|
|
|
} catch (XmlPullParserException e) {
|
2014-08-15 11:34:55 +00:00
|
|
|
throw new IOException("error resetting parser");
|
2014-01-30 15:42:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Tag readTag() throws XmlPullParserException, IOException {
|
|
|
|
if (wakeLock.isHeld()) {
|
2014-06-25 15:06:20 +00:00
|
|
|
try { wakeLock.release();} catch (RuntimeException re) {}
|
2014-01-30 15:42:35 +00:00
|
|
|
}
|
2014-04-17 08:34:25 +00:00
|
|
|
try {
|
2014-08-09 09:37:30 +00:00
|
|
|
while(this.is != null && parser.next() != XmlPullParser.END_DOCUMENT) {
|
2014-04-17 08:34:25 +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) {
|
|
|
|
Tag tag = Tag.end(parser.getName());
|
|
|
|
return tag;
|
|
|
|
} else if (parser.getEventType() == XmlPullParser.TEXT) {
|
|
|
|
Tag tag = Tag.no(parser.getText());
|
|
|
|
return tag;
|
2014-02-19 00:35:23 +00:00
|
|
|
}
|
2014-01-30 15:42:35 +00:00
|
|
|
}
|
2014-04-17 08:34:25 +00:00
|
|
|
if (wakeLock.isHeld()) {
|
2014-06-25 15:06:20 +00:00
|
|
|
try { wakeLock.release();} catch (RuntimeException re) {}
|
2014-01-30 15:42:35 +00:00
|
|
|
}
|
2014-04-17 08:34:25 +00:00
|
|
|
} catch (ArrayIndexOutOfBoundsException e) {
|
|
|
|
throw new IOException("xml parser mishandled ArrayIndexOufOfBounds", e);
|
2014-06-25 15:24:03 +00:00
|
|
|
} catch (StringIndexOutOfBoundsException e) {
|
|
|
|
throw new IOException("xml parser mishandled StringIndexOufOfBounds", e);
|
2014-08-30 06:31:05 +00:00
|
|
|
} catch (NullPointerException e) {
|
|
|
|
throw new IOException("xml parser mishandled NullPointerException", e);
|
|
|
|
} catch (IndexOutOfBoundsException e) {
|
|
|
|
throw new IOException("xml parser mishandled IndexOutOfBound", e);
|
2014-01-30 15:42:35 +00:00
|
|
|
}
|
2014-04-17 08:34:25 +00:00
|
|
|
return null;
|
2014-01-30 15:42:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Element readElement(Tag currentTag) throws XmlPullParserException, IOException {
|
|
|
|
Element element = new Element(currentTag.getName());
|
|
|
|
element.setAttributes(currentTag.getAttributes());
|
|
|
|
Tag nextTag = this.readTag();
|
2014-06-25 15:54:00 +00:00
|
|
|
if (nextTag == null) {
|
|
|
|
throw new IOException("unterupted mid tag");
|
|
|
|
}
|
2014-01-30 15:42:35 +00:00
|
|
|
if(nextTag.isNo()) {
|
|
|
|
element.setContent(nextTag.getName());
|
|
|
|
nextTag = this.readTag();
|
2014-06-25 15:54:00 +00:00
|
|
|
if (nextTag == null) {
|
|
|
|
throw new IOException("unterupted mid tag");
|
|
|
|
}
|
2014-03-28 12:32:36 +00:00
|
|
|
}
|
2014-01-30 15:42:35 +00:00
|
|
|
while(!nextTag.isEnd(element.getName())) {
|
2014-02-03 15:04:27 +00:00
|
|
|
if (!nextTag.isNo()) {
|
|
|
|
Element child = this.readElement(nextTag);
|
|
|
|
element.addChild(child);
|
|
|
|
}
|
2014-01-30 15:42:35 +00:00
|
|
|
nextTag = this.readTag();
|
2014-05-14 16:39:59 +00:00
|
|
|
if (nextTag == null) {
|
|
|
|
throw new IOException("unterupted mid tag");
|
|
|
|
}
|
2014-01-30 15:42:35 +00:00
|
|
|
}
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
}
|