summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2010-08-08 18:41:53 (GMT)
committerRemko Tronçon <git@el-tramo.be>2010-08-08 18:41:53 (GMT)
commit19df82042f44c201e5a2821b4fa35465e33a1c90 (patch)
tree1746bddfa31ce2a6488ef5186036a049a255c9da /Swiften/Parser
parent4a5a0977f661bf5c7c34ee7aa48b35073a682203 (diff)
downloadswift-19df82042f44c201e5a2821b4fa35465e33a1c90.zip
swift-19df82042f44c201e5a2821b4fa35465e33a1c90.tar.bz2
Added XEP-0004 data forms parsing & serializing.
Diffstat (limited to 'Swiften/Parser')
-rw-r--r--Swiften/Parser/PayloadParsers/DiscoInfoParser.h5
-rw-r--r--Swiften/Parser/PayloadParsers/FormParser.cpp132
-rw-r--r--Swiften/Parser/PayloadParsers/FormParser.h106
-rw-r--r--Swiften/Parser/PayloadParsers/FormParserFactory.h29
-rw-r--r--Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp2
-rw-r--r--Swiften/Parser/PayloadParsers/UnitTest/FormParserTest.cpp112
-rw-r--r--Swiften/Parser/SConscript1
7 files changed, 383 insertions, 4 deletions
diff --git a/Swiften/Parser/PayloadParsers/DiscoInfoParser.h b/Swiften/Parser/PayloadParsers/DiscoInfoParser.h
index f1502fc..925d349 100644
--- a/Swiften/Parser/PayloadParsers/DiscoInfoParser.h
+++ b/Swiften/Parser/PayloadParsers/DiscoInfoParser.h
@@ -4,8 +4,7 @@
* See Documentation/Licenses/GPLv3.txt for more information.
*/
-#ifndef SWIFTEN_DiscoInfoParser_H
-#define SWIFTEN_DiscoInfoParser_H
+#pragma once
#include "Swiften/Elements/DiscoInfo.h"
#include "Swiften/Parser/GenericPayloadParser.h"
@@ -27,5 +26,3 @@ namespace Swift {
int level_;
};
}
-
-#endif
diff --git a/Swiften/Parser/PayloadParsers/FormParser.cpp b/Swiften/Parser/PayloadParsers/FormParser.cpp
new file mode 100644
index 0000000..f08e7a3
--- /dev/null
+++ b/Swiften/Parser/PayloadParsers/FormParser.cpp
@@ -0,0 +1,132 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include "Swiften/Parser/PayloadParsers/FormParser.h"
+
+namespace Swift {
+
+FormParser::FormParser() : level_(TopLevel) {
+}
+
+void FormParser::handleStartElement(const String& element, const String&, const AttributeMap& attributes) {
+ if (level_ == TopLevel) {
+ String type = attributes.getAttribute("type");
+ if (type == "form") {
+ getPayloadInternal()->setType(Form::FormType);
+ }
+ else if (type == "submit") {
+ getPayloadInternal()->setType(Form::SubmitType);
+ }
+ else if (type == "cancel") {
+ getPayloadInternal()->setType(Form::CancelType);
+ }
+ else if (type == "result") {
+ getPayloadInternal()->setType(Form::ResultType);
+ }
+ }
+ else if (level_ == PayloadLevel) {
+ if (element == "title") {
+ currentText_.clear();
+ }
+ else if (element == "instructions") {
+ currentText_.clear();
+ }
+ else if (element == "field") {
+ String type = attributes.getAttribute("type");
+ if (type == "boolean") {
+ currentFieldParseHelper_ = BooleanFormFieldParseHelper::create();
+ }
+ else if (type == "fixed") {
+ currentFieldParseHelper_ = FixedFormFieldParseHelper::create();
+ }
+ else if (type == "hidden") {
+ currentFieldParseHelper_ = HiddenFormFieldParseHelper::create();
+ }
+ else if (type == "jid-multi") {
+ currentFieldParseHelper_ = JIDMultiFormFieldParseHelper::create();
+ }
+ else if (type == "jid-single") {
+ currentFieldParseHelper_ = JIDSingleFormFieldParseHelper::create();
+ }
+ else if (type == "list-multi") {
+ currentFieldParseHelper_ = ListMultiFormFieldParseHelper::create();
+ }
+ else if (type == "list-single") {
+ currentFieldParseHelper_ = ListSingleFormFieldParseHelper::create();
+ }
+ else if (type == "text-multi") {
+ currentFieldParseHelper_ = TextMultiFormFieldParseHelper::create();
+ }
+ else if (type == "text-private") {
+ currentFieldParseHelper_ = TextPrivateFormFieldParseHelper::create();
+ }
+ else if (type == "text-single") {
+ currentFieldParseHelper_ = TextSingleFormFieldParseHelper::create();
+ }
+ if (currentFieldParseHelper_) {
+ currentFieldParseHelper_->getField()->setName(attributes.getAttribute("var"));
+ currentFieldParseHelper_->getField()->setLabel(attributes.getAttribute("label"));
+ }
+ }
+ }
+ else if (level_ == FieldLevel && currentFieldParseHelper_) {
+ currentText_.clear();
+ if (element == "option") {
+ currentOptionLabel_ = attributes.getAttribute("label");
+ }
+ }
+ ++level_;
+}
+
+void FormParser::handleEndElement(const String& element, const String&) {
+ --level_;
+ if (level_ == PayloadLevel) {
+ if (element == "title") {
+ String currentTitle = getPayloadInternal()->getTitle();
+ if (currentTitle.isEmpty()) {
+ getPayloadInternal()->setTitle(currentText_);
+ }
+ else {
+ getPayloadInternal()->setTitle(currentTitle + "\n" + currentText_);
+ }
+ }
+ else if (element == "instructions") {
+ String currentInstructions = getPayloadInternal()->getInstructions();
+ if (currentInstructions.isEmpty()) {
+ getPayloadInternal()->setInstructions(currentText_);
+ }
+ else {
+ getPayloadInternal()->setInstructions(currentInstructions + "\n" + currentText_);
+ }
+ }
+ else if (element == "field") {
+ if (currentFieldParseHelper_) {
+ getPayloadInternal()->addField(currentFieldParseHelper_->getField());
+ currentFieldParseHelper_.reset();
+ }
+ }
+ }
+ else if (level_ == FieldLevel && currentFieldParseHelper_) {
+ if (element == "required") {
+ currentFieldParseHelper_->getField()->setRequired(true);
+ }
+ else if (element == "desc") {
+ currentFieldParseHelper_->getField()->setDescription(currentText_);
+ }
+ else if (element == "option") {
+ currentFieldParseHelper_->getField()->addOption(FormField::Option(currentOptionLabel_, currentText_));
+ }
+ else if (element == "value") {
+ currentFieldParseHelper_->addValue(currentText_);
+ }
+ }
+}
+
+void FormParser::handleCharacterData(const String& text) {
+ currentText_ += text;
+}
+
+}
diff --git a/Swiften/Parser/PayloadParsers/FormParser.h b/Swiften/Parser/PayloadParsers/FormParser.h
new file mode 100644
index 0000000..76a54b9
--- /dev/null
+++ b/Swiften/Parser/PayloadParsers/FormParser.h
@@ -0,0 +1,106 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include "Swiften/Elements/Form.h"
+#include "Swiften/Parser/GenericPayloadParser.h"
+
+namespace Swift {
+ class FormParser : public GenericPayloadParser<Form> {
+ public:
+ FormParser();
+
+ virtual void handleStartElement(const String& element, const String&, const AttributeMap& attributes);
+ virtual void handleEndElement(const String& element, const String&);
+ virtual void handleCharacterData(const String& data);
+
+ private:
+ class FieldParseHelper {
+ public:
+ virtual ~FieldParseHelper() {}
+ virtual void addValue(const String&) = 0;
+ virtual boost::shared_ptr<FormField> getField() const {
+ return field;
+ }
+ protected:
+ boost::shared_ptr<FormField> field;
+ };
+ class BoolFieldParseHelper : public FieldParseHelper {
+ virtual void addValue(const String& s) {
+ boost::dynamic_pointer_cast< GenericFormField<bool> >(getField())->setValue(s == "1" || s == "true");
+ }
+ };
+ class StringFieldParseHelper : public FieldParseHelper {
+ virtual void addValue(const String& s) {
+ boost::shared_ptr<GenericFormField<String> > field = boost::dynamic_pointer_cast< GenericFormField<String> >(getField());
+ if (field->getValue().isEmpty()) {
+ field->setValue(s);
+ }
+ else {
+ field->setValue(field->getValue() + "\n" + s);
+ }
+ }
+ };
+ class JIDFieldParseHelper : public FieldParseHelper {
+ virtual void addValue(const String& s) {
+ boost::dynamic_pointer_cast< GenericFormField<JID> >(getField())->setValue(JID(s));
+ }
+ };
+ class StringListFieldParseHelper : public FieldParseHelper {
+ virtual void addValue(const String& s) {
+ // FIXME: Inefficient, but too much hassle to do efficiently
+ boost::shared_ptr<GenericFormField< std::vector<String> > > field = boost::dynamic_pointer_cast< GenericFormField<std::vector<String > > >(getField());
+ std::vector<String> l = field->getValue();
+ l.push_back(s);
+ field->setValue(l);
+ }
+ };
+ class JIDListFieldParseHelper : public FieldParseHelper {
+ virtual void addValue(const String& s) {
+ // FIXME: Inefficient, but too much hassle to do efficiently
+ boost::shared_ptr< GenericFormField< std::vector<JID> > > field = boost::dynamic_pointer_cast< GenericFormField<std::vector<JID > > >(getField());
+ std::vector<JID> l = field->getValue();
+ l.push_back(JID(s));
+ field->setValue(l);
+ }
+ };
+
+#define SWIFTEN_DECLARE_FORM_FIELD_PARSE_HELPER(name, baseParser) \
+ class name##FormFieldParseHelper : public baseParser##FieldParseHelper { \
+ public: \
+ typedef boost::shared_ptr<name##FormFieldParseHelper> ref; \
+ static ref create() { \
+ return ref(new name##FormFieldParseHelper()); \
+ } \
+ private: \
+ name##FormFieldParseHelper() : baseParser##FieldParseHelper() { \
+ field = name##FormField::create(); \
+ } \
+ };
+
+ SWIFTEN_DECLARE_FORM_FIELD_PARSE_HELPER(Boolean, Bool);
+ SWIFTEN_DECLARE_FORM_FIELD_PARSE_HELPER(Fixed, String);
+ SWIFTEN_DECLARE_FORM_FIELD_PARSE_HELPER(Hidden, String);
+ SWIFTEN_DECLARE_FORM_FIELD_PARSE_HELPER(ListSingle, String);
+ SWIFTEN_DECLARE_FORM_FIELD_PARSE_HELPER(TextMulti, String);
+ SWIFTEN_DECLARE_FORM_FIELD_PARSE_HELPER(TextPrivate, String);
+ SWIFTEN_DECLARE_FORM_FIELD_PARSE_HELPER(TextSingle, String);
+ SWIFTEN_DECLARE_FORM_FIELD_PARSE_HELPER(JIDSingle, JID);
+ SWIFTEN_DECLARE_FORM_FIELD_PARSE_HELPER(JIDMulti, JIDList);
+ SWIFTEN_DECLARE_FORM_FIELD_PARSE_HELPER(ListMulti, StringList);
+
+ enum Level {
+ TopLevel = 0,
+ PayloadLevel = 1,
+ FieldLevel = 2
+ };
+ int level_;
+ String currentText_;
+ String currentOptionLabel_;
+ boost::shared_ptr<FieldParseHelper> currentFieldParseHelper_;
+ };
+}
diff --git a/Swiften/Parser/PayloadParsers/FormParserFactory.h b/Swiften/Parser/PayloadParsers/FormParserFactory.h
new file mode 100644
index 0000000..805b0f1
--- /dev/null
+++ b/Swiften/Parser/PayloadParsers/FormParserFactory.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include "Swiften/Parser/PayloadParserFactory.h"
+#include "Swiften/Parser/PayloadParsers/FormParser.h"
+
+namespace Swift {
+ class PayloadParserFactoryCollection;
+
+ class FormParserFactory : public PayloadParserFactory {
+ public:
+ FormParserFactory() {
+ }
+
+ virtual bool canParse(const String& /*element*/, const String& ns, const AttributeMap&) const {
+ return ns == "jabber:x:data";
+ }
+
+ virtual PayloadParser* createPayloadParser() {
+ return new FormParser();
+ }
+
+ };
+}
diff --git a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp
index eace3d1..b115b10 100644
--- a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp
+++ b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp
@@ -24,6 +24,7 @@
#include "Swiften/Parser/PayloadParsers/DiscoItemsParserFactory.h"
#include "Swiften/Parser/PayloadParsers/SecurityLabelParserFactory.h"
#include "Swiften/Parser/PayloadParsers/SecurityLabelsCatalogParserFactory.h"
+#include "Swiften/Parser/PayloadParsers/FormParserFactory.h"
#include "Swiften/Parser/PayloadParsers/VCardUpdateParserFactory.h"
#include "Swiften/Parser/PayloadParsers/VCardParserFactory.h"
#include "Swiften/Parser/PayloadParsers/RawXMLPayloadParserFactory.h"
@@ -51,6 +52,7 @@ FullPayloadParserFactoryCollection::FullPayloadParserFactoryCollection() {
factories_.push_back(shared_ptr<PayloadParserFactory>(new StartSessionParserFactory()));
factories_.push_back(shared_ptr<PayloadParserFactory>(new SecurityLabelParserFactory()));
factories_.push_back(shared_ptr<PayloadParserFactory>(new SecurityLabelsCatalogParserFactory()));
+ factories_.push_back(shared_ptr<PayloadParserFactory>(new FormParserFactory()));
factories_.push_back(shared_ptr<PayloadParserFactory>(new VCardUpdateParserFactory()));
factories_.push_back(shared_ptr<PayloadParserFactory>(new VCardParserFactory()));
factories_.push_back(shared_ptr<PayloadParserFactory>(new PrivateStorageParserFactory(this)));
diff --git a/Swiften/Parser/PayloadParsers/UnitTest/FormParserTest.cpp b/Swiften/Parser/PayloadParsers/UnitTest/FormParserTest.cpp
new file mode 100644
index 0000000..e7d80e3
--- /dev/null
+++ b/Swiften/Parser/PayloadParsers/UnitTest/FormParserTest.cpp
@@ -0,0 +1,112 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+
+#include "Swiften/Parser/PayloadParsers/FormParser.h"
+#include "Swiften/Parser/PayloadParsers/UnitTest/PayloadsParserTester.h"
+
+using namespace Swift;
+
+class FormParserTest : public CppUnit::TestFixture {
+ CPPUNIT_TEST_SUITE(FormParserTest);
+ CPPUNIT_TEST(testParse_FormInformation);
+ CPPUNIT_TEST(testParse);
+ CPPUNIT_TEST_SUITE_END();
+
+ public:
+ void testParse_FormInformation() {
+ PayloadsParserTester parser;
+
+ CPPUNIT_ASSERT(parser.parse(
+ "<x type=\"submit\" xmlns=\"jabber:x:data\">"
+ "<title>Bot Configuration</title>"
+ "<instructions>Hello!</instructions>"
+ "<instructions>Fill out this form to configure your new bot!</instructions>"
+ "</x>"
+ ));
+
+ Form* payload = dynamic_cast<Form*>(parser.getPayload().get());
+ CPPUNIT_ASSERT_EQUAL(String("Bot Configuration"), payload->getTitle());
+ CPPUNIT_ASSERT_EQUAL(String("Hello!\nFill out this form to configure your new bot!"), payload->getInstructions());
+ CPPUNIT_ASSERT_EQUAL(Form::SubmitType, payload->getType());
+ }
+
+ void testParse() {
+ PayloadsParserTester parser;
+
+ CPPUNIT_ASSERT(parser.parse(
+ "<x type=\"form\" xmlns=\"jabber:x:data\">"
+ "<field type=\"hidden\" var=\"FORM_TYPE\">"
+ "<value>jabber:bot</value>"
+ "</field>"
+ "<field type=\"fixed\"><value>Section 1: Bot Info</value></field>"
+ "<field label=\"The name of your bot\" type=\"text-single\" var=\"botname\"/>"
+ "<field label=\"Helpful description of your bot\" type=\"text-multi\" var=\"description\"><value>This is a bot.</value><value>A quite good one actually</value></field>"
+ "<field label=\"Public bot?\" type=\"boolean\" var=\"public\">"
+ "<required/>"
+ "<value>1</value>"
+ "</field>"
+ "<field label=\"Password for special access\" type=\"text-private\" var=\"password\"/>"
+ "<field label=\"What features will the bot support?\" type=\"list-multi\" var=\"features\">"
+ "<value>news</value>"
+ "<value>search</value>"
+ "<option label=\"Contests\"><value>contests</value></option>"
+ "<option label=\"News\"><value>news</value></option>"
+ "<option label=\"Polls\"><value>polls</value></option>"
+ "<option label=\"Reminders\"><value>reminders</value></option>"
+ "<option label=\"Search\"><value>search</value></option>"
+ "</field>"
+ "<field label=\"Maximum number of subscribers\" type=\"list-single\" var=\"maxsubs\">"
+ "<value>20</value>"
+ "<option label=\"10\"><value>10</value></option>"
+ "<option label=\"20\"><value>20</value></option>"
+ "<option label=\"30\"><value>30</value></option>"
+ "<option label=\"50\"><value>50</value></option>"
+ "<option label=\"100\"><value>100</value></option>"
+ "<option label=\"None\"><value>none</value></option>"
+ "</field>"
+ "<field label=\"People to invite\" type=\"jid-multi\" var=\"invitelist\">"
+ "<desc>Tell all your friends about your new bot!</desc>"
+ "<value>foo@bar.com</value>"
+ "<value>baz@fum.org</value>"
+ "</field>"
+ "</x>"));
+
+ Form* payload = dynamic_cast<Form*>(parser.getPayload().get());
+
+ CPPUNIT_ASSERT_EQUAL(9, 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());
+
+ CPPUNIT_ASSERT_EQUAL(String("Section 1: Bot Info"), boost::dynamic_pointer_cast<FixedFormField>(payload->getFields()[1])->getValue());
+
+ CPPUNIT_ASSERT_EQUAL(String("The name of your bot"), payload->getFields()[2]->getLabel());
+
+ CPPUNIT_ASSERT_EQUAL(String("This is a bot.\nA quite good one actually"), boost::dynamic_pointer_cast<TextMultiFormField>(payload->getFields()[3])->getValue());
+
+ CPPUNIT_ASSERT_EQUAL(true, boost::dynamic_pointer_cast<BooleanFormField>(payload->getFields()[4])->getValue());
+ CPPUNIT_ASSERT(payload->getFields()[4]->getRequired());
+
+ CPPUNIT_ASSERT_EQUAL(String("news"), boost::dynamic_pointer_cast<ListMultiFormField>(payload->getFields()[6])->getValue()[0]);
+ CPPUNIT_ASSERT_EQUAL(String("search"), boost::dynamic_pointer_cast<ListMultiFormField>(payload->getFields()[6])->getValue()[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);
+ CPPUNIT_ASSERT_EQUAL(String("News"), payload->getFields()[6]->getOptions()[1].label);
+ CPPUNIT_ASSERT_EQUAL(String("news"), payload->getFields()[6]->getOptions()[1].value);
+
+ CPPUNIT_ASSERT_EQUAL(String("20"), boost::dynamic_pointer_cast<ListSingleFormField>(payload->getFields()[7])->getValue());
+
+ 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_TEST_SUITE_REGISTRATION(FormParserTest);
diff --git a/Swiften/Parser/SConscript b/Swiften/Parser/SConscript
index 7b64ee3..1826432 100644
--- a/Swiften/Parser/SConscript
+++ b/Swiften/Parser/SConscript
@@ -23,6 +23,7 @@ sources = [
"PayloadParsers/DiscoInfoParser.cpp",
"PayloadParsers/DiscoItemsParser.cpp",
"PayloadParsers/ErrorParser.cpp",
+ "PayloadParsers/FormParser.cpp",
"PayloadParsers/FullPayloadParserFactoryCollection.cpp",
"PayloadParsers/PriorityParser.cpp",
"PayloadParsers/PrivateStorageParser.cpp",