summaryrefslogtreecommitdiffstats
path: root/wsdl2c/wsdl2c.c
blob: 43d0a3c370ae51cbcd8c0458427545a900a4ed9f (plain)
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#include <libxml/xpath.h>
#include <xsd2c/xsd2c.h>
#include <xsd2c/util.h> /* parseNS */
#include <xsd2c/formatter.h> /* formatter sax serializer */
#include <xsd2c/obj.h> /* formatter sax serializer */
#include <string.h>

xmlXPathObjectPtr xpath_eval(xmlDocPtr doc, const char *xpath)
{
  xmlXPathContextPtr context;
  xmlXPathObjectPtr result;
  xmlNodePtr node;
  xmlNsPtr default_ns;

  node = xmlDocGetRootElement(doc);
  if (node == NULL) {
    fprintf(stderr,"empty document\n");
    return NULL;
  }

  
  context = xmlXPathNewContext(doc);

  context->namespaces = xmlGetNsList(doc, node);
  context->nsNr = 0;
  if (context->namespaces != NULL) {
     while (context->namespaces[context->nsNr] != NULL) {
/*      fprintf(stdout, "NS: '%s' - '%s'\n", context->namespaces[context->nsNr]->href, 
  context->namespaces[context->nsNr]->prefix); */
      context->nsNr++;
     }
  }

  /* search default namespace */
  default_ns = xmlSearchNs(doc, node, NULL);
  if (default_ns != NULL && default_ns->href != NULL) {
    fprintf(stdout, "Register default namespace '%s'\n", (const char*)default_ns->href);
    xmlXPathRegisterNs(context, "wsdl", default_ns->href ); /* default_ns : "http://schemas.xmlsoap.org/wsdl/" */
  } 
  


  result = xmlXPathEvalExpression((xmlChar*)xpath, context);  
  if (result == NULL) {
    fprintf(stderr, "ERROR: XPath error!\n");
    return NULL;
  }

  if(xmlXPathNodeSetIsEmpty(result->nodesetval)){
    /* no result */
    return NULL;
  }

  xmlXPathFreeContext(context);
  return result;  
}

xmlNodePtr findPortType(xmlDocPtr doc, const char *name)
{
  xmlNodePtr root;
  xmlNodePtr cur;
  xmlChar *attr_name;

  root = xmlDocGetRootElement(doc);
  if (root == NULL) {
    fprintf(stderr, "Empty document!\n");
    return;
  }

  cur = root->xmlChildrenNode;

  while (cur != NULL) {
    
    if (cur->type != XML_ELEMENT_NODE) {
      cur = cur->next;
      continue;
    }

    if (xmlStrcmp(cur->name, (const xmlChar*)"portType")) {
      cur = cur->next;
      continue;
    }

    attr_name = xmlGetProp(cur, "name");  
    if (attr_name == NULL) {
      cur = cur->next;
      continue;
    }

    if (!xmlStrcmp(attr_name, (const xmlChar*)name)) {
      xmlFree(attr_name);
      return cur;
    }
    
    xmlFree(attr_name);
    cur = cur->next;
  }

  return NULL;
}


xmlNodePtr findMessage(xmlDocPtr doc, const char *name)
{
  xmlNodePtr root;
  xmlNodePtr cur;
  xmlChar *attr_name;

  root = xmlDocGetRootElement(doc);
  if (root == NULL) {
    fprintf(stderr, "Empty document!\n");
    return;
  }

  cur = root->xmlChildrenNode;

  while (cur != NULL) {
    
    if (cur->type != XML_ELEMENT_NODE) {
      cur = cur->next;
      continue;
    }

    if (xmlStrcmp(cur->name, (const xmlChar*)"message")) {
      cur = cur->next;
      continue;
    }

    attr_name = xmlGetProp(cur, "name");  
    if (attr_name == NULL) {
      cur = cur->next;
      continue;
    }

    if (!xmlStrcmp(attr_name, (const xmlChar*)name)) {
      xmlFree(attr_name);
      return cur;
    }
    
    xmlFree(attr_name);
    cur = cur->next;
  }

  return NULL;
}


xmlNodePtr findSubNode(xmlNodePtr root, const char *element_name)
{
  xmlNodePtr cur;

  cur = root->xmlChildrenNode;

  while (cur != NULL) {
    
    if (cur->type != XML_ELEMENT_NODE) {
      cur = cur->next;
      continue;
    }

    if (!xmlStrcmp(cur->name, (const xmlChar*)element_name)) {
      return cur;
    }

    cur = cur->next;
  }

  return NULL;
}


