From bb52f4e377fac262fe5b4f0ca2e1f6ed3669ee7d Mon Sep 17 00:00:00 2001 From: Tarun Gupta Date: Wed, 17 Jun 2015 23:48:22 +0530 Subject: Add Nickname Element. Adds Nickname Element, its Parser and Serializer. License: This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details. Test-Information: None. Change-Id: I62affc8ef482f3e04a7c337f25b068b6d602c5bb diff --git a/src/com/isode/stroke/elements/Nickname.java b/src/com/isode/stroke/elements/Nickname.java new file mode 100644 index 0000000..f77a636 --- /dev/null +++ b/src/com/isode/stroke/elements/Nickname.java @@ -0,0 +1,51 @@ +/* + * 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.elements; + +import com.isode.stroke.elements.Payload; +import com.isode.stroke.base.NotNull; + +public class Nickname extends Payload { + + private String nickname; + + /** + * Default Constructor. + */ + public Nickname() { + this(""); + } + + /** + * Parameterized Constructor. + * @param nickname, Not Null. + */ + public Nickname(String nickname) { + NotNull.exceptIfNull(nickname, "nickname"); + this.nickname = nickname; + } + + /** + * @param nickname, Not Null. + */ + public void setNickname(String nickname) { + NotNull.exceptIfNull(nickname, "nickname"); + this.nickname = nickname; + } + + /** + * @return nickname, Not Null. + */ + public String getNickname() { + return nickname; + } +} \ No newline at end of file diff --git a/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java b/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java index 08d9f28..b591047 100644 --- a/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java +++ b/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java @@ -83,7 +83,7 @@ public class FullPayloadParserFactoryCollection extends PayloadParserFactoryColl addFactory(new GenericPayloadParserFactory("fin", "urn:xmpp:mam:0", MAMFinParser.class)); addFactory(new GenericPayloadParserFactory("tune", "http://jabber.org/protocol/tune", UserTuneParser.class)); addFactory(new GenericPayloadParserFactory("geoloc", "http://jabber.org/protocol/geoloc", UserLocationParser.class)); - //addFactory(new NicknameParserFactory()); + addFactory(new GenericPayloadParserFactory("nick", "http://jabber.org/protocol/nick", NicknameParser.class)); PayloadParserFactory defaultFactory = new RawXMLPayloadParserFactory(); setDefaultFactory(defaultFactory); diff --git a/src/com/isode/stroke/parser/payloadparsers/NicknameParser.java b/src/com/isode/stroke/parser/payloadparsers/NicknameParser.java new file mode 100644 index 0000000..97e9d30 --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/NicknameParser.java @@ -0,0 +1,59 @@ +/* + * 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 com.isode.stroke.parser.GenericPayloadParser; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.elements.Nickname; +import com.isode.stroke.base.NotNull; + +public class NicknameParser extends GenericPayloadParser { + + private int level = 0; + private String text = ""; + + public NicknameParser() { + super(new Nickname()); + this.level = 0; + } + + /** + * @param element. + * @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) { + getPayloadInternal().setNickname(text); + } + } + + /** + * @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 8c1f14b..9b88d0c 100644 --- a/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java +++ b/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java @@ -57,7 +57,7 @@ public class FullPayloadSerializerCollection extends PayloadSerializerCollection addSerializer(new FormSerializer()); addSerializer(new PrivateStorageSerializer(this)); addSerializer(new CommandSerializer()); - //addSerializer(new NicknameSerializer()); + addSerializer(new NicknameSerializer()); addSerializer(new SearchPayloadSerializer()); addSerializer(new ReplaceSerializer()); addSerializer(new LastSerializer()); diff --git a/src/com/isode/stroke/serializer/payloadserializers/NicknameSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/NicknameSerializer.java new file mode 100644 index 0000000..2964930 --- /dev/null +++ b/src/com/isode/stroke/serializer/payloadserializers/NicknameSerializer.java @@ -0,0 +1,32 @@ +/* + * 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 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.Nickname; +import com.isode.stroke.base.NotNull; + +public class NicknameSerializer extends GenericPayloadSerializer { + + public NicknameSerializer() { + super(Nickname.class); + } + + public String serializePayload(Nickname nick) { + XMLElement nickElement = new XMLElement("nick", "http://jabber.org/protocol/nick"); + nickElement.addNode(new XMLTextNode(nick.getNickname())); + return nickElement.serialize(); + } +} \ No newline at end of file -- cgit v0.10.2-6-g49f6