From: <svn...@op...> - 2009-09-04 23:56:34
|
Author: dgollub Date: Sat Sep 5 01:56:21 2009 New Revision: 5746 URL: http://www.opensync.org/changeset/5746 Log: Port xmlformat plugin to new Merger API Modified: format-plugins/xmlformat/trunk/src/xmlformat.c format-plugins/xmlformat/trunk/src/xmlformat_merge.c format-plugins/xmlformat/trunk/tests/check_merger.c Modified: format-plugins/xmlformat/trunk/src/xmlformat.c ============================================================================== --- format-plugins/xmlformat/trunk/src/xmlformat.c Sat Sep 5 00:58:28 2009 (r5745) +++ format-plugins/xmlformat/trunk/src/xmlformat.c Sat Sep 5 01:56:21 2009 (r5746) @@ -387,6 +387,26 @@ return initialize("note", error); } +static osync_bool register_merger(OSyncFormatEnv *env, const char *objformat, OSyncError **error) +{ + /* register xmlformat merger */ + OSyncMerger *merger = osync_merger_new(objformat, "xmlformat", error); + if (!merger) + goto error; + + osync_merger_set_merge_func(merger, merge_xmlformat); + osync_merger_set_demerge_func(merger, demerge_xmlformat); + + + osync_format_env_register_merger(env, merger); + osync_merger_unref(merger); + + return TRUE; + +error: + return FALSE; +} + osync_bool get_format_info(OSyncFormatEnv *env) { OSyncError *error = NULL; @@ -413,12 +433,12 @@ osync_objformat_set_marshal_func(format, marshal_xmlformat); osync_objformat_set_demarshal_func(format, demarshal_xmlformat); - osync_objformat_set_merge_func(format, merge_xmlformat); - osync_objformat_set_demerge_func(format, demerge_xmlformat); osync_format_env_register_objformat(env, format); osync_objformat_unref(format); + if (!register_merger(env, "xmlformat-contact", &error)) + goto error; /* register xmlformat-event */ format = osync_objformat_new("xmlformat-event", "event", &error); @@ -441,12 +461,11 @@ osync_objformat_set_marshal_func(format, marshal_xmlformat); osync_objformat_set_demarshal_func(format, demarshal_xmlformat); - osync_objformat_set_merge_func(format, merge_xmlformat); - osync_objformat_set_demerge_func(format, demerge_xmlformat); - osync_format_env_register_objformat(env, format); osync_objformat_unref(format); + if (!register_merger(env, "xmlformat-event", &error)) + goto error; /* register xmlformat-todo */ format = osync_objformat_new("xmlformat-todo", "todo", &error); @@ -469,12 +488,11 @@ osync_objformat_set_marshal_func(format, marshal_xmlformat); osync_objformat_set_demarshal_func(format, demarshal_xmlformat); - osync_objformat_set_merge_func(format, merge_xmlformat); - osync_objformat_set_demerge_func(format, demerge_xmlformat); - osync_format_env_register_objformat(env, format); osync_objformat_unref(format); + if (!register_merger(env, "xmlformat-todo", &error)) + goto error; /* register xmlformat-note */ format = osync_objformat_new("xmlformat-note", "note", &error); @@ -497,12 +515,12 @@ osync_objformat_set_marshal_func(format, marshal_xmlformat); osync_objformat_set_demarshal_func(format, demarshal_xmlformat); - osync_objformat_set_merge_func(format, merge_xmlformat); - osync_objformat_set_demerge_func(format, demerge_xmlformat); - osync_format_env_register_objformat(env, format); osync_objformat_unref(format); + if (!register_merger(env, "xmlformat-note", &error)) + goto error; + return TRUE; error: Modified: format-plugins/xmlformat/trunk/src/xmlformat_merge.c ============================================================================== --- format-plugins/xmlformat/trunk/src/xmlformat_merge.c Sat Sep 5 00:58:28 2009 (r5745) +++ format-plugins/xmlformat/trunk/src/xmlformat_merge.c Sat Sep 5 01:56:21 2009 (r5746) @@ -43,7 +43,9 @@ objtype = osync_xmlformat_get_objtype(xmlformat); - cap_cur = osync_capabilities_get_first(caps, objtype); + OSyncCapabilitiesObjType *capsobjtype = osync_capabilities_get_objtype(caps, objtype); + OSyncList *caplist = osync_capabilities_objtype_get_caps(capsobjtype); + cap_cur = (OSyncCapability *) caplist->data; if(!cap_cur) goto error; @@ -76,12 +78,11 @@ * KEY4 * KEY4 */ - if( osync_capability_has_key(cap_cur) && + if( osync_capability_get_childs(cap_cur) && !strcmp(osync_capability_get_name(cap_cur), osync_xmlfield_get_name(new_cur))) { - OSyncCapability *cap_tmp; OSyncXMLField *new_tmp; - OSyncCapability *cap_node = osync_capability_get_child(cap_cur); + OSyncList *cap_tmp, *childs = osync_capability_get_childs(cap_cur); OSyncXMLField *new_node = osync_xmlfield_get_child(new_cur); OSyncXMLField *old_node = osync_xmlfield_get_child(old_cur); @@ -103,9 +104,9 @@ } while(i == 0); /* search for the curkeyname in the capabilities */ - for(cap_tmp = cap_node; cap_tmp != NULL; cap_tmp = osync_capability_get_next(cap_tmp)) { - if(!strcmp(osync_capability_get_name(cap_tmp), curkeyname)) { - cap_node = cap_tmp; + for(cap_tmp = childs; cap_tmp != NULL; cap_tmp = cap_tmp->next) { + if(!strcmp(osync_capability_get_name((OSyncCapability *) cap_tmp->data), curkeyname)) { + childs = cap_tmp; break; } } @@ -150,7 +151,8 @@ } else if(ret < 0) { - cap_cur = osync_capability_get_next(cap_cur); + caplist = caplist->next; + cap_cur = caplist->data; continue; } else if(ret > 0) @@ -190,7 +192,10 @@ xmlformat = (OSyncXMLFormat *) *buf; - cur_capability = osync_capabilities_get_first(caps, osync_xmlformat_get_objtype(xmlformat)); + + OSyncCapabilitiesObjType *capsobjtype = osync_capabilities_get_objtype(caps, osync_xmlformat_get_objtype(xmlformat)); + OSyncList *caplist = osync_capabilities_objtype_get_caps(capsobjtype); + cur_capability = (OSyncCapability *) caplist->data; cur_xmlfield = osync_xmlformat_get_first_field(xmlformat); if(!cur_capability) /* if there is no capability - it means that the device can handle all xmlfields */ @@ -212,10 +217,10 @@ rc = strcmp(osync_xmlfield_get_name(cur_xmlfield), osync_capability_get_name(cur_capability)); if(rc == 0) { /* check the secound level here */ - if(osync_capability_has_key(cur_capability)) /* if there is no key - it means that the xmlfield can handle all keys */ + if(osync_capability_get_childs(cur_capability)) /* if there is no key - it means that the xmlfield can handle all keys */ { int i, j=0; - int capability_keys = osync_capability_get_key_count(cur_capability); + int capability_keys = osync_list_length(osync_capability_get_childs(cur_capability)); int xmlfield_keys = osync_xmlfield_get_key_count(cur_xmlfield); for(i=0; i < xmlfield_keys; i++) @@ -229,7 +234,7 @@ break; } - krc = strcmp(osync_xmlfield_get_nth_key_name(cur_xmlfield, i), osync_capability_get_nth_key(cur_capability, j)); + krc = strcmp(osync_xmlfield_get_nth_key_name(cur_xmlfield, i), osync_list_nth_data(osync_capability_get_childs(cur_capability), j)); if(krc == 0) { continue; } @@ -249,7 +254,8 @@ continue; } if(rc > 0) { - cur_capability = osync_capability_get_next(cur_capability); + caplist = caplist->next; + cur_capability = caplist->data; continue; } if(rc < 0) { Modified: format-plugins/xmlformat/trunk/tests/check_merger.c ============================================================================== --- format-plugins/xmlformat/trunk/tests/check_merger.c Sat Sep 5 00:58:28 2009 (r5745) +++ format-plugins/xmlformat/trunk/tests/check_merger.c Sat Sep 5 01:56:21 2009 (r5746) @@ -4,6 +4,45 @@ #include <opensync/opensync-serializer.h> #include <opensync/opensync-xmlformat.h> + +static OSyncCapabilities *_merger_assemble_caps(void) +{ + OSyncError *error; + + OSyncCapabilities *capabilities = osync_capabilities_new("xmlformat-caps", &error); + fail_unless(capabilities != NULL, NULL); + + /* Assemble Capabilities */ + OSyncCapabilitiesObjType *capsobjtype = osync_capabilities_objtype_new(capabilities, "contact", &error); + fail_unless(capsobjtype != NULL, NULL); + + OSyncCapability *cap = osync_capability_new(capsobjtype, &error); + osync_capability_set_name(cap, "Name"); + + cap = osync_capability_new(capsobjtype, &error); + osync_capability_set_name(cap, "LastName"); + + cap = osync_capability_new(capsobjtype, &error); + osync_capability_set_name(cap, "Telephone"); + + cap = osync_capability_new(capsobjtype, &error); + osync_capability_set_name(cap, "Categories"); + + cap = osync_capability_new(capsobjtype, &error); + osync_capability_set_name(cap, "Address"); + + cap = osync_capability_new(capsobjtype, &error); + osync_capability_set_name(cap, "Street"); + + cap = osync_capability_new(capsobjtype, &error); + osync_capability_set_name(cap, "City"); + + cap = osync_capability_new(capsobjtype, &error); + osync_capability_set_name(cap, "Region"); + + return capabilities; +} + START_TEST (merger_merge) { char *testbed = setup_testbed("merger"); @@ -13,7 +52,7 @@ unsigned int xmlformat_size = osync_xmlformat_size(); OSyncError *error = NULL; OSyncXMLFormat *xmlformat, *xmlformat_entire; - OSyncCapabilities *capabilities; + OSyncCapabilities *capabilities = _merger_assemble_caps(); fail_unless(osync_file_read("contact.xml", &buffer, &size, &error), NULL); xmlformat = osync_xmlformat_parse(buffer, size, &error); @@ -28,12 +67,14 @@ fail_unless(error == NULL, NULL); g_free(buffer); osync_xmlformat_sort(xmlformat_entire); - + +#if 0 fail_unless(osync_file_read("capabilities.xml", &buffer, &size, &error), NULL); capabilities = osync_capabilities_parse(buffer, size, &error); fail_unless(capabilities != NULL, NULL); fail_unless(error == NULL, NULL); g_free(buffer); +#endif fail_unless(merge_xmlformat((char **) &xmlformat, &xmlformat_size, (char *) xmlformat_entire, osync_xmlformat_size(), capabilities, NULL, &error), NULL); @@ -54,7 +95,7 @@ unsigned int xmlformat_size = osync_xmlformat_size(); OSyncError *error = NULL; OSyncXMLFormat *xmlformat, *xmlformat_entire; - OSyncCapabilities *capabilities; + OSyncCapabilities *capabilities = _merger_assemble_caps(); fail_unless(osync_file_read("contact.xml", &buffer, &size, &error), NULL); xmlformat = osync_xmlformat_parse(buffer, size, &error); @@ -69,13 +110,6 @@ fail_unless(error == NULL, NULL); g_free(buffer); osync_xmlformat_sort(xmlformat_entire); - - fail_unless(osync_file_read("capabilities.xml", &buffer, &size, &error), NULL); - capabilities = osync_capabilities_parse(buffer, size, &error); - fail_unless(capabilities != NULL, NULL); - fail_unless(error == NULL, NULL); -//printf("\n%s", buffer); - g_free(buffer); //osync_xmlformat_assemble(xmlformat, &buffer, &size); printf("\n%s", buffer); g_free(buffer); //osync_xmlformat_assemble(xmlformat_entire, &buffer, &size); printf("\n%s", buffer); g_free(buffer); |