blib/lib/W3C/SOAP/WSDL/Parser.pm

filter: Statementstmt 89.4% 110 / 123 Branchbran 52.1% 24 / 46 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 89.4% 17 / 19 Podpod 100% 4 / 4 totaltotal 79.1% 156 / 197 CC 30 SCAR 22.7
CC
30
Cov
78%
dynamic_classes
20.2
write_modules
8.2
_xsd_parser
7.7
CRAP 9.7
« blib/lib/W3C/SOAP/WSDL.pm ↑ Summary blib/lib/W3C/SOAP/XSD/Types.pm »
1 package W3C::SOAP::WSDL::Parser;
2  
3 # Created on: 2012-05-27 18:58:29
4 # Create by: Ivan Wills
5 # $Id$
6 # $Revision$, $HeadURL$, $Date$
7 # $Revision$, $Source$, $Date$
8  
9 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Data::Dumper qw/Dumper/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use English qw/ -no_match_vars /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use W3C::SOAP::Utils qw/ns2module/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use W3C::SOAP::XSD::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use W3C::SOAP::WSDL::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use W3C::SOAP::WSDL::Meta::Method;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use File::ShareDir qw/dist_dir/;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 5 Moose::Exporter->setup_import_methods(
22 as_is => ['load_wsdl'],
23 );
24  
25 5 extends 'W3C::SOAP::Parser';
26  
27 5 our $VERSION = 0.13;
28  
29 5 has '+document' => (
30 isa => 'W3C::SOAP::WSDL::Document',
31 required => 1,
32 handles => {
33 module => 'module',
34 has_module => 'has_module',
35 ns_module_map => 'ns_module_map',
36 module_base => 'module_base',
37 has_module_base => 'has_module_base',
38 },
39 );
40 5 has location => (
41 is => 'rw',
42 isa => 'Str',
43 );
44 5 has xsd_parser => (
45 is => 'rw',
46 isa => 'W3C::SOAP::XSD::Parser',
47 builder => '_xsd_parser',
48 lazy => 1,
49 );
50  
51 sub write_modules {
52 2 ▶ my ($self) = @_;
Subroutine write_modules: called CC=8 SCAR=21.0
Pod documented
53 2 ▶ confess "No lib directory setup" if !$self->has_lib;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_lib 0 2 2
54 2 ▶ confess "No module name setup" if !$self->has_module;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_module 0 2 2
55 2 ▶ confess "No template object set" if !$self->has_template;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_template 0 2 2
56  
57 2 my $wsdl = $self->document;
58 2 my $template = $self->template;
59 2 my $file = $self->lib . '/' . $self->module . '.pm';
60 2 $file =~ s{::}{/}g;
61 2 $file = path($file);
62 2 my $parent = $file->parent;
63 2 my @missing;
64 2 ▶ while ( !-d $parent ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $parent) 2 2 4
65 2 push @missing, $parent;
66 2 $parent = $parent->parent;
67 }
68 2 mkdir $_ for reverse @missing;
69 2 my $xsd_parser = $self->get_xsd;
70 2 my @modules = $xsd_parser->write_modules;
71  
72 2 ▶ confess "No XSD modules found!\n" unless @modules;
Branch: 1 branch
BranchTrueFalseTotal
unless @modules 0 2 2
73  
74 2 my $data = {
75 wsdl => $wsdl,
76 module => $self->module,
77 xsd => shift @modules,
78 modules => \@modules,
79 location => $self->location,
80 w3c_version => $VERSION,
81 };
82 2 $template->process('wsdl/pm.tt', $data, "$file");
83 2 ▶ confess "Error in creating $file (xsd.pm): ". $template->error."\n"
Branch: 1 branch
BranchTrueFalseTotal
if $template->error 0 2 2
84 if $template->error;
85  
86 2 return ( $file, $xsd_parser->written_modules );
87 }
88  
89 sub _xsd_parser {
90 4 ▶ my ($self) = @_;
Subroutine _xsd_parser: called CC=5 SCAR=20.4
91  
92 4 my @args;
93 4 ▶ push @args, ( template => $self->template ) if $self->has_template;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_template 4 0 4
94 4 ▶ push @args, ( lib => $self->lib ) if $self->has_lib ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_lib 4 0 4
95 4 ▶ if ( $self->has_module_base ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_module_base) 0 4 4
96 0 my $base = $self->module_base;
97 0 $base =~ s/WSDL/XSD/;
98 0 ▶ $base .= '::XSD' if $base !~ /XSD/;
Branch: 1 branch
BranchTrueFalseTotal
unless $base =~ /XSD/ 0 0 0
99 0 push @args, ( module_base => $base );
100 }
101  
102 4 my $parse = W3C::SOAP::XSD::Parser->new(
103 document => [],
104 ns_module_map => $self->ns_module_map,
105 @args,
106 );
107  
108 4 return $parse;
109 }
110  
111 sub get_xsd {
112 4 ▶ my ($self) = @_;
Subroutine get_xsd: called CC=3 SCAR=11.0
Pod documented
113 4 my $parse = $self->xsd_parser;
114  
115 4 for my $xsd (@{ $self->document->schemas }) {
116 4 $xsd->ns_module_map($self->ns_module_map);
117 4 $xsd->clear_xpc;
118  
119 4 push @{ $parse->document }, $xsd;
120  
121 4 ▶ $parse->document->[-1]->target_namespace($self->document->target_namespace)
Branch: 1 branch
BranchTrueFalseTotal
unless $parse->document->[-1]->has_target_namespace 0 4 4
122 if !$parse->document->[-1]->has_target_namespace;
123 }
124  
125 4 return $parse;
126 }
127  
128 5 my %cache;
129 sub load_wsdl {
130 0 ▶ my ($location) = @_;
Subroutine load_wsdl: not called CC=2 SCAR=17.9
Pod documented
131  
132 0 ▶ return $cache{$location} if $cache{$location};
Branch: 1 branch
BranchTrueFalseTotal
if $cache{$location} 0 0 0
133  
134 0 my $parser = __PACKAGE__->new(
135 location => $location,
136 ns_module_map => {},
137 module_base => 'Dynamic::WSDL',
138 );
139  
140 0 my $class = $parser->dynamic_classes;
141  
142 0 return $cache{$location} = $class->new;
143 }
144  
145 sub dynamic_classes {
146 2 ▶ my ($self) = @_;
Subroutine dynamic_classes: called CC=16 SCAR=30.1
Pod documented
147 2 my @classes = $self->get_xsd->dynamic_classes;
148  
149 2 ▶ $self->module_base('Dynamic::WSDL') if !$self->has_module_base;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_module_base 2 0 2
150 2 my $class_name = $self->module_base . '::' . ns2module($self->document->target_namespace);
151  
152 2 my $wsdl = $self->document;
153 2 my %method;
154 2 for my $service (@{ $wsdl->services }) {
155 2 for my $port (@{ $service->ports }) {
156 2 for my $operation (@{ $port->binding->operations }) {
157 2 my $in_element = eval { $operation->port_type->inputs->[0]->message->element };
158 2 my $in_header_element = eval { $operation->port_type->inputs->[0]->header->element };
159 2 my $out_element = eval { $operation->port_type->outputs->[0]->message->element };
160 2 my $out_header_element = eval { $operation->port_type->outputs->[0]->header->element };
161 2 my @faults = eval {
162 map {{
163 0 class => $_->message->element->module,
164 name => $_->message->element->perl_name,
165 }}
166 2 @{ $operation->port_type->faults }
167 };
168  
169 $method{ $operation->perl_name } = W3C::SOAP::WSDL::Meta::Method->wrap(
170 1 ▶ body => sub { shift->_request($operation->perl_name => @_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine op_get: called CC=1 SCAR=0.0
171 2 ▶ package_name => $class_name,
Branch: 9 branches
BranchTrueFalseTotal
$in_element ? : 2 0 2
$in_element ? : 2 0 2
$in_header_element ? : 1 1 2
$in_header_element ? : 1 1 2
$out_element ? : 2 0 2
$out_element ? : 2 0 2
$out_header_element ? : 0 2 2
$out_header_element ? : 0 2 2
@faults ? : 0 2 2
172 name => $operation->perl_name,
173 wsdl_operation => $operation->name,
174 $in_element ? ( in_class => $in_element->module ) : (),
175 $in_element ? ( in_attribute => $in_element->perl_name ) : (),
176 $in_header_element ? ( in_header_class => $in_header_element->module ) : (),
177 $in_header_element ? ( in_header_attribute => $in_header_element->perl_name ) : (),
178 $out_element ? ( out_class => $out_element->module ) : (),
179 $out_element ? ( out_attribute => $out_element->perl_name ) : (),
180 $out_header_element ? ( out_header_class => $out_header_element->module ) : (),
181 $out_header_element ? ( out_header_attribute => $out_header_element->perl_name ) : (),
182 @faults ? ( faults => \@faults ) : (),
183 );
184  
185 2 ▶ if ( $ENV{W3C_SOAP_NAME_STYLE} eq 'both' && $operation->name ne $operation->perl_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"W3C_SOAP_NAME_STYLE"} eq "both" and $operation->name ne $operation->perl_name) 0 2 2
Condition33%1/3
$ENV{"W3C_SOAP_NAME_STYLE"} eq "both" and $operation->name ne $operation->perl_name
!ll&&!rl&&r
200
MC/DC0%0/2
$ENV{"W3C_SOAP_NAME_STYLE"} eq "both" and $operation->name ne $operation->perl_name
$ENV{"W3C_SOAP_NAME_STYLE"} eq "both" $operation->name ne $operation->perl_name
Truth table33%1/3
A and B
A= $ENV{"W3C_SOAP_NAME_STYLE"} eq "both"B= $operation->name ne $operation->perl_name
ABresult
0X0
100
111
186 0 my $name = $operation->perl_name;
187 $method{ $operation->name } = Moose::Meta::Method->wrap(
188 0 ▶ body => sub { shift->$name(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
189 0 package_name => $class_name,
190 name => $operation->name,
191 );
192 }
193 }
194 }
195 }
196  
197 2 my $class = Moose::Meta::Class->create(
198 $class_name,
199 superclasses => [ 'W3C::SOAP::WSDL' ],
200 methods => \%method,
201 );
202  
203 2 $class->add_attribute(
204 '+location',
205 default => $wsdl->services->[0]->ports->[0]->address,
206 required => 1,
207 );
208  
209 2 return $class_name;
210 }
211  
212 5 1;
213  
214 __END__
215  
216 =head1 NAME
217  
218 W3C::SOAP::WSDL::Parser - Parses WSDL documents to generate Perl client
219 libraries to access the Web Service defined.
220  
221 =head1 VERSION
222  
223 This documentation refers to W3C::SOAP::WSDL::Parser version 0.13.
224  
225 =head1 SYNOPSIS
226  
227 use W3C::SOAP::WSDL::Parser qw/load_wsdl/;
228  
229 # quick/simple usage
230 # create a SOAP client
231 $url = 'http://example.com/soap.wsdl';
232 my $client = load_wsdl($url);
233 my $result = $client->some_action(...);
234  
235 # Create a new object
236 my $wsdl = W3C::SOAP::WSDL::Parser->new(
237 location => $url,
238 module => 'MyApp::WSDL',
239 lib => './lib',
240 template => Template->new(...),
241 ns_module_map => {
242 'http://example.com/xsd/namespace' => 'MyAPP::XSD::Example',
243 'some.other.namespace' => 'MyApp::XSD::SomeOther',
244 },
245 );
246  
247 # Write the generated WSDL module to disk
248 $wsdl->write_modules();
249 # may generate the files
250 # lib/MyApp/WSDL.pm
251 # lib/MyApp/XSD/Example.pm
252 # lib/MyApp/XSD/SomeOther.pm
253  
254 =head1 DESCRIPTION
255  
256 This module parses a WSDL file so that it can produce a client to talk to the
257 SOAP service.
258  
259 There are two ways of using this file:
260  
261 =over 4
262  
263 =item 1
264  
265 Dynamic : C<load_wsdl(...)> or C<<W3C::SOAP::WSDL->new()->dynamic_classes>>
266  
267 These return an in memory generated WSDL client which you can use to talk
268 to the specified web service.
269  
270 =item 2
271  
272 Static : C<<W3C::SOAP::WSDL->new()->write_modules()>> or use L<wsdl-parser>
273 command line script.
274  
275 This writes perl modules to disk so that you can C<use> the modules in your
276 later. This has the advantage that you don't have to recompile the WSDL
277 every time you run your code but it has the disadvantage that your client
278 may be out of date compared to the web service's WSDL.
279  
280 =back
281  
282 Both interfaces are identical once you have the client object. If you want
283 to change at a later point the code change should be adding or removing a
284 use statement and switching from a C<<Module->new>> to C<load_wsdl()>.
285  
286 =head1 SUBROUTINES/METHODS
287  
288 =head2 EXPORTED SUBROUTINES
289  
290 =over 4
291  
292 =item C<load_wsdl ($location)>
293  
294 Helper method that takes the supplied location and creates the dynamic WSDL
295 client object.
296  
297 =back
298  
299 =head2 CLASS METHODS
300  
301 =over 4
302  
303 =item C<new (%args)>
304  
305 Create the new object C<new> accepts the following arguments:
306  
307 =over 4
308  
309 =item location
310  
311 This is the location of the WSDL file, it may be a local file or a URL, it
312 is used to create the C<document> attribute if not supplied.
313  
314 =item document
315  
316 A L<W3C::SOAP::Document> object representing the WSDL file.
317  
318 =item module
319  
320 This is the name of the module to be generated, it is required when writing
321 the SOAP client to disk, the dynamic client generator creates a semi random
322 namespace.
323  
324 =item lib
325  
326 The library directory where modules should be stored. only required when
327 calling C<write_modules>
328  
329 =item template
330  
331 The Template Toolkit object used for the generation of on static modules
332 when using the L</write_modules> method.
333  
334 =item ns_module_map
335  
336 The mapping of XSD namespaces to perl Modules.
337  
338 =back
339  
340 =back
341  
342 =head2 OBJECT METHODS
343  
344 =over 4
345  
346 =item C<<$wsdl->write_modules ()>>
347  
348 Writes out a module that is a SOAP Client to interface with the contained
349 WSDL document, also writes any referenced XSDs.
350  
351 =item C<<$wsdl->dynamic_classes ()>>
352  
353 Creates a dynamic SOAP client object to talk to the WSDL this object was
354 created for
355  
356 =item C<<$wsdl->get_xsd ()>>
357  
358 Creates the L<W3C::SOAP::XSD::Parser> object that represents the XSDs that
359 are used by the specified WSDL file.
360  
361 =back
362  
363 =head1 DIAGNOSTICS
364  
365 =head1 CONFIGURATION AND ENVIRONMENT
366  
367 =head1 DEPENDENCIES
368  
369 =head1 INCOMPATIBILITIES
370  
371 =head1 BUGS AND LIMITATIONS
372  
373 There are no known bugs in this module.
374  
375 Please report problems to Ivan Wills (ivan.wills@gmail.com).
376  
377 Patches are welcome.
378  
379 =head1 AUTHOR
380  
381 Ivan Wills - (ivan.wills@gmail.com)
382  
383 =head1 LICENSE AND COPYRIGHT
384  
385 Copyright (c) 2012 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW Australia 2077).
386 All rights reserved.
387  
388 This module is free software; you can redistribute it and/or modify it under
389 the same terms as Perl itself. See L<perlartistic>. This program is
390 distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
391 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
392 PARTICULAR PURPOSE.
393  
394 =cut
« blib/lib/W3C/SOAP/WSDL.pm ↑ Summary blib/lib/W3C/SOAP/XSD/Types.pm »