removing signedness warnings coming from libxml2
[OpenSRF.git] / src / libopensrf / xml_utils.c
1 #include <opensrf/xml_utils.h>
2
3
4 void recurse_doc( xmlNodePtr node ) {
5         if( node == NULL ) return;
6         printf("Recurse: %s =>  %s", node->name, node->content );
7         xmlNodePtr t = node->children;
8         while(t) {
9                 recurse_doc(t);
10                 t = t->next;
11         }
12 }
13
14
15
16 jsonObject* xmlDocToJSON(xmlDocPtr doc) {
17         if(!doc) return NULL;
18         return _xmlToJSON(xmlDocGetRootElement(doc), NULL);
19 }
20
21 jsonObject* _xmlToJSON(xmlNodePtr node, jsonObject* obj) {
22
23         if(!node) return NULL;
24         if(xmlIsBlankNode(node)) return NULL;
25         if(obj == NULL) obj = jsonNewObject(NULL);
26
27         if(node->type == XML_TEXT_NODE) {
28                 jsonObjectSetString(obj, (char*) node->content);        
29
30         } else if(node->type == XML_ELEMENT_NODE || node->type == XML_ATTRIBUTE_NODE ) {
31
32                 jsonObject* new_obj = jsonNewObject(NULL);
33
34                 jsonObject* old;
35
36                 /* do the duplicate node / array shuffle */
37                 if( (old = jsonObjectGetKey(obj, (char*) node->name)) ) {
38                         if(old->type == JSON_ARRAY ) {
39                                 jsonObjectPush(old, new_obj);
40                         } else {
41                                 jsonObject* arr = jsonNewObject(NULL);
42                                 jsonObjectPush(arr, jsonObjectClone(old));
43                                 jsonObjectPush(arr, new_obj);
44                                 jsonObjectSetKey(obj, (char*) node->name, arr);
45                         }
46                 } else {
47                         jsonObjectSetKey(obj, (char*) node->name, new_obj);
48                 }
49
50                 xmlNodePtr child = node->children;
51                 if (child) { // at least one...
52                         if (child != node->last) { // more than one -- ignore TEXT nodes
53                                 while(child) {
54                                         if (child->type != XML_TEXT_NODE) _xmlToJSON(child, new_obj);
55                                         child = child->next;
56                                 }
57                         } else {
58                                 _xmlToJSON(child, new_obj);
59                         }
60                 }
61         }       
62
63         return obj;
64 }
65
66
67 char* xmlDocToString(xmlDocPtr doc, int full) {
68
69         if(!doc) return NULL;
70
71         char* xml;
72
73         if(full) {
74
75                 xmlChar* xmlbuf;
76                 int size;
77                 xmlDocDumpMemory(doc, &xmlbuf, &size);
78                 xml = strdup((char*) (xmlbuf));
79                 xmlFree(xmlbuf);
80                 return xml;
81
82         } else {
83
84                 xmlBufferPtr xmlbuf = xmlBufferCreate();
85                 xmlNodeDump( xmlbuf, doc, xmlDocGetRootElement(doc), 0, 0);
86                 xml = strdup((char*) (xmlBufferContent(xmlbuf)));
87                 xmlBufferFree(xmlbuf);
88                 return xml;
89
90         }
91 }
92
93
94
95
96 char* xmlSaxAttr( const xmlChar** atts, char* name ) {
97         if( atts && name ) {
98                 int i;
99                 for(i = 0; (atts[i] != NULL); i++) {
100                         if(!strcmp((char*) atts[i], name)) {
101                                 if(atts[++i]) return (char*) atts[i];
102                         }
103                 }
104         }
105         return NULL;
106 }
107
108
109 int xmlAddAttrs( xmlNodePtr node, const xmlChar** atts ) {
110         if( node && atts ) {
111                 int i;
112                 for(i = 0; (atts[i] != NULL); i++) {
113                         if(atts[i+1]) {
114                                 xmlSetProp(node, atts[i], atts[i+1]);
115                                 i++;
116                         }
117                 }
118         }
119         return 0;
120 }
121