diff options
author | iNPUTmice <daniel@gultsch.de> | 2014-08-31 16:28:21 +0200 |
---|---|---|
committer | iNPUTmice <daniel@gultsch.de> | 2014-08-31 16:28:21 +0200 |
commit | 1ac5be485502e7d6d4c117335e083c684739e6af (patch) | |
tree | 279c22e269158dde838f31ebcf3daf4272573583 /src/eu/siacs/conversations/xml | |
parent | 8d456085e57334dc34707a49666006619e2c77c6 (diff) |
some code cleanup
Diffstat (limited to 'src/eu/siacs/conversations/xml')
-rw-r--r-- | src/eu/siacs/conversations/xml/Element.java | 6 | ||||
-rw-r--r-- | src/eu/siacs/conversations/xml/Tag.java | 51 | ||||
-rw-r--r-- | src/eu/siacs/conversations/xml/TagWriter.java | 53 | ||||
-rw-r--r-- | src/eu/siacs/conversations/xml/XmlReader.java | 82 |
4 files changed, 104 insertions, 88 deletions
diff --git a/src/eu/siacs/conversations/xml/Element.java b/src/eu/siacs/conversations/xml/Element.java index 3d22ba6a..d23a81ba 100644 --- a/src/eu/siacs/conversations/xml/Element.java +++ b/src/eu/siacs/conversations/xml/Element.java @@ -146,10 +146,10 @@ public class Element { } public void setAttribute(String name, long value) { - this.setAttribute(name, ""+value); + this.setAttribute(name, "" + value); } - + public void setAttribute(String name, int value) { - this.setAttribute(name, ""+value); + this.setAttribute(name, "" + value); } } diff --git a/src/eu/siacs/conversations/xml/Tag.java b/src/eu/siacs/conversations/xml/Tag.java index 2d5ffdbc..2b393397 100644 --- a/src/eu/siacs/conversations/xml/Tag.java +++ b/src/eu/siacs/conversations/xml/Tag.java @@ -10,77 +10,78 @@ public class Tag { 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); + return new Tag(NO, text); } - + public static Tag start(String name) { - return new Tag(START,name); + return new Tag(START, name); } - + public static Tag end(String name) { - return new Tag(END,name); + return new Tag(END, name); } - + public static Tag empty(String name) { - return new Tag(EMPTY,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) { - if (needle==null) return false; + if (needle == null) + return false; return (this.type == START) && (needle.equals(this.name)); } - + public boolean isEnd(String needle) { - if (needle==null) return false; + if (needle == null) + return false; return (this.type == END) && (needle.equals(this.name)); } - + public boolean isNo() { return (this.type == NO); } - + public String toString() { StringBuilder tagOutput = new StringBuilder(); tagOutput.append('<'); - if (type==END) { + if (type == END) { tagOutput.append('/'); } tagOutput.append(name); - if(type!=END) { + 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(); + while (it.hasNext()) { + Entry<String, String> entry = it.next(); tagOutput.append(' '); tagOutput.append(entry.getKey()); tagOutput.append("=\""); @@ -88,7 +89,7 @@ public class Tag { tagOutput.append('"'); } } - if (type==EMPTY) { + if (type == EMPTY) { tagOutput.append('/'); } tagOutput.append('>'); diff --git a/src/eu/siacs/conversations/xml/TagWriter.java b/src/eu/siacs/conversations/xml/TagWriter.java index 4828d5d9..f11c1846 100644 --- a/src/eu/siacs/conversations/xml/TagWriter.java +++ b/src/eu/siacs/conversations/xml/TagWriter.java @@ -8,22 +8,23 @@ import java.util.concurrent.LinkedBlockingQueue; import eu.siacs.conversations.xmpp.stanzas.AbstractStanza; public class TagWriter { - + private OutputStream plainOutputStream; private OutputStreamWriter outputStream; private boolean finshed = false; private LinkedBlockingQueue<AbstractStanza> writeQueue = new LinkedBlockingQueue<AbstractStanza>(); private Thread asyncStanzaWriter = new Thread() { private boolean shouldStop = false; + @Override public void run() { - while(!shouldStop) { - if ((finshed)&&(writeQueue.size() == 0)) { + while (!shouldStop) { + if ((finshed) && (writeQueue.size() == 0)) { return; } try { AbstractStanza output = writeQueue.take(); - if (outputStream==null) { + if (outputStream == null) { shouldStop = true; } else { outputStream.write(output.toString()); @@ -37,12 +38,12 @@ public class TagWriter { } } }; - + public TagWriter() { } - + public void setOutputStream(OutputStream out) throws IOException { - if (out==null) { + if (out == null) { throw new IOException(); } this.plainOutputStream = out; @@ -50,23 +51,23 @@ public class TagWriter { } public OutputStream getOutputStream() throws IOException { - if (this.plainOutputStream==null) { + if (this.plainOutputStream == null) { throw new IOException(); } return this.plainOutputStream; } public TagWriter beginDocument() throws IOException { - if (outputStream==null) { + if (outputStream == null) { throw new IOException("output stream was null"); } outputStream.write("<?xml version='1.0'?>"); outputStream.flush(); return this; } - + public TagWriter writeTag(Tag tag) throws IOException { - if (outputStream==null) { + if (outputStream == null) { throw new IOException("output stream was null"); } outputStream.write(tag.toString()); @@ -75,34 +76,34 @@ public class TagWriter { } public TagWriter writeElement(Element element) throws IOException { - if (outputStream==null) { + if (outputStream == null) { throw new IOException("output stream was null"); } outputStream.write(element.toString()); outputStream.flush(); return this; } - + public TagWriter writeStanzaAsync(AbstractStanza stanza) { - if (finshed) { - return this; - } else { - if (!asyncStanzaWriter.isAlive()) { - try { - asyncStanzaWriter.start(); - } catch (IllegalThreadStateException e) { - //already started - } + if (finshed) { + return this; + } else { + if (!asyncStanzaWriter.isAlive()) { + try { + asyncStanzaWriter.start(); + } catch (IllegalThreadStateException e) { + // already started } - writeQueue.add(stanza); - return this; } + writeQueue.add(stanza); + return this; + } } - + public void finish() { this.finshed = true; } - + public boolean finished() { return (this.writeQueue.size() == 0); } diff --git a/src/eu/siacs/conversations/xml/XmlReader.java b/src/eu/siacs/conversations/xml/XmlReader.java index cf6b8c73..52d3d46a 100644 --- a/src/eu/siacs/conversations/xml/XmlReader.java +++ b/src/eu/siacs/conversations/xml/XmlReader.java @@ -7,13 +7,14 @@ import java.io.InputStreamReader; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; +import eu.siacs.conversations.Config; + 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; @@ -21,15 +22,16 @@ public class XmlReader { public XmlReader(WakeLock wakeLock) { this.parser = Xml.newPullParser(); try { - this.parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES,true); + this.parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, + true); } catch (XmlPullParserException e) { - Log.d(LOGTAG,"error setting namespace feature on parser"); + Log.d(Config.LOGTAG, "error setting namespace feature on parser"); } this.wakeLock = wakeLock; } - + public void setInputStream(InputStream inputStream) throws IOException { - if (inputStream==null) { + if (inputStream == null) { throw new IOException(); } this.is = inputStream; @@ -41,14 +43,14 @@ public class XmlReader { } public InputStream getInputStream() throws IOException { - if (this.is==null) { + if (this.is == null) { throw new IOException(); } return is; } public void reset() throws IOException { - if (this.is==null) { + if (this.is == null) { throw new IOException(); } try { @@ -57,62 +59,74 @@ public class XmlReader { throw new IOException("error resetting parser"); } } - + public Tag readTag() throws XmlPullParserException, IOException { if (wakeLock.isHeld()) { - try { wakeLock.release();} catch (RuntimeException re) {} + 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; + 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) {} + try { + wakeLock.release(); + } catch (RuntimeException re) { + } } } catch (ArrayIndexOutOfBoundsException e) { - throw new IOException("xml parser mishandled ArrayIndexOufOfBounds", e); + throw new IOException( + "xml parser mishandled ArrayIndexOufOfBounds", e); } catch (StringIndexOutOfBoundsException e) { - throw new IOException("xml parser mishandled StringIndexOufOfBounds", e); + throw new IOException( + "xml parser mishandled StringIndexOufOfBounds", e); } catch (NullPointerException e) { - throw new IOException("xml parser mishandled 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 { + 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()) { + if (nextTag.isNo()) { element.setContent(nextTag.getName()); nextTag = this.readTag(); if (nextTag == null) { throw new IOException("unterupted mid tag"); } } - while(!nextTag.isEnd(element.getName())) { + while (!nextTag.isEnd(element.getName())) { if (!nextTag.isNo()) { Element child = this.readElement(nextTag); element.addChild(child); |