From 6385fd0f848ffd829ca52a04a90989730a10616f Mon Sep 17 00:00:00 2001 From: Alan Young Date: Thu, 30 Apr 2015 18:12:17 +0200 Subject: Add parser & serializer for SecurityLabel & SecurityLabelsCatalog Change visibility of some methods in SecurityLabel & SecurityLabelsCatalog and provide default values for strings and collections in those clases. Change-Id: I1ea3f6b20deac1d1ac7999dd304e2e755d5e7c8a diff --git a/src/com/isode/stroke/elements/SecurityLabel.java b/src/com/isode/stroke/elements/SecurityLabel.java index 8134811..d20d529 100644 --- a/src/com/isode/stroke/elements/SecurityLabel.java +++ b/src/com/isode/stroke/elements/SecurityLabel.java @@ -10,53 +10,53 @@ import java.util.Collection; public class SecurityLabel extends Payload { private Collection equivalentLabels = new ArrayList(); - private String foregroundColor; - private String displayMarking; - private String backgroundColor; - private String label; + private String foregroundColor = ""; + private String displayMarking = ""; + private String backgroundColor = ""; + private String label = ""; - final Collection getEquivalentLabels() { + public final Collection getEquivalentLabels() { return equivalentLabels; } - void setEquivalentLabels(final Collection value) { - this.equivalentLabels = value ; + public void setEquivalentLabels(final Collection value) { + this.equivalentLabels = value; } - void addEquivalentLabel(final String value) { + public void addEquivalentLabel(final String value) { this.equivalentLabels.add(value); } - final String getForegroundColor() { + public final String getForegroundColor() { return foregroundColor; } - void setForegroundColor(final String value) { - this.foregroundColor = value ; + public void setForegroundColor(final String value) { + this.foregroundColor = value; } - final String getDisplayMarking() { + public final String getDisplayMarking() { return displayMarking; } - void setDisplayMarking(final String value) { - this.displayMarking = value ; + public void setDisplayMarking(final String value) { + this.displayMarking = value; } - final String getBackgroundColor() { + public final String getBackgroundColor() { return backgroundColor; } - void setBackgroundColor(final String value) { - this.backgroundColor = value ; + public void setBackgroundColor(final String value) { + this.backgroundColor = value; } - final String getLabel() { + public final String getLabel() { return label; } - void setLabel(final String value) { - this.label = value ; + public void setLabel(final String value) { + this.label = value; } } diff --git a/src/com/isode/stroke/elements/SecurityLabelsCatalog.java b/src/com/isode/stroke/elements/SecurityLabelsCatalog.java index 4bb9493..29cf3f7 100644 --- a/src/com/isode/stroke/elements/SecurityLabelsCatalog.java +++ b/src/com/isode/stroke/elements/SecurityLabelsCatalog.java @@ -4,38 +4,39 @@ */ package com.isode.stroke.elements; +import java.util.ArrayList; import java.util.Collection; import com.isode.stroke.jid.JID; public class SecurityLabelsCatalog extends Payload { private JID to_; - private String name_; - private String description_; - private Collection items_; + private String name_ = ""; + private String description_ = ""; + private Collection items_ = new ArrayList(); - public class Item { + public static class Item { private SecurityLabel label_; - private String selector_; + private String selector_ = ""; private boolean default_; public SecurityLabel getLabel() { return label_; } - void setLabel(SecurityLabel label) { + public void setLabel(SecurityLabel label) { label_ = label; } - final String getSelector() { return selector_; } + public final String getSelector() { return selector_; } - void setSelector(final String selector) { + public void setSelector(final String selector) { selector_ = selector; } - boolean getIsDefault() { return default_; } + public boolean getIsDefault() { return default_; } - void setIsDefault(boolean isDefault) { + public void setIsDefault(boolean isDefault) { default_ = isDefault; } }; diff --git a/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java b/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java index e443a00..af91aff 100644 --- a/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java +++ b/src/com/isode/stroke/parser/payloadparsers/FullPayloadParserFactoryCollection.java @@ -31,8 +31,8 @@ public class FullPayloadParserFactoryCollection extends PayloadParserFactoryColl addFactory(new GenericPayloadParserFactory ("c", "http://jabber.org/protocol/caps", CapsInfoParser.class)); addFactory(new ResourceBindParserFactory()); addFactory(new StartSessionParserFactory()); - //addFactory(new SecurityLabelParserFactory()); - //addFactory(new SecurityLabelsCatalogParserFactory()); + addFactory(new SecurityLabelParserFactory()); + addFactory(new GenericPayloadParserFactory("catalog", "urn:xmpp:sec-label:catalog:2", SecurityLabelsCatalogParser.class)); addFactory(new FormParserFactory()); addFactory(new GenericPayloadParserFactory("command", "http://jabber.org/protocol/commands", CommandParser.class)); diff --git a/src/com/isode/stroke/parser/payloadparsers/SecurityLabelParser.java b/src/com/isode/stroke/parser/payloadparsers/SecurityLabelParser.java new file mode 100644 index 0000000..5370944 --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/SecurityLabelParser.java @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2010-2015, Isode Limited, London, England. + * All rights reserved. + */ + +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.elements.SecurityLabel; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.GenericPayloadParser; +import com.isode.stroke.parser.SerializingParser; + +public class SecurityLabelParser extends GenericPayloadParser { + + private int level_ = 0; + private final static int TopLevel = 0; + private final static int PayloadLevel = 1; + private final static int DisplayMarkingOrLabelLevel = 2; + private final static int SecurityLabelLevel = 3; + + private SerializingParser labelParser_; + + private String currentText_ = ""; + + public SecurityLabelParser() { + super(new SecurityLabel()); + } + + public void handleStartElement(String element, String ns, AttributeMap attributes) { + ++level_; + if (level_ == DisplayMarkingOrLabelLevel) { + if ("displaymarking".equals(element)) { + currentText_ = ""; + getPayloadInternal().setBackgroundColor(attributes.getAttribute("bgcolor")); + getPayloadInternal().setForegroundColor(attributes.getAttribute("fgcolor")); + } + else if ("label".equals(element) || "equivalentlabel".equals(element)) { + assert(labelParser_ == null); + labelParser_ = new SerializingParser(); + } + } + else if (level_ >= SecurityLabelLevel && labelParser_ != null) { + labelParser_.handleStartElement(element, ns, attributes); + } + } + + public void handleEndElement(String element, String ns) { + if (level_ == DisplayMarkingOrLabelLevel) { + if ("displaymarking".equals(element)) { + getPayloadInternal().setDisplayMarking(currentText_); + } + else if (labelParser_ != null) { + if ("label".equals(element)) { + getPayloadInternal().setLabel(labelParser_.getResult()); + } + else { + getPayloadInternal().addEquivalentLabel(labelParser_.getResult()); + } + labelParser_ = null; + } + } + else if (labelParser_ != null && level_ >= SecurityLabelLevel) { + labelParser_.handleEndElement(element, ns); + } + --level_; + } + + public void handleCharacterData(String data) { + if (labelParser_ != null) { + labelParser_.handleCharacterData(data); + } + else { + currentText_ += data; + } + } + + public SecurityLabel getLabelPayload() { + return getPayloadInternal(); + } +} diff --git a/src/com/isode/stroke/parser/payloadparsers/SecurityLabelParserFactory.java b/src/com/isode/stroke/parser/payloadparsers/SecurityLabelParserFactory.java new file mode 100644 index 0000000..dc10578 --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/SecurityLabelParserFactory.java @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2010-2015 Isode Limited, London, England. + * All rights reserved. + */ +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.parser.GenericPayloadParserFactory; + +public class SecurityLabelParserFactory extends GenericPayloadParserFactory { + + public SecurityLabelParserFactory() { + super("securitylabel", "urn:xmpp:sec-label:0", SecurityLabelParser.class); + } + +} diff --git a/src/com/isode/stroke/parser/payloadparsers/SecurityLabelsCatalogParser.java b/src/com/isode/stroke/parser/payloadparsers/SecurityLabelsCatalogParser.java new file mode 100644 index 0000000..b27aab9 --- /dev/null +++ b/src/com/isode/stroke/parser/payloadparsers/SecurityLabelsCatalogParser.java @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2010-2015, Isode Limited, London, England. + * All rights reserved. + */ + +package com.isode.stroke.parser.payloadparsers; + +import com.isode.stroke.elements.SecurityLabel; +import com.isode.stroke.elements.SecurityLabelsCatalog; +import com.isode.stroke.jid.JID; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.GenericPayloadParser; + +public class SecurityLabelsCatalogParser extends GenericPayloadParser { + + private int level_ = 0; + private final static int TopLevel = 0; + private final static int PayloadLevel = 1; + private final static int ItemLevel = 2; + private final static int LabelLevel = 3; + + private final SecurityLabelParserFactory labelParserFactory_; + private SecurityLabelParser labelParser_; + private SecurityLabelsCatalog.Item currentItem_; + + public SecurityLabelsCatalogParser() { + super(new SecurityLabelsCatalog()); + labelParserFactory_ = new SecurityLabelParserFactory(); + } + + public void handleStartElement(String element, String ns, AttributeMap attributes) { + ++level_; + if (level_ == PayloadLevel) { + getPayloadInternal().setTo(new JID(attributes.getAttribute("to"))); + getPayloadInternal().setName(attributes.getAttribute("name")); + getPayloadInternal().setDescription(attributes.getAttribute("desc")); + } + else if (level_ == ItemLevel && "item".equals(element) && "urn:xmpp:sec-label:catalog:2".equals(ns)) { + currentItem_ = new SecurityLabelsCatalog.Item(); + currentItem_.setSelector(attributes.getAttribute("selector")); + currentItem_.setIsDefault(attributes.getBoolAttribute("default", false)); + } + else if (level_ == LabelLevel) { + assert(labelParser_ == null); + if (labelParserFactory_.canParse(element, ns, attributes)) { + labelParser_ = (SecurityLabelParser)(labelParserFactory_.createPayloadParser()); + assert (labelParser_) != null; + } + } + + if (labelParser_ != null) { + labelParser_.handleStartElement(element, ns, attributes); + } + } + + public void handleEndElement(String element, String ns) { + if (labelParser_ != null) { + labelParser_.handleEndElement(element, ns); + } + if (level_ == LabelLevel && labelParser_ != null && currentItem_ != null) { + SecurityLabel currentLabel = labelParser_.getLabelPayload(); + assert (currentLabel) != null; + currentItem_.setLabel(currentLabel); + labelParser_ = null; + } + else if (level_ == ItemLevel && "item".equals(element) && "urn:xmpp:sec-label:catalog:2".equals(ns)) { + if (currentItem_ != null) { + getPayloadInternal().addItem(currentItem_); + currentItem_ = null; + } + } + --level_; + } + + public void handleCharacterData(String data) { + if (labelParser_ != null) { + labelParser_.handleCharacterData(data); + } + } + +} diff --git a/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java b/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java index f4caa29..a0dcebd 100644 --- a/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java +++ b/src/com/isode/stroke/serializer/payloadserializers/FullPayloadSerializerCollection.java @@ -37,8 +37,8 @@ public class FullPayloadSerializerCollection extends PayloadSerializerCollection addSerializer(new CapsInfoSerializer()); addSerializer(new ResourceBindSerializer()); addSerializer(new StartSessionSerializer()); - //addSerializer(new SecurityLabelSerializer()); - //addSerializer(new SecurityLabelsCatalogSerializer()); + addSerializer(new SecurityLabelSerializer()); + addSerializer(new SecurityLabelsCatalogSerializer()); //addSerializer(new StreamInitiationSerializer()); //addSerializer(new BytestreamsSerializer()); addSerializer(new VCardSerializer()); diff --git a/src/com/isode/stroke/serializer/payloadserializers/SecurityLabelSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/SecurityLabelSerializer.java new file mode 100644 index 0000000..69d087b --- /dev/null +++ b/src/com/isode/stroke/serializer/payloadserializers/SecurityLabelSerializer.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2010-2015 Isode Limited, London, England. + * All rights reserved. + */ + +package com.isode.stroke.serializer.payloadserializers; + +import com.isode.stroke.elements.SecurityLabel; +import com.isode.stroke.serializer.GenericPayloadSerializer; +import com.isode.stroke.serializer.xml.XMLElement; +import com.isode.stroke.serializer.xml.XMLRawTextNode; +import com.isode.stroke.serializer.xml.XMLTextNode; + +public class SecurityLabelSerializer extends GenericPayloadSerializer { + + public SecurityLabelSerializer() { + super(SecurityLabel.class); + } + + @Override + protected String serializePayload(SecurityLabel label) { + XMLElement element = new XMLElement("securitylabel", "urn:xmpp:sec-label:0"); + if (!label.getDisplayMarking().isEmpty()) { + XMLElement displayMarking = new XMLElement("displaymarking"); + if (!label.getForegroundColor().isEmpty()) { + displayMarking.setAttribute("fgcolor", label.getForegroundColor()); + } + if (!label.getBackgroundColor().isEmpty()) { + displayMarking.setAttribute("bgcolor", label.getBackgroundColor()); + } + displayMarking.addNode(new XMLTextNode(label.getDisplayMarking())); + element.addNode(displayMarking); + } + + XMLElement labelElement = new XMLElement("label"); + labelElement.addNode(new XMLRawTextNode(label.getLabel())); + element.addNode(labelElement); + + for(String equivalentLabel : label.getEquivalentLabels()) { + XMLElement equivalentLabelElement = new XMLElement("equivalentlabel"); + equivalentLabelElement.addNode(new XMLRawTextNode(equivalentLabel)); + element.addNode(equivalentLabelElement); + } + return element.serialize(); + } +} diff --git a/src/com/isode/stroke/serializer/payloadserializers/SecurityLabelsCatalogSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/SecurityLabelsCatalogSerializer.java new file mode 100644 index 0000000..e203157 --- /dev/null +++ b/src/com/isode/stroke/serializer/payloadserializers/SecurityLabelsCatalogSerializer.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2010-2015 Isode Limited, London, England. + * All rights reserved. + */ + +package com.isode.stroke.serializer.payloadserializers; + +import com.isode.stroke.elements.SecurityLabelsCatalog; +import com.isode.stroke.serializer.GenericPayloadSerializer; +import com.isode.stroke.serializer.xml.XMLElement; +import com.isode.stroke.serializer.xml.XMLRawTextNode; + +public class SecurityLabelsCatalogSerializer extends GenericPayloadSerializer { + + public SecurityLabelsCatalogSerializer() { + super(SecurityLabelsCatalog.class); + } + + @Override + protected String serializePayload(SecurityLabelsCatalog catalog) { + XMLElement element = new XMLElement("catalog", "urn:xmpp:sec-label:catalog:2"); + if (!catalog.getName().isEmpty()) { + element.setAttribute("name", catalog.getName()); + } + if (catalog.getTo().isValid()) { + element.setAttribute("to", catalog.getTo().toString()); + } + if (!catalog.getDescription().isEmpty()) { + element.setAttribute("desc", catalog.getDescription()); + } + for (SecurityLabelsCatalog.Item item : catalog.getItems()) { + XMLElement itemElement = new XMLElement("item"); + itemElement.setAttribute("selector", item.getSelector()); + if (item.getIsDefault()) { + itemElement.setAttribute("default", "true"); + } + if (item.getLabel() != null) { + String serializedLabel = new SecurityLabelSerializer().serialize(item.getLabel()); + itemElement.addNode(new XMLRawTextNode(serializedLabel)); + } + element.addNode(itemElement); + } + return element.serialize(); + } +} -- cgit v0.10.2-6-g49f6