2 * C Implementation: dump_idl
7 * Author: Scott McKellar <scott@esilibrary.com>, (C) 2009
9 * Copyright: See COPYING file that comes with this distribution
15 #include <opensrf/string_array.h>
16 #include <opensrf/osrf_hash.h>
17 #include <openils/oils_idl.h>
19 static void dump_idl( osrfHash* IDLHash );
20 static void dump_class( osrfHash* class_hash, const char* class_name );
21 static void dump_fields( osrfHash* field_hash );
22 static void dump_one_field( osrfHash* field_hash, const char* field_name );
23 static void dump_links( osrfHash* links_hash );
24 static void dump_one_link( osrfHash* link_hash, const char* link_name );
25 static void dump_permacrud( osrfHash* pcrud_hash );
26 static void dump_action( osrfHash* action_hash, const char* action_name );
27 static void dump_foreign_context( osrfHash* fc_hash );
28 static void dump_fc_class( osrfHash* fc_class_hash, const char* class_name );
29 static void dump_string_array(
30 osrfStringArray* sarr, const char* name, const char* indent );
32 int main( int argc, char* argv[] ) {
35 // Suppress informational messages
36 osrfLogSetLevel( OSRF_LOG_WARNING );
38 // Get name of IDL file, if specified on command line
39 const char* IDL_filename = NULL;
40 int filename_expected = 0; // boolean
42 for( i = 1; i < argc; ++i ) {
43 const char* arg = argv[ i ];
44 printf( "%s\n", arg );
45 if( filename_expected ) {
47 filename_expected = 0;
49 if( '-' == arg[ 0 ] && 'f' == arg[1] ) {
51 fprintf( stderr, "Only one IDL file may be specified\n" );
55 IDL_filename = arg + 2;
57 filename_expected = 1;
65 if( filename_expected ) {
66 fprintf( stderr, "IDL filename expected on command line, not found\n" );
70 // No filename? Look in the environment
72 IDL_filename = getenv( "OILS_IDL_FILENAME" );
74 // Still no filename? Apply a default
76 IDL_filename = "/openils/conf/fm_IDL.xml";
78 printf( "IDL filename: %s\n", IDL_filename );
80 osrfHash* IDL = oilsIDLInit( IDL_filename );
82 fputs( "Failed to build IDL\n", stderr );
87 // No classes specified? Dump them all
90 // Dump the requested classes
91 dump_class( osrfHashGet( IDL, argv[ i ] ), argv[ i ] );
98 static void dump_idl( osrfHash* IDLHash ) {
102 if( 0 == osrfHashGetCount( IDLHash ) )
105 osrfHashIterator* iter = osrfNewHashIterator( IDLHash );
106 osrfHash* class_hash = NULL;
110 class_hash = osrfHashIteratorNext( iter );
112 dump_class( class_hash, osrfHashIteratorKey( iter ) );
117 osrfHashIteratorFree( iter );
120 static void dump_class( osrfHash* class_hash, const char* class_name )
122 if( !class_hash || !class_name )
125 if( 0 == osrfHashGetCount( class_hash ) )
128 printf( "Class %s\n", class_name );
129 const char* indent = " ";
131 osrfHashIterator* iter = osrfNewHashIterator( class_hash );
133 // Dump each attribute, etc. of the class hash
135 void* class_attr = osrfHashIteratorNext( iter );
137 const char* attr_name = osrfHashIteratorKey( iter );
138 if( !strcmp( attr_name, "classname" ) )
139 printf( "%s%s: %s\n", indent, attr_name, (char*) class_attr );
140 else if( !strcmp( attr_name, "fieldmapper" ) )
141 printf( "%s%s: %s\n", indent, attr_name, (char*) class_attr );
142 else if( !strcmp( attr_name, "tablename" ) )
143 printf( "%s%s: %s\n", indent, attr_name, (char*) class_attr );
144 else if( !strcmp( attr_name, "restrict_primary" ) )
145 printf( "%s%s: %s\n", indent, attr_name, (char*) class_attr );
146 else if( !strcmp( attr_name, "virtual" ) )
147 printf( "%s%s: %s\n", indent, attr_name, (char*) class_attr );
148 else if( !strcmp( attr_name, "controller" ) )
149 dump_string_array( (osrfStringArray*) class_attr, attr_name, indent );
150 else if( !strcmp( attr_name, "fields" ) )
151 dump_fields( (osrfHash*) class_attr );
152 else if( !strcmp( attr_name, "links" ) )
153 dump_links( (osrfHash*) class_attr );
154 else if( !strcmp( attr_name, "primarykey" ) )
155 printf( "%s%s: %s\n", indent, attr_name, (char*) class_attr );
156 else if( !strcmp( attr_name, "sequence" ) )
157 printf( "%s%s: %s\n", indent, attr_name, (char*) class_attr );
158 else if( !strcmp( attr_name, "permacrud" ) )
159 dump_permacrud( (osrfHash*) class_attr );
160 else if( !strcmp( attr_name, "source_definition" ) )
161 printf( "%s%s:\n%s\n", indent, attr_name, (char*) class_attr );
163 printf( "%s%s (unknown)\n", indent, attr_name );
169 static void dump_fields( osrfHash* fields_hash ) {
170 if( NULL == fields_hash )
173 if( 0 == osrfHashGetCount( fields_hash ) )
176 fputs( " fields\n", stdout );
178 osrfHashIterator* iter = osrfNewHashIterator( fields_hash );
179 osrfHash* fields_attr = NULL;
183 fields_attr = osrfHashIteratorNext( iter );
185 dump_one_field( fields_attr, osrfHashIteratorKey( iter ) );
190 osrfHashIteratorFree( iter );
193 static void dump_one_field( osrfHash* field_hash, const char* field_name ) {
194 if( !field_hash || !field_name )
197 if( 0 == osrfHashGetCount( field_hash ) )
200 printf( " %s\n", field_name );
202 osrfHashIterator* iter = osrfNewHashIterator( field_hash );
203 const char* field_attr = NULL;
204 const char* indent = " ";
206 // Dump each field attribute
208 field_attr = osrfHashIteratorNext( iter );
210 printf( "%s%s: %s\n", indent, osrfHashIteratorKey( iter ), field_attr );
215 osrfHashIteratorFree( iter );
218 static void dump_links( osrfHash* links_hash ) {
219 if( NULL == links_hash )
222 if( 0 == osrfHashGetCount( links_hash ) )
225 fputs( " links\n", stdout );
227 osrfHashIterator* iter = osrfNewHashIterator( links_hash );
228 osrfHash* links_attr = NULL;
232 links_attr = osrfHashIteratorNext( iter );
234 dump_one_link( links_attr, osrfHashIteratorKey( iter ) );
239 osrfHashIteratorFree( iter );
242 static void dump_one_link( osrfHash* link_hash, const char* link_name ) {
243 if( !link_hash || !link_name )
246 if( 0 == osrfHashGetCount( link_hash ) )
249 printf( " %s\n", link_name );
251 osrfHashIterator* iter = osrfNewHashIterator( link_hash );
252 const void* link_attr = NULL;
253 const char* indent = " ";
255 // Dump each link attribute
257 link_attr = osrfHashIteratorNext( iter );
259 const char* link_attr_name = osrfHashIteratorKey( iter );
260 if( !strcmp( link_attr_name, "reltype" ) )
261 printf( "%s%s: %s\n", indent, link_attr_name, (char*) link_attr );
262 else if( !strcmp( link_attr_name, "key" ) )
263 printf( "%s%s: %s\n", indent, link_attr_name, (char*) link_attr );
264 else if( !strcmp( link_attr_name, "class" ) )
265 printf( "%s%s: %s\n", indent, link_attr_name, (char*) link_attr );
266 else if( !strcmp( link_attr_name, "map" ) )
267 dump_string_array( (osrfStringArray*) link_attr, link_attr_name, indent );
268 else if( !strcmp( link_attr_name, "field" ) )
269 printf( "%s%s: %s\n", indent, link_attr_name, (char*) link_attr );
271 printf( "%s%s (unknown)\n", indent, link_attr_name );
276 osrfHashIteratorFree( iter );
279 static void dump_permacrud( osrfHash* pcrud_hash ) {
280 if( NULL == pcrud_hash )
283 if( 0 == osrfHashGetCount( pcrud_hash ) )
286 fputs( " permacrud\n", stdout );
288 osrfHashIterator* iter = osrfNewHashIterator( pcrud_hash );
289 osrfHash* pcrud_attr = NULL;
293 pcrud_attr = osrfHashIteratorNext( iter );
295 dump_action( pcrud_attr, osrfHashIteratorKey( iter ) );
300 osrfHashIteratorFree( iter );
303 static void dump_action( osrfHash* action_hash, const char* action_name ) {
304 if( !action_hash || !action_name )
307 if( 0 == osrfHashGetCount( action_hash ) )
310 printf( " %s\n", action_name );
312 osrfHashIterator* iter = osrfNewHashIterator( action_hash );
313 void* action_attr = NULL;
314 const char* indent = " ";
316 // Dump each attribute of the action
318 action_attr = osrfHashIteratorNext( iter );
320 const char* attr_name = osrfHashIteratorKey( iter );
321 if( !strcmp( attr_name, "permission" ) )
322 dump_string_array( action_attr, attr_name, indent );
323 else if( !strcmp( attr_name, "global_required" ) )
324 printf( "%s%s: %s\n", indent, attr_name, (char*) action_attr );
325 else if( !strcmp( attr_name, "local_context" ) )
326 dump_string_array( action_attr, attr_name, indent );
327 else if( !strcmp( attr_name, "foreign_context" ) )
328 dump_foreign_context( action_attr );
330 printf( "%s%s (unknown)\n", indent, attr_name );
335 osrfHashIteratorFree( iter );
338 static void dump_foreign_context( osrfHash* fc_hash ) {
342 if( 0 == osrfHashGetCount( fc_hash ) )
345 fputs( " foreign_context\n", stdout );
347 osrfHashIterator* iter = osrfNewHashIterator( fc_hash );
348 osrfHash* fc_attr = NULL;
350 // Dump each foreign context attribute
352 fc_attr = osrfHashIteratorNext( iter );
354 dump_fc_class( (osrfHash*) fc_attr, osrfHashIteratorKey( iter ) );
359 osrfHashIteratorFree( iter );
362 static void dump_fc_class( osrfHash* fc_class_hash, const char* class_name )
364 if( ! fc_class_hash )
367 if( 0 == osrfHashGetCount( fc_class_hash ) )
370 printf( " %s\n", class_name );
372 osrfHashIterator* iter = osrfNewHashIterator( fc_class_hash );
373 void* fc_class_attr = NULL;
374 const char* indent = " ";
376 // Dump each foreign context attribute
378 fc_class_attr = osrfHashIteratorNext( iter );
379 if( fc_class_attr ) {
380 const char* fc_class_attr_name = osrfHashIteratorKey( iter );
381 if( !strcmp( fc_class_attr_name, "field" ) )
382 printf( "%s%s: %s\n", indent, fc_class_attr_name, (const char*) fc_class_attr );
383 else if( !strcmp( fc_class_attr_name, "fkey" ) )
384 printf( "%s%s: %s\n", indent, fc_class_attr_name, (const char*) fc_class_attr );
385 else if( !strcmp( fc_class_attr_name, "jump" ) )
386 dump_string_array( (osrfStringArray*) fc_class_attr, fc_class_attr_name, indent );
387 else if( !strcmp( fc_class_attr_name, "context" ) )
388 dump_string_array( (osrfStringArray*) fc_class_attr, fc_class_attr_name, indent );
390 printf( "%s%s\n", indent, fc_class_attr_name );
395 osrfHashIteratorFree( iter );
398 static void dump_string_array(
399 osrfStringArray* sarr, const char* name, const char* indent ) {
400 if( !sarr || !name || !indent )
403 int size = sarr->size;
405 // Ignore an empty array
409 printf( "%s%s (string array)\n", indent, name );
412 for( i = 0; i < size; ++i )
413 printf( "%s\t%s\n", indent, osrfStringArrayGetString( sarr, i ) );