| 1 | package Workflow::Config::XML; | ||||||||||||
| 2 | |||||||||||||
| 3 | 18 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 18 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 18 | ▶ | use v5.14.0; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 18 | ▶ | use parent qw( Workflow::Config ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 18 | ▶ | use Log::Any qw( $log ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 18 | ▶ | use Workflow::Exception qw( configuration_error ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 18 | ▶ | use Carp qw(croak); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 18 | ▶ | use Syntax::Keyword::Try; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | 18 | $Workflow::Config::XML::VERSION = '2.12'; | |||||||||||
| 13 | |||||||||||||
| 14 | 18 | my %XML_OPTIONS = ( | |||||||||||
| 15 | action => { | ||||||||||||
| 16 | ForceArray => | ||||||||||||
| 17 | [ 'action', 'field', 'source_list', 'param', 'validator', 'arg' ], | ||||||||||||
| 18 | KeyAttr => [], | ||||||||||||
| 19 | }, | ||||||||||||
| 20 | condition => { | ||||||||||||
| 21 | ForceArray => [ 'condition', 'param' ], | ||||||||||||
| 22 | KeyAttr => [], | ||||||||||||
| 23 | }, | ||||||||||||
| 24 | persister => { | ||||||||||||
| 25 | ForceArray => ['persister'], | ||||||||||||
| 26 | KeyAttr => [], | ||||||||||||
| 27 | }, | ||||||||||||
| 28 | validator => { | ||||||||||||
| 29 | ForceArray => [ 'validator', 'param' ], | ||||||||||||
| 30 | KeyAttr => [], | ||||||||||||
| 31 | }, | ||||||||||||
| 32 | workflow => { | ||||||||||||
| 33 | ForceArray => [ | ||||||||||||
| 34 | 'extra_data', 'state', | ||||||||||||
| 35 | 'action', 'resulting_state', | ||||||||||||
| 36 | 'condition', 'observer' | ||||||||||||
| 37 | ], | ||||||||||||
| 38 | KeyAttr => [], | ||||||||||||
| 39 | }, | ||||||||||||
| 40 | observer => { | ||||||||||||
| 41 | ForceArray => [ 'observer' ], | ||||||||||||
| 42 | KeyAttr => [], | ||||||||||||
| 43 | } | ||||||||||||
| 44 | ); | ||||||||||||
| 45 | |||||||||||||
| 46 | 18 | my $XML_REQUIRED = 0; | |||||||||||
| 47 | 18 | my $warned = 0; | |||||||||||
| 48 | |||||||||||||
| 49 | 118 | ▶ | sub parse { | ||||||||||
|
Subroutine
parse: called CC=6 SCAR=18.5
Pod
documented
| |||||||||||||
| 50 | 118 | my ( $self, $type, @items ) = @_; | |||||||||||
| 51 | |||||||||||||
| 52 | 118 | ▶ | unless ($warned) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 53 | 17 | warn $log->warning( | |||||||||||
| 54 | 'WARNING: The use of XML configuration is deprecated; it\'s ' | ||||||||||||
| 55 | . 'based on XML::Simple, which itself is deprecated since 2012. ' | ||||||||||||
| 56 | . "Please use YAML instead.\n" ); | ||||||||||||
| 57 | |||||||||||||
| 58 | # warn once | ||||||||||||
| 59 | 17 | $warned = 1; | |||||||||||
| 60 | } | ||||||||||||
| 61 | 118 | $self->_check_config_type($type); | |||||||||||
| 62 | 118 | my @config_items = Workflow::Config::_expand_refs(@items); | |||||||||||
| 63 | 118 | ▶ | return () unless ( scalar @config_items ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 64 | |||||||||||||
| 65 | 118 | my @config = (); | |||||||||||
| 66 | 118 | foreach my $item (@config_items) { | |||||||||||
| 67 | 125 | ▶ | my $file_name = ( ref $item ) ? '[scalar ref]' : $item; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 68 | 125 | $log->info("Will parse '$type' XML config file '$file_name'"); | |||||||||||
| 69 | 125 | my $this_config; | |||||||||||
| 70 | try { | ||||||||||||
| 71 | 125 | $this_config = $self->_translate_xml( $type, $item ); | |||||||||||
| 72 | } | ||||||||||||
| 73 | 125 | catch ($error) { | |||||||||||
| 74 | # If processing multiple config files, this makes it much easier | ||||||||||||
| 75 | # to find a problem. | ||||||||||||
| 76 | 0 | croak $log->error("Processing $file_name: ", $error); | |||||||||||
| 77 | } | ||||||||||||
| 78 | 125 | $log->info("Parsed XML '$file_name' ok"); | |||||||||||
| 79 | |||||||||||||
| 80 | # This sets the outer-most tag to use | ||||||||||||
| 81 | # when returning the parsed XML. | ||||||||||||
| 82 | 125 | my $outer_tag = $self->get_config_type_tag($type); | |||||||||||
| 83 | 125 | ▶ | if ( ref $this_config->{$outer_tag} eq 'ARRAY' ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 84 | 0 | $log->debug("Adding multiple configurations for '$type'"); | |||||||||||
| 85 | 0 | push @config, @{ $this_config->{$outer_tag} }; | |||||||||||
| 86 | } else { | ||||||||||||
| 87 | 125 | $log->debug("Adding single configuration for '$type'"); | |||||||||||
| 88 | 125 | push @config, $this_config; | |||||||||||
| 89 | } | ||||||||||||
| 90 | } | ||||||||||||
| 91 | 118 | return @config; | |||||||||||
| 92 | } | ||||||||||||
| 93 | |||||||||||||
| 94 | # $config can either be a filename or scalar ref with file contents | ||||||||||||
| 95 | |||||||||||||
| 96 | 125 | ▶ | sub _translate_xml { | ||||||||||
|
Subroutine
_translate_xml: called CC=3 SCAR=11.1
| |||||||||||||
| 97 | 125 | my ( $self, $type, $config ) = @_; | |||||||||||
| 98 | 125 | ▶ | unless ($XML_REQUIRED) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 99 | try { | ||||||||||||
| 100 | 17 | require XML::Simple; | |||||||||||
| 101 | } | ||||||||||||
| 102 | 17 | catch ($error) { | |||||||||||
| 103 | 0 | configuration_error "XML::Simple must be installed to parse ", | |||||||||||
| 104 | "configuration files/data in XML format"; | ||||||||||||
| 105 | } | ||||||||||||
| 106 | |||||||||||||
| 107 | 17 | XML::Simple->import(':strict'); | |||||||||||
| 108 | 17 | $XML_REQUIRED++; | |||||||||||
| 109 | } | ||||||||||||
| 110 | 125 | ▶ | my $options = $XML_OPTIONS{$type} || {}; | ||||||||||
|
Condition50%1/2
$XML_OPTIONS{$type} || {}
MC/DC0%0/1
$XML_OPTIONS{$type} || {}
$XML_OPTIONS{$type}
Truth table50%1/2
A
A= $XML_OPTIONS{$type}
| |||||||||||||
| 111 | 125 | my $data = XMLin( $config, %{$options} ); | |||||||||||
| 112 | 125 | return $data; | |||||||||||
| 113 | } | ||||||||||||
| 114 | |||||||||||||
| 115 | 18 | 1; | |||||||||||
| 116 | |||||||||||||
| 117 | __END__ | ||||||||||||