summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTim Robbings <tim.robbings@isode.com>2015-01-26 17:33:20 (GMT)
committerKevin Smith <kevin.smith@isode.com>2015-02-17 12:17:35 (GMT)
commit55461d1b5f97591b4ab9510896ca1bc5b5e2a71f (patch)
tree17c79419d06c6740bb6ff04f6e17a13e7c4533a5 /Swiften/Serializer
parent265b779d6766130afa8f2f166733dc172ba22dca (diff)
downloadswift-55461d1b5f97591b4ab9510896ca1bc5b5e2a71f.zip
swift-55461d1b5f97591b4ab9510896ca1bc5b5e2a71f.tar.bz2
Swiften XEP-0141 support
Classes to support XEP-0141 data forms layout. This includes <page/>, <section/>, <reportedref/> and <text/> elements. The form parser and serializer classes have also been updated to handle these new elements, with added CPPUnit tests. Test-information: Tested using updated CPPUnit tests to check the parsing and serializing of new elements. All tests complete successfully. Change-Id: Ibeab22d2834512d06c7f656acd1ef24eea39d650
Diffstat (limited to 'Swiften/Serializer')
-rw-r--r--Swiften/Serializer/PayloadSerializers/FormSerializer.cpp76
-rw-r--r--Swiften/Serializer/PayloadSerializers/FormSerializer.h13
-rw-r--r--Swiften/Serializer/PayloadSerializers/UnitTest/FormSerializerTest.cpp69
3 files changed, 145 insertions, 13 deletions
diff --git a/Swiften/Serializer/PayloadSerializers/FormSerializer.cpp b/Swiften/Serializer/PayloadSerializers/FormSerializer.cpp
index a343989..633ead6 100644
--- a/Swiften/Serializer/PayloadSerializers/FormSerializer.cpp
+++ b/Swiften/Serializer/PayloadSerializers/FormSerializer.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010-2013 Isode Limited.
+ * Copyright (c) 2010-2015 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
@@ -30,11 +30,9 @@ namespace {
}
}
-
namespace Swift {
-FormSerializer::FormSerializer() : GenericPayloadSerializer<Form>() {
-}
+FormSerializer::FormSerializer() : GenericPayloadSerializer<Form>() {}
std::string FormSerializer::serializePayload(boost::shared_ptr<Form> form) const {
if (!form) {
@@ -56,6 +54,9 @@ std::string FormSerializer::serializePayload(boost::shared_ptr<Form> form) cons
if (!form->getInstructions().empty()) {
multiLineify(form->getInstructions(), "instructions", formElement);
}
+ foreach(boost::shared_ptr<FormPage> page, form->getPages()) {
+ formElement->addNode(pageToXML(page));
+ }
foreach(boost::shared_ptr<FormField> field, form->getFields()) {
formElement->addNode(fieldToXML(field, true));
}
@@ -66,6 +67,7 @@ std::string FormSerializer::serializePayload(boost::shared_ptr<Form> form) cons
}
formElement->addNode(reportedElement);
}
+
foreach(Form::FormItem item, form->getItems()) {
boost::shared_ptr<XMLElement> itemElement(new XMLElement("item"));
foreach(FormField::ref field, item) {
@@ -74,9 +76,72 @@ std::string FormSerializer::serializePayload(boost::shared_ptr<Form> form) cons
formElement->addNode(itemElement);
}
+ foreach(const FormText::text text, form->getTextElements()) {
+ formElement->addNode(textToXML(text));
+ }
+
+ foreach (boost::shared_ptr<FormField> field, fields_) {
+ formElement->addNode(fieldToXML(field,true));
+ }
+
return formElement->serialize();
}
+boost::shared_ptr<XMLElement> FormSerializer::textToXML(boost::shared_ptr<FormText> text) const {
+ boost::shared_ptr<XMLElement> textElement (new XMLElement("text"));
+ textElement->addNode(boost::make_shared<XMLTextNode>(text->getTextString()));
+ return textElement;
+}
+
+boost::shared_ptr<XMLElement> FormSerializer::fieldRefToXML(const std::string& ref) const {
+ boost::shared_ptr<XMLElement> fieldRefElement(new XMLElement("fieldref"));
+ fieldRefElement->setAttribute("var", ref);
+ return fieldRefElement;
+}
+
+boost::shared_ptr<XMLElement> FormSerializer::pageToXML(boost::shared_ptr<FormPage> page) const {
+ boost::shared_ptr<XMLElement> pageElement(new XMLElement("page"));
+ pageElement->setAttribute("xmlns", page->getXMLNS());
+ if (!page->getLabel().empty()) {
+ pageElement->setAttribute("label", page->getLabel());
+ }
+ foreach(const FormText::text text, page->getTextElements()) {
+ pageElement->addNode(textToXML(text));
+ }
+ foreach (const boost::shared_ptr<FormField> field, page->getFields()) {
+ pageElement->addNode(fieldRefToXML(field->getName()));
+ fields_.push_back(field);
+ }
+ foreach(const FormReportedRef::ref reportedRef, page->getReportedRefs()) {
+ pageElement->addNode(boost::make_shared<XMLElement>("reportedref"));
+ }
+ foreach(const FormSection::section section, page->getChildSections()) {
+ pageElement->addNode(sectionToXML(section));
+ }
+ return pageElement;
+}
+
+boost::shared_ptr<XMLElement> FormSerializer::sectionToXML(boost::shared_ptr<FormSection> section) const {
+ boost::shared_ptr<XMLElement> sectionElement(new XMLElement("section"));
+ if (!section->getLabel().empty()) {
+ sectionElement->setAttribute("label", section->getLabel());
+ }
+ foreach(const FormText::text text, section->getTextElements()) {
+ sectionElement->addNode(textToXML(text));
+ }
+ foreach(const boost::shared_ptr<FormField> field, section->getFields()) {
+ sectionElement->addNode(fieldRefToXML(field->getName()));
+ fields_.push_back(field);
+ }
+ foreach(const FormReportedRef::ref reportedRef, section->getReportedRefs()) {
+ sectionElement->addNode(boost::make_shared<XMLElement>("reportedref"));
+ }
+ foreach(const FormSection::section childSection, section->getChildSections()) {
+ sectionElement->addNode(sectionToXML(childSection));
+ }
+ return sectionElement;
+}
+
boost::shared_ptr<XMLElement> FormSerializer::fieldToXML(boost::shared_ptr<FormField> field, bool withTypeAttribute) const {
boost::shared_ptr<XMLElement> fieldElement(new XMLElement("field"));
if (!field->getName().empty()) {
@@ -127,10 +192,8 @@ boost::shared_ptr<XMLElement> FormSerializer::fieldToXML(boost::shared_ptr<FormF
boost::shared_ptr<XMLElement> valueElement(new XMLElement("value"));
valueElement->addNode(XMLTextNode::create(option.value));
optionElement->addNode(valueElement);
-
fieldElement->addNode(optionElement);
}
-
return fieldElement;
}
@@ -144,5 +207,4 @@ void FormSerializer::multiLineify(const std::string& text, const std::string& el
element->addNode(lineElement);
}
}
-
}
diff --git a/Swiften/Serializer/PayloadSerializers/FormSerializer.h b/Swiften/Serializer/PayloadSerializers/FormSerializer.h
index 88f2a43..ad3f472 100644
--- a/Swiften/Serializer/PayloadSerializers/FormSerializer.h
+++ b/Swiften/Serializer/PayloadSerializers/FormSerializer.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010-2013 Isode Limited.
+ * Copyright (c) 2010-2015 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
@@ -10,19 +10,24 @@
#include <Swiften/Serializer/GenericPayloadSerializer.h>
#include <Swiften/Elements/Form.h>
#include <Swiften/Elements/FormField.h>
+#include <Swiften/Elements/FormPage.h>
+#include <Swiften/Elements/FormSection.h>
#include <Swiften/Serializer/XML/XMLElement.h>
namespace Swift {
class SWIFTEN_API FormSerializer : public GenericPayloadSerializer<Form> {
public:
FormSerializer();
-
virtual std::string serializePayload(boost::shared_ptr<Form>) const;
private:
+ boost::shared_ptr<XMLElement> textToXML(boost::shared_ptr<FormText> textElement) const;
+ boost::shared_ptr<XMLElement> fieldRefToXML(const std::string& ref) const;
+ boost::shared_ptr<XMLElement> reportedRefToXML(boost::shared_ptr<FormReportedRef> reportedRef) const;
+ boost::shared_ptr<XMLElement> pageToXML(boost::shared_ptr<FormPage> page) const;
+ boost::shared_ptr<XMLElement> sectionToXML(boost::shared_ptr<FormSection> section) const;
boost::shared_ptr<XMLElement> fieldToXML(boost::shared_ptr<FormField> field, bool withTypeAttribute) const;
void multiLineify(const std::string& text, const std::string& elementName, boost::shared_ptr<XMLElement> parent) const;
+ mutable std::vector<boost::shared_ptr<FormField> > fields_;
};
}
-
-
diff --git a/Swiften/Serializer/PayloadSerializers/UnitTest/FormSerializerTest.cpp b/Swiften/Serializer/PayloadSerializers/UnitTest/FormSerializerTest.cpp
index 887edc1..73c9db1 100644
--- a/Swiften/Serializer/PayloadSerializers/UnitTest/FormSerializerTest.cpp
+++ b/Swiften/Serializer/PayloadSerializers/UnitTest/FormSerializerTest.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010-2013 Isode Limited.
+ * Copyright (c) 2010-2015 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
@@ -14,6 +14,7 @@ using namespace Swift;
class FormSerializerTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(FormSerializerTest);
CPPUNIT_TEST(testSerializeFormInformation);
+ CPPUNIT_TEST(testSerializeLayout);
CPPUNIT_TEST(testSerializeFields);
CPPUNIT_TEST(testSerializeFormItems);
CPPUNIT_TEST_SUITE_END();
@@ -33,6 +34,71 @@ class FormSerializerTest : public CppUnit::TestFixture {
"</x>"), testling.serialize(form));
}
+ void testSerializeLayout() {
+ FormSerializer testling;
+ boost::shared_ptr<Form> form(new Form(Form::FormType));
+
+ FormPage::page page = boost::make_shared<FormPage>();
+ page->setLabel("P1");
+ FormReportedRef::ref reportedRef = boost::make_shared<FormReportedRef>();
+ page->addReportedRef(reportedRef);
+ FormText::text formText = boost::make_shared<FormText>();
+ formText->setTextString("P1T1");
+ page->addTextElement(formText);
+ FormField::ref field = boost::make_shared<FormField>(FormField::TextSingleType);
+ field->setName("P1F1");
+ field->setLabel("field one");
+ page->addField(field);
+
+ FormSection::section section = boost::make_shared<FormSection>();
+ section->setLabel("P1S1");
+ formText = boost::make_shared<FormText>();
+ formText->setTextString("P1S1T1");
+ section->addTextElement(formText);
+ field = boost::make_shared<FormField>(FormField::TextSingleType);
+ field->setName("P1S1F1");
+ field->setLabel("field two");
+ section->addField(field);
+ page->addChildSection(section);
+ form->addPage(page);
+
+ page = boost::make_shared<FormPage>();
+ page->setLabel("P2");
+ section = boost::make_shared<FormSection>();
+ section->setLabel("P2S1");
+ FormSection::section subSection = boost::make_shared<FormSection>();
+ subSection->setLabel("P2S2");
+ FormSection::section subSection2 = boost::make_shared<FormSection>();
+ subSection2->setLabel("P2S3");
+ subSection->addChildSection(subSection2);
+ section->addChildSection(subSection);
+ page->addChildSection(section);
+ form->addPage(page);
+
+ // P1 = page one, S1 = section one, F1 = field one, T1 = text one
+ CPPUNIT_ASSERT_EQUAL(std::string(
+ "<x type=\"form\" xmlns=\"jabber:x:data\">"
+ "<page label=\"P1\" xmlns=\"http://jabber.org/protocol/xdata-layout\">"
+ "<text>P1T1</text>"
+ "<fieldref var=\"P1F1\"/>"
+ "<reportedref/>"
+ "<section label=\"P1S1\">"
+ "<text>P1S1T1</text>"
+ "<fieldref var=\"P1S1F1\"/>"
+ "</section>"
+ "</page>"
+ "<page label=\"P2\" xmlns=\"http://jabber.org/protocol/xdata-layout\">"
+ "<section label=\"P2S1\">"
+ "<section label=\"P2S2\">"
+ "<section label=\"P2S3\"/>"
+ "</section>"
+ "</section>"
+ "</page>"
+ "<field label=\"field one\" type=\"text-single\" var=\"P1F1\"/>"
+ "<field label=\"field two\" type=\"text-single\" var=\"P1S1F1\"/>"
+ "</x>"), testling.serialize(form));
+ }
+
void testSerializeFields() {
FormSerializer testling;
boost::shared_ptr<Form> form(new Form(Form::FormType));
@@ -140,7 +206,6 @@ class FormSerializerTest : public CppUnit::TestFixture {
FormSerializer testling;
boost::shared_ptr<Form> form(new Form(Form::ResultType));
-
FormField::ref field = boost::make_shared<FormField>(FormField::HiddenType, "jabber:iq:search");
field->setName("FORM_TYPE");
form->addField(field);