summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2010-09-06 09:19:02 (GMT)
committerRemko Tronçon <git@el-tramo.be>2010-09-06 11:06:49 (GMT)
commit1e2c3461458f4274d539daf51507ce81d845cce0 (patch)
tree9507863b011691f22212d5cc76d484dff1343d36 /Swiften/Parser/PayloadParsers
parente8dbe2e2b78cd5e4a66e2e580e12d05b2e69b120 (diff)
downloadswift-1e2c3461458f4274d539daf51507ce81d845cce0.zip
swift-1e2c3461458f4274d539daf51507ce81d845cce0.tar.bz2
Partial VCard support without losing unknown data.
Diffstat (limited to 'Swiften/Parser/PayloadParsers')
-rw-r--r--Swiften/Parser/PayloadParsers/RosterParser.cpp1
-rw-r--r--Swiften/Parser/PayloadParsers/UnitTest/VCardParserTest.cpp59
-rw-r--r--Swiften/Parser/PayloadParsers/VCardParser.cpp87
-rw-r--r--Swiften/Parser/PayloadParsers/VCardParser.h2
4 files changed, 134 insertions, 15 deletions
diff --git a/Swiften/Parser/PayloadParsers/RosterParser.cpp b/Swiften/Parser/PayloadParsers/RosterParser.cpp
index c3a35b6..6db2b30 100644
--- a/Swiften/Parser/PayloadParsers/RosterParser.cpp
+++ b/Swiften/Parser/PayloadParsers/RosterParser.cpp
@@ -71,6 +71,7 @@ void RosterParser::handleEndElement(const String& element, const String& ns) {
if (unknownContentParser_) {
unknownContentParser_->handleEndElement(element, ns);
currentItem_.addUnknownContent(unknownContentParser_->getResult());
+ delete unknownContentParser_;
unknownContentParser_ = NULL;
}
else if (element == "group") {
diff --git a/Swiften/Parser/PayloadParsers/UnitTest/VCardParserTest.cpp b/Swiften/Parser/PayloadParsers/UnitTest/VCardParserTest.cpp
index 999eb34..dfcc0ec 100644
--- a/Swiften/Parser/PayloadParsers/UnitTest/VCardParserTest.cpp
+++ b/Swiften/Parser/PayloadParsers/UnitTest/VCardParserTest.cpp
@@ -12,15 +12,68 @@
using namespace Swift;
-class VCardParserTest : public CppUnit::TestFixture
-{
+class VCardParserTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(VCardParserTest);
+ CPPUNIT_TEST(testParse);
CPPUNIT_TEST(testParse_Photo);
CPPUNIT_TEST(testParse_Nickname);
CPPUNIT_TEST_SUITE_END();
public:
- VCardParserTest() {}
+ void testParse() {
+ PayloadsParserTester parser;
+
+ CPPUNIT_ASSERT(parser.parse(
+ "<vCard xmlns=\"vcard-temp\">"
+ "<VERSION>2.0</VERSION>"
+ "<FN>Alice In Wonderland</FN>"
+ "<N>"
+ "<FAMILY>Wonderland</FAMILY>"
+ "<GIVEN>Alice</GIVEN>"
+ "<MIDDLE>In</MIDDLE>"
+ "<PREFIX>Mrs</PREFIX>"
+ "<SUFFIX>PhD</SUFFIX>"
+ "</N>"
+ "<EMAIL>"
+ "<USERID>alice@wonderland.lit</USERID>"
+ "<HOME/>"
+ "<INTERNET/>"
+ "<PREF/>"
+ "</EMAIL>"
+ "<EMAIL>"
+ "<USERID>alice@teaparty.lit</USERID>"
+ "<WORK/>"
+ "<X400/>"
+ "</EMAIL>"
+ "<NICKNAME>DreamGirl</NICKNAME>"
+ "<BDAY>1234</BDAY>"
+ "<MAILER>mutt</MAILER>"
+ "</vCard>"));
+
+ boost::shared_ptr<VCard> payload = boost::dynamic_pointer_cast<VCard>(parser.getPayload());
+ CPPUNIT_ASSERT_EQUAL(String("2.0"), payload->getVersion());
+ CPPUNIT_ASSERT_EQUAL(String("Alice In Wonderland"), payload->getFullName());
+ CPPUNIT_ASSERT_EQUAL(String("Alice"), payload->getGivenName());
+ CPPUNIT_ASSERT_EQUAL(String("In"), payload->getMiddleName());
+ CPPUNIT_ASSERT_EQUAL(String("Wonderland"), payload->getFamilyName());
+ CPPUNIT_ASSERT_EQUAL(String("Mrs"), payload->getPrefix());
+ CPPUNIT_ASSERT_EQUAL(String("PhD"), payload->getSuffix());
+ CPPUNIT_ASSERT_EQUAL(String("DreamGirl"), payload->getNickname());
+ CPPUNIT_ASSERT_EQUAL(String("<BDAY xmlns=\"vcard-temp\">1234</BDAY><MAILER xmlns=\"vcard-temp\">mutt</MAILER>"), payload->getUnknownContent());
+ CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(payload->getEMailAddresses().size()));
+ CPPUNIT_ASSERT_EQUAL(String("alice@wonderland.lit"), payload->getEMailAddresses()[0].address);
+ CPPUNIT_ASSERT(payload->getEMailAddresses()[0].isHome);
+ CPPUNIT_ASSERT(payload->getEMailAddresses()[0].isInternet);
+ CPPUNIT_ASSERT(payload->getEMailAddresses()[0].isPreferred);
+ CPPUNIT_ASSERT(!payload->getEMailAddresses()[0].isWork);
+ CPPUNIT_ASSERT(!payload->getEMailAddresses()[0].isX400);
+ CPPUNIT_ASSERT_EQUAL(String("alice@teaparty.lit"), payload->getEMailAddresses()[1].address);
+ CPPUNIT_ASSERT(!payload->getEMailAddresses()[1].isHome);
+ CPPUNIT_ASSERT(!payload->getEMailAddresses()[1].isInternet);
+ CPPUNIT_ASSERT(!payload->getEMailAddresses()[1].isPreferred);
+ CPPUNIT_ASSERT(payload->getEMailAddresses()[1].isWork);
+ CPPUNIT_ASSERT(payload->getEMailAddresses()[1].isX400);
+ }
void testParse_Photo() {
PayloadsParserTester parser;
diff --git a/Swiften/Parser/PayloadParsers/VCardParser.cpp b/Swiften/Parser/PayloadParsers/VCardParser.cpp
index 338efc6..2f1f8dc 100644
--- a/Swiften/Parser/PayloadParsers/VCardParser.cpp
+++ b/Swiften/Parser/PayloadParsers/VCardParser.cpp
@@ -7,27 +7,39 @@
#include "Swiften/Parser/PayloadParsers/VCardParser.h"
#include "Swiften/Base/foreach.h"
#include "Swiften/StringCodecs/Base64.h"
+#include "Swiften/Parser/SerializingParser.h"
namespace Swift {
-VCardParser::VCardParser() {
+VCardParser::VCardParser() : unknownContentParser_(NULL) {
}
-void VCardParser::handleStartElement(const String& element, const String&, const AttributeMap&) {
+void VCardParser::handleStartElement(const String& element, const String& ns, const AttributeMap& attributes) {
elementStack_.push_back(element);
+ String elementHierarchy = getElementHierarchy();
+ if (elementHierarchy == "/vCard/EMAIL") {
+ currentEMailAddress_ = VCard::EMailAddress();
+ }
+ if (elementStack_.size() == 2) {
+ assert(!unknownContentParser_);
+ unknownContentParser_ = new SerializingParser();
+ unknownContentParser_->handleStartElement(element, ns, attributes);
+ }
+ else if (unknownContentParser_) {
+ unknownContentParser_->handleStartElement(element, ns, attributes);
+ }
+
currentText_ = "";
}
-void VCardParser::handleEndElement(const String&, const String&) {
- String elementHierarchy = getElementHierarchy();
- if (elementHierarchy == "/vCard/PHOTO/TYPE") {
- getPayloadInternal()->setPhotoType(currentText_);
- }
- else if (elementHierarchy == "/vCard/PHOTO/BINVAL") {
- getPayloadInternal()->setPhoto(Base64::decode(currentText_));
+void VCardParser::handleEndElement(const String& element, const String& ns) {
+ if (unknownContentParser_) {
+ unknownContentParser_->handleEndElement(element, ns);
}
- else if (elementHierarchy == "/vCard/NICKNAME") {
- getPayloadInternal()->setNickname(currentText_);
+
+ String elementHierarchy = getElementHierarchy();
+ if (elementHierarchy == "/vCard/VERSION") {
+ getPayloadInternal()->setVersion(currentText_);
}
else if (elementHierarchy == "/vCard/FN") {
getPayloadInternal()->setFullName(currentText_);
@@ -38,13 +50,64 @@ void VCardParser::handleEndElement(const String&, const String&) {
else if (elementHierarchy == "/vCard/N/GIVEN") {
getPayloadInternal()->setGivenName(currentText_);
}
+ else if (elementHierarchy == "/vCard/N/MIDDLE") {
+ getPayloadInternal()->setMiddleName(currentText_);
+ }
+ else if (elementHierarchy == "/vCard/N/PREFIX") {
+ getPayloadInternal()->setPrefix(currentText_);
+ }
+ else if (elementHierarchy == "/vCard/N/SUFFIX") {
+ getPayloadInternal()->setSuffix(currentText_);
+ }
+ else if (elementHierarchy == "/vCard/N") {
+ }
+ else if (elementHierarchy == "/vCard/NICKNAME") {
+ getPayloadInternal()->setNickname(currentText_);
+ }
+ else if (elementHierarchy == "/vCard/PHOTO/TYPE") {
+ getPayloadInternal()->setPhotoType(currentText_);
+ }
+ else if (elementHierarchy == "/vCard/PHOTO/BINVAL") {
+ getPayloadInternal()->setPhoto(Base64::decode(currentText_));
+ }
+ else if (elementHierarchy == "/vCard/PHOTO") {
+ }
else if (elementHierarchy == "/vCard/EMAIL/USERID") {
- getPayloadInternal()->setEMail(currentText_);
+ currentEMailAddress_.address = currentText_;
+ }
+ else if (elementHierarchy == "/vCard/EMAIL/HOME") {
+ currentEMailAddress_.isHome = true;
+ }
+ else if (elementHierarchy == "/vCard/EMAIL/WORK") {
+ currentEMailAddress_.isWork = true;
+ }
+ else if (elementHierarchy == "/vCard/EMAIL/INTERNET") {
+ currentEMailAddress_.isInternet = true;
+ }
+ else if (elementHierarchy == "/vCard/EMAIL/X400") {
+ currentEMailAddress_.isX400 = true;
+ }
+ else if (elementHierarchy == "/vCard/EMAIL/PREF") {
+ currentEMailAddress_.isPreferred = true;
+ }
+ else if (elementHierarchy == "/vCard/EMAIL") {
+ getPayloadInternal()->addEMailAddress(currentEMailAddress_);
+ }
+ else if (elementStack_.size() == 2 && unknownContentParser_) {
+ getPayloadInternal()->addUnknownContent(unknownContentParser_->getResult());
+ }
+
+ if (elementStack_.size() == 2 && unknownContentParser_) {
+ delete unknownContentParser_;
+ unknownContentParser_ = NULL;
}
elementStack_.pop_back();
}
void VCardParser::handleCharacterData(const String& text) {
+ if (unknownContentParser_) {
+ unknownContentParser_->handleCharacterData(text);
+ }
currentText_ += text;
}
diff --git a/Swiften/Parser/PayloadParsers/VCardParser.h b/Swiften/Parser/PayloadParsers/VCardParser.h
index d22f8f5..f912ff1 100644
--- a/Swiften/Parser/PayloadParsers/VCardParser.h
+++ b/Swiften/Parser/PayloadParsers/VCardParser.h
@@ -25,6 +25,8 @@ namespace Swift {
private:
std::vector<String> elementStack_;
+ VCard::EMailAddress currentEMailAddress_;
+ SerializingParser* unknownContentParser_;
String currentText_;
};
}