1 #include "openils/oils_idl.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>
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"
21 static xmlDocPtr idlDoc = NULL; // parse and store the IDL here
23 /* parse and store the IDL here */
24 static osrfHash* idlHash;
26 osrfHash* oilsIDL(void) { return idlHash; }
27 osrfHash* oilsIDLInit( const char* idl_filename ) {
29 if (idlHash) return idlHash;
31 char* prop_str = NULL;
33 idlHash = osrfNewHash();
34 osrfHash* class_def_hash = NULL;
36 osrfLogInfo(OSRF_LOG_MARK, "Parsing the IDL XML...");
37 idlDoc = xmlReadFile( idl_filename, NULL, XML_PARSE_XINCLUDE );
40 osrfLogError(OSRF_LOG_MARK, "Could not load or parse the IDL XML file!");
44 osrfLogDebug(OSRF_LOG_MARK, "Initializing the Fieldmapper IDL...");
46 xmlNodePtr docRoot = xmlDocGetRootElement(idlDoc);
47 xmlNodePtr kid = docRoot->children;
49 if (!strcmp( (char*)kid->name, "class" )) {
51 class_def_hash = osrfNewHash();
52 char* current_class_name = (char*) xmlGetProp(kid, BAD_CAST "id");
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" );
58 osrfHashSet( idlHash, class_def_hash, current_class_name );
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 );
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 );
78 if ((prop_str = (char*)xmlGetNsProp(kid, BAD_CAST "virtual", BAD_CAST PERSIST_NS))) {
86 // Tokenize controller attribute into an osrfStringArray
87 prop_str = (char*) xmlGetProp(kid, BAD_CAST "controller");
89 osrfLogDebug(OSRF_LOG_MARK, "Controller list is %s", prop_str );
90 osrfStringArray* controller = osrfStringArrayTokenize( prop_str, ' ' );
92 osrfHashSet( class_def_hash, controller, "controller");
94 osrfHash* current_links_hash = osrfNewHash();
95 osrfHash* current_fields_hash = osrfNewHash();
97 osrfHashSet( class_def_hash, current_fields_hash, "fields" );
98 osrfHashSet( class_def_hash, current_links_hash, "links" );
100 xmlNodePtr _cur = kid->children;
104 if (!strcmp( (char*)_cur->name, "fields" )) {
106 if( (prop_str = (char*)xmlGetNsProp(_cur, BAD_CAST "primary", BAD_CAST PERSIST_NS)) ) {
114 if( (prop_str = (char*)xmlGetNsProp(_cur, BAD_CAST "sequence", BAD_CAST PERSIST_NS)) ) {
122 xmlNodePtr _f = _cur->children;
125 if (strcmp( (char*)_f->name, "field" )) {
130 osrfHash* field_def_hash = osrfNewHash();
132 if( (prop_str = (char*)xmlGetNsProp(_f, BAD_CAST "array_position", BAD_CAST OBJECT_NS)) ) {
140 if( (prop_str = (char*)xmlGetNsProp(_f, BAD_CAST "i18n", BAD_CAST PERSIST_NS)) ) {
148 if( (prop_str = (char*)xmlGetNsProp(_f, BAD_CAST "virtual", BAD_CAST PERSIST_NS)) ) {
156 if( (prop_str = (char*)xmlGetNsProp(_f, BAD_CAST "primitive", BAD_CAST PERSIST_NS)) ) {
164 if( (prop_str = (char*)xmlGetProp(_f, BAD_CAST "name")) ) {
170 osrfLogDebug(OSRF_LOG_MARK,
171 "Found field %s for class %s", prop_str, current_class_name );
173 osrfLogDebug(OSRF_LOG_MARK,
174 "Found field with no name for class %s", current_class_name );
185 if (!strcmp( (char*)_cur->name, "links" )) {
186 xmlNodePtr _l = _cur->children;
189 if (strcmp( (char*)_l->name, "link" )) {
194 osrfHash* link_def_hash = osrfNewHash();
196 if( (prop_str = (char*)xmlGetProp(_l, BAD_CAST "reltype")) ) {
202 osrfLogDebug(OSRF_LOG_MARK, "Adding link with reltype %s", prop_str );
204 osrfLogDebug(OSRF_LOG_MARK, "Adding link with no reltype" );
206 if( (prop_str = (char*)xmlGetProp(_l, BAD_CAST "key")) ) {
212 osrfLogDebug(OSRF_LOG_MARK, "Link fkey is %s", prop_str );
214 osrfLogDebug(OSRF_LOG_MARK, "Link with no fkey" );
216 if( (prop_str = (char*)xmlGetProp(_l, BAD_CAST "class")) ) {
222 osrfLogDebug(OSRF_LOG_MARK, "Link fclass is %s", prop_str );
224 osrfLogDebug(OSRF_LOG_MARK, "Link with no fclass" );
226 // Tokenize map attribute into an osrfStringArray
227 prop_str = (char*) xmlGetProp(_l, BAD_CAST "map");
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");
234 if( (prop_str = (char*)xmlGetProp(_l, BAD_CAST "field")) ) {
240 osrfLogDebug(OSRF_LOG_MARK, "Link fclass is %s", prop_str );
242 osrfLogDebug(OSRF_LOG_MARK, "Link with no fclass" );
253 /**** Structure of permacrud in memory ****
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 ] }, ...}
261 retrieve : null, -- no perm check, or structure similar to the others
262 update : -- like create
264 delete : -- like create
268 **** Structure of permacrud in memory ****/
270 if (!strcmp( (char*)_cur->name, "permacrud" )) {
271 osrfHash* pcrud = osrfNewHash();
272 osrfHashSet( class_def_hash, pcrud, "permacrud" );
273 xmlNodePtr _l = _cur->children;
276 if (strcmp( (char*)_l->name, "actions" )) {
281 xmlNodePtr _a = _l->children;
284 const char* action_name = (const char*) _a->name;
286 strcmp( action_name, "create" ) &&
287 strcmp( action_name, "retrieve" ) &&
288 strcmp( action_name, "update" ) &&
289 strcmp( action_name, "delete" )
295 osrfLogDebug(OSRF_LOG_MARK, "Found Permacrud action %s for class %s",
296 action_name, current_class_name );
298 osrfHash* action_def_hash = osrfNewHash();
299 osrfHashSet( pcrud, action_def_hash, action_name );
301 // Tokenize permission attribute into an osrfStringArray
302 prop_str = (char*) xmlGetProp(_a, BAD_CAST "permission");
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");
310 osrfHashSet( action_def_hash,
311 (char*)xmlGetNoNsProp(_a, BAD_CAST "global_required"), "global_required");
313 // Tokenize context_field attribute into an osrfStringArray
314 prop_str = (char*) xmlGetProp(_a, BAD_CAST "context_field");
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");
322 osrfHash* foreign_context = osrfNewHash();
323 osrfHashSet( action_def_hash, foreign_context, "foreign_context");
325 xmlNodePtr _f = _a->children;
328 if ( strcmp( (char*)_f->name, "context" ) ) {
333 if( (prop_str = (char*)xmlGetNoNsProp(_f, BAD_CAST "link")) ) {
334 osrfLogDebug(OSRF_LOG_MARK,
335 "Permacrud context link definition is %s", prop_str );
337 osrfHash* _tmp_fcontext = osrfNewHash();
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" );
346 if( (prop_str = (char*)xmlGetNoNsProp(_f, BAD_CAST "jump")) )
347 osrfHashSet( _tmp_fcontext, osrfStringArrayTokenize( prop_str, '.' ), "jump" );
350 // Tokenize field attribute into an osrfStringArray
351 char * field_list = (char*) xmlGetProp(_f, BAD_CAST "field");
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 );
359 // Insert the new hash into a hash attached to the parent node
360 osrfHashSet( foreign_context, _tmp_fcontext, osrfHashGet( _flink, "class" ) );
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 );
369 if (strlen( map_list ) > 0) {
371 char* _map_class = strtok_r(map_list, " ", &st_tmp);
373 osrfHashGet( action_def_hash, "local_context"), _map_class);
375 while ((_map_class = strtok_r(NULL, " ", &st_tmp))) {
377 osrfHashGet( action_def_hash, "local_context"), _map_class);
392 if (!strcmp( (char*)_cur->name, "source_definition" )) {
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 );
413 osrfLogInfo(OSRF_LOG_MARK, "...IDL XML parsed");
418 osrfHash* oilsIDLFindPath( const char* path, ... ) {
419 if(!path || strlen(path) < 1) return NULL;
421 osrfHash* obj = idlHash;
423 VA_LIST_TO_STRING(path);
430 token = strtok_r(t, "/", &tt);
431 if(!token) return NULL;
434 obj = osrfHashGet(obj, token);
435 } while( (token = strtok_r(NULL, "/", &tt)) && obj);
440 static osrfHash* findClassDef( const char* classname ) {
441 if( !classname || !idlHash )
444 return osrfHashGet( idlHash, classname );
447 osrfHash* oilsIDL_links( const char* classname ) {
448 osrfHash* classdef = findClassDef( classname );
450 return osrfHashGet( classdef, "links" );
455 osrfHash* oilsIDL_fields( const char* classname ) {
456 osrfHash* classdef = findClassDef( classname );
458 return osrfHashGet( classdef, "fields" );
463 int oilsIDL_classIsFieldmapper ( const char* classname ) {
464 if( findClassDef( classname ) )
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 );
475 return -1; // No such class, or no fields for it
477 osrfHash* field_def_hash = osrfHashGet( fields_hash, fieldname );
478 if( !field_def_hash )
479 return -1; // No such field
481 const char* pos_attr = osrfHashGet( field_def_hash, "array_position" );
483 return -1; // No array_position attribute
485 return atoi( pos_attr ); // Return position as int
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 );
493 return NULL; // No such class, or no fields for it
496 osrfHash* field_def_hash = NULL;
497 osrfHashIterator* iter = osrfNewHashIterator( fields_hash );
499 while ( ( field_def_hash = osrfHashIteratorNext( iter ) ) ) {
500 if ( atoi( osrfHashGet( field_def_hash, "array_position" ) ) == pos ) {
501 ret = strdup( osrfHashIteratorKey( iter ) );
506 osrfHashIteratorFree( iter );