From e868c966e23716d8a0261b222cccbeb04616e235 Mon Sep 17 00:00:00 2001 From: Tarun Gupta Date: Fri, 12 Jun 2015 01:53:56 +0530 Subject: Add the UserLocation Element. Adds UserLocation Element, its parser and Serializer. License: This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details. Test-Information: Tests added for UserLocation Parser and UserLocation Serializer, which passes. Change-Id: Ia1d57f4233351e755770f06fdc8e292db1acf521 diff --git a/src/com/isode/stroke/elements/UserLocation.java b/src/com/isode/stroke/elements/UserLocation.java new file mode 100644 index 0000000..bb974ae --- /dev/null +++ b/src/com/isode/stroke/elements/UserLocation.java @@ -0,0 +1,355 @@ +/* + * Copyright (c) 2013 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.base.NotNull; +import com.isode.stroke.elements.Payload; +import java.util.Date; +import java.util.TimeZone; + +public class UserLocation extends Payload { + + private String area; + private Float altitude; + private String locality; + private Float latitude; + private Float accuracy; + private String description; + private String countryCode; + private Date timestamp; + private String floor; + private String building; + private String room; + private String country; + private String region; + private String uri; + private Float longitude; + private Float error; + private String postalCode; + private Float bearing; + private String text; + private String datum; + private String street; + private Float speed; + + public UserLocation() { + + } + + /** + * @return Area. + */ + public String getArea() { + return area; + } + + /** + * @param Area. + */ + public void setArea(String value) { + this.area = value ; + } + + /** + * @return altitude. + */ + public Float getAltitude() { + return altitude; + } + + /** + * @param altitude. + */ + public void setAltitude(Float value) { + this.altitude = value ; + } + + /** + * @return locality. + */ + public String getLocality() { + return locality; + } + + /** + * @param locality. + */ + public void setLocality(String value) { + this.locality = value ; + } + + /** + * @return latitude. + */ + public Float getLatitude() { + return latitude; + } + + /** + * @param latitude. + */ + public void setLatitude(Float value) { + this.latitude = value ; + } + + /** + * @return accuracy. + */ + public Float getAccuracy() { + return accuracy; + } + + /** + * @param accuracy. + */ + public void setAccuracy(Float value) { + this.accuracy = value ; + } + + /** + * @return description. + */ + public String getDescription() { + return description; + } + + /** + * @param description. + */ + public void setDescription(String value) { + this.description = value ; + } + + /** + * @return countryCode. + */ + public String getCountryCode() { + return countryCode; + } + + /** + * @param countryCode. + */ + public void setCountryCode(String value) { + this.countryCode = value ; + } + + /** + * @return timestamp. + */ + public Date getTimestamp() { + return timestamp; + } + + /** + * @param timestamp. + */ + public void setTimestamp(Date value) { + this.timestamp = value ; + } + + /** + * @return floor. + */ + public String getFloor() { + return floor; + } + + /** + * @param floor. + */ + public void setFloor(String value) { + this.floor = value ; + } + + /** + * @return building. + */ + public String getBuilding() { + return building; + } + + /** + * @param building. + */ + public void setBuilding(String value) { + this.building = value ; + } + + /** + * @return room. + */ + public String getRoom() { + return room; + } + + /** + * @param room. + */ + public void setRoom(String value) { + this.room = value ; + } + + /** + * @return country. + */ + public String getCountry() { + return country; + } + + /** + * @param country. + */ + public void setCountry(String value) { + this.country = value ; + } + + /** + * @return region. + */ + public String getRegion() { + return region; + } + + /** + * @param region. + */ + public void setRegion(String value) { + this.region = value ; + } + + /** + * @return uri. + */ + public String getURI() { + return uri; + } + + /** + * @param uri. + */ + public void setURI(String value) { + this.uri = value ; + } + + /** + * @return longitude. + */ + public Float getLongitude() { + return longitude; + } + + /** + * @param longitude. + */ + public void setLongitude(Float value) { + this.longitude = value ; + } + + /** + * @return error. + */ + public Float getError() { + return error; + } + + /** + * @param error. + */ + public void setError(Float value) { + this.error = value ; + } + + /** + * @return postalCode. + */ + public String getPostalCode() { + return postalCode; + } + + /** + * @param postalCode. + */ + public void setPostalCode(String value) { + this.postalCode = value ; + } + + /** + * @return bearing. + */ + public Float getBearing() { + return bearing; + } + + /** + * @param bearing. + */ + public void setBearing(Float value) { + this.bearing = value ; + } + + /** + * @return text. + */ + public String getText() { + return text; + } + + /** + * @param text. + */ + public void setText(String value) { + this.text = value ; + } + + /** + * @return datum. + */ + public String getDatum() { + return datum; + } + + /** + * @param datum. + */ + public void setDatum(String value) { + this.datum = value ; + } + + /** + * @return street. + */ + public String getStreet() { + return street; + } + + /** + * @param street. + */ + public void setStreet(String value) { + this.street = value ; + } + + /** + * @return speed. + */ + public Float getSpeed() { + return speed; + } + + /** + * @param speed. + */ + public void setSpeed(Float value) { + this.speed = value ; + } +} \ 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 ef491dd..1872feb 100644 --- a/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java +++ b/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java @@ -72,6 +72,7 @@ public class FullPayloadParserFactoryCollection extends PayloadParserFactoryColl addFactory(new GenericPayloadParserFactory("query", "urn:xmpp:mam:0", MAMQueryParser.class)); 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()); PayloadParserFactory defaultFactory = new RawXMLPayloadParserFactory(); diff --git a/src/com/isode/stroke/parser/payloadparsers/UserLocationParser.java b/src/com/isode/stroke/parser/payloadparsers/UserLocationParser.java new file mode 100644 index 0000000..28cde78 --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/UserLocationParser.java @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2013 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.UserLocation; +import com.isode.stroke.base.NotNull; +import com.isode.stroke.base.DateTime; + +public class UserLocationParser extends GenericPayloadParser { + + private int level = 0; + private String currentText = ""; + + public UserLocationParser() { + super(new UserLocation()); + } + + /** + * @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("accuracy")) { + getPayloadInternal().setAccuracy(Float.parseFloat(currentText)); + } + else if (element.equals("alt")) { + getPayloadInternal().setAltitude(Float.parseFloat(currentText)); + } + else if (element.equals("area")) { + getPayloadInternal().setArea(currentText); + } + else if (element.equals("bearing")) { + getPayloadInternal().setBearing(Float.parseFloat(currentText)); + } + else if (element.equals("building")) { + getPayloadInternal().setBuilding(currentText); + } + else if (element.equals("country")) { + getPayloadInternal().setCountry(currentText); + } + else if (element.equals("countrycode")) { + getPayloadInternal().setCountryCode(currentText); + } + else if (element.equals("datum")) { + getPayloadInternal().setDatum(currentText); + } + else if (element.equals("description")) { + getPayloadInternal().setDescription(currentText); + } + else if (element.equals("error")) { + getPayloadInternal().setError(Float.parseFloat(currentText)); + } + else if (element.equals("floor")) { + getPayloadInternal().setFloor(currentText); + } + else if (element.equals("lat")) { + getPayloadInternal().setLatitude(Float.parseFloat(currentText)); + } + else if (element.equals("locality")) { + getPayloadInternal().setLocality(currentText); + } + else if (element.equals("lon")) { + getPayloadInternal().setLongitude(Float.parseFloat(currentText)); + } + else if (element.equals("postalcode")) { + getPayloadInternal().setPostalCode(currentText); + } + else if (element.equals("region")) { + getPayloadInternal().setRegion(currentText); + } + else if (element.equals("room")) { + getPayloadInternal().setRoom(currentText); + } + else if (element.equals("speed")) { + getPayloadInternal().setSpeed(Float.parseFloat(currentText)); + } + else if (element.equals("street")) { + getPayloadInternal().setStreet(currentText); + } + else if (element.equals("text")) { + getPayloadInternal().setText(currentText); + } + else if (element.equals("timestamp")) { + getPayloadInternal().setTimestamp(DateTime.stringToDate(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 bdb57cc..c25ac12 100644 --- a/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java +++ b/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java @@ -69,6 +69,7 @@ public class FullPayloadSerializerCollection extends PayloadSerializerCollection addSerializer(new MAMQuerySerializer()); addSerializer(new MAMFinSerializer()); addSerializer(new UserTuneSerializer(this)); + addSerializer(new UserLocationSerializer(this)); } } diff --git a/src/com/isode/stroke/serializer/payloadserializers/UserLocationSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/UserLocationSerializer.java new file mode 100644 index 0000000..233886e --- /dev/null +++ b/src/com/isode/stroke/serializer/payloadserializers/UserLocationSerializer.java @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2013 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.UserLocation; +import com.isode.stroke.base.NotNull; +import com.isode.stroke.base.DateTime; + +public class UserLocationSerializer extends GenericPayloadSerializer { + + private PayloadSerializerCollection serializers; + + public UserLocationSerializer(PayloadSerializerCollection serializers) { + super(UserLocation.class); + this.serializers = serializers; + } + + public String serializePayload(UserLocation payload) { + if (payload == null) { + return ""; + } + + XMLElement element = new XMLElement("geoloc", "http://jabber.org/protocol/geoloc"); + if (payload.getArea() != null) { + element.addNode(new XMLElement("area", "", payload.getArea())); + } + if (payload.getAltitude() != null) { + element.addNode(new XMLElement("alt", "", Float.toString(payload.getAltitude()))); + } + if (payload.getLocality() != null) { + element.addNode(new XMLElement("locality", "", payload.getLocality())); + } + if (payload.getLatitude() != null) { + element.addNode(new XMLElement("lat", "", Float.toString(payload.getLatitude()))); + } + if (payload.getAccuracy() != null) { + element.addNode(new XMLElement("accuracy", "", Float.toString(payload.getAccuracy()))); + } + if (payload.getDescription() != null) { + element.addNode(new XMLElement("description", "", payload.getDescription())); + } + if (payload.getCountryCode() != null) { + element.addNode(new XMLElement("countrycode", "", payload.getCountryCode())); + } + if (payload.getTimestamp() != null) { + element.addNode(new XMLElement("timestamp", "", DateTime.dateToString(payload.getTimestamp()))); + } + if (payload.getFloor() != null) { + element.addNode(new XMLElement("floor", "", payload.getFloor())); + } + if (payload.getBuilding() != null) { + element.addNode(new XMLElement("building", "", payload.getBuilding())); + } + if (payload.getRoom() != null) { + element.addNode(new XMLElement("room", "", payload.getRoom())); + } + if (payload.getCountry() != null) { + element.addNode(new XMLElement("country", "", payload.getCountry())); + } + if (payload.getRegion() != null) { + element.addNode(new XMLElement("region", "", payload.getRegion())); + } + if (payload.getURI() != null) { + element.addNode(new XMLElement("uri", "", payload.getURI())); + } + if (payload.getLongitude() != null) { + element.addNode(new XMLElement("lon", "", Float.toString(payload.getLongitude()))); + } + if (payload.getError() != null) { + element.addNode(new XMLElement("error", "", Float.toString(payload.getError()))); + } + if (payload.getPostalCode() != null) { + element.addNode(new XMLElement("postalcode", "", payload.getPostalCode())); + } + if (payload.getBearing() != null) { + element.addNode(new XMLElement("bearing", "", Float.toString(payload.getBearing()))); + } + if (payload.getText() != null) { + element.addNode(new XMLElement("text", "", payload.getText())); + } + if (payload.getDatum() != null) { + element.addNode(new XMLElement("datum", "", payload.getDatum())); + } + if (payload.getStreet() != null) { + element.addNode(new XMLElement("street", "", payload.getStreet())); + } + if (payload.getSpeed() != null) { + element.addNode(new XMLElement("speed", "", Float.toString(payload.getSpeed()))); + } + return element.serialize(); + } +} \ No newline at end of file diff --git a/test/com/isode/stroke/parser/payloadparsers/UserLocationParserTest.java b/test/com/isode/stroke/parser/payloadparsers/UserLocationParserTest.java new file mode 100644 index 0000000..015fab3 --- /dev/null +++ b/test/com/isode/stroke/parser/payloadparsers/UserLocationParserTest.java @@ -0,0 +1,102 @@ +/* + * 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.UserLocation; +import com.isode.stroke.parser.payloadparsers.UserLocationParser; +import com.isode.stroke.parser.payloadparsers.PayloadsParserTester; +import com.isode.stroke.eventloop.DummyEventLoop; +import java.util.Date; +import com.isode.stroke.base.DateTime; +import java.util.TimeZone; + +public class UserLocationParserTest { + + public UserLocationParserTest() { + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + } + + @Test + public void testParse_with_all_variables() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + assertNotNull(parser.parse("" + + "Barbaric5.75Near" + + "1.670.95Nice" + + "+912015-06-11T20:55:50Z3" + + "FirstE315USA" + + "NewSodeURIs6.75785.66" + + "6712.89Hello" + + "DateeHighway56.77")); + + UserLocation payload = (UserLocation)parser.getPayload(); + + assertEquals ("Barbaric", payload.getArea()); + assertEquals (Float.valueOf(5.75F), payload.getAltitude()); + assertEquals ("Near", payload.getLocality()); + assertEquals (Float.valueOf(1.670F), payload.getLatitude()); + assertEquals (Float.valueOf(0.95F), payload.getAccuracy()); + assertEquals ("Nice", payload.getDescription()); + assertEquals ("+91", payload.getCountryCode()); + assertEquals (DateTime.dateToString(new Date(1434056150620L)), DateTime.dateToString(payload.getTimestamp())); + assertEquals ("3", payload.getFloor()); + assertEquals ("First", payload.getBuilding()); + assertEquals ("E315", payload.getRoom()); + assertEquals ("USA", payload.getCountry()); + assertEquals ("NewSode", payload.getRegion()); + assertEquals ("URIs", payload.getURI()); + assertEquals (Float.valueOf(6.7578F), payload.getLongitude()); + assertEquals (Float.valueOf(5.66F), payload.getError()); + assertEquals ("67", payload.getPostalCode()); + assertEquals (Float.valueOf(12.89F), payload.getBearing()); + assertEquals ("Hello", payload.getText()); + assertEquals ("Datee", payload.getDatum()); + assertEquals ("Highway", payload.getStreet()); + assertEquals (Float.valueOf(56.77F), payload.getSpeed()); + } + + @Test + public void testParse_with_Some_variables() { + DummyEventLoop eventLoop = new DummyEventLoop(); + PayloadsParserTester parser = new PayloadsParserTester(eventLoop); + assertNotNull(parser.parse("" + + "Barbaric5.75Near" + + "0.95Nice" + + "+912015-06-11T20:55:50Z3" + + "NewSodeURIs6.75785.66" + + "6712.89Hello")); + + UserLocation payload = (UserLocation)parser.getPayload(); + + assertEquals ("Barbaric", payload.getArea()); + assertEquals (Float.valueOf(5.75F), payload.getAltitude()); + assertEquals ("Near", payload.getLocality()); + assertNull(payload.getLatitude()); + assertEquals (Float.valueOf(0.95F), payload.getAccuracy()); + assertEquals ("Nice", payload.getDescription()); + assertEquals ("+91", payload.getCountryCode()); + assertEquals (DateTime.dateToString(new Date(1434056150620L)), DateTime.dateToString(payload.getTimestamp())); + assertEquals ("3", payload.getFloor()); + assertNull(payload.getBuilding()); + assertNull(payload.getRoom()); + assertNull(payload.getCountry()); + assertEquals ("NewSode", payload.getRegion()); + assertEquals ("URIs", payload.getURI()); + assertEquals (Float.valueOf(6.7578F), payload.getLongitude()); + assertEquals (Float.valueOf(5.66F), payload.getError()); + assertEquals ("67", payload.getPostalCode()); + assertEquals (Float.valueOf(12.89F), payload.getBearing()); + assertEquals ("Hello", payload.getText()); + assertNull(payload.getDatum()); + assertNull(payload.getStreet()); + assertNull(payload.getSpeed()); + } +} \ No newline at end of file diff --git a/test/com/isode/stroke/serializer/payloadserializers/UserLocationSerializerTest.java b/test/com/isode/stroke/serializer/payloadserializers/UserLocationSerializerTest.java new file mode 100644 index 0000000..1bd07c1 --- /dev/null +++ b/test/com/isode/stroke/serializer/payloadserializers/UserLocationSerializerTest.java @@ -0,0 +1,91 @@ +/* + * 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.UserLocationSerializer; +import com.isode.stroke.serializer.PayloadSerializerCollection; +import com.isode.stroke.elements.UserLocation; +import java.util.Date; +import java.util.TimeZone; + +public class UserLocationSerializerTest { + + public UserLocationSerializerTest() { + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + } + + @Test + public void testSerialize_withAllVariablesSet() { + PayloadSerializerCollection serializerCollection = new PayloadSerializerCollection(); + UserLocationSerializer testling = new UserLocationSerializer(serializerCollection); + UserLocation userLocation = new UserLocation(); + userLocation.setArea("Barbaric"); + userLocation.setAltitude(5.75F); + userLocation.setLocality("Near"); + userLocation.setLatitude(1.670F); + userLocation.setAccuracy(0.95F); + userLocation.setDescription("Nice"); + userLocation.setCountryCode("+91"); + userLocation.setTimestamp(new Date(1434056150620L)); + userLocation.setFloor("3"); + userLocation.setBuilding("First"); + userLocation.setRoom("E315"); + userLocation.setCountry("USA"); + userLocation.setRegion("NewSode"); + userLocation.setURI("URIs"); + userLocation.setLongitude(6.7578F); + userLocation.setError(5.66F); + userLocation.setPostalCode("67"); + userLocation.setBearing(12.89F); + userLocation.setText("Hello"); + userLocation.setDatum("Datee"); + userLocation.setStreet("Highway"); + userLocation.setSpeed(56.77F); + + String expectedResult = "" + + "Barbaric5.75Near" + + "1.670.95Nice" + + "+912015-06-11T20:55:50Z3" + + "FirstE315USA" + + "NewSodeURIs6.75785.66" + + "6712.89Hello" + + "DateeHighway56.77"; + assertEquals(expectedResult, testling.serialize(userLocation)); + } + + @Test + public void testSerialize_withSomeVariablesSet() { + PayloadSerializerCollection serializerCollection = new PayloadSerializerCollection(); + UserLocationSerializer testling = new UserLocationSerializer(serializerCollection); + UserLocation userLocation = new UserLocation(); + userLocation.setArea("Barbaric"); + userLocation.setAltitude(5.75F); + userLocation.setLocality("Near"); + userLocation.setAccuracy(0.95F); + userLocation.setDescription("Nice"); + userLocation.setCountryCode("+91"); + userLocation.setTimestamp(new Date(1434056150620L)); + userLocation.setFloor("3"); + userLocation.setRegion("NewSode"); + userLocation.setURI("URIs"); + userLocation.setLongitude(6.7578F); + userLocation.setError(5.66F); + userLocation.setPostalCode("67"); + userLocation.setBearing(12.89F); + userLocation.setText("Hello"); + + String expectedResult = "" + + "Barbaric5.75Near" + + "0.95Nice" + + "+912015-06-11T20:55:50Z3" + + "NewSodeURIs6.75785.66" + + "6712.89Hello"; + assertEquals(expectedResult, testling.serialize(userLocation)); + } +} \ No newline at end of file -- cgit v0.10.2-6-g49f6