void handleInputParameters(xmlDocPtr doc, const char *name)
{
  char ns[10];
  char message_name[255];
  xmlNodePtr node;
  xmlNodePtr cur;

  xmlChar *var_name, *var_type, *var_element;

  parseNS(name, ns, message_name); /* check why to pase ns? */

  node = findMessage(doc, message_name);
  if (node == NULL) {
    fprintf(stderr, "ERROR: Can not find message '%s'\n", message_name);
    return;
  }

  cur = node->xmlChildrenNode;
  while (cur != NULL) {
    
    if (cur->type != XML_ELEMENT_NODE) {
      cur = cur->next;
      continue;
    }

    if (xmlStrcmp(cur->name, (const xmlChar*)"part")) {
      cur = cur->next;
      continue;
    }

    var_name = xmlGetProp(cur, "name");  
    if (var_name == NULL) {
      fprintf(stderr, "ERROR: Found <part> without attrbute 'name'! (message:'%s')\n", 
        message_name);
      cur = cur->next;
      continue;
    }

    var_type = xmlGetProp(cur, "type");  
    if (var_type != NULL) {
      fprintf(stdout, "\t\t(type:%s,%s)\n", (const char*)var_type, (const char*)var_name);
      xmlFree(var_name);
      xmlFree(var_type);
      cur = cur->next;
      continue;
    } 

    var_element = xmlGetProp(cur, "element");  
    if (var_element == NULL) {
      fprintf(stderr, "ERROR: Found <part> without attrbute 'type' or 'element'! (message:'%s')\n", 
        message_name);
      xmlFree(var_name);
      cur = cur->next;
      continue;
    }

    fprintf(stdout, "\t\t(element:%s,%s)\n", (const char*)var_element, (const char*)var_name);

    xmlFree(var_name);
    xmlFree(var_type);

    cur = cur->next;
  }

}

void handlePortType(xmlDocPtr doc, const char *name)
{
  xmlNodePtr node;
  xmlNodePtr input;
  xmlNodePtr output;
  xmlNodePtr cur;
  xmlChar *attr_name;
  xmlChar *message;

  char opname[255];

  node = findPortType(doc, name);
  if (node == NULL) {
    fprintf(stderr, "PortType '%s' not found!\n", name);
    return;
  }

  cur = node->xmlChildrenNode;
  while (cur != NULL) {
    
    if (cur->type != XML_ELEMENT_NODE) {
      cur = cur->next;
      continue;
    }

    if (xmlStrcmp(cur->name, (const xmlChar*)"operation")) {
      cur = cur->next;
      continue;
    }

    attr_name = xmlGetProp(cur, "name");  
    if (attr_name == NULL) {
      cur = cur->next;
      continue;
    }

    strcpy(opname, (const char*)attr_name);
    xmlFree(attr_name);
    fprintf(stdout, "Operation -> '%s'\n", opname);

    /* handle input */
    input = findSubNode(cur, "input");
    if (input == NULL) {
      fprintf(stderr, "WARNING: No input for operation '%'\n", opname);
      cur = cur->next;
      continue;
    }

    message = xmlGetProp(input, "message");  
    if (message == NULL) {
      fprintf(stderr, "ERROR: No message attribute for input operation '%'\n", opname);
      cur = cur->next;
      continue;
    }
    
    fprintf(stdout, "\tinput  = '%s'\n", (const char*)message);
    
    handleInputParameters(doc, (const char*)message);

    xmlFree(message);
  
    /* handle output */
    output = findSubNode(cur, "output");
    if (output == NULL) { /* one way operation is ok */
      cur = cur->next;
      continue;
    }

    message = xmlGetProp(output, "message");  
    if (message == NULL) {
      fprintf(stderr, "ERROR: No message attribute for output operation '%'\n", opname);
      cur = cur->next;
      continue;
    }
    
    fprintf(stdout, "\toutput = '%s'\n", (const char*)message);
    xmlFree(message);
  
    cur = cur->next;
  }

}

