Commit f62426cd authored by Alexandre Savard's avatar Alexandre Savard
Browse files

[#3963] Full Instant Messaging xml parsing, from xml to UriList

parent d816f6e8
......@@ -15,37 +15,36 @@ static inline char* duplicateString (char dst[], const char src[], size_t len)
static void XMLCALL startElementCallback (void *userData, const char *name, const char **atts)
{
std::cout << "startElement " << name << std::endl;
int *depthPtr = (int *) userData;
char attribute[50];
char value[50];
char attribute[100];
char value[100];
const char **att;
const char **val;
for (att = atts; *att; att += 2) {
if (strcmp (name, "entry") == 0) {
const char **val = att+1;
sfl::InstantMessaging::UriList *list = static_cast<sfl::InstantMessaging::UriList *> (userData);
sfl::InstantMessaging::UriEntry *entry = new sfl::InstantMessaging::UriEntry();
duplicateString (attribute, *att, strlen (*att));
std::cout << "att: " << attribute << std::endl;
for (att = atts; *att; att += 2) {
duplicateString (value, *val, strlen (*val));
std::cout << "val: " << value << std::endl;
}
const char **val = att+1;
duplicateString (attribute, *att, strlen (*att));
duplicateString (value, *val, strlen (*val));
entry->insert (std::pair<std::string, std::string> (attribute, value));
}
*depthPtr += 1;
list->push_back (entry);
}
}
static void XMLCALL endElementCallback (void *userData, const char *name)
{
// std::cout << "endElement " << name << std::endl;
int *depthPtr = (int *) userData;
*depthPtr -= 1;
}
......@@ -237,8 +236,8 @@ std::string InstantMessaging::generateXmlUriList (UriList& list)
while (iterEntry != list.end()) {
xmlbuffer.append ("<entry uri=");
UriEntry entry = static_cast<UriEntry> (*iterEntry);
iterAttr = entry.find (sfl::IM_XML_URI);
UriEntry *entry = static_cast<UriEntry *> (*iterEntry);
iterAttr = entry->find (sfl::IM_XML_URI);
xmlbuffer.append (iterAttr->second);
xmlbuffer.append (" cp:copyControl=\"to\" />");
......@@ -258,7 +257,7 @@ InstantMessaging::UriList InstantMessaging::parseXmlUriList (std::string& urilis
XML_Parser parser = XML_ParserCreate (NULL);
int depth = 0;
XML_SetUserData (parser, &depth);
XML_SetUserData (parser, &list);
XML_SetElementHandler (parser, startElementCallback, endElementCallback);
if (XML_Parse (parser, urilist.c_str(), urilist.size(), 1) == XML_STATUS_ERROR) {
......
......@@ -36,7 +36,7 @@ class InstantMessaging
public:
typedef std::map <std::string, std::string> UriEntry;
typedef std::list <UriEntry> UriList;
typedef std::list <UriEntry *> UriList;
/*
* Class constructor
......
......@@ -195,6 +195,7 @@ void InstantMessagingTest::testGenerateXmlUriList ()
std::cout << std::endl;
// Create a test list with two entries
sfl::InstantMessaging::UriList list;
sfl::InstantMessaging::UriEntry entry1;
......@@ -203,14 +204,15 @@ void InstantMessagingTest::testGenerateXmlUriList ()
sfl::InstantMessaging::UriEntry entry2;
entry2[sfl::IM_XML_URI] = "\"sip:manu@example.com\"";
list.push_front(entry1);
list.push_front(entry2);
list.push_front(&entry1);
list.push_front(&entry2);
std::string buffer = _im->generateXmlUriList(list);
CPPUNIT_ASSERT(buffer.size() != 0);
std::cout << buffer << std::endl;
// parse the resuling xml (further tests are performed in callbacks)
XML_Parser parser = XML_ParserCreate(NULL);
int nbEntry = 0;
XML_SetUserData(parser, &nbEntry);
......@@ -229,8 +231,38 @@ void InstantMessagingTest::testGenerateXmlUriList ()
void InstantMessagingTest::testXmlUriListParsing ()
{
std::string xmlbuffer = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
xmlbuffer.append ("<resource-lists xmlns=\"urn:ietf:params:xml:ns:resource-lists\" xmlns:cp=\"urn:ietf:params:xml:ns:copycontrol\">");
xmlbuffer.append ("<list>");
xmlbuffer.append ("<entry uri=\"sip:alex@example.com\" cp:copyControl=\"to\" />");
xmlbuffer.append ("<entry uri=\"sip:manu@example.com\" cp:copyControl=\"to\" />");
xmlbuffer.append ("</list>");
xmlbuffer.append ("</resource-lists>");
sfl::InstantMessaging::UriList list = _im->parseXmlUriList(xmlbuffer);
CPPUNIT_ASSERT(list.size() == 2);
// An iterator over xml attribute
sfl::InstantMessaging::UriEntry::iterator iterAttr;
// An iterator over list entries
sfl::InstantMessaging::UriList::iterator iterEntry = list.begin();
while (iterEntry != list.end()) {
sfl::InstantMessaging::UriEntry *entry = static_cast<sfl::InstantMessaging::UriEntry *> (*iterEntry);
iterAttr = entry->find (sfl::IM_XML_URI);
/*
if((iterAttr->second == std::string("sip:alex@example.com")) ||
(iterAttr->second == std::string("sip:manu@example.com")))
CPPUNIT_ASSERT(1==1);
else
CPPUNIT_ASSERT(0==1);
*/
iterEntry++;
}
}
void InstantMessagingTest::tearDown()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment