| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
#ifndef __OS_solaris |
| 40 |
#define _XOPEN_SOURCE 600 |
| 41 |
#define _BSD_SOURCE \ |
| 42 |
1 |
| 43 |
|
| 44 |
|
| 45 |
#define _DEFAULT_SOURCE 1 |
| 46 |
#define _DARWIN_C_SOURCE 1 |
| 47 |
#else |
| 48 |
#define _XOPEN_SOURCE_EXTENDED 1 |
| 49 |
#endif |
| 50 |
|
| 51 |
#include <time.h> |
| 52 |
|
| 53 |
#undef _XOPEN_SOURCE |
| 54 |
#undef _XOPEN_SOURCE_EXTENDED |
| 55 |
|
| 56 |
#include <string.h> |
| 57 |
#include <stdlib.h> |
| 58 |
#include <libxml/parser.h> |
| 59 |
|
| 60 |
#include "../../core/mem/mem.h" |
| 61 |
#include "../../core/dprint.h" |
| 62 |
|
| 63 |
#include "pidf.h" |
| 64 |
|
| 65 |
xmlAttrPtr xmlNodeGetAttrByName(xmlNodePtr node, const char *name) |
| 66 |
{ |
| 67 |
xmlAttrPtr attr = node->properties; |
| 68 |
while(attr) { |
| 69 |
if(xmlStrcasecmp(attr->name, (unsigned char *)name) == 0) |
| 70 |
return attr; |
| 71 |
attr = attr->next; |
| 72 |
} |
| 73 |
return NULL; |
| 74 |
} |
| 75 |
|
| 76 |
char *xmlNodeGetAttrContentByName(xmlNodePtr node, const char *name) |
| 77 |
{ |
| 78 |
xmlAttrPtr attr = xmlNodeGetAttrByName(node, name); |
| 79 |
if(attr) |
| 80 |
return (char *)xmlNodeGetContent(attr->children); |
| 81 |
else |
| 82 |
return NULL; |
| 83 |
} |
| 84 |
|
| 85 |
xmlNodePtr xmlNodeGetChildByName(xmlNodePtr node, const char *name) |
| 86 |
{ |
| 87 |
xmlNodePtr cur = node->children; |
| 88 |
while(cur) { |
| 89 |
if(xmlStrcasecmp(cur->name, (unsigned char *)name) == 0) |
| 90 |
return cur; |
| 91 |
cur = cur->next; |
| 92 |
} |
| 93 |
return NULL; |
| 94 |
} |
| 95 |
|
| 96 |
xmlNodePtr xmlNodeGetNodeByName( |
| 97 |
xmlNodePtr node, const char *name, const char *ns) |
| 98 |
{ |
| 99 |
xmlNodePtr cur = node; |
| 100 |
while(cur) { |
| 101 |
xmlNodePtr match = NULL; |
| 102 |
if(xmlStrcasecmp(cur->name, (unsigned char *)name) == 0) { |
| 103 |
if(!ns |
| 104 |
|| (cur->ns |
| 105 |
&& xmlStrcasecmp( |
| 106 |
cur->ns->prefix, (unsigned char *)ns) |
| 107 |
== 0)) |
| 108 |
return cur; |
| 109 |
} |
| 110 |
match = xmlNodeGetNodeByName(cur->children, name, ns); |
| 111 |
if(match) |
| 112 |
return match; |
| 113 |
cur = cur->next; |
| 114 |
} |
| 115 |
return NULL; |
| 116 |
} |
| 117 |
|
| 118 |
char *xmlNodeGetNodeContentByName( |
| 119 |
xmlNodePtr root, const char *name, const char *ns) |
| 120 |
{ |
| 121 |
xmlNodePtr node = xmlNodeGetNodeByName(root, name, ns); |
| 122 |
if(node) |
| 123 |
return (char *)xmlNodeGetContent(node->children); |
| 124 |
else |
| 125 |
return NULL; |
| 126 |
} |
| 127 |
|
| 128 |
xmlNodePtr xmlDocGetNodeByName(xmlDocPtr doc, const char *name, const char *ns) |
| 129 |
{ |
| 130 |
xmlNodePtr cur = doc->children; |
| 131 |
if(cur) |
| 132 |
return xmlNodeGetNodeByName(cur, name, ns); |
| 133 |
else |
| 134 |
return NULL; |
| 135 |
} |
| 136 |
|
| 137 |
char *xmlDocGetNodeContentByName( |
| 138 |
xmlDocPtr doc, const char *name, const char *ns) |
| 139 |
{ |
| 140 |
xmlNodePtr node = xmlDocGetNodeByName(doc, name, ns); |
| 141 |
if(node) |
| 142 |
return (char *)xmlNodeGetContent(node->children); |
| 143 |
else |
| 144 |
return NULL; |
| 145 |
} |
| 146 |
|
| 147 |
xmlXPathObjectPtr xmlGetNodeSet(xmlDocPtr doc, xmlChar *xpath, xmlChar *ns) |
| 148 |
{ |
| 149 |
|
| 150 |
xmlXPathContextPtr context = NULL; |
| 151 |
xmlXPathObjectPtr result = NULL; |
| 152 |
|
| 153 |
context = xmlXPathNewContext(doc); |
| 154 |
if(context == NULL) { |
| 155 |
return NULL; |
| 156 |
} |
| 157 |
|
| 158 |
if((ns != NULL) && (xmlRegisterNamespaces(context, ns) < 0)) { |
| 159 |
xmlXPathFreeContext(context); |
| 160 |
return NULL; |
| 161 |
} |
| 162 |
|
| 163 |
result = xmlXPathEvalExpression(xpath, context); |
| 164 |
xmlXPathFreeContext(context); |
| 165 |
|
| 166 |
if(result == NULL) { |
| 167 |
LM_ERR("xmlXPathEvalExpression() failed\n"); |
| 168 |
return NULL; |
| 169 |
} |
| 170 |
if(xmlXPathNodeSetIsEmpty(result->nodesetval)) { |
| 171 |
xmlXPathFreeObject(result); |
| 172 |
LM_DBG("xmlXPathEvalExpression() returned no result\n"); |
| 173 |
return NULL; |
| 174 |
} |
| 175 |
|
| 176 |
return result; |
| 177 |
} |
| 178 |
|
| 179 |
int xmlRegisterNamespaces(xmlXPathContextPtr context, const xmlChar *ns) |
| 180 |
{ |
| 181 |
xmlChar *nsListDup; |
| 182 |
xmlChar *prefix; |
| 183 |
xmlChar *href; |
| 184 |
xmlChar *next; |
| 185 |
|
| 186 |
nsListDup = xmlStrdup(ns); |
| 187 |
if(nsListDup == NULL) { |
| 188 |
return -1; |
| 189 |
} |
| 190 |
|
| 191 |
next = nsListDup; |
| 192 |
while(next != NULL) { |
| 193 |
|
| 194 |
while((*next) == ' ') |
| 195 |
next++; |
| 196 |
if((*next) == '\0') |
| 197 |
break; |
| 198 |
|
| 199 |
|
| 200 |
prefix = next; |
| 201 |
next = (xmlChar *)xmlStrchr(next, '='); |
| 202 |
if(next == NULL) { |
| 203 |
xmlFree(nsListDup); |
| 204 |
return -1; |
| 205 |
} |
| 206 |
*(next++) = '\0'; |
| 207 |
|
| 208 |
|
| 209 |
href = next; |
| 210 |
next = (xmlChar *)xmlStrchr(next, ' '); |
| 211 |
if(next != NULL) { |
| 212 |
*(next++) = '\0'; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
if(xmlXPathRegisterNs(context, prefix, href) != 0) { |
| 217 |
xmlFree(nsListDup); |
| 218 |
return -1; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
xmlFree(nsListDup); |
| 223 |
return 0; |
| 224 |
} |