1 /******************************************************************
2 * $Id: xsd2c.c,v 1.7 2004/10/15 13:35:39 snowdrop Exp $
3 *
4 * CSOAP Project: A SOAP client/server library in C
5 * Copyright (C) 2003 Ferhat Ayaz
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 *
22 * Email: ayaz@jprogrammer.net
23 ******************************************************************/
24
25 #include <libxml/xmlmemory.h>
26 #include <libxml/parser.h>
27
28
29 #include <stdio.h>
30 #include <string.h>
31 #include "obj.h"
32 #include "tr.h"
33 #include "formatter.h"
34 #include "xsd2c.h"
35
36 #include <sys/stat.h>
37
38 #define NODE_NAME_EQUALS(xmlnode, text) \
39 (!xmlStrcmp(xmlnode->name, (const xmlChar *)text))
40
41 static xmlNodePtr _xmlGetChild (xmlNodePtr node);
42 static xmlNodePtr _xmlGetNext (xmlNodePtr node);
43 static HCOMPLEXTYPE xsdProcComplexType (xmlNodePtr node, const char *type);
44
45 static xsdKeyword
xsdGetKeyword(xmlNodePtr node)46 xsdGetKeyword (xmlNodePtr node)
47 {
48 if (node == NULL)
49 return XSD_UNKNOWN;
50
51 if (NODE_NAME_EQUALS (node, XSD_ALL_STR))
52 return XSD_ALL;
53 if (NODE_NAME_EQUALS (node, XSD_ANNOTATION_STR))
54 return XSD_ANNOTATION;
55 if (NODE_NAME_EQUALS (node, XSD_ANY_STR))
56 return XSD_ANY;
57 if (NODE_NAME_EQUALS (node, XSD_ANY_ATTRIBUTE_STR))
58 return XSD_ANY_ATTRIBUTE;
59 if (NODE_NAME_EQUALS (node, XSD_APPINFO_STR))
60 return XSD_APPINFO;
61 if (NODE_NAME_EQUALS (node, XSD_ATTRIBUTE_STR))
62 return XSD_ATTRIBUTE;
63 if (NODE_NAME_EQUALS (node, XSD_ATTRIBUTE_GROUP_STR))
64 return XSD_ATTRIBUTE_GROUP;
65 if (NODE_NAME_EQUALS (node, XSD_CHOICE_STR))
66 return XSD_CHOICE;
67 if (NODE_NAME_EQUALS (node, XSD_COMPLEX_TYPE_STR))
68 return XSD_COMPLEX_TYPE;
69 if (NODE_NAME_EQUALS (node, XSD_COMPLEX_CONTENT_STR))
70 return XSD_COMPLEX_CONTENT;
71 if (NODE_NAME_EQUALS (node, XSD_DOCUMENTATION_STR))
72 return XSD_DOCUMENTATION;
73 if (NODE_NAME_EQUALS (node, XSD_ELEMENT_STR))
74 return XSD_ELEMENT;
75 if (NODE_NAME_EQUALS (node, XSD_ENUMERATION_STR))
76 return XSD_ENUMERATION;
77 if (NODE_NAME_EQUALS (node, XSD_EXTENSION_STR))
78 return XSD_EXTENSION;
79 if (NODE_NAME_EQUALS (node, XSD_FIELD_STR))
80 return XSD_FIELD;
81 if (NODE_NAME_EQUALS (node, XSD_GROUP_STR))
82 return XSD_GROUP;
83 if (NODE_NAME_EQUALS (node, XSD_IMPORT_STR))
84 return XSD_IMPORT;
85 if (NODE_NAME_EQUALS (node, XSD_INCLUDE_STR))
86 return XSD_INCLUDE;
87 if (NODE_NAME_EQUALS (node, XSD_KEY_STR))
88 return XSD_KEY;
89 if (NODE_NAME_EQUALS (node, XSD_KEYREF_STR))
90 return XSD_KEYREF;
91 if (NODE_NAME_EQUALS (node, XSD_LIST_STR))
92 return XSD_LIST;
93 if (NODE_NAME_EQUALS (node, XSD_NOTATION_STR))
94 return XSD_NOTATION;
95 if (NODE_NAME_EQUALS (node, XSD_REDEFINE_STR))
96 return XSD_REDEFINE;
97 if (NODE_NAME_EQUALS (node, XSD_RESTRICTION_STR))
98 return XSD_RESTRICTION;
99 if (NODE_NAME_EQUALS (node, XSD_SCHEMA_STR))
100 return XSD_SCHEMA;
101 if (NODE_NAME_EQUALS (node, XSD_SELECTOR_STR))
102 return XSD_SELECTOR;
103 if (NODE_NAME_EQUALS (node, XSD_SEQUENCE_STR))
104 return XSD_SEQUENCE;
105 if (NODE_NAME_EQUALS (node, XSD_SIMPLE_CONTENT_STR))
106 return XSD_SIMPLE_CONTENT;
107 if (NODE_NAME_EQUALS (node, XSD_SIMPLE_TYPE_STR))
108 return XSD_SIMPLE_TYPE;
109 if (NODE_NAME_EQUALS (node, XSD_UNION_STR))
110 return XSD_UNION;
111 if (NODE_NAME_EQUALS (node, XSD_UNIQUE_STR))
112 return XSD_UNIQUE;
113 if (NODE_NAME_EQUALS (node, XSD_MIN_INCLUSIVE_STR))
114 return XSD_MIN_INCLUSIVE;
115 if (NODE_NAME_EQUALS (node, XSD_MAX_INCLUSIVE_STR))
116 return XSD_MAX_INCLUSIVE;
117 if (NODE_NAME_EQUALS (node, XSD_MIN_EXCLUSIVE_STR))
118 return XSD_MIN_EXCLUSIVE;
119 if (NODE_NAME_EQUALS (node, XSD_MAX_EXCLUSIVE_STR))
120 return XSD_MAX_EXCLUSIVE;
121
122 return XSD_UNKNOWN;
123 }
124
125
126 static char outDir[1054];
127
128
129
130
131 static xmlNodePtr
xmlFindSubElement(xmlNodePtr root,const char * element_name)132 xmlFindSubElement (xmlNodePtr root, const char *element_name)
133 {
134 xmlNodePtr cur;
135
136 cur = root->xmlChildrenNode;
137 while (cur != NULL)
138 {
139
140 if (cur->type != XML_ELEMENT_NODE)
141 {
142 cur = cur->next;
143 continue;
144 }
145
146 if (!xmlStrcmp (cur->name, (const xmlChar *) element_name))
147 {
148 return cur;
149 }
150
151 cur = cur->next;
152 }
153
154 return NULL;
155 }
156
157 xmlNodePtr
xsdLoadFile(const char * filename)158 xsdLoadFile (const char *filename)
159 {
160 xmlDocPtr doc;
161 xmlNodePtr cur;
162
163 doc = xmlParseFile (filename);
164 if (doc == NULL)
165 return NULL;
166
167 cur = xmlDocGetRootElement (doc);
168
169 return cur;
170 }
171
172
173 xmlNodePtr
wsdlLoadFile(const char * filename)174 wsdlLoadFile (const char *filename)
175 {
176 xmlDocPtr doc;
177 xmlNodePtr cur;
178 xmlNodePtr sub;
179 xsdKeyword keyword;
180
181 doc = xmlParseFile (filename);
182 if (doc == NULL)
183 return NULL;
184
185 cur = xmlDocGetRootElement (doc);
186 if (cur == NULL)
187 {
188 return NULL;
189 }
190
191 cur = xmlFindSubElement (cur, "types");
192 if (cur == NULL)
193 return NULL;
194 /*
195 sub = xmlFindSubElement(cur, "schema");
196 if (sub != NULL)
197 return sub;
198 */
199 /* some wsdl's defines xsd without root <schema> element */
200 sub = _xmlGetChild (cur);
201 keyword = xsdGetKeyword (sub);
202 switch (keyword)
203 {
204 case XSD_ELEMENT:
205 case XSD_COMPLEX_TYPE:
206 case XSD_SIMPLE_TYPE:
207 case XSD_SCHEMA:
208 return sub;
209 default:
210 fprintf (stderr, "Unexpected node: '%s'\n", cur->name);
211 }
212
213 return NULL;
214 }
215
216
217 static xmlNodePtr
_xmlGetChild(xmlNodePtr node)218 _xmlGetChild (xmlNodePtr node)
219 {
220 xmlNodePtr cur = NULL;
221 cur = node->xmlChildrenNode;
222 while (cur != NULL)
223 {
224 if (cur->type != XML_ELEMENT_NODE)
225 {
226 cur = cur->next;
227 continue;
228 }
229 return cur;
230 }
231 return cur;
232 }
233
234 static xmlNodePtr
_xmlGetNext(xmlNodePtr node)235 _xmlGetNext (xmlNodePtr node)
236 {
237 xmlNodePtr cur = NULL;
238 cur = node->next;
239 while (cur != NULL)
240 {
241 if (cur->type != XML_ELEMENT_NODE)
242 {
243 cur = cur->next;
244 continue;
245 }
246 return cur;
247 }
248 return cur;
249 }
250
251 static void
xsdProcAttribute(HCOMPLEXTYPE parent,xmlNodePtr node)252 xsdProcAttribute (HCOMPLEXTYPE parent, xmlNodePtr node)
253 {
254 char *name, *type;
255 /* xmlNodePtr cur;
256 char buffer[1054];
257 */
258 name = xmlGetProp (node, ATTR_NAME_STR);
259 type = xmlGetProp (node, ATTR_TYPE_STR);
260
261 /* printf(" %s: %s\n", type?type:"(null)",
262 name?name:"(null)");
263 */
264 if (name == NULL)
265 {
266 fprintf (stderr, "WARNING: Attribute without name!\n");
267 return;
268 }
269
270 if (type == NULL)
271 {
272 fprintf (stderr, "WARNING: Attribute '%s' has no type\n", name);
273 }
274
275 /* sprintf(buffer, "attr_%s", name); */
276
277 objAddAttribute (parent, name, type, 0);
278 }
279
280 static void
xsdProcElement(HCOMPLEXTYPE parent,xmlNodePtr node)281 xsdProcElement (HCOMPLEXTYPE parent, xmlNodePtr node)
282 {
283 char *name, *type, *minostr, *maxostr;
284 xmlNodePtr cur;
285 xsdKeyword keyword;
286 char buffer[1054];
287 HCOMPLEXTYPE ct;
288 int mino, maxo;
289
290 name = xmlGetProp (node, ATTR_NAME_STR);
291 type = xmlGetProp (node, ATTR_TYPE_STR);
292 minostr = xmlGetProp (node, ATTR_MIN_OCCURS_STR);
293 maxostr = xmlGetProp (node, ATTR_MAX_OCCURS_STR);
294
295 /* printf(" %s: %s\n", type?type:"(null)",
296 name?name:"(null)");
297 */
298 if (minostr == NULL)
299 mino = 1;
300 else
301 mino = atoi (minostr);
302
303 if (maxostr == NULL)
304 maxo = 1;
305 else
306 {
307 if (!strcmp (maxostr, ATTR_VALUE_UNBOUNDED))
308 maxo = -1;
309 else
310 maxo = atoi (maxostr);
311 }
312
313
314 if (type == NULL)
315 {
316 /* check for complexType */
317 cur = _xmlGetChild (node);
318 if (cur == NULL)
319 {
320 fprintf (stderr, "WARNING: Element '%s' has no childs\n", name);
321 return;
322 }
323
324 do
325 {
326 keyword = xsdGetKeyword (cur);
327
328 switch (keyword)
329 {
330 case XSD_COMPLEX_TYPE:
331 /*
332 type = xmlGetProp(cur, ATTR_NAME_STR);
333 if (type == NULL)
334 {
335 fprintf(stderr, "WARNING: Type name not found\n");
336 break;
337 }
338 */
339
340 sprintf (buffer, "%s_%s", parent->type, (const char *) name);
341 ct = xsdProcComplexType (cur, (const char *) buffer);
342 if (ct != NULL)
343 {
344 objAddElement (parent, name, buffer, 0, mino, maxo);
345 }
346 break;
347
348 default:
349 fprintf (stderr, "Unexpected node: '%s'\n", cur->name);
350 }
351
352 } while ((cur = _xmlGetNext (cur)) != NULL);
353 }
354 else
355 {
356 objAddElement (parent, name, type, 0, mino, maxo);
357 }
358
359 /* if (name) xmlFree(name);
360 if (type) xmlFree(type);*/
361 }
362
363 static void
xsdProcSequence(HCOMPLEXTYPE ct,xmlNodePtr node)364 xsdProcSequence (HCOMPLEXTYPE ct, xmlNodePtr node)
365 {
366 xmlNodePtr cur = NULL;
367 xsdKeyword keyword;
368
369 cur = _xmlGetChild (node);
370 if (cur == NULL)
371 {
372 fprintf (stderr, "WARNING: Empty sequence\n");
373 return;
374 }
375
376 do
377 {
378 keyword = xsdGetKeyword (cur);
379
380 switch (keyword)
381 {
382 case XSD_ANNOTATION:
383 /* nothing to do */
384 break;
385
386 case XSD_GROUP:
387 fprintf (stderr, "WARNING: %s not supported\n", XSD_GROUP_STR);
388 break;
389
390 case XSD_CHOICE:
391 fprintf (stderr, "WARNING: %s not supported\n", XSD_CHOICE_STR);
392 break;
393
394 case XSD_SEQUENCE:
395 fprintf (stderr, "WARNING: %s not supported\n", XSD_SEQUENCE_STR);
396 break;
397
398 case XSD_ANY:
399 fprintf (stderr, "WARNING: %s not supported\n", XSD_ANY_STR);
400 break;
401
402 case XSD_ELEMENT:
403 xsdProcElement (ct, cur);
404 break;
405
406 default:
407 fprintf (stderr, "WARNING: Unknown child ('%s')!\n",
408 (char *) cur->name);
409 };
410 } while ((cur = _xmlGetNext (cur)) != NULL);
411 }
412
413
414 static void
xsdProcExtension(HCOMPLEXTYPE ct,xmlNodePtr node,const char * type)415 xsdProcExtension (HCOMPLEXTYPE ct, xmlNodePtr node, const char *type)
416 {
417 xmlNodePtr cur = NULL;
418 xsdKeyword keyword;
419 char *base;
420
421
422 base = xmlGetProp (node, ATTR_BASE_STR);
423 if (base == NULL)
424 {
425 fprintf (stderr, "WARNING: No base defined\n");
426 return;
427 }
428
429 printf (" =[Base] -> %s\n", base);
430 objSetBaseType (ct, base);
431 /* xmlFree(base);*/
432
433 cur = _xmlGetChild (node);
434 if (cur == NULL)
435 {
436 fprintf (stderr, "WARNING: Empty node\n");
437 return;
438 }
439
440 do
441 {
442 keyword = xsdGetKeyword (cur);
443
444 switch (keyword)
445 {
446 case XSD_ANNOTATION:
447 /* nothing to do */
448 break;
449
450 case XSD_ALL:
451 fprintf (stderr, " WARNING: %s not supported\n", XSD_ALL_STR);
452 break;
453
454 case XSD_GROUP:
455 fprintf (stderr, "WARNING: %s not supported\n", XSD_GROUP_STR);
456 break;
457
458 case XSD_CHOICE:
459 fprintf (stderr, "WARNING: %s not supported\n", XSD_CHOICE_STR);
460 break;
461
462 case XSD_ATTRIBUTE:
463 xsdProcAttribute (ct, cur);
464 break;
465
466 case XSD_ATTRIBUTE_GROUP:
467 fprintf (stderr, "WARNING: %s not supported\n",
468 XSD_ATTRIBUTE_GROUP_STR);
469 break;
470
471 case XSD_ANY_ATTRIBUTE:
472 fprintf (stderr, "WARNING: %s not supported\n", XSD_ANY_ATTRIBUTE_STR);
473 break;
474
475 case XSD_SEQUENCE:
476 xsdProcSequence (ct, cur);
477 break;
478
479 default:
480 fprintf (stderr, "WARNING: Unknown child ('%s')!\n",
481 (char *) cur->name);
482 };
483
484 } while ((cur = _xmlGetNext (cur)) != NULL);
485 }
486
487
488 static void
xsdProcComplexContent(HCOMPLEXTYPE ct,xmlNodePtr node,const char * type)489 xsdProcComplexContent (HCOMPLEXTYPE ct, xmlNodePtr node, const char *type)
490 {
491 xmlNodePtr cur = NULL;
492 xsdKeyword keyword;
493
494 cur = _xmlGetChild (node);
495 if (cur == NULL)
496 {
497 fprintf (stderr, "WARNING: Empty sequence\n");
498 return;
499 }
500
501 do
502 {
503 keyword = xsdGetKeyword (cur);
504
505 switch (keyword)
506 {
507 case XSD_ANNOTATION:
508 /* nothing to do */
509 break;
510
511 case XSD_EXTENSION:
512 xsdProcExtension (ct, cur, type);
513 break;
514
515 case XSD_RESTRICTION:
516 fprintf (stderr, "WARNING: %s not supported\n", XSD_RESTRICTION_STR);
517 break;
518
519 default:
520 fprintf (stderr, "WARNING: Unknown child ('%s')!\n",
521 (char *) cur->name);
522 };
523
524 } while ((cur = _xmlGetNext (cur)) != NULL);
525 }
526
527 void
xsdProcRestriction(HCOMPLEXTYPE ct,xmlNodePtr node,const char * type)528 xsdProcRestriction(HCOMPLEXTYPE ct, xmlNodePtr node, const char *type)
529 {
530 xmlNodePtr cur = NULL;
531 xsdKeyword keyword;
532 char *value;
533 HRESTRICTION res;
534 char *base;
535
536
537 base = xmlGetProp (node, ATTR_BASE_STR);
538 if (base == NULL)
539 {
540 fprintf (stderr, "WARNING: No base defined\n");
541 return;
542 }
543
544 printf (" =[Base] -> %s\n", base);
545 res = resCreate(base);
546 ct->restriction = res;
547
548 /* xmlFree(base);*/
549
550 cur = _xmlGetChild (node);
551 if (cur == NULL)
552 {
553 fprintf (stderr, "WARNING: Empty sequence\n");
554 return;
555 }
556
557 do
558 {
559 value = xmlGetProp (cur, ATTR_VALUE_STR);
560 if (!value) {
561 fprintf(stderr, "WARNING: Found SimpleContent->%s without attribute value.\n",
562 cur->name);
563 continue;
564 }
565
566 keyword = xsdGetKeyword (cur);
567
568 switch (keyword)
569 {
570 case XSD_MIN_INCLUSIVE:
571 res->minInclusive = atoi(value);
572 res->minInclusiveSet = 1;
573 res->mode = RES_MODE_MINMAX;
574 break;
575
576 case XSD_MAX_INCLUSIVE:
577 res->maxInclusive = atoi(value);
578 res->maxInclusiveSet = 1;
579 res->mode = RES_MODE_MINMAX;
580 break;
581
582 case XSD_MIN_EXCLUSIVE:
583 res->minExclusive = atoi(value);
584 res->minExclusiveSet = 1;
585 res->mode = RES_MODE_MINMAX;
586 break;
587
588 case XSD_MAX_EXCLUSIVE:
589 res->maxExclusive = atoi(value);
590 res->maxExclusiveSet = 1;
591 res->mode = RES_MODE_MINMAX;
592 break;
593
594 case XSD_ENUMERATION:
595 Enumeration_Add_value(res->enumeration, value);
596 res->mode = RES_MODE_ENUMERATION;
597 default:
598 fprintf (stderr, "WARNING: Unknown child (SimpleContent->'%s')!\n",
599 (char *) cur->name);
600 };
601
602 } while ((cur = _xmlGetNext (cur)) != NULL);
603
604 }
605
606 void
xsdProcSimpleContent(HCOMPLEXTYPE ct,xmlNodePtr node,const char * type)607 xsdProcSimpleContent (HCOMPLEXTYPE ct, xmlNodePtr node, const char *type)
608 {
609 xmlNodePtr cur = NULL;
610 xsdKeyword keyword;
611
612 ct->isSimpleContent = 1;
613
614 cur = _xmlGetChild (node);
615 if (cur == NULL)
616 {
617 fprintf (stderr, "WARNING: Empty node\n");
618 return;
619 }
620
621 do
622 {
623 keyword = xsdGetKeyword (cur);
624
625 switch (keyword)
626 {
627 case XSD_ANNOTATION:
628 /* nothing to do */
629 break;
630
631 case XSD_RESTRICTION:
632 xsdProcRestriction(ct, cur, type);
633 break;
634
635 case XSD_ATTRIBUTE:
636 xsdProcAttribute (ct, cur);
637 break;
638
639 default:
640 fprintf (stderr, "WARNING: Unknown child (SimpleContent->'%s')!\n",
641 (char *) cur->name);
642 };
643
644 } while ((cur = _xmlGetNext (cur)) != NULL);
645 }
646
647 static HCOMPLEXTYPE
xsdProcComplexType(xmlNodePtr node,const char * type)648 xsdProcComplexType (xmlNodePtr node, const char *type)
649 {
650 char *name;
651 xmlNodePtr cur = NULL;
652 xsdKeyword keyword;
653 HCOMPLEXTYPE ct;
654
655
656 if (!type)
657 name = xmlGetProp (node, ATTR_NAME_STR);
658 else
659 {
660 name = (char *) malloc (strlen (type) + 1);
661 strcpy (name, type);
662 }
663
664 if (!name)
665 {
666 fprintf (stderr, "\nWARNING: complexType has no typename!\n");
667 return NULL;
668 }
669
670 ct = objCreateComplexType (name);
671
672 printf ("\ncomplexType->%s\n", name);
673
674 cur = _xmlGetChild (node);
675 if (cur == NULL)
676 {
677 fprintf (stderr, "WARNING: Empty complexType\n");
678 return ct;
679 }
680
681
682
683 do
684 {
685 keyword = xsdGetKeyword (cur);
686
687 switch (keyword)
688 {
689 case XSD_ANNOTATION:
690 /* nothing to do */
691 break;
692
693 case XSD_SIMPLE_CONTENT:
694 xsdProcSimpleContent (ct, cur, name);
695 break;
696
697 case XSD_COMPLEX_CONTENT:
698 xsdProcComplexContent (ct, cur, name);
699 /* fprintf(stderr, "WARNING: %s not supported\n", XSD_COMPLEX_CONTENT_STR); */
700 break;
701
702 case XSD_ALL:
703 fprintf (stderr, "WARNING: %s not supported\n", XSD_ALL_STR);
704 break;
705
706 case XSD_GROUP:
707 fprintf (stderr, "WARNING: %s not supported\n", XSD_GROUP_STR);
708 break;
709
710 case XSD_CHOICE:
711 fprintf (stderr, "WARNING: %s not supported\n", XSD_CHOICE_STR);
712 break;
713
714 case XSD_ATTRIBUTE:
715 xsdProcAttribute (ct, cur);
716 break;
717
718 case XSD_ATTRIBUTE_GROUP:
719 fprintf (stderr, "WARNING: %s not supported\n",
720 XSD_ATTRIBUTE_GROUP_STR);
721 break;
722
723 case XSD_ANY_ATTRIBUTE:
724 fprintf (stderr, "WARNING: %s not supported\n", XSD_ANY_ATTRIBUTE_STR);
725 break;
726
727 case XSD_SEQUENCE:
728 xsdProcSequence (ct, cur);
729 break;
730
731 default:
732 fprintf (stderr, "WARNING: Unknown child ('%s')!\n",
733 (char *) cur->name);
734 };
735
736 } while ((cur = _xmlGetNext (cur)) != NULL);
737
738 /* xmlFree(name);*/
739 return ct;
740 }
741
742
743
744 static void
runGenerator(xmlNodePtr xsdRoot)745 runGenerator (xmlNodePtr xsdRoot)
746 {
747 xmlNodePtr cur;
748 xmlNodePtr node;
749 xmlChar *type;
750
751 cur = xsdRoot->xmlChildrenNode;
752 while (cur != NULL)
753 {
754
755 if (cur->type != XML_ELEMENT_NODE)
756 {
757 cur = cur->next;
758 continue;
759 }
760
761 if (xsdGetKeyword (cur) == XSD_COMPLEX_TYPE)
762 {
763
764 xsdProcComplexType (cur, NULL);
765
766 }
767 else if (xsdGetKeyword (cur) == XSD_SIMPLE_TYPE)
768 {
769
770 fprintf (stderr, "WARNING: SimpleType not supported!\n");
771
772 }
773 else if (xsdGetKeyword (cur) == XSD_ELEMENT)
774 {
775
776 type = xmlGetProp (cur, "name");
777 if (type == NULL)
778 {
779 fprintf (stderr, "WARNING: Element found without name ('%s')\n",
780 cur->name);
781 }
782 else
783 {
784
785 node = xmlFindSubElement (cur, XSD_COMPLEX_TYPE_STR);
786 if (node != NULL)
787 {
788 xsdProcComplexType (node, type);
789 }
790 else
791 {
792 /* fprintf (stderr, "WARNING: Element on root node will be ignored\n"); */
793 /* TODO: I don't know if this is a good idea!?
794 make typedef instead.*/
795 xsdProcComplexType(cur, type);
796 }
797 }
798 /*xsdProcElement(..., cur); */
799 }
800 else
801 {
802 fprintf(stderr, "WARNING: '%s' not supported!\n", cur->name);
803 }
804
805 cur = cur->next;
806 }
807 }
808
809 int
declareStructs(HCOMPLEXTYPE ct)810 declareStructs (HCOMPLEXTYPE ct)
811 {
812 char fname[255];
813 FILE *f;
814
815 sprintf (fname, "%s/%s.h", outDir, ct->type); /* _xsd */
816 printf ("Generating file '%s' ...\n", fname);
817 f = fopen (fname, "w");
818 if (f == NULL)
819 {
820 fprintf (stderr, "Can not open '%s'\n", fname);
821 return 0;
822 }
823
824 writeComplexTypeHeaderFile (f, ct);
825 fclose (f);
826
827 return 1;
828 }
829
830 int
writeSource(HCOMPLEXTYPE ct)831 writeSource (HCOMPLEXTYPE ct)
832 {
833 char fname[255];
834 FILE *f;
835
836 sprintf (fname, "%s/%s.c", outDir, ct->type); /* _xsd */
837 printf ("Generating file '%s' ...\n", fname);
838 f = fopen (fname, "w");
839 if (f == NULL)
840 {
841 fprintf (stderr, "Can not open '%s'\n", fname);
842 return 0;
843 }
844
845 writeComplexTypeSourceFile (f, ct);
846 fclose (f);
847
848 return 1;
849 }
850
851
852 int
xsdInitTrModule(xmlNodePtr xsdNode)853 xsdInitTrModule (xmlNodePtr xsdNode)
854 {
855 xmlNsPtr ns = NULL;
856 char xsd_ns[50];
857
858 ns =
859 xmlSearchNsByHref (xsdNode->doc, xsdNode,
860 "http://www.w3.org/2001/XMLSchema");
861 if (ns == NULL)
862 ns =
863 xmlSearchNsByHref (xsdNode->doc, xsdNode,
864 "http://www.w3.org/2001/XMLSchema/");
865 if (ns == NULL)
866 ns =
867 xmlSearchNsByHref (xsdNode->doc, xsdNode,
868 "http://www.w3.org/1999/XMLSchema");
869 if (ns == NULL)
870 ns =
871 xmlSearchNsByHref (xsdNode->doc, xsdNode,
872 "http://www.w3.org/1999/XMLSchema/");
873 if (ns == NULL)
874 ns =
875 xmlSearchNsByHref (xsdNode->doc, xmlDocGetRootElement (xsdNode->doc),
876 "http://www.w3.org/2001/XMLSchema");
877 if (ns == NULL)
878 ns =
879 xmlSearchNsByHref (xsdNode->doc, xmlDocGetRootElement (xsdNode->doc),
880 "http://www.w3.org/2001/XMLSchema/");
881 if (ns == NULL)
882 ns =
883 xmlSearchNsByHref (xsdNode->doc, xmlDocGetRootElement (xsdNode->doc),
884 "http://www.w3.org/1999/XMLSchema");
885 if (ns == NULL)
886 ns =
887 xmlSearchNsByHref (xsdNode->doc, xmlDocGetRootElement (xsdNode->doc),
888 "http://www.w3.org/1999/XMLSchema/");
889
890 /*
891 if (ns != NULL && ns->prefix != NULL) {
892 fprintf(stdout, "XMLSchema namespace prefix: '%s'\n", ns->prefix);
893 trInitModule(ns->prefix);
894 } else { */
895 /*
896 Search for:
897 <definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema">
898 <type>
899 <schema xmlns="http://www.w3.org/2001/XMLSchema">
900 ...
901 */
902 /*if (ns != NULL && ns->prefix != NULL) {
903 fprintf(stdout, "XMLSchema namespace prefix: '%s'\n", ns->prefix);
904 trInitModule(ns->prefix);
905 } else {
906 printf("Initializing XML Schema type register with default 'xs'\n");
907 trInitModule("xs");
908 }
909 }
910
911 } else {
912 printf("Initializing XML Schema type register with default 'xs'\n");
913 trInitModule("xs");
914 }
915 */
916 if (ns != NULL && ns->prefix != NULL)
917 {
918 strcpy (xsd_ns, ns->prefix);
919 }
920 else
921 {
922 fprintf (stderr, "WARNING: using XML Schema prefix 'xsd' as default!\n");
923 strcpy (xsd_ns, "xsd");
924 }
925
926 trInitModule ();
927
928 fprintf(stdout, "XML Schema prefix: '%s'\n", xsd_ns);
929
930 trRegisterTypeNS (xsd_ns, "ID", "char*", 1);
931 trRegisterTypeNS (xsd_ns, "IDREF", "char*", 1);
932 trRegisterTypeNS (xsd_ns, "IDREFS", "char*", 1);
933 trRegisterTypeNS (xsd_ns, "string", "char*", 1);
934 trRegisterTypeNS (xsd_ns, "integer", "int", 1);
935 trRegisterTypeNS (xsd_ns, "int", "int", 1);
936 trRegisterTypeNS (xsd_ns, "double", "double", 1);
937 trRegisterTypeNS (xsd_ns, "float", "float", 1);
938 trRegisterTypeNS (xsd_ns, "boolean", "int", 1);
939
940 ns = xmlSearchNsByHref (xsdNode->doc, xsdNode, "http://www.w3.org/2003/05/soap-encoding");
941 if (ns == NULL)
942 ns = xmlSearchNsByHref (xsdNode->doc, xsdNode, "http://www.w3.org/2003/05/soap-encoding/");
943 if (ns != NULL && ns->prefix != NULL) {
944 strcpy(xsd_ns, ns->prefix);
945 trRegisterTypeNS (xsd_ns, "ID", "char*", 1);
946 trRegisterTypeNS (xsd_ns, "IDREF", "char*", 1);
947 trRegisterTypeNS (xsd_ns, "IDREFS", "char*", 1);
948 trRegisterTypeNS (xsd_ns, "string", "char*", 1);
949 trRegisterTypeNS (xsd_ns, "integer", "int", 1);
950 trRegisterTypeNS (xsd_ns, "int", "int", 1);
951 trRegisterTypeNS (xsd_ns, "double", "double", 1);
952 trRegisterTypeNS (xsd_ns, "float", "float", 1);
953 trRegisterTypeNS (xsd_ns, "boolean", "int", 1);
954 }
955
956 return 1;
957 }
958
959
960 int
xsdInitObjModule(xmlNodePtr xsdNode)961 xsdInitObjModule (xmlNodePtr xsdNode)
962 {
963 xmlChar *tns = NULL;
964 xmlNsPtr ns;
965
966 if (xsdNode != NULL)
967 tns = xmlGetProp (xsdNode, (const xmlChar *) "targetNamespace");
968
969 if (tns == NULL)
970 {
971
972 objInitModule (NULL);
973
974 }
975 else
976 {
977
978 ns = xmlSearchNsByHref (xsdNode->doc, xsdNode, tns);
979 if (ns == NULL)
980 {
981 fprintf (stderr, "WARNING: Target namespace not found!\n");
982 return 0;
983 }
984
985 if (ns->prefix == NULL)
986 {
987 fprintf (stderr, "WARNING: Target namespace not found!\n");
988 return 0;
989 }
990
991 fprintf (stdout, "Target namespace ('%s') prefix: '%s'\n", tns,
992 ns->prefix);
993 objInitModule (ns->prefix);
994
995 }
996
997
998 return 1;
999 }
1000
1001 void
xsdSetDestDir(const char * destDir)1002 xsdSetDestDir (const char *destDir)
1003 {
1004 strcpy (outDir, destDir);
1005
1006 #ifdef __MINGW32__
1007 mkdir (destDir);
1008 #else
1009 mkdir (destDir, S_IRUSR | S_IWUSR | S_IXUSR |
1010 S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH);
1011 #endif
1012 }
1013
1014 int
xsdEngineRun(xmlNodePtr xsdNode,const char * destDir)1015 xsdEngineRun (xmlNodePtr xsdNode, const char *destDir)
1016 {
1017
1018
1019 xsdSetDestDir (destDir);
1020
1021 if (xsdNode != NULL)
1022 {
1023 runGenerator (xsdNode);
1024 objRegistryEnumComplexType (declareStructs);
1025 objRegistryEnumComplexType (writeSource);
1026 }
1027
1028
1029 return 0;
1030 }
1031