| 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
| |||||||||||||||||||||||
| 31 | 15 | ▶ | $self->{_config} = {} unless $self->{_config}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||
| 33 | elsif ($tag_name eq 'include') { | ||||||||||||||||||||||
| 34 | 8 | my $path = $self->_get_value($el, 'path'); | |||||||||||||||||||||
| 35 | 8 | ▶ | (-e $path) | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 36 | || die "Cannot find include file at '$path'"; | ||||||||||||||||||||||
| 37 | 8 | ▶ | ($self->{_include_depth} < $self->MAX_INCLUDE_DEPTH) | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 68 | 19 | ▶ | return unless $self->{_constant_deferred}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 69 | ($self->{_current_text}) | ||||||||||||||||||||||
| 70 | 3 | ▶ | || die "We dont have anything to put into the constant '" . $self->{_current_constant} . "'"; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 82 | 3 | ▶ | if ($self->{_expected_type}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 83 | 3 | my $value = eval $data; | |||||||||||||||||||||
| 84 | 3 | ▶ | die "eval of constant value failed: '$data' -> $@" if $@; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||
| 98 | 76 | return $el->{Attributes}->{'{}' . $key}->{Value}; | |||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||
| 100 | |||||||||||||||||||||||
| 101 | 10 | 1; | |||||||||||||||||||||
| 102 | |||||||||||||||||||||||
| 103 | __END__ | ||||||||||||||||||||||