summaryrefslogtreecommitdiffstats
path: root/xsd2c/xsd2c.c
blob: 973dbfa1db8581cc625a2a6b1cf0276657181484 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
/******************************************************************
 *  $Id: xsd2c.c,v 1.1 2004/06/02 11:17:03 snowdrop Exp $
 *
 * CSOAP Project:  A SOAP client/server library in C
 * Copyright (C) 2003  Ferhat Ayaz
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 * 
 * Email: ayaz@jprogrammet.net
 ******************************************************************/

#include <libxml/xmlmemory.h>
#include <libxml/parser.h>


#include <stdio.h>
#include "obj.h"
#include "tr.h"
#include "formatter.h"


#define NODE_NAME_EQUALS(xmlnode, text) \
  (!xmlStrcmp(xmlnode->name, (const xmlChar *)text))


#define   XSD_ALL_STR   "all"
#define   XSD_ANNOTATION_STR		"annotation"
#define   XSD_ANY_STR		""
#define   XSD_ANY_ATTRIBUTE_STR		"any"
#define   XSD_APPINFO_STR		"appInfo"
#define   XSD_ATTRIBUTE_STR		"attribute"
#define   XSD_ATTRIBUTE_GROUP_STR		"attributeGroup"
#define   XSD_CHOICE_STR		"choice"
#define   XSD_COMPLEX_TYPE_STR		"complexType"
#define   XSD_COMPLEX_CONTENT_STR		"complexContent"
#define   XSD_DOCUMENTATION_STR		"documentation"
#define   XSD_ELEMENT_STR		"element"
#define   XSD_EXTENSION_STR		"extension"
#define   XSD_FIELD_STR		"field"
#define   XSD_GROUP_STR		"group"
#define   XSD_IMPORT_STR		"import"
#define   XSD_INCLUDE_STR		"include"
#define   XSD_KEY_STR		"key"
#define   XSD_KEYREF_STR		"keyref"
#define   XSD_LIST_STR		"list"
#define   XSD_NOTATION_STR		"notation"
#define   XSD_REDEFINE_STR		"redefine"
#define   XSD_RESTRICTION_STR		"restriction"
#define   XSD_SCHEMA_STR		"schema"
#define   XSD_SELECTOR_STR		"selector"
#define   XSD_SEQUENCE_STR		"sequence"
#define   XSD_SIMPLE_CONTENT_STR		"simpleContent"
#define   XSD_SIMPLE_TYPE_STR		"simpleType"
#define   XSD_UNION_STR		"union"
#define   XSD_UNIQUE_STR		"unique"

#define   ATTR_TYPE_STR "type"
#define   ATTR_NAME_STR "name"
#define   ATTR_BASE_STR "base"
#define   ATTR_MIN_OCCURS_STR "minOccurs"
#define   ATTR_MAX_OCCURS_STR "maxOccurs"

#define   ATTR_VALUE_UNBOUNDED "unbounded"

enum _xsdAttr
{
  ATTR_UNKNOWN,
  ATTR_TYPE,
  ATTR_NAME,
  ATTR_BASE,
  ATTR_MIN_OCCURS,
  ATTR_MAX_OCCURS
};

enum _xsdKeyword 
{
  XSD_UNKNOWN,
  XSD_ALL,
  XSD_ANNOTATION, 
  XSD_ANY, 
  XSD_ANY_ATTRIBUTE, 
  XSD_APPINFO, 
  XSD_ATTRIBUTE, 
  XSD_ATTRIBUTE_GROUP, 
  XSD_CHOICE, 
  XSD_COMPLEX_TYPE, 
  XSD_COMPLEX_CONTENT, 
  XSD_DOCUMENTATION, 
  XSD_ELEMENT, 
  XSD_EXTENSION, 
  XSD_FIELD, 
  XSD_GROUP, 
  XSD_IMPORT, 
  XSD_INCLUDE, 
  XSD_KEY, 
  XSD_KEYREF, 
  XSD_LIST, 
  XSD_NOTATION, 
  XSD_REDEFINE, 
  XSD_RESTRICTION, 
  XSD_SCHEMA, 
  XSD_SELECTOR, 
  XSD_SEQUENCE, 
  XSD_SIMPLE_CONTENT, 
  XSD_SIMPLE_TYPE, 
  XSD_UNION, 
  XSD_UNIQUE
};

