bug fixes after testing
[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                 double x = jsonObjectGetNumber(obj);
116                 if (notag) {
117                         if (x == (int)x)
118                                 buffer_fadd(buf,"%d",(int)x);
119                         else
120                                 buffer_fadd(buf,"%lf",x);
121                 } else {
122                         if (x == (int)x)
123                                 buffer_fadd(buf,"<%s>%d</%s>",tag, (int)x,tag);
124                         else
125                                 buffer_fadd(buf,"<%s>%lf</%s>",tag, x,tag);
126                 }
127
128         } else if (obj->type == JSON_ARRAY) {
129                 if (!notag) {
130                         if(!isFieldmapper(obj_class))
131                                 buffer_add(buf,"<array>");
132                         else
133                                 buffer_fadd(buf,"<%s>",tag);
134                 }
135
136                 for( i = 0; i!= obj->size; i++ ) {
137                         _rest_xml_output(buf, jsonObjectGetIndex(obj,i), obj->classname, i,0);
138                 }
139
140                 if (!notag) {
141                         if(!isFieldmapper(obj_class))
142                                 buffer_add(buf,"</array>");
143                         else
144                                 buffer_fadd(buf,"</%s>",tag);
145                 }
146
147         } else if (obj->type == JSON_HASH) {
148
149                 if (!notag) {
150                         if(!obj_class)
151                                 buffer_add(buf,"<hash>");
152                         else
153                                 buffer_fadd(buf,"<%s>",tag);
154                 }
155
156                 jsonObjectIterator* itr = jsonNewObjectIterator(obj);
157                 jsonObjectNode* tmp;
158                 while( (tmp = jsonObjectIteratorNext(itr)) ) {
159                         if (notag) {
160                                 buffer_fadd(buf,"<%s>",tmp->key);
161                         } else {
162                                 buffer_add(buf,"<pair>");
163                                 buffer_fadd(buf,"<key>%s</key><value>",tmp->key);
164                         }
165
166                         _rest_xml_output(buf, tmp->item, NULL,0,notag);
167
168                         if (notag) {
169                                 buffer_fadd(buf,"</%s>",tmp->key);
170                         } else {
171                                 buffer_add(buf,"</value></pair>");
172                         }
173                 }
174                 jsonObjectIteratorFree(itr);
175
176                 if (!notag) {
177                         if(!obj_class)
178                                 buffer_add(buf,"</hash>");
179                         else
180                                 buffer_fadd(buf,"</%s>",tag);
181                 }
182
183         }
184
185         if (obj->classname)
186                 buffer_fadd(buf,"</Object></%s>",tag);
187
188         free(tag);
189 }
190