Aesthetics.
[opensync/xsltformat-cdf.git] / src / xsltformat.c
blob11bc09d94e9f2444df6d36d7535629f6d62de457
1 /*
2 * Copyright (C) 2009 Daniel Gollub <dgollub@suse.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 * - make the convertion (should be easy thanks to xslt_aux functions)
32 #include <opensync/opensync.h>
33 #include <opensync/opensync-format.h>
34 #include "xslt_aux.h"
36 static OSyncConvCmpResult compare_format1(const char *leftdata, unsigned int leftsize,
37 const char *rightdata, unsigned int rightsize,
38 void *user_data)
41 * This function can be used to compare two types of your formats.
42 * This is optional. For example, you can only provide a conversion
43 * from and to the xml format and let all the comparison be done there
48 * Compare your objects here. You might need to cast the data of the change
49 * objects to you own defined types.
51 * The possible result of the comparison are:
53 * return OSYNC_CONV_DATA_SAME;
54 * The objects are exactly the same. (they might differ in some uid or
55 * timestamp though, but ALL the "real" information is the same)
57 * return OSYNC_CONV_DATA_SIMILAR;
58 * The objects are not _exactly_ the same, but they look similar. This is used
59 * to detect conflicts. It is up to you to decide what "similar" means for your
60 * object
62 * return OSYNC_CONV_DATA_MISMATCH;
63 * This means the objects are not the same and not similar.
66 return OSYNC_CONV_DATA_MISMATCH;
69 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)
72 * This function can be used to convert your format to another format.
73 * Return TRUE if the conversion was successful or return FALSE and set
74 * the error if something bad has happend.
78 /* The arguments mean:
80 * - conv_data:
81 * Pointer to the data you returned in your init function (if any)
83 * - input:
84 * The data you need to convert
85 * - inpsize
86 * The size of the input data
88 * - output:
89 * After converting you need to set this
90 * to your result
91 * - outpsize:
92 * The size of the output
94 * - free_input:
95 * You need to set this to TRUE if opensync
96 * can free the input after the conversion (so you dont
97 * use any reference from or to the input). A example where
98 * *free_input = FALSE; needs to be done would be a encapsulator
99 * that stores the input reference somewhere in its struct
101 * - error:
102 * if something bad happens and you cannot convert, set the error!
106 return TRUE;
109 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)
112 * This function can be used to convert another format to your format.
113 * Return TRUE if the conversion was successful or return FALSE and set
114 * the error if something bad has happend.
118 return TRUE;
122 static void destroy_format1(char *input, unsigned int inpsize, void *user_data)
125 * Here you have to free the data allocated by your format
130 static osync_bool duplicate_format1(const char *uid, const char *input, unsigned int insize, char **newuid, char **output, unsigned int *outsize, osync_bool *dirty, OSyncError **error)
133 * This function can be used to duplicate your format.
134 * Duplication does not mean to make 2 objects out of one,
135 * but to change to uid of the object in such a way that it
136 * differes from the original uid.
138 * Most format will never need this.
142 return TRUE;
145 static char *print_format1(const char *data, unsigned int size)
148 * If your format is not in a human printable format already
149 * you have to return a human readable string here describing the object
150 * as closely as possible. This information will be used by the user to decide
151 * which object to pick in a conflict.
154 return NULL;
157 osync_bool get_format_info(OSyncFormatEnv *env, OSyncError **error)
160 * this function is called to register a new format
162 OSyncObjFormat *format = osync_objformat_new("xslt", "contact", error);
163 if (!format)
164 return FALSE;
166 osync_objformat_set_compare_func(format, compare_format1);
167 osync_objformat_set_destroy_func(format, destroy_format1);
168 //osync_objformat_set_duplicate_func(format, duplicate_format1);
169 //osync_objformat_set_print_func(format, print_format1);
172 osync_format_env_register_objformat(env, format);
173 osync_objformat_unref(format);
175 return TRUE;
178 void *initialize_xslt(const char* config, OSyncError **error)
180 struct xslt_resources *converter = xslt_new();
181 int result = 0;
182 if (!converter)
183 osync_error_set(error, OSYNC_ERROR_GENERIC,
184 "Unable create xslt converter context.");
185 if ((result = xslt_initialize(converter, config)))
186 osync_error_set(error, OSYNC_ERROR_GENERIC,
187 "Unable load xslt stylesheet.");
189 return converter;
193 void finalize_xslt(void *userdata)
195 struct xslt_resources *converter = NULL;
196 if (!userdata)
197 return;
199 converter = (struct xslt_resources *)userdata;
200 xslt_delete(converter);
204 static osync_bool reg_conv(OSyncFormatEnv *env,
205 OSyncObjFormat *from, OSyncObjFormat *to,
206 OSyncFormatConvertFunc convert_func,
207 OSyncFormatConverterInitializeFunc initialize_func,
208 OSyncFormatConverterFinalizeFunc finalize_func)
210 OSyncError *error = NULL;
211 OSyncFormatConverter *conv = osync_converter_new(OSYNC_CONVERTER_CONV,
212 from, to,
213 convert_func, &error);
214 if (!conv)
215 return FALSE;
217 osync_converter_set_initialize_func(conv, initialize_func);
218 osync_converter_set_finalize_func(conv, finalize_func);
219 osync_format_env_register_converter(env, conv);
220 osync_converter_unref(conv);
222 return TRUE;
226 osync_bool get_conversion_info(OSyncFormatEnv *env, OSyncError **error)
228 osync_bool result;
229 /* Supported formats */
230 OSyncObjFormat *xslt_contact = osync_format_env_find_objformat(env, "xslt-contact");
231 if (!xslt_contact) {
232 osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to find xslt-contact"
233 " format");
234 return FALSE;
237 OSyncObjFormat *xml_contact = osync_format_env_find_objformat(env, "xmlformat-contact");
238 if (!xml_contact) {
239 osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to find xmlformat-contact format");
240 return FALSE;
243 /* XSLT to xml-contact */
244 result = reg_conv(env, xslt_contact, xml_contact,
245 conv_xslt_to_contact,
246 initialize_xslt, finalize_xslt);
247 if (!result)
248 return FALSE;
250 /* xml-contact to XSLT */
251 result = reg_conv(env, xml_contact, xslt_contact,
252 conv_contact_to_xslt,
253 initialize_xslt, finalize_xslt);
254 return result;
258 int get_version(void)
260 /* always return 1 */
261 return 1;