Constants/XML/SAX/Handler.pm

filter: Statementstmt 96.7% 60 / 62 Branchbran 82.3% 28 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 5 / 5 totaltotal 92.7% 103 / 111 CC 18 SCAR 17.4
CC
18
Cov
92%
start_element
10.1
characters
5.3
end_element
4.0
CRAP 5.7
« Constants.pm ↑ Summary Constants/Perl.pm »
1 package Config::Constants::XML::SAX::Handler;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 our $VERSION = '0.03';
7  
8 10 ▶use constant MAX_INCLUDE_DEPTH => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10 ▶use base 'XML::SAX::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 8 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
13 8 my $class = shift;
14 8 my $self = $class->SUPER::new(@_);
15 8 $self->{_config} = undef;
16 8 $self->{_current_module} = undef;
17 8 $self->{_current_constant} = undef;
18 8 $self->{_constant_deferred} = 0;
19 8 $self->{_expected_type} = undef;
20 8 $self->{_current_text} = undef;
21 8 $self->{_include_depth} = 0;
22 8 return $self;
23 }
24  
25 7 ▶sub config { (shift)->{_config} }
Subroutine config: called CC=1 SCAR=0.0
Pod documented
26  
27 56 ▶sub start_element {
Subroutine start_element: called CC=10 SCAR=23.1
Pod documented
28 56 my ($self, $el) = @_;
29 56 my $tag_name = lc($el->{Name});
30 56 ▶ if ($tag_name eq 'config') {
Branch: 4 branches
BranchTrueFalseTotal
if ($tag_name eq 'config') { } 15 41 56
elsif ($tag_name eq 'include') { } 8 33 41
elsif ($tag_name eq 'module') { } 14 19 33
elsif ($tag_name eq 'constant') { } 19 0 19
31 15 ▶ $self->{_config} = {} unless $self->{_config};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_config"} 8 7 15
32 }
33 elsif ($tag_name eq 'include') {
34 8 my $path = $self->_get_value($el, 'path');
35 8 ▶ (-e $path)
Branch: 1 branch
BranchTrueFalseTotal
-e $path or die "Cannot find include file at '${path}'" 8 0 8
36 || die "Cannot find include file at '$path'";
37 8 ▶ ($self->{_include_depth} < $self->MAX_INCLUDE_DEPTH)
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_include_depth"} < $self->MAX_INCLUDE_DEPTH or die "You have reached the max include depth" 7 1 8
38 || die "You have reached the max include depth";
39 7 $self->{_include_depth}++;
40 7 my $p = XML::SAX::ParserFactory->parser(Handler => $self);
41 7 $p->parse_uri($path);
42 }
43 elsif ($tag_name eq 'module') {
44 14 $self->{_current_module} = $self->_get_value($el, 'name');
45 $self->{_config}->{$self->{_current_module}} = {}
46 14 ▶ unless exists $self->{_config}->{$self->{_current_module}};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_config"}{$self->{"_current_module"}} 12 2 14
47 }
48 elsif ($tag_name eq 'constant') {
49 19 $self->{_current_constant} = $self->_get_value($el, 'name');
50 19 ▶ if (defined($self->_get_value($el, 'value'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_get_value($el, 'value')) { } 16 3 19
51 $self->{_config}
52 ->{$self->{_current_module}}
53 16 ->{$self->{_current_constant}} = $self->_get_value($el, 'value')
54 }
55 else {
56 3 $self->{_expected_type} = $self->_get_value($el, 'type');
57 3 $self->{_constant_deferred} = 1;
58 }
59 }
60 else {
61 0 die "did not recognize the tag: $tag_name";
62 }
63 }
64  
65 44 ▶sub end_element {
Subroutine end_element: called CC=4 SCAR=13.9
Pod documented
66 44 my ($self, $el) = @_;
67 44 ▶ return unless lc($el->{Name}) eq 'constant';
Branch: 1 branch
BranchTrueFalseTotal
unless lc $el->{"Name"} eq "constant" 25 19 44
68 19 ▶ return unless $self->{_constant_deferred};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_constant_deferred"} 16 3 19
69 ($self->{_current_text})
70 3 ▶ || die "We dont have anything to put into the constant '" . $self->{_current_constant} . "'";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_current_text"} or die q[We dont have anything to put into the constant '] . $self->{"_current_constant"} . q['] 3 0 3
71 $self->{_config}
72 ->{$self->{_current_module}}
73 3 ->{$self->{_current_constant}} = $self->{_current_text};
74 3 $self->{_current_text} = undef;
75 3 $self->{_constant_deferred} = 0;
76 }
77  
78 67 ▶sub characters {
Subroutine characters: called CC=5 SCAR=16.7
Pod documented
79 67 my ($self, $el) = @_;
80 67 my $data = $el->{Data};
81 67 ▶ return if $data =~ /^\s+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $data =~ /^\s+$/ 64 3 67
82 3 ▶ if ($self->{_expected_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_expected_type'}) { } 3 0 3
83 3 my $value = eval $data;
84 3 ▶ die "eval of constant value failed: '$data' -> $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
85 (UNIVERSAL::isa($value, $self->{_expected_type}))
86 3 ▶ || die "constant did not eval into the type we expected ($data); got : '$value' - expected: '" . $self->{_expected_type}. "'";
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($value, $self->{"_expected_type"}) or die "constant did not eval into the type we expected ($data); got : '${value}' - expected: '" . $self->{"_expected_type"} . q['] 3 0 3
87 3 $self->{_current_text} = $value;
88 }
89 else {
90 # its just plain text
91 0 $self->{_current_text} = $data;
92 }
93 }
94  
95 79 ▶sub _get_value {
Subroutine _get_value: called CC=2 SCAR=6.9
96 79 my ($self, $el, $key) = @_;
97 79 ▶ return undef unless exists $el->{Attributes}->{'{}' . $key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $el->{"Attributes"}{'{}' . $key} 3 76 79
98 76 return $el->{Attributes}->{'{}' . $key}->{Value};
99 }
100  
101 10 1;
102  
103 __END__
« Constants.pm ↑ Summary Constants/Perl.pm »