summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTobias Markmann <tm@ayena.de>2012-01-06 22:18:25 (GMT)
committerKevin Smith <git@kismith.co.uk>2012-01-23 14:51:26 (GMT)
commit83afa3d4bf18e4feb8a33d084ed181508084fc64 (patch)
treeb81a0c6eaaedbad3f3e607ea52a69acf98f95eff /Swiften/Parser/PayloadParsers
parent093d499945d779cfed92b45e413644834004b0d9 (diff)
downloadswift-83afa3d4bf18e4feb8a33d084ed181508084fc64.zip
swift-83afa3d4bf18e4feb8a33d084ed181508084fc64.tar.bz2
XEP-0004 form support for user search.
License: This patch is BSD-licensed, see http://www.opensource.org/licenses/bsd-license.php
Diffstat (limited to 'Swiften/Parser/PayloadParsers')
-rw-r--r--Swiften/Parser/PayloadParsers/FormParser.cpp61
-rw-r--r--Swiften/Parser/PayloadParsers/FormParser.h4
-rw-r--r--Swiften/Parser/PayloadParsers/UnitTest/FormParserTest.cpp67
-rw-r--r--Swiften/Parser/PayloadParsers/UnitTest/SearchPayloadParserTest.cpp110
4 files changed, 230 insertions, 12 deletions
diff --git a/Swiften/Parser/PayloadParsers/FormParser.cpp b/Swiften/Parser/PayloadParsers/FormParser.cpp
index 3905302..2df0a85 100644
--- a/Swiften/Parser/PayloadParsers/FormParser.cpp
+++ b/Swiften/Parser/PayloadParsers/FormParser.cpp
@@ -6,9 +6,11 @@
#include <Swiften/Parser/PayloadParsers/FormParser.h>
+#include <Swiften/Base/foreach.h>
+
namespace Swift {
-FormParser::FormParser() : level_(TopLevel) {
+FormParser::FormParser() : level_(TopLevel), parsingItem_(false), parsingReported_(false) {
}
void FormParser::handleStartElement(const std::string& element, const std::string&, const AttributeMap& attributes) {
@@ -35,32 +37,43 @@ void FormParser::handleStartElement(const std::string& element, const std::strin
currentText_.clear();
}
else if (element == "field") {
- std::string type = attributes.getAttribute("type");
- if (type == "boolean") {
+ std::string type;
+ FormField::ref correspondingReportedField;
+ if (!parsingItem_) {
+ type = attributes.getAttribute("type");
+ } else {
+ foreach(FormField::ref field, getPayloadInternal()->getReportedFields()) {
+ if (field->getName() == attributes.getAttribute("var")) {
+ correspondingReportedField = field;
+ break;
+ }
+ }
+ }
+ if (type == "boolean" || boost::dynamic_pointer_cast<BooleanFormField>(correspondingReportedField)) {
currentFieldParseHelper_ = BooleanFormFieldParseHelper::create();
}
- else if (type == "fixed") {
+ else if (type == "fixed" || boost::dynamic_pointer_cast<FixedFormField>(correspondingReportedField)) {
currentFieldParseHelper_ = FixedFormFieldParseHelper::create();
}
- else if (type == "hidden") {
+ else if (type == "hidden" || boost::dynamic_pointer_cast<HiddenFormField>(correspondingReportedField)) {
currentFieldParseHelper_ = HiddenFormFieldParseHelper::create();
}
- else if (type == "jid-multi") {
+ else if (type == "jid-multi" || boost::dynamic_pointer_cast<JIDMultiFormField>(correspondingReportedField)) {
currentFieldParseHelper_ = JIDMultiFormFieldParseHelper::create();
}
- else if (type == "jid-single") {
+ else if (type == "jid-single" || boost::dynamic_pointer_cast<JIDSingleFormField>(correspondingReportedField)) {
currentFieldParseHelper_ = JIDSingleFormFieldParseHelper::create();
}
- else if (type == "list-multi") {
+ else if (type == "list-multi" || boost::dynamic_pointer_cast<ListMultiFormField>(correspondingReportedField)) {
currentFieldParseHelper_ = ListMultiFormFieldParseHelper::create();
}
- else if (type == "list-single") {
+ else if (type == "list-single" || boost::dynamic_pointer_cast<ListSingleFormField>(correspondingReportedField)) {
currentFieldParseHelper_ = ListSingleFormFieldParseHelper::create();
}
- else if (type == "text-multi") {
+ else if (type == "text-multi" || boost::dynamic_pointer_cast<TextMultiFormField>(correspondingReportedField)) {
currentFieldParseHelper_ = TextMultiFormFieldParseHelper::create();
}
- else if (type == "text-private") {
+ else if (type == "text-private" || boost::dynamic_pointer_cast<TextPrivateFormField>(correspondingReportedField)) {
currentFieldParseHelper_ = TextPrivateFormFieldParseHelper::create();
}
else /*if (type == "text-single") || undefined */ {
@@ -71,6 +84,14 @@ void FormParser::handleStartElement(const std::string& element, const std::strin
currentFieldParseHelper_->getField()->setLabel(attributes.getAttribute("label"));
}
}
+ else if (element == "reported") {
+ parsingReported_ = true;
+ level_ = PayloadLevel - 1;
+ }
+ else if (element == "item") {
+ parsingItem_ = true;
+ level_ = PayloadLevel - 1;
+ }
}
else if (level_ == FieldLevel && currentFieldParseHelper_) {
currentText_.clear();
@@ -104,7 +125,13 @@ void FormParser::handleEndElement(const std::string& element, const std::string&
}
else if (element == "field") {
if (currentFieldParseHelper_) {
- getPayloadInternal()->addField(currentFieldParseHelper_->getField());
+ if (parsingReported_) {
+ getPayloadInternal()->addReportedField(currentFieldParseHelper_->getField());
+ } else if (parsingItem_) {
+ currentFields_.push_back(currentFieldParseHelper_->getField());
+ } else {
+ getPayloadInternal()->addField(currentFieldParseHelper_->getField());
+ }
currentFieldParseHelper_.reset();
}
}
@@ -123,6 +150,16 @@ void FormParser::handleEndElement(const std::string& element, const std::string&
currentFieldParseHelper_->addValue(currentText_);
}
}
+ if (element == "reported") {
+ parsingReported_ = false;
+ level_++;
+ }
+ else if (element == "item") {
+ parsingItem_ = false;
+ level_++;
+ getPayloadInternal()->addItem(currentFields_);
+ currentFields_.clear();
+ }
}
void FormParser::handleCharacterData(const std::string& text) {
diff --git a/Swiften/Parser/PayloadParsers/FormParser.h b/Swiften/Parser/PayloadParsers/FormParser.h
index eae40a1..a3e2524 100644
--- a/Swiften/Parser/PayloadParsers/FormParser.h
+++ b/Swiften/Parser/PayloadParsers/FormParser.h
@@ -49,6 +49,7 @@ namespace Swift {
};
class JIDFieldParseHelper : public FieldParseHelper {
virtual void addValue(const std::string& s) {
+ getField()->addRawValue(s);
boost::dynamic_pointer_cast< GenericFormField<JID> >(getField())->setValue(JID(s));
}
};
@@ -106,5 +107,8 @@ namespace Swift {
std::string currentText_;
std::string currentOptionLabel_;
boost::shared_ptr<FieldParseHelper> currentFieldParseHelper_;
+ bool parsingItem_;
+ bool parsingReported_;
+ std::vector<FormField::ref> currentFields_;
};
}
diff --git a/Swiften/Parser/PayloadParsers/UnitTest/FormParserTest.cpp b/Swiften/Parser/PayloadParsers/UnitTest/FormParserTest.cpp
index 86845be..c36fbeb 100644
--- a/Swiften/Parser/PayloadParsers/UnitTest/FormParserTest.cpp
+++ b/Swiften/Parser/PayloadParsers/UnitTest/FormParserTest.cpp
@@ -16,6 +16,7 @@ class FormParserTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(FormParserTest);
CPPUNIT_TEST(testParse_FormInformation);
CPPUNIT_TEST(testParse);
+ CPPUNIT_TEST(testParse_FormItems);
CPPUNIT_TEST_SUITE_END();
public:
@@ -115,6 +116,72 @@ class FormParserTest : public CppUnit::TestFixture {
CPPUNIT_ASSERT_EQUAL(std::string("foo"), boost::dynamic_pointer_cast<TextSingleFormField>(payload->getFields()[9])->getValue());
}
+
+ void testParse_FormItems() {
+ PayloadsParserTester parser;
+
+ CPPUNIT_ASSERT(parser.parse(
+ "<x xmlns='jabber:x:data' type='result'>"
+ "<field type='hidden' var='FORM_TYPE'>"
+ "<value>jabber:iq:search</value>"
+ "</field>"
+ "<reported>"
+ "<field var='first' label='Given Name' type='text-single'/>"
+ "<field var='last' label='Family Name' type='text-single'/>"
+ "<field var='jid' label='Jabber ID' type='jid-single'/>"
+ "<field var='x-gender' label='Gender' type='list-single'/>"
+ "</reported>"
+ "<item>"
+ "<field var='first'><value>Benvolio</value></field>"
+ "<field var='last'><value>Montague</value></field>"
+ "<field var='jid'><value>benvolio@montague.net</value></field>"
+ "<field var='x-gender'><value>male</value></field>"
+ "</item>"
+ "<item>"
+ "<field var='first'><value>Romeo</value></field>"
+ "<field var='last'><value>Montague</value></field>"
+ "<field var='jid'><value>romeo@montague.net</value></field>"
+ "<field var='x-gender'><value>male</value></field>"
+ "</item>"
+ "</x>"));
+
+ Form* dataForm = dynamic_cast<Form*>(parser.getPayload().get());
+ CPPUNIT_ASSERT(dataForm);
+
+ Form::FormItem reported = dataForm->getReportedFields();
+ CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(4), reported.size());
+
+ std::vector<Form::FormItem> items = dataForm->getItems();
+ CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), items.size());
+
+ Form::FormItem item = items[0];
+ CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(4), item.size());
+
+ CPPUNIT_ASSERT_EQUAL(std::string("Benvolio"), item[0]->getRawValues()[0]);
+ CPPUNIT_ASSERT_EQUAL(std::string("first"), item[0]->getName());
+ CPPUNIT_ASSERT_EQUAL(std::string("Montague"), item[1]->getRawValues()[0]);
+ CPPUNIT_ASSERT_EQUAL(std::string("last"), item[1]->getName());
+ JIDSingleFormField::ref jidField = boost::dynamic_pointer_cast<JIDSingleFormField>(item[2]);
+ CPPUNIT_ASSERT(jidField);
+ CPPUNIT_ASSERT_EQUAL(JID("benvolio@montague.net"), jidField->getValue());
+ CPPUNIT_ASSERT_EQUAL(std::string("jid"), item[2]->getName());
+ CPPUNIT_ASSERT_EQUAL(std::string("male"), item[3]->getRawValues()[0]);
+ CPPUNIT_ASSERT_EQUAL(std::string("x-gender"), item[3]->getName());
+
+ item = items[1];
+ CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(4), item.size());
+
+ CPPUNIT_ASSERT_EQUAL(std::string("Romeo"), item[0]->getRawValues()[0]);
+ CPPUNIT_ASSERT_EQUAL(std::string("first"), item[0]->getName());
+ CPPUNIT_ASSERT_EQUAL(std::string("Montague"), item[1]->getRawValues()[0]);
+ CPPUNIT_ASSERT_EQUAL(std::string("last"), item[1]->getName());
+ jidField = boost::dynamic_pointer_cast<JIDSingleFormField>(item[2]);
+ CPPUNIT_ASSERT(jidField);
+ CPPUNIT_ASSERT_EQUAL(JID("romeo@montague.net"), jidField->getValue());
+ CPPUNIT_ASSERT_EQUAL(std::string("jid"), item[2]->getName());
+ CPPUNIT_ASSERT_EQUAL(std::string("male"), item[3]->getRawValues()[0]);
+ CPPUNIT_ASSERT_EQUAL(std::string("x-gender"), item[3]->getName());
+ }
};
CPPUNIT_TEST_SUITE_REGISTRATION(FormParserTest);
diff --git a/Swiften/Parser/PayloadParsers/UnitTest/SearchPayloadParserTest.cpp b/Swiften/Parser/PayloadParsers/UnitTest/SearchPayloadParserTest.cpp
index c07cd7f..ef48ced 100644
--- a/Swiften/Parser/PayloadParsers/UnitTest/SearchPayloadParserTest.cpp
+++ b/Swiften/Parser/PayloadParsers/UnitTest/SearchPayloadParserTest.cpp
@@ -16,6 +16,8 @@ class SearchPayloadParserTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(SearchPayloadParserTest);
CPPUNIT_TEST(testParse_FormRequestResponse);
CPPUNIT_TEST(testParse_Results);
+ CPPUNIT_TEST(testParse_FormRequestResponse_XDATA);
+ CPPUNIT_TEST(testParse_Results_XDATA);
CPPUNIT_TEST_SUITE_END();
public:
@@ -66,6 +68,114 @@ class SearchPayloadParserTest : public CppUnit::TestFixture {
CPPUNIT_ASSERT_EQUAL(std::string("juliet@shakespeare.lit"), payload->getItems()[0].email);
CPPUNIT_ASSERT_EQUAL(JID("tybalt@shakespeare.lit"), payload->getItems()[1].jid);
}
+
+ void testParse_FormRequestResponse_XDATA() {
+ PayloadsParserTester parser;
+
+ CPPUNIT_ASSERT(parser.parse(
+ "<query xmlns='jabber:iq:search'>"
+ "<instructions>"
+ "Use the enclosed form to search. If your Jabber client does not"
+ " support Data Forms, visit http://shakespeare.lit/"
+ "</instructions>"
+ "<x xmlns='jabber:x:data' type='form'>"
+ "<title>User Directory Search</title>"
+ "<instructions>"
+ "Please provide the following information"
+ " to search for Shakespearean characters."
+ "</instructions>"
+ "<field type='hidden'"
+ " var='FORM_TYPE'>"
+ "<value>jabber:iq:search</value>"
+ "</field>"
+ "<field type='text-single'"
+ " label='Given Name'"
+ " var='first'/>"
+ "<field type='text-single'"
+ " label='Family Name'"
+ " var='last'/>"
+ "<field type='list-single'"
+ " label='Gender'"
+ " var='x-gender'>"
+ "<option label='Male'><value>male</value></option>"
+ "<option label='Female'><value>female</value></option>"
+ "</field>"
+ "</x>"
+ "</query>"
+ ));
+
+ SearchPayload::ref payload = parser.getPayload<SearchPayload>();
+ CPPUNIT_ASSERT_EQUAL(std::string("Use the enclosed form to search. If your Jabber client does not"
+ " support Data Forms, visit http://shakespeare.lit/"), *payload->getInstructions());
+ CPPUNIT_ASSERT(payload->getForm());
+ CPPUNIT_ASSERT_EQUAL(std::string("Please provide the following information"
+ " to search for Shakespearean characters."), payload->getForm()->getInstructions());
+ }
+
+ void testParse_Results_XDATA() {
+ PayloadsParserTester parser;
+
+ CPPUNIT_ASSERT(parser.parse("<query xmlns='jabber:iq:search'>"
+ " <x xmlns='jabber:x:data' type='result'>"
+ " <field type='hidden' var='FORM_TYPE'>"
+ " <value>jabber:iq:search</value>"
+ " </field>"
+ " <reported>"
+ " <field var='first' label='Given Name' type='text-single'/>"
+ " <field var='last' label='Family Name' type='text-single'/>"
+ " <field var='jid' label='Jabber ID' type='jid-single'/>"
+ " <field var='x-gender' label='Gender' type='list-single'/>"
+ " </reported>"
+ " <item>"
+ " <field var='first'><value>Benvolio</value></field>"
+ " <field var='last'><value>Montague</value></field>"
+ " <field var='jid'><value>benvolio@montague.net</value></field>"
+ " <field var='x-gender'><value>male</value></field>"
+ " </item>"
+ " <item>"
+ " <field var='first'><value>Romeo</value></field>"
+ " <field var='last'><value>Montague</value></field>"
+ " <field var='jid'><value>romeo@montague.net</value></field>"
+ " <field var='x-gender'><value>male</value></field>"
+ " </item>"
+ " </x>"
+ "</query>"));
+ SearchPayload::ref payload = parser.getPayload<SearchPayload>();
+ CPPUNIT_ASSERT(payload);
+
+ Form::ref dataForm = payload->getForm();
+ CPPUNIT_ASSERT(dataForm);
+
+ Form::FormItem reported = dataForm->getReportedFields();
+ CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(4), reported.size());
+
+ std::vector<Form::FormItem> items = dataForm->getItems();
+ CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), items.size());
+
+ Form::FormItem item = items[0];
+ CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(4), item.size());
+
+ CPPUNIT_ASSERT_EQUAL(std::string("Benvolio"), item[0]->getRawValues()[0]);
+ CPPUNIT_ASSERT_EQUAL(std::string("first"), item[0]->getName());
+ CPPUNIT_ASSERT_EQUAL(std::string("Montague"), item[1]->getRawValues()[0]);
+ CPPUNIT_ASSERT_EQUAL(std::string("last"), item[1]->getName());
+ CPPUNIT_ASSERT_EQUAL(std::string("benvolio@montague.net"), item[2]->getRawValues()[0]);
+ CPPUNIT_ASSERT_EQUAL(std::string("jid"), item[2]->getName());
+ CPPUNIT_ASSERT_EQUAL(std::string("male"), item[3]->getRawValues()[0]);
+ CPPUNIT_ASSERT_EQUAL(std::string("x-gender"), item[3]->getName());
+
+ item = items[1];
+ CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(4), item.size());
+
+ CPPUNIT_ASSERT_EQUAL(std::string("Romeo"), item[0]->getRawValues()[0]);
+ CPPUNIT_ASSERT_EQUAL(std::string("first"), item[0]->getName());
+ CPPUNIT_ASSERT_EQUAL(std::string("Montague"), item[1]->getRawValues()[0]);
+ CPPUNIT_ASSERT_EQUAL(std::string("last"), item[1]->getName());
+ CPPUNIT_ASSERT_EQUAL(std::string("romeo@montague.net"), item[2]->getRawValues()[0]);
+ CPPUNIT_ASSERT_EQUAL(std::string("jid"), item[2]->getName());
+ CPPUNIT_ASSERT_EQUAL(std::string("male"), item[3]->getRawValues()[0]);
+ CPPUNIT_ASSERT_EQUAL(std::string("x-gender"), item[3]->getName());
+ }
};
CPPUNIT_TEST_SUITE_REGISTRATION(SearchPayloadParserTest);