DirectI/Parse.pm

filter: Statementstmt 76.0% 140 / 184 Branchbran 52.7% 38 / 72 Conditioncond 50.0% 13 / 26 MC/DCmcdc 11.7% 2 / 17 Subroutinesub 88.8% 16 / 18 Podpod 0.0% 0 / 4 totaltotal 65.1% 209 / 321 CC 49 SCAR 24.9
CC
49
Cov
68%
_parse_boolean
30.0
fetch_data_and_signature
21.6
parse_xml_string
13.8
CRAP 12.1
↑ Summary DirectI/Serialize.pm »
1 #
2 #===============================================================================
3 #
4 # FILE: Parse.pm
5 #
6 # DESCRIPTION: SOAP::DirectI::Parse -- parsing SOAP DirectI's responses
7 #
8 # FILES: ---
9 # BUGS: ---
10 # NOTES: ---
11 # AUTHOR: Pavel Boldin (), <davinchi@cpan.org>
12 # COMPANY:
13 # VERSION: 1.0
14 # CREATED: 17.03.2009 20:52:05 MSK
15 # REVISION: ---
16 #===============================================================================
17  
18 package SOAP::DirectI::Parse;
19  
20 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Smart::Comments -ENV;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 2 local $Data::Dumper::Purity = 1;
29 2 local $Data::Dumper::Indent = 1;
30  
31 #my $slurp = do {
32 # local $/;
33 # <>;
34 #};
35  
36 #$slurp =~ s/.*(<soapenv:Body)/$1/;
37 #$slurp =~ s/(<\/soapenv:Body>).*/$1/;
38  
39 4 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
40 4 my $class = shift;
41 4 ▶ $class = ref( $class ) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
42  
43 4 my $self = {};
44 4 bless $self, $class;
45  
46 4 return $self;
47 }
48  
49 #bless my $obj = {}, __PACKAGE__;
50 #$obj->parse_xml_string( $slurp );
51  
52 #warn Dumper $obj->{tree};
53  
54 #warn Dumper [ $obj->parse_to_data_and_signature ];
55  
56 17 ▶sub parse_xml_string {
Subroutine parse_xml_string: called CC=13 SCAR=26.2
Pod undocumented
57 17 my $self = shift;
58 17 my $str = shift;
59  
60 17 my $parent_tag = shift;
61  
62 #my @tag = ($str =~ m/^<(\w*:)?(\w+)([^>]*)(?:\/>|>(.*?)<\/\1?\2>)$/mxogs);
63  
64 ### parse_xml_string: $str
65  
66 # my @tag = ( $str =~ m{\G<(\w*:)?(\w+)([^>]*)(?:/>|>(.*?)</\1?\2>)}gms );
67  
68 17 ▶ if ( ! $parent_tag && not $str =~ s{\A\s*<\?xml[^>]*\?>}{} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent_tag or $str =~ s/\A\s*<\?xml[^>]*\?>//) 0 17 17
Condition66%2/3
$parent_tag or $str =~ s/\A\s*<\?xml[^>]*\?>//
l!l&&r!l&&!r
1340
MC/DC0%0/2
$parent_tag or $str =~ s/\A\s*<\?xml[^>]*\?>//
$parent_tag $str =~ s/\A\s*<\?xml[^>]*\?>//
Truth table66%2/3
A or B
A= $parent_tagB= $str =~ s/\A\s*<\?xml[^>]*\?>//
ABresult
1X1
011
000
69 0 croak "Not an XML data\n";
70 }
71  
72 #use re 'debug';
73  
74 17 ▶ while ( $str =~ s{\A\s* # start of string
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ s[\A\s* # start of string <([\w\-]+:)? # start of tag and namespace (if any) (\w+) # name of tag ([^>]*) # attributes in string form (?: /> | (?<!/)> # end of tag either by /> or > (.*?) # content of tag </\1?\2> # namespace and tagname in closing tag ) ][]gsx) 34 13 47
75 <([\w\-]+:)? # start of tag and namespace (if any)
76 (\w+) # name of tag
77 ([^>]*) # attributes in string form
78 (?:
79 />
80 |
81 (?<!/)> # end of tag either by /> or >
82 (.*?) # content of tag
83 </\1?\2> # namespace and tagname in closing tag
84 )
85 }{}gxs ) {
86 34 my @tag_arr = my ($namespace, $name, $attr, $content) = ($1, $2, $3, $4);
87  
88 34 ▶ if ( ! defined $name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 34 34
89 0 croak "Unable to parse: $str";
90 }
91  
92  
93 34 my $tag = {};
94  
95 34 ▶ if ( $namespace ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($namespace) 12 22 34
96 12 $namespace =~ tr/://d;
97 12 $tag->{namespace} = $namespace;
98 }
99  
100 34 ▶ $tag->{name} = $name if $name ;
Branch: 1 branch
BranchTrueFalseTotal
if $name 34 0 34
101  
102 ### @tag_arr
103  
104 34 ▶ if ( $content ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content) 31 3 34
105 31 $tag->{content} = $content;
106  
107 31 ▶ if ( $content =~ m/[<>]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /[<>]/) 13 18 31
108 13 $self->parse_xml_string( $content, $tag );
109 }
110  
111 }
112  
113 34 $tag->{attrs} = $attr;
114  
115 34 ▶ while( $attr =~ s{^\s*((?:[a-zA-Z-_]+:)?[a-zA-Z-_]+)=[\'\"]([^'"]*)[\'\"]}{}mgosx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr =~ s/^\s*((?:[a-zA-Z-_]+:)?[a-zA-Z-_]+)=[\'\"]([^'"]*)[\'\"]//gmosx) 54 34 88
116 54 $tag->{attr}{ $1 } = $2;
117 }
118  
119  
120 34 ▶ if ( ! $parent_tag ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent_tag) 4 30 34
121 4 $self->{tree} = $tag;
122 4 last;
123 }
124  
125 30 push @{ $parent_tag->{siblings} }, $tag;
126 }
127  
128 17 ▶ if ( ! $parent_tag && ! $self->{tree} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $parent_tag || $self->{"tree"}) 0 17 17
Condition66%2/3
$parent_tag || $self->{'tree'}
l!l&&r!l&&!r
1340
MC/DC0%0/2
$parent_tag || $self->{'tree'}
$parent_tag $self->{'tree'}
Truth table66%2/3
A || B
A= $parent_tagB= $self->{'tree'}
ABresult
1X1
011
000
129 0 croak "Could not parse $str: $.";
130 }
131 }
132  
133 4 ▶sub fetch_data_and_signature {
Subroutine fetch_data_and_signature: called CC=14 SCAR=30.7
Pod undocumented
134 4 my $self = shift;
135  
136 4 my $signature = {};
137  
138 ### $self->{tree}
139  
140 4 my $tree_root = $self->{tree};
141  
142 4 ▶ while(
Branch: 1 branch
BranchTrueFalseTotal
if (@{$tree_root->{"siblings"} or [];} == 1 and $tree_root->{"siblings"}[0]{"namespace"} =~ /soap/i) 4 4 8
Condition66%2/3
@{$tree_root->{"siblings"} or [];} == 1 and $tree_root->{"siblings"}[0]{"namespace"} =~ /soap/i
!ll&&!rl&&r
044
MC/DC50%1/2
@{$tree_root->{"siblings"} or [];} == 1 and $tree_root->{"siblings"}[0]{"namespace"} =~ /soap/i
@{$tree_root->{"siblings"} or [];} == 1 $tree_root->{"siblings"}[0]{"namespace"} =~ /soap/i
Truth table66%2/3
A and B
A= @{$tree_root->{"siblings"} or [];} == 1B= $tree_root->{"siblings"}[0]{"namespace"} =~ /soap/i
ABresult
0X0
100
111
143 8 ▶ @{ $tree_root->{siblings} || [] } == 1
Condition50%1/2
$tree_root->{'siblings'} || []
l!l
80
MC/DC0%0/1
$tree_root->{'siblings'} || []
$tree_root->{'siblings'}
Truth table50%1/2
A
A= $tree_root->{'siblings'}
Aresult
11
00
144 && $tree_root->{siblings}[0]{namespace} =~ /soap/i
145 ) {
146  
147 4 $tree_root = $tree_root->{siblings}[0];
148 }
149  
150 4 my ($data, $args_sig);
151  
152 my @tags =
153 4 grep { not $_->{name} =~ m/multiRef/ } @{ $tree_root->{siblings} };
154  
155 ### @tags
156 ### $tree_root
157  
158 4 ▶ if ( @tags == 1 && $tags[0]->{name} =~ m/Response/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@tags == 1 and $tags[0]{'name'} =~ /Response/i) { } 0 4 4
elsif (@tags == 1) { } 4 0 4
Condition33%1/3
@tags == 1 and $tags[0]{"name"} =~ /Response/i
!ll&&!rl&&r
040
MC/DC0%0/2
@tags == 1 and $tags[0]{"name"} =~ /Response/i
@tags == 1 $tags[0]{"name"} =~ /Response/i
Truth table33%1/3
A and B
A= @tags == 1B= $tags[0]{"name"} =~ /Response/i
ABresult
0X0
100
111
159 my %multirefs =
160 0 map { $_->{attr}{id} => $_ }
161 0 grep { $_->{name} =~ m/multiRef/ }
162 0 @{ $tree_root->{siblings} };
163  
164 ### %multirefs
165  
166 0 $self->{multirefs} = \%multirefs;
167  
168 #warn "multirefs: ", scalar keys %multirefs;
169  
170 0 my ($main_tag, $other) = @{ $tags[0]->{siblings} };
171 0 ▶ if ( $other ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($other) 0 0 0
172 0 croak "Something bad happened";
173 }
174
175 #local $main_tag->{ name } = $tags[0]->{name};
176  
177 #warn $main_tag->{name};
178 0 $signature->{name} = $tags[0]->{name};
179  
180 0 @tags = ({ %$main_tag, name => $tags[0]->{name} });
181 }
182 elsif( @tags == 1 ) {
183 4 $tree_root = $tags[0];
184 4 @tags = @{ $tags[0]->{siblings} };
185 }
186  
187 4 foreach my $sibling ( @tags ) {
188 21 my @ret = $self->_parse_tag( $sibling );
189  
190 21 my $tname = $ret[1]->{key};
191 21 $tname = join '_', map { lc } split /(?=[A-Z])/, $tname;
192  
193 21 $data->{ $tname } = $ret[0];
194 21 push @$args_sig, $ret[1];
195 }
196  
197 4 ▶ if ( @tags != 1 && $tree_root->{namespace} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tags != 1 and $tree_root->{"namespace"}) 2 2 4
Condition66%2/3
@tags != 1 and $tree_root->{"namespace"}
!ll&&!rl&&r
202
MC/DC50%1/2
@tags != 1 and $tree_root->{"namespace"}
@tags != 1 $tree_root->{"namespace"}
Truth table66%2/3
A and B
A= @tags != 1B= $tree_root->{"namespace"}
ABresult
0X0
100
111
198 2 my $ns = $self->{tree}{attr}{ 'xmlns:'.$tree_root->{namespace} };
199 2 ▶ $signature->{namespace} = $ns if $ns;
Branch: 1 branch
BranchTrueFalseTotal
if $ns 2 0 2
200 }
201  
202 4 ▶ $signature->{name} ||= $tree_root->{name};
Condition33%1/3
$signature->{"name"} ||= $tree_root->{"name"}
l!l&&r!l&&!r
040
MC/DC0%0/2
$signature->{"name"} ||= $tree_root->{"name"}
$signature->{"name"} $tree_root->{"name"}
Truth table33%1/3
A ||= B
A= $signature->{"name"}B= $tree_root->{"name"}
ABresult
1X1
011
000
203 4 $signature->{args} = $args_sig;
204  
205 4 ▶ if ( @tags == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tags == 1) 2 2 4
206 2 return (values %$data, $signature);
207 }
208  
209 2 return ($data, $signature);
210 }
211  
212 22 ▶sub _get_type {
Subroutine _get_type: called CC=3 SCAR=13.5
213 22 my $self = shift;
214 22 my $tag = shift;
215  
216 22 ▶ if ( $tag->{name} =~ m/^fault/ || $tag->{name} eq 'detail' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag->{"name"} =~ /^fault/ or $tag->{"name"} eq "detail") 0 22 22
Condition33%1/3
$tag->{"name"} =~ /^fault/ or $tag->{"name"} eq "detail"
l!l&&r!l&&!r
0022
MC/DC0%0/2
$tag->{"name"} =~ /^fault/ or $tag->{"name"} eq "detail"
$tag->{"name"} =~ /^fault/ $tag->{"name"} eq "detail"
Truth table33%1/3
A or B
A= $tag->{"name"} =~ /^fault/B= $tag->{"name"} eq "detail"
ABresult
1X1
011
000
217 0 $tag->{attr}{'xsi:type'} = 'xsd:string';
218 0 return 'string';
219 }
220  
221 22 return $tag->{attr}{'xsi:type'};
222 }
223  
224 22 ▶sub _parse_tag {
Subroutine _parse_tag: called CC=6 SCAR=23.5
225 22 my $self = shift;
226 22 my $tag = shift;
227  
228 22 ▶ $tag or croak "No tag given";
Branch: 1 branch
BranchTrueFalseTotal
$tag or croak("No tag given") 22 0 22
229  
230 22 ▶ if ( my $href = $tag->{attr}{href} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $href = $tag->{"attr"}{"href"}) 0 22 22
231 0 $href =~ s/^#//;
232 0 ▶ if ( ! wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not wantarray) { } 0 0 0
233 0 return $self->_parse_tag( $self->{multirefs}{ $href } );
234 }
235 else {
236 0 my @ret = $self->_parse_tag( $self->{multirefs}{ $href } );
237  
238 0 $ret[1]->{key} = $tag->{name};
239  
240 0 return @ret;
241 }
242 }
243  
244 22 ▶ my $type = $self->_get_type( $tag )
Branch: 1 branch
BranchTrueFalseTotal
my $type = $self->_get_type($tag) or croak(qq[Unknown type for tag $tag->{"name"}]) 22 0 22
245 or croak "Unknown type for tag $tag->{name}";
246  
247 22 $type =~ s/.*?://;
248  
249 22 $type = lc $type;
250 22 ▶ if ( my $s = $self->can('_parse_'.$type) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $s = $self->can('_parse_' . $type)) 22 0 22
251 22 return $s->( $self, $tag );
252 }
253  
254 0 croak "Cannot parse $type";
255 }
256  
257 20 ▶sub unescape_xml {
Subroutine unescape_xml: called CC=2 SCAR=6.9
Pod undocumented
258 20 $_ = shift;
259  
260 20 ▶ return $_ unless $_;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 2 18 20
261  
262 18 s/&amp;/&/xgs;
263 18 s/&lt;/</xgs;
264 18 s/&gt;/>/xgs;
265 18 s/&quot;/\"/xgs;
266  
267 18 return $_;
268 }
269  
270 20 ▶sub _parse_string {
Subroutine _parse_string: called CC=2 SCAR=7.0
271 20 my ($self, $tag) = @_;
272  
273 20 my $c = unescape_xml( $tag->{content} );
274  
275 20 ▶ return $c if not wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 20 20
276  
277 20 my @ret = ($c);
278  
279 20 my $t = $tag->{attr}{'xsi:type'};
280 20 $t =~ s/^.*://;
281  
282 push @ret, {
283 key => $tag->{name},
284 20 type => $t,
285 };
286  
287 20 return @ret;
288 }
289  
290 0 ▶sub _parse_boolean {
Subroutine _parse_boolean: not called CC=5 SCAR=34.0
291 0 my ($self, $tag) = @_;
292  
293 0 my ($val, $sig);
294  
295 0 ▶ if ( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
296 0 ($val, $sig) = $self->_parse_string( $tag );
297 }
298 else {
299 0 $val = $self->_parse_string( $tag );
300 }
301  
302 0 ▶ if ( lc $val eq 'true' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (lc $val eq 'true') { } 0 0 0
elsif ($val eq 'false') { } 0 0 0
303 0 $val = 1;
304 }
305 elsif( $val eq 'false' ) {
306 0 $val = 0;
307 }
308  
309 0 ▶ return wantarray ? ($val, $sig) : $val;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
310 }
311  
312  
313 2 ▶sub _parse_int {
Subroutine _parse_int: called CC=2 SCAR=7.0
314 2 my ($self, $tag) = @_;
315  
316 2 ▶ return int( $self->_parse_string( $tag ) ) if not wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 2 2
317  
318 2 my @ret = $self->_parse_string( $tag );
319 2 $ret[0] = int( $ret[0] );
320  
321 2 return @ret;
322 }
323  
324 1 ▶sub _parse_vector_or_array {
Subroutine _parse_vector_or_array: called CC=5 SCAR=16.5
325 1 my $self = shift;
326 1 my $tag = shift;
327  
328 1 my $array = [];
329 1 my $items = $tag->{siblings};
330  
331 1 my $elem_sig;
332  
333 1 foreach my $item (@$items) {
334 1 ▶ if ( $item->{name} ne 'item' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{"name"} ne "item") 0 1 1
335 0 croak "Vector or Array item has no name 'item'";
336 }
337  
338 1 my $value;
339
340 1 ▶ if ( $elem_sig ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem_sig) { } 0 1 1
341 0 $value = $self->_parse_tag($item);
342 }
343 else {
344 1 ($value, $elem_sig) = $self->_parse_tag($item);
345 }
346  
347 1 push @$array, $value;
348 }
349  
350 1 ▶ return $array if not wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 1 1
351  
352 1 my $type = $tag->{attr}{'xsi:type'};
353  
354 1 $type =~ m/(array|vector)/i;
355  
356 my $signature = {
357 key => $tag->{name},
358 1 type => lc $1,
359 elem_sig => $elem_sig,
360 };
361  
362 1 return ($array, $signature);
363 }
364  
365 1 ▶sub _parse_vector {
Subroutine _parse_vector: called CC=1 SCAR=0.0
366 1 shift->_parse_vector_or_array( @_ );
367 }
368  
369 0 ▶sub _parse_array {
Subroutine _parse_array: not called CC=1 SCAR=6.9
370 0 shift->_parse_vector_or_array( @_ );
371 }
372  
373 1 ▶sub _parse_map {
Subroutine _parse_map: called CC=5 SCAR=20.9
374 1 my $self = shift;
375 1 my $tag = shift;
376  
377 1 my $hash = {};
378  
379 1 my $items = $tag->{siblings};
380  
381 1 my ($key_sig, $value_sig);
382  
383 1 foreach my $item (@$items) {
384 0 ▶ if ( $item->{name} ne 'item' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{"name"} ne "item") 0 0 0
385 0 croak "Map item has no name 'item'";
386 }
387  
388 0 my ($key, $value);
389  
390 0 ▶ if ( $key_sig ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_sig) { } 0 0 0
391 0 $key = $self->_parse_tag($item->{siblings}[0]);
392 0 $value = $self->_parse_tag($item->{siblings}[1]);
393 }
394 else {
395 0 ($key, $key_sig) = $self->_parse_tag($item->{siblings}[0]);
396 0 ($value, $value_sig) = $self->_parse_tag($item->{siblings}[1]);
397  
398 #$key_type = ref $key ? $key_sig : $key_sig->{type}
399 #or croak "No key type given for $tag->{name}";
400 #$value_type = ref $value ? $value_sig : $value_sig->{type}
401 #or croak "No value type given for $tag->{name}";
402 }
403  
404  
405 0 $hash->{ $key } = $value;
406 }
407  
408 # warn Dumper $items;
409  
410 1 ▶ return $hash if not wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 1 1
411  
412 1 my $signature = {};
413  
414 1 $signature->{key} = $tag->{name};
415 1 $signature->{type} = 'map';
416  
417 1 $signature->{key_sig} = $key_sig ;
418 1 $signature->{value_sig} = $value_sig ;
419  
420  
421 1 return ($hash, $signature);
422 }
423  
424 2 1;
↑ Summary DirectI/Serialize.pm »