diff options
Diffstat (limited to 'Swiften/Parser/PayloadParsers')
6 files changed, 65 insertions, 5 deletions
diff --git a/Swiften/Parser/PayloadParsers/DiscoInfoParser.cpp b/Swiften/Parser/PayloadParsers/DiscoInfoParser.cpp index 81f2ce8..c47c703 100644 --- a/Swiften/Parser/PayloadParsers/DiscoInfoParser.cpp +++ b/Swiften/Parser/PayloadParsers/DiscoInfoParser.cpp @@ -5,13 +5,14 @@ */ #include "Swiften/Parser/PayloadParsers/DiscoInfoParser.h" +#include "Swiften/Parser/PayloadParsers/FormParser.h" namespace Swift { -DiscoInfoParser::DiscoInfoParser() : level_(TopLevel) { +DiscoInfoParser::DiscoInfoParser() : level_(TopLevel), formParser_(NULL) { } -void DiscoInfoParser::handleStartElement(const String& element, const String&, const AttributeMap& attributes) { +void DiscoInfoParser::handleStartElement(const String& element, const String& ns, const AttributeMap& attributes) { if (level_ == PayloadLevel) { if (element == "identity") { getPayloadInternal()->addIdentity(DiscoInfo::Identity(attributes.getAttribute("name"), attributes.getAttribute("category"), attributes.getAttribute("type"), attributes.getAttribute("lang"))); @@ -19,15 +20,33 @@ void DiscoInfoParser::handleStartElement(const String& element, const String&, c else if (element == "feature") { getPayloadInternal()->addFeature(attributes.getAttribute("var")); } + else if (element == "x" && ns == "jabber:x:data") { + assert(!formParser_); + formParser_ = new FormParser(); + } + } + if (formParser_) { + formParser_->handleStartElement(element, ns, attributes); } ++level_; } -void DiscoInfoParser::handleEndElement(const String&, const String&) { +void DiscoInfoParser::handleEndElement(const String& element, const String& ns) { --level_; + if (formParser_) { + formParser_->handleEndElement(element, ns); + } + if (level_ == PayloadLevel && formParser_) { + getPayloadInternal()->addExtension(formParser_->getPayloadInternal()); + delete formParser_; + formParser_ = NULL; + } } -void DiscoInfoParser::handleCharacterData(const String&) { +void DiscoInfoParser::handleCharacterData(const String& data) { + if (formParser_) { + formParser_->handleCharacterData(data); + } } } diff --git a/Swiften/Parser/PayloadParsers/DiscoInfoParser.h b/Swiften/Parser/PayloadParsers/DiscoInfoParser.h index 925d349..d9bfb54 100644 --- a/Swiften/Parser/PayloadParsers/DiscoInfoParser.h +++ b/Swiften/Parser/PayloadParsers/DiscoInfoParser.h @@ -24,5 +24,6 @@ namespace Swift { PayloadLevel = 1 }; int level_; + FormParser* formParser_; }; } diff --git a/Swiften/Parser/PayloadParsers/FormParser.cpp b/Swiften/Parser/PayloadParsers/FormParser.cpp index f08e7a3..dc15ece 100644 --- a/Swiften/Parser/PayloadParsers/FormParser.cpp +++ b/Swiften/Parser/PayloadParsers/FormParser.cpp @@ -66,6 +66,9 @@ void FormParser::handleStartElement(const String& element, const String&, const else if (type == "text-single") { currentFieldParseHelper_ = TextSingleFormFieldParseHelper::create(); } + else { + currentFieldParseHelper_ = UntypedFormFieldParseHelper::create(); + } if (currentFieldParseHelper_) { currentFieldParseHelper_->getField()->setName(attributes.getAttribute("var")); currentFieldParseHelper_->getField()->setLabel(attributes.getAttribute("label")); diff --git a/Swiften/Parser/PayloadParsers/FormParser.h b/Swiften/Parser/PayloadParsers/FormParser.h index 76a54b9..c41e27f 100644 --- a/Swiften/Parser/PayloadParsers/FormParser.h +++ b/Swiften/Parser/PayloadParsers/FormParser.h @@ -32,6 +32,7 @@ namespace Swift { class BoolFieldParseHelper : public FieldParseHelper { virtual void addValue(const String& s) { boost::dynamic_pointer_cast< GenericFormField<bool> >(getField())->setValue(s == "1" || s == "true"); + getField()->addRawValue(s); } }; class StringFieldParseHelper : public FieldParseHelper { @@ -43,6 +44,7 @@ namespace Swift { else { field->setValue(field->getValue() + "\n" + s); } + getField()->addRawValue(s); } }; class JIDFieldParseHelper : public FieldParseHelper { @@ -57,6 +59,7 @@ namespace Swift { std::vector<String> l = field->getValue(); l.push_back(s); field->setValue(l); + getField()->addRawValue(s); } }; class JIDListFieldParseHelper : public FieldParseHelper { @@ -66,6 +69,7 @@ namespace Swift { std::vector<JID> l = field->getValue(); l.push_back(JID(s)); field->setValue(l); + getField()->addRawValue(s); } }; @@ -92,6 +96,7 @@ namespace Swift { SWIFTEN_DECLARE_FORM_FIELD_PARSE_HELPER(JIDSingle, JID); SWIFTEN_DECLARE_FORM_FIELD_PARSE_HELPER(JIDMulti, JIDList); SWIFTEN_DECLARE_FORM_FIELD_PARSE_HELPER(ListMulti, StringList); + SWIFTEN_DECLARE_FORM_FIELD_PARSE_HELPER(Untyped, StringList); enum Level { TopLevel = 0, diff --git a/Swiften/Parser/PayloadParsers/UnitTest/DiscoInfoParserTest.cpp b/Swiften/Parser/PayloadParsers/UnitTest/DiscoInfoParserTest.cpp index 2e7e7af..79b28db 100644 --- a/Swiften/Parser/PayloadParsers/UnitTest/DiscoInfoParserTest.cpp +++ b/Swiften/Parser/PayloadParsers/UnitTest/DiscoInfoParserTest.cpp @@ -16,6 +16,7 @@ class DiscoInfoParserTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(DiscoInfoParserTest); CPPUNIT_TEST(testParse); + CPPUNIT_TEST(testParse_Form); CPPUNIT_TEST_SUITE_END(); public: @@ -48,6 +49,27 @@ class DiscoInfoParserTest : public CppUnit::TestFixture CPPUNIT_ASSERT_EQUAL(String("bar-feature"), payload->getFeatures()[1]); CPPUNIT_ASSERT_EQUAL(String("baz-feature"), payload->getFeatures()[2]); } + + void testParse_Form() { + PayloadsParserTester parser; + + CPPUNIT_ASSERT(parser.parse( + "<query xmlns=\"http://jabber.org/protocol/disco#info\">" + "<feature var=\"foo-feature\"/>" + "<x type=\"submit\" xmlns=\"jabber:x:data\">" + "<title>Bot Configuration</title>" + "<instructions>Hello!</instructions>" + "</x>" + "<feature var=\"bar-feature\"/>" + "</query>")); + + DiscoInfo* payload = dynamic_cast<DiscoInfo*>(parser.getPayload().get()); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(payload->getExtensions().size())); + CPPUNIT_ASSERT_EQUAL(String("Bot Configuration"), payload->getExtensions()[0]->getTitle()); + CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(payload->getFeatures().size())); + CPPUNIT_ASSERT_EQUAL(String("foo-feature"), payload->getFeatures()[0]); + CPPUNIT_ASSERT_EQUAL(String("bar-feature"), payload->getFeatures()[1]); + } }; CPPUNIT_TEST_SUITE_REGISTRATION(DiscoInfoParserTest); diff --git a/Swiften/Parser/PayloadParsers/UnitTest/FormParserTest.cpp b/Swiften/Parser/PayloadParsers/UnitTest/FormParserTest.cpp index 0d6e85e..aede75d 100644 --- a/Swiften/Parser/PayloadParsers/UnitTest/FormParserTest.cpp +++ b/Swiften/Parser/PayloadParsers/UnitTest/FormParserTest.cpp @@ -75,11 +75,15 @@ class FormParserTest : public CppUnit::TestFixture { "<value>foo@bar.com</value>" "<value>baz@fum.org</value>" "</field>" + "<field var=\"untyped\">" + "<value>foo</value>" + "<value>baz</value>" + "</field>" "</x>")); Form* payload = dynamic_cast<Form*>(parser.getPayload().get()); - CPPUNIT_ASSERT_EQUAL(9, static_cast<int>(payload->getFields().size())); + CPPUNIT_ASSERT_EQUAL(10, static_cast<int>(payload->getFields().size())); CPPUNIT_ASSERT_EQUAL(String("jabber:bot"), boost::dynamic_pointer_cast<HiddenFormField>(payload->getFields()[0])->getValue()); CPPUNIT_ASSERT_EQUAL(String("FORM_TYPE"), payload->getFields()[0]->getName()); CPPUNIT_ASSERT(!payload->getFields()[0]->getRequired()); @@ -92,9 +96,12 @@ class FormParserTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(true, boost::dynamic_pointer_cast<BooleanFormField>(payload->getFields()[4])->getValue()); CPPUNIT_ASSERT(payload->getFields()[4]->getRequired()); + CPPUNIT_ASSERT_EQUAL(String("1"), boost::dynamic_pointer_cast<BooleanFormField>(payload->getFields()[4])->getRawValues()[0]); CPPUNIT_ASSERT_EQUAL(String("news"), boost::dynamic_pointer_cast<ListMultiFormField>(payload->getFields()[6])->getValue()[0]); + CPPUNIT_ASSERT_EQUAL(String("news"), payload->getFields()[6]->getRawValues()[0]); CPPUNIT_ASSERT_EQUAL(String("search"), boost::dynamic_pointer_cast<ListMultiFormField>(payload->getFields()[6])->getValue()[1]); + CPPUNIT_ASSERT_EQUAL(String("search"), payload->getFields()[6]->getRawValues()[1]); CPPUNIT_ASSERT_EQUAL(5, static_cast<int>(payload->getFields()[6]->getOptions().size())); CPPUNIT_ASSERT_EQUAL(String("Contests"), payload->getFields()[6]->getOptions()[0].label); CPPUNIT_ASSERT_EQUAL(String("contests"), payload->getFields()[6]->getOptions()[0].value); @@ -106,6 +113,9 @@ class FormParserTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(JID("foo@bar.com"), boost::dynamic_pointer_cast<JIDMultiFormField>(payload->getFields()[8])->getValue()[0]); CPPUNIT_ASSERT_EQUAL(JID("baz@fum.org"), boost::dynamic_pointer_cast<JIDMultiFormField>(payload->getFields()[8])->getValue()[1]); CPPUNIT_ASSERT_EQUAL(String("Tell all your friends about your new bot!"), payload->getFields()[8]->getDescription()); + + CPPUNIT_ASSERT_EQUAL(String("foo"), boost::dynamic_pointer_cast<UntypedFormField>(payload->getFields()[9])->getValue()[0]); + CPPUNIT_ASSERT_EQUAL(String("baz"), boost::dynamic_pointer_cast<UntypedFormField>(payload->getFields()[9])->getValue()[1]); } }; |