32 #include "asterisk/autoconfig.h"
34 #if defined(HAVE_LIBXML2)
35 #include <libxml/parser.h>
36 #include <libxml/tree.h>
37 #include <libxml/xinclude.h>
38 #include <libxml/xpath.h>
39 #include <libxml/xpathInternals.h>
42 #include <libxslt/xsltInternals.h>
43 #include <libxslt/transform.h>
44 #include <libxslt/xsltutils.h>
61 #ifdef HAVE_LIBXSLT_CLEANUP
83 static int process_xincludes(xmlDoc *doc)
88 res = xmlXIncludeProcess(doc);
102 doc = xmlReadFile(filename, NULL, XML_PARSE_RECOVER | XML_PARSE_NOENT);
108 if (process_xincludes(doc) < 0) {
115 xsltStylesheetPtr xslt = xsltLoadStylesheetPI(doc);
117 xmlDocPtr tmpdoc = xsltApplyStylesheet(xslt, doc, NULL);
118 xsltFreeStylesheet(xslt);
127 ast_log(LOG_NOTICE,
"XSLT support not found. XML documentation may be incomplete.\n");
131 xmlXPathOrderDocElems(doc);
133 return (
struct ast_xml_doc *) doc;
140 doc = xmlNewDoc((
const xmlChar *)
"1.0");
141 return (
struct ast_xml_doc *) doc;
151 node = xmlNewNode(NULL, (
const xmlChar *) name);
153 return (
struct ast_xml_node *) node;
160 if (!parent || !child_name) {
164 child = xmlNewChild((xmlNode *) parent, NULL, (
const xmlChar *) child_name, NULL);
165 return (
struct ast_xml_node *) child;
170 if (!parent || !child) {
173 return (
struct ast_xml_node *) xmlAddChild((xmlNode *) parent, (xmlNode *) child);
178 if (!parent || !child) {
181 return (
struct ast_xml_node *) xmlAddChildList((xmlNode *) parent, (xmlNode *) child);
189 return (
struct ast_xml_node *) xmlCopyNodeList((xmlNode *) list);
200 if (!(doc = xmlParseMemory(buffer, (
int) size))) {
202 if (process_xincludes(doc) < 0) {
208 return (
struct ast_xml_doc *) doc;
217 xmlFreeDoc((xmlDoc *) doc);
227 xmlDocSetRootElement((xmlDoc *) doc, (xmlNode *) node);
238 root_node = xmlDocGetRootElement((xmlDoc *) doc);
240 return (
struct ast_xml_node *) root_node;
249 xmlFreeNode((xmlNode *) node);
256 xmlFree((
char *) attribute);
263 xmlFree((
char *) text);
279 attrvalue = xmlGetProp((xmlNode *) node, (xmlChar *) attrname);
281 return (
const char *) attrvalue;
286 if (!name || !value) {
290 if (!xmlSetProp((xmlNode *) node, (xmlChar *) name, (xmlChar *) value)) {
297 struct ast_xml_node *
ast_xml_find_element(
struct ast_xml_node *root_node,
const char *name,
const char *attrname,
const char *attrvalue)
299 struct ast_xml_node *cur;
312 if (!attrname || !attrvalue) {
318 if (!strcmp(attr, attrvalue)) {
335 return (
struct ast_xml_doc *) ((xmlNode *)node)->doc;
338 struct ast_xml_ns *ast_xml_find_namespace(
struct ast_xml_doc *doc,
struct ast_xml_node *
node,
const char *ns_name) {
339 xmlNsPtr ns = xmlSearchNs((xmlDocPtr) doc, (xmlNodePtr) node, (xmlChar *) ns_name);
340 return (
struct ast_xml_ns *) ns;
345 return (
const char *) ((xmlNsPtr) ns)->prefix;
350 return (
const char *) ((xmlNsPtr) ns)->href;
359 return (
const char *) xmlNodeGetContent((xmlNode *) node);
364 if (!node || !content) {
368 xmlNodeSetContent((xmlNode *) node, (
const xmlChar *) content);
373 if (!node || !name) {
377 xmlNodeSetName((xmlNode *) node, (
const xmlChar *) name);
382 return xmlDocDump(output, (xmlDocPtr)doc);
387 xmlDocDumpFormatMemory((xmlDocPtr)doc, (xmlChar **)buffer, length, 1);
392 return (
const char *) ((xmlNode *) node)->name;
397 return (
struct ast_xml_node *) ((xmlNode *) node)->children;
402 return (
struct ast_xml_node *) ((xmlNode *) node)->next;
407 return (
struct ast_xml_node *) ((xmlNode *) node)->prev;
412 return (
struct ast_xml_node *) ((xmlNode *) node)->parent;
417 return (
struct ast_xml_node *) ((xmlXPathObjectPtr) results)->nodesetval->nodeTab[0];
422 return (
struct ast_xml_node *) ((xmlXPathObjectPtr) results)->nodesetval->nodeTab[i];
430 xmlXPathFreeObject((xmlXPathObjectPtr) results);
438 return ((xmlXPathObjectPtr) results)->nodesetval->nodeNr;
441 struct ast_xml_xpath_results *
ast_xml_query(
struct ast_xml_doc *doc,
const char *xpath_str)
443 xmlXPathContextPtr context;
444 xmlXPathObjectPtr result;
445 if (!(context = xmlXPathNewContext((xmlDoc *) doc))) {
446 ast_log(LOG_ERROR,
"Could not create XPath context!\n");
449 result = xmlXPathEvalExpression((xmlChar *) xpath_str, context);
450 xmlXPathFreeContext(context);
452 ast_log(LOG_WARNING,
"Error for query: %s\n", xpath_str);
455 if (xmlXPathNodeSetIsEmpty(result->nodesetval)) {
456 xmlXPathFreeObject(result);
457 ast_debug(5,
"No results for query: %s\n", xpath_str);
460 return (
struct ast_xml_xpath_results *) result;
466 xmlXPathContextPtr context;
467 xmlXPathObjectPtr result;
470 if (!(context = xmlXPathNewContext((xmlDoc *) doc))) {
471 ast_log(LOG_ERROR,
"Could not create XPath context!\n");
477 if (xmlXPathRegisterNs(context, (xmlChar *)ns.prefix,
478 (xmlChar *)ns.href) != 0) {
479 xmlXPathFreeContext(context);
480 ast_log(LOG_ERROR,
"Could not register namespace %s:%s\n",
486 result = xmlXPathEvalExpression((xmlChar *) xpath_str, context);
487 xmlXPathFreeContext(context);
489 ast_log(LOG_WARNING,
"Error for query: %s\n", xpath_str);
492 if (xmlXPathNodeSetIsEmpty(result->nodesetval)) {
493 xmlXPathFreeObject(result);
494 ast_debug(5,
"No results for query: %s\n", xpath_str);
497 return (
struct ast_xml_xpath_results *) result;
503 xsltStylesheet *xslt;
506 xml = xmlReadFile(filename, NULL, XML_PARSE_RECOVER | XML_PARSE_NOENT);
511 if (process_xincludes(xml) < 0) {
515 xmlXPathOrderDocElems(xml);
517 if (!(xslt = xsltParseStylesheetDoc(xml))) {
522 return (
struct ast_xslt_doc *) xslt;
527 xsltStylesheet *xslt;
534 doc = xmlReadMemory(buffer, (
int) size, NULL, NULL, XML_PARSE_RECOVER | XML_PARSE_NOENT);
539 if (process_xincludes(doc) < 0) {
544 if (!(xslt = xsltParseStylesheetDoc(doc))) {
549 return (
struct ast_xslt_doc *) xslt;
558 xsltFreeStylesheet((xsltStylesheet *) axslt);
561 struct ast_xml_doc *
ast_xslt_apply(
struct ast_xslt_doc *axslt,
struct ast_xml_doc *axml,
const char **params)
563 xsltStylesheet *xslt = (xsltStylesheet *)axslt;
564 xmlDoc *xml = (xmlDoc *)axml;
566 xsltTransformContextPtr ctxt;
568 int options = XSLT_PARSE_OPTIONS;
586 res = xsltApplyStylesheet(xslt, xml, params);
587 return (
struct ast_xml_doc *)res;
590 ctxt = xsltNewTransformContext(xslt, xml);
591 xsltSetCtxtParseOptions(ctxt, options);
593 for (ns = xslt->doc->children->nsDef; ns; ns = ns->next) {
594 if (xmlXPathRegisterNs(ctxt->xpathCtxt, ns->prefix, ns->href) != 0) {
595 xsltFreeTransformContext(ctxt);
600 res = xsltApplyStylesheetUser(xslt, xml, params, NULL, NULL, ctxt);
601 xmlXPathFreeContext(ctxt->xpathCtxt);
602 ctxt->xpathCtxt = NULL;
603 xsltFreeTransformContext(ctxt);
605 return (
struct ast_xml_doc *)res;
609 struct ast_xslt_doc *axslt)
611 return xsltSaveResultToString((xmlChar **)buffer, length, (xmlDoc *)result, (xsltStylesheet *)axslt);
const char * ast_xml_get_text(struct ast_xml_node *node)
Get an element content string.
Asterisk main include file. File version handling, generic pbx functions.
void ast_xml_free_text(const char *text)
Free a content element that was returned by ast_xml_get_text()
struct ast_xml_doc * ast_xml_new(void)
Create a XML document.
struct ast_xml_xpath_results * ast_xml_query(struct ast_xml_doc *doc, const char *xpath_str)
Execute an XPath query on an XML document.
const char * ast_xml_get_ns_prefix(struct ast_xml_ns *ns)
Get the prefix of a namespace.
void ast_xml_xpath_results_free(struct ast_xml_xpath_results *results)
Free the XPath results.
void ast_xml_set_text(struct ast_xml_node *node, const char *content)
Set an element content string.
int ast_xml_init(void)
Initialize the XML library implementation. This function is used to setup everything needed to start ...
struct ast_xml_node * ast_xml_new_child(struct ast_xml_node *parent, const char *child_name)
Add a child node inside a passed parent node.
int ast_xml_xpath_num_results(struct ast_xml_xpath_results *results)
Return the number of results from an XPath query.
struct ast_xml_node * ast_xml_copy_node_list(struct ast_xml_node *list)
Create a copy of a n ode list.
const char * ast_xml_get_ns_href(struct ast_xml_ns *ns)
Get the href of a namespace.
void ast_xml_close(struct ast_xml_doc *doc)
Close an already open document and free the used structure.
void ast_xml_free_attr(const char *attribute)
Free an attribute returned by ast_xml_get_attribute()
struct ast_xml_xpath_results * ast_xml_query_with_namespaces(struct ast_xml_doc *doc, const char *xpath_str, struct ast_xml_namespace_def_vector *namespaces)
Execute an XPath query on an XML document with namespaces.
int ast_xml_doc_dump_file(FILE *output, struct ast_xml_doc *doc)
Dump the specified document to a file.
const char * ast_xml_get_attribute(struct ast_xml_node *node, const char *attrname)
Get a node attribute by name.
const char * ast_xml_node_get_name(struct ast_xml_node *node)
Get the name of a node.
void ast_xslt_close(struct ast_xslt_doc *axslt)
Close a stylesheet document and free its resources.
struct ast_xml_node * ast_xml_add_child(struct ast_xml_node *parent, struct ast_xml_node *child)
Add a child node, to a specified parent node.
struct ast_xml_node * ast_xml_xpath_get_result(struct ast_xml_xpath_results *results, int i)
Return a specific result node of an XPath query.
struct ast_xml_node * ast_xml_node_get_next(struct ast_xml_node *node)
Get the next node in the same level.
#define ast_debug(level,...)
Log a DEBUG message.
struct ast_xml_node * ast_xml_node_get_children(struct ast_xml_node *node)
Get the node's children.
Asterisk XML abstraction layer.
void ast_xml_set_root(struct ast_xml_doc *doc, struct ast_xml_node *node)
Specify the root node of a XML document.
struct ast_xml_doc * ast_xml_get_doc(struct ast_xml_node *node)
Get the document based on a node.
struct ast_xml_node * ast_xml_node_get_prev(struct ast_xml_node *node)
Get the previous node in the same leve.
void ast_xml_doc_dump_memory(struct ast_xml_doc *doc, char **buffer, int *length)
Dump the specified document to a buffer.
struct ast_xml_node * ast_xml_xpath_get_first_result(struct ast_xml_xpath_results *results)
Return the first result node of an XPath query.
Support for logging to various files, console and syslog Configuration in file logger.conf.
struct ast_xml_node * ast_xml_new_node(const char *name)
Create a XML node.
struct ast_xml_doc * ast_xml_read_memory(char *buffer, size_t size)
Open an XML document that resides in memory.
int ast_xml_set_attribute(struct ast_xml_node *node, const char *name, const char *value)
Set an attribute to a node.
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
struct ast_xml_node * ast_xml_node_get_parent(struct ast_xml_node *node)
Get the parent of a specified node.
struct ast_xml_node * ast_xml_add_child_list(struct ast_xml_node *parent, struct ast_xml_node *child)
Add a list of child nodes, to a specified parent node.
void ast_xml_set_name(struct ast_xml_node *node, const char *name)
Set or reset an element's name.
struct ast_xml_node * ast_xml_get_root(struct ast_xml_doc *doc)
Get the document root node.
struct ast_xml_doc * ast_xslt_apply(struct ast_xslt_doc *axslt, struct ast_xml_doc *axml, const char **params)
Apply an XSLT stylesheet to an XML document.
struct ast_xslt_doc * ast_xslt_open(char *filename)
Open an XSLT document.
struct ast_xml_doc * ast_xml_open(char *filename)
Open an XML document.
int ast_xslt_save_result_to_string(char **buffer, int *length, struct ast_xml_doc *result, struct ast_xslt_doc *axslt)
Save the results of applying a stylesheet to a string.
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
void ast_xml_free_node(struct ast_xml_node *node)
Free node.
struct ast_xslt_doc * ast_xslt_read_memory(char *buffer, size_t size)
Open an XSLT document that resides in memory.
struct ast_xml_node * ast_xml_find_element(struct ast_xml_node *root_node, const char *name, const char *attrname, const char *attrvalue)
Find a node element by name.
int ast_xml_finish(void)
Cleanup library allocated global data.