Routino SVN Repository Browser

Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino

ViewVC logotype

Contents of /trunk/src/xml/xsd-to-xmlparser.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 355 - (show annotations) (download) (as text)
Thu Apr 8 17:21:06 2010 UTC (15 years ago) by amb
File MIME type: text/x-csrc
File size: 13813 byte(s)
Make the strings const and add the number of attributes to the xmltag structure.

1 /***************************************
2 $Header: /home/amb/CVS/routino/src/xml/xsd-to-xmlparser.c,v 1.6 2010-04-08 17:20:43 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 /* The local variables and functions */
34
35 int ntags=0;
36 xmltag **tags=NULL;
37 char **types=NULL;
38 char *currenttype=NULL;
39
40 static char *safe(const char *name);
41
42
43 /* The XML tag processing function prototypes */
44
45 static void xml_function(int _type_,const char *version,const char *encoding);
46 static void xsd_schema_function(int _type_,const char *elementFormDefault,const char *xmlns_xsd);
47 static void xsd_complexType_function(int _type_,const char *name);
48 static void xsd_attribute_function(int _type_,const char *name,const char *type);
49 static void xsd_sequence_function(int _type_);
50 static void xsd_element_function(int _type_,const char *name,const char *type,const char *minOccurs,const char *maxOccurs);
51
52
53 /* The XML tag definitions */
54
55 /*+ The elementType type tag. +*/
56 static xmltag xsd_element_tag=
57 {"xsd:element",
58 4, {"name","type","minOccurs","maxOccurs"},
59 xsd_element_function,
60 {NULL}};
61
62 /*+ The sequenceType type tag. +*/
63 static xmltag xsd_sequence_tag=
64 {"xsd:sequence",
65 0, {NULL},
66 xsd_sequence_function,
67 {&xsd_element_tag,NULL}};
68
69 /*+ The attributeType type tag. +*/
70 static xmltag xsd_attribute_tag=
71 {"xsd:attribute",
72 2, {"name","type"},
73 xsd_attribute_function,
74 {NULL}};
75
76 /*+ The complexType type tag. +*/
77 static xmltag xsd_complexType_tag=
78 {"xsd:complexType",
79 1, {"name"},
80 xsd_complexType_function,
81 {&xsd_sequence_tag,&xsd_attribute_tag,NULL}};
82
83 /*+ The schemaType type tag. +*/
84 static xmltag xsd_schema_tag=
85 {"xsd:schema",
86 2, {"elementFormDefault","xmlns:xsd"},
87 xsd_schema_function,
88 {&xsd_element_tag,&xsd_complexType_tag,NULL}};
89
90 /*+ The xmlType type tag. +*/
91 static xmltag xml_tag=
92 {"xml",
93 2, {"version","encoding"},
94 xml_function,
95 {NULL}};
96
97
98 /*+ The complete set of tags at the top level. +*/
99 static xmltag *xml_toplevel_tags[]={&xml_tag,&xsd_schema_tag,NULL};
100
101
102 /* The XML tag processing functions */
103
104
105 /*++++++++++++++++++++++++++++++++++++++
106 The function that is called when the elementType XSD type is seen
107
108 int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.
109
110 const char *name The contents of the 'name' attribute (or NULL if not defined).
111
112 const char *type The contents of the 'type' attribute (or NULL if not defined).
113
114 const char *minOccurs The contents of the 'minOccurs' attribute (or NULL if not defined).
115
116 const char *maxOccurs The contents of the 'maxOccurs' attribute (or NULL if not defined).
117 ++++++++++++++++++++++++++++++++++++++*/
118
119 static void xsd_element_function(int _type_,const char *name,const char *type,const char *minOccurs,const char *maxOccurs)
120 {
121 xmltag *tag=NULL;
122 int i,j;
123
124 if(_type_==XMLPARSE_TAG_END)
125 return;
126
127 for(i=0;i<ntags;i++)
128 if(!strcmp(type,types[i]) && !strcmp(name,tags[i]->name))
129 tag=tags[i];
130
131 if(!tag)
132 {
133 ntags++;
134 tags=(xmltag**)realloc((void*)tags,ntags*sizeof(xmltag*));
135 types=(char**)realloc((void*)types,ntags*sizeof(char*));
136
137 tags[ntags-1]=(xmltag*)calloc(1,sizeof(xmltag));
138 tags[ntags-1]->name=strcpy(malloc(strlen(name)+1),name);
139 types[ntags-1]=strcpy(malloc(strlen(type)+1),type);
140
141 tag=tags[ntags-1];
142 }
143
144 if(!currenttype)
145 return;
146
147 for(i=0;i<ntags;i++)
148 if(!strcmp(types[i],currenttype))
149 {
150 for(j=0;j<XMLPARSE_MAX_SUBTAGS;j++)
151 if(!tags[i]->subtags[j])
152 {
153 tags[i]->subtags[j]=tag;
154 break;
155 }
156
157 if(j==XMLPARSE_MAX_SUBTAGS)
158 {fprintf(stderr,"Too many subtags seen for type '%s'.\n",currenttype); exit(1);}
159 }
160 }
161
162
163 /*++++++++++++++++++++++++++++++++++++++
164 The function that is called when the sequenceType XSD type is seen
165
166 int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.
167 ++++++++++++++++++++++++++++++++++++++*/
168
169 static void xsd_sequence_function(int _type_)
170 {
171 }
172
173
174 /*++++++++++++++++++++++++++++++++++++++
175 The function that is called when the attributeType XSD type is seen
176
177 int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.
178
179 const char *name The contents of the 'name' attribute (or NULL if not defined).
180
181 const char *type The contents of the 'type' attribute (or NULL if not defined).
182 ++++++++++++++++++++++++++++++++++++++*/
183
184 static void xsd_attribute_function(int _type_,const char *name,const char *type)
185 {
186 int i,j;
187
188 if(_type_==XMLPARSE_TAG_END)
189 return;
190
191 for(i=0;i<ntags;i++)
192 if(!strcmp(types[i],currenttype))
193 {
194 for(j=0;j<XMLPARSE_MAX_ATTRS;j++)
195 if(!tags[i]->attributes[j])
196 {
197 tags[i]->attributes[j]=strcpy(malloc(strlen(name)+1),name);
198 tags[i]->nattributes++;
199 break;
200 }
201
202 if(j==XMLPARSE_MAX_ATTRS)
203 {fprintf(stderr,"Too many attributes seen for type '%s'.\n",currenttype); exit(1);}
204 }
205 }
206
207
208 /*++++++++++++++++++++++++++++++++++++++
209 The function that is called when the complexType XSD type is seen
210
211 int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.
212
213 const char *name The contents of the 'name' attribute (or NULL if not defined).
214 ++++++++++++++++++++++++++++++++++++++*/
215
216 static void xsd_complexType_function(int _type_,const char *name)
217 {
218 if(_type_==XMLPARSE_TAG_END)
219 return;
220
221 currenttype=strcpy(realloc(currenttype,strlen(name)+1),name);
222 }
223
224
225 /*++++++++++++++++++++++++++++++++++++++
226 The function that is called when the schemaType XSD type is seen
227
228 int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.
229
230 const char *elementFormDefault The contents of the 'elementFormDefault' attribute (or NULL if not defined).
231
232 const char *xmlns_xsd The contents of the 'xmlns:xsd' attribute (or NULL if not defined).
233 ++++++++++++++++++++++++++++++++++++++*/
234
235 static void xsd_schema_function(int _type_,const char *elementFormDefault,const char *xmlns_xsd)
236 {
237 }
238
239
240 /*++++++++++++++++++++++++++++++++++++++
241 The function that is called when the xmlType XSD type is seen
242
243 int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.
244
245 const char *version The contents of the 'version' attribute (or NULL if not defined).
246
247 const char *encoding The contents of the 'encoding' attribute (or NULL if not defined).
248 ++++++++++++++++++++++++++++++++++++++*/
249
250 static void xml_function(int _type_,const char *version,const char *encoding)
251 {
252 }
253
254
255 /*++++++++++++++++++++++++++++++++++++++
256 The XML Schema Definition XML parser and C program generator.
257 ++++++++++++++++++++++++++++++++++++++*/
258
259 int main(int argc,char **argv)
260 {
261 int i,j,k;
262
263 if(ParseXML(stdin,xml_toplevel_tags,0))
264 {
265 fprintf(stderr,"Cannot parse XML file - exiting.\n");
266 exit(1);
267 }
268
269 /* Sort the tags */
270
271 sorttags:
272
273 for(i=0;i<ntags;i++)
274 {
275 for(j=0;j<XMLPARSE_MAX_SUBTAGS;j++)
276 if(tags[i]->subtags[j])
277 {
278 for(k=0;k<XMLPARSE_MAX_SUBTAGS;k++)
279 if(tags[i]->subtags[j]==tags[k])
280 break;
281
282 if(i<k)
283 {
284 xmltag *tempx=tags[i];
285 char *tempc=types[i];
286
287 tags[i]=tags[k];
288 types[i]=types[k];
289
290 tags[k]=tempx;
291 types[k]=tempc;
292
293 goto sorttags;
294 }
295 }
296 }
297
298 currenttype=NULL;
299 xsd_element_function(XMLPARSE_TAG_START|XMLPARSE_TAG_END,"xml","xmlType",NULL,NULL);
300 xsd_complexType_function(XMLPARSE_TAG_START,"xmlType");
301 xsd_attribute_function(XMLPARSE_TAG_START|XMLPARSE_TAG_END,"version",NULL);
302 xsd_attribute_function(XMLPARSE_TAG_START|XMLPARSE_TAG_END,"encoding",NULL);
303 xsd_complexType_function(XMLPARSE_TAG_END,NULL);
304
305 /* Print the header */
306
307 printf("/***************************************\n");
308 printf(" An automatically generated skeleton XML parser.\n");
309 printf("\n");
310 printf(" Automatically generated by xsd-to-xmlparser.\n");
311 printf(" ***************************************/\n");
312 printf("\n");
313 printf("\n");
314 printf("#include <stdio.h>\n");
315 printf("\n");
316 printf("#include \"xmlparse.h\"\n");
317
318 /* Print the function prototypes */
319
320 printf("\n");
321 printf("\n");
322 printf("/* The XML tag processing function prototypes */\n");
323 printf("\n");
324
325 for(i=ntags-1;i>=0;i--)
326 {
327 printf("static void %s_function(int _type_",safe(tags[i]->name));
328
329 for(j=0;j<tags[i]->nattributes;j++)
330 printf(",const char *%s",safe(tags[i]->attributes[j]));
331
332 printf(");\n");
333 }
334
335 /* Print the xmltag variables */
336
337 printf("\n");
338 printf("\n");
339 printf("/* The XML tag definitions */\n");
340
341 for(i=0;i<ntags;i++)
342 {
343 printf("\n");
344 printf("/*+ The %s type tag. +*/\n",types[i]);
345 printf("static xmltag %s_tag=\n",safe(tags[i]->name));
346 printf(" {\"%s\",\n",tags[i]->name);
347
348 printf(" %d, {",tags[i]->nattributes);
349 for(j=0;j<tags[i]->nattributes;j++)
350 printf("%s\"%s\"",(j?",":""),tags[i]->attributes[j]);
351 printf("%s},\n",(tags[i]->nattributes?"":"NULL"));
352
353 printf(" %s_function,\n",safe(tags[i]->name));
354
355 printf(" {");
356 for(j=0;j<XMLPARSE_MAX_SUBTAGS;j++)
357 if(tags[i]->subtags[j])
358 printf("&%s_tag,",safe(tags[i]->subtags[j]->name));
359 printf("NULL}};\n");
360 }
361
362 printf("\n");
363 printf("\n");
364 printf("/*+ The complete set of tags at the top level. +*/\n");
365 printf("static xmltag *xml_toplevel_tags[]={");
366 printf("&%s_tag,",safe(tags[ntags-1]->name));
367 printf("&%s_tag,",safe(tags[ntags-2]->name));
368 printf("NULL};\n");
369
370 /* Print the functions */
371
372 printf("\n");
373 printf("\n");
374 printf("/* The XML tag processing functions */\n");
375
376 for(i=0;i<ntags;i++)
377 {
378 printf("\n");
379 printf("\n");
380 printf("/*++++++++++++++++++++++++++++++++++++++\n");
381 printf(" The function that is called when the %s XSD type is seen\n",types[i]);
382 printf("\n");
383 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");
384 for(j=0;j<tags[i]->nattributes;j++)
385 {
386 printf("\n");
387 printf(" const char *%s The contents of the '%s' attribute (or NULL if not defined).\n",safe(tags[i]->attributes[j]),tags[i]->attributes[j]);
388 }
389 printf(" ++++++++++++++++++++++++++++++++++++++*/\n");
390 printf("\n");
391
392 printf("static void %s_function(int _type_",safe(tags[i]->name));
393
394 for(j=0;j<tags[i]->nattributes;j++)
395 printf(",const char *%s",safe(tags[i]->attributes[j]));
396
397 printf(")\n");
398
399 printf("{\n");
400
401 if(i==(ntags-1)) /* XML tag */
402 {
403 printf(" printf(\"<?%s\");\n",tags[i]->name);
404 for(j=0;j<tags[i]->nattributes;j++)
405 {
406 char *safename=safe(tags[i]->attributes[j]);
407 printf(" if(%s) printf(\" %s=\\\"%%s\\\"\",ParseXML_Encode_Safe_XML(%s));\n",safename,tags[i]->attributes[j],safename);
408 }
409 printf(" printf(\" ?>\\n\");\n");
410 }
411 else
412 {
413 printf(" printf(\"<%%s%s\",(_type_==XMLPARSE_TAG_END)?\"/\":\"\");\n",tags[i]->name);
414 for(j=0;j<tags[i]->nattributes;j++)
415 {
416 char *safename=safe(tags[i]->attributes[j]);
417 printf(" if(%s) printf(\" %s=\\\"%%s\\\"\",ParseXML_Encode_Safe_XML(%s));\n",safename,tags[i]->attributes[j],safename);
418 }
419 printf(" printf(\"%%s>\\n\",(_type_==(XMLPARSE_TAG_START|XMLPARSE_TAG_END))?\" /\":\"\");\n");
420 }
421
422 printf("}\n");
423 }
424
425 /* Print the main function */
426
427 printf("\n");
428 printf("\n");
429 printf("/*++++++++++++++++++++++++++++++++++++++\n");
430 printf(" A skeleton XML parser.\n");
431 printf(" ++++++++++++++++++++++++++++++++++++++*/\n");
432 printf("\n");
433 printf("int main(int argc,char **argv)\n");
434 printf("{\n");
435 printf(" if(ParseXML(stdin,xml_toplevel_tags,1))\n");
436 printf(" return(1);\n");
437 printf(" else\n");
438 printf(" return(0);\n");
439 printf("}\n");
440
441 return(0);
442 }
443
444
445 /*++++++++++++++++++++++++++++++++++++++
446 A function to return a safe C identifier from an XML tag or attribute name.
447
448 char *safe Returns the safe name in a private string (only use once).
449
450 const char *name The name to convert.
451 ++++++++++++++++++++++++++++++++++++++*/
452
453 static char *safe(const char *name)
454 {
455 static char *safe=NULL;
456 int i;
457
458 safe=realloc(safe,strlen(name)+1);
459
460 for(i=0;name[i];i++)
461 if(isalnum(name[i]))
462 safe[i]=name[i];
463 else
464 safe[i]='_';
465
466 safe[i]=0;
467
468 return(safe);
469 }

Properties

Name Value
cvs:description An XML parser that can convert XML Scheme description XML files into a skeleton source code file that works with the generic XML parser.