blib/lib/Magpie/ConfigReader/XML.pm

filter: Statementstmt 85.9% 196 / 228 Branchbran 75.5% 68 / 90 Conditioncond 54.1% 13 / 24 MC/DCmcdc 26.6% 4 / 15 Subroutinesub 95.0% 19 / 20 Podpod 0.0% 0 / 11 totaltotal 77.3% 300 / 388 CC 70 SCAR 25.8
CC
70
Cov
81%
process_match
25.0
process_asset_service
16.4
process_add
12.1
CRAP 13.2
« blib/lib/Magpie/Matcher.pm ↑ Summary blib/lib/Magpie/Transformer/XSP.pm »
1 package Magpie::ConfigReader::XML;
2 #ABSTRACT: Magpie Configuration via XML
3 12 $Magpie::ConfigReader::XML::VERSION = '1.163200';
4 12 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Magpie::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Magpie::Plugin::URITemplate;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use Class::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 #use Data::Printer;
11  
12 60 ▶sub make_token {
Subroutine make_token: called CC=1 SCAR=0.0
Pod undocumented
13 60 return '__MTOKEN__XMLCONF';
14 }
15  
16 36 ▶sub make_match_token {
Subroutine make_match_token: called CC=1 SCAR=0.0
Pod undocumented
17 36 return '__MATCH__' . int( rand(100000) );
18 }
19  
20 has match_stack => (
21 traits => ['Array'],
22 is => 'rw',
23 isa => 'ArrayRef[ArrayRef]',
24 12 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
25 12 handles => {
26 push_stack => 'push',
27 },
28 );
29  
30 has assets => (
31 traits => ['Array'],
32 is => 'rw',
33 isa => 'ArrayRef[Object]',
34 12 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
35 12 handles => {
36 add_asset => 'push',
37 },
38 );
39  
40 has accept_matrix => (
41 traits => ['Array'],
42 is => 'rw',
43 isa => 'ArrayRef[ArrayRef]',
44 12 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
45 12 handles => {
46 add_variant => 'push',
47 },
48 );
49  
50 12 ▶sub process {
Subroutine process: called CC=8 SCAR=20.8
Pod undocumented
51 12 my $self = shift;
52 12 my $xml_file = shift;
53  
54 12 my $dom = XML::LibXML->load_xml( location => $xml_file );
55 12 my $root = $dom->documentElement;
56  
57 12 ▶ if ( $root->exists('//accept_matrix')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($root->exists("//accept_matrix")) 2 10 12
58 2 $self->process_accept_matrix( $root->findnodes('//accept_matrix') );
59 }
60  
61 12 ▶ if ( $root->exists('//assets')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($root->exists("//assets")) 6 6 12
62 6 $self->process_assets( $root->findnodes('//assets') );
63 }
64  
65 # now process the handler pipeline
66 12 foreach my $pipe ($root->findnodes('//pipeline')) {
67 12 foreach my $pipe_child ($pipe->childNodes) {
68 62 my $pipe_child_name = $pipe_child->localname;
69 62 ▶ next unless $pipe_child_name; # skip txt nodes here
Branch: 1 branch
BranchTrueFalseTotal
unless $pipe_child_name 37 25 62
70 25 my ($match_type, $to_match, $input) = (undef, undef, []);
71 25 ▶ if ( $pipe_child_name eq 'add' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pipe_child_name eq 'add') { } 4 21 25
elsif ($pipe_child_name eq 'match') { } 21 0 21
72 4 $match_type = 'AUTO';
73 4 push @{$input}, process_add( $pipe_child );
74 }
75 elsif ( $pipe_child_name eq 'match' ) {
76 #push @{$input}, $self->process_match( $pipe_child );
77 21 $self->process_match( $pipe_child );
78 21 next;
79 }
80 else {
81 0 warn "Unknown child element '$pipe_child_name' in config.\n";
82 }
83 4 $self->push_stack( [$match_type, $to_match, $input, make_token, make_match_token] );
84 }
85  
86 }
87  
88 }
89  
90 32 ▶sub process_match {
Subroutine process_match: called CC=23 SCAR=32.2
Pod undocumented
91 32 my $self = shift;
92 32 my $node = shift;
93 32 my @input = ();
94 32 my $match_type = $node->findvalue('@type|./type/text()');
95 32 $match_type = uc $match_type;
96 32 my $to_match = $node->findvalue('@rule|./rule/text()');
97  
98 32 foreach my $child ($node->childNodes) {
99 180 my $name = $child->localname;
100 180 ▶ next unless $name && length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name and length $name 106 74 180
Condition66%2/3
$name and length $name
!ll&&!rl&&r
106074
MC/DC50%1/2
$name and length $name
$name length $name
Truth table66%2/3
A and B
A= $nameB= length $name
ABresult
0X0
100
111
101 74 ▶ if ($name eq 'add') {
Branch: 3 branches
BranchTrueFalseTotal
if ($name eq 'add') { } 57 17 74
elsif ($name eq 'match') { } 11 6 17
elsif ($name eq 'reset') { } 1 5 6
102 57 push @input, process_add($child);
103 }
104 elsif ($name eq 'match') {
105 11 push @input, $self->process_match($child);
106 }
107 elsif ($name eq 'reset') {
108 1 push @input, '__RESET__';
109 }
110 }
111  
112 32 ▶ if ( $match_type eq 'REGEXP' ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($match_type eq 'REGEXP') { } 17 15 32
elsif ($match_type eq 'LITERAL') { } 1 14 15
elsif ($match_type eq 'ENV') { } 5 9 14
elsif ($match_type eq 'ACCEPT') { } 4 5 9
elsif ($match_type eq 'TEMPLATE') { } 5 0 5
113 17 $to_match = qr|$to_match|;
114 }
115 elsif ($match_type eq 'LITERAL') {
116 1 $match_type = 'STRING';
117 }
118 elsif ($match_type eq 'ENV') {
119 5 $match_type = 'HASH';
120 5 $to_match = {};
121 5 foreach my $rule ($node->findnodes('./rules/rule')) {
122 5 my $key = $rule->findvalue('@key|./key/text()');
123 5 my $val = $rule->findvalue('@value|./value/text()');
124 5 my $type = $rule->findvalue('@type|./value/@type|./value/type/text()');
125 5 ▶ if ( $type && $type eq 'regexp' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type and $type eq "regexp") 2 3 5
Condition66%2/3
$type and $type eq "regexp"
!ll&&!rl&&r
302
MC/DC50%1/2
$type and $type eq "regexp"
$type $type eq "regexp"
Truth table66%2/3
A and B
A= $typeB= $type eq "regexp"
ABresult
0X0
100
111
126 2 $val = qr|$val|;
127 }
128 5 ▶ next unless $key && $val;
Branch: 1 branch
BranchTrueFalseTotal
unless $key and $val 0 5 5
Condition33%1/3
$key and $val
!ll&&!rl&&r
005
MC/DC0%0/2
$key and $val
$key $val
Truth table33%1/3
A and B
A= $keyB= $val
ABresult
0X0
100
111
129 5 $to_match->{$key} = $val;
130 }
131 }
132 elsif ($match_type eq 'ACCEPT') {
133 4 $to_match = $node->findvalue('@variant_name|./variant_name/text()');
134 }
135 # NOTE: See comment in Plack::Middleware::Magpie re: this munging.
136 elsif ($match_type eq 'TEMPLATE') {
137 5 $match_type = 'REGEXP';
138 5 my $uri_template = $to_match;
139  
140 # to_match becomes the compiled regexp here
141 5 my ($match_re, $names) = Magpie::Plugin::URITemplate::process_template($to_match);
142 5 $to_match = $match_re;
143 5 my @parameterized = ();
144 5 ▶ for (my $i = 0; $i < scalar @input; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @input) 11 5 16
145 11 ▶ next if ref( $input[$i] ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $input[$i] eq "HASH" 5 6 11
146 6 ▶ if ($input[$i] =~/__MATCH__/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input[$i] =~ /__MATCH__/) 1 5 6
147 1 push @parameterized, $input[$i];
148 1 next;
149 }
150  
151 5 my $args = {};
152 5 ▶ if ( ref( $input[$i + 1 ]) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $input[$i + 1] eq "HASH") 5 0 5
153 5 $args = $input[$i + 1 ];
154 }
155  
156 5 ▶ if (defined $args->{traits}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{'traits'}) { } 0 5 5
157 0 ▶ if (ref $args->{traits} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->{'traits'} eq 'ARRAY') { } 0 0 0
158 0 push @{$args->{traits}}, '+Magpie::Plugin::URITemplate';
159 }
160 else {
161 0 my $existing = delete $args->{traits};
162 0 $args->{traits} = [$existing, '+Magpie::Plugin::URITemplate'];
163 }
164 }
165 else {
166 5 $args->{traits} = ['+Magpie::Plugin::URITemplate'];
167 }
168  
169 5 $args->{uri_template} = $uri_template;
170 5 push @parameterized, ($input[$i], $args);
171 }
172 5 @input = @parameterized;
173 }
174  
175 32 my $match_token = make_match_token;
176 32 $self->push_stack( [$match_type, $to_match, \@input, make_token, $match_token] );
177 32 return $match_token;
178 }
179  
180 61 ▶sub process_add {
Subroutine process_add: called CC=8 SCAR=24.9
Pod undocumented
181 61 my $node = shift;
182 61 my $class_name = $node->findvalue('@class|./class/text()');
183 61 my $params = {};
184 61 ▶ if ($node->exists('./parameters')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->exists("./parameters")) 19 42 61
185 19 foreach my $param ($node->findnodes('./parameters/*')) {
186 19 my ($name, $value) = (undef, undef);
187 19 ▶ if ($param->localname eq 'parameter' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->localname eq 'parameter') { } 0 19 19
188 0 $name = $param->findvalue('@name|./name/text()');
189 0 $value = $param->findvalue('@value|./value/text()');
190 }
191 else {
192 19 $name = $param->localname;
193 19 $value = $param->findvalue('./text()');
194 }
195  
196 19 $value =~ s/^\s+//;
197 19 $value =~ s/\s+$//;
198  
199  
200 19 ▶ if( $name && $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name and $value) 19 0 19
Condition33%1/3
$name and $value
!ll&&!rl&&r
0019
MC/DC0%0/2
$name and $value
$name $value
Truth table33%1/3
A and B
A= $nameB= $value
ABresult
0X0
100
111
201 19 ▶ if (defined $params->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params->{$name}) { } 0 19 19
202 0 ▶ if (ref ($params->{$name}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params->{$name} eq 'ARRAY') { } 0 0 0
203 0 push @{$params->{$name}}, $value;
204 }
205 else {
206 0 my $existing = delete $params->{$name};
207 0 $params->{$name} = [$existing, $value];
208 }
209 }
210 else {
211 19 $params->{$name} = $value;
212 }
213 }
214 }
215 }
216  
217 61 return ($class_name, $params);
218 }
219  
220 0 ▶sub trim_whitespace {
Subroutine trim_whitespace: not called CC=1 SCAR=6.9
Pod undocumented
221 0 my $string = shift;
222 0 $string =~ s/^\s+//;
223 0 $string =~ s/\s+$//;
224 0 return $string;
225 }
226  
227 2 ▶sub process_accept_matrix {
Subroutine process_accept_matrix: called CC=9 SCAR=24.8
Pod undocumented
228 2 my $self = shift;
229 2 my $node = shift;
230 2 foreach my $variant ($node->findnodes('./variant')) {
231 4 my $name = $variant->findvalue('@name|./name/text()');
232 4 ▶ next unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 4 4
233 4 my ($type, $lang, $qs, $encoding, $charset, $length);
234  
235 4 ▶ if ($variant->exists('@type|./type')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($variant->exists("\@type|./type")) 4 0 4
236 4 $type = $variant->findvalue('@type|./type/text()');
237 }
238  
239 4 ▶ if ($variant->exists('@language|./language|@lang|./lang')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($variant->exists("\@language|./language|\@lang|./lang")) 4 0 4
240 4 $lang = $variant->findvalue('@language|./language/text()|@lang|./lang/text()');
241 }
242  
243 4 ▶ if ($variant->exists('@qs|./qs')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($variant->exists("\@qs|./qs")) 0 4 4
244 0 $qs = $variant->findvalue('@qs|./qs/text()');
245 }
246  
247 4 ▶ if ($variant->exists('@encoding|./encoding')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($variant->exists("\@encoding|./encoding")) 0 4 4
248 0 $encoding = $variant->findvalue('@encoding|./encoding/text()');
249 }
250  
251 4 ▶ if ($variant->exists('@charset|./charset')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($variant->exists("\@charset|./charset")) 0 4 4
252 0 $charset = $variant->findvalue('@charset|./charset/text()');
253 }
254  
255 4 ▶ if ($variant->exists('@length|./length')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($variant->exists("\@length|./length")) 0 4 4
256 0 $length = $variant->findvalue('@length|./length/text()');
257 }
258 4 $self->add_variant([$name, $qs, $type, $encoding, $charset, $lang, $length]);
259 }
260 }
261  
262 6 ▶sub process_assets {
Subroutine process_assets: called CC=4 SCAR=15.2
Pod undocumented
263 6 Class::Load::load_class('Bread::Board');
264 6 my $self = shift;
265 6 my $node = shift;
266 6 foreach my $container ($node->findnodes('./container')) {
267 5 $self->process_asset_container($container);
268 }
269  
270 6 foreach my $service ($node->findnodes('./service')) {
271 6 $self->process_asset_service($service);
272 }
273  
274 6 foreach my $alias ($node->findnodes('./alias')) {
275 0 my $name = $alias->findvalue('@name|./name/text()');
276 0 my $path = $alias->findvalue('@path|./path/text()');
277 0 my $service_alias = Bread::Board::Service::Alias->new(
278 name => $name,
279 aliased_from_path => $path,
280 );
281 0 $self->add_asset($service_alias)
282 }
283 }
284  
285 6 ▶sub process_asset_container {
Subroutine process_asset_container: called CC=6 SCAR=19.0
Pod undocumented
286 6 my ($self, $node, $parent) = @_;
287 6 ▶ my $name = $node->findvalue('@name|./name/text()') || '';
Condition50%1/2
$node->findvalue('@name|./name/text()') || ''
l!l
60
MC/DC0%0/1
$node->findvalue('@name|./name/text()') || ''
$node->findvalue('@name|./name/text()')
Truth table50%1/2
A
A= $node->findvalue('@name|./name/text()')
Aresult
11
00
288 6 my $c = Bread::Board::Container->new( name => $name );
289 6 ▶ if (defined $parent) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parent) { } 1 5 6
290 1 $parent->add_sub_container($c);
291 }
292 else {
293 5 $self->add_asset($c);
294 }
295  
296 6 foreach my $child ($node->findnodes('./container')) {
297 1 $self->process_asset_container($child, $c);
298 }
299  
300 6 foreach my $service ($node->findnodes('./service')) {
301 6 $self->process_asset_service($service, $c);
302 }
303  
304 6 foreach my $alias ($node->findnodes('./alias')) {
305 0 my $name = $alias->findvalue('@name|./name/text()');
306 0 my $path = $alias->findvalue('@path|./path/text()');
307 0 my $service_alias = Bread::Board::Service::Alias->new(
308 name => $name,
309 aliased_from_path => $path,
310 );
311 0 $c->add_service($service_alias)
312 }
313  
314 }
315  
316 12 ▶sub process_asset_service {
Subroutine process_asset_service: called CC=16 SCAR=28.0
Pod undocumented
317 12 my ($self, $node, $container) = @_;
318  
319 12 my %service_args = (
320 name => $node->findvalue('@name|./name/text()'),
321 );
322  
323 12 ▶ if ($node->exists('@class|./class')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->exists("\@class|./class")) 5 7 12
324 5 $service_args{class} = $node->findvalue('@class|./class/text()');
325 }
326  
327 12 my $injector_type = $node->findvalue('@type|./type/text()');
328  
329 12 ▶ $injector_type ||= 'literal';
Condition100%2/2
$injector_type ||= "literal"
l!l
57
MC/DC100%1/1
$injector_type ||= "literal"
$injector_type
Truth table100%2/2
A
A= $injector_type
Aresult
11
00
330 12 my $injector_subclass;
331  
332 12 ▶ if ($injector_type eq 'literal') {
Branch: 4 branches
BranchTrueFalseTotal
if ($injector_type eq 'literal') { } 7 5 12
elsif ($injector_type eq 'constructor') { } 3 2 5
elsif ($injector_type eq 'setter') { } 1 1 2
elsif ($injector_type eq 'block') { } 1 0 1
333 7 $injector_subclass = 'Bread::Board::Literal';
334 7 $service_args{value} = $node->findvalue('@value|./value/text()|./text()');
335 }
336 elsif ($injector_type eq 'constructor') {
337 3 $injector_subclass = 'Bread::Board::ConstructorInjection';
338 }
339 elsif ($injector_type eq 'setter') {
340 1 $injector_subclass = 'Bread::Board::SetterInjection';
341 }
342 elsif ($injector_type eq 'block') {
343 1 $injector_subclass = 'Bread::Board::BlockInjection';
344 1 my %deps = ();
345  
346 1 ▶ if ($node->exists('@class|./class')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->exists("\@class|./class")) 1 0 1
347 1 my $dep = $node->findvalue('@class|./class/text()');
348 1 $deps{$dep} = 1;
349 }
350  
351 1 foreach my $classnode ($node->findnodes('./requires/class')) {
352 0 my $dep = $classnode->findvalue('.');
353 0 $deps{$dep} = 1;
354 }
355  
356 1 my $dep_string = join "\n", map { "use $_;" } keys %deps;
357  
358 1 my $block = $node->findvalue('./block/text()');
359 1 my $pkg_name = random_string();
360 1 my $subname = random_string();
361 1 my $full_name = $pkg_name . '::' . $subname;
362 1 my $pkg = 'package ' . $pkg_name .'; ' . $dep_string . ' sub ' . $subname . '{' . $block . '} 1;';
363  
364 1 ▶ eval "$pkg";
Subroutine BEGIN: called CC=1 SCAR=0.0
365 1 $service_args{block} = \&$full_name;
366 }
367  
368 12 ▶ if ($node->exists('@lifecycle|./lifecycle')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->exists("\@lifecycle|./lifecycle")) 0 12 12
369 0 $service_args{lifecycle} = $node->findvalue('@lifecycle|./lifecycle');
370 }
371  
372 12 ▶ if ($node->exists('./dependencies')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->exists("./dependencies")) 5 7 12
373 5 my $deps = {};
374 5 foreach my $d ($node->findnodes('./dependencies/dependency')) {
375 10 my $dep_type = $d->findvalue('@type|./type/text()');
376 10 ▶ if ($dep_type && $dep_type eq 'literal') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dep_type and $dep_type eq 'literal') { } 5 5 10
Condition66%2/3
$dep_type and $dep_type eq "literal"
!ll&&!rl&&r
505
MC/DC50%1/2
$dep_type and $dep_type eq "literal"
$dep_type $dep_type eq "literal"
Truth table66%2/3
A and B
A= $dep_typeB= $dep_type eq "literal"
ABresult
0X0
100
111
377 5 my $dep_name = $d->findvalue('@name|./name/text()');
378 5 my $dep_val = $d->findvalue('@value|./value/text()');
379 5 ▶ my $dep_key = $d->findvalue('@key|./key/text()') || $dep_name;
Condition33%1/3
$d->findvalue('@key|./key/text()') || $dep_name
l!l&&r!l&&!r
050
MC/DC0%0/2
$d->findvalue('@key|./key/text()') || $dep_name
$d->findvalue('@key|./key/text()') $dep_name
Truth table33%1/3
A || B
A= $d->findvalue('@key|./key/text()')B= $dep_name
ABresult
1X1
011
000
380 5 $deps->{$dep_key} = Bread::Board::Literal->new( name => $dep_name, value => $dep_val);
381 }
382 else {
383 5 my $dep_name = $d->findvalue('@name|./name/text()');
384 5 my $dep_path = $d->findvalue('@service_path|./service_path/text()');
385 5 $deps->{$dep_name} = Bread::Board::Dependency->new( service_path => $dep_path );
386 }
387 }
388  
389 5 $service_args{dependencies} = $deps;
390 }
391  
392 12 my $s = $injector_subclass->new(%service_args);
393  
394 12 ▶ if (defined $container) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $container) { } 6 6 12
395 6 $container->add_service($s);
396 }
397 else {
398 6 $self->add_asset($s);
399 }
400 }
401  
402 3 ▶sub random_string {
Subroutine random_string: called CC=3 SCAR=11.0
Pod undocumented
403 3 ▶ my $length = shift || 10;
Condition50%1/2
shift() || 10
l!l
02
MC/DC0%0/1
shift() || 10
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
404 3 my $ret = '';
405 3 my @chars = ('a'..'z', 'A'..'Z');
406 2 for (0..$length) {
407 22 $ret .= $chars[ rand @chars ];
408 }
409 2 return $ret;
410 }
411 # SEEALSO: Magpie
412  
413 12 1;
414  
415 __END__
« blib/lib/Magpie/Matcher.pm ↑ Summary blib/lib/Magpie/Transformer/XSP.pm »