0cd42a426d13f234300f05c851a365cbd7188569
[Evergreen.git] / Open-ILS / src / apachemods / json_xml.c
1 #include "json_xml.h"
2 #include "fieldmapper_lookup.h"
3
4 void _rest_xml_output(growing_buffer*, jsonObject*, char*, int, int);
5 char* _escape_xml (char*);
6
7 char* json_string_to_xml(char* content) {
8         jsonObject * obj;
9         growing_buffer * res_xml;
10         char * output;
11         int i;
12
13         obj = json_parse_string( content );
14         res_xml = buffer_init(1024);
15
16         if (!obj)
17                 return NULL;
18         
19         buffer_add(res_xml, "<response>");
20
21         if(obj->type == JSON_ARRAY ) {
22                 for( i = 0; i!= obj->size; i++ ) {
23                         _rest_xml_output(res_xml, jsonObjectGetIndex(obj,i), NULL, 0,0);
24                 }
25         } else {
26                 _rest_xml_output(res_xml, obj, NULL, 0,0);
27         }
28
29         buffer_add(res_xml, "</response>");
30
31         output = buffer_data(res_xml);
32         buffer_free(res_xml);
33         jsonObjectFree(obj);
34
35         return output;
36 }
37
38 char* _escape_xml (char* text) {
39         char* out;
40         growing_buffer* b = buffer_init(256);
41         int len = strlen(text);
42         int i;
43         for (i = 0; i < len; i++) {
44                 if (text[i] == '&')
45                         buffer_add(b,"&amp;");
46                 else if (text[i] == '<')
47                         buffer_add(b,"&lt;");
48                 else if (text[i] == '>')
49                         buffer_add(b,"&gt;");
50                 else
51                         buffer_add_char(b,text[i]);
52         }
53         out = buffer_data(b);
54         buffer_free(b);
55         return out;
56 }
57
58 void _rest_xml_output(growing_buffer* buf, jsonObject* obj, char * obj_class, int arr_index, int notag) {
59         char * tag;
60         int i;
61         
62         if(!obj) return;
63
64         if (obj->classname)
65                 notag = 1;
66
67         if(isFieldmapper(obj_class)) {
68                 tag = fm_pton(obj_class,arr_index);
69         } else if(obj_class) {
70                 tag = strdup(obj_class);
71         } else {
72                 tag = strdup("datum");
73         }
74
75         
76    /* add class hints if we have a class name */
77    if(obj->classname) {
78         if(obj->type == JSON_NULL) {
79                         buffer_fadd(buf,"<%s><Object class_hint=\\\"%s\\\"/></%s>", tag, obj->classname, tag);
80                         return;
81                 } else {
82                         buffer_fadd(buf,"<%s><Object class_hint=\\\"%s\\\">", tag, obj->classname);
83                 }
84         }
85
86
87         /* now add the data */
88         if(obj->type == JSON_NULL) {
89                 if (!notag)
90                         buffer_fadd(buf, "<%s/>",tag);
91         } else if(obj->type == JSON_BOOL && obj->value.b) {
92                 if (notag)
93                         buffer_add(buf, "true");
94                 else
95                         buffer_fadd(buf, "<%s>true</%s>",tag,tag);
96                 
97         } else if(obj->type == JSON_BOOL && ! obj->value.b) {
98                 if (notag)
99                         buffer_add(buf, "false");
100                 else
101                         buffer_fadd(buf, "<%s>false</%s>",tag,tag);
102
103         } else if (obj->type == JSON_STRING) {
104                 if (notag) {
105                         char * t = _escape_xml(jsonObjectGetString(obj));
106                         buffer_add(buf,t);
107                         free(t);
108                 } else {
109                         char * t = _escape_xml(jsonObjectGetString(obj));
110                         buffer_fadd(buf,"<%s>%s</%s>",tag,t,tag);
111                         free(t);
112                 }
113
114         } else if(obj->type == JSON_NUMBER) {
115                 if (notag)
116                         buffer_fadd(buf,"%lf",tag, jsonObjectGetNumber(obj),tag);
117                 else
118                         buffer_fadd(buf,"<%s>%lf</%s>",tag, jsonObjectGetNumber(obj),tag);
119
120
121         } else if (obj->type == JSON_ARRAY) {
122                 if (!notag) {
123                         if(!isFieldmapper(obj_class))
124                                 buffer_add(buf,"<array>");
125                         else
126                                 buffer_fadd(buf,"<%s>",tag);
127                 }
128
129                 for( i = 0; i!= obj->size; i++ ) {
130                         _rest_xml_output(buf, jsonObjectGetIndex(obj,i), obj->classname, i,0);
131                 }
132
133                 if (!notag) {
134                         if(!isFieldmapper(obj_class))
135                                 buffer_add(buf,"</array>");
136                         else
137                                 buffer_fadd(buf,"</%s>",tag);
138                 }
139
140         } else if (obj->type == JSON_HASH) {
141
142                 if (!notag) {
143                         if(!obj_class)
144                                 buffer_add(buf,"<hash>");
145                         else
146                                 buffer_fadd(buf,"<%s>",tag);
147                 }
148
149                 jsonObjectIterator* itr = jsonNewObjectIterator(obj);
150                 jsonObjectNode* tmp;
151                 while( (tmp = jsonObjectIteratorNext(itr)) ) {
152                         if (notag) {
153                                 buffer_fadd(buf,"<%s>",tmp->key);
154                         } else {
155                                 buffer_add(buf,"<pair>");
156                                 buffer_fadd(buf,"<key>%s</key><value>",tmp->key);
157                         }
158
159                         _rest_xml_output(buf, tmp->item, NULL,0,notag);
160
161                         if (notag) {
162                                 buffer_fadd(buf,"</%s>",tmp->key);
163                         } else {
164                                 buffer_add(buf,"</value></pair>");
165                         }
166                 }
167                 jsonObjectIteratorFree(itr);
168
169                 if (!notag) {
170                         if(!obj_class)
171                                 buffer_add(buf,"</hash>");
172                         else
173                                 buffer_fadd(buf,"</%s>",tag);
174                 }
175
176         }
177
178         if (obj->classname)
179                 buffer_fadd(buf,"</Object></%s>",tag);
180
181         free(tag);
182 }
183