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
23 * @author Adenilson Cavalcanti da Silva <adenilson.silva@indt.org.br>
25 * @brief A generic xslt format plugin.
28 * - add more error handling code
29 * - test the plugin for real
30 * - add support for other data types (notes, events, etc)
35 #include <opensync/opensync.h>
36 #include <opensync/opensync-format.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
)))
48 if (!(*output
= strdup((char*)converter
->xml_str
)))
55 * - use a common function to call xslt transform?
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
)))
71 if (!(*output
= strdup((char*)converter
->xml_str
)))
78 * - use a common function to call xslt transform?
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
);
101 if( !osync_format_env_register_objformat(env
, format
, error
) )
103 osync_objformat_unref(format
);
108 void *initialize_xslt(const char* config
, OSyncError
**error
)
110 struct xslt_resources
*converter
= xslt_new();
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.");
123 static osync_bool
finalize_xslt(void *userdata
, OSyncError
**error
)
125 struct xslt_resources
*converter
= NULL
;
129 converter
= (struct xslt_resources
*)userdata
;
130 xslt_delete(converter
);
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
,
143 convert_func
, &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
) )
151 osync_converter_unref(conv
);
157 osync_converter_unref(conv
);
158 osync_trace(TRACE_INTERNAL
, "%s", osync_error_print(&error
));
159 osync_error_unref(&error
);
163 osync_bool
get_conversion_info(OSyncFormatEnv
*env
, OSyncError
**error
)
166 /* Supported formats */
167 OSyncObjFormat
*xslt_contact
= osync_format_env_find_objformat(env
, "xslt-contact");
169 osync_error_set(error
, OSYNC_ERROR_GENERIC
, "Unable to find xslt-contact"
174 OSyncObjFormat
*xml_contact
= osync_format_env_find_objformat(env
, "xmlformat-contact");
176 osync_error_set(error
, OSYNC_ERROR_GENERIC
, "Unable to find xmlformat-contact format");
180 /* XSLT to xml-contact */
181 result
= reg_conv(env
, xslt_contact
, xml_contact
,
182 conv_xslt_to_contact
,
183 initialize_xslt
, finalize_xslt
);
187 /* xml-contact to XSLT */
188 result
= reg_conv(env
, xml_contact
, xslt_contact
,
189 conv_contact_to_xslt
,
190 initialize_xslt
, finalize_xslt
);
195 int get_version(void)
197 /* always return 1 */