xml_utils.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (C) 2005 iptelorg GmbH
  3. *
  4. * This file is part of ser, a free SIP server.
  5. *
  6. * ser is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * For a license to use the ser software under conditions
  12. * other than those described here, or to purchase support for this
  13. * software, please contact iptel.org by e-mail at the following addresses:
  14. * [email protected]
  15. *
  16. * ser is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <time.h>
  28. #include <libxml/parser.h>
  29. #include <libxml/tree.h>
  30. #include <cds/logger.h>
  31. #include <xcap/xml_utils.h>
  32. int xml_parser_flags = XML_PARSE_NOERROR | XML_PARSE_NOWARNING;
  33. static int str2int(const char *s, int *dst)
  34. {
  35. /* if not null sets the given integer to value */
  36. if (!s) {
  37. *dst = 0;
  38. return -1;
  39. }
  40. else *dst = atoi(s);
  41. return 0;
  42. }
  43. int get_int_attr(xmlNode *n, const char *attr_name, int *dst)
  44. {
  45. const char *s = get_attr_value(find_attr(n->properties, attr_name));
  46. if (!s) return 1;
  47. return str2int(s, dst);
  48. }
  49. int get_str_attr(xmlNode *n, const char *attr_name, str_t *dst)
  50. {
  51. const char *s = get_attr_value(find_attr(n->properties, attr_name));
  52. if (!s) {
  53. str_clear(dst);
  54. return 1;
  55. }
  56. else return str_dup_zt(dst, s);
  57. }
  58. int xmlstrcmp(const xmlChar *xmls, const char *name)
  59. {
  60. if (!xmls) return -1;
  61. if (!name) return 1;
  62. return strcmp((const char*)xmls, name);
  63. }
  64. /* xmlNode *find_node(xmlNode *parent, const char *name) */
  65. xmlNode *find_node(xmlNode *parent, const char *name, const char *nspace)
  66. {
  67. if (!parent) return NULL;
  68. xmlNode *n = parent->children;
  69. while (n) {
  70. if (cmp_node(n, name, nspace) >= 0) break;
  71. n = n->next;
  72. }
  73. return n;
  74. }
  75. const char *find_value(xmlNode *first_child)
  76. {
  77. const char *s = NULL;
  78. xmlNode *c = first_child;
  79. while (c) {
  80. if (c->type == XML_TEXT_NODE) {
  81. if (c->content) s = (const char *)c->content;
  82. break;
  83. }
  84. c = c->next;
  85. }
  86. return s;
  87. }
  88. const char *get_node_value(xmlNode *n)
  89. {
  90. if (!n) return NULL;
  91. return find_value(n->children);
  92. }
  93. xmlAttr *find_attr(xmlAttr *first, const char *name)
  94. {
  95. xmlAttr *a = first;
  96. while (a) {
  97. if (xmlstrcmp(a->name, name) == 0) break;
  98. a = a->next;
  99. }
  100. return a;
  101. }
  102. const char *get_attr_value(xmlAttr *a)
  103. {
  104. if (!a) return NULL;
  105. return find_value(a->children);
  106. }
  107. int cmp_node(xmlNode *node, const char *name, const char *nspace)
  108. {
  109. if (!node) return -1;
  110. if (node->type != XML_ELEMENT_NODE) return -1;
  111. if (xmlstrcmp(node->name, name) != 0) return -1;
  112. if (!nspace) return 0;
  113. if (!node->ns) {
  114. /* DEBUG_LOG("nemam NS!!!!!!!\n"); */
  115. return 1;
  116. }
  117. if (xmlstrcmp(node->ns->href, nspace) == 0) return 0;
  118. return -1;
  119. }
  120. time_t xmltime2time(const char *xt)
  121. {
  122. /* TODO: translate XML time in input parametr to time_t structure */
  123. ERROR_LOG("can't translate xmltime to time_t: not finished yet!\n");
  124. return 0;
  125. }