diff options
6 files changed, 183 insertions, 2 deletions
diff --git a/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java b/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java index b1afefd..0a46d75 100644 --- a/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java +++ b/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java @@ -34,7 +34,7 @@ public class FullPayloadParserFactoryCollection extends PayloadParserFactoryColl addFactory(new GenericPayloadParserFactory<JingleIBBTransportMethodPayloadParser>("transport", "urn:xmpp:jingle:transports:ibb:1", JingleIBBTransportMethodPayloadParser.class)); addFactory(new GenericPayloadParserFactory<JingleS5BTransportMethodPayloadParser>("transport", "urn:xmpp:jingle:transports:s5b:1", JingleS5BTransportMethodPayloadParser.class)); addFactory(new JingleContentPayloadParserFactory(this)); - //addFactory(new GenericPayloadParserFactory<PriorityParser>("priority", PriorityParser.class)); + addFactory(new GenericPayloadParserFactory<PriorityParser>("priority", PriorityParser.class)); addFactory(new ErrorParserFactory(this)); addFactory(new SoftwareVersionParserFactory()); addFactory(new GenericPayloadParserFactory<IsodeIQDelegationParser>("delegate", "http://isode.com/iq_delegation", IsodeIQDelegationParser.class)); diff --git a/src/com/isode/stroke/parser/payloadparsers/PriorityParser.java b/src/com/isode/stroke/parser/payloadparsers/PriorityParser.java new file mode 100644 index 0000000..f2a99e9 --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/PriorityParser.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2010-2015 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.Priority; +import com.isode.stroke.base.NotNull; + +public class PriorityParser extends GenericPayloadParser<Priority> { + + private int level_ = 0; + private String text_ = ""; + + public PriorityParser() { + super(new Priority()); + this.level_ = 0; + } + + /** + * @param element, NotNull. + * @param ns. + * @param attributes. + */ + @Override + public void handleStartElement(String element, String ns, AttributeMap attributes) { + ++level_; + } + + /** + * @param element, NotNull. + * @param ns. + */ + @Override + public void handleEndElement(String element, String ns) { + --level_; + if (level_ == 0) { + int priority = 0; + try { + priority = Integer.parseInt(text_); + } + catch (NumberFormatException e) { + + } + getPayloadInternal().setPriority(priority); + } + } + + /** + * @param data, NotNull. + */ + @Override + public void handleCharacterData(String data) { + NotNull.exceptIfNull(data, "data"); + text_ += 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 ba0d4e9..001d126 100644 --- a/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java +++ b/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java @@ -25,7 +25,7 @@ public class FullPayloadSerializerCollection extends PayloadSerializerCollection addSerializer(new CarbonsPrivateSerializer()); addSerializer(new CarbonsReceivedSerializer(this)); addSerializer(new CarbonsSentSerializer(this)); - //addSerializer(new PrioritySerializer()); + addSerializer(new PrioritySerializer()); addSerializer(new ErrorSerializer()); addSerializer(new InBandRegistrationPayloadSerializer()); addSerializer(new IBBSerializer()); diff --git a/src/com/isode/stroke/serializer/payloadserializers/PrioritySerializer.java b/src/com/isode/stroke/serializer/payloadserializers/PrioritySerializer.java new file mode 100644 index 0000000..e043c4d --- /dev/null +++ b/src/com/isode/stroke/serializer/payloadserializers/PrioritySerializer.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2010-2015 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.elements.Priority; +import com.isode.stroke.base.NotNull; + +public class PrioritySerializer extends GenericPayloadSerializer<Priority> { + + public PrioritySerializer() { + super(Priority.class); + } + + public String serializePayload(Priority priority) { + return "<priority>" + Integer.toString(priority.getPriority()) + "</priority>"; + } +}
\ No newline at end of file diff --git a/test/com/isode/stroke/parser/payloadparsers/PriorityParserTest.java b/test/com/isode/stroke/parser/payloadparsers/PriorityParserTest.java new file mode 100644 index 0000000..5422bf0 --- /dev/null +++ b/test/com/isode/stroke/parser/payloadparsers/PriorityParserTest.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2010 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 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.Priority; +import com.isode.stroke.parser.payloadparsers.PriorityParser; +import com.isode.stroke.parser.payloadparsers.PayloadsParserTester; +import com.isode.stroke.eventloop.DummyEventLoop; + +public class PriorityParserTest { + + public PriorityParserTest() { + + } + + @Test + public void testParse() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + + assertNotNull(parser.parse("<priority>-120</priority>")); + + Priority payload = (Priority)(parser.getPayload()); + assertEquals(-120, payload.getPriority()); + } + + @Test + public void testParse_Invalid() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + + assertNotNull(parser.parse("<priority>invalid</priority>")); + + Priority payload = (Priority)(parser.getPayload()); + assertEquals(0, payload.getPriority()); + } +}
\ No newline at end of file diff --git a/test/com/isode/stroke/serializer/payloadserializers/PrioritySerializerTest.java b/test/com/isode/stroke/serializer/payloadserializers/PrioritySerializerTest.java new file mode 100644 index 0000000..c3c3a37 --- /dev/null +++ b/test/com/isode/stroke/serializer/payloadserializers/PrioritySerializerTest.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2010 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 static org.junit.Assert.assertEquals; +import org.junit.Test; +import com.isode.stroke.serializer.payloadserializers.PrioritySerializer; +import com.isode.stroke.serializer.PayloadSerializerCollection; +import com.isode.stroke.elements.Priority; + +public class PrioritySerializerTest { + + /** + * Default Constructor. + */ + public PrioritySerializerTest() { + + } + + @Test + public void testSerialize() { + PrioritySerializer testling = new PrioritySerializer(); + Priority priority = new Priority(-113); + + assertEquals("<priority>-113</priority>", testling.serialize(priority)); + } +}
\ No newline at end of file |