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);
84 static void lpc2xml_genericxml_warning(void *ctx, const char *fmt, ...) {
85 lpc2xml_context *xmlCtx = (lpc2xml_context *)ctx;
86 int sl = strlen(xmlCtx->warningBuffer);
89 vsnprintf(xmlCtx->warningBuffer + sl, LPC2XML_BZ-sl, fmt, args);
93 static int processEntry(const char *section, const char *entry, xmlNode *node, lpc2xml_context *ctx) {
94 const char *content = lp_config_get_string(ctx->lpc, section, entry, NULL);
96 lpc2xml_log(ctx->ctx, LPC2XML_ERROR, "Issue when reading the lpc");
99 xmlNodeSetContent(node, (const xmlChar *) content);
103 struct __processSectionCtx {
107 lpc2xml_context *ctx;
110 static void processSection_cb(const char *entry, struct __processSectionCtx *ctx) {
112 xmlNode *node = xmlNewChild(ctx->node, NULL, (const xmlChar *)"entry", NULL);
114 lpc2xml_log(ctx->ctx, LPC2XML_ERROR, "Can't create \"entry\" element");
118 xmlAttr *name_attr = xmlSetProp(node, (const xmlChar *)"name", (const xmlChar *)entry);
119 if(name_attr == NULL) {
120 lpc2xml_log(ctx->ctx, LPC2XML_ERROR, "Can't create name attribute for \"entry\" element");
124 ctx->ret = processEntry(ctx->section, entry, node, ctx->ctx);
128 static int processSection(const char *section, xmlNode *node, lpc2xml_context *ctx) {
129 struct __processSectionCtx pc_ctx = {0, section, node, ctx};
130 lp_config_for_each_entry(ctx->lpc, section, (void (*)(const char *, void *))processSection_cb, (void*)&pc_ctx);
136 struct __processConfigCtx {
139 lpc2xml_context *ctx;
142 static void processConfig_cb(const char *section, struct __processConfigCtx *ctx) {
144 xmlNode *node = xmlNewChild(ctx->node, NULL, (const xmlChar *)"section", NULL);
146 lpc2xml_log(ctx->ctx, LPC2XML_ERROR, "Can't create \"section\" element");
150 xmlAttr *name_attr = xmlSetProp(node, (const xmlChar *)"name", (const xmlChar *)section);
151 if(name_attr == NULL) {
152 lpc2xml_log(ctx->ctx, LPC2XML_ERROR, "Can't create name attribute for \"section\" element");
156 ctx->ret = processSection(section, node, ctx->ctx);
160 static int processConfig(xmlNode *node, lpc2xml_context *ctx) {
161 struct __processConfigCtx pc_ctx = {0, node, ctx};
162 lp_config_for_each_section(ctx->lpc, (void (*)(const char *, void *))processConfig_cb, (void*)&pc_ctx);
166 static int processDoc(xmlDoc *doc, lpc2xml_context *ctx) {
168 xmlNode *root_node = xmlNewNode(NULL, (const xmlChar *)"config");
169 if(root_node == NULL) {
170 lpc2xml_log(ctx, LPC2XML_ERROR, "Can't create \"config\" element");
173 xmlNs *lpc_ns = xmlNewNs(root_node, (const xmlChar *)"http://www.linphone.org/xsds/lpconfig.xsd", NULL);
175 lpc2xml_log(ctx, LPC2XML_WARNING, "Can't create lpc namespace");
177 xmlSetNs(root_node, lpc_ns);
179 xmlNs *xsi_ns = xmlNewNs(root_node, (const xmlChar *)"http://www.w3.org/2001/XMLSchema-instance", (const xmlChar *)"xsi");
181 lpc2xml_log(ctx, LPC2XML_WARNING, "Can't create xsi namespace");
183 xmlAttr *schemaLocation = xmlNewNsProp(root_node, xsi_ns, (const xmlChar *)"schemaLocation", (const xmlChar *)"http://www.linphone.org/xsds/lpconfig.xsd lpconfig.xsd");
184 if(schemaLocation == NULL) {
185 lpc2xml_log(ctx, LPC2XML_WARNING, "Can't create schemaLocation");
187 ret = processConfig(root_node, ctx);
188 xmlDocSetRootElement(doc, root_node);
192 static int internal_convert_lpc2xml(lpc2xml_context *ctx) {
194 lpc2xml_log(ctx, LPC2XML_DEBUG, "Generation started");
195 if(ctx->doc != NULL) {
196 xmlFreeDoc(ctx->doc);
199 xmlDoc *doc = xmlNewDoc((const xmlChar *)"1.0");
200 ret = processDoc(doc, ctx);
206 lpc2xml_log(ctx, LPC2XML_DEBUG, "Generation ended ret:%d", ret);
210 int lpc2xml_set_lpc(lpc2xml_context* context, const LpConfig *lpc) {
215 int lpc2xml_convert_file(lpc2xml_context* context, const char *filename) {
217 lpc2xml_context_clear_logs(context);
218 xmlSetGenericErrorFunc(context, lpc2xml_genericxml_error);
219 xmlSaveCtxtPtr save_ctx = xmlSaveToFilename(filename, "UTF-8", XML_SAVE_FORMAT);
220 if(save_ctx != NULL) {
221 ret = internal_convert_lpc2xml(context);
223 ret = xmlSaveDoc(save_ctx, context->doc);
225 lpc2xml_log(context, LPC2XML_ERROR, "Can't save document");
226 lpc2xml_log(context, LPC2XML_ERROR, "%s", context->errorBuffer);
229 xmlSaveClose(save_ctx);
231 lpc2xml_log(context, LPC2XML_ERROR, "Can't open file:%s", filename);
232 lpc2xml_log(context, LPC2XML_ERROR, "%s", context->errorBuffer);
237 int lpc2xml_convert_fd(lpc2xml_context* context, int fd) {
239 lpc2xml_context_clear_logs(context);
240 xmlSetGenericErrorFunc(context, lpc2xml_genericxml_error);
241 xmlSaveCtxtPtr save_ctx = xmlSaveToFd(fd, "UTF-8", XML_SAVE_FORMAT);
242 if(save_ctx != NULL) {
243 ret = internal_convert_lpc2xml(context);
245 ret = xmlSaveDoc(save_ctx, context->doc);
247 lpc2xml_log(context, LPC2XML_ERROR, "Can't save document");
248 lpc2xml_log(context, LPC2XML_ERROR, "%s", context->errorBuffer);
251 xmlSaveClose(save_ctx);
253 lpc2xml_log(context, LPC2XML_ERROR, "Can't open fd:%d", fd);
254 lpc2xml_log(context, LPC2XML_ERROR, "%s", context->errorBuffer);
259 int lpc2xml_convert_string(lpc2xml_context* context, char **content) {
261 xmlBufferPtr buffer = xmlBufferCreate();
262 lpc2xml_context_clear_logs(context);
263 xmlSetGenericErrorFunc(context, lpc2xml_genericxml_error);
264 xmlSaveCtxtPtr save_ctx = xmlSaveToBuffer(buffer, "UTF-8", XML_SAVE_FORMAT);
265 if(save_ctx != NULL) {
266 ret = internal_convert_lpc2xml(context);
268 ret = xmlSaveDoc(save_ctx, context->doc);
270 lpc2xml_log(context, LPC2XML_ERROR, "Can't save document");
271 lpc2xml_log(context, LPC2XML_ERROR, "%s", context->errorBuffer);
274 xmlSaveClose(save_ctx);
276 lpc2xml_log(context, LPC2XML_ERROR, "Can't initialize internal buffer");
277 lpc2xml_log(context, LPC2XML_ERROR, "%s", context->errorBuffer);
280 #if LIBXML_VERSION >= 20800
281 *content = (char *)xmlBufferDetach(buffer);
283 *content = strdup((const char *)xmlBufferContent(buffer));
286 xmlBufferFree(buffer);