xml2lpc.c 10.7 KB
Newer Older
Yann Diorcet's avatar
Yann Diorcet committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
linphone
Copyright (C) 2012 Belledonne Communications SARL
Yann DIORCET (yann.diorcet@linphone.org)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Yann Diorcet's avatar
Yann Diorcet committed
19 20
*/

21
#include "xml2lpc.h"
Yann Diorcet's avatar
Yann Diorcet committed
22 23 24 25 26 27 28 29 30 31
#include <string.h>
#include <libxml/xmlreader.h>


#define XML2LPC_BZ 2048

struct _xml2lpc_context {
	LpConfig *lpc;
	xml2lpc_function cbf;
	void *ctx;
32

Yann Diorcet's avatar
Yann Diorcet committed
33 34 35 36 37 38 39 40 41 42 43 44 45
	xmlDoc *doc;
	xmlDoc *xsd;
	char errorBuffer[XML2LPC_BZ];
	char warningBuffer[XML2LPC_BZ];
};


xml2lpc_context* xml2lpc_context_new(xml2lpc_function cbf, void *ctx) {
	xml2lpc_context *xmlCtx = (xml2lpc_context*)malloc(sizeof(xml2lpc_context));
	if(xmlCtx != NULL) {
		xmlCtx->lpc = NULL;
		xmlCtx->cbf = cbf;
		xmlCtx->ctx = ctx;
46

Yann Diorcet's avatar
Yann Diorcet committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
		xmlCtx->doc = NULL;
		xmlCtx->xsd = NULL;
		xmlCtx->errorBuffer[0]='\0';
		xmlCtx->warningBuffer[0]='\0';
	}
	return xmlCtx;
}

void xml2lpc_context_destroy(xml2lpc_context *ctx) {
	if(ctx->doc != NULL) {
		xmlFreeDoc(ctx->doc);
		ctx->doc = NULL;
	}
	if(ctx->xsd != NULL) {
		xmlFreeDoc(ctx->xsd);
		ctx->xsd = NULL;
	}
64 65 66 67 68

	// TODO Fix me, xml2 isn't robust for use with multiple context
	// Unset callback to not disturb other user of the xml lib
	xmlSetGenericErrorFunc(NULL, NULL);

Yann Diorcet's avatar
Yann Diorcet committed
69 70 71
	free(ctx);
}

Yann Diorcet's avatar
Yann Diorcet committed
72
static void xml2lpc_context_clear_logs(xml2lpc_context *ctx) {
Yann Diorcet's avatar
Yann Diorcet committed
73 74 75 76
	ctx->errorBuffer[0]='\0';
	ctx->warningBuffer[0]='\0';
}

77
static void xml2lpc_log(xml2lpc_context *xmlCtx, xml2lpc_log_level level, const char *fmt, ...) {
78 79
	va_list args;
	va_start(args, fmt);
Yann Diorcet's avatar
Yann Diorcet committed
80 81 82
	if(xmlCtx->cbf != NULL) {
		xmlCtx->cbf((xmlCtx)->ctx, level, fmt, args);
	}
83
	va_end(args);
Yann Diorcet's avatar
Yann Diorcet committed
84 85
}

86
static void xml2lpc_genericxml_error(void *ctx, const char *fmt, ...) {
Yann Diorcet's avatar
Yann Diorcet committed
87
	xml2lpc_context *xmlCtx = (xml2lpc_context *)ctx;
88
	size_t sl = strlen(xmlCtx->errorBuffer);
89 90
	va_list args;
	va_start(args, fmt);
Yann Diorcet's avatar
Yann Diorcet committed
91 92 93 94
	vsnprintf(xmlCtx->errorBuffer + sl, XML2LPC_BZ-sl, fmt, args);
	va_end(args);
}

95
static void xml2lpc_genericxml_warning(void *ctx, const char *fmt, ...) {
Yann Diorcet's avatar
Yann Diorcet committed
96
	xml2lpc_context *xmlCtx = (xml2lpc_context *)ctx;
97
	size_t sl = strlen(xmlCtx->warningBuffer);
98 99
	va_list args;
	va_start(args, fmt);
Yann Diorcet's avatar
Yann Diorcet committed
100 101 102 103
	vsnprintf(xmlCtx->warningBuffer + sl, XML2LPC_BZ-sl, fmt, args);
	va_end(args);
}