typedef enum _xsdKeyword xsdKeyword; 
typedef enum _xsdAttr xsdAttr; 

static xmlNodePtr _xmlGetChild(xmlNodePtr node);
static xmlNodePtr _xmlGetNext(xmlNodePtr node);
static HCOMPLEXTYPE xsdProcComplexType(xmlNodePtr node, const char* type);

static 
xsdKeyword xsdGetKeyword(xmlNodePtr node)
{
  if (node == NULL) return XSD_UNKNOWN;

  if (NODE_NAME_EQUALS(node, XSD_ALL_STR)) return XSD_ALL;
  if (NODE_NAME_EQUALS(node, XSD_ANNOTATION_STR)) return XSD_ANNOTATION;
  if (NODE_NAME_EQUALS(node, XSD_ANY_STR)) return XSD_ANY;
  if (NODE_NAME_EQUALS(node, XSD_ANY_ATTRIBUTE_STR)) return XSD_ANY_ATTRIBUTE;
  if (NODE_NAME_EQUALS(node, XSD_APPINFO_STR)) return XSD_APPINFO;
  if (NODE_NAME_EQUALS(node, XSD_ATTRIBUTE_STR)) return XSD_ATTRIBUTE;
  if (NODE_NAME_EQUALS(node, XSD_ATTRIBUTE_GROUP_STR)) return XSD_ATTRIBUTE_GROUP;
  if (NODE_NAME_EQUALS(node, XSD_CHOICE_STR)) return XSD_CHOICE;
  if (NODE_NAME_EQUALS(node, XSD_COMPLEX_TYPE_STR)) return XSD_COMPLEX_TYPE;
  if (NODE_NAME_EQUALS(node, XSD_COMPLEX_CONTENT_STR)) return XSD_COMPLEX_CONTENT;
  if (NODE_NAME_EQUALS(node, XSD_DOCUMENTATION_STR)) return XSD_DOCUMENTATION;
  if (NODE_NAME_EQUALS(node, XSD_ELEMENT_STR)) return XSD_ELEMENT;
  if (NODE_NAME_EQUALS(node, XSD_EXTENSION_STR)) return XSD_EXTENSION;
  if (NODE_NAME_EQUALS(node, XSD_FIELD_STR)) return XSD_FIELD;
  if (NODE_NAME_EQUALS(node, XSD_GROUP_STR)) return XSD_GROUP;
  if (NODE_NAME_EQUALS(node, XSD_IMPORT_STR)) return XSD_IMPORT;
  if (NODE_NAME_EQUALS(node, XSD_INCLUDE_STR)) return XSD_INCLUDE;
  if (NODE_NAME_EQUALS(node, XSD_KEY_STR)) return XSD_KEY;
  if (NODE_NAME_EQUALS(node, XSD_KEYREF_STR)) return XSD_KEYREF;
  if (NODE_NAME_EQUALS(node, XSD_LIST_STR)) return XSD_LIST;
  if (NODE_NAME_EQUALS(node, XSD_NOTATION_STR)) return XSD_NOTATION;
  if (NODE_NAME_EQUALS(node, XSD_REDEFINE_STR)) return XSD_REDEFINE;
  if (NODE_NAME_EQUALS(node, XSD_RESTRICTION_STR)) return XSD_RESTRICTION;
  if (NODE_NAME_EQUALS(node, XSD_SCHEMA_STR)) return XSD_SCHEMA;
  if (NODE_NAME_EQUALS(node, XSD_SELECTOR_STR)) return XSD_SELECTOR;
  if (NODE_NAME_EQUALS(node, XSD_SEQUENCE_STR)) return XSD_SEQUENCE;
  if (NODE_NAME_EQUALS(node, XSD_SIMPLE_CONTENT_STR)) return XSD_SIMPLE_CONTENT;
  if (NODE_NAME_EQUALS(node, XSD_SIMPLE_TYPE_STR)) return XSD_SIMPLE_TYPE;
  if (NODE_NAME_EQUALS(node, XSD_UNION_STR)) return XSD_UNION;
  if (NODE_NAME_EQUALS(node, XSD_UNIQUE_STR)) return XSD_UNIQUE;

  return XSD_UNKNOWN;
}


static char outDir[1054];



