summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/com/isode/stroke/elements/UserTune.java130
-rw-r--r--src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java2
-rw-r--r--src/com/isode/stroke/parser/payloadparsers/UserTuneParser.java87
-rw-r--r--src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java1
-rw-r--r--src/com/isode/stroke/serializer/payloadserializers/UserTuneSerializer.java59
-rw-r--r--test/com/isode/stroke/parser/payloadparsers/UserTuneParserTest.java57
-rw-r--r--test/com/isode/stroke/serializer/payloadserializers/UserTuneSerializerTest.java55
7 files changed, 390 insertions, 1 deletions
diff --git a/src/com/isode/stroke/elements/UserTune.java b/src/com/isode/stroke/elements/UserTune.java
new file mode 100644
index 0000000..9e651a5
--- /dev/null
+++ b/src/com/isode/stroke/elements/UserTune.java
@@ -0,0 +1,130 @@
+/*
+ * Copyright (c) 2014 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.elements;
+
+import com.isode.stroke.elements.Payload;
+
+public class UserTune extends Payload {
+
+ private Integer rating;
+ private String title;
+ private String track;
+ private String artist;
+ private String uri;
+ private String source;
+ private Integer length;
+
+ /**
+ * Default Constructor.
+ */
+ public UserTune() {
+
+ }
+
+ /**
+ * @return rating.
+ */
+ public Integer getRating() {
+ return rating;
+ }
+
+ /**
+ * @param rating.
+ */
+ public void setRating(Integer value) {
+ this.rating = value;
+ }
+
+ /**
+ * @return title.
+ */
+ public String getTitle() {
+ return title;
+ }
+
+ /**
+ * @param title.
+ */
+ public void setTitle(String value) {
+ this.title = value;
+ }
+
+ /**
+ * @return track.
+ */
+ public String getTrack() {
+ return track;
+ }
+
+ /**
+ * @param track.
+ */
+ public void setTrack(String value) {
+ this.track = value;
+ }
+
+ /**
+ * @return artist.
+ */
+ public String getArtist() {
+ return artist;
+ }
+
+ /**
+ * @param artist.
+ */
+ public void setArtist(String value) {
+ this.artist = value;
+ }
+
+ /**
+ * @return URI.
+ */
+ public String getURI() {
+ return uri;
+ }
+
+ /**
+ * @param URI.
+ */
+ public void setURI(String value) {
+ this.uri = value;
+ }
+
+ /**
+ * @return source.
+ */
+ public String getSource() {
+ return source;
+ }
+
+ /**
+ * @param source.
+ */
+ public void setSource(String value) {
+ this.source = value;
+ }
+
+ /**
+ * @return length.
+ */
+ public Integer getLength() {
+ return length;
+ }
+
+ /**
+ * @param length.
+ */
+ public void setLength(Integer value) {
+ this.length = value;
+ }
+}
diff --git a/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java b/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java
index 7c9679b..ef491dd 100644
--- a/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java
+++ b/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java
@@ -71,7 +71,7 @@ public class FullPayloadParserFactoryCollection extends PayloadParserFactoryColl
addFactory(new GenericPayloadParserFactory2<MAMResultParser>("result", "urn:xmpp:mam:0", this, MAMResultParser.class));
addFactory(new GenericPayloadParserFactory<MAMQueryParser>("query", "urn:xmpp:mam:0", MAMQueryParser.class));
addFactory(new GenericPayloadParserFactory<MAMFinParser>("fin", "urn:xmpp:mam:0", MAMFinParser.class));
-
+ addFactory(new GenericPayloadParserFactory<UserTuneParser>("tune", "http://jabber.org/protocol/tune", UserTuneParser.class));
//addFactory(new NicknameParserFactory());
PayloadParserFactory defaultFactory = new RawXMLPayloadParserFactory();
diff --git a/src/com/isode/stroke/parser/payloadparsers/UserTuneParser.java b/src/com/isode/stroke/parser/payloadparsers/UserTuneParser.java
new file mode 100644
index 0000000..3526150
--- /dev/null
+++ b/src/com/isode/stroke/parser/payloadparsers/UserTuneParser.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright (c) 2014 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.parser.payloadparsers;
+
+import com.isode.stroke.parser.GenericPayloadParser;
+import com.isode.stroke.parser.AttributeMap;
+import com.isode.stroke.elements.UserTune;
+import com.isode.stroke.base.NotNull;
+
+public class UserTuneParser extends GenericPayloadParser<UserTune> {
+
+ private int level = 0;
+ private String currentText;
+
+ public UserTuneParser() {
+ super(new UserTune());
+ }
+
+ /**
+ * @param element, NotNull.
+ * @param ns.
+ * @param attributes.
+ */
+ @Override
+ public void handleStartElement(String element, String ns, AttributeMap attributes) {
+ if (level == 1) {
+ currentText = "";
+ }
+ ++level;
+ }
+
+ /**
+ * @param element, NotNull.
+ * @param ns.
+ */
+ @Override
+ public void handleEndElement(String element, String ns) {
+ NotNull.exceptIfNull(element, "element");
+ --level;
+ if (level == 1) {
+ try {
+ if (element.equals("artist")) {
+ getPayloadInternal().setArtist(currentText);
+ }
+ else if (element.equals("length")) {
+ getPayloadInternal().setLength(Integer.parseInt(currentText));
+ }
+ else if (element.equals("rating")) {
+ getPayloadInternal().setRating(Integer.parseInt(currentText));
+ }
+ else if (element.equals("source")) {
+ getPayloadInternal().setSource(currentText);
+ }
+ else if (element.equals("title")) {
+ getPayloadInternal().setTitle(currentText);
+ }
+ else if (element.equals("track")) {
+ getPayloadInternal().setTrack(currentText);
+ }
+ else if (element.equals("URI")) {
+ getPayloadInternal().setURI(currentText);
+ }
+ }
+ catch (NumberFormatException e) {
+
+ }
+ }
+ }
+
+ /**
+ * @param data, NotNull.
+ */
+ @Override
+ public void handleCharacterData(String data) {
+ NotNull.exceptIfNull(data, "data");
+ currentText += data;
+ }
+} \ No newline at end of file
diff --git a/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java b/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java
index 644b7e0..bdb57cc 100644
--- a/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java
+++ b/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java
@@ -68,6 +68,7 @@ public class FullPayloadSerializerCollection extends PayloadSerializerCollection
addSerializer(new MAMResultSerializer(this));
addSerializer(new MAMQuerySerializer());
addSerializer(new MAMFinSerializer());
+ addSerializer(new UserTuneSerializer(this));
}
}
diff --git a/src/com/isode/stroke/serializer/payloadserializers/UserTuneSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/UserTuneSerializer.java
new file mode 100644
index 0000000..2a0a399
--- /dev/null
+++ b/src/com/isode/stroke/serializer/payloadserializers/UserTuneSerializer.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2014 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.serializer.payloadserializers;
+
+import com.isode.stroke.serializer.GenericPayloadSerializer;
+import com.isode.stroke.serializer.xml.XMLTextNode;
+import com.isode.stroke.serializer.xml.XMLElement;
+import com.isode.stroke.serializer.PayloadSerializerCollection;
+import com.isode.stroke.elements.UserTune;
+import com.isode.stroke.base.NotNull;
+
+public class UserTuneSerializer extends GenericPayloadSerializer<UserTune> {
+
+ private PayloadSerializerCollection serializers;
+
+ public UserTuneSerializer(PayloadSerializerCollection serializers) {
+ super(UserTune.class);
+ this.serializers = serializers;
+ }
+
+ public String serializePayload(UserTune payload) {
+ if (payload == null) {
+ return "";
+ }
+
+ XMLElement element = new XMLElement("tune", "http://jabber.org/protocol/tune");
+ if (payload.getRating() != null) {
+ element.addNode(new XMLElement("rating", "", Integer.toString(payload.getRating())));
+ }
+ if (payload.getTitle() != null) {
+ element.addNode(new XMLElement("title", "", payload.getTitle()));
+ }
+ if (payload.getTrack() != null) {
+ element.addNode(new XMLElement("track", "", payload.getTrack()));
+ }
+ if (payload.getArtist() != null) {
+ element.addNode(new XMLElement("artist", "", payload.getArtist()));
+ }
+ if (payload.getURI() != null) {
+ element.addNode(new XMLElement("uri", "", payload.getURI()));
+ }
+ if (payload.getSource() != null) {
+ element.addNode(new XMLElement("source", "", payload.getSource()));
+ }
+ if (payload.getLength() != null) {
+ element.addNode(new XMLElement("length", "", Integer.toString(payload.getLength())));
+ }
+ return element.serialize();
+ }
+} \ No newline at end of file
diff --git a/test/com/isode/stroke/parser/payloadparsers/UserTuneParserTest.java b/test/com/isode/stroke/parser/payloadparsers/UserTuneParserTest.java
new file mode 100644
index 0000000..b136ea1
--- /dev/null
+++ b/test/com/isode/stroke/parser/payloadparsers/UserTuneParserTest.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.parser.payloadparsers;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import org.junit.Test;
+import com.isode.stroke.elements.UserTune;
+import com.isode.stroke.parser.payloadparsers.UserTuneParser;
+import com.isode.stroke.parser.payloadparsers.PayloadsParserTester;
+import com.isode.stroke.eventloop.DummyEventLoop;
+
+public class UserTuneParserTest {
+
+ public UserTuneParserTest() {
+
+ }
+
+ @Test
+ public void testParse_with_all_variables() {
+ DummyEventLoop eventLoop = new DummyEventLoop();
+ PayloadsParserTester parser = new PayloadsParserTester(eventLoop);
+ assertNotNull(parser.parse("<tune xmlns=\"http://jabber.org/protocol/tune\">" +
+ "<rating>5</rating><title>Minion</title><track>Yellow</track><artist>Ice</artist><URI>Fire</URI><source>Origin</source><length>226</length></tune>"));
+
+ UserTune payload = (UserTune)parser.getPayload();
+ assertEquals(Integer.valueOf(5), payload.getRating());
+ assertEquals("Minion", payload.getTitle());
+ assertEquals("Yellow", payload.getTrack());
+ assertEquals("Ice", payload.getArtist());
+ assertEquals("Fire", payload.getURI());
+ assertEquals("Origin", payload.getSource());
+ assertEquals(Integer.valueOf(226), payload.getLength());
+ }
+
+ @Test
+ public void testParse_with_some_variables() {
+ DummyEventLoop eventLoop = new DummyEventLoop();
+ PayloadsParserTester parser = new PayloadsParserTester(eventLoop);
+ assertNotNull(parser.parse("<tune xmlns=\"http://jabber.org/protocol/tune\">" +
+ "<title>Minion</title><track>Yellow</track><source>Origin</source><length>226</length></tune>"));
+
+ UserTune payload = (UserTune)parser.getPayload();
+ assertNull(payload.getRating());
+ assertEquals("Minion", payload.getTitle());
+ assertEquals("Yellow", payload.getTrack());
+ assertNull(payload.getArtist());
+ assertNull(payload.getURI());
+ assertEquals("Origin", payload.getSource());
+ assertEquals(Integer.valueOf(226), payload.getLength());
+ }
+} \ No newline at end of file
diff --git a/test/com/isode/stroke/serializer/payloadserializers/UserTuneSerializerTest.java b/test/com/isode/stroke/serializer/payloadserializers/UserTuneSerializerTest.java
new file mode 100644
index 0000000..765ca5c
--- /dev/null
+++ b/test/com/isode/stroke/serializer/payloadserializers/UserTuneSerializerTest.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.serializer.payloadserializers;
+
+import static org.junit.Assert.assertEquals;
+import org.junit.Test;
+import com.isode.stroke.serializer.payloadserializers.UserTuneSerializer;
+import com.isode.stroke.serializer.PayloadSerializerCollection;
+import com.isode.stroke.elements.UserTune;
+
+public class UserTuneSerializerTest {
+
+ /**
+ * Default Constructor.
+ */
+ public UserTuneSerializerTest() {
+
+ }
+
+ @Test
+ public void testSerialize_withAllVariablesSet() {
+ PayloadSerializerCollection serializerCollection = new PayloadSerializerCollection();
+ UserTuneSerializer testling = new UserTuneSerializer(serializerCollection);
+ UserTune userTune = new UserTune();
+ userTune.setRating(5);
+ userTune.setTitle("Minion");
+ userTune.setTrack("Yellow");
+ userTune.setArtist("Ice");
+ userTune.setURI("Fire");
+ userTune.setSource("Origin");
+ userTune.setLength(226);
+ String expectedResult = "<tune xmlns=\"http://jabber.org/protocol/tune\">" +
+ "<rating>5</rating><title>Minion</title><track>Yellow</track><artist>Ice</artist><uri>Fire</uri><source>Origin</source><length>226</length></tune>";
+ assertEquals(expectedResult, testling.serialize(userTune));
+ }
+
+ @Test
+ public void testSerialize_withSomeVariablesSet() {
+ PayloadSerializerCollection serializerCollection = new PayloadSerializerCollection();
+ UserTuneSerializer testling = new UserTuneSerializer(serializerCollection);
+ UserTune userTune = new UserTune();
+ userTune.setTitle("Minion");
+ userTune.setTrack("Yellow");
+ userTune.setArtist("Ice");
+ userTune.setSource("Origin");
+ userTune.setLength(226);
+ String expectedResult = "<tune xmlns=\"http://jabber.org/protocol/tune\">" +
+ "<title>Minion</title><track>Yellow</track><artist>Ice</artist><source>Origin</source><length>226</length></tune>";
+ assertEquals(expectedResult, testling.serialize(userTune));
+ }
+} \ No newline at end of file