e34f89fee90f4630f272143175a2e9a163227e23
[working/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*, object*, char*, int, int);
5 char* _escape_xml (char*);
6
7 char* json_string_to_xml(char* content) {
8         object * 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->is_array) {
22                 for( i = 0; i!= obj->size; i++ ) {
23                         _rest_xml_output(res_xml, obj->get_index(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         free_object(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, object* 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->is_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->is_null) {
89                 if (!notag)
90                         buffer_fadd(buf, "<%s/>",tag);
91         } else if(obj->is_bool && obj->bool_value) {
92                 if (notag)
93                         buffer_add(buf, "true");
94                 else
95                         buffer_fadd(buf, "<%s>true</%s>",tag,tag);
96                 
97         } else if(obj->is_bool && ! obj->bool_value) {
98                 if (notag)
99                         buffer_add(buf, "false");
100                 else
101                         buffer_fadd(buf, "<%s>false</%s>",tag,tag);
102
103         } else if (obj->is_string) {
104                 if (notag) {
105                         char * t = _escape_xml(obj->string_data);
106                         buffer_add(buf,t);
107                         free(t);
108                 } else {
109                         char * t = _escape_xml(obj->string_data);
110                         buffer_fadd(buf,"<%s>%s</%s>",tag,t,tag);
111                         free(t);
112                 }
113
114         } else if(obj->is_number) {
115
116                 if (notag)
117                         buffer_fadd(buf,"%ld",obj->num_value);
118                 else
119                         buffer_fadd(buf,"<%s>%ld</%s>",tag,obj->num_value,tag);
120
121
122         } else if(obj->is_double) {
123                 if (notag)
124                         buffer_fadd(buf,"%lf",tag,obj->double_value,tag);
125                 else
126                         buffer_fadd(buf,"<%s>%lf</%s>",tag,obj->double_value,tag);
127
128
129         } else if (obj->is_array) {
130                 if (!notag) {
131                         if(!isFieldmapper(obj_class))
132                                 buffer_add(buf,"<array>");
133                         else
134                                 buffer_fadd(buf,"<%s>",tag);
135                 }
136
137                 for( i = 0; i!= obj->size; i++ ) {
138                         _rest_xml_output(buf, obj->get_index(obj,i), obj->classname, i,0);
139                 }
140
141                 if (!notag) {
142                         if(!isFieldmapper(obj_class))
143                                 buffer_add(buf,"</array>");
144                         else
145                                 buffer_fadd(buf,"</%s>",tag);
146                 }
147
148         } else if (obj->is_hash) {
149
150                 if (!notag) {
151                         if(!obj_class)
152                                 buffer_add(buf,"<hash>");
153                         else
154                                 buffer_fadd(buf,"<%s>",tag);
155                 }
156
157                 object_iterator* itr = new_iterator(obj);
158                 object_node* tmp;
159                 while( (tmp = itr->next(itr)) ) {
160                         if (notag) {
161                                 buffer_fadd(buf,"<%s>",tmp->key);
162                         } else {
163                                 buffer_add(buf,"<pair>");
164                                 buffer_fadd(buf,"<key>%s</key><value>",tmp->key);
165                         }
166
167                         _rest_xml_output(buf, tmp->item, NULL,0,notag);
168
169                         if (notag) {
170                                 buffer_fadd(buf,"</%s>",tmp->key);
171                         } else {
172                                 buffer_add(buf,"</value></pair>");
173                         }
174                 }
175                 free_iterator(itr);
176
177                 if (!notag) {
178                         if(!obj_class)
179                                 buffer_add(buf,"</hash>");
180                         else
181                                 buffer_fadd(buf,"</%s>",tag);
182                 }
183
184         }
185
186         if (obj->classname)
187                 buffer_fadd(buf,"</Object></%s>",tag);
188
189         free(tag);
190 }
191