WADL/Parser.pm

filter: Statementstmt 29.9% 67 / 224 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 60.0% 21 / 35 Podpod 100% 10 / 10 totaltotal 27.9% 98 / 351 CC 46 SCAR 38.4
CC
46
Cov
23%
write_modules
191.9
dynamic_classes
42.0
__ANON__
30.0
CRAP 46.6
« WADL/Element.pm ↑ Summary WADL/XSD.pm »
1 package W3C::SOAP::WADL::Parser;
2  
3 # Created on: 2013-04-21 10:52:01
4 # Create by: Ivan Wills
5 # $Id$
6 # $Revision$, $HeadURL$, $Date$
7 # $Revision$, $Source$, $Date$
8  
9 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Data::Dumper qw/Dumper/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use English qw/ -no_match_vars /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use W3C::SOAP::Utils qw/ns2module/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use W3C::SOAP::WADL::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use File::ShareDir qw/dist_dir/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use W3C::SOAP::Utils qw/split_ns/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use W3C::SOAP::XSD;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use W3C::SOAP::XSD::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use W3C::SOAP::WADL;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use W3C::SOAP::WADL::Traits;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use W3C::SOAP::WADL::Meta::Method;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use MooseX::Types::Moose qw/Str Int HashRef/;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use JSON qw/decode_json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use W3C::SOAP::Utils qw/ns2module/;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 ▶use TryCatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 3 Moose::Exporter->setup_import_methods(
30 as_is => ['load_wadl'],
31 );
32  
33 3 extends 'W3C::SOAP::Parser';
34  
35 3 our $VERSION = version->new('0.007');
36  
37 3 has '+document' => (
38 isa => 'W3C::SOAP::WADL::Document',
39 required => 1,
40 handles => {
41 module => 'module',
42 has_module => 'has_module',
43 ns_module_map => 'ns_module_map',
44 module_base => 'module_base',
45 has_module_base => 'has_module_base',
46 target_namespace => 'target_namespace',
47 },
48 );
49  
50 3 has xsd_parser => (
51 is => 'rw',
52 isa => 'W3C::SOAP::XSD::Parser',
53 builder => '_xsd_parser',
54 lazy => 1,
55 );
56  
57 0 ▶around BUILDARGS => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
58 0 my ($orig, $class, @args) = @_;
59 0 ▶ my $args
Branch: 2 branches
BranchTrueFalseTotal
!@args ? : 0 0 0
@args == 1 ? : 0 0 0
60 = !@args ? {}
61 : @args == 1 ? $args[0]
62 : {@args};
63  
64 # keep the interface the same as other W3C::SOAP parsers but need to
65 # support XML::Rabbits parameters
66 0 ▶ if ( $args->{location} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args->{'location'}) { } 0 0 0
elsif ($args->{'string'}) { } 0 0 0
67 # stringify location to make XML::Rabbit happy
68 0 $args->{file} = "$args->{location}";
69 }
70 elsif ( $args->{string} ) {
71 0 $args->{xml} = $args->{string};
72 }
73  
74 0 return $class->$orig($args);
75 3 };
76  
77 0 ▶sub write_modules {
Subroutine write_modules: not called CC=14 SCAR=52.6
Pod documented
78 0 my ($self) = @_;
79 0 ▶ confess "No lib directory setup" if !$self->has_lib;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_lib 0 0 0
80 0 ▶ confess "No template object set" if !$self->has_template;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_template 0 0 0
81  
82 0 ▶ if ( !$self->has_module ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_module) 0 0 0
83 0 ▶ confess "No module name setup" if !$self->module_base;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->module_base 0 0 0
84 0 $self->module($self->module_base . '::' . ns2module($self->target_namespace));
85 }
86  
87 0 ▶ my $class_base = $self->module || $self->module_base || 'Dynamic::WADL';
Condition0%0/5
$self->module || $self->module_base || 'Dynamic::WADL'
l!l
00
$self->module or $self->module_base
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->module || $self->module_base || 'Dynamic::WADL'
$self->module $self->module_base
Truth table0%0/3
A or B
A= $self->moduleB= $self->module_base
ABresult
1X1
011
000
88  
89 0 my $xsd_parser = $self->get_xsd;
90 0 my @modules = $xsd_parser->write_modules;
91  
92 0 for my $resources (@{ $self->document->resources }) {
93 0 my $class_name = $class_base . '::' . ns2module($resources->path);
94 0 ▶ my $file = $self->lib . '/' . ( $self->module || $self->module_base ) . '.pm';
Condition0%0/3
$self->module || $self->module_base
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->module || $self->module_base
$self->module $self->module_base
Truth table0%0/3
A || B
A= $self->moduleB= $self->module_base
ABresult
1X1
011
000
95 0 $file =~ s{::}{/}g;
96 0 my %methods;
97  
98 0 for my $resource (@{ $resources->resource }) {
99 0 for my $method (@{ $resource->method }) {
100 0 ▶ try {
Subroutine BEGIN: called CC=1 SCAR=4.1
101 0 my $request = $self->write_method_object(
102 $class_name,
103 $resources,
104 $resource,
105 $method,
106 $method->request
107 );
108  
109 0 my %responses;
110 0 eval { $method->response };
111 0 ▶ if ( $method->has_response ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method->has_response) 0 0 0
112 0 for my $response (@{ $method->response }) {
113 0 $responses{$response->status}
114 = $self->write_method_object(
115 $class_name,
116 $resources,
117 $resource,
118 $method,
119 $response,
120 );
121 }
122 }
123  
124 0 my $name = $self->path_to_name($resource->path, 'method') . '_' . uc $method->name;
125 0 $methods{$name} = {
126 package_name => $class_name,
127 name => $name,
128 path => $resource->path,
129 method => $method->name,
130 request => $request,
131 response => \%responses,
132 };
133 }
134 0 ▶ catch ($e) {
Subroutine BEGIN: called CC=1 SCAR=4.9
Branch: 1 branch
BranchTrueFalseTotal
if ($TryCatch::Error) 0 0 0
135 0 warn "Couldn't generate output for $class_name " . $resource->path . ' - ' . $method->name ."!\n$e";
136 }
137 0 }
138 }
139  
140 $self->write_module(
141 0 'wadl/pm.tt',
142 {
143 module => $class_base,
144 methods => \%methods,
145 location => $resources->path,
146 },
147 $file,
148 );
149 }
150  
151 0 return $class_base;
152 }
153  
154 3 my %written;
155 0 ▶sub write_module {
Subroutine write_module: not called CC=3 SCAR=24.8
Pod documented
156 0 my ($self, $tt, $data, $file) = @_;
157 0 my $template = $self->template;
158  
159 0 ▶ if ($written{$file}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($written{$file}++) 0 0 0
160 0 warn "Already written $file!\n";
161 0 return;
162 }
163  
164 0 $template->process($tt, $data, "$file");
165 0 ▶ confess "Error in creating $file (via $tt): ". $template->error."\n"
Branch: 1 branch
BranchTrueFalseTotal
if $template->error 0 0 0
166 if $template->error;
167  
168 0 return;
169 }
170  
171 0 ▶sub path_to_name {
Subroutine path_to_name: not called CC=3 SCAR=24.8
Pod documented
172 0 my ($self, $path, $type) = @_;
173  
174 0 $path =~ s{^/}{};
175  
176 0 ▶ if ($type eq 'module') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'module') { } 0 0 0
elsif ($type eq 'method') { } 0 0 0
177 0 $path =~ s{/}{::}g;
178 }
179 elsif ( $type eq 'method' ) {
180 0 $path =~ s{/}{_}g;
181 }
182  
183 0 $path =~ s{[^\w:]}{_}g;
184  
185 0 return $path;
186 }
187  
188 0 ▶sub write_method_object {
Subroutine write_method_object: not called CC=3 SCAR=24.8
Pod documented
189 0 my ( $self, $base, $resources, $resource, $method, $type ) = @_;
190 0 my $path = $self->path_to_name($resource->path, 'module');
191 0 my $class_name = $base . '::' . $path . uc $method->name;
192 0 ▶ $class_name .= '::' . $type->status if $type->can('status') && $type->status;
Branch: 1 branch
BranchTrueFalseTotal
if $type->can("status") and $type->status 0 0 0
Condition0%0/3
$type->can("status") and $type->status
!ll&&!rl&&r
000
MC/DC0%0/2
$type->can("status") and $type->status
$type->can("status") $type->status
Truth table0%0/3
A and B
A= $type->can("status")B= $type->status
ABresult
0X0
100
111
193 0 my $file = $self->lib . '/' . $class_name . '.pm';
194 0 $file =~ s{::}{/}g;
195  
196 0 $self->write_module(
197 'wadl/element.pm.tt',
198 {
199 module => $class_name,
200 params => [ $resources, $resource, $type ],
201 representations => $type,
202 },
203 $file,
204 );
205  
206 0 return $class_name;
207 }
208  
209 my %cache;
210 0 ▶sub load_wadl {
Subroutine load_wadl: not called CC=2 SCAR=17.9
Pod documented
211 0 my ($location) = @_;
212 0 ▶ return $cache{$location} if $cache{$location};
Branch: 1 branch
BranchTrueFalseTotal
if $cache{$location} 0 0 0
213  
214 0 my $parser = __PACKAGE__->new(
215 location => $location,
216 );
217  
218 0 my $class = $parser->dynamic_classes;
219 0 return $cache{$location} = $class->new;
220 }
221  
222 0 ▶sub dynamic_classes {
Subroutine dynamic_classes: not called CC=6 SCAR=37.4
Pod documented
223 0 my ($self) = @_;
224 0 my @classes;
225  
226 0 for my $resources (@{ $self->document->resources }) {
227 0 my $class_name = "Dynamic::WADL::" . ns2module($resources->path);
228 0 push @classes, $class_name;
229 0 my %methods;
230  
231 0 for my $resource (@{ $resources->resource }) {
232 0 for my $method (@{ $resource->method }) {
233 0 my $request = $self->build_method_object( $class_name, $resources, $resource, $method, $method->request );
234  
235 0 my %responses;
236 0 eval { $method->response };
237 0 ▶ if ( $method->has_response ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method->has_response) 0 0 0
238 0 for my $response (@{ $method->response }) {
239 0 $responses{$response->status}
240 = $self->build_method_object(
241 $class_name,
242 $resources,
243 $resource,
244 $method,
245 $response,
246 );
247 }
248 }
249  
250 0 my $name = $resource->path . '_' . uc $method->name;
251 $methods{$name} = W3C::SOAP::WADL::Meta::Method->wrap(
252 0 ▶ body => sub { shift->_request( $name => @_ ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
253 0 package_name => $class_name,
254 name => $name,
255 path => $resource->path,
256 method => $method->name,
257 request => $request,
258 response => \%responses,
259 );
260 }
261 }
262  
263 0 my $class = Moose::Meta::Class->create(
264 $class_name,
265 superclasses => [ 'W3C::SOAP::WADL' ],
266 methods => \%methods,
267 );
268 0 $class->add_attribute(
269 '+location',
270 default => $resources->path,
271 );
272 }
273  
274 0 return $classes[0];
275 }
276  
277 0 ▶sub build_method_object {
Subroutine build_method_object: not called CC=3 SCAR=24.8
Pod documented
278 0 my ( $self, $base, $resources, $resource, $method, $type ) = @_;
279 0 my $class_name = $base . '::' . $resource->path . uc $method->name;
280 0 ▶ $class_name .= '::' . $type->status if $type->can('status') && $type->status;
Branch: 1 branch
BranchTrueFalseTotal
if $type->can("status") and $type->status 0 0 0
Condition0%0/3
$type->can("status") and $type->status
!ll&&!rl&&r
000
MC/DC0%0/2
$type->can("status") and $type->status
$type->can("status") $type->status
Truth table0%0/3
A and B
A= $type->can("status")B= $type->status
ABresult
0X0
100
111
281  
282 0 my $class = Moose::Meta::Class->create(
283 $class_name,
284 superclasses => [ 'W3C::SOAP::WADL::Element' ],
285 );
286  
287 0 $self->add_params( $class, $resources );
288 0 $self->add_params( $class, $resource );
289 0 $self->add_params( $class, $type );
290 0 $self->add_representations( $class, $class_name, $type );
291  
292 0 return $class_name;
293 }
294  
295 0 ▶sub add_params {
Subroutine add_params: not called CC=5 SCAR=34.0
Pod documented
296 0 my ($self, $class, $container) = @_;
297 0 eval {$container->param};
298  
299 0 ▶ if ( $container->has_param ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($container->has_param) 0 0 0
300 0 for my $param (@{ $container->param }) {
301 0 my $name = $param->name;
302 0 $name =~ s/\W/_/g;
303  
304 0 ▶ my $required = !!( ( $param->required || '' ) eq 'true' );
Condition0%0/2
$param->required || ''
l!l
00
MC/DC0%0/1
$param->required || ''
$param->required
Truth table0%0/2
A
A= $param->required
Aresult
11
00
305 $class->add_attribute(
306 $name,
307 is => 'rw',
308 isa => Str, # TODO Get type validation done
309 predicate => 'has_' . $name,
310 required => $required,
311 0 ▶ documentation => eval { $param->doc } || '',
Condition0%0/2
eval { do { $param->doc } } || ''
l!l
00
MC/DC0%0/1
eval { do { $param->doc } } || ''
eval { do { $param->doc } }
Truth table0%0/2
A
A= eval { do { $param->doc } }
Aresult
11
00
312 traits => [qw{ W3C::SOAP::WADL }],
313 style => $param->style,
314 real_name => $param->name,
315 );
316 }
317 }
318  
319 0 return;
320 }
321  
322 0 ▶sub add_representations {
Subroutine add_representations: not called CC=4 SCAR=30.0
Pod documented
323 0 my ( $self, $class, $base, $type ) = @_;
324 0 my %rep_map;
325  
326 0 eval { $type->representation };
327 0 ▶ if ( $type->has_representation ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->has_representation) 0 0 0
328 0 for my $rep (@{ $type->representation }) {
329 0 eval { $rep->media_type };
330 0 ▶ $rep->media_type('text/plain') unless $rep->media_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $rep->media_type 0 0 0
331  
332 # work out if the representation has a matching class
333 0 $rep_map{ $rep->media_type } = {};
334 }
335 }
336  
337 # Add the representations as semi constant
338 $class->add_attribute(
339 '_representations',
340 is => 'ro',
341 isa => HashRef,
342 predicate => 'has_representations',
343 0 ▶ default => sub { \%rep_map },
Subroutine __ANON__: not called CC=1 SCAR=6.9
344 0 );
345  
346 0 return;
347 }
348  
349 0 ▶sub _xsd_parser {
Subroutine _xsd_parser: not called CC=5 SCAR=34.0
350 0 my ($self) = @_;
351  
352 0 my @args;
353 0 ▶ push @args, ( template => $self->template ) if $self->has_template;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_template 0 0 0
354 0 ▶ push @args, ( lib => $self->lib ) if $self->has_lib ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_lib 0 0 0
355 0 ▶ if ( $self->has_module_base ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_module_base) 0 0 0
356 0 my $base = $self->module_base;
357 0 $base =~ s/WSDL/XSD/;
358 0 ▶ $base .= '::XSD' if $base !~ /XSD/;
Branch: 1 branch
BranchTrueFalseTotal
unless $base =~ /XSD/ 0 0 0
359 0 push @args, ( module_base => $base );
360 }
361  
362 0 my $parse = W3C::SOAP::XSD::Parser->new(
363 document => [],
364 ns_module_map => $self->ns_module_map,
365 @args,
366 );
367  
368 0 return $parse;
369 }
370  
371 0 ▶sub get_xsd {
Subroutine get_xsd: not called CC=4 SCAR=30.0
Pod documented
372 0 my ($self) = @_;
373 0 my $parse = $self->xsd_parser;
374  
375 0 for my $xsd (@{ $self->document->schemas }) {
376 0 $xsd->ns_module_map($self->ns_module_map);
377 0 ▶ $xsd->module_base($self->module_base . '::XSD') if defined $self->module_base;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->module_base 0 0 0
378 0 $xsd->clear_xpc;
379  
380 0 push @{ $parse->document }, $xsd;
381  
382 0 ▶ $parse->document->[-1]->target_namespace($self->document->target_namespace)
Branch: 1 branch
BranchTrueFalseTotal
unless $parse->document->[-1]->has_target_namespace 0 0 0
383 if !$parse->document->[-1]->has_target_namespace;
384 }
385  
386 0 return $parse;
387 }
388  
389 3 1;
390  
391 __END__
« WADL/Element.pm ↑ Summary WADL/XSD.pm »