Yann Diorcet's avatar
Yann Diorcet committed
104
#if 0
Yann Diorcet's avatar
Yann Diorcet committed
105
static void dumpNodes(int level, xmlNode * a_node, xml2lpc_context *ctx) {
106
	xmlNode *cur_node = NULL;
Yann Diorcet's avatar
Yann Diorcet committed
107

108 109 110 111 112 113
	for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
		if (cur_node->type == XML_ELEMENT_NODE) {
			xml2lpc_log(ctx, XML2LPC_DEBUG, "node level: %d type: Element, name: %s", level, cur_node->name);
		} else {
			xml2lpc_log(ctx, XML2LPC_DEBUG, "node level: %d type: %d, name: %s", level, cur_node->type, cur_node->name);
		}
Yann Diorcet's avatar
Yann Diorcet committed
114

115 116
		dumpNodes(level + 1, cur_node->children, ctx);
	}
Yann Diorcet's avatar
Yann Diorcet committed
117
}
Yann Diorcet's avatar
Yann Diorcet committed
118
#endif
Yann Diorcet's avatar
Yann Diorcet committed
119 120 121


static void dumpNode(xmlNode *node, xml2lpc_context *ctx) {
122
	xml2lpc_log(ctx, XML2LPC_DEBUG, "node type: %d, name: %s", node->type, node->name);
Yann Diorcet's avatar
Yann Diorcet committed
123 124 125
}

static void dumpAttr(xmlNode *node, xml2lpc_context *ctx) {
126
	xml2lpc_log(ctx, XML2LPC_DEBUG, "attr name: %s value:%s", node->name, node->children->content);
Yann Diorcet's avatar
Yann Diorcet committed
127 128 129
}

static void dumpContent(xmlNode *node, xml2lpc_context *ctx) {
130 131 132 133
	if (node->children)
		xml2lpc_log(ctx, XML2LPC_DEBUG, "content: %s", node->children->content);
	else
		xml2lpc_log(ctx, XML2LPC_DEBUG, "content: ");
Yann Diorcet's avatar
Yann Diorcet committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
}

static int processEntry(xmlElement *element, const char *sectionName, xml2lpc_context *ctx) {
	xmlNode *cur_attr = NULL;
	const char *name = NULL;
	const char *value = NULL;
	bool_t overwrite = FALSE;

	for (cur_attr = (xmlNode *)element->attributes; cur_attr; cur_attr = cur_attr->next) {
		dumpAttr(cur_attr, ctx);
		if(strcmp((const char*)cur_attr->name, "name") == 0) {
			name = (const char*)cur_attr->children->content;
		} else if(strcmp((const char*)cur_attr->name, "overwrite") == 0) {
			if(strcmp((const char*)cur_attr->children->content, "true") == 0) {
				overwrite = TRUE;
			}
		}
	}

	dumpContent((xmlNode *)element, ctx);
154 155 156 157
	if (element->children)
		value = (const char *)element->children->content;
	else
		value = "";
Yann Diorcet's avatar
Yann Diorcet committed
158 159 160 161

	if(name != NULL) {
		const char *str = lp_config_get_string(ctx->lpc, sectionName, name, NULL);
		if(str == NULL || overwrite) {
Yann Diorcet's avatar
Yann Diorcet committed
162
			xml2lpc_log(ctx, XML2LPC_MESSAGE, "Set %s|%s = %s", sectionName, name, value);
Yann Diorcet's avatar
Yann Diorcet committed
163 164 165 166 167 168 169
			lp_config_set_string(ctx->lpc, sectionName, name, value);
		} else {
			xml2lpc_log(ctx, XML2LPC_MESSAGE, "Don't touch %s|%s = %s",sectionName, name, str);
		}
	} else {
		xml2lpc_log(ctx, XML2LPC_WARNING, "ignored entry with no \"name\" attribute line:%d",xmlGetLineNo((xmlNode*)element));
	}
170
		return 0;
Yann Diorcet's avatar
Yann Diorcet committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
}

static int processSection(xmlElement *element, xml2lpc_context *ctx) {
	xmlNode *cur_node = NULL;
	xmlNode *cur_attr = NULL;
	const char *name = NULL;

	for (cur_attr = (xmlNode *)element->attributes; cur_attr; cur_attr = cur_attr->next) {
		dumpAttr(cur_attr, ctx);
		if(strcmp((const char*)cur_attr->name, "name") == 0) {
			name = (const char*)cur_attr->children->content;
		}
	}

	if(name != NULL) {
		for (cur_node = element->children; cur_node; cur_node = cur_node->next) {
			dumpNode(cur_node, ctx);
			if (cur_node->type == XML_ELEMENT_NODE) {
				if(strcmp((const char*)cur_node->name, "entry") == 0 ) {
					processEntry((xmlElement*)cur_node, name, ctx);
				}
			}
193 194 195 196

		}
		} else {
			xml2lpc_log(ctx, XML2LPC_WARNING, "ignored section with no \"name\" attribute, line:%d", xmlGetLineNo((xmlNode*)element));
Yann Diorcet's avatar
Yann Diorcet committed
197 198
		}

199
		return 0;
Yann Diorcet's avatar
Yann Diorcet committed
200 201 202 203 204 205 206
}

