-
Notifications
You must be signed in to change notification settings - Fork 1
/
parsexsd.c
267 lines (207 loc) · 7.99 KB
/
parsexsd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* parsexsd.c : Walk the xsd tree as provided by libxml
*
*
* Copyright (c) 2018 Ram Gopalkrisha [email protected] */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "parsexsd.h"
static void print(xmlNodePtr node);
/* Walk the XSD tree and build a list of all the elements that are defined as arrayrs in xsd
*
* complexName - xsd Complex type name
* elemName - xsd Element name
* minO - minimum occurence
* maxO - maximum occurence
*/
xmlArrayDefPtr xsdmaproot = NULL ;
xmlChar nullstring[1] = "\0" ;
char complexName[100];
int buildArrayTree(xmlChar* complexNamein, xmlChar* elemName, xmlChar* minOin,
xmlChar* maxOin, xmlChar* typein)
{
xmlArrayDefPtr t = malloc(sizeof(struct xmlArrayDef));
t->elemName = malloc(sizeof(char)*100);
t->complexName = malloc(sizeof(char)*100);
t->type = malloc(sizeof(char)*100);
t->minOccurs = -1;
t->maxOccurs = 0;
t->isArray = 0;
t->next = NULL;
xmlArrayDefPtr r = NULL;
xmlChar *ubound = "unbounded";
strcpy((char*)t->complexName, (char*)complexNamein);
strcpy((char*)t->elemName, (char*)elemName);
strcpy((char*)t->type, (char*)typein);
if(strlen((char*)complexNamein) == 0) strcpy((char*)complexNamein, "root");
if(strlen((char*)minOin) == 0) strcpy((char*)minOin, "0");
t->minOccurs = strtol((char*)minOin,NULL,10);
if (errno == EINVAL) {
printf("Conversion error occured for minOin");
return 0;
}
if(strlen((char*)maxOin) == 0) strcpy((char*)maxOin, "0");
/* -99 is an arbitary negative number, as XSD comes with "unbound", the size of array is platform dependent
* also, this number has little or no impact in conversion" */
if(xmlStrEqual(maxOin, (xmlChar*)&ubound)) strcpy((char*)maxOin, "-99");
t->maxOccurs = strtol((char*)maxOin,NULL,10);
if (errno == EINVAL){
printf("Conversion error occured for minOin");
return 0;
}
/* minOccurs = 0 && maxOccurs > 1 - optional or an array
minOccurs = +{d,>1} - mandatory and an array
minOccurs > 1 && maxOccurs > 1 - mandatory and array */
if(t->minOccurs == 0 && t->maxOccurs > 1)
t->isArray = OPTIONAL_OR_ARRAY ;
if((t->minOccurs > 1) || ( t->minOccurs > 1 && t->maxOccurs > 1))
t->isArray = MANDATORY_AND_ARRAY ;
if(xsdmaproot == NULL) {
xsdmaproot = t;
return 1;
} else {
for(r=xsdmaproot ; r->next ; r=r->next);
r->next=t ;
return 1;
}
}
xmlChar* getType(xmlNodePtr node)
{
xmlAttrPtr anode = node->properties;
xmlChar *xsdType = "type";
while(!(xmlStrEqual((xmlChar*)&xsdType,anode->name)) && anode->next)
anode=anode->next;
if (xmlStrEqual((xmlChar*)&xsdType,anode->name) &&
anode != NULL && anode->children != NULL)
return (xmlChar*) anode->children->content;
return (xmlChar*)&nullstring;
}
/* Given a node, get the complex type assosiated with it by traversing to
* parent or previous
*/
xmlChar* getComplexTypeName(xmlNodePtr node)
{
xmlNodePtr tmp2;
for (tmp2=node; tmp2; tmp2=tmp2->parent) {
if(xmlStrEqual((const xmlChar*)"element",tmp2->name) &&
(tmp2->properties != NULL) && (tmp2->properties->children != NULL))
return (xmlChar*)tmp2->properties->children->content;
}
for(tmp2=node; tmp2; tmp2=tmp2->prev) {
if(xmlStrEqual((const xmlChar*)"element",tmp2->name) &&
(tmp2->properties != NULL) && (tmp2->properties->children != NULL))
return (xmlChar*)tmp2->properties->children->content;
}
return (xmlChar*)&nullstring;
}
/* For debugging purposes */
static void print(xmlNodePtr node)
{
xmlNodePtr t ;
for(t=node ; t->parent ; t=t->parent);
if(t->prev != NULL) {
printf("%s - ", node->name);
printf("%s ", t->name);
}
}
/* Given a node, get the schema name */
xmlChar* getSchemaName(xmlNodePtr node)
{
if(node->properties->children != NULL)
return (xmlChar*)node->properties->children->content;
return (xmlChar*)&nullstring;
}
/* Given a node, get the element name assosiated with it by traversing to
* child or next
*/
xmlChar* getElementName(xmlNodePtr node)
{
xmlAttrPtr anode = node->properties;
while(!(xmlStrEqual((const xmlChar*)"name",anode->name)) &&
anode->next)
anode=anode->next;
if (anode != NULL && anode->children != NULL)
return (xmlChar*) anode->children->content;
return (xmlChar*)&nullstring;
}
/* Given a node, get min occurs property */
xmlChar* getMinOccurs(xmlNodePtr node)
{
xmlAttrPtr anode = node->properties;
while(!(xmlStrEqual((const xmlChar*)"minOccurs",anode->name)) && anode->next)
anode=anode->next;
if (xmlStrEqual((const xmlChar*)"minOccurs",anode->name) &&
anode != NULL && anode->children != NULL)
return (xmlChar*) anode->children->content;
return (xmlChar*)&nullstring;
}
/* Given a node, get max occurs property */
xmlChar* getMaxOccurs(xmlNodePtr node)
{
xmlAttrPtr anode = node->properties;
while(!(xmlStrEqual((const xmlChar*)"maxOccurs",anode->name)) &&
anode->next)
anode=anode->next;
if (xmlStrEqual((const xmlChar*)"maxOccurs",anode->name) &&
anode != NULL && anode->children != NULL)
return (xmlChar*) anode->children->content;
else
return (xmlChar*)&nullstring;
return (xmlChar*)&nullstring;
}
/* Walk the xsd schema and build a list of required name and properties */
int walkXsdSchema(xmlNodePtr root)
{
xmlNodePtr node;
char elementName[100];
char minO[100];
char maxO[100];
char gtype[100];
memset( elementName, '\0', sizeof(char)*100 );
memset( minO, '\0', sizeof(char)*100 );
memset( maxO, '\0', sizeof(char)*100 );
memset( gtype, '\0', sizeof(char)*100 );
xmlChar *xsdcType = "complexType";
xmlChar *xsdeType = "element";
for (node = root; node; node = node->next) {
if (xmlStrEqual(node->name, xsdcType)) {
strcpy(complexName, (char*)getComplexTypeName(node));
}
if (xmlStrEqual(node->name, xsdeType) &&
node->properties != NULL) {
print(root);
strcpy(elementName, (char*)getElementName(node)) ;
strcpy(minO, (char*)getMinOccurs(node));
strcpy(maxO, (char*)getMaxOccurs(node));
if (!(buildArrayTree((xmlChar*)complexName,
(xmlChar*)elementName,
(xmlChar*)minO, (xmlChar*)maxO,
getType(node)))) {
memset( complexName, '\0', sizeof(char)*100 );
exit(0); /* XXX: Cleanup?? */
}
}
walkXsdSchema(node->children);
}
return (1);
}
/* print the build list -- debuging */
void print_array_elements(void)
{
xmlArrayDefPtr t;
for (t=xsdmaproot; t; t=t->next)
printf("%s -> %s [ %lu , %d ] %s, %d\n", t->complexName,
t->elemName, t->minOccurs, t->maxOccurs, t->type,
t->isArray);
}
void xsdschemafree(void)
{
xmlArrayDefPtr t=xsdmaproot;
xmlArrayDefPtr j=NULL;
while (t) {
j = t;
t=j->next;
free(j);
}
}