diff options
Diffstat (limited to 'Swiften/Serializer')
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); |