2 * XML wrapper for libxml2
3 * Copyright (c) 2012-2013, Qualcomm Atheros, Inc.
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
10 #define LIBXML_VALID_ENABLED
11 #include <libxml/tree.h>
12 #include <libxml/xmlschemastypes.h>
16 #include "xml-utils.h"
31 static void add_str(void *ctx_ptr, const char *fmt, ...)
33 struct str_buf *str = ctx_ptr;
38 n = os_realloc(str->buf, str->len + MAX_STR + 2);
44 len = vsnprintf(str->buf + str->len, MAX_STR, fmt, ap);
49 str->buf[str->len] = '\0';
53 int xml_validate(struct xml_node_ctx *ctx, xml_node_t *node,
54 const char *xml_schema_fname, char **ret_err)
58 xmlSchemaParserCtxtPtr pctx;
59 xmlSchemaValidCtxtPtr vctx;
62 struct str_buf errors;
67 doc = xmlNewDoc((xmlChar *) "1.0");
70 n = xmlDocCopyNode((xmlNodePtr) node, doc, 1);
75 xmlDocSetRootElement(doc, n);
77 os_memset(&errors, 0, sizeof(errors));
79 pctx = xmlSchemaNewParserCtxt(xml_schema_fname);
80 xmlSchemaSetParserErrors(pctx, (xmlSchemaValidityErrorFunc) add_str,
81 (xmlSchemaValidityWarningFunc) add_str,
83 schema = xmlSchemaParse(pctx);
84 xmlSchemaFreeParserCtxt(pctx);
86 vctx = xmlSchemaNewValidCtxt(schema);
87 xmlSchemaSetValidErrors(vctx, (xmlSchemaValidityErrorFunc) add_str,
88 (xmlSchemaValidityWarningFunc) add_str,
91 ret = xmlSchemaValidateDoc(vctx, doc);
92 xmlSchemaFreeValidCtxt(vctx);
94 xmlSchemaFree(schema);
101 *ret_err = errors.buf;
107 *ret_err = errors.buf;
115 int xml_validate_dtd(struct xml_node_ctx *ctx, xml_node_t *node,
116 const char *dtd_fname, char **ret_err)
123 struct str_buf errors;
128 doc = xmlNewDoc((xmlChar *) "1.0");
131 n = xmlDocCopyNode((xmlNodePtr) node, doc, 1);
136 xmlDocSetRootElement(doc, n);
138 os_memset(&errors, 0, sizeof(errors));
140 dtd = xmlParseDTD(NULL, (const xmlChar *) dtd_fname);
146 os_memset(&vctx, 0, sizeof(vctx));
147 vctx.userData = &errors;
148 vctx.error = add_str;
149 vctx.warning = add_str;
150 ret = xmlValidateDtd(&vctx, doc, dtd);
159 *ret_err = errors.buf;
167 void xml_node_free(struct xml_node_ctx *ctx, xml_node_t *node)
169 xmlFreeNode((xmlNodePtr) node);
173 xml_node_t * xml_node_get_parent(struct xml_node_ctx *ctx, xml_node_t *node)
175 return (xml_node_t *) ((xmlNodePtr) node)->parent;
179 xml_node_t * xml_node_from_buf(struct xml_node_ctx *ctx, const char *buf)
184 doc = xmlParseMemory(buf, strlen(buf));
187 node = xmlDocGetRootElement(doc);
188 node = xmlCopyNode(node, 1);
191 return (xml_node_t *) node;
195 const char * xml_node_get_localname(struct xml_node_ctx *ctx,
198 return (const char *) ((xmlNodePtr) node)->name;
202 char * xml_node_to_str(struct xml_node_ctx *ctx, xml_node_t *node)
207 xmlNodePtr n = (xmlNodePtr) node;
210 doc = xmlNewDoc((xmlChar *) "1.0");
211 n = xmlDocCopyNode(n, doc, 1);
212 xmlDocSetRootElement(doc, n);
213 xmlDocDumpFormatMemory(doc, &buf, &bufsiz, 0);
216 if (strncmp(pos, "<?xml", 5) == 0) {
217 pos = strchr(pos, '>');
220 while (pos && (*pos == '\r' || *pos == '\n'))
224 ret = os_strdup(pos);
235 while (pos >= ret && *pos == '\n')
243 void xml_node_detach(struct xml_node_ctx *ctx, xml_node_t *node)
245 xmlUnlinkNode((xmlNodePtr) node);
249 void xml_node_add_child(struct xml_node_ctx *ctx, xml_node_t *parent,
252 xmlAddChild((xmlNodePtr) parent, (xmlNodePtr) child);
256 xml_node_t * xml_node_create_root(struct xml_node_ctx *ctx, const char *ns_uri,
257 const char *ns_prefix,
258 xml_namespace_t **ret_ns, const char *name)
263 node = xmlNewNode(NULL, (const xmlChar *) name);
267 ns = xmlNewNs(node, (const xmlChar *) ns_uri,
268 (const xmlChar *) ns_prefix);
273 *ret_ns = (xml_namespace_t *) ns;
275 return (xml_node_t *) node;
279 xml_node_t * xml_node_create(struct xml_node_ctx *ctx, xml_node_t *parent,
280 xml_namespace_t *ns, const char *name)
283 node = xmlNewChild((xmlNodePtr) parent, (xmlNsPtr) ns,
284 (const xmlChar *) name, NULL);
285 return (xml_node_t *) node;
289 xml_node_t * xml_node_create_text(struct xml_node_ctx *ctx,
290 xml_node_t *parent, xml_namespace_t *ns,
291 const char *name, const char *value)
294 node = xmlNewTextChild((xmlNodePtr) parent, (xmlNsPtr) ns,
295 (const xmlChar *) name, (const xmlChar *) value);
296 return (xml_node_t *) node;
300 xml_node_t * xml_node_create_text_ns(struct xml_node_ctx *ctx,
301 xml_node_t *parent, const char *ns_uri,
302 const char *name, const char *value)
307 node = xmlNewTextChild((xmlNodePtr) parent, NULL,
308 (const xmlChar *) name, (const xmlChar *) value);
309 ns = xmlNewNs(node, (const xmlChar *) ns_uri, NULL);
311 return (xml_node_t *) node;
315 void xml_node_set_text(struct xml_node_ctx *ctx, xml_node_t *node,
318 /* TODO: escape XML special chars in value */
319 xmlNodeSetContent((xmlNodePtr) node, (xmlChar *) value);
323 int xml_node_add_attr(struct xml_node_ctx *ctx, xml_node_t *node,
324 xml_namespace_t *ns, const char *name, const char *value)
329 attr = xmlNewNsProp((xmlNodePtr) node, (xmlNsPtr) ns,
330 (const xmlChar *) name,
331 (const xmlChar *) value);
333 attr = xmlNewProp((xmlNodePtr) node, (const xmlChar *) name,
334 (const xmlChar *) value);
337 return attr ? 0 : -1;
341 char * xml_node_get_attr_value(struct xml_node_ctx *ctx, xml_node_t *node,
344 return (char *) xmlGetNoNsProp((xmlNodePtr) node,
345 (const xmlChar *) name);
349 char * xml_node_get_attr_value_ns(struct xml_node_ctx *ctx, xml_node_t *node,
350 const char *ns_uri, char *name)
352 return (char *) xmlGetNsProp((xmlNodePtr) node, (const xmlChar *) name,
353 (const xmlChar *) ns_uri);
357 void xml_node_get_attr_value_free(struct xml_node_ctx *ctx, char *val)
360 xmlFree((xmlChar *) val);
364 xml_node_t * xml_node_first_child(struct xml_node_ctx *ctx,
367 return (xml_node_t *) ((xmlNodePtr) parent)->children;
371 xml_node_t * xml_node_next_sibling(struct xml_node_ctx *ctx,
374 return (xml_node_t *) ((xmlNodePtr) node)->next;
378 int xml_node_is_element(struct xml_node_ctx *ctx, xml_node_t *node)
380 return ((xmlNodePtr) node)->type == XML_ELEMENT_NODE;
384 char * xml_node_get_text(struct xml_node_ctx *ctx, xml_node_t *node)
386 if (xmlChildElementCount((xmlNodePtr) node) > 0)
388 return (char *) xmlNodeGetContent((xmlNodePtr) node);
392 void xml_node_get_text_free(struct xml_node_ctx *ctx, char *val)
395 xmlFree((xmlChar *) val);
399 char * xml_node_get_base64_text(struct xml_node_ctx *ctx, xml_node_t *node,
406 txt = xml_node_get_text(ctx, node);
410 ret = base64_decode((unsigned char *) txt, strlen(txt), &len);
413 xml_node_get_text_free(ctx, txt);
416 txt = os_malloc(len + 1);
421 os_memcpy(txt, ret, len);
427 xml_node_t * xml_node_copy(struct xml_node_ctx *ctx, xml_node_t *node)
431 return (xml_node_t *) xmlCopyNode((xmlNodePtr) node, 1);
435 struct xml_node_ctx * xml_node_init_ctx(void *upper_ctx,
438 struct xml_node_ctx *xctx;
440 xctx = os_zalloc(sizeof(*xctx));
443 xctx->ctx = upper_ctx;
451 void xml_node_deinit_ctx(struct xml_node_ctx *ctx)
453 xmlSchemaCleanupTypes();