aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/siacs/conversations/xml
diff options
context:
space:
mode:
Diffstat (limited to 'src/eu/siacs/conversations/xml')
-rw-r--r--src/eu/siacs/conversations/xml/Element.java101
-rw-r--r--src/eu/siacs/conversations/xml/Tag.java99
-rw-r--r--src/eu/siacs/conversations/xml/TagWriter.java64
-rw-r--r--src/eu/siacs/conversations/xml/XmlReader.java102
4 files changed, 366 insertions, 0 deletions
diff --git a/src/eu/siacs/conversations/xml/Element.java b/src/eu/siacs/conversations/xml/Element.java
new file mode 100644
index 00000000..ad95ef9c
--- /dev/null
+++ b/src/eu/siacs/conversations/xml/Element.java
@@ -0,0 +1,101 @@
+package eu.siacs.conversations.xml;
+
+import java.util.ArrayList;
+import java.util.Hashtable;
+import java.util.List;
+
+import android.util.Log;
+
+public class Element {
+ protected String name;
+ protected Hashtable<String, String> attributes = new Hashtable<String, String>();
+ protected String content;
+ protected List<Element> children = new ArrayList<Element>();
+
+ public Element(String name) {
+ this.name = name;
+ }
+
+ public Element addChild(Element child) {
+ this.content = null;
+ children.add(child);
+ return this;
+ }
+
+ public Element setContent(String content) {
+ this.content = content;
+ this.children.clear();
+ return this;
+ }
+
+ public Element findChild(String name) {
+ for(Element child : this.children) {
+ if (child.getName().equals(name)) {
+ return child;
+ }
+ }
+ return null;
+ }
+
+ public boolean hasChild(String name) {
+ for(Element child : this.children) {
+ if (child.getName().equals(name)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public List<Element> getChildren() {
+ return this.children;
+ }
+
+ public String getContent() {
+ return content;
+ }
+
+ public Element setAttribute(String name, String value) {
+ this.attributes.put(name, value);
+ return this;
+ }
+
+ public Element setAttributes(Hashtable<String, String> attributes) {
+ this.attributes = attributes;
+ return this;
+ }
+
+ public String getAttribute(String name) {
+ if (this.attributes.containsKey(name)) {
+ return this.attributes.get(name);
+ } else {
+ return null;
+ }
+ }
+
+ public String toString() {
+ StringBuilder elementOutput = new StringBuilder();
+ if ((content==null)&&(children.size() == 0)) {
+ Tag emptyTag = Tag.empty(name);
+ emptyTag.setAtttributes(this.attributes);
+ elementOutput.append(emptyTag.toString());
+ } else {
+ Tag startTag = Tag.start(name);
+ startTag.setAtttributes(this.attributes);
+ elementOutput.append(startTag);
+ if (content!=null) {
+ elementOutput.append(content);
+ } else {
+ for(Element child : children) {
+ elementOutput.append(child.toString());
+ }
+ }
+ Tag endTag = Tag.end(name);
+ elementOutput.append(endTag);
+ }
+ return elementOutput.toString();
+ }
+
+ public String getName() {
+ return name;
+ }
+}
diff --git a/src/eu/siacs/conversations/xml/Tag.java b/src/eu/siacs/conversations/xml/Tag.java
new file mode 100644
index 00000000..970cf0ae
--- /dev/null
+++ b/src/eu/siacs/conversations/xml/Tag.java
@@ -0,0 +1,99 @@
+package eu.siacs.conversations.xml;
+
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.Map.Entry;
+import java.util.Set;
+
+public class Tag {
+ public static final int NO = -1;
+ public static final int START = 0;
+ public static final int END = 1;
+ public static final int EMPTY = 2;
+
+ protected int type;
+ protected String name;
+ protected Hashtable<String, String> attributes = new Hashtable<String, String>();
+
+ protected Tag(int type, String name) {
+ this.type = type;
+ this.name = name;
+ }
+
+
+ public static Tag no(String text) {
+ return new Tag(NO,text);
+ }
+
+ public static Tag start(String name) {
+ return new Tag(START,name);
+ }
+
+ public static Tag end(String name) {
+ return new Tag(END,name);
+ }
+
+ public static Tag empty(String name) {
+ return new Tag(EMPTY,name);
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getAttribute(String attrName) {
+ return this.attributes.get(attrName);
+ }
+
+ public Tag setAttribute(String attrName, String attrValue) {
+ this.attributes.put(attrName, attrValue);
+ return this;
+ }
+
+ public Tag setAtttributes(Hashtable<String, String> attributes) {
+ this.attributes = attributes;
+ return this;
+ }
+
+ public boolean isStart(String needle) {
+ return (this.type == START) && (this.name.equals(needle));
+ }
+
+ public boolean isEnd(String needle) {
+ return (this.type == END) && (this.name.equals(needle));
+ }
+
+ public boolean isNo() {
+ return (this.type == NO);
+ }
+
+ public String toString() {
+ StringBuilder tagOutput = new StringBuilder();
+ tagOutput.append('<');
+ if (type==END) {
+ tagOutput.append('/');
+ }
+ tagOutput.append(name);
+ if(type!=END) {
+ Set<Entry<String, String>> attributeSet = attributes.entrySet();
+ Iterator<Entry<String, String>> it = attributeSet.iterator();
+ while(it.hasNext()) {
+ Entry<String,String> entry = it.next();
+ tagOutput.append(' ');
+ tagOutput.append(entry.getKey());
+ tagOutput.append("=\"");
+ tagOutput.append(entry.getValue());
+ tagOutput.append('"');
+ }
+ }
+ if (type==EMPTY) {
+ tagOutput.append('/');
+ }
+ tagOutput.append('>');
+ return tagOutput.toString();
+ }
+
+ public Hashtable<String, String> getAttributes() {
+ return this.attributes;
+ }
+}
diff --git a/src/eu/siacs/conversations/xml/TagWriter.java b/src/eu/siacs/conversations/xml/TagWriter.java
new file mode 100644
index 00000000..109078ca
--- /dev/null
+++ b/src/eu/siacs/conversations/xml/TagWriter.java
@@ -0,0 +1,64 @@
+package eu.siacs.conversations.xml;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.util.concurrent.LinkedBlockingQueue;
+
+import android.util.Log;
+
+public class TagWriter {
+
+ private OutputStreamWriter outputStream;
+ private LinkedBlockingQueue<String> writeQueue = new LinkedBlockingQueue<String>();
+ private Thread writer = new Thread() {
+ public boolean shouldStop = false;
+ @Override
+ public void run() {
+ while(!shouldStop) {
+ try {
+ String output = writeQueue.take();
+ outputStream.write(output);
+ outputStream.flush();
+ } catch (IOException e) {
+ Log.d("xmppService", "error writing to stream");
+ } catch (InterruptedException e) {
+
+ }
+ }
+ }
+ };
+
+
+ public TagWriter() {
+
+ }
+
+ public TagWriter(OutputStream out) {
+ this.setOutputStream(out);
+ writer.start();
+ }
+
+ public void setOutputStream(OutputStream out) {
+ this.outputStream = new OutputStreamWriter(out);
+ if (!writer.isAlive()) writer.start();
+ }
+
+ public TagWriter beginDocument() {
+ writeQueue.add("<?xml version='1.0'?>");
+ return this;
+ }
+
+ public TagWriter writeTag(Tag tag) {
+ writeQueue.add(tag.toString());
+ return this;
+ }
+
+ public void writeString(String string) {
+ writeQueue.add(string);
+ }
+
+ public void writeElement(Element element) {
+ writeQueue.add(element.toString());
+ }
+}
diff --git a/src/eu/siacs/conversations/xml/XmlReader.java b/src/eu/siacs/conversations/xml/XmlReader.java
new file mode 100644
index 00000000..131141dd
--- /dev/null
+++ b/src/eu/siacs/conversations/xml/XmlReader.java
@@ -0,0 +1,102 @@
+package eu.siacs.conversations.xml;
+
+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;
+ }
+
+ public void setInputStream(InputStream inputStream) {
+ this.is = inputStream;
+ try {
+ parser.setInput(new InputStreamReader(this.is));
+ } catch (XmlPullParserException e) {
+ Log.d(LOGTAG,"error setting input stream");
+ }
+ }
+
+ public void reset() {
+ try {
+ parser.setInput(new InputStreamReader(this.is));
+ } catch (XmlPullParserException e) {
+ Log.d(LOGTAG,"error resetting input stream");
+ }
+ }
+
+ public Tag readTag() throws XmlPullParserException, IOException {
+ if (wakeLock.isHeld()) {
+ //Log.d(LOGTAG,"there was a wake lock. releasing it till next event");
+ wakeLock.release(); //release wake look while waiting on next parser event
+ }
+ //Log.d(LOGTAG,"waiting for new event...");
+ while(parser.next() != XmlPullParser.END_DOCUMENT) {
+ //Log.d(LOGTAG,"found new event. acquiring wake lock");
+ 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()) {
+ wakeLock.release();
+ }
+ return null; //end document;
+ }
+
+ public Element readElement(Tag currentTag) throws XmlPullParserException, IOException {
+ Element element = new Element(currentTag.getName());
+ //Log.d(LOGTAG,"trying to read element "+element.getName());
+ element.setAttributes(currentTag.getAttributes());
+ Tag nextTag = this.readTag();
+ //Log.d(LOGTAG,"next Tag is: "+nextTag.toString());
+ if(nextTag.isNo()) {
+ element.setContent(nextTag.getName());
+ nextTag = this.readTag();
+ }
+ //Log.d(LOGTAG,"reading till the end of "+element.getName());
+ while(!nextTag.isEnd(element.getName())) {
+ if (!nextTag.isNo()) {
+ Element child = this.readElement(nextTag);
+ element.addChild(child);
+ }
+ nextTag = this.readTag();
+ }
+ //Log.d(LOGTAG,"return with element"+element);
+ return element;
+ }
+}