API and compile changes
[opensync/xsltformat-cdf.git] / src / xsltformat.c
blob2b99e2d22730b76826241859e95c4f1c4407ae83
1 /*
2 * Copyright (C) 2009 Daniel Gollub <gollub@b1-systems.de>
3 * Copyright (C) 2009 Instituto Nokia de Tecnologia
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 /**
22 * @file xsltformat.c
23 * @author Adenilson Cavalcanti da Silva <adenilson.silva@indt.org.br>
25 * @brief A generic xslt format plugin.
27 * \todo:
28 * - add more error handling code
29 * - test the plugin for real
30 * - add support for other data types (notes, events, etc)
31 * - utests?
35 #include <opensync/opensync.h>
36 #include <opensync/opensync-format.h>
37 #include "xslt_aux.h"
39 static osync_bool conv_xslt_to_contact(char *input, unsigned int inpsize, char **output, unsigned int *outpsize, osync_bool *free_input, const char *config, void *userdata, OSyncError **error)
41 osync_bool result = FALSE;
42 struct xslt_resources *converter = NULL;
44 converter = (struct xslt_resources *)userdata;
45 if ((result = xslt_transform(converter, input)))
46 goto exit;
48 if (!(*output = strdup((char*)converter->xml_str)))
49 goto exit;
51 *free_input = TRUE;
53 /* TODO:
54 * - set OsyncError
55 * - use a common function to call xslt transform?
58 exit:
59 return result;
62 static osync_bool conv_contact_to_xslt(char *input, unsigned int inpsize, char **output, unsigned int *outpsize, osync_bool *free_input, const char *config, void *userdata, OSyncError **error)
64 osync_bool result = FALSE;
65 struct xslt_resources *converter = NULL;
67 converter = (struct xslt_resources *)userdata;
68 if ((result = xslt_transform(converter, input)))
69 goto exit;
71 if (!(*output = strdup((char*)converter->xml_str)))
72 goto exit;
74 *free_input = TRUE;
76 /* TODO:
77 * - set OsyncError
78 * - use a common function to call xslt transform?
81 exit:
82 return result;
87 static void destroy_format1(char *input, unsigned int inpsize, void *user_data)
90 // Here you have to free the data allocated by your format
95 osync_bool get_format_info(OSyncFormatEnv *env, OSyncError **error)
97 OSyncObjFormat *format = osync_objformat_new("xslt", "contact", error);
98 if (!format)
99 return FALSE;
101 if( !osync_format_env_register_objformat(env, format, error) )
102 return FALSE;
103 osync_objformat_unref(format);
105 return TRUE;
108 void *initialize_xslt(const char* config, OSyncError **error)
110 struct xslt_resources *converter = xslt_new();
111 int result = 0;
112 if (!converter)
113 osync_error_set(error, OSYNC_ERROR_GENERIC,
114 "Unable create xslt converter context.");
115 if ((result = xslt_initialize(converter, config)))
116 osync_error_set(error, OSYNC_ERROR_GENERIC,
117 "Unable load xslt stylesheet.");
119 return converter;
123 static osync_bool finalize_xslt(void *userdata, OSyncError **error)
125 struct xslt_resources *converter = NULL;
126 if (!userdata)
127 return FALSE;
129 converter = (struct xslt_resources *)userdata;
130 xslt_delete(converter);
131 return TRUE;
134 static osync_bool reg_conv(OSyncFormatEnv *env,
135 OSyncObjFormat *from, OSyncObjFormat *to,
136 OSyncFormatConvertFunc convert_func,
137 OSyncFormatConverterInitializeFunc initialize_func,
138 OSyncFormatConverterFinalizeFunc finalize_func)
140 OSyncError *error = NULL;
141 OSyncFormatConverter *conv = osync_converter_new(OSYNC_CONVERTER_CONV,
142 from, to,
143 convert_func, &error);
144 if (!conv)
145 goto error;
147 osync_converter_set_initialize_func(conv, initialize_func);
148 osync_converter_set_finalize_func(conv, finalize_func);
149 if( !osync_format_env_register_converter(env, conv, &error) )
150 goto error;
151 osync_converter_unref(conv);
153 return TRUE;
155 error:
156 if( conv )
157 osync_converter_unref(conv);
158 osync_trace(TRACE_INTERNAL, "%s", osync_error_print(&error));
159 osync_error_unref(&error);
160 return FALSE;
163 osync_bool get_conversion_info(OSyncFormatEnv *env, OSyncError **error)
165 osync_bool result;
166 /* Supported formats */
167 OSyncObjFormat *xslt_contact = osync_format_env_find_objformat(env, "xslt-contact");
168 if (!xslt_contact) {
169 osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to find xslt-contact"
170 " format");
171 return FALSE;
174 OSyncObjFormat *xml_contact = osync_format_env_find_objformat(env, "xmlformat-contact");
175 if (!xml_contact) {
176 osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to find xmlformat-contact format");
177 return FALSE;
180 /* XSLT to xml-contact */
181 result = reg_conv(env, xslt_contact, xml_contact,
182 conv_xslt_to_contact,
183 initialize_xslt, finalize_xslt);
184 if (!result)
185 return FALSE;
187 /* xml-contact to XSLT */
188 result = reg_conv(env, xml_contact, xslt_contact,
189 conv_contact_to_xslt,
190 initialize_xslt, finalize_xslt);
191 return result;
195 int get_version(void)
197 /* always return 1 */
198 return 1;