5 #include <libxml/globals.h>
6 #include <libxml/xmlerror.h>
7 #include <libxml/parser.h>
8 #include <libxml/tree.h>
9 #include <libxml/debugXML.h>
10 #include <libxml/xmlmemory.h>
12 #define PERSIST_NS "http://open-ils.org/spec/opensrf/IDL/persistance/v1"
13 #define OBJECT_NS "http://open-ils.org/spec/opensrf/IDL/objects/v1"
14 #define BASE_NS "http://opensrf.org/spec/IDL/base/v1"
16 xmlDocPtr idlDoc = NULL; // parse and store the IDL here
18 /* parse and store the IDL here */
21 osrfHash* oilsIDL() { return idlHash; }
22 osrfHash* oilsIDLInit( char* idl_filename ) {
24 if (idlHash) return idlHash;
26 char* string_tmp = NULL;
28 idlHash = osrfNewHash();
29 osrfHash* usrData = NULL;
31 osrfLogInfo(OSRF_LOG_MARK, "Parsing the IDL XML...");
32 idlDoc = xmlReadFile( idl_filename, NULL, XML_PARSE_XINCLUDE );
35 osrfLogError(OSRF_LOG_MARK, "Could not load or parse the IDL XML file!");
39 osrfLogInfo(OSRF_LOG_MARK, "Initializing the Fieldmapper IDL...");
41 xmlNodePtr docRoot = xmlDocGetRootElement(idlDoc);
42 xmlNodePtr kid = docRoot->children;
44 if (!strcmp( (char*)kid->name, "class" )) {
46 usrData = osrfNewHash();
47 osrfHashSet( usrData, xmlGetProp(kid, "id"), "classname");
48 osrfHashSet( usrData, xmlGetNsProp(kid, "fieldmapper", OBJECT_NS), "fieldmapper");
50 osrfHashSet( idlHash, usrData, (char*)osrfHashGet(usrData, "classname") );
53 if ((string_tmp = (char*)xmlGetNsProp(kid, "tablename", PERSIST_NS))) {
62 if ((string_tmp = (char*)xmlGetNsProp(kid, "virtual", PERSIST_NS))) {
71 osrfHash* links = osrfNewHash();
72 osrfHash* fields = osrfNewHash();
74 osrfHashSet( usrData, fields, "fields" );
75 osrfHashSet( usrData, links, "links" );
77 xmlNodePtr _cur = kid->children;
81 if (!strcmp( (char*)_cur->name, "fields" )) {
84 if( (string_tmp = (char*)xmlGetNsProp(_cur, "primary", PERSIST_NS)) ) {
93 if( (string_tmp = (char*)xmlGetNsProp(_cur, "sequence", PERSIST_NS)) ) {
101 xmlNodePtr _f = _cur->children;
104 if (strcmp( (char*)_f->name, "field" )) {
109 _tmp = osrfNewHash();
112 if( (string_tmp = (char*)xmlGetNsProp(_f, "array_position", OBJECT_NS)) ) {
115 strdup( string_tmp ),
121 if( (string_tmp = (char*)xmlGetNsProp(_f, "virtual", PERSIST_NS)) ) {
124 strdup( string_tmp ),
130 if( (string_tmp = (char*)xmlGetNsProp(_f, "primitive", PERSIST_NS)) ) {
133 strdup( string_tmp ),
139 if( (string_tmp = (char*)xmlGetProp(_f, "name")) ) {
142 strdup( string_tmp ),
147 osrfLogInfo(OSRF_LOG_MARK, "Found field %s for class %s", string_tmp, osrfHashGet(usrData, "classname") );
158 if (!strcmp( (char*)_cur->name, "links" )) {
159 xmlNodePtr _l = _cur->children;
162 if (strcmp( (char*)_l->name, "link" )) {
167 _tmp = osrfNewHash();
170 if( (string_tmp = (char*)xmlGetProp(_l, "reltype")) ) {
173 strdup( string_tmp ),
177 osrfLogInfo(OSRF_LOG_MARK, "Adding link with reltype %s", string_tmp );
180 if( (string_tmp = (char*)xmlGetProp(_l, "key")) ) {
183 strdup( string_tmp ),
187 osrfLogInfo(OSRF_LOG_MARK, "Link fkey is %s", string_tmp );
190 if( (string_tmp = (char*)xmlGetProp(_l, "class")) ) {
193 strdup( string_tmp ),
197 osrfLogInfo(OSRF_LOG_MARK, "Link fclass is %s", string_tmp );
199 osrfStringArray* map = osrfNewStringArray(0);
202 if( (string_tmp = (char*)xmlGetProp(_l, "map") )) {
203 char* map_list = strdup( string_tmp );
204 osrfLogInfo(OSRF_LOG_MARK, "Link mapping list is %s", string_tmp );
206 if (strlen( map_list ) > 0) {
208 char* _map_class = strtok_r(map_list, " ", &st_tmp);
209 osrfStringArrayAdd(map, strdup(_map_class));
211 while ((_map_class = strtok_r(NULL, " ", &st_tmp))) {
212 osrfStringArrayAdd(map, strdup(_map_class));
216 osrfHashSet( _tmp, map, "map");
219 if( (string_tmp = (char*)xmlGetProp(_l, "field")) ) {
222 strdup( string_tmp ),
233 osrfLogInfo(OSRF_LOG_MARK, "Found link %s for class %s", string_tmp, osrfHashGet(usrData, "classname") );
246 osrfLogInfo(OSRF_LOG_MARK, "...IDL XML parsed");
251 osrfHash* oilsIDLFindPath( char* path, ... ) {
252 if(!path || strlen(path) < 1) return NULL;
254 osrfHash* obj = idlHash;
256 VA_LIST_TO_STRING(path);
263 token = strtok_r(t, "/", &tt);
264 if(!token) return NULL;
267 obj = osrfHashGet(obj, token);
268 } while( (token = strtok_r(NULL, "/", &tt)) && obj);