static int processConfig(xmlElement *element, xml2lpc_context *ctx) {
	xmlNode *cur_node = NULL;

	for (cur_node = element->children; cur_node; cur_node = cur_node->next) {
		dumpNode(cur_node, ctx);
207
		if (cur_node->type == XML_ELEMENT_NODE &&
Yann Diorcet's avatar
Yann Diorcet committed
208 209
			strcmp((const char*)cur_node->name, "section") == 0 ) {
			processSection((xmlElement*)cur_node, ctx);
210 211 212
			}

		}
Yann Diorcet's avatar
Yann Diorcet committed
213 214 215 216 217
	return 0;
}

static int processDoc(xmlNode *node, xml2lpc_context *ctx) {
	dumpNode(node, ctx);
218 219

	if (node->type == XML_ELEMENT_NODE &&
Yann Diorcet's avatar
Yann Diorcet committed
220 221 222 223 224 225 226 227
		strcmp((const char*)node->name, "config") == 0 ) {
		processConfig((xmlElement*)node, ctx);
	} else {
		xml2lpc_log(ctx, XML2LPC_WARNING, "root element is not \"config\", line:%d", xmlGetLineNo(node));
	}
	return 0;
}

228
static int internal_convert_xml2lpc(xml2lpc_context *ctx) {
229 230 231
	xmlNode *rootNode;
	int ret;

Yann Diorcet's avatar
Yann Diorcet committed
232
	xml2lpc_log(ctx, XML2LPC_DEBUG, "Parse started");
233
	rootNode = xmlDocGetRootElement(ctx->doc);
Yann Diorcet's avatar
Yann Diorcet committed
234
	//dumpNodes(0, rootNode, cbf, ctx);
235
	ret = processDoc(rootNode, ctx);
Yann Diorcet's avatar
Yann Diorcet committed
236 237 238 239 240 241
	xml2lpc_log(ctx, XML2LPC_DEBUG, "Parse ended ret:%d", ret);
	return ret;
}

int xml2lpc_validate(xml2lpc_context *xmlCtx) {
	xmlSchemaValidCtxtPtr validCtx;
242 243 244 245 246
	xmlSchemaParserCtxtPtr parserCtx;
	int ret;

	xml2lpc_context_clear_logs(xmlCtx);
	parserCtx = xmlSchemaNewDocParserCtxt(xmlCtx->xsd);
Yann Diorcet's avatar
Yann Diorcet committed
247 248
	validCtx = xmlSchemaNewValidCtxt(xmlSchemaParse(parserCtx));
	xmlSchemaSetValidErrors(validCtx, xml2lpc_genericxml_error, xml2lpc_genericxml_warning, xmlCtx);
249
	ret = xmlSchemaValidateDoc(validCtx, xmlCtx->doc);
250
	if(ret > 0) {
Yann Diorcet's avatar
Yann Diorcet committed
251 252 253 254
		if(strlen(xmlCtx->warningBuffer) > 0)
			xml2lpc_log(xmlCtx, XML2LPC_WARNING, "%s", xmlCtx->warningBuffer);
		if(strlen(xmlCtx->errorBuffer) > 0)
			xml2lpc_log(xmlCtx, XML2LPC_ERROR, "%s", xmlCtx->errorBuffer);
255
	} else if(ret < 0) {
Yann Diorcet's avatar
Yann Diorcet committed
256 257 258 259 260 261 262 263
		xml2lpc_log(xmlCtx, XML2LPC_ERROR, "Internal error");
	}
	xmlSchemaFreeValidCtxt(validCtx);
	return ret;
}

int xml2lpc_convert(xml2lpc_context *xmlCtx, LpConfig *lpc) {
	xml2lpc_context_clear_logs(xmlCtx);
Yann Diorcet's avatar
Yann Diorcet committed
264 265 266 267 268 269 270
	if(xmlCtx->doc == NULL) {
		xml2lpc_log(xmlCtx, XML2LPC_ERROR, "No doc set");
		return -1;
	}
	if(lpc == NULL) {
		xml2lpc_log(xmlCtx, XML2LPC_ERROR, "Invalid lpc");
	}
Yann Diorcet's avatar
Yann Diorcet committed
271
	xmlCtx->lpc = lpc;
272
	return internal_convert_xml2lpc(xmlCtx);
Yann Diorcet's avatar
Yann Diorcet committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
}

