From: <svn...@op...> - 2009-04-12 18:58:02
|
Author: scriptor Date: Sun Apr 12 20:57:57 2009 New Revision: 5588 URL: http://www.opensync.org/changeset/5588 Log: Fixed check_do_convert_from_to.c. Reenabled those tests that read in an xmlformat-contact etc. Modified: plugins/ldap-sync/tests/CMakeLists.txt plugins/ldap-sync/tests/check_do_convert_from_to.c Modified: plugins/ldap-sync/tests/CMakeLists.txt ============================================================================== --- plugins/ldap-sync/tests/CMakeLists.txt Sun Apr 12 20:57:46 2009 (r5587) +++ plugins/ldap-sync/tests/CMakeLists.txt Sun Apr 12 20:57:57 2009 (r5588) @@ -333,19 +333,19 @@ -#ADD_TEST( convert_xmlformat_contacts1 ${TESTDIR}/check_convert_from_to ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR} "${CMAKE_SOURCE_DIR}/tests/xmlformats/contact*.xml" "--to-ldap-evolutionperson" ) +ADD_TEST( convert_xmlformat_contacts1 ${TESTDIR}/check_convert_from_to ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR} "${CMAKE_SOURCE_DIR}/tests/xmlformats/contact*.xml" "--to-ldap-evolutionperson" ) -#ADD_TEST( convert_xmlformat_contacts2 ${TESTDIR}/check_convert_from_to ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR} "${CMAKE_SOURCE_DIR}/tests/xmlformats/contact*.xml" "--to-ldap-inetorgperson" ) +ADD_TEST( convert_xmlformat_contacts2 ${TESTDIR}/check_convert_from_to ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR} "${CMAKE_SOURCE_DIR}/tests/xmlformats/contact*.xml" "--to-ldap-inetorgperson" ) -#ADD_TEST( convert_xmlformat_contacts3 ${TESTDIR}/check_convert_from_to ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR} "${CMAKE_SOURCE_DIR}/tests/contact*.xml" "--to-ldap-inetorgperson" ) +ADD_TEST( convert_xmlformat_contacts3 ${TESTDIR}/check_convert_from_to ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR} "${CMAKE_SOURCE_DIR}/tests/contact*.xml" "--to-ldap-inetorgperson" ) -#ADD_TEST( convert_xmlformat_event1 ${TESTDIR}/check_convert_from_to ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR} "${CMAKE_SOURCE_DIR}/tests/xmlformats/event*.xml" "--to-ldap-event" ) +ADD_TEST( convert_xmlformat_event1 ${TESTDIR}/check_convert_from_to ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR} "${CMAKE_SOURCE_DIR}/tests/xmlformats/event*.xml" "--to-ldap-event" ) -#ADD_TEST( convert_xmlformat_event2 ${TESTDIR}/check_convert_from_to ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR} "${CMAKE_SOURCE_DIR}/tests/event*.xml" "--to-ldap-event" ) +ADD_TEST( convert_xmlformat_event2 ${TESTDIR}/check_convert_from_to ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR} "${CMAKE_SOURCE_DIR}/tests/event*.xml" "--to-ldap-event" ) -#ADD_TEST( convert_xmlformat_todo1 ${TESTDIR}/check_convert_from_to ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR} "${CMAKE_SOURCE_DIR}/tests/todo*.xml" "--to-ldap-todo" ) +ADD_TEST( convert_xmlformat_todo1 ${TESTDIR}/check_convert_from_to ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR} "${CMAKE_SOURCE_DIR}/tests/todo*.xml" "--to-ldap-todo" ) -#ADD_TEST( convert_xmlformat_note1 ${TESTDIR}/check_convert_from_to ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR} "${CMAKE_SOURCE_DIR}/tests/note*.xml" "--to-ldap-note" ) +ADD_TEST( convert_xmlformat_note1 ${TESTDIR}/check_convert_from_to ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR} "${CMAKE_SOURCE_DIR}/tests/note*.xml" "--to-ldap-note" ) Modified: plugins/ldap-sync/tests/check_do_convert_from_to.c ============================================================================== --- plugins/ldap-sync/tests/check_do_convert_from_to.c Sun Apr 12 20:57:46 2009 (r5587) +++ plugins/ldap-sync/tests/check_do_convert_from_to.c Sun Apr 12 20:57:57 2009 (r5588) @@ -1,10 +1,13 @@ /* - * $Id$ + * $Id: CMakeLists.txt 5382 2009-03-28 18:09:31Z scriptor $ * * ldap-sync - A plugin for the opensync framework * * Copyright (C) 2009 Juergen Leising <jle...@us...> - * + * + * This utility was inspired by vformat/tools/vconvert.c. + * Many thanks, indeed, to the authors of the vformat plugin. + * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either @@ -29,6 +32,7 @@ #include <opensync/opensync-format.h> #include <opensync/opensync-helper.h> #include <opensync/opensync-plugin.h> +#include <opensync/opensync-xmlformat.h> #include <stdio.h> #include <string.h> #include <sys/types.h> @@ -61,10 +65,14 @@ -OSyncObjFormat *conv_run_detection(OSyncFormatEnv *env, const char *sfname, conv_detection type, OSyncError **error); +OSyncObjFormat *ldap_format_get_targetformat(const OSyncFormatEnv *env, const char *sfname, const conv_detection type, OSyncError **error); int usage(char *name); +osync_bool ldap_format_prepare_xmlformat_data(const char *buffer, const unsigned int size, const char *objtype, const char *new_sourceformat_name, OSyncData **data, OSyncObjFormat **sourceformat, OSyncError **error); + + +osync_bool ldap_format_perform_conversion(const OSyncFormatEnv *format_env_orig, OSyncData **data, OSyncObjFormat *sourceformat, const conv_detection detection_type, const char *format_config, OSyncError **error); @@ -78,13 +86,20 @@ -OSyncObjFormat *conv_run_detection(OSyncFormatEnv *env, const char *sfname, conv_detection type, OSyncError **error) + + + +OSyncObjFormat *ldap_format_get_targetformat(const OSyncFormatEnv *env_orig, const char *sfname, const conv_detection type, OSyncError **error) { OSyncObjFormat *targetformat = NULL; + OSyncFormatEnv *env = (OSyncFormatEnv *)env_orig; + + osync_trace(TRACE_ENTRY, "%s(%p, %p, %i, %p)", __func__, env_orig, sfname, type, error); - if (env == NULL) { - osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: env = NULL. ", __FILE__, __LINE__); + + if (env == NULL || env_orig == NULL) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: env_orig = NULL. ", __FILE__, __LINE__); goto error; } @@ -95,7 +110,7 @@ - fprintf(stderr, "Type of the source format: \"%s\".\n", sfname); + osync_trace(TRACE_INTERNAL, "%s:%i: ERROR: Type of the source format: \"%s\".\n", __FILE__, __LINE__, sfname); @@ -382,11 +397,12 @@ default: osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: Unable to convert xmlformat-note-doc into this format. Supported formats: vnote11, ldap-note, xmlformat-note.", __FILE__, __LINE__); goto error; - } - + } - } else if (!strcmp(sfname, "xmlformat-journal-doc")) { + } else + // Does not exist, any more, does it? + if (!strcmp(sfname, "xmlformat-journal-doc")) { switch (type) { case TARGET_AUTO: case TARGET_VJOURNAL: @@ -535,24 +551,35 @@ } - fprintf(stderr, "Type of the target format: \"%s\".\n", (char *)osync_objformat_get_name(targetformat)); + osync_trace(TRACE_INTERNAL, "%s:%i: Type of the target format: \"%s\".\n", __FILE__, __LINE__, (char *)osync_objformat_get_name(targetformat)); + osync_trace(TRACE_EXIT, "%s", __func__); return targetformat; error: - if (osync_error_is_set(error)) { - fprintf(stderr, "%s:%i: ERROR: %s\n", __FILE__, __LINE__, osync_error_print(error)); - } else { - fprintf(stderr, "%s:%i: ERROR: Unknown reason.\n", __FILE__, __LINE__); + if (!osync_error_is_set(error)) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: Unknown reason.\n", __FILE__, __LINE__); } + + osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); return NULL; } + + + + + + + int usage(char *name) { + osync_trace(TRACE_ENTRY, "%s(%p)", __func__, name); + + if (name == NULL) { name = (char *)__FILE__; } @@ -575,20 +602,269 @@ fprintf(stderr, "--to-ldap-note\tConvert to LDAP note\n"); fprintf(stderr, "--to-xmlformat\tConvert to xmlformat\n"); + + osync_trace(TRACE_EXIT, "%s", __func__); return 1; } + + + +osync_bool ldap_format_prepare_xmlformat_data(const char *buffer, const unsigned int size, const char *objtype, const char *new_sourceformat_name, OSyncData **data, OSyncObjFormat **sourceformat, OSyncError **error) +{ + OSyncXMLFormat *xmlformat = NULL; + + + osync_trace(TRACE_ENTRY, "%s(%p, %u, %p, %p, %p, %p, %p)", __func__, buffer, size, objtype, new_sourceformat_name, data, sourceformat, error); + + + osync_trace(TRACE_INTERNAL, "%s:%i: Applying workaround.\n", __FILE__, __LINE__); + + if (buffer == NULL) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: buffer = NULL.", __FILE__, __LINE__); + goto error; + } + + if (size == 0) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: buffer_size = 0.", __FILE__, __LINE__); + goto error; + } + + if (data == NULL) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: data = NULL.", __FILE__, __LINE__); + goto error; + } + + if (new_sourceformat_name == NULL) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: new_sourceformat_name = NULL.", __FILE__, __LINE__); + goto error; + } + + if (sourceformat == NULL) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: sourceformat = NULL.", __FILE__, __LINE__); + goto error; + } + + if (objtype == NULL) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: objtype = NULL.", __FILE__, __LINE__); + goto error; + } + + + // Pack it into an OSyncXMLFormat + xmlformat = osync_xmlformat_parse(buffer, size, error); + if (!xmlformat) { + osync_trace(TRACE_ERROR, "%s:%i: ERROR: osync_xmlformat_parse() has failed.\nFaulty input was:\n%.*s\n", __FILE__, __LINE__, size, buffer); + if (!osync_error_is_set(error)) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: osync_xmlformat_parse() must have failed."); + } + + goto error; + } + + + // Sort the format object: + osync_xmlformat_sort(xmlformat); + + if (osync_xmlformat_is_sorted(xmlformat)) { + osync_trace(TRACE_INTERNAL, "%s:%i: xmlformat just produced IS sorted.\n", __FILE__, __LINE__); + } else { + osync_trace(TRACE_INTERNAL, "%s:%i: xmlformat just produced is NOT sorted.\n", __FILE__, __LINE__); + } + + + // Define a new sourceformat based on a new sourceformat_name + *sourceformat = osync_objformat_new(new_sourceformat_name, objtype, error); + + + // Wrap it up into an OSyncData object + *data = osync_data_new((char *)xmlformat, osync_xmlformat_size(), *sourceformat, error); + if (*data == NULL || osync_error_is_set(error)) { + osync_trace(TRACE_ERROR, "%s:%i: ERROR: osync_data_new() has failed.", __FILE__, __LINE__); + + if (!osync_error_is_set(error)) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: data = NULL.", __FILE__, __LINE__); + } + + goto error; + } + + // Tell the data object what kind of source format it now carries. + osync_data_set_objformat(*data, *sourceformat); + if (osync_error_is_set(error)) { + goto error; + } + + + osync_trace(TRACE_EXIT, "%s", __func__); + return TRUE; + +error: + if (!osync_error_is_set(error)) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: Unknown reason.", __FILE__, __LINE__); + } + + osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); + return FALSE; +} + + + + + + + + + +osync_bool ldap_format_perform_conversion(const OSyncFormatEnv *format_env_orig, OSyncData **data, OSyncObjFormat *sourceformat, const conv_detection detection_type, const char *format_config, OSyncError **error) +{ + char *sourceformat_name = NULL, *targetformat_name = NULL; + OSyncObjFormat *targetformat = NULL; + OSyncFormatConverterPath *converter_path = NULL; + OSyncList *list = NULL, *l = NULL; + OSyncFormatEnv *format_env = (OSyncFormatEnv *) format_env_orig; + + + osync_trace(TRACE_ENTRY, "%s(%p, %p, %p, %i, %p, %p)", __func__, format_env_orig, data, sourceformat, detection_type, format_config, error); + + + if (data == NULL) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: data = NULL."); + goto error; + } + + if (format_env == NULL) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: format_env = NULL."); + goto error; + } + + if (sourceformat == NULL) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: sourceformat = NULL."); + goto error; + } + + + + sourceformat_name = (char *)osync_objformat_get_name(sourceformat); + + + + // Try and get the target format + targetformat = ldap_format_get_targetformat(format_env, sourceformat_name, detection_type, error); + if (targetformat == NULL) { + if (!osync_error_is_set(error)) + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: targetformat could not be recognized.", __FILE__, __LINE__); + + goto error; + } + + + targetformat_name = (char *)osync_objformat_get_name(targetformat); + if (!strcmp(targetformat_name, sourceformat_name)) { + osync_trace(TRACE_ERROR, "%s:%i: WARNING: targetformat_name is identical to sourceformat_name. So nothing to do.\n", __FILE__, __LINE__); + + goto out; + } + + + + // Try and find the converter path for a particular source format and + // target format + converter_path = osync_format_env_find_path(format_env, sourceformat, targetformat, error); + if (converter_path == NULL) { + if (!osync_error_is_set(error)) + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: converter_pathcould not be found for sourceformat = \"%s\" and targetformat = \"%s\".", __FILE__, __LINE__, sourceformat, targetformat); + + goto error; + } + + + // Add some additional configuration, if available + if (format_config) { + osync_converter_path_set_config(converter_path, format_config); + osync_trace(TRACE_INTERNAL, "%s:%i: config of converter path: \"%s\"\n", __FILE__, __LINE__, osync_converter_path_get_config (converter_path)); + +#ifdef DEBUG + } else { + osync_trace(TRACE_INTERNAL, "%s:%i: WARNING: format_config = NULL.\n", __FILE__, __LINE__); +#endif + } + + + // Dump the converters involved in the conversion path, + // but only the ones which reside at the edges. + l = NULL; + list = osync_converter_path_get_edges(converter_path); + if (list) { + for (l = list; l; l = l->next) { + if (l->data) { + osync_trace(TRACE_INTERNAL, "Edge converter: \"%s\"\n", osync_objformat_get_name(osync_converter_get_targetformat(l->data))); + } + } + } else { + osync_trace(TRACE_ERROR, "%s:%i: WARNING: list = NULL.", __FILE__, __LINE__); + } + + + // Dump the converters involved + l = NULL; + list = osync_format_env_find_converters(format_env, sourceformat, targetformat); + if (list) { + for (l = list; l; l = l->next) { + if (l->data) { + osync_trace(TRACE_INTERNAL, "Converter: \"%s\"\n", osync_objformat_get_name(osync_converter_get_targetformat(l->data))); + } + } + } else { + osync_trace(TRACE_INTERNAL, "%s:%i: WARNING: list = NULL.", __FILE__, __LINE__); + } + + + + // Now, convert the data from sourceformat to targetformat + if (!osync_format_env_convert(format_env, converter_path, *data, error)) { + if (!osync_error_is_set(error)) + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: conversion could not be performed for sourceformat = \"%s\" and targetformat = \"%s\".", __FILE__, __LINE__, sourceformat, targetformat); + + goto error; + } + + if (data == NULL) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: output data = NULL. Conversion must have failed.", __FILE__, __LINE__); + goto error; + } + + if (*data == NULL) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: output *data = NULL. Conversion must have failed.", __FILE__, __LINE__); + goto error; + } + + +out: + osync_trace(TRACE_EXIT, "%s", __func__); + return TRUE; + +error: + if (!osync_error_is_set(error)) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: Unknown reason.", __FILE__, __LINE__); + } + + osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); + return FALSE; +} + + + + int main (int argc, char *argv[]) { OSyncData *data = NULL; OSyncError *error = NULL; OSyncFormatEnv *format_env = NULL; OSyncObjFormat *sourceformat = NULL; - OSyncObjFormat *targetformat = NULL; OSyncFormatConverterPath *converter_path = NULL; - OSyncList *l = NULL, *list = NULL; conv_detection detection_type = TARGET_AUTO; char *program = NULL; char *format_config = NULL; @@ -601,16 +877,19 @@ int i = 0; + osync_trace(TRACE_ENTRY, "%s(%i, %p)", __func__, argc, argv); + + // Parse arguments if (argc < 2) { osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: Not enough arguments. You must state the name of the input file to be converted.", __FILE__, __LINE__); goto error; } - if (!strcmp(argv[0], "-h") || !strcmp(argv[0], "--help")) { - usage(argv[0]); - return 0; + if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) { + usage(argv[1]); + goto out; } @@ -624,7 +903,7 @@ if (!strcmp(arg, "--help") || !strcmp(arg, "-h")) { usage(program); - return 0; + goto out; } else if (!strcmp(arg, "--to-vcard21")) { detection_type = TARGET_VCARD_21; @@ -669,15 +948,15 @@ detection_type = TARGET_LDAP_NOTE; } else if (!strcmp (arg, "--out")) { if (!strncmp(argv[i + 1], "--", 2)) { - fprintf(stderr, "ERROR: Output filename MUST NOT begin with \'--\'. Use --help, if necessary."); + osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: Output filename MUST NOT begin with \'--\'. Use --help, if necessary.", __FILE__, __LINE__); continue; } else if (!strncmp(argv[i + 1], "-", 1)) { - fprintf(stderr, "ERROR: Output filename MUST NOT begin with \'-\'. Use --help, if necessary."); + osync_trace(TRACE_ERROR, "%s:%i: ERROR. Output filename MUST NOT begin with \'-\'. Use --help, if necessary.", __FILE__, __LINE__); continue; } else if (argv[i + 1] == 0) { - fprintf(stderr, "ERROR: --out must be followed by a name of a file where the output can be written to. Use --help, if necessary."); + osync_trace(TRACE_ERROR, "%s:%i: ERROR: --out must be followed by a name of a file where the output can be written to. Use --help, if necessary.", __FILE__, __LINE__); continue; } else { @@ -688,19 +967,19 @@ } else if (!strcmp (arg, "--")) { break; - } else if (arg[0] == '-') { - fprintf(stderr, "%s:%i: ERROR: \"-\" is an invalid option. Use --help, if necessary.\n", __FILE__, __LINE__); + } else if (arg[0] == '-' && arg[1] == 0) { + osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: \"-\" is an invalid option. Use --help, if necessary.\n", __FILE__, __LINE__); goto error; } else { - fprintf(stderr, "%s:%i: ERROR: Unknown option.\n", __FILE__, __LINE__); + osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: Unknown option.\n", __FILE__, __LINE__); goto error; } } - + // Check input_filename if (input_filename == NULL) { osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: input_filename = NULL. input_filename has not been set.", __FILE__, __LINE__); @@ -714,6 +993,7 @@ } + // Check for presence of the input file errno = 0; if (stat(input_filename, &stat_buffer)) { osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: Could not load \"%s\": %s", __FILE__, __LINE__, input_filename, strerror(errno)); @@ -734,6 +1014,7 @@ size++; + // Allocate a new format environment format_env = osync_format_env_new(&error); if (format_env == NULL) { @@ -768,7 +1049,6 @@ // Analyze the source format - sourceformat = osync_format_env_detect_objformat(format_env, data); if (sourceformat == NULL) { osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: sourceformat = NULL. Source format could not be recognized.", __FILE__, __LINE__); @@ -776,16 +1056,6 @@ } -/* - if (!osync_format_env_detect_objformat_full(format_env, data, &sourceformat, &error)) { - if (!osync_error_is_set(&error)) { - osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: osync_format_env_detect_objformat_full() has failed.", __FILE__, __LINE__); - } - - goto error; - } -*/ - if (sourceformat == NULL) { osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: sourceformat = NULL. Source format could not be recognized.", __FILE__, __LINE__); @@ -795,14 +1065,6 @@ char *sourceformat_name = (char *)osync_objformat_get_name(sourceformat); -#if 0 - // Workaround - if (!strcmp(sourceformat_name, "xmlformat-contact-doc")) { - sourceformat_name = (char *)"xmlformat-contact"; - sourceformat = osync_objformat_new(sourceformat_name, "contact", &error); - } -#endif - // Tell the data object what kind of source format we have just found. osync_data_set_objformat(data, sourceformat); @@ -811,97 +1073,86 @@ } - - - // Try and get the target format - targetformat = conv_run_detection(format_env, sourceformat_name, detection_type, &error); - if (targetformat == NULL) { - if (!osync_error_is_set(&error)) - osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: targetformat could not be recognized.", __FILE__, __LINE__); - - goto error; - } - - - - // Try and find the converter path for a particular source format and - // target format - converter_path = osync_format_env_find_path(format_env, sourceformat, targetformat, &error); - if (converter_path == NULL) { - if (!osync_error_is_set(&error)) - osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: converter_pathcould not be found for sourceformat = \"%s\" and targetformat = \"%s\".", __FILE__, __LINE__, sourceformat, targetformat); - - goto error; - } - - if (format_config) { - osync_converter_path_set_config(converter_path, format_config); - fprintf(stderr, "%s:%i: config of converter path: \"%s\"\n", __FILE__, __LINE__, osync_converter_path_get_config (converter_path)); -#ifdef DEBUG - } else { - fprintf(stderr, "%s:%i: WARNING: format_config = NULL.\n", __FILE__, __LINE__); -#endif - } - - - - - - - - - // Dump the converters involved in the conversion path, - // but only the ones which reside at the edges. - l = NULL; - list = osync_converter_path_get_edges(converter_path); - if (list) { - for (l = list; l; l = l->next) { - if (l->data) { - fprintf(stderr, "Edge converter: \"%s\"\n", osync_objformat_get_name(osync_converter_get_targetformat(l->data))); - } + // The xmlformat plugin expects input as OSyncXMLFormat. So we need to + // reshuffle the data before sending it to that plugin. + if (!strcmp(sourceformat_name, "xmlformat-contact-doc")) { + if (!ldap_format_prepare_xmlformat_data(buffer, size, "contact", "xmlformat-contact", &data, &sourceformat, &error)) { + goto error; } - } else { - osync_trace(TRACE_ERROR, "%s:%i: WARNING: list = NULL.", __FILE__, __LINE__); - } + } else if (!strcmp(sourceformat_name, "xmlformat-event-doc")) { + if (!ldap_format_prepare_xmlformat_data(buffer, size, "event", "xmlformat-event", &data, &sourceformat, &error)) { + goto error; + } + } else if (!strcmp(sourceformat_name, "xmlformat-todo-doc")) { + if (!ldap_format_prepare_xmlformat_data(buffer, size, "todo", "xmlformat-todo", &data, &sourceformat, &error)) { + goto error; + } + } if (!strcmp(sourceformat_name, "xmlformat-note-doc")) { + if (!ldap_format_prepare_xmlformat_data(buffer, size, "note", "xmlformat-note", &data, &sourceformat, &error)) { + goto error; + } + } - // Now, convert the data from sourceformat to targetformat - if (!osync_format_env_convert(format_env, converter_path, data, &error)) { + // Now do the actual conversion work + if (!ldap_format_perform_conversion(format_env, &data, sourceformat, detection_type, format_config, &error)) { if (!osync_error_is_set(&error)) - osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: conversion could not be performed for sourceformat = \"%s\" and targetformat = \"%s\".", __FILE__, __LINE__, sourceformat, targetformat); + osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: ldap_format_perform_conversion() has failed.", __FILE__, __LINE__); goto error; } - if (data == NULL) { - osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: output data = NULL. Conversion must have failed.", __FILE__, __LINE__); - goto error; - } - + // Care about the output result = osync_data_get_printable(data); if (result == NULL) { - osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: result = NULL. Conversion must have failed.", __FILE__, __LINE__); - goto error; - } + char *buf = NULL; + unsigned int buf_size = 0; + osync_trace(TRACE_ERROR, "%s:%i: WARNING: result = NULL. Has the conversion failed?", __FILE__, __LINE__); + osync_trace(TRACE_ERROR, "%s:%i: Resulting format = \"%s\"", __FILE__, __LINE__, (char *)osync_objformat_get_name(osync_data_get_objformat(data))); + osync_trace(TRACE_ERROR, "%s:%i: Resulting objtype = \"%s\"", __FILE__, __LINE__, osync_data_get_objtype (data)); + + + osync_data_get_data(data, &buf, &buf_size); + if (buf && buf_size > 0) { + if (output_filename) { + // Output to a file + if (!osync_file_write(output_filename, buf, buf_size, 0644, &error)) { + osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: Unable to write file %s.", __FILE__, __LINE__, output_filename); + goto error; + } + } else { + // Output to the screen + printf("%.*s\n", buf_size, buf); + } + } else { + osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: result = NULL. And buf = %p and buf_size = %u. Conversion must have failed.", __FILE__, __LINE__, buf, buf_size); - // Output to screen or file - if (output_filename) { - if (!osync_file_write(output_filename, result, strlen(result), 0644, &error)) { - osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: Unable to write file %s.", __FILE__, __LINE__, output_filename); goto error; } } else { - printf("%s\n", result); + if (output_filename) { + // Output to a file + if (!osync_file_write(output_filename, result, strlen(result), 0644, &error)) { + osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: Unable to write file %s.", __FILE__, __LINE__, output_filename); + goto error; + } + } else { + // Output to the screen + printf("%s\n", result); + } } + + + // Clean up if (data) osync_data_unref(data); @@ -911,8 +1162,10 @@ if (format_env) osync_format_env_unref(format_env); - return 0; +out: + osync_trace(TRACE_EXIT, "%s", __func__); + return 0; error: if (data) @@ -924,10 +1177,12 @@ if (format_env) osync_format_env_unref(format_env); - if (osync_error_is_set(&error)) { - fprintf(stderr, "%s\n", osync_error_print(&error)); - osync_error_unref(&error); + + if (!osync_error_is_set(&error)) { + osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: Unknown reason.\n", __FILE__, __LINE__); } + + osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(&error)); return 1; } |