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 "virtual", BAD_CAST PERSIST_NS))) {
77 // Tokenize controller attribute into an osrfStringArray
78 prop_str = (char*) xmlGetProp(kid, BAD_CAST "controller");
80 osrfLogDebug(OSRF_LOG_MARK, "Controller list is %s", prop_str );
81 osrfStringArray* controller = osrfStringArrayTokenize( prop_str, ' ' );
82 osrfHashSet( class_def_hash, controller, "controller");
84 osrfHash* current_links_hash = osrfNewHash();
85 osrfHash* current_fields_hash = osrfNewHash();
86 osrfHash* pcrud = osrfNewHash();
88 osrfHashSet( class_def_hash, current_fields_hash, "fields" );
89 osrfHashSet( class_def_hash, current_links_hash, "links" );
91 xmlNodePtr _cur = kid->children;
95 if (!strcmp( (char*)_cur->name, "fields" )) {
97 if( (prop_str = (char*)xmlGetNsProp(_cur, BAD_CAST "primary", BAD_CAST PERSIST_NS)) ) {
105 if( (prop_str = (char*)xmlGetNsProp(_cur, BAD_CAST "sequence", BAD_CAST PERSIST_NS)) ) {
113 xmlNodePtr _f = _cur->children;
116 if (strcmp( (char*)_f->name, "field" )) {
121 osrfHash* field_def_hash = osrfNewHash();
123 if( (prop_str = (char*)xmlGetNsProp(_f, BAD_CAST "array_position", BAD_CAST OBJECT_NS)) ) {
131 if( (prop_str = (char*)xmlGetNsProp(_f, BAD_CAST "i18n", BAD_CAST PERSIST_NS)) ) {
139 if( (prop_str = (char*)xmlGetNsProp(_f, BAD_CAST "virtual", BAD_CAST PERSIST_NS)) ) {
147 if( (prop_str = (char*)xmlGetNsProp(_f, BAD_CAST "primitive", BAD_CAST PERSIST_NS)) ) {
155 if( (prop_str = (char*)xmlGetProp(_f, BAD_CAST "name")) ) {
161 osrfLogDebug(OSRF_LOG_MARK,
162 "Found field %s for class %s", prop_str, current_class_name );
164 osrfLogDebug(OSRF_LOG_MARK,
165 "Found field with no name for class %s", current_class_name );
176 if (!strcmp( (char*)_cur->name, "links" )) {
177 xmlNodePtr _l = _cur->children;
180 if (strcmp( (char*)_l->name, "link" )) {
185 osrfHash* link_def_hash = osrfNewHash();
187 if( (prop_str = (char*)xmlGetProp(_l, BAD_CAST "reltype")) ) {
193 osrfLogDebug(OSRF_LOG_MARK, "Adding link with reltype %s", prop_str );
195 osrfLogDebug(OSRF_LOG_MARK, "Adding link with no reltype" );
197 if( (prop_str = (char*)xmlGetProp(_l, BAD_CAST "key")) ) {
203 osrfLogDebug(OSRF_LOG_MARK, "Link fkey is %s", prop_str );
205 osrfLogDebug(OSRF_LOG_MARK, "Link with no fkey" );
207 if( (prop_str = (char*)xmlGetProp(_l, BAD_CAST "class")) ) {
213 osrfLogDebug(OSRF_LOG_MARK, "Link fclass is %s", prop_str );
215 osrfLogDebug(OSRF_LOG_MARK, "Link with no fclass" );
217 // Tokenize map attribute into an osrfStringArray
218 prop_str = (char*) xmlGetProp(_l, BAD_CAST "map");
220 osrfLogDebug(OSRF_LOG_MARK, "Link mapping list is %s", prop_str );
221 osrfStringArray* map = osrfStringArrayTokenize( prop_str, ' ' );
222 osrfHashSet( link_def_hash, map, "map");
224 if( (prop_str = (char*)xmlGetProp(_l, BAD_CAST "field")) ) {
230 osrfLogDebug(OSRF_LOG_MARK, "Link fclass is %s", prop_str );
232 osrfLogDebug(OSRF_LOG_MARK, "Link with no fclass" );
243 /**** Structure of permacrud in memory ****
246 { permission : [ x, y, z ],
247 global_required : "true", -- anything else, or missing, is false
248 local_context : [ f1, f2 ],
249 foreign_context : { class1 : { fkey : local_class_key, field : class1_field, context : [ a, b, c ] }, ...}
251 retrieve : null, -- no perm check, or structure similar to the others
252 update : -- like create
254 delete : -- like create
258 **** Structure of permacrud in memory ****/
260 if (!strcmp( (char*)_cur->name, "permacrud" )) {
261 osrfHashSet( class_def_hash, pcrud, "permacrud" );
262 xmlNodePtr _l = _cur->children;
265 if (strcmp( (char*)_l->name, "actions" )) {
270 xmlNodePtr _a = _l->children;
273 const char* action_name = (const char*) _a->name;
275 strcmp( action_name, "create" ) &&
276 strcmp( action_name, "retrieve" ) &&
277 strcmp( action_name, "update" ) &&
278 strcmp( action_name, "delete" )
284 osrfLogDebug(OSRF_LOG_MARK, "Found Permacrud action %s for class %s",
285 action_name, current_class_name );
287 osrfHash* action_def_hash = osrfNewHash();
288 osrfHashSet( pcrud, action_def_hash, action_name );
290 // Tokenize permission attribute into an osrfStringArray
291 prop_str = (char*) xmlGetProp(_a, BAD_CAST "permission");
293 osrfLogDebug(OSRF_LOG_MARK,
294 "Permacrud permission list is %s", prop_str );
295 osrfStringArray* map = osrfStringArrayTokenize( prop_str, ' ' );
296 osrfHashSet( action_def_hash, map, "permission");
298 osrfHashSet( action_def_hash,
299 (char*)xmlGetNoNsProp(_a, BAD_CAST "global_required"), "global_required");
301 // Tokenize context_field attribute into an osrfStringArray
302 prop_str = (char*) xmlGetProp(_a, BAD_CAST "context_field");
304 osrfLogDebug(OSRF_LOG_MARK,
305 "Permacrud context_field list is %s", prop_str );
306 map = osrfStringArrayTokenize( prop_str, ' ' );
307 osrfHashSet( action_def_hash, map, "local_context");
309 osrfHash* foreign_context = osrfNewHash();
310 osrfHashSet( action_def_hash, foreign_context, "foreign_context");
312 xmlNodePtr _f = _a->children;
315 if ( strcmp( (char*)_f->name, "context" ) ) {
320 if( (prop_str = (char*)xmlGetNoNsProp(_f, BAD_CAST "link")) ) {
321 osrfLogDebug(OSRF_LOG_MARK,
322 "Permacrud context link definition is %s", prop_str );
324 osrfHash* _tmp_fcontext = osrfNewHash();
326 // Store pointers to elements already stored
327 // from the <link> aggregate
328 osrfHash* _flink = osrfHashGet( current_links_hash, prop_str );
329 osrfHashSet( _tmp_fcontext, osrfHashGet(_flink, "field"), "fkey" );
330 osrfHashSet( _tmp_fcontext, osrfHashGet(_flink, "key"), "field" );
332 if( (prop_str = (char*)xmlGetNoNsProp(_f, BAD_CAST "jump")) )
333 osrfHashSet( _tmp_fcontext, osrfStringArrayTokenize( prop_str, '.' ), "jump" );
335 // Tokenize field attribute into an osrfStringArray
336 const char * field_list = (char*) xmlGetProp(_f, BAD_CAST "field");
338 osrfLogDebug(OSRF_LOG_MARK,
339 "Permacrud foreign context field list is %s", field_list );
340 map = osrfStringArrayTokenize( field_list, ' ' );
341 osrfHashSet( _tmp_fcontext, map, "context");
343 // Insert the new hash into a hash attached to the parent node
344 osrfHashSet( foreign_context, _tmp_fcontext, osrfHashGet( _flink, "class" ) );
348 if( (prop_str = (char*)xmlGetNoNsProp(_f, BAD_CAST "field") )) {
349 char* map_list = strdup( prop_str );
350 osrfLogDebug(OSRF_LOG_MARK,
351 "Permacrud local context field list is %s", prop_str );
353 if (strlen( map_list ) > 0) {
355 char* _map_class = strtok_r(map_list, " ", &st_tmp);
357 osrfHashGet( action_def_hash, "local_context"), _map_class);
359 while ((_map_class = strtok_r(NULL, " ", &st_tmp))) {
361 osrfHashGet( action_def_hash, "local_context"), _map_class);
376 if (!strcmp( (char*)_cur->name, "source_definition" )) {
377 const char* content_str;
378 if( (content_str = (char*)xmlNodeGetContent(_cur)) ) {
379 osrfLogDebug(OSRF_LOG_MARK, "Using source definition '%s' for class %s",
380 content_str, current_class_name );
383 strdup( content_str ),
397 osrfLogInfo(OSRF_LOG_MARK, "...IDL XML parsed");
402 osrfHash* oilsIDLFindPath( const char* path, ... ) {
403 if(!path || strlen(path) < 1) return NULL;
405 osrfHash* obj = idlHash;
407 VA_LIST_TO_STRING(path);
414 token = strtok_r(t, "/", &tt);
415 if(!token) return NULL;
418 obj = osrfHashGet(obj, token);
419 } while( (token = strtok_r(NULL, "/", &tt)) && obj);
424 static osrfHash* findClassDef( const char* classname ) {
425 if( !classname || !idlHash )
428 return osrfHashGet( idlHash, classname );
431 int oilsIDL_classIsFieldmapper ( const char* classname ) {
432 if( findClassDef( classname ) )
438 int oilsIDL_ntop (const char* classname, const char* fieldname) {
439 osrfHash* class_def_hash = findClassDef( classname );
440 if( !class_def_hash )
441 return -1; // No such class
443 osrfHash* fields_hash = osrfHashGet( class_def_hash, "fields" );
445 return -1; // No list of fields fo the class
447 osrfHash* field_def_hash = osrfHashGet( fields_hash, fieldname );
448 if( !field_def_hash )
449 return -1; // No such field
451 const char* pos_attr = osrfHashGet( field_def_hash, "array_position" );
453 return -1; // No array_position attribute
455 return atoi( pos_attr ); // Return position as int
458 char * oilsIDL_pton (const char* classname, int pos) {
459 osrfHash* class_def_hash = findClassDef( classname );
460 if( !class_def_hash )
461 return NULL; // No such class
463 osrfHash* fields_hash = osrfHashGet( class_def_hash, "fields" );
465 return NULL; // No list of fields fo the class
468 osrfHash* field_def_hash = NULL;
469 osrfHashIterator* iter = osrfNewHashIterator( fields_hash );
471 while ( ( field_def_hash = osrfHashIteratorNext( iter ) ) ) {
472 if ( atoi( osrfHashGet( field_def_hash, "array_position" ) ) == pos ) {
473 ret = strdup( osrfHashIteratorKey( iter ) );
478 osrfHashIteratorFree( iter );