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 352 - (show annotations) (download) (as text)
Tue Apr 6 18:48:41 2010 UTC (15 years ago) by amb
File MIME type: text/x-csrc
File size: 13690 byte(s)
Don't print anything for attributes that are not set.

1 /***************************************
2 $Header: /home/amb/CVS/routino/src/xml/xsd-to-xmlparser.c,v 1.5 2010-04-06 18:48:41 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_,char *version,char *encoding);
46 static void xsd_schema_function(int _type_,char *elementFormDefault,char *xmlns_xsd);
47 static void xsd_complexType_function(int _type_,char *name);
48 static void xsd_attribute_function(int _type_,char *name,char *type);
49 static void xsd_sequence_function(int _type_);
50 static void xsd_element_function(int _type_,char *name,char *type,char *minOccurs,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 {"name","type","minOccurs","maxOccurs",NULL},
59 xsd_element_function,
60 {NULL}};
61
62 /*+ The sequenceType type tag. +*/
63 static xmltag xsd_sequence_tag=
64 {"xsd:sequence",
65 {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 {"name","type",NULL},
73 xsd_attribute_function,
74 {NULL}};
75
76 /*+ The complexType type tag. +*/
77 static xmltag xsd_complexType_tag=
78 {"xsd:complexType",
79 {"name",NULL},
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 {"elementFormDefault","xmlns:xsd",NULL},
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 {"version","encoding",NULL},
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 char *name The contents of the 'name' attribute (or NULL if not defined).
111
112 char *type The contents of the 'type' attribute (or NULL if not defined).
113
114 char *minOccurs The contents of the 'minOccurs' attribute (or NULL if not defined).
115
116 char *maxOccurs The contents of the 'maxOccurs' attribute (or NULL if not defined).
117 ++++++++++++++++++++++++++++++++++++++*/
118
119 static void xsd_element_function(int _type_,char *name,char *type,char *minOccurs,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 char *name The contents of the 'name' attribute (or NULL if not defined).
180
181 char *type The contents of the 'type' attribute (or NULL if not defined).
182 ++++++++++++++++++++++++++++++++++++++*/
183
184 static void xsd_attribute_function(int _type_,char *name,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 break;
199 }
200
201 if(j==XMLPARSE_MAX_ATTRS)
202 {fprintf(stderr,"Too many attributes seen for type '%s'.\n",currenttype); exit(1);}
203 }
204 }
205
206
207 /*++++++++++++++++++++++++++++++++++++++
208 The function that is called when the complexType XSD type is seen
209
210 int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.
211
212 char *name The contents of the 'name' attribute (or NULL if not defined).
213 ++++++++++++++++++++++++++++++++++++++*/
214
215 static void xsd_complexType_function(int _type_,char *name)
216 {
217 if(_type_==XMLPARSE_TAG_END)
218 return;
219
220 currenttype=strcpy(realloc(currenttype,strlen(name)+1),name);
221 }
222
223
224 /*++++++++++++++++++++++++++++++++++++++
225 The function that is called when the schemaType XSD type is seen
226
227 int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.
228
229 char *elementFormDefault The contents of the 'elementFormDefault' attribute (or NULL if not defined).
230
231 char *xmlns_xsd The contents of the 'xmlns:xsd' attribute (or NULL if not defined).
232 ++++++++++++++++++++++++++++++++++++++*/
233
234 static void xsd_schema_function(int _type_,char *elementFormDefault,char *xmlns_xsd)
235 {
236 }
237
238
239 /*++++++++++++++++++++++++++++++++++++++
240 The function that is called when the xmlType XSD type is seen
241
242 int _type_ Set to XMLPARSE_TAG_START at the start of a tag and/or XMLPARSE_TAG_END at the end of a tag.
243
244 char *version The contents of the 'version' attribute (or NULL if not defined).
245
246 char *encoding The contents of the 'encoding' attribute (or NULL if not defined).
247 ++++++++++++++++++++++++++++++++++++++*/
248
249 static void xml_function(int _type_,char *version,char *encoding)
250 {
251 }
252
253
254 /*++++++++++++++++++++++++++++++++++++++
255 The XML Schema Definition XML parser and C program generator.
256 ++++++++++++++++++++++++++++++++++++++*/
257
258 int main(int argc,char **argv)
259 {
260 int i,j,k;
261
262 if(ParseXML(stdin,xml_toplevel_tags,0))
263 {
264 fprintf(stderr,"Cannot parse XML file - exiting.\n");
265 exit(1);
266 }
267
268 /* Sort the tags */
269
270 sorttags:
271
272 for(i=0;i<ntags;i++)
273 {
274 for(j=0;j<XMLPARSE_MAX_SUBTAGS;j++)
275 if(tags[i]->subtags[j])
276 {
277 for(k=0;k<XMLPARSE_MAX_SUBTAGS;k++)
278 if(tags[i]->subtags[j]==tags[k])
279 break;
280
281 if(i<k)
282 {
283 xmltag *tempx=tags[i];
284 char *tempc=types[i];
285
286 tags[i]=tags[k];
287 types[i]=types[k];
288
289 tags[k]=tempx;
290 types[k]=tempc;
291
292 goto sorttags;
293 }
294 }
295 }
296
297 currenttype=NULL;
298 xsd_element_function(XMLPARSE_TAG_START|XMLPARSE_TAG_END,"xml","xmlType",NULL,NULL);
299 xsd_complexType_function(XMLPARSE_TAG_START,"xmlType");
300 xsd_attribute_function(XMLPARSE_TAG_START|XMLPARSE_TAG_END,"version",NULL);
301 xsd_attribute_function(XMLPARSE_TAG_START|XMLPARSE_TAG_END,"encoding",NULL);
302 xsd_complexType_function(XMLPARSE_TAG_END,NULL);
303
304 /* Print the header */
305
306 printf("/***************************************\n");
307 printf(" An automatically generated skeleton XML parser.\n");
308 printf("\n");
309 printf(" Automatically generated by xsd-to-xmlparser.\n");
310 printf(" ***************************************/\n");
311 printf("\n");
312 printf("\n");
313 printf("#include <stdio.h>\n");
314 printf("\n");
315 printf("#include \"xmlparse.h\"\n");
316
317 /* Print the function prototypes */
318
319 printf("\n");
320 printf("\n");
321 printf("/* The XML tag processing function prototypes */\n");
322 printf("\n");
323
324 for(i=ntags-1;i>=0;i--)
325 {
326 printf("static void %s_function(int _type_",safe(tags[i]->name));
327
328 for(j=0;j<XMLPARSE_MAX_ATTRS;j++)
329 if(tags[i]->attributes[j])
330 printf(",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(" {");
349 for(j=0;j<XMLPARSE_MAX_ATTRS;j++)
350 if(tags[i]->attributes[j])
351 printf("\"%s\",",tags[i]->attributes[j]);
352 printf("NULL},\n");
353
354 printf(" %s_function,\n",safe(tags[i]->name));
355
356 printf(" {");
357 for(j=0;j<XMLPARSE_MAX_SUBTAGS;j++)
358 if(tags[i]->subtags[j])
359 printf("&%s_tag,",safe(tags[i]->subtags[j]->name));
360 printf("NULL}};\n");
361 }
362
363 printf("\n");
364 printf("\n");
365 printf("/*+ The complete set of tags at the top level. +*/\n");
366 printf("static xmltag *xml_toplevel_tags[]={");
367 printf("&%s_tag,",safe(tags[ntags-1]->name));
368 printf("&%s_tag,",safe(tags[ntags-2]->name));
369 printf("NULL};\n");
370
371 /* Print the functions */
372
373 printf("\n");
374 printf("\n");
375 printf("/* The XML tag processing functions */\n");
376
377 for(i=0;i<ntags;i++)
378 {
379 printf("\n");
380 printf("\n");
381 printf("/*++++++++++++++++++++++++++++++++++++++\n");
382 printf(" The function that is called when the %s XSD type is seen\n",types[i]);
383 printf("\n");
384 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");
385 for(j=0;j<XMLPARSE_MAX_ATTRS;j++)
386 if(tags[i]->attributes[j])
387 {
388 printf("\n");
389 printf(" char *%s The contents of the '%s' attribute (or NULL if not defined).\n",safe(tags[i]->attributes[j]),tags[i]->attributes[j]);
390 }
391 printf(" ++++++++++++++++++++++++++++++++++++++*/\n");
392 printf("\n");
393
394 printf("static void %s_function(int _type_",safe(tags[i]->name));
395
396 for(j=0;j<XMLPARSE_MAX_ATTRS;j++)
397 if(tags[i]->attributes[j])
398 printf(",char *%s",safe(tags[i]->attributes[j]));
399
400 printf(")\n");
401
402 printf("{\n");
403
404 if(i==(ntags-1)) /* XML tag */
405 {
406 printf(" printf(\"<?%s\");\n",tags[i]->name);
407 for(j=0;j<XMLPARSE_MAX_ATTRS;j++)
408 if(tags[i]->attributes[j])
409 {
410 char *safename=safe(tags[i]->attributes[j]);
411 printf(" if(%s) printf(\" %s=\\\"%%s\\\"\",%s);\n",safename,tags[i]->attributes[j],safename);
412 }
413 printf(" printf(\" ?>\\n\");\n");
414 }
415 else
416 {
417 printf(" printf(\"<%%s%s\",(_type_==XMLPARSE_TAG_END)?\"/\":\"\");\n",tags[i]->name);
418 for(j=0;j<XMLPARSE_MAX_ATTRS;j++)
419 if(tags[i]->attributes[j])
420 {
421 char *safename=safe(tags[i]->attributes[j]);
422 printf(" if(%s) printf(\" %s=\\\"%%s\\\"\",%s);\n",safename,tags[i]->attributes[j],safename);
423 }
424 printf(" printf(\"%%s>\\n\",(_type_==(XMLPARSE_TAG_START|XMLPARSE_TAG_END))?\" /\":\"\");\n");
425 }
426
427 printf("}\n");
428 }
429
430 /* Print the main function */
431
432 printf("\n");
433 printf("\n");
434 printf("/*++++++++++++++++++++++++++++++++++++++\n");
435 printf(" A skeleton XML parser.\n");
436 printf(" ++++++++++++++++++++++++++++++++++++++*/\n");
437 printf("\n");
438 printf("int main(int argc,char **argv)\n");
439 printf("{\n");
440 printf(" if(ParseXML(stdin,xml_toplevel_tags,1))\n");
441 printf(" return(1);\n");
442 printf(" else\n");
443 printf(" return(0);\n");
444 printf("}\n");
445
446 return(0);
447 }
448
449
450 /*++++++++++++++++++++++++++++++++++++++
451 A function to return a safe C identifier from an XML tag or attribute name.
452
453 char *safe Returns the safe name in a private string (only use once).
454
455 const char *name The name to convert.
456 ++++++++++++++++++++++++++++++++++++++*/
457
458 static char *safe(const char *name)
459 {
460 static char *safe=NULL;
461 int i;
462
463 safe=realloc(safe,strlen(name)+1);
464
465 for(i=0;name[i];i++)
466 if(isalnum(name[i]))
467 safe[i]=name[i];
468 else
469 safe[i]='_';
470
471 safe[i]=0;
472
473 return(safe);
474 }

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.