static 
void usage();


static 
xmlNodePtr xmlFindSubElement(xmlNodePtr root, const char* element_name)
{
  xmlNodePtr cur;

  cur = root->xmlChildrenNode;
  while (cur != NULL) {
    
    if (cur->type != XML_ELEMENT_NODE) {
      cur = cur->next;
      continue;
    }

    if (!xmlStrcmp(cur->name, (const xmlChar*)element_name)) {
      return cur;
    }

    cur = cur->next;
  }

  return NULL;
}

static
xmlNodePtr xsdLoadFile(const char* filename)
{
  xmlDocPtr doc;
  xmlNodePtr cur;

  doc = xmlParseFile(filename);
  if (doc == NULL) return NULL;

  cur = xmlDocGetRootElement(doc);

  return cur;
}


static
xmlNodePtr wsdlLoadFile(const char* filename)
{
  xmlDocPtr doc;
  xmlNodePtr cur;
  xmlNodePtr sub;
  xsdKeyword keyword;

  doc = xmlParseFile(filename);
  if (doc == NULL) return NULL;

  cur = xmlDocGetRootElement(doc);
  if (cur == NULL) {
    return NULL;
  }

  cur = xmlFindSubElement(cur, "types");
  if (cur == NULL) return NULL;
/*
  sub = xmlFindSubElement(cur, "schema");
  if (sub != NULL)
    return sub;
*/
  /* some wsdl's defines xsd without root <schema> element */
  sub = _xmlGetChild(cur);
  keyword = xsdGetKeyword(sub);
  switch (keyword) 
  {
    case XSD_ELEMENT:
    case XSD_COMPLEX_TYPE:
    case XSD_SIMPLE_TYPE:
    case XSD_SCHEMA:
      return sub;
  }

  return NULL;
}


static 
xmlNodePtr _xmlGetChild(xmlNodePtr node)
{
  xmlNodePtr cur = NULL;
  cur = node->xmlChildrenNode;
  while (cur != NULL) {
    if (cur->type != XML_ELEMENT_NODE) {
      cur = cur->next;
      continue;
    }
    return cur;
  }
  return cur;
}

static 
xmlNodePtr _xmlGetNext(xmlNodePtr node)
{
  xmlNodePtr cur = NULL;
  cur = node->next;
  while (cur != NULL) {
    if (cur->type != XML_ELEMENT_NODE) {
      cur = cur->next;
      continue;
    }
    return cur;
  }
  return cur;
}

static 
void xsdProcAttribute(HCOMPLEXTYPE parent, xmlNodePtr node)
{
  char *name, *type;
  xmlNodePtr cur;
  char buffer[1054];
  
  name = xmlGetProp(node, ATTR_NAME_STR);
  type = xmlGetProp(node, ATTR_TYPE_STR);

 /* printf("  %s: %s\n", type?type:"(null)",
    name?name:"(null)");
*/
  if (name == NULL)
  {
    fprintf(stderr, "WARNING: Attribute without name!\n");
    return;
  }

  if (type == NULL)
  {
    fprintf(stderr, "WARNING: Attribute '%s' has no type\n", name);
  }

/*  sprintf(buffer, "attr_%s", name); */

  objAddAttribute(parent, name, type, 0);
}

