diff options
author | Alex Clayton <alex.clayton@isode.com> | 2016-02-02 11:26:01 (GMT) |
---|---|---|
committer | Alex Clayton <alex.clayton@isode.com> | 2016-02-04 10:09:21 (GMT) |
commit | be72c822d3cffd840e753e01fe28857b081cf6f3 (patch) | |
tree | ae3fe8fe22175f9bfee89f0688b71ca377f7f1e9 /src | |
parent | 2c9305a872e4ca958bfd86472c4540cc10d11a2f (diff) | |
download | stroke-be72c822d3cffd840e753e01fe28857b081cf6f3.zip stroke-be72c822d3cffd840e753e01fe28857b081cf6f3.tar.bz2 |
Add Block Parser and Serializer
Add BlockParser and BlockSerializer for the block payload classes.
This required slightly different approach to the Swiften code as C++ templates
are different to Java generics. In short needed to add a type hierachy to the
Block payload classes and to the Parsers and Serilaizers associated with them.
Test-information:
Ran unit tests everything passed ok.
Change-Id: I3ea2d66afd7cb3b5c4c3515be3a1ef99d7dbb566
Diffstat (limited to 'src')
13 files changed, 361 insertions, 23 deletions
diff --git a/src/com/isode/stroke/elements/AbstractBlockPayload.java b/src/com/isode/stroke/elements/AbstractBlockPayload.java new file mode 100644 index 0000000..575b6ba --- /dev/null +++ b/src/com/isode/stroke/elements/AbstractBlockPayload.java @@ -0,0 +1,49 @@ +/* Copyright (c) 2016, Isode Limited, London, England. + * All rights reserved. + * + * Acquisition and use of this software and related materials for any + * purpose requires a written license agreement from Isode Limited, + * or a written license from an organisation licensed by Isode Limited + * to grant such a license. + * + */ +package com.isode.stroke.elements; + +import java.util.Vector; + +import com.isode.stroke.jid.JID; + +/** + * Parent abstract class for the Block pay load classes {@link BlockPayload}, + * {@link BlockListPayload} and {@link UnblockPayload}. + */ +public abstract class AbstractBlockPayload extends Payload { + /* + * Note this is slightly different to Swiften code as templates in C++ work + * different to Java Generics. In Swiften there exits a BlockParser template + * for which instances can be created of type <BlockPayload>, <BlockListPayload> and + * <UnblockPayload>. To get this to work in java we have to create a parent + * abstract class for all these Block elements types. + */ + + + /** + * Constructor + */ + protected AbstractBlockPayload() { + super(); + } + + /** + * Adds an item + * @param item item, not {@code null} + */ + public abstract void addItem(JID item); + + /** + * Gets the items + * @return items, NotNull. + */ + public abstract Vector<JID> getItems(); + +} diff --git a/src/com/isode/stroke/elements/BlockListPayload.java b/src/com/isode/stroke/elements/BlockListPayload.java index 5c6904d..66bd30d 100644 --- a/src/com/isode/stroke/elements/BlockListPayload.java +++ b/src/com/isode/stroke/elements/BlockListPayload.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011 Isode Limited. + * Copyright (c) 2011-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -11,12 +11,12 @@ package com.isode.stroke.elements; +import java.util.Vector; + import com.isode.stroke.base.NotNull; -import com.isode.stroke.elements.Payload; import com.isode.stroke.jid.JID; -import java.util.Vector; -public class BlockListPayload extends Payload { +public class BlockListPayload extends AbstractBlockPayload { Vector<JID> items = new Vector<JID>(); @@ -36,7 +36,8 @@ public class BlockListPayload extends Payload { /** * @param item, NotNull. */ - public void addItem(JID item) { + @Override + public void addItem(JID item) { NotNull.exceptIfNull(item, "item"); items.add(item); } @@ -44,7 +45,8 @@ public class BlockListPayload extends Payload { /** * @return items, NotNull. */ - public Vector<JID> getItems() { + @Override + public Vector<JID> getItems() { return items; } }
\ No newline at end of file diff --git a/src/com/isode/stroke/elements/BlockPayload.java b/src/com/isode/stroke/elements/BlockPayload.java index 8ce8620..9579430 100644 --- a/src/com/isode/stroke/elements/BlockPayload.java +++ b/src/com/isode/stroke/elements/BlockPayload.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011 Isode Limited. + * Copyright (c) 2011-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -11,12 +11,12 @@ package com.isode.stroke.elements; +import java.util.Vector; + import com.isode.stroke.base.NotNull; -import com.isode.stroke.elements.Payload; import com.isode.stroke.jid.JID; -import java.util.Vector; -public class BlockPayload extends Payload { +public class BlockPayload extends AbstractBlockPayload { Vector<JID> items = new Vector<JID>(); @@ -36,7 +36,8 @@ public class BlockPayload extends Payload { /** * @param item, NotNull. */ - public void addItem(JID item) { + @Override + public void addItem(JID item) { NotNull.exceptIfNull(item, "item"); items.add(item); } @@ -44,7 +45,8 @@ public class BlockPayload extends Payload { /** * @return items, NotNull. */ - public Vector<JID> getItems() { + @Override + public Vector<JID> getItems() { return items; } }
\ No newline at end of file diff --git a/src/com/isode/stroke/elements/UnblockPayload.java b/src/com/isode/stroke/elements/UnblockPayload.java index 5a5c709..7893527 100644 --- a/src/com/isode/stroke/elements/UnblockPayload.java +++ b/src/com/isode/stroke/elements/UnblockPayload.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2015 Isode Limited. + * Copyright (c) 2011-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -11,12 +11,12 @@ package com.isode.stroke.elements; -import com.isode.stroke.elements.Payload; -import com.isode.stroke.jid.JID; -import com.isode.stroke.base.NotNull; import java.util.Vector; -public class UnblockPayload extends Payload { +import com.isode.stroke.base.NotNull; +import com.isode.stroke.jid.JID; + +public class UnblockPayload extends AbstractBlockPayload { private Vector<JID> items = new Vector<JID>(); @@ -36,7 +36,8 @@ public class UnblockPayload extends Payload { /** * @param item, Not Null. */ - public void addItem(JID item) { + @Override + public void addItem(JID item) { NotNull.exceptIfNull(item, "item"); items.add(item); } @@ -44,7 +45,8 @@ public class UnblockPayload extends Payload { /** * @return items, Not Null. */ - public Vector<JID> getItems() { + @Override + public Vector<JID> getItems() { return items; } }
\ No newline at end of file diff --git a/src/com/isode/stroke/parser/payloadparsers/BlockBlockListPayloadParser.java b/src/com/isode/stroke/parser/payloadparsers/BlockBlockListPayloadParser.java new file mode 100644 index 0000000..b957a31 --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/BlockBlockListPayloadParser.java @@ -0,0 +1,26 @@ +/* Copyright (c) 2016, Isode Limited, London, England. + * All rights reserved. + * + * Acquisition and use of this software and related materials for any + * purpose requires a written license agreement from Isode Limited, + * or a written license from an organisation licensed by Isode Limited + * to grant such a license. + * + */ +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.elements.BlockListPayload; + +/** + * {@link BlockParser} for pay loads of type {@link BlockListPayload} + */ +public class BlockBlockListPayloadParser extends BlockParser<BlockListPayload> { + + /** + * Constructor + */ + public BlockBlockListPayloadParser() { + super(new BlockListPayload()); + } + +} diff --git a/src/com/isode/stroke/parser/payloadparsers/BlockBlockPayloadParser.java b/src/com/isode/stroke/parser/payloadparsers/BlockBlockPayloadParser.java new file mode 100644 index 0000000..3a0a3df --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/BlockBlockPayloadParser.java @@ -0,0 +1,26 @@ +/* Copyright (c) 2016, Isode Limited, London, England. + * All rights reserved. + * + * Acquisition and use of this software and related materials for any + * purpose requires a written license agreement from Isode Limited, + * or a written license from an organisation licensed by Isode Limited + * to grant such a license. + * + */ +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.elements.BlockPayload; + +/** + * {@link BlockParser} for pay loads of type {@link BlockPayload} + */ +public class BlockBlockPayloadParser extends BlockParser<BlockPayload> { + + /** + * Constructor + */ + public BlockBlockPayloadParser() { + super(new BlockPayload()); + } + +} diff --git a/src/com/isode/stroke/parser/payloadparsers/BlockParser.java b/src/com/isode/stroke/parser/payloadparsers/BlockParser.java new file mode 100644 index 0000000..f7304df --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/BlockParser.java @@ -0,0 +1,71 @@ +/* Copyright (c) 2016, Isode Limited, London, England. + * All rights reserved. + * + * Acquisition and use of this software and related materials for any + * purpose requires a written license agreement from Isode Limited, + * or a written license from an organisation licensed by Isode Limited + * to grant such a license. + * + */ +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.elements.AbstractBlockPayload; +import com.isode.stroke.elements.BlockListPayload; +import com.isode.stroke.elements.BlockPayload; +import com.isode.stroke.elements.Payload; +import com.isode.stroke.elements.UnblockPayload; +import com.isode.stroke.jid.JID; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.GenericPayloadParser; + + +/** + * Base class for parser that pass Block pay loads such as + * {@link BlockPayload}, {@link BlockListPayload} and {@link UnblockPayload} + * @param <T> Type of {@link Payload} that will be parsed. + */ +public abstract class BlockParser<T extends AbstractBlockPayload> extends GenericPayloadParser<T> { + /* + * Note this is slightly different to Swiften code as templates in C++ work + * different to Java Generics. In Swiften there exits a BlockParser template + * for which instances can be created of type <BlockPayload>, <BlockListPayload> and + * <UnblockPayload>. In Java as we need to pass an instance of the class to the constructor + * (which we can't do with generics) we need to create specific sub types for each of + * the different Block payloads. + */ + + private int level = 0; + + /** + * Constructor + * @param payload New instance of T to create the parsed + * version from. + */ + protected BlockParser(T payload) { + super(payload); + } + + @Override + public void handleStartElement(String element, String ns, + AttributeMap attributes) { + if (level == 1 && "item".equals(element)) { + JID jid = new JID(attributes.getAttribute("jid")); + if (jid.isValid()) { + AbstractBlockPayload payload = getPayloadInternal(); + payload.addItem(jid); + } + } + ++level; + } + + @Override + public void handleEndElement(String element, String ns) { + --level; + } + + @Override + public void handleCharacterData(String data) { + // Empty method + } + +} diff --git a/src/com/isode/stroke/parser/payloadparsers/BlockUnblockPayloadPaser.java b/src/com/isode/stroke/parser/payloadparsers/BlockUnblockPayloadPaser.java new file mode 100644 index 0000000..6ae9f83 --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/BlockUnblockPayloadPaser.java @@ -0,0 +1,26 @@ +/* Copyright (c) 2016, Isode Limited, London, England. + * All rights reserved. + * + * Acquisition and use of this software and related materials for any + * purpose requires a written license agreement from Isode Limited, + * or a written license from an organisation licensed by Isode Limited + * to grant such a license. + * + */ +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.elements.UnblockPayload; + +/** + * {@link BlockParser} for pay loads of type {@link UnblockPayload} + */ +public class BlockUnblockPayloadPaser extends BlockParser<UnblockPayload> { + + /** + * Constructor + */ + public BlockUnblockPayloadPaser() { + super(new UnblockPayload()); + } + +} diff --git a/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java b/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java index 9cb7cb8..39dd9f7 100644 --- a/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java +++ b/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java @@ -8,7 +8,6 @@ import com.isode.stroke.parser.GenericPayloadParserFactory; import com.isode.stroke.parser.GenericPayloadParserFactory2; import com.isode.stroke.parser.PayloadParserFactory; import com.isode.stroke.parser.PayloadParserFactoryCollection; -import com.isode.stroke.parser.payloadparsers.PubSubOwnerPubSubParser; public class FullPayloadParserFactoryCollection extends PayloadParserFactoryCollection { public FullPayloadParserFactoryCollection() { @@ -33,9 +32,9 @@ public class FullPayloadParserFactoryCollection extends PayloadParserFactoryColl addFactory(new GenericPayloadParserFactory<CapsInfoParser> ("c", "http://jabber.org/protocol/caps", CapsInfoParser.class)); addFactory(new ResourceBindParserFactory()); addFactory(new StartSessionParserFactory()); - // addFactory(new GenericPayloadParserFactory<BlockParser<BlockPayload>("block", "urn:xmpp:blocking", BlockParser.class)); - // addFactory(GenericPayloadParserFactory<BlockParser<BlockListPayload>("blocklist", "urn:xmpp:blocking", BlockParser.class)); - // addFactory(GenericPayloadParserFactory<BlockParser<UnblockPayload>("unblock", "urn:xmpp:blocking", BlockParser.class)); + addFactory(new GenericPayloadParserFactory<BlockBlockPayloadParser>("block", "urn:xmpp:blocking", BlockBlockPayloadParser.class)); + addFactory(new GenericPayloadParserFactory<BlockBlockListPayloadParser>("blocklist", "urn:xmpp:blocking", BlockBlockListPayloadParser.class)); + addFactory(new GenericPayloadParserFactory<BlockUnblockPayloadPaser>("unblock", "urn:xmpp:blocking", BlockUnblockPayloadPaser.class)); addFactory(new SecurityLabelParserFactory()); addFactory(new GenericPayloadParserFactory<SecurityLabelsCatalogParser>("catalog", "urn:xmpp:sec-label:catalog:2", SecurityLabelsCatalogParser.class)); addFactory(new FormParserFactory()); diff --git a/src/com/isode/stroke/serializer/payloadserializers/BlockBlockListPayloadSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/BlockBlockListPayloadSerializer.java new file mode 100644 index 0000000..8556f6d --- /dev/null +++ b/src/com/isode/stroke/serializer/payloadserializers/BlockBlockListPayloadSerializer.java @@ -0,0 +1,27 @@ +/* Copyright (c) 2016, Isode Limited, London, England. + * All rights reserved. + * + * Acquisition and use of this software and related materials for any + * purpose requires a written license agreement from Isode Limited, + * or a written license from an organisation licensed by Isode Limited + * to grant such a license. + * + */ +package com.isode.stroke.serializer.payloadserializers; + +import com.isode.stroke.elements.BlockListPayload; + +/** + * {@link BlockSerializer} for {@link BlockListPayload} + */ +public class BlockBlockListPayloadSerializer extends BlockSerializer<BlockListPayload> { + + /** + * Constructor + * @param tag Tag + */ + protected BlockBlockListPayloadSerializer(String tag) { + super(BlockListPayload.class, tag); + } + +} diff --git a/src/com/isode/stroke/serializer/payloadserializers/BlockBlockPayloadSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/BlockBlockPayloadSerializer.java new file mode 100644 index 0000000..4800681 --- /dev/null +++ b/src/com/isode/stroke/serializer/payloadserializers/BlockBlockPayloadSerializer.java @@ -0,0 +1,27 @@ +/* Copyright (c) 2016, Isode Limited, London, England. + * All rights reserved. + * + * Acquisition and use of this software and related materials for any + * purpose requires a written license agreement from Isode Limited, + * or a written license from an organisation licensed by Isode Limited + * to grant such a license. + * + */ +package com.isode.stroke.serializer.payloadserializers; + +import com.isode.stroke.elements.BlockPayload; + +/** + * {@link BlockSerializer} for {@link BlockPayload} + */ +public class BlockBlockPayloadSerializer extends BlockSerializer<BlockPayload> { + + /** + * Constructor + * @param tag Tag + */ + public BlockBlockPayloadSerializer(String tag) { + super(BlockPayload.class, tag); + } + +} diff --git a/src/com/isode/stroke/serializer/payloadserializers/BlockSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/BlockSerializer.java new file mode 100644 index 0000000..2bdfea9 --- /dev/null +++ b/src/com/isode/stroke/serializer/payloadserializers/BlockSerializer.java @@ -0,0 +1,55 @@ +/* Copyright (c) 2016, Isode Limited, London, England. + * All rights reserved. + * + * Acquisition and use of this software and related materials for any + * purpose requires a written license agreement from Isode Limited, + * or a written license from an organisation licensed by Isode Limited + * to grant such a license. + * + */ +package com.isode.stroke.serializer.payloadserializers; + +import com.isode.stroke.elements.AbstractBlockPayload; +import com.isode.stroke.jid.JID; +import com.isode.stroke.serializer.GenericPayloadSerializer; +import com.isode.stroke.serializer.xml.XMLElement; + +/** + * Abstract class for serializing pay loads of type {@link AbstractBlockPayload} + * + * @param <T> Type of {@link AbstractBlockPayload} to serialize + */ +public abstract class BlockSerializer<T extends AbstractBlockPayload> extends GenericPayloadSerializer<T> { + /* + * Note this is slightly different to Swiften code as templates in C++ work + * different to Java Generics. In Swiften there exits a BlockSerialize template + * for which instances can be created of type <BlockPayload>, <BlockListPayload> and + * <UnblockPayload>. In Java as we need to pass an instance of the class to the constructor + * (which we can't do with generics) we need to create specific sub types for each of + * the different Block payloads. + */ + + private final String tag; + + /** + * Constructor + * @param c Class of the {@link AbstractBlockPayload} that this will serialize + * @param tag Tag for the XML element to serialize + */ + protected BlockSerializer(Class<T> c,String tag) { + super(c); + this.tag = tag; + } + + @Override + protected String serializePayload(T payload) { + XMLElement element = new XMLElement(tag,"urn:xmpp:blocking"); + for (JID jid : payload.getItems()) { + XMLElement item = new XMLElement("item"); + item.setAttribute("jid", jid.toString()); + element.addNode(item); + } + return element.serialize(); + } + +} diff --git a/src/com/isode/stroke/serializer/payloadserializers/BlockUnblockPayloadSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/BlockUnblockPayloadSerializer.java new file mode 100644 index 0000000..598b708 --- /dev/null +++ b/src/com/isode/stroke/serializer/payloadserializers/BlockUnblockPayloadSerializer.java @@ -0,0 +1,26 @@ +/* Copyright (c) 2016, Isode Limited, London, England. + * All rights reserved. + * + * Acquisition and use of this software and related materials for any + * purpose requires a written license agreement from Isode Limited, + * or a written license from an organisation licensed by Isode Limited + * to grant such a license. + * + */ +package com.isode.stroke.serializer.payloadserializers; + +import com.isode.stroke.elements.UnblockPayload; + +/** + * {@link BlockSerializer} for {@link UnblockPayload} + */ +public class BlockUnblockPayloadSerializer extends BlockSerializer<UnblockPayload> { + + /** + * @param tag Tag + */ + public BlockUnblockPayloadSerializer(String tag) { + super(UnblockPayload.class, tag); + } + +} |