void handleBinding(xmlDocPtr doc, const char *type)
{
  
  int i;
  xmlChar *name;
  xmlChar *btype;
  xmlNodeSetPtr nodeset; 
  xmlXPathObjectPtr xpathObj;
  char query[255];
  char port_name[255], port_ns[10];
  char binding_type[255], binding_ns[10];

  parseNS(type, binding_ns, binding_type);

  /* Set xpath  query */
  sprintf(query, "//wsdl:definitions/wsdl:binding[@name=\"%s\"]", binding_type);

  /* Find Bindings */
  xpathObj = xpath_eval(doc, query);
  
  if (xpathObj == NULL) {
    fprintf(stderr, "No Soap Binding found!\n");
    return;
  }

  /* Check if found nodes */
  nodeset = xpathObj->nodesetval;
  if (nodeset == NULL) {
    fprintf(stderr, "No Soap Binding found! nodeset empty!\n");
    return;
  }

  /* Iterate all soap services */
  for (i=0;i < nodeset->nodeNr; i++) {
    
    name = xmlGetProp(nodeset->nodeTab[i], "name");

    if (name == NULL) {
      fprintf(stderr, "WARNING: no attribute name found!\n");
    } else {
      fprintf(stdout, "Found Binding -> (name: '%s')\n", 
        (const char*)name);
    }

    btype = xmlGetProp(nodeset->nodeTab[i], "type");

    if (btype == NULL) {
      fprintf(stderr, "WARNING: no attribute type found!\n");
    } else {
      parseNS(btype, port_ns, port_name);
      handlePortType(doc, port_name);
    }

    if (name != NULL) xmlFree(name);
    if (btype != NULL) xmlFree(btype);  
  }
  
}

void wsdlParse(xmlDocPtr doc)
{
  int i;
  xmlChar *name, *binding;
  xmlNodeSetPtr nodeset; 
  xmlXPathObjectPtr xpathObj;


  /* Find Soap Service */
  xpathObj = xpath_eval(doc, "//wsdl:definitions/wsdl:service/wsdl:port[soap:address]");
  
  if (xpathObj == NULL) {
    fprintf(stderr, "No Soap Service (soap:address) found!\n");
    return;
  }

  /* Check if found nodes */
  nodeset = xpathObj->nodesetval;
  if (nodeset == NULL) {
    fprintf(stderr, "No Soap Service (soap:address) found! nodeset empty!\n");
    return;
  }

  /* Iterate all soap services */
  for (i=0;i < nodeset->nodeNr; i++) {
    
    name = xmlGetProp(nodeset->nodeTab[i], "name");
    binding = xmlGetProp(nodeset->nodeTab[i], "binding");

    if (name == NULL) {
      fprintf(stderr, "WARNING: no attribute type found!\n");
    } if (binding == NULL) {
      fprintf(stderr, "WARNING: no attribute binding found!\n");
    } else {
      fprintf(stdout, "Found SOAP port -> (type: '%s') (binding: '%s')\n", 
        (const char*)name, (const char*)binding);
    }

    handleBinding(doc, binding);

    if (name != NULL) xmlFree(name);
    if (binding != NULL) xmlFree(binding);
  }
  

}


static
void usage(const char* execname);

int main(int argc, char *argv[])
{
  xmlDocPtr doc;
  xmlNodePtr cur;
  xmlNodePtr node;
  xmlNodePtr xsdRoot;
  xmlNsPtr default_ns;
  int i;
  char outDir[1054];
  char fname[255];

  if (argc < 2) {
    usage(argv[0]);
    return 1;
  }


  
 strcpy(outDir, ".");

  for (i=1;i<argc;i++) 
  {
    if (!strcmp(argv[i], "-d"))
      if (i==argc-1) usage(argv[0]);
      else strcpy(outDir, argv[++i]);
    else if (!strcmp(argv[i], "-S"))
      formatter_generate_sax_serializer = 1;
    else strcpy(fname, argv[i]);
  }
  

  xsdRoot = wsdlLoadFile(fname);
  
  if (xsdRoot == NULL ) 
  {
    fprintf(stderr,"No Schema data found\n");
    doc = xmlParseFile(fname);
    if (doc == NULL) 
    {
	    fprintf(stderr,"Can not parse document\n");
	    return 1;
    }
  } 
  else 
  {
	  doc = xsdRoot->doc;
  }
  

  node = xmlDocGetRootElement(doc);
  if (node == NULL) {
    fprintf(stderr,"empty document\n");
    xmlFreeDoc(doc);
    return 1;
  }

  /* search default namespace */
  default_ns = xmlSearchNs(doc, node, NULL);
  if (default_ns == NULL || ( default_ns != NULL && default_ns->href == NULL)) {
    fprintf(stdout, "Adding default namespace 'http://schemas.xmlsoap.org/wsdl/'\n");
    xmlNewDocProp(doc,  "xmlns", "http://schemas.xmlsoap.org/wsdl/");
  } 


  if (xsdEngineRun(xsdRoot, outDir)) {
   fprintf(stderr, "xsd2c engine error\n");
  	return 1;
  }

  wsdlParse(doc);

  trFreeModule();
  objFreeModule();

  xmlFreeDoc(doc);
  return 0;
}



static
void usage(const char* execname)
{
	fprintf(stderr, "usage: %s -d dest <wsdl file>\n", execname);	
}