lib/SOAP/Clean/WSDL.pm untested

filter: Statementstmt 0.0% 0 / 323 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 134 MC/DCmcdc 0.0% 0 / 88 Subroutinesub 0.0% 0 / 32 Podpod 0.0% 0 / 23 totaltotal 0.0% 0 / 660 CC 31 SCAR 30.7
CC
31
Cov
0%
parse
90.0
_parse_portType
30.0
_parse_service
20.0
CRAP 21.5
1 # Copyright (c) 2003, Cornell University
2 # See the file COPYING for the status of this software
3  
4 ########################################################################
5 # Some internal routines
6 ########################################################################
7  
8 package SOAP::Clean::WSDL::Parser;
9  
10 use strict;
11 use warnings;
12  
13 use Data::Dumper;
14 $Data::Dumper::Indent = 1;
15  
16 use SOAP::Clean::Misc;
17 use SOAP::Clean::XML;
18 use SOAP::Clean::Internal;
19  
20 sub new {
21 my $proto = shift;
22 my $class = ref($proto) || $proto;
23 my $self = {};
24 bless $self, $class;
25 $self->initialize(@_);
26 return $self;
27 }
28  
29 sub initialize {
30 my ($self) = @_;
31 }
32  
33 my $xsd99 = "http://www.w3.org/1999/XMLSchema";
34  
35 sub reset_parser {
36 my ($self) = @_;
37  
38 $self->{type_defs} = {};
39 $self->_define("type_defs",$xsd,"int",{kind=>"int"});
40 $self->_define("type_defs",$xsd,"boolean",{kind=>"boolean"});
41 $self->_define("type_defs",$xsd,"float",{kind=>"float"});
42 $self->_define("type_defs",$xsd,"base64Binary",{kind=>"base64Binary"});
43 $self->_define("type_defs",$xsd,"string",{kind=>"string"});
44 $self->_define("type_defs",$xsd,"schema",{kind=>"schema"});
45  
46 $self->_define("type_defs",$xsd99,"string",{kind=>"string"});
47 $self->_define("type_defs",$xsd99,"float",{kind=>"float"});
48 $self->_define("type_defs",$xsd99,"int",{kind=>"int"});
49  
50 $self->{message_defs} = {};
51 $self->{portType_defs} = {};
52 $self->{binding_defs} = {};
53 $self->{service_defs} = {};
54 }
55  
56 ########################################################################
57  
58 sub _pdie {
59 my ($self,$n,$msg) = @_;
60 print STDERR "------------------------------------------------------------------------\n";
61 print STDERR $msg,"\n";
62 print STDERR "Backtrace:\n";
63 print STDERR backtrace();
64 print STDERR "Offending node:\n";
65 print STDERR xml_to_string($n),"\n";
66 print STDERR "------------------------------------------------------------------------\n";
67 exit 1;
68 }
69  
70 ########################################################################
71  
72 sub _define {
73 my ($self,$h_name,$urn,$local,$value) = @_;
74 my $h = $self->{$h_name};
75 $$h{"{$urn}$local"} = $value;
76 }
77  
78 sub _lookup {
79 my ($self,$h_name,$urn,$local) = @_;
80 my $h = $self->{$h_name};
81 my $value = $$h{"{$urn}$local"};
82 defined($value) || die "No such name - {$urn}$local";
83 return $value;
84 }
85  
86 ########################################################################
87  
88 sub parse {
89 my ($self,$d) = @_;
90  
91 reset_parser($self);
92  
93 my $definitions = xml_get_child($d,$wsdl,'definitions');
94 $definitions || $self->_pdie($d,"No wsdl:definitions?");
95  
96 my $definitions_name = xml_get_attr($definitions,0,"name");
97 my $definitions_ns = xml_get_attr($definitions,0,"targetNamespace",0);
98  
99 my $types;
100 my @messages = ();
101 my @portTypes = ();
102 my @bindings = ();
103 my @services = ();
104  
105 foreach my $c (xml_get_children($definitions)) {
106 my ($urn,$local) = xml_get_name($c);
107 xml_same_urns($urn,$wsdl)
108 || $self->_pdie($c,"Element not in WSDL namespace");
109  
110 if ( $local eq "documentation" ) {
111 # ignore documenation
112 ;
113 } elsif ( $local eq "import" ) {
114 $self->_pdie($c,"wsdl:import not implemented");
115 } elsif ( $local eq "types" ) {
116 (!defined($types)) || $self->_pdie($c,"Multiple \"types\" elements");
117 $types = $c;
118 } elsif ( $local eq "message" ) {
119 push @messages, $c;
120 } elsif ( $local eq "portType" ) {
121 push @portTypes, $c;
122 } elsif ( $local eq "binding" ) {
123 push @bindings, $c;
124 } elsif ( $local eq "service" ) {
125 push @services, $c;
126 } else {
127 $self->_pdie($c,"Unexpected \"$local\" element in wsdl:definitions")
128 }
129 }
130  
131 #print ":::::::::::::::::::: types ::::::::::::::::::::\n";
132 if (defined($types)) {
133 foreach my $schema (xml_get_children($types)) {
134 $self->_parse_schema($schema);
135 }
136 }
137 #print Dumper($type_defs),"\n";;
138  
139 #print ":::::::::::::::::::: messages ::::::::::::::::::::\n";
140 foreach my $message ( @messages ) {
141 $self->_parse_message($definitions_ns, $message);
142 }
143 #print Dumper($message_defs),"\n";;
144  
145 #print ":::::::::::::::::::: portType ::::::::::::::::::::\n";
146 foreach my $portType ( @portTypes ) {
147 $self->_parse_portType($definitions_ns, $portType);
148 }
149 #print Dumper($portType_defs),"\n";;
150  
151 #print ":::::::::::::::::::: binding ::::::::::::::::::::\n";
152 foreach my $binding ( @bindings ) {
153 $self->_parse_binding($definitions_ns, $binding);
154 }
155 #print Dumper($binding_defs),"\n";;
156  
157 #print ":::::::::::::::::::: service ::::::::::::::::::::\n";
158 foreach my $service ( @services ) {
159 $self->_parse_service($definitions_ns, $service);
160 }
161 # print Dumper($service_defs),"\n";;
162  
163 return $self->{service_defs};
164 }
165  
166 ########################################################################
167  
168 sub _parse_schema {
169 my ($self,$schema) = @_;
170  
171 xml_same_names($xsd, "schema", xml_get_name($schema))
172 || $self->_pdie($schema,"Expected xsd:schema inside wsdl:types");
173  
174 # xsd:elementFormDefault
175 (xml_get_attr($schema,0,"elementFormDefault","unqualified")
176 eq "qualified")
177 || $self->_pdie($schema,"elementFormDefault=unqualified not implemented");
178  
179 # xsd:targetNamespace
180 my $schema_ns = xml_get_attr($schema,0,"targetNamespace");
181 defined ($schema_ns)
182 || $self->_pdie($schema,"xsd:schema without a targetNamespace?");
183  
184 ELEMENT:
185 foreach my $element (xml_get_children($schema)) {
186 my @element_name = xml_get_name($element);
187 if ( xml_same_names($xsd, "import", @element_name) ) {
188 # just ignore it.
189 # fixme: we ignore too much.
190 next ELEMENT;
191 }
192 xml_same_names($xsd, "element", @element_name) ||
193 $self->_pdie($element,"Not an xsd:element");
194  
195 my $type = $self->_parse_schema_element($schema_ns,$element);
196 my $name = $$type{name};
197 $self->_define("type_defs",$schema_ns,$name,$type);
198 }
199 }
200  
201 sub _parse_schema_element {
202 my ($self,$schema_ns,$element) = @_;
203  
204 my $name = xml_get_attr($element,0,"name",0);
205  
206 # xsd:nillable
207 my $nillable = xml_get_attr($element,0,"nillable","false");
208 # xsd:minOccurs
209 my $min_occurs = xml_get_attr($element,0,"minOccurs",1);
210 # xsd:maxOccurs
211 my $max_occurs = xml_get_attr($element,0,"maxOccurs",1);
212  
213 my $type;
214  
215 # Simple types
216 # xsd:type
217 my $type_attr = xml_get_attr($element,0,"type");
218 if (defined($type_attr)) {
219 $type = $self->_lookup("type_defs",xml_fix_name($element,$type_attr));
220 goto DONE;
221 }
222  
223 # xsd:ref
224 my $ref_attr = xml_get_attr($element,0,"ref");
225 if (defined($ref_attr)) {
226 $type = $self->_lookup("type_defs",xml_fix_name($element,$ref_attr));
227 goto DONE;
228 }
229  
230 # complexType
231 my @element_children = xml_get_children($element);
232 ( $#element_children == 0 )
233 || $self->_pdie($element,"xsd:element with multiple children?");
234  
235 $type = $self->_parse_schema_complexType($schema_ns,$element_children[0]);
236  
237 DONE:
238 return { kind => "element",
239 namespace => $schema_ns,
240 name => $name,
241 nillable => $nillable,
242 min_occurs => $min_occurs,
243 max_occurs => $max_occurs,
244 basetype => $type
245 };
246 }
247  
248 sub _parse_schema_complexType {
249 my ($self,$schema_ns,$complexType) = @_;
250  
251 xml_same_names($xsd, "complexType", xml_get_name($complexType)) ||
252 $self->_pdie($complexType,"Not an xsd:complexType");
253
254 # sequence
255 my @complexType_children = xml_get_children($complexType);
256  
257 # empty sequence - fixme: is this right?
258 if ($#complexType_children == -1) {
259 return { kind => "sequence", list => [] };
260 }
261  
262 # non-empty sequence
263 ( $#complexType_children == 0 )
264 || $self->_pdie($complexType,"xsd:complexType with multiple children?");
265 my $sequence = $complexType_children[0];
266 return $self->_parse_schema_sequence($schema_ns,$sequence);
267 }
268  
269 sub _parse_schema_sequence {
270 my ($self,$schema_ns,$sequence) = @_;
271  
272 xml_same_names($xsd, "sequence", xml_get_name($sequence)) ||
273 $self->_pdie($sequence,"Not an xsd:sequence");
274  
275 my @sequence_values = ();
276 foreach my $c (xml_get_children($sequence)) {
277 my @c_name = xml_get_name($c);
278 if (xml_same_names($xsd, "element", @c_name)) {
279 push @sequence_values, $self->_parse_schema_element($schema_ns,$c);
280 } elsif (xml_same_names($xsd, "any", @c_name)) {
281 push @sequence_values, { kind => "any" };
282 } else {
283 $self->_pdie($c,"Not an xsd:element");
284 }
285 }
286 return { kind => "sequence", list => \@sequence_values };
287 }
288  
289 ########################################################################
290  
291 sub _parse_message {
292 my ($self,$ns,$message) = @_;
293  
294 my $message_name = xml_get_attr($message,0,"name");
295 defined($message_name) || $self->_pdie($message,"wsdl:message without name?");
296  
297 my %parts = ();
298 foreach my $part (xml_get_children($message)) {
299 xml_same_names($wsdl,"part", xml_get_name($part))
300 || $self->_pdie($part,"Expected wsdl:part inside wsdl:message");
301  
302 my $part_name = xml_get_attr($part,0,"name");
303 defined($part_name) || $self->_pdie($part,"wsdl:part without name?");
304  
305 my $type;
306 if ( my $element_attr = xml_get_attr($part,0,"element") ) {
307 $type = $self->_lookup("type_defs",xml_fix_name($part,$element_attr));
308 } elsif ( my $type_attr = xml_get_attr($part,0,"type") ) {
309 $type = $self->_lookup("type_defs",xml_fix_name($part,$type_attr));
310 } else {
311 $self->_pdie($part,"wsdl:part without element or types attribute?");
312 }
313  
314 $parts{$part_name} = $type;
315 }
316  
317 $self->_define("message_defs",$ns,$message_name,
318 { __type__ => "message",
319 parts => \%parts });
320 }
321  
322 ########################################################################
323  
324 sub _parse_portType {
325 my ($self,$ns,$portType) = @_;
326  
327 my $t;
328 my $portType_name = xml_get_attr($portType,0,"name");
329 defined($portType_name) || $self->_pdie($portType,"wsdl:portType without name?");
330  
331 my %operations = ();
332 foreach my $operation (xml_get_children($portType)) {
333 xml_same_names($wsdl,"operation", xml_get_name($operation))
334 || $self->_pdie($operation,"Expected wsdl:operation inside wsdl:portType");
335  
336 my $operation_name = xml_get_attr($operation,0,"name");
337 defined($operation_name) || $self->_pdie($operation,"wsdl:operation without name?");
338  
339 my $parameter_order = xml_get_attr($operation,0,"parameterOrder",0);
340  
341 my @operation_kids = xml_get_children($operation);
342  
343 my ($input_name, $input_message);
344 my ($output_name, $output_message);
345 my @order = ();
346  
347 # walk the children looking for input and output elements. skip
348 # documentation elements
349 KID:
350 foreach my $kid ( @operation_kids ) {
351 my $kid_kind;
352 KIND:
353 foreach my $str ( "input","output", "documentation" ) {
354 if ( xml_same_names($wsdl,$str, xml_get_name($kid)) ) {
355 $kid_kind = $str;
356 last KIND;
357 }
358 }
359 defined($kid_kind)
360 || $self->_pdie($kid,"Unknown child of wsdl:operation element");
361 if ( $kid_kind eq "documentation" ) {
362 next KID;
363 }
364  
365 push @order, $kid_kind;
366  
367 my $raw_name =
368 $operation_name.($kid_kind eq "input" ? "Request" : "Response");
369 my $name = xml_get_attr($kid,0,"name",$raw_name);
370 my $message_attr = xml_get_attr($kid,0,"message");
371 defined($message_attr) || $self->_pdie($kid,"wsdl:$kid_kind without wsdl:message attribute?");
372 my $message = $self->_lookup("message_defs",xml_fix_name($kid,$message_attr));
373  
374 if ($kid_kind eq "input") {
375 ($input_name, $input_message) = ($name, $message);
376 } else {
377 ($output_name, $output_message) = ($name, $message);
378 }
379  
380 }
381  
382 $operations{$operation_name} = { __type__ => "operation",
383 parameterOrder => $parameter_order,
384 type => "request-response",
385 flits => {
386 $input_name => $input_message,
387 $output_name => $output_message,
388 }
389 };
390 }
391  
392 $self->_define("portType_defs",$ns,$portType_name,
393 { __type__ => "portType",
394 operations => \%operations
395 });
396 }
397  
398 ########################################################################
399  
400 sub _parse_binding {
401 my ($self,$ns,$binding) = @_;
402  
403 my $binding_name = xml_get_attr($binding,0,"name");
404 defined($binding_name) || $self->_pdie($binding,"wsdl:binding without name?");
405  
406 my $type_attr = xml_get_attr($binding,0,"type");
407 defined($type_attr) || $self->_pdie($binding,"wsdl:binding without wsdl:type attribute?");
408 my $portType = $self->_lookup("portType_defs",xml_fix_name($binding,$type_attr));
409 my $portType_operations = $$portType{operations};
410  
411 my @kids = xml_get_children($binding);
412 my $protocol_binding = shift @kids;
413  
414 foreach my $k ( @kids ) {
415 xml_same_names($wsdl, "operation", xml_get_name($k))
416 || $self->_pdie($k,"Expected wsdl:operation inside wsdl:binding");
417 }
418  
419 my $binding_info;
420 if (xml_same_names($wsdl_soap,"binding",xml_get_name($protocol_binding))) {
421 $binding_info = $self->_parse_soap_binding($ns,$portType_operations,$protocol_binding,@kids);
422 } elsif (xml_same_names($wsdl_http,"binding",xml_get_name($protocol_binding))) {
423 $binding_info = $self->_parse_http_binding($ns,$portType_operations,$protocol_binding,@kids);
424 } else {
425 $self->_pdie($binding,"Cannot determine protocol for wsdl:binding");
426 }
427  
428 $self->_define("binding_defs",$ns,$binding_name,$binding_info);
429 }
430  
431 sub _destruct_binding_operation {
432 my ($self,$raw_operation,$urn) = @_;
433 my $name = xml_get_attr($raw_operation,0,"name");
434 defined($name) || $self->_pdie($raw_operation,"wsdl:operation without name?");
435  
436 my $protocol_operation = xml_get_child($raw_operation,$urn,"operation");
437 defined($protocol_operation)
438 || $self->_pdie($raw_operation,"wsdl:operation without $urn:operation");
439  
440 my $raw_input = xml_get_child($raw_operation,$wsdl,"input");
441 defined($raw_input)
442 || $self->_pdie($raw_operation,"wsdl:operation without wsdl:input");
443  
444 my $raw_output = xml_get_child($raw_operation,$wsdl,"output");
445 defined($raw_output)
446 || $self->_pdie($raw_operation,"wsdl:operation without wsdl:output");
447  
448 return ($name,$protocol_operation,
449 $name."Request",$raw_input,
450 $name."Response",$raw_output);
451 }
452  
453 sub _parse_soap_binding {
454 my ($self,$ns,$portType_operations,$protocol_binding,@raw_binding_operations) = @_;
455 my $t;
456  
457 # soap:style
458 my $binding_style = xml_get_attr($protocol_binding,0,"style","document");
459  
460 # soap:transport
461 my $raw_transport = xml_get_attr($protocol_binding,0,"transport");
462 defined($raw_transport)
463 || $self->_pdie($protocol_binding,"no soap:transport attribute in soap:binding?");
464 my $transport =
465 $raw_transport eq $soaphttp ? "http"
466 : ($self->_pdie($protocol_binding,"don't know this value of soap:transport"));
467  
468 my %operations = ();
469 foreach my $raw_operation ( @raw_binding_operations ) {
470 my ($name,$protocol_operation,
471 $default_input_name,$raw_input,
472 $default_output_name,$raw_output)
473 = $self->_destruct_binding_operation($raw_operation,$wsdl_soap);
474  
475 my $portType_operation = $$portType_operations{$name};
476 defined($portType_operation)
477 || $self->_pdie($raw_operation,"Can't find this operation in the portType");
478 my $portType_flits = $$portType_operation{flits};
479  
480 my $soapAction;
481 if ($transport eq "http") {
482 $soapAction = xml_get_attr($protocol_operation,0,"soapAction");
483 defined($soapAction) ||
484 $self->_pdie($protocol_operation,"soapAction attribute required for this operation");
485 } else {
486 $soapAction = 0;
487 }
488 my $style = xml_get_attr($protocol_operation,0,"style",$binding_style);
489  
490 my $input = $self->_parse_soap_flit($portType_flits,$default_input_name,$raw_input);
491 my $output = $self->_parse_soap_flit($portType_flits,$default_output_name,$raw_output);
492  
493 $operations{$name} = { __type__ => "binding_operation",
494 soapAction => $soapAction,
495 style => $style,
496 input => $input,
497 output => $output,
498 };
499 }
500  
501 return { __type__ => "binding",
502 protocol => "soap",
503 operations => \%operations };
504 }
505  
506 sub _parse_soap_flit {
507 my ($self,$portType_flits,$default_flit_name,$raw_flit) = @_;
508  
509 my @x_name = xml_get_name($raw_flit);
510 my $in_or_out =
511 xml_same_names($wsdl,"input",@x_name) ? "input"
512 : (xml_same_names($wsdl,"output",@x_name) ? "output"
513 : $self->_pdie($raw_flit,"Expected wsdl:input or wsdl:output"));
514  
515 my $raw_flit_name = xml_get_attr($raw_flit,0,"name",$default_flit_name);
516 my $portType_flit = $$portType_flits{$raw_flit_name};
517 defined($portType_flit) ||
518 $self->_pdie($raw_flit,"Cannot find element name $raw_flit_name in portType");
519  
520 my $body = xml_get_child($raw_flit,$wsdl_soap,"body");
521 defined($body) || $self->_pdie($raw_flit,"Expected to find a soap:body");
522 my $body_parts = xml_get_attr($body,0,"parts",0);
523 my $body_use = xml_get_attr($body,0,"use");
524 defined($body_use) || $self->_pdie($body,"Expected to find a \"use\" attribute");
525 my $body_encodingStyle = xml_get_attr($body,0,"encodingStyle",0);
526 my $body_namespace = xml_get_attr($body,0,"namespace",0);
527  
528 my $header = xml_get_child($raw_flit,$wsdl_soap,"header");
529 (!defined($header)) || $self->_pdie($header,"Need to implement soap:header");
530  
531 return { __type__ => "binding_flit",
532 kind => $in_or_out,
533 parts => $body_parts,
534 use => $body_use,
535 encodingStyle => $body_encodingStyle,
536 namespace => $body_namespace,
537 parts => $$portType_flit{parts},
538 };
539 }
540  
541 sub _parse_http_binding {
542 my ($self,$ns,$portType_operations,$protocol_binding,@raw_binding_operations) = @_;
543 return { __type__ => "binding",
544 protocol => "http" };
545 }
546  
547 ########################################################################
548  
549 sub _parse_service {
550 my ($self,$ns,$service) = @_;
551  
552 my $service_name = xml_get_attr($service,0,"name");
553 defined($service_name) || $self->_pdie($service,"wsdl:service without name?");
554  
555 my %ports = ();
556 PORT:
557 foreach my $port (xml_get_children($service)) {
558 if (xml_same_names($wsdl,"documentation", xml_get_name($port))) {
559 next PORT;
560 }
561  
562 xml_same_names($wsdl,"port", xml_get_name($port))
563 || $self->_pdie($port,"Expected wsdl:port inside wsdl:service");
564  
565 my $port_name = xml_get_attr($port,0,"name");
566 defined($port_name) || $self->_pdie($port,"wsdl:port without name?");
567  
568 my $binding_name = xml_get_attr($port,0,"binding");
569 defined($binding_name) || $self->_pdie($port,"wsdl:port without binding?");
570 my $binding = $self->_lookup("binding_defs",xml_fix_name($port,$binding_name));
571  
572 my $port_info = {%$binding};
573 $$port_info{__type__} = "port";
574 if ( $$binding{protocol} eq "soap" ) {
575 $port_info = $self->_parse_soap_port($port_info,$port);
576 } elsif ( $$binding{protocol} eq "http" ) {
577 $port_info = $self->_parse_http_port($port_info,$port);
578 } else {
579 die;
580 }
581 $ports{$port_name} = $port_info;
582 }
583  
584 $self->_define("service_defs",$ns,$service_name,
585 { __type__ => "service",
586 ports => \%ports });
587 }
588  
589 sub _parse_soap_port {
590 my ($self,$port_info,$port) = @_;
591  
592 my $address = xml_get_child($port,$wsdl_soap,"address");
593 defined($address) || $self->_pdie($port,"wsdl:port without soap:address?");
594  
595 my $location = xml_get_attr($address,0,"location");
596 defined($location) || $self->_pdie($address,"Soap:address without soap:address?");
597 $$port_info{location} = $location;
598  
599 return $port_info;
600 }
601  
602 sub _parse_http_port {
603 my ($self,$port_info,$port) = @_;
604  
605 my $address = xml_get_child($port,$wsdl_http,"address");
606 defined($address) || $self->_pdie($port,"wsdl:port without http:address?");
607  
608 my $location = xml_get_attr($address,0,"location");
609 defined($location) || $self->_pdie($address,"Http:address without http:address?");
610 $$port_info{location} = $location;
611  
612 return $port_info;
613 }
614  
615 ########################################################################
616 ########################################################################
617  
618  
619 package SOAP::Clean::WSDL;
620  
621 use strict;
622 use warnings;
623  
624 BEGIN {
625 use Exporter ();
626 our (@ISA, @EXPORT);
627  
628 @ISA = qw(Exporter);
629 @EXPORT = qw(
630 &generate_services
631 );
632 };
633  
634 sub generate_services {
635 my ($services_defs) = @_;
636 my @services = values %$services_defs;
637 ($#services == 0)
638 || die "Cannot generate ambiguous service";
639  
640 my $service = $services[0];
641  
642  
643 my $port = 0;
644 foreach my $p (values %{$$service{ports}}) {
645 if ( $$p{protocol} eq "soap" ) {
646 ($port == 0) || die "Cannot generate ambiguous port";
647 $port = $p;
648 }
649 }
650 ($port != 0) || die "Cannot generate ambiguous port";
651  
652 return $port;
653 }
654  
655 ########################################################################
656  
657 1;