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 344 - (show annotations) (download) (as text)
Wed Mar 31 17:19:22 2010 UTC (14 years, 11 months ago) by amb
File MIME type: text/x-csrc
File size: 13212 byte(s)
Call the XML tag functions for the end tags as well as the start tags.

1 /***************************************
2 $Header: /home/amb/CVS/routino/src/xml/xsd-to-xmlparser.c,v 1.3 2010-03-31 17:19:22 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 ParseXML(stdin,xml_toplevel_tags,0);
263
264 /* Sort the tags */
265
266 sorttags:
267
268 for(i=0;i<ntags;i++)
269 {
270 for(j=0;j<XMLPARSE_MAX_SUBTAGS;j++)
271 if(tags[i]->subtags[j])
272 {
273 for(k=0;k<XMLPARSE_MAX_SUBTAGS;k++)
274 if(tags[i]->subtags[j]==tags[k])
275 break;
276
277 if(i<k)
278 {
279 xmltag *tempx=tags[i];
280 char *tempc=types[i];
281
282 tags[i]=tags[k];
283 types[i]=types[k];
284
285 tags[k]=tempx;
286 types[k]=tempc;
287
288 goto sorttags;
289 }
290 }
291 }
292
293 currenttype=NULL;
294 xsd_element_function(XMLPARSE_TAG_START|XMLPARSE_TAG_END,"?xml","?xmlType",NULL,NULL);
295 xsd_complexType_function(XMLPARSE_TAG_START,"?xmlType");
296 xsd_attribute_function(XMLPARSE_TAG_START|XMLPARSE_TAG_END,"version",NULL);
297 xsd_attribute_function(XMLPARSE_TAG_START|XMLPARSE_TAG_END,"encoding",NULL);
298 xsd_complexType_function(XMLPARSE_TAG_END,NULL);
299
300 /* Print the header */
301
302 printf("/***************************************\n");
303 printf(" An automatically generated skeleton XML parser.\n");
304 printf("\n");
305 printf(" Automatically generated by xsd-to-xmlparser.\n");
306 printf(" ***************************************/\n");
307 printf("\n");
308 printf("\n");
309 printf("#include <stdio.h>\n");
310 printf("\n");
311 printf("#include \"xmlparse.h\"\n");
312
313 /* Print the function prototypes */
314
315 printf("\n");
316 printf("\n");
317 printf("/* The XML tag processing function prototypes */\n");
318 printf("\n");
319
320 for(i=ntags-1;i>=0;i--)
321 {
322 printf("static void %s_function(int _type_",safe(tags[i]->name));
323
324 for(j=0;j<XMLPARSE_MAX_ATTRS;j++)
325 if(tags[i]->attributes[j])
326 printf(",char *%s",safe(tags[i]->attributes[j]));
327
328 printf(");\n");
329 }
330
331 /* Print the xmltag variables */
332
333 printf("\n");
334 printf("\n");
335 printf("/* The XML tag definitions */\n");
336
337 for(i=0;i<ntags;i++)
338 {
339 printf("\n");
340 printf("/*+ The %s type tag. +*/\n",types[i]);
341 printf("static xmltag %s_tag=\n",safe(tags[i]->name));
342 printf(" {\"%s\",\n",tags[i]->name);
343
344 printf(" {");
345 for(j=0;j<XMLPARSE_MAX_ATTRS;j++)
346 if(tags[i]->attributes[j])
347 printf("\"%s\",",tags[i]->attributes[j]);
348 printf("NULL},\n");
349
350 printf(" %s_function,\n",safe(tags[i]->name));
351
352 printf(" {");
353 for(j=0;j<XMLPARSE_MAX_SUBTAGS;j++)
354 if(tags[i]->subtags[j])
355 printf("&%s_tag,",safe(tags[i]->subtags[j]->name));
356 printf("NULL}};\n");
357 }
358
359 printf("\n");
360 printf("\n");
361 printf("/*+ The complete set of tags at the top level. +*/\n");
362 printf("static xmltag *xml_toplevel_tags[]={");
363 printf("&%s_tag,",safe(tags[ntags-1]->name));
364 printf("&%s_tag,",safe(tags[ntags-2]->name));
365 printf("NULL};\n");
366
367 /* Print the functions */
368
369 printf("\n");
370 printf("\n");
371 printf("/* The XML tag processing functions */\n");
372
373 for(i=0;i<ntags;i++)
374 {
375 printf("\n");
376 printf("\n");
377 printf("/*++++++++++++++++++++++++++++++++++++++\n");
378 printf(" The function that is called when the %s XSD type is seen\n",types[i]);
379 printf("\n");
380 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");
381 for(j=0;j<XMLPARSE_MAX_ATTRS;j++)
382 if(tags[i]->attributes[j])
383 {
384 printf("\n");
385 printf(" char *%s The contents of the '%s' attribute (or NULL if not defined).\n",safe(tags[i]->attributes[j]),tags[i]->attributes[j]);
386 }
387 printf(" ++++++++++++++++++++++++++++++++++++++*/\n");
388 printf("\n");
389
390 printf("static void %s_function(int _type_",safe(tags[i]->name));
391
392 for(j=0;j<XMLPARSE_MAX_ATTRS;j++)
393 if(tags[i]->attributes[j])
394 printf(",char *%s",safe(tags[i]->attributes[j]));
395
396 printf(")\n");
397
398 printf("{\n");
399
400 printf(" if(_type_&XMLPARSE_TAG_START)\n");
401 printf(" printf(\"<%s",tags[i]->name);
402 for(j=0;j<XMLPARSE_MAX_ATTRS;j++)
403 if(tags[i]->attributes[j])
404 printf(" %s=\\\"%%s\\\"",tags[i]->attributes[j]);
405 printf("%%s>\\n\"");
406 for(j=0;j<XMLPARSE_MAX_ATTRS;j++)
407 if(tags[i]->attributes[j])
408 printf(",(%s?%s:\"\")",safe(tags[i]->attributes[j]),safe(tags[i]->attributes[j]));
409 printf(",(_type_&XMLPARSE_TAG_END)?\"/\":\"\");\n");
410 printf(" else\n");
411 printf(" printf(\"</%s>\\n\");\n",tags[i]->name);
412
413 printf("}\n");
414 }
415
416 /* Print the main function */
417
418 printf("\n");
419 printf("\n");
420 printf("/*++++++++++++++++++++++++++++++++++++++\n");
421 printf(" A skeleton XML parser.\n");
422 printf(" ++++++++++++++++++++++++++++++++++++++*/\n");
423 printf("\n");
424 printf("int main(int argc,char **argv)\n");
425 printf("{\n");
426 printf(" ParseXML(stdin,xml_toplevel_tags,1);\n");
427 printf("\n");
428 printf(" return(0);\n");
429 printf("}\n");
430
431 return(0);
432 }
433
434
435 /*++++++++++++++++++++++++++++++++++++++
436 A function to return a safe C identifier from an XML tag or attribute name.
437
438 char *safe Returns the safe name in a private string (only use once).
439
440 const char *name The name to convert.
441 ++++++++++++++++++++++++++++++++++++++*/
442
443 static char *safe(const char *name)
444 {
445 static char *safe=NULL;
446 int i;
447
448 safe=realloc(safe,strlen(name)+1);
449
450 for(i=0;name[i];i++)
451 if(isalnum(name[i]))
452 safe[i]=name[i];
453 else
454 safe[i]='_';
455
456 safe[i]=0;
457
458 return(safe);
459 }

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.