aboutsummaryrefslogtreecommitdiffstats
path: root/src/de/gultsch/chat/xml
diff options
context:
space:
mode:
authorDaniel Gultsch <daniel.gultsch@rwth-aachen.de>2014-01-30 16:42:35 +0100
committerDaniel Gultsch <daniel.gultsch@rwth-aachen.de>2014-01-30 16:42:35 +0100
commit6c5c3ac2decac75ec3208d47912e67c4e1a33548 (patch)
treec6cdcc5d76608369da08eb76891c48ffefbb9a48 /src/de/gultsch/chat/xml
parentad11dab6359a1eb2b6921d36117093066999fb96 (diff)
first draft on xml parser and communication. a long way to go. code definitly not perfect. will refactor asap
Diffstat (limited to '')
-rw-r--r--src/de/gultsch/chat/xml/Element.java65
-rw-r--r--src/de/gultsch/chat/xml/Tag.java99
-rw-r--r--src/de/gultsch/chat/xml/TagWriter.java46
-rw-r--r--src/de/gultsch/chat/xml/XmlReader.java91
4 files changed, 301 insertions, 0 deletions
diff --git a/src/de/gultsch/chat/xml/Element.java b/src/de/gultsch/chat/xml/Element.java
new file mode 100644
index 00000000..d6d1b23d
--- /dev/null
+++ b/src/de/gultsch/chat/xml/Element.java
@@ -0,0 +1,65 @@
+package de.gultsch.chat.xml;
+
+import java.util.ArrayList;
+import java.util.Hashtable;
+import java.util.List;
+
+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 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 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/de/gultsch/chat/xml/Tag.java b/src/de/gultsch/chat/xml/Tag.java
new file mode 100644
index 00000000..275229cf
--- /dev/null
+++ b/src/de/gultsch/chat/xml/Tag.java
@@ -0,0 +1,99 @@
+package de.gultsch.chat.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/de/gultsch/chat/xml/TagWriter.java b/src/de/gultsch/chat/xml/TagWriter.java
new file mode 100644
index 00000000..35f27477
--- /dev/null
+++ b/src/de/gultsch/chat/xml/TagWriter.java
@@ -0,0 +1,46 @@
+package de.gultsch.chat.xml;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+
+import android.util.Log;
+
+public class TagWriter {
+
+ OutputStreamWriter writer;
+
+ public TagWriter() {
+
+ }
+
+ public TagWriter(OutputStream out) {
+ this.setOutputStream(out);
+ }
+
+ public void setOutputStream(OutputStream out) {
+ this.writer = new OutputStreamWriter(out);
+ }
+
+ public TagWriter beginDocument() throws IOException {
+ writer.write("<?xml version='1.0'?>");
+ return this;
+ }
+
+ public TagWriter writeTag(Tag tag) throws IOException {
+ writer.write(tag.toString());
+ return this;
+ }
+
+ public void flush() throws IOException {
+ writer.flush();
+ }
+
+ public void writeString(String string) throws IOException {
+ writer.write(string);
+ }
+
+ public void writeElement(Element element) throws IOException {
+ writer.write(element.toString());
+ }
+}
diff --git a/src/de/gultsch/chat/xml/XmlReader.java b/src/de/gultsch/chat/xml/XmlReader.java
new file mode 100644
index 00000000..e086c8ae
--- /dev/null
+++ b/src/de/gultsch/chat/xml/XmlReader.java
@@ -0,0 +1,91 @@
+package de.gultsch.chat.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
+ }
+ 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));
+ }
+ 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());
+ element.setAttributes(currentTag.getAttributes());
+ Tag nextTag = this.readTag();
+ if(nextTag.isNo()) {
+ element.setContent(nextTag.getName());
+ nextTag = this.readTag();
+ }
+ while(!nextTag.isEnd(element.getName())) {
+ Element child = this.readElement(nextTag);
+ element.addChild(child);
+ nextTag = this.readTag();
+ }
+ return element;
+ }
+}