int xml2lpc_set_xml_file(xml2lpc_context* xmlCtx, const char *filename) {
	xml2lpc_context_clear_logs(xmlCtx);
	xmlSetGenericErrorFunc(xmlCtx, xml2lpc_genericxml_error);
	if(xmlCtx->doc != NULL) {
		xmlFreeDoc(xmlCtx->doc);
		xmlCtx->doc = NULL;
	}
	xmlCtx->doc = xmlReadFile(filename, NULL, 0);
	if(xmlCtx->doc == NULL) {
		xml2lpc_log(xmlCtx, XML2LPC_ERROR, "Can't open/parse file \"%s\"", filename);
		xml2lpc_log(xmlCtx, XML2LPC_ERROR, "%s", xmlCtx->errorBuffer);
		return -1;
	}
	return 0;
}

int xml2lpc_set_xml_fd(xml2lpc_context* xmlCtx, int fd) {
	xml2lpc_context_clear_logs(xmlCtx);
	xmlSetGenericErrorFunc(xmlCtx, xml2lpc_genericxml_error);
	if(xmlCtx->doc != NULL) {
		xmlFreeDoc(xmlCtx->doc);
		xmlCtx->doc = NULL;
	}
	xmlCtx->doc = xmlReadFd(fd, 0, NULL, 0);
	if(xmlCtx->doc == NULL) {
		xml2lpc_log(xmlCtx, XML2LPC_ERROR, "Can't open/parse fd \"%d\"", fd);
		xml2lpc_log(xmlCtx, XML2LPC_ERROR, "%s", xmlCtx->errorBuffer);
		return -1;
	}
	return 0;
}

int xml2lpc_set_xml_string(xml2lpc_context* xmlCtx, const char *content) {
	xml2lpc_context_clear_logs(xmlCtx);
	xmlSetGenericErrorFunc(xmlCtx, xml2lpc_genericxml_error);
	if(xmlCtx->doc != NULL) {
		xmlFreeDoc(xmlCtx->doc);
		xmlCtx->doc = NULL;
	}
	xmlCtx->doc = xmlReadDoc((const unsigned char*)content, 0, NULL, 0);
	if(xmlCtx->doc == NULL) {
		xml2lpc_log(xmlCtx, XML2LPC_ERROR, "Can't parse string");
		xml2lpc_log(xmlCtx, XML2LPC_ERROR, "%s", xmlCtx->errorBuffer);
		return -1;
	}
	return 0;
}

int xml2lpc_set_xsd_file(xml2lpc_context* xmlCtx, const char *filename) {
	xml2lpc_context_clear_logs(xmlCtx);
	xmlSetGenericErrorFunc(xmlCtx, xml2lpc_genericxml_error);
	if(xmlCtx->xsd != NULL) {
		xmlFreeDoc(xmlCtx->xsd);
		xmlCtx->xsd = NULL;
	}
	xmlCtx->xsd = xmlReadFile(filename, NULL, 0);
	if(xmlCtx->xsd == NULL) {
		xml2lpc_log(xmlCtx, XML2LPC_ERROR, "Can't open/parse file \"%s\"", filename);
		xml2lpc_log(xmlCtx, XML2LPC_ERROR, "%s", xmlCtx->errorBuffer);
		return -1;
	}
	return 0;
}

int xml2lpc_set_xsd_fd(xml2lpc_context* xmlCtx, int fd) {
	xml2lpc_context_clear_logs(xmlCtx);
	xmlSetGenericErrorFunc(xmlCtx, xml2lpc_genericxml_error);
	if(xmlCtx->xsd != NULL) {
		xmlFreeDoc(xmlCtx->xsd);
		xmlCtx->xsd = NULL;
	}
	xmlCtx->xsd = xmlReadFd(fd, 0, NULL, 0);
	if(xmlCtx->xsd == NULL) {
		xml2lpc_log(xmlCtx, XML2LPC_ERROR, "Can't open/parse fd \"%d\"", fd);
		xml2lpc_log(xmlCtx, XML2LPC_ERROR, "%s", xmlCtx->errorBuffer);
		return -1;
	}
	return 0;
}

int xml2lpc_set_xsd_string(xml2lpc_context* xmlCtx, const char *content) {
	xml2lpc_context_clear_logs(xmlCtx);
	xmlSetGenericErrorFunc(xmlCtx, xml2lpc_genericxml_error);
	if(xmlCtx->xsd != NULL) {
		xmlFreeDoc(xmlCtx->xsd);
		xmlCtx->xsd = NULL;
	}
	xmlCtx->xsd = xmlReadDoc((const unsigned char*)content, 0, NULL, 0);
	if(xmlCtx->xsd == NULL) {
		xml2lpc_log(xmlCtx, XML2LPC_ERROR, "Can't parse string");
		xml2lpc_log(xmlCtx, XML2LPC_ERROR, "%s", xmlCtx->errorBuffer);
		return -1;
	}
	return 0;
Yann Diorcet's avatar
Yann Diorcet committed
369
}