Imported Upstream version 1.4.1
[routino] / src / xml / xsd-to-xmlparser.c
1 /***************************************
2  $Header: /home/amb/routino/src/xml/RCS/xsd-to-xmlparser.c,v 1.10 2010/04/23 18:41:20 amb Exp $
3
4  An XML parser for simplified XML Schema Definitions to create XML parser skeletons.
5
6  Part of the Routino routing software.
7  ******************/ /******************
8  This file Copyright 2010 Andrew M. Bishop
9
10  This program is free software: you can redistribute it and/or modify
11  it under the terms of the GNU Affero General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU Affero General Public License for more details.
19
20  You should have received a copy of the GNU Affero General Public License
21  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  ***************************************/
23
24
25 #include <stdio.h>
26 #include <ctype.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "xmlparse.h"
31
32
33 /*+ A forward definition of the xmltagx +*/
34 typedef struct _xmltagx xmltagx;
35
36
37 /*+ A structure to hold the extended definition of a tag. +*/
38 struct _xmltagx
39 {
40  char *name;                              /*+ The name of the tag. +*/
41  char *type;                              /*+ The type of the tag. +*/
42
43  int  nattributes;                        /*+ The number of valid attributes for the tag. +*/
44  char *attributes[XMLPARSE_MAX_ATTRS];    /*+ The valid attributes for the tag. +*/
45
46  int nsubtagsx;                           /*+ The number of valid attributes for the tag. +*/
47  xmltagx *subtagsx[XMLPARSE_MAX_SUBTAGS]; /*+ The list of types for the subtags contained within this one. +*/
48 };
49
50
51 /* The local variables and functions */
52
53 int ntagsx=0;
54 xmltagx **tagsx=NULL;
55 char *currenttype=NULL;
56
57 static char *safe(const char *name);
58
59
60 /* The XML tag processing function prototypes */
61
62 static int xmlDeclaration_function(const char *_tag_,int _type_,const char *version,const char *encoding);
63 static int schemaType_function(const char *_tag_,int _type_,const char *elementFormDefault,const char *xmlns_xsd);
64 static int complexType_function(const char *_tag_,int _type_,const char *name);
65 static int attributeType_function(const char *_tag_,int _type_,const char *name,const char *type);
66 static int sequenceType_function(const char *_tag_,int _type_);
67 static int elementType_function(const char *_tag_,int _type_,const char *name,const char *type,const char *minOccurs,const char *maxOccurs);
68
69
70 /* The XML tag definitions */
71
72 /*+ The elementType type tag. +*/
73 static xmltag elementType_tag=
74               {"xsd:element",
75                4, {"name","type","minOccurs","maxOccurs"},
76                elementType_function,
77                {NULL}};
78
79 /*+ The sequenceType type tag. +*/
80 static xmltag sequenceType_tag=
81               {"xsd:sequence",
82                0, {NULL},
83                sequenceType_function,
84                {&elementType_tag,NULL}};
85
86 /*+ The attributeType type tag. +*/
87 static xmltag attributeType_tag=
88               {"xsd:attribute",
89                2, {"name","type"},
90                attributeType_function,
91                {NULL}};
92
93 /*+ The complexType type tag. +*/
94 static xmltag complexType_tag=
95               {"xsd:complexType",
96                1, {"name"},
97                complexType_function,
98                {&sequenceType_tag,&attributeType_tag,NULL}};
99
100 /*+ The schemaType type tag. +*/
101 static xmltag schemaType_tag=
102               {"xsd:schema",
103                2, {"elementFormDefault","xmlns:xsd"},
104                schemaType_function,
105                {&elementType_tag,&complexType_tag,NULL}};
106
107 /*+ The xmlDeclaration type tag. +*/
108 static xmltag xmlDeclaration_tag=
109               {"xml",
110                2, {"version","encoding"},
111                xmlDeclaration_function,
112                {NULL}};
113
114
115 /*+ The complete set of tags at the top level. +*/
116 static xmltag *xml_toplevel_tags[]={&xmlDeclaration_tag,&schemaType_tag,NULL};
117
118
119 /* The XML tag processing functions */
120
121
122 /*++++++++++++++++++++++++++++++++++++++
123   The function that is called when the elementType XSD type is seen
124
125   int elementType_function Returns 0 if no error occured or something else otherwise.
126
127   const char *_tag_ Set to the name of the element tag that triggered this function call.
128
129   int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.
130
131   const char *name The contents of the 'name' attribute (or NULL if not defined).
132
133   const char *type The contents of the 'type' attribute (or NULL if not defined).
134
135   const char *minOccurs The contents of the 'minOccurs' attribute (or NULL if not defined).
136
137   const char *maxOccurs The contents of the 'maxOccurs' attribute (or NULL if not defined).
138   ++++++++++++++++++++++++++++++++++++++*/
139
140 static int elementType_function(const char *_tag_,int _type_,const char *name,const char *type,const char *minOccurs,const char *maxOccurs)
141 {
142  xmltagx *tagx=NULL;
143  int i;
144
145  if(_type_==XMLPARSE_TAG_END)
146     return(0);
147
148  for(i=0;i<ntagsx;i++)
149     if(!strcmp(type,tagsx[i]->type) && !strcmp(name,tagsx[i]->name))
150        tagx=tagsx[i];
151
152  if(!tagx)
153    {
154     ntagsx++;
155     tagsx=(xmltagx**)realloc((void*)tagsx,ntagsx*sizeof(xmltagx*));
156
157     tagsx[ntagsx-1]=(xmltagx*)calloc(1,sizeof(xmltagx));
158     tagsx[ntagsx-1]->name=strcpy(malloc(strlen(name)+1),name);
159     tagsx[ntagsx-1]->type=strcpy(malloc(strlen(type)+1),type);
160
161     tagx=tagsx[ntagsx-1];
162    }
163
164  if(!currenttype)
165     return(0);
166
167  for(i=0;i<ntagsx;i++)
168     if(!strcmp(tagsx[i]->type,currenttype))
169       {
170        tagsx[i]->subtagsx[tagsx[i]->nsubtagsx]=tagx;
171        tagsx[i]->nsubtagsx++;
172
173        if(tagsx[i]->nsubtagsx==XMLPARSE_MAX_SUBTAGS)
174          {fprintf(stderr,"Too many subtags seen for type '%s'.\n",currenttype); exit(1);}
175       }
176
177  return(0);
178 }
179
180
181 /*++++++++++++++++++++++++++++++++++++++
182   The function that is called when the sequenceType XSD type is seen
183
184   int sequenceType_function Returns 0 if no error occured or something else otherwise.
185
186   const char *_tag_ Set to the name of the element tag that triggered this function call.
187
188   int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.
189   ++++++++++++++++++++++++++++++++++++++*/
190
191 static int sequenceType_function(const char *_tag_,int _type_)
192 {
193  return(0);
194 }
195
196
197 /*++++++++++++++++++++++++++++++++++++++
198   The function that is called when the attributeType XSD type is seen
199
200   int attributeType_function Returns 0 if no error occured or something else otherwise.
201
202   const char *_tag_ Set to the name of the element tag that triggered this function call.
203
204   int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.
205
206   const char *name The contents of the 'name' attribute (or NULL if not defined).
207
208   const char *type The contents of the 'type' attribute (or NULL if not defined).
209   ++++++++++++++++++++++++++++++++++++++*/
210
211 static int attributeType_function(const char *_tag_,int _type_,const char *name,const char *type)
212 {
213  int i;
214
215  if(_type_==XMLPARSE_TAG_END)
216     return(0);
217
218  for(i=0;i<ntagsx;i++)
219     if(!strcmp(tagsx[i]->type,currenttype))
220       {
221        tagsx[i]->attributes[tagsx[i]->nattributes]=strcpy(malloc(strlen(name)+1),name);
222        tagsx[i]->nattributes++;
223
224        if(tagsx[i]->nattributes==XMLPARSE_MAX_ATTRS)
225          {fprintf(stderr,"Too many attributes seen for type '%s'.\n",currenttype); exit(1);}
226       }
227
228  return(0);
229 }
230
231
232 /*++++++++++++++++++++++++++++++++++++++
233   The function that is called when the complexType XSD type is seen
234
235   int complexType_function Returns 0 if no error occured or something else otherwise.
236
237   const char *_tag_ Set to the name of the element tag that triggered this function call.
238
239   int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.
240
241   const char *name The contents of the 'name' attribute (or NULL if not defined).
242   ++++++++++++++++++++++++++++++++++++++*/
243
244 static int complexType_function(const char *_tag_,int _type_,const char *name)
245 {
246  if(_type_==XMLPARSE_TAG_END)
247     return(0);
248
249  currenttype=strcpy(realloc(currenttype,strlen(name)+1),name);
250
251  return(0);
252 }
253
254
255 /*++++++++++++++++++++++++++++++++++++++
256   The function that is called when the schemaType XSD type is seen
257
258   int schemaType_function Returns 0 if no error occured or something else otherwise.
259
260   const char *_tag_ Set to the name of the element tag that triggered this function call.
261
262   int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.
263
264   const char *elementFormDefault The contents of the 'elementFormDefault' attribute (or NULL if not defined).
265
266   const char *xmlns_xsd The contents of the 'xmlns:xsd' attribute (or NULL if not defined).
267   ++++++++++++++++++++++++++++++++++++++*/
268
269 static int schemaType_function(const char *_tag_,int _type_,const char *elementFormDefault,const char *xmlns_xsd)
270 {
271  return(0);
272 }
273
274
275 /*++++++++++++++++++++++++++++++++++++++
276   The function that is called when the XML declaration is seen
277
278   int xmlDeclaration_function Returns 0 if no error occured or something else otherwise.
279
280   const char *_tag_ Set to the name of the element tag that triggered this function call.
281
282   int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.
283
284   const char *version The contents of the 'version' attribute (or NULL if not defined).
285
286   const char *encoding The contents of the 'encoding' attribute (or NULL if not defined).
287   ++++++++++++++++++++++++++++++++++++++*/
288
289 static int xmlDeclaration_function(const char *_tag_,int _type_,const char *version,const char *encoding)
290 {
291  return(0);
292 }
293
294
295 /*++++++++++++++++++++++++++++++++++++++
296   The XML Schema Definition XML parser and C program generator.
297   ++++++++++++++++++++++++++++++++++++++*/
298
299 int main(int argc,char **argv)
300 {
301  int i,j,k;
302
303  if(ParseXML(stdin,xml_toplevel_tags,XMLPARSE_UNKNOWN_ATTR_IGNORE))
304    {
305     fprintf(stderr,"Cannot parse XML file - exiting.\n");
306     exit(1);
307    }
308
309  /* Add the XML declaration as a tag. */
310
311  currenttype=NULL;
312  elementType_function("xsd:element",XMLPARSE_TAG_START|XMLPARSE_TAG_END,"xml","xmlDeclaration",NULL,NULL);
313  complexType_function("xsd:complexType",XMLPARSE_TAG_START,"xmlDeclaration");
314  attributeType_function("xsd:attribute",XMLPARSE_TAG_START|XMLPARSE_TAG_END,"version",NULL);
315  attributeType_function("xsd:attribute",XMLPARSE_TAG_START|XMLPARSE_TAG_END,"encoding",NULL);
316  complexType_function("xsd:complexType",XMLPARSE_TAG_END,NULL);
317
318  /* Sort the tags */
319
320  sorttags:
321
322  for(i=0;i<ntagsx;i++)
323    {
324     for(j=0;j<tagsx[i]->nsubtagsx;j++)
325       {
326        for(k=0;k<ntagsx;k++)
327           if(tagsx[i]->subtagsx[j]==tagsx[k])
328              break;
329
330        if(i<k)
331          {
332           xmltagx *temp=tagsx[i];
333
334           tagsx[i]=tagsx[k];
335
336           tagsx[k]=temp;
337
338           goto sorttags;
339          }
340       }
341    }
342
343  /* Print the header */
344
345  printf("/***************************************\n");
346  printf(" An automatically generated skeleton XML parser.\n");
347  printf("\n");
348  printf(" Automatically generated by xsd-to-xmlparser.\n");
349  printf(" ***************************************/\n");
350  printf("\n");
351  printf("\n");
352  printf("#include <stdio.h>\n");
353  printf("\n");
354  printf("#include \"xmlparse.h\"\n");
355
356  /* Print the function prototypes */
357
358  printf("\n");
359  printf("\n");
360  printf("/* The XML tag processing function prototypes */\n");
361  printf("\n");
362
363  for(i=ntagsx-1;i>=0;i--)
364    {
365     printf("static int %s_function(const char *_tag_,int _type_",safe(tagsx[i]->type));
366
367     for(j=0;j<tagsx[i]->nattributes;j++)
368        printf(",const char *%s",safe(tagsx[i]->attributes[j]));
369
370     printf(");\n");
371    }
372
373  /* Print the xmltag variables */
374
375  printf("\n");
376  printf("\n");
377  printf("/* The XML tag definitions */\n");
378
379  for(i=0;i<ntagsx;i++)
380    {
381     printf("\n");
382     printf("/*+ The %s type tag. +*/\n",tagsx[i]->type);
383     printf("static xmltag %s_tag=\n",safe(tagsx[i]->type));
384     printf("              {\"%s\",\n",tagsx[i]->name);
385
386     printf("               %d, {",tagsx[i]->nattributes);
387     for(j=0;j<tagsx[i]->nattributes;j++)
388        printf("%s\"%s\"",(j?",":""),tagsx[i]->attributes[j]);
389     printf("%s},\n",(tagsx[i]->nattributes?"":"NULL"));
390
391     printf("               %s_function,\n",safe(tagsx[i]->type));
392
393     printf("               {");
394     for(j=0;j<tagsx[i]->nsubtagsx;j++)
395        printf("&%s_tag,",safe(tagsx[i]->subtagsx[j]->type));
396     printf("NULL}};\n");
397    }
398
399  printf("\n");
400  printf("\n");
401  printf("/*+ The complete set of tags at the top level. +*/\n");
402  printf("static xmltag *xml_toplevel_tags[]={");
403  printf("&%s_tag,",safe(tagsx[ntagsx-1]->type));
404  printf("&%s_tag,",safe(tagsx[ntagsx-2]->type));
405  printf("NULL};\n");
406
407  /* Print the functions */
408
409  printf("\n");
410  printf("\n");
411  printf("/* The XML tag processing functions */\n");
412
413  for(i=0;i<ntagsx;i++)
414    {
415     printf("\n");
416     printf("\n");
417     printf("/*++++++++++++++++++++++++++++++++++++++\n");
418     if(i==(ntagsx-1))            /* XML tag */
419        printf("  The function that is called when the XML declaration is seen\n");
420     else
421        printf("  The function that is called when the %s XSD type is seen\n",tagsx[i]->type);
422     printf("\n");
423     printf("  int %s_function Returns 0 if no error occured or something else otherwise.\n",safe(tagsx[i]->type));
424     printf("\n");
425     printf("  const char *_tag_ Set to the name of the element tag that triggered this function call.\n");
426     printf("\n");
427     printf("  int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.\n");
428     for(j=0;j<tagsx[i]->nattributes;j++)
429       {
430        printf("\n");
431        printf("  const char *%s The contents of the '%s' attribute (or NULL if not defined).\n",safe(tagsx[i]->attributes[j]),tagsx[i]->attributes[j]);
432       }
433     printf("  ++++++++++++++++++++++++++++++++++++++*/\n");
434     printf("\n");
435
436     printf("static int %s_function(const char *_tag_,int _type_",safe(tagsx[i]->type));
437
438     for(j=0;j<tagsx[i]->nattributes;j++)
439        printf(",const char *%s",safe(tagsx[i]->attributes[j]));
440
441     printf(")\n");
442
443     printf("{\n");
444
445     if(i==(ntagsx-1))            /* XML tag */
446       {
447        printf(" printf(\"<?%%s\",_tag_);\n");
448        for(j=0;j<tagsx[i]->nattributes;j++)
449          {
450           char *safename=safe(tagsx[i]->attributes[j]);
451           printf(" if(%s) printf(\" %s=\\\"%%s\\\"\",ParseXML_Encode_Safe_XML(%s));\n",safename,tagsx[i]->attributes[j],safename);
452          }
453        printf(" printf(\" ?>\\n\");\n");
454       }
455     else
456       {
457        printf(" printf(\"<%%s%%s\",(_type_==XMLPARSE_TAG_END)?\"/\":\"\",_tag_);\n");
458        for(j=0;j<tagsx[i]->nattributes;j++)
459          {
460           char *safename=safe(tagsx[i]->attributes[j]);
461           printf(" if(%s) printf(\" %s=\\\"%%s\\\"\",ParseXML_Encode_Safe_XML(%s));\n",safename,tagsx[i]->attributes[j],safename);
462          }
463        printf(" printf(\"%%s>\\n\",(_type_==(XMLPARSE_TAG_START|XMLPARSE_TAG_END))?\" /\":\"\");\n");
464       }
465
466     printf(" return(0);\n");
467     printf("}\n");
468    }
469
470  /* Print the main function */
471
472  printf("\n");
473  printf("\n");
474  printf("/*++++++++++++++++++++++++++++++++++++++\n");
475  printf("  A skeleton XML parser.\n");
476  printf("  ++++++++++++++++++++++++++++++++++++++*/\n");
477  printf("\n");
478  printf("int main(int argc,char **argv)\n");
479  printf("{\n");
480  printf(" if(ParseXML(stdin,xml_toplevel_tags,XMLPARSE_UNKNOWN_ATTR_WARN))\n");
481  printf("    return(1);\n");
482  printf(" else\n");
483  printf("    return(0);\n");
484  printf("}\n");
485
486  return(0);
487 }
488
489
490 /*++++++++++++++++++++++++++++++++++++++
491   A function to return a safe C identifier from an XML tag or attribute name.
492
493   char *safe Returns the safe name in a private string (only use once).
494
495   const char *name The name to convert.
496   ++++++++++++++++++++++++++++++++++++++*/
497
498 static char *safe(const char *name)
499 {
500  static char *safe=NULL;
501  int i;
502
503  safe=realloc(safe,strlen(name)+1);
504
505  for(i=0;name[i];i++)
506     if(isalnum(name[i]))
507        safe[i]=name[i];
508     else
509        safe[i]='_';
510
511  safe[i]=0;
512
513  return(safe);
514 }