diff options
author | Tarun Gupta <tarun1995gupta@gmail.com> | 2015-06-11 20:23:56 (GMT) |
---|---|---|
committer | Tarun Gupta <tarun1995gupta@gmail.com> | 2015-06-16 19:14:39 (GMT) |
commit | e868c966e23716d8a0261b222cccbeb04616e235 (patch) | |
tree | 2e26cc205e671f54bc5de527917913f8de47e0f4 /src/com/isode | |
parent | b2a8ac9a0387a9a5e75e31f7704e62ac077faba4 (diff) | |
download | stroke-e868c966e23716d8a0261b222cccbeb04616e235.zip stroke-e868c966e23716d8a0261b222cccbeb04616e235.tar.bz2 |
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
Diffstat (limited to 'src/com/isode')
5 files changed, 595 insertions, 0 deletions
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<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 GenericPayloadParserFactory<UserLocationParser>("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<UserLocation> { + + 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<UserLocation> { + + 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 |