From 1052d9fe49095e9a739122adfc402ac4d851d56f Mon Sep 17 00:00:00 2001 From: Benjamin Reis Date: Tue, 14 Feb 2017 11:57:41 +0100 Subject: [PATCH] Use relative Xpath to parse only XMLLine with presence --- coreapi/friendlist.c | 8 +++----- coreapi/private.h | 1 + coreapi/xml.c | 7 ++++++- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/coreapi/friendlist.c b/coreapi/friendlist.c index 0bbcd9cf1..ac7076906 100644 --- a/coreapi/friendlist.c +++ b/coreapi/friendlist.c @@ -202,7 +202,6 @@ static void linphone_friend_list_parse_multipart_related_body(LinphoneFriendList xmlSetGenericErrorFunc(xml_ctx, linphone_xmlparsing_genericxml_error); xml_ctx->doc = xmlReadDoc((const unsigned char*)first_part_body, 0, NULL, 0); if (xml_ctx->doc != NULL) { - char xpath_str[MAX_XPATH_LENGTH]; LinphoneFriend *lf; LinphoneContent *presence_part; xmlXPathObjectPtr resource_object; @@ -253,8 +252,8 @@ static void linphone_friend_list_parse_multipart_related_body(LinphoneFriendList if ((resource_object != NULL) && (resource_object->nodesetval != NULL)) { for (i = 1; i <= resource_object->nodesetval->nodeNr; i++) { const char *cid = NULL; - snprintf(xpath_str, sizeof(xpath_str),"/rlmi:list/rlmi:resource[%i]/rlmi:instance/@cid", i); - cid = linphone_get_xml_text_content(xml_ctx, xpath_str); + linphone_xml_xpath_context_set_node(xml_ctx, xmlXPathNodeSetItem(resource_object->nodesetval, i-1)); + cid = linphone_get_xml_text_content(xml_ctx, "./rlmi:instance/@cid"); if (cid != NULL) { presence_part = linphone_content_find_part_by_header(body, "Content-Id", cid); if (presence_part == NULL) { @@ -265,8 +264,7 @@ static void linphone_friend_list_parse_multipart_related_body(LinphoneFriendList if (presence != NULL) { // Try to reduce CPU cost of linphone_address_new and find_friend_by_address by only doing it when we know for sure we have a presence to notify LinphoneAddress* addr; - snprintf(xpath_str, sizeof(xpath_str), "/rlmi:list/rlmi:resource[%i]/@uri", i); - uri = linphone_get_xml_text_content(xml_ctx, xpath_str); + uri = linphone_get_xml_text_content(xml_ctx, "./@uri"); if (uri == NULL) continue; addr = linphone_address_new(uri); if (!addr) continue; diff --git a/coreapi/private.h b/coreapi/private.h index 66407ad0f..c94a1f984 100644 --- a/coreapi/private.h +++ b/coreapi/private.h @@ -1510,6 +1510,7 @@ xmlparsing_context_t * linphone_xmlparsing_context_new(void); void linphone_xmlparsing_context_destroy(xmlparsing_context_t *ctx); void linphone_xmlparsing_genericxml_error(void *ctx, const char *fmt, ...); int linphone_create_xml_xpath_context(xmlparsing_context_t *xml_ctx); +void linphone_xml_xpath_context_set_node(xmlparsing_context_t *xml_ctx, xmlNodePtr node); char * linphone_get_xml_text_content(xmlparsing_context_t *xml_ctx, const char *xpath_expression); const char * linphone_get_xml_attribute_text_content(xmlparsing_context_t *xml_ctx, const char *xpath_expression, const char *attribute_name); void linphone_free_xml_text_content(const char *text); diff --git a/coreapi/xml.c b/coreapi/xml.c index db45239b9..4e2e154a4 100644 --- a/coreapi/xml.c +++ b/coreapi/xml.c @@ -67,6 +67,11 @@ int linphone_create_xml_xpath_context(xmlparsing_context_t *xml_ctx) { return 0; } +void linphone_xml_xpath_context_set_node(xmlparsing_context_t *xml_ctx, xmlNodePtr node) { + //xmlXPathSetContextNode(node, xml_ctx->xpath_ctx); + xml_ctx->xpath_ctx->node = node; +} + char * linphone_get_xml_text_content(xmlparsing_context_t *xml_ctx, const char *xpath_expression) { xmlXPathObjectPtr xpath_obj; xmlChar *text = NULL; @@ -130,4 +135,4 @@ void linphone_xml_xpath_context_init_carddav_ns(xmlparsing_context_t *xml_ctx) { xmlXPathRegisterNs(xml_ctx->xpath_ctx, (const xmlChar*)"card", (const xmlChar*)"urn:ietf:params:xml:ns:carddav"); xmlXPathRegisterNs(xml_ctx->xpath_ctx, (const xmlChar*)"x1", (const xmlChar*)"http://calendarserver.org/ns/"); } -} \ No newline at end of file +}