XmlReader.java

  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					for (int i = 0; i < parser.getAttributeCount(); ++i) {
 69						tag.setAttribute(parser.getAttributeName(i),
 70								parser.getAttributeValue(i));
 71					}
 72					String xmlns = parser.getNamespace();
 73					if (xmlns != null) {
 74						tag.setAttribute("xmlns", xmlns);
 75					}
 76					return tag;
 77				} else if (parser.getEventType() == XmlPullParser.END_TAG) {
 78					return Tag.end(parser.getName());
 79				} else if (parser.getEventType() == XmlPullParser.TEXT) {
 80					return Tag.no(parser.getText());
 81				}
 82			}
 83
 84		} catch (Throwable throwable) {
 85			throw new IOException("xml parser mishandled "+throwable.getClass().getSimpleName()+"("+throwable.getMessage()+")", throwable);
 86		} finally {
 87			if (wakeLock.isHeld()) {
 88				try {
 89					wakeLock.release();
 90				} catch (RuntimeException re) {
 91					Log.d(Config.LOGTAG,"runtime exception releasing wakelock after exception "+re.getMessage());
 92				}
 93			}
 94		}
 95		return null;
 96	}
 97
 98	public Element readElement(Tag currentTag) throws XmlPullParserException,
 99			IOException {
100		Element element = new Element(currentTag.getName());
101		element.setAttributes(currentTag.getAttributes());
102		Tag nextTag = this.readTag();
103		if (nextTag == null) {
104			throw new IOException("interrupted mid tag");
105		}
106		if (nextTag.isNo()) {
107			element.setContent(nextTag.getName());
108			nextTag = this.readTag();
109			if (nextTag == null) {
110				throw new IOException("interrupted mid tag");
111			}
112		}
113		while (!nextTag.isEnd(element.getName())) {
114			if (!nextTag.isNo()) {
115				Element child = this.readElement(nextTag);
116				element.addChild(child);
117			}
118			nextTag = this.readTag();
119			if (nextTag == null) {
120				throw new IOException("interrupted mid tag");
121			}
122		}
123		return element;
124	}
125}