]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/c-apps/oils_idl-core.c
Plugged some massive memory leaks.
[Evergreen.git] / Open-ILS / src / c-apps / oils_idl-core.c
1 #include "openils/oils_idl.h"
2 /*
3  * vim:noet:ts=4:
4  */
5
6 #include <stdlib.h>
7 #include <string.h>
8 #include <libxml/globals.h>
9 #include <libxml/xmlerror.h>
10 #include <libxml/parser.h>
11 #include <libxml/tree.h>
12 #include <libxml/debugXML.h>
13 #include <libxml/xmlmemory.h>
14
15 #define PERSIST_NS "http://open-ils.org/spec/opensrf/IDL/persistence/v1"
16 #define OBJECT_NS "http://open-ils.org/spec/opensrf/IDL/objects/v1"
17 #define BASE_NS "http://opensrf.org/spec/IDL/base/v1"
18 #define REPORTER_NS "http://open-ils.org/spec/opensrf/IDL/reporter/v1"
19 #define PERM_NS "http://open-ils.org/spec/opensrf/IDL/permacrud/v1"
20
21 static xmlDocPtr idlDoc = NULL; // parse and store the IDL here
22
23 /* parse and store the IDL here */
24 static osrfHash* idlHash;
25
26 osrfHash* oilsIDL(void) { return idlHash; }
27 osrfHash* oilsIDLInit( const char* idl_filename ) {
28
29         if (idlHash) return idlHash;
30
31         char* prop_str = NULL;
32
33         idlHash = osrfNewHash();
34         osrfHash* class_def_hash = NULL;
35
36         osrfLogInfo(OSRF_LOG_MARK, "Parsing the IDL XML...");
37         idlDoc = xmlReadFile( idl_filename, NULL, XML_PARSE_XINCLUDE );
38         
39         if (!idlDoc) {
40                 osrfLogError(OSRF_LOG_MARK, "Could not load or parse the IDL XML file!");
41                 return NULL;
42         }
43
44         osrfLogDebug(OSRF_LOG_MARK, "Initializing the Fieldmapper IDL...");
45
46         xmlNodePtr docRoot = xmlDocGetRootElement(idlDoc);
47         xmlNodePtr kid = docRoot->children;
48         while (kid) {
49                 if (!strcmp( (char*)kid->name, "class" )) {
50
51                         class_def_hash = osrfNewHash();
52                         char* current_class_name = (char*) xmlGetProp(kid, BAD_CAST "id");
53                         
54                         osrfHashSet( class_def_hash, current_class_name, "classname" );
55                         osrfHashSet( class_def_hash, xmlGetNsProp(kid, BAD_CAST "fieldmapper", BAD_CAST OBJECT_NS), "fieldmapper" );
56                         osrfHashSet( class_def_hash, xmlGetNsProp(kid, BAD_CAST "readonly", BAD_CAST PERSIST_NS), "readonly" );
57
58                         osrfHashSet( idlHash, class_def_hash, current_class_name );
59
60                         if ((prop_str = (char*)xmlGetNsProp(kid, BAD_CAST "tablename", BAD_CAST PERSIST_NS))) {
61                                 osrfLogDebug(OSRF_LOG_MARK, "Using table '%s' for class %s", prop_str, current_class_name );
62                                 osrfHashSet(
63                                         class_def_hash,
64                                         prop_str,
65                                         "tablename"
66                                 );
67                         }
68
69                         if ((prop_str = (char*)xmlGetNsProp(kid, BAD_CAST "restrict_primary", BAD_CAST PERSIST_NS))) {
70                                 osrfLogDebug(OSRF_LOG_MARK, "Delete restriction policy set at '%s' for pkey of class %s", prop_str, current_class_name );
71                                 osrfHashSet(
72                                         class_def_hash,
73                                         prop_str,
74                                         "restrict_primary"
75                                 );
76                         }
77
78                         if ((prop_str = (char*)xmlGetNsProp(kid, BAD_CAST "virtual", BAD_CAST PERSIST_NS))) {
79                                 osrfHashSet(
80                                         class_def_hash,
81                                         prop_str,
82                                         "virtual"
83                                 );
84                         }
85
86                         // Tokenize controller attribute into an osrfStringArray
87                         prop_str = (char*) xmlGetProp(kid, BAD_CAST "controller");
88                         if( prop_str )
89                                 osrfLogDebug(OSRF_LOG_MARK, "Controller list is %s", prop_str );
90                         osrfStringArray* controller = osrfStringArrayTokenize( prop_str, ' ' );
91                         xmlFree( prop_str );
92                         osrfHashSet( class_def_hash, controller, "controller");
93
94                         osrfHash* current_links_hash = osrfNewHash();
95                         osrfHash* current_fields_hash = osrfNewHash();
96
97                         osrfHashSet( class_def_hash, current_fields_hash, "fields" );
98                         osrfHashSet( class_def_hash, current_links_hash, "links" );
99
100                         xmlNodePtr _cur = kid->children;
101
102                         while (_cur) {
103
104                                 if (!strcmp( (char*)_cur->name, "fields" )) {
105
106                                         if( (prop_str = (char*)xmlGetNsProp(_cur, BAD_CAST "primary", BAD_CAST PERSIST_NS)) ) {
107                                                 osrfHashSet(
108                                                         class_def_hash,
109                                                         prop_str,
110                                                         "primarykey"
111                                                 );
112                                         }
113
114                                         if( (prop_str = (char*)xmlGetNsProp(_cur, BAD_CAST "sequence", BAD_CAST PERSIST_NS)) ) {
115                                                 osrfHashSet(
116                                                         class_def_hash,
117                                                         prop_str,
118                                                         "sequence"
119                                                 );
120                                         }
121
122                                         xmlNodePtr _f = _cur->children;
123
124                                         while(_f) {
125                                                 if (strcmp( (char*)_f->name, "field" )) {
126                                                         _f = _f->next;
127                                                         continue;
128                                                 }
129
130                                                 osrfHash* field_def_hash = osrfNewHash();
131
132                                                 if( (prop_str = (char*)xmlGetNsProp(_f, BAD_CAST "array_position", BAD_CAST OBJECT_NS)) ) {
133                                                         osrfHashSet(
134                                                                 field_def_hash,
135                                                                 prop_str,
136                                                                 "array_position"
137                                                         );
138                                                 }
139
140                                                 if( (prop_str = (char*)xmlGetNsProp(_f, BAD_CAST "i18n", BAD_CAST PERSIST_NS)) ) {
141                                                         osrfHashSet(
142                                                                 field_def_hash,
143                                                                 prop_str,
144                                                                 "i18n"
145                                                         );
146                                                 }
147
148                                                 if( (prop_str = (char*)xmlGetNsProp(_f, BAD_CAST "virtual", BAD_CAST PERSIST_NS)) ) {
149                                                         osrfHashSet(
150                                                                 field_def_hash,
151                                                                 prop_str,
152                                                                 "virtual"
153                                                         );
154                                                 }
155
156                                                 if( (prop_str = (char*)xmlGetNsProp(_f, BAD_CAST "primitive", BAD_CAST PERSIST_NS)) ) {
157                                                         osrfHashSet(
158                                                                 field_def_hash,
159                                                                 prop_str,
160                                                                 "primitive"
161                                                         );
162                                                 }
163
164                                                 if( (prop_str = (char*)xmlGetProp(_f, BAD_CAST "name")) ) {
165                                                         osrfHashSet(
166                                                                 field_def_hash,
167                                                                 prop_str,
168                                                                 "name"
169                                                         );
170                                                         osrfLogDebug(OSRF_LOG_MARK, 
171                                                                 "Found field %s for class %s", prop_str, current_class_name );
172                                                 } else
173                                                         osrfLogDebug(OSRF_LOG_MARK, 
174                                                                 "Found field with no name for class %s", current_class_name );
175
176                                                 osrfHashSet(
177                                                         current_fields_hash,
178                                                         field_def_hash,
179                                                         prop_str
180                                                 );
181                                                 _f = _f->next;
182                                         }
183                                 }
184
185                                 if (!strcmp( (char*)_cur->name, "links" )) {
186                                         xmlNodePtr _l = _cur->children;
187
188                                         while(_l) {
189                                                 if (strcmp( (char*)_l->name, "link" )) {
190                                                         _l = _l->next;
191                                                         continue;
192                                                 }
193
194                                                 osrfHash* link_def_hash = osrfNewHash();
195
196                                                 if( (prop_str = (char*)xmlGetProp(_l, BAD_CAST "reltype")) ) {
197                                                         osrfHashSet(
198                                                                 link_def_hash,
199                                                                 prop_str,
200                                                                 "reltype"
201                                                         );
202                                                         osrfLogDebug(OSRF_LOG_MARK, "Adding link with reltype %s", prop_str );
203                                                 } else
204                                                         osrfLogDebug(OSRF_LOG_MARK, "Adding link with no reltype" );
205
206                                                 if( (prop_str = (char*)xmlGetProp(_l, BAD_CAST "key")) ) {
207                                                         osrfHashSet(
208                                                                 link_def_hash,
209                                                                 prop_str,
210                                                                 "key"
211                                                         );
212                                                         osrfLogDebug(OSRF_LOG_MARK, "Link fkey is %s", prop_str );
213                                                 } else
214                                                         osrfLogDebug(OSRF_LOG_MARK, "Link with no fkey" );
215
216                                                 if( (prop_str = (char*)xmlGetProp(_l, BAD_CAST "class")) ) {
217                                                         osrfHashSet(
218                                                                 link_def_hash,
219                                                                 prop_str,
220                                                                 "class"
221                                                         );
222                                                         osrfLogDebug(OSRF_LOG_MARK, "Link fclass is %s", prop_str );
223                                                 } else
224                                                         osrfLogDebug(OSRF_LOG_MARK, "Link with no fclass" );
225
226                                                 // Tokenize map attribute into an osrfStringArray
227                                                 prop_str = (char*) xmlGetProp(_l, BAD_CAST "map");
228                                                 if( prop_str )
229                                                         osrfLogDebug(OSRF_LOG_MARK, "Link mapping list is %s", prop_str );
230                                                 osrfStringArray* map = osrfStringArrayTokenize( prop_str, ' ' );
231                                                 osrfHashSet( link_def_hash, map, "map");
232                                                 xmlFree( prop_str );
233
234                                                 if( (prop_str = (char*)xmlGetProp(_l, BAD_CAST "field")) ) {
235                                                         osrfHashSet(
236                                                                 link_def_hash,
237                                                                 prop_str,
238                                                                 "field"
239                                                         );
240                                                         osrfLogDebug(OSRF_LOG_MARK, "Link fclass is %s", prop_str );
241                                                 } else
242                                                         osrfLogDebug(OSRF_LOG_MARK, "Link with no fclass" );
243
244                                                 osrfHashSet(
245                                                         current_links_hash,
246                                                         link_def_hash,
247                                                         prop_str
248                                                 );
249
250                                                 _l = _l->next;
251                                         }
252                                 }
253 /**** Structure of permacrud in memory ****
254
255 { create :
256     { permission : [ x, y, z ],
257       global_required : "true", -- anything else, or missing, is false
258       local_context : [ f1, f2 ],
259       foreign_context : { class1 : { fkey : local_class_key, field : class1_field, context : [ a, b, c ] }, ...}
260     },
261   retrieve : null, -- no perm check, or structure similar to the others
262   update : -- like create
263     ...
264   delete : -- like create
265     ...
266 }   
267
268 **** Structure of permacrud in memory ****/
269
270                                 if (!strcmp( (char*)_cur->name, "permacrud" )) {
271                                         osrfHash* pcrud = osrfNewHash();
272                                         osrfHashSet( class_def_hash, pcrud, "permacrud" );
273                                         xmlNodePtr _l = _cur->children;
274
275                                         while(_l) {
276                                                 if (strcmp( (char*)_l->name, "actions" )) {
277                                                         _l = _l->next;
278                                                         continue;
279                                                 }
280
281                                                 xmlNodePtr _a = _l->children;
282
283                                                 while(_a) {
284                                                         const char* action_name = (const char*) _a->name;
285                                                         if (
286                                                                 strcmp( action_name, "create" ) &&
287                                                                 strcmp( action_name, "retrieve" ) &&
288                                                                 strcmp( action_name, "update" ) &&
289                                                                 strcmp( action_name, "delete" )
290                                                         ) {
291                                                                 _a = _a->next;
292                                                                 continue;
293                                                         }
294
295                                                         osrfLogDebug(OSRF_LOG_MARK, "Found Permacrud action %s for class %s",
296                                                                 action_name, current_class_name );
297
298                                                         osrfHash* action_def_hash = osrfNewHash();
299                                                         osrfHashSet( pcrud, action_def_hash, action_name );
300
301                                                         // Tokenize permission attribute into an osrfStringArray
302                                                         prop_str = (char*) xmlGetProp(_a, BAD_CAST "permission");
303                                                         if( prop_str )
304                                                                 osrfLogDebug(OSRF_LOG_MARK,
305                                                                         "Permacrud permission list is %s", prop_str );
306                                                         osrfStringArray* map = osrfStringArrayTokenize( prop_str, ' ' );
307                                                         osrfHashSet( action_def_hash, map, "permission");
308                                                         xmlFree( prop_str );
309
310                                                 osrfHashSet( action_def_hash,
311                                                                 (char*)xmlGetNoNsProp(_a, BAD_CAST "global_required"), "global_required");
312
313                                                         // Tokenize context_field attribute into an osrfStringArray
314                                                         prop_str = (char*) xmlGetProp(_a, BAD_CAST "context_field");
315                                                         if( prop_str )
316                                                                 osrfLogDebug(OSRF_LOG_MARK,
317                                                                         "Permacrud context_field list is %s", prop_str );
318                                                         map = osrfStringArrayTokenize( prop_str, ' ' );
319                                                         osrfHashSet( action_def_hash, map, "local_context");
320                                                         xmlFree( prop_str );
321
322                                                         osrfHash* foreign_context = osrfNewHash();
323                                                         osrfHashSet( action_def_hash, foreign_context, "foreign_context");
324
325                                                         xmlNodePtr _f = _a->children;
326
327                                                         while(_f) {
328                                                                 if ( strcmp( (char*)_f->name, "context" ) ) {
329                                                                         _f = _f->next;
330                                                                         continue;
331                                                                 }
332
333                                                                 if( (prop_str = (char*)xmlGetNoNsProp(_f, BAD_CAST "link")) ) {
334                                                                         osrfLogDebug(OSRF_LOG_MARK,
335                                                                                 "Permacrud context link definition is %s", prop_str );
336
337                                                                         osrfHash* _tmp_fcontext = osrfNewHash();
338
339                                                                         // Store pointers to elements already stored
340                                                                         // from the <link> aggregate
341                                                                         osrfHash* _flink = osrfHashGet( current_links_hash, prop_str );
342                                                                         osrfHashSet( _tmp_fcontext, osrfHashGet(_flink, "field"), "fkey" );
343                                                                         osrfHashSet( _tmp_fcontext, osrfHashGet(_flink, "key"), "field" );
344                                                                         xmlFree( prop_str );
345
346                                                                     if( (prop_str = (char*)xmlGetNoNsProp(_f, BAD_CAST "jump")) )
347                                                                             osrfHashSet( _tmp_fcontext, osrfStringArrayTokenize( prop_str, '.' ), "jump" );
348                                                                         xmlFree( prop_str );
349
350                                                                         // Tokenize field attribute into an osrfStringArray
351                                                                         char * field_list = (char*) xmlGetProp(_f, BAD_CAST "field");
352                                                                         if( field_list )
353                                                                                 osrfLogDebug(OSRF_LOG_MARK,
354                                                                                         "Permacrud foreign context field list is %s", field_list );
355                                                                         map = osrfStringArrayTokenize( field_list, ' ' );
356                                                                         osrfHashSet( _tmp_fcontext, map, "context");
357                                                                         xmlFree( field_list );
358
359                                                                         // Insert the new hash into a hash attached to the parent node
360                                                                         osrfHashSet( foreign_context, _tmp_fcontext, osrfHashGet( _flink, "class" ) );
361
362                                                                 } else {
363
364                                                                         if( (prop_str = (char*)xmlGetNoNsProp(_f, BAD_CAST "field") )) {
365                                                                                 char* map_list = prop_str;
366                                                                                 osrfLogDebug(OSRF_LOG_MARK,
367                                                                                         "Permacrud local context field list is %s", prop_str );
368                         
369                                                                                 if (strlen( map_list ) > 0) {
370                                                                                         char* st_tmp = NULL;
371                                                                                         char* _map_class = strtok_r(map_list, " ", &st_tmp);
372                                                                                         osrfStringArrayAdd(
373                                                                                                 osrfHashGet( action_def_hash, "local_context"), _map_class);
374                                                                         
375                                                                                         while ((_map_class = strtok_r(NULL, " ", &st_tmp))) {
376                                                                                                 osrfStringArrayAdd(
377                                                                                                         osrfHashGet( action_def_hash, "local_context"), _map_class);
378                                                                                         }
379                                                                                 }
380                                                                                 xmlFree(map_list);
381                                                                         }
382
383                                                                 }
384                                                                 _f = _f->next;
385                                                         }
386                                                         _a = _a->next;
387                                                 }
388                                                 _l = _l->next;
389                                         }
390                                 }
391
392                                 if (!strcmp( (char*)_cur->name, "source_definition" )) {
393                                         char* content_str;
394                                         if( (content_str = (char*)xmlNodeGetContent(_cur)) ) {
395                                                 osrfLogDebug(OSRF_LOG_MARK, "Using source definition '%s' for class %s",
396                                                         content_str, current_class_name );
397                                                 osrfHashSet(
398                                                         class_def_hash,
399                                                         content_str,
400                                                         "source_definition"
401                                                 );
402                                         }
403
404                                 }
405
406                                 _cur = _cur->next;
407                         } // end while
408                 }
409
410                 kid = kid->next;
411         } // end while
412
413         osrfLogInfo(OSRF_LOG_MARK, "...IDL XML parsed");
414
415         return idlHash;
416 }
417
418 osrfHash* oilsIDLFindPath( const char* path, ... ) {
419         if(!path || strlen(path) < 1) return NULL;
420
421         osrfHash* obj = idlHash;
422
423         VA_LIST_TO_STRING(path);
424         char* buf = VA_BUF;
425
426         char* token = NULL;
427         char* t = buf;
428         char* tt;
429
430         token = strtok_r(t, "/", &tt);
431         if(!token) return NULL;
432
433         do {
434                 obj = osrfHashGet(obj, token);
435         } while( (token = strtok_r(NULL, "/", &tt)) && obj);
436
437         return obj;
438 }
439
440 static osrfHash* findClassDef( const char* classname ) {
441         if( !classname || !idlHash )
442                 return NULL;
443         else
444                 return osrfHashGet( idlHash, classname );
445 }
446
447 osrfHash* oilsIDL_links( const char* classname ) {
448         osrfHash* classdef = findClassDef( classname );
449         if( classdef )
450                 return osrfHashGet( classdef, "links" );
451         else
452                 return NULL;
453 }
454
455 osrfHash* oilsIDL_fields( const char* classname ) {
456         osrfHash* classdef = findClassDef( classname );
457         if( classdef )
458                 return osrfHashGet( classdef, "fields" );
459         else
460                 return NULL;
461 }
462
463 int oilsIDL_classIsFieldmapper ( const char* classname ) {
464         if( findClassDef( classname ) )
465                 return 1;
466         else
467                 return 0;
468 }
469
470 // For a given class: return the array_position associated with a 
471 // specified field. (or -1 if it doesn't exist)
472 int oilsIDL_ntop (const char* classname, const char* fieldname) {
473         osrfHash* fields_hash = oilsIDL_fields( classname );
474         if( !fields_hash )
475                 return -1;     // No such class, or no fields for it
476
477         osrfHash* field_def_hash = osrfHashGet( fields_hash, fieldname );
478         if( !field_def_hash )
479                 return -1;                      // No such field
480
481         const char* pos_attr = osrfHashGet( field_def_hash, "array_position" );
482         if( !pos_attr )
483                 return -1;                      // No array_position attribute
484
485         return atoi( pos_attr );        // Return position as int
486 }
487
488 // For a given class: return a copy of the name of the field 
489 // at a specified array_position (or NULL if there is none)
490 char * oilsIDL_pton (const char* classname, int pos) {
491         osrfHash* fields_hash = oilsIDL_fields( classname );
492         if( !fields_hash )
493                 return NULL;     // No such class, or no fields for it
494
495         char* ret = NULL;
496         osrfHash* field_def_hash = NULL;
497         osrfHashIterator* iter = osrfNewHashIterator( fields_hash );
498
499         while ( ( field_def_hash = osrfHashIteratorNext( iter ) ) ) {
500                 if ( atoi( osrfHashGet( field_def_hash, "array_position" ) ) == pos ) {
501                         ret = strdup( osrfHashIteratorKey( iter ) );
502                         break;
503                 }
504         }
505
506         osrfHashIteratorFree( iter );
507
508         return ret;
509 }
510