3 Copyright (C) 2012 Belledonne Communications SARL
4 Yann DIORCET (yann.diorcet@linphone.org)
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include <libxml/xmlsave.h>
24 #include <libxml/xmlversion.h>
26 #define LPC2XML_BZ 2048
28 struct _lpc2xml_context {
34 char errorBuffer[LPC2XML_BZ];
35 char warningBuffer[LPC2XML_BZ];
39 lpc2xml_context* lpc2xml_context_new(lpc2xml_function cbf, void *ctx) {
40 lpc2xml_context *xmlCtx = (lpc2xml_context*)malloc(sizeof(lpc2xml_context));
47 xmlCtx->errorBuffer[0]='\0';
48 xmlCtx->warningBuffer[0]='\0';
53 void lpc2xml_context_destroy(lpc2xml_context *ctx) {
54 if(ctx->doc != NULL) {
61 static void lpc2xml_context_clear_logs(lpc2xml_context *ctx) {
62 ctx->errorBuffer[0]='\0';
63 ctx->warningBuffer[0]='\0';
66 static void lpc2xml_log(lpc2xml_context *xmlCtx, int level, const char *fmt, ...) {
69 if(xmlCtx->cbf != NULL) {
70 xmlCtx->cbf((xmlCtx)->ctx, level, fmt, args);
75 static void lpc2xml_genericxml_error(void *ctx, const char *fmt, ...) {
76 lpc2xml_context *xmlCtx = (lpc2xml_context *)ctx;
77 int sl = strlen(xmlCtx->errorBuffer);
80 vsnprintf(xmlCtx->errorBuffer + sl, LPC2XML_BZ-sl, fmt, args);
85 static void lpc2xml_genericxml_warning(void *ctx, const char *fmt, ...) {
86 lpc2xml_context *xmlCtx = (lpc2xml_context *)ctx;
87 int sl = strlen(xmlCtx->warningBuffer);
90 vsnprintf(xmlCtx->warningBuffer + sl, LPC2XML_BZ-sl, fmt, args);
95 static int processEntry(const char *section, const char *entry, xmlNode *node, lpc2xml_context *ctx) {
96 const char *content = lp_config_get_string(ctx->lpc, section, entry, NULL);
98 lpc2xml_log(ctx->ctx, LPC2XML_ERROR, "Issue when reading the lpc");
101 xmlNodeSetContent(node, (const xmlChar *) content);
105 struct __processSectionCtx {
109 lpc2xml_context *ctx;
112 static void processSection_cb(const char *entry, struct __processSectionCtx *ctx) {
114 xmlNode *node = xmlNewChild(ctx->node, NULL, (const xmlChar *)"entry", NULL);
116 lpc2xml_log(ctx->ctx, LPC2XML_ERROR, "Can't create \"entry\" element");
120 xmlAttr *name_attr = xmlSetProp(node, (const xmlChar *)"name", (const xmlChar *)entry);
121 if(name_attr == NULL) {
122 lpc2xml_log(ctx->ctx, LPC2XML_ERROR, "Can't create name attribute for \"entry\" element");
126 ctx->ret = processEntry(ctx->section, entry, node, ctx->ctx);
130 static int processSection(const char *section, xmlNode *node, lpc2xml_context *ctx) {
131 struct __processSectionCtx pc_ctx = {0, section, node, ctx};
132 lp_config_for_each_entry(ctx->lpc, section, (void (*)(const char *, void *))processSection_cb, (void*)&pc_ctx);
138 struct __processConfigCtx {
141 lpc2xml_context *ctx;
144 static void processConfig_cb(const char *section, struct __processConfigCtx *ctx) {
146 xmlNode *node = xmlNewChild(ctx->node, NULL, (const xmlChar *)"section", NULL);
148 lpc2xml_log(ctx->ctx, LPC2XML_ERROR, "Can't create \"section\" element");
152 xmlAttr *name_attr = xmlSetProp(node, (const xmlChar *)"name", (const xmlChar *)section);
153 if(name_attr == NULL) {
154 lpc2xml_log(ctx->ctx, LPC2XML_ERROR, "Can't create name attribute for \"section\" element");
158 ctx->ret = processSection(section, node, ctx->ctx);
162 static int processConfig(xmlNode *node, lpc2xml_context *ctx) {
163 struct __processConfigCtx pc_ctx = {0, node, ctx};
164 lp_config_for_each_section(ctx->lpc, (void (*)(const char *, void *))processConfig_cb, (void*)&pc_ctx);
168 static int processDoc(xmlDoc *doc, lpc2xml_context *ctx) {
170 xmlNode *root_node = xmlNewNode(NULL, (const xmlChar *)"config");
171 if(root_node == NULL) {
172 lpc2xml_log(ctx, LPC2XML_ERROR, "Can't create \"config\" element");
175 xmlNs *lpc_ns = xmlNewNs(root_node, (const xmlChar *)"http://www.linphone.org/xsds/lpconfig.xsd", NULL);
177 lpc2xml_log(ctx, LPC2XML_WARNING, "Can't create lpc namespace");
179 xmlSetNs(root_node, lpc_ns);
181 xmlNs *xsi_ns = xmlNewNs(root_node, (const xmlChar *)"http://www.w3.org/2001/XMLSchema-instance", (const xmlChar *)"xsi");
183 lpc2xml_log(ctx, LPC2XML_WARNING, "Can't create xsi namespace");
185 xmlAttr *schemaLocation = xmlNewNsProp(root_node, xsi_ns, (const xmlChar *)"schemaLocation", (const xmlChar *)"http://www.linphone.org/xsds/lpconfig.xsd lpconfig.xsd");
186 if(schemaLocation == NULL) {
187 lpc2xml_log(ctx, LPC2XML_WARNING, "Can't create schemaLocation");
189 ret = processConfig(root_node, ctx);
190 xmlDocSetRootElement(doc, root_node);
194 static int internal_convert_lpc2xml(lpc2xml_context *ctx) {
196 lpc2xml_log(ctx, LPC2XML_DEBUG, "Generation started");
197 if(ctx->doc != NULL) {
198 xmlFreeDoc(ctx->doc);
201 xmlDoc *doc = xmlNewDoc((const xmlChar *)"1.0");
202 ret = processDoc(doc, ctx);
208 lpc2xml_log(ctx, LPC2XML_DEBUG, "Generation ended ret:%d", ret);
212 int lpc2xml_set_lpc(lpc2xml_context* context, const LpConfig *lpc) {
217 int lpc2xml_convert_file(lpc2xml_context* context, const char *filename) {
219 lpc2xml_context_clear_logs(context);
220 xmlSetGenericErrorFunc(context, lpc2xml_genericxml_error);
221 xmlSaveCtxtPtr save_ctx = xmlSaveToFilename(filename, "UTF-8", XML_SAVE_FORMAT);
222 if(save_ctx != NULL) {
223 ret = internal_convert_lpc2xml(context);
225 ret = xmlSaveDoc(save_ctx, context->doc);
227 lpc2xml_log(context, LPC2XML_ERROR, "Can't save document");
228 lpc2xml_log(context, LPC2XML_ERROR, "%s", context->errorBuffer);
231 xmlSaveClose(save_ctx);
233 lpc2xml_log(context, LPC2XML_ERROR, "Can't open file:%s", filename);
234 lpc2xml_log(context, LPC2XML_ERROR, "%s", context->errorBuffer);
239 int lpc2xml_convert_fd(lpc2xml_context* context, int fd) {
241 lpc2xml_context_clear_logs(context);
242 xmlSetGenericErrorFunc(context, lpc2xml_genericxml_error);
243 xmlSaveCtxtPtr save_ctx = xmlSaveToFd(fd, "UTF-8", XML_SAVE_FORMAT);
244 if(save_ctx != NULL) {
245 ret = internal_convert_lpc2xml(context);
247 ret = xmlSaveDoc(save_ctx, context->doc);
249 lpc2xml_log(context, LPC2XML_ERROR, "Can't save document");
250 lpc2xml_log(context, LPC2XML_ERROR, "%s", context->errorBuffer);
253 xmlSaveClose(save_ctx);
255 lpc2xml_log(context, LPC2XML_ERROR, "Can't open fd:%d", fd);
256 lpc2xml_log(context, LPC2XML_ERROR, "%s", context->errorBuffer);
261 int lpc2xml_convert_string(lpc2xml_context* context, char **content) {
263 xmlBufferPtr buffer = xmlBufferCreate();
264 lpc2xml_context_clear_logs(context);
265 xmlSetGenericErrorFunc(context, lpc2xml_genericxml_error);
266 xmlSaveCtxtPtr save_ctx = xmlSaveToBuffer(buffer, "UTF-8", XML_SAVE_FORMAT);
267 if(save_ctx != NULL) {
268 ret = internal_convert_lpc2xml(context);
270 ret = xmlSaveDoc(save_ctx, context->doc);
272 lpc2xml_log(context, LPC2XML_ERROR, "Can't save document");
273 lpc2xml_log(context, LPC2XML_ERROR, "%s", context->errorBuffer);
276 xmlSaveClose(save_ctx);
278 lpc2xml_log(context, LPC2XML_ERROR, "Can't initialize internal buffer");
279 lpc2xml_log(context, LPC2XML_ERROR, "%s", context->errorBuffer);
282 #if LIBXML_VERSION >= 20800
283 *content = (char *)xmlBufferDetach(buffer);
285 *content = strdup((const char *)xmlBufferContent(buffer));
288 xmlBufferFree(buffer);