static 
void xsdProcElement(HCOMPLEXTYPE parent, xmlNodePtr node)
{
  char *name, *type, *minostr, *maxostr;
  xmlNodePtr cur;
  xsdKeyword keyword;
  char buffer[1054];
  HCOMPLEXTYPE ct;
  int mino, maxo;
  
  name = xmlGetProp(node, ATTR_NAME_STR);
  type = xmlGetProp(node, ATTR_TYPE_STR);
  minostr = xmlGetProp(node, ATTR_MIN_OCCURS_STR);
  maxostr = xmlGetProp(node, ATTR_MAX_OCCURS_STR);

/*  printf("  %s: %s\n", type?type:"(null)",
    name?name:"(null)");
*/
  if (minostr == NULL) mino = 1;
  else mino = atoi(minostr);

  if (maxostr == NULL) maxo = 1;
  else {
    if (!strcmp(maxostr, ATTR_VALUE_UNBOUNDED))
      maxo = -1;
    else
      maxo = atoi(maxostr);
  }
  

  if (type == NULL)
  {
    /* check for complexType */
    cur = _xmlGetChild(node);
    if (cur == NULL)
    {
      fprintf(stderr, "WARNING: Element '%s' has no childs\n", name);
      return;
    }

    do
    {
      keyword = xsdGetKeyword(cur);

      switch (keyword)
      {
        case XSD_COMPLEX_TYPE:
          /*
          type = xmlGetProp(cur, ATTR_NAME_STR);
          if (type == NULL) 
          {
            fprintf(stderr, "WARNING: Type name not found\n");
            break;
          }
          */
          
          sprintf(buffer, "%s_%s", parent->type, (const char*)name);
          ct = xsdProcComplexType(cur, (const char*)buffer);
          if (ct != NULL)
          {
            objAddElement(parent, name, buffer,0, mino, maxo);
          }
          break;
      }

    } while ((cur = _xmlGetNext(cur)) != NULL);
  }
  else
  {
    objAddElement(parent, name, type,0, mino, maxo);
  }

  if (name) xmlFree(name);
  if (type) xmlFree(type);
}

static
void xsdProcSequence(HCOMPLEXTYPE ct, xmlNodePtr node)
{
  xmlNodePtr cur = NULL;
  xsdKeyword keyword;

  cur = _xmlGetChild(node);
  if (cur == NULL) {
    fprintf(stderr, "WARNING: Empty sequence\n");
    return;
  }

  do {
    keyword = xsdGetKeyword(cur);

    switch (keyword) 
    {
      case XSD_ANNOTATION:
        /* nothing to do*/
        break;

      case XSD_GROUP:
        fprintf(stderr, "WARNING: %s not supported\n", XSD_GROUP_STR);
        break;

      case XSD_CHOICE:
        fprintf(stderr, "WARNING: %s not supported\n", XSD_CHOICE_STR);
        break;

      case XSD_SEQUENCE:
        fprintf(stderr, "WARNING: %s not supported\n", XSD_SEQUENCE_STR);
        break;

      case XSD_ANY:
        fprintf(stderr, "WARNING: %s not supported\n", XSD_ANY_STR);
        break;

      case XSD_ELEMENT:
        xsdProcElement(ct, cur);
        break;

      default:
         fprintf(stderr, "WARNING: Unknown child ('%s')!\n", (char*)cur->name);
    };
   } while ((cur = _xmlGetNext(cur)) != NULL);
}


static
void xsdProcExtension(HCOMPLEXTYPE ct, xmlNodePtr node, const char* type)
{
  xmlNodePtr cur = NULL;
  xsdKeyword keyword;
  char * base;


  base = xmlGetProp(node, ATTR_BASE_STR);
  if (base == NULL) {
    fprintf(stderr, "WARNING: No base defined\n");
    return;
  }
  
  printf(" =[Base] -> %s\n", base);
  objSetBaseType(ct, base);
  xmlFree(base);

  cur = _xmlGetChild(node);
  if (cur == NULL) {
    fprintf(stderr, "WARNING: Empty node\n");
    return;
  }

  do {
    keyword = xsdGetKeyword(cur);

    switch (keyword) 
    {
      case XSD_ANNOTATION:
        /* nothing to do*/
        break;

      case XSD_ALL:
        fprintf(stderr, " WARNING: %s not supported\n", XSD_ALL_STR);
        break;

      case XSD_GROUP:
        fprintf(stderr, "WARNING: %s not supported\n", XSD_GROUP_STR);
        break;

      case XSD_CHOICE:
        fprintf(stderr, "WARNING: %s not supported\n", XSD_CHOICE_STR);
        break;

      case XSD_ATTRIBUTE:
        xsdProcAttribute(ct, cur);
        break;

      case XSD_ATTRIBUTE_GROUP:
        fprintf(stderr, "WARNING: %s not supported\n", XSD_ATTRIBUTE_GROUP_STR);
        break;

      case XSD_ANY_ATTRIBUTE:
        fprintf(stderr, "WARNING: %s not supported\n", XSD_ANY_ATTRIBUTE_STR);
        break;

       case XSD_SEQUENCE:
         xsdProcSequence(ct, cur);
         break;
        
       default:
         fprintf(stderr, "WARNING: Unknown child ('%s')!\n", (char*)cur->name);
    };
    
  } while ((cur = _xmlGetNext(cur)) != NULL);
}


static
void xsdProcComplexContent(HCOMPLEXTYPE ct, xmlNodePtr node, const char* type)
{
  xmlNodePtr cur = NULL;
  xsdKeyword keyword;

  cur = _xmlGetChild(node);
  if (cur == NULL) {
    fprintf(stderr, "WARNING: Empty sequence\n");
    return;
  }

  do {
    keyword = xsdGetKeyword(cur);

    switch (keyword) 
    {
      case XSD_ANNOTATION:
        /* nothing to do*/
        break;

      case XSD_EXTENSION:
        xsdProcExtension(ct, cur, type);
        break;

      case XSD_RESTRICTION:
        fprintf(stderr, "WARNING: %s not supported\n", XSD_RESTRICTION_STR);
        break;

       default:
         fprintf(stderr, "WARNING: Unknown child ('%s')!\n", (char*)cur->name);
    };
    
  } while ((cur = _xmlGetNext(cur)) != NULL);
}

static 
HCOMPLEXTYPE xsdProcComplexType(xmlNodePtr node, const char* type)
{
  char *name;
  xmlNodePtr cur = NULL;
  xsdKeyword keyword;
  HCOMPLEXTYPE ct;

  if (!type)
    name = xmlGetProp(node, ATTR_NAME_STR);
  else {
    name = (char*)malloc(strlen(type)+1);
    strcpy(name, type);
  }

  if (!name)
  {
    fprintf(stderr, "\nWARNING: complexType has no typename!\n", name);
    return NULL;
  }
  
  ct = objCreateComplexType(name); 
  
  printf("\ncomplexType->%s\n", name);
  
  cur = _xmlGetChild(node);
  if (cur == NULL) {
    fprintf(stderr, "WARNING: Empty complexType\n");
    return ct;
  }



  do {
    keyword = xsdGetKeyword(cur);

    switch (keyword) 
    {
      case XSD_ANNOTATION:
        /* nothing to do*/
        break;

      case XSD_SIMPLE_CONTENT:
        fprintf(stderr, "WARNING: %s not supported\n", XSD_SIMPLE_CONTENT_STR);
        break;

      case XSD_COMPLEX_CONTENT:
        xsdProcComplexContent(ct, cur, name);
      /*  fprintf(stderr, "WARNING: %s not supported\n", XSD_COMPLEX_CONTENT_STR); */
        break;

      case XSD_ALL:
        fprintf(stderr, "WARNING: %s not supported\n", XSD_ALL_STR);
        break;

      case XSD_GROUP:
        fprintf(stderr, "WARNING: %s not supported\n", XSD_GROUP_STR);
        break;

      case XSD_CHOICE:
        fprintf(stderr, "WARNING: %s not supported\n", XSD_CHOICE_STR);
        break;

      case XSD_ATTRIBUTE:
        xsdProcAttribute(ct, cur);
        break;

      case XSD_ATTRIBUTE_GROUP:
        fprintf(stderr, "WARNING: %s not supported\n", XSD_ATTRIBUTE_GROUP_STR);
        break;

      case XSD_ANY_ATTRIBUTE:
        fprintf(stderr, "WARNING: %s not supported\n", XSD_ANY_ATTRIBUTE_STR);
        break;

       case XSD_SEQUENCE:
         xsdProcSequence(ct, cur);
         break;
        
       default:
         fprintf(stderr, "WARNING: Unknown child ('%s')!\n", (char*)cur->name);
    };
    
  } while ((cur = _xmlGetNext(cur)) != NULL);

  xmlFree(name);
  return ct;
}



static 
void runGenerator(xmlNodePtr xsdRoot)
{
  xmlNodePtr cur;
  xmlNodePtr node;
  xmlChar *type;

  cur = xsdRoot->xmlChildrenNode;
  while (cur != NULL) {

    if (cur->type != XML_ELEMENT_NODE) {
      cur = cur->next;
      continue;
    }

    if (xsdGetKeyword(cur) == XSD_COMPLEX_TYPE){
      
      xsdProcComplexType(cur, NULL);

		}  else if (xsdGetKeyword(cur) == XSD_ELEMENT) {
      
      type = xmlGetProp(cur, "name");
      if (type == NULL) {
        fprintf(stderr, "WARNING: Element found without name  ('%s')\n", cur->name);
      } else {
        
        node = xmlFindSubElement(cur, XSD_COMPLEX_TYPE_STR);
        if (node != NULL) {
          xsdProcComplexType(node, type);
        }

      }

    }

    cur = cur->next;
  }
}

int declareStructs(HCOMPLEXTYPE ct)
{
  char fname[255];
  FILE* f;

  sprintf(fname, "%s/%s_xsd.h", outDir, ct->type);
  printf("Generating file '%s' ...\n", fname);
  f = fopen(fname, "w");
  if (f == NULL)
  {
    fprintf(stderr, "Can not open '%s'\n", fname);
    return 0;
  }

  writeComplexTypeHeaderFile(f, ct);
  fclose(f);

  return 1;
}

int writeSource(HCOMPLEXTYPE ct)
{
  char fname[255];
  FILE* f;

  sprintf(fname, "%s/%s_xsd.c", outDir, ct->type);
  printf("Generating file '%s' ...\n", fname);
  f = fopen(fname, "w");
  if (f == NULL)
  {
    fprintf(stderr, "Can not open '%s'\n", fname);
    return 0;
  }

  writeComplexTypeSourceFile(f, ct);
  fclose(f);

  return 1;
}


int xsdInitTrModule(xmlNodePtr xsdNode)
{
  xmlNsPtr ns;

  ns = xmlSearchNsByHref(xsdNode->doc, xsdNode, "http://www.w3.org/2001/XMLSchema");
  if (ns == NULL) {
    fprintf(stderr, "XML Schema namespace not found!\n");
    return 0;
  } 

  if (ns->prefix == NULL) {
    fprintf(stderr, "XML Schema namespace not found!\n");
    return 0;
  }

  fprintf(stdout, "XMLSchema namespace prefix: '%s'\n", ns->prefix);
  trInitModule(ns->prefix);

  return 1;
}


int xsdInitObjModule(xmlNodePtr xsdNode)
{
  xmlChar *tns;
  xmlNsPtr ns;

  tns = xmlGetProp(xsdNode, (const xmlChar*)"targetNamespace");

  if (tns == NULL) {
   
    objInitModule(NULL);

  } else {

    ns = xmlSearchNsByHref(xsdNode->doc, xsdNode, tns);
    if (ns == NULL) {
      fprintf(stderr, "WARNING: Target namespace not found!\n");
      return 0;
    } 

    if (ns->prefix == NULL) {
      fprintf(stderr, "WARNING: Target namespace not found!\n");
      return 0;
    }

    fprintf(stdout, "Target namespace ('%s') prefix: '%s'\n", tns, ns->prefix);
    objInitModule(ns->prefix);

  }


  return 1;
}


int main(int argc, char *argv[])
{
  int i;
  xmlNodePtr xsdNode = NULL;
  char fname[255];
  int wsdl = 0;

  if (argc < 2) {
    usage(argv[0]);
    return 1;
  }
 
  
 strcpy(outDir, ".");

  for (i=1;i<argc;i++) 
  {
    if (!strcmp(argv[i], "-d"))
      if (i==argc-1) usage(argv[0]);
      else strcpy(outDir, argv[++i]);
    else if (!strcmp(argv[i], "-S"))
      formatter_generate_sax_serializer = 1;
    else if (!strcmp(argv[i], "-wsdl"))
      wsdl = 1;
    else strcpy(fname, argv[i]);
  }
  
  mkdir(outDir, 000);

  if (wsdl)
    xsdNode = wsdlLoadFile(fname);
  else
    xsdNode = xsdLoadFile(fname);

  if (xsdNode == NULL) {
    fprintf(stderr, "can not load xsd file!\n");
    return 1;
  }

  if (!xsdInitTrModule(xsdNode))
    return 1;

  if (!xsdInitObjModule(xsdNode))
    return 1;

  runGenerator(xsdNode);
  objRegistryEnumComplexType(declareStructs);
  objRegistryEnumComplexType(writeSource);

  xmlFreeDoc(xsdNode->doc);
  trFreeModule();
  objFreeModule();

  return 0;
}



void usage(const char* execName) 
{
  printf("usage: %s [-d <destdir> -S -D] <xsd filename>\n", execName);
}