diff options
Diffstat (limited to 'src/com/isode/stroke/parser/payloadparsers')
4 files changed, 178 insertions, 2 deletions
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<CapsInfoParser> ("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<SecurityLabelsCatalogParser>("catalog", "urn:xmpp:sec-label:catalog:2", SecurityLabelsCatalogParser.class)); addFactory(new FormParserFactory()); addFactory(new GenericPayloadParserFactory<CommandParser>("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<SecurityLabel> { + + 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<SecurityLabelParser> { + + 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<SecurityLabelsCatalog> { + + 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); + } + } + +} |