File Coverage

blib/lib/Workflow/Config/XML.pm
Criterion Covered Total %
statement 51 54 94.4
branch 7 10 70.0
condition 1 2 50.0
subroutine 10 10 100.0
pod 1 1 100.0
total 70 77 90.9


line stmt bran cond sub pod time code
1             package Workflow::Config::XML;
2              
3 18     18   197126 use warnings;
  18         49  
  18         1412  
4 18     18   125 use strict;
  18         34  
  18         530  
5 18     18   267 use v5.14.0;
  18         61  
6 18     18   93 use parent qw( Workflow::Config );
  18         63  
  18         145  
7 18     18   1607 use Log::Any qw( $log );
  18         51  
  18         145  
8 18     18   10872 use Workflow::Exception qw( configuration_error );
  18         44  
  18         1409  
9 18     18   117 use Carp qw(croak);
  18         32  
  18         985  
10 18     18   1249 use Syntax::Keyword::Try;
  18         6905  
  18         151  
11              
12             $Workflow::Config::XML::VERSION = '2.09';
13              
14             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             my $XML_REQUIRED = 0;
47             my $warned = 0;
48              
49             sub parse {
50 118     118 1 15047 my ( $self, $type, @items ) = @_;
51              
52 118 100       445 unless ($warned) {
53 17         123 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         2562 $warned = 1;
60             }
61 118         750 $self->_check_config_type($type);
62 118         489 my @config_items = Workflow::Config::_expand_refs(@items);
63 118 50       361 return () unless ( scalar @config_items );
64              
65 118         235 my @config = ();
66 118         242 foreach my $item (@config_items) {
67 125 50       425 my $file_name = ( ref $item ) ? '[scalar ref]' : $item;
68 125         882 $log->info("Will parse '$type' XML config file '$file_name'");
69 125         495 my $this_config;
70             try {
71             $this_config = $self->_translate_xml( $type, $item );
72             }
73 125         338 catch ($error) {
74             # If processing multiple config files, this makes it much easier
75             # to find a problem.
76             croak $log->error("Processing $file_name: ", $error);
77             }
78 125         1360 $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         1394 my $outer_tag = $self->get_config_type_tag($type);
83 125 50       710 if ( ref $this_config->{$outer_tag} eq 'ARRAY' ) {
84 0         0 $log->debug("Adding multiple configurations for '$type'");
85 0         0 push @config, @{ $this_config->{$outer_tag} };
  0         0  
86             } else {
87 125         847 $log->debug("Adding single configuration for '$type'");
88 125         688 push @config, $this_config;
89             }
90             }
91 118         881 return @config;
92             }
93              
94             # $config can either be a filename or scalar ref with file contents
95              
96             sub _translate_xml {
97 125     125   376 my ( $self, $type, $config ) = @_;
98 125 100       406 unless ($XML_REQUIRED) {
99             try {
100             require XML::Simple;
101             }
102 17         38 catch ($error) {
103             configuration_error "XML::Simple must be installed to parse ",
104             "configuration files/data in XML format";
105             }
106              
107 17         364869 XML::Simple->import(':strict');
108 17         2136 $XML_REQUIRED++;
109             }
110 125   50     565 my $options = $XML_OPTIONS{$type} || {};
111 125         521 my $data = XMLin( $config, %{$options} );
  125         711  
112 125         4319203 return $data;
113             }
114              
115             1;
116              
117             __END__
118              
119             =pod
120              
121             =head1 NAME
122              
123             Workflow::Config::XML - Parse workflow configurations from XML content
124              
125             =head1 VERSION
126              
127             This documentation describes version 2.09 of this package
128              
129             =head1 SYNOPSIS
130              
131             my $parser = Workflow::Config->new( 'xml' );
132             my $conf = $parser->parse( 'condition',
133             'my_conditions.xml', 'your_conditions.xml' );
134              
135             =head1 DESCRIPTION
136              
137             Implementation of configuration parser for XML files/data; requires
138             L<XML::Simple> to be installed. See L<Workflow::Config> for C<parse()>
139             description.
140              
141             =head2 Status of this module
142              
143             The use of XML configuration is deprecated and slated for removal in
144             Workflow 3.0. Please use L<Workflow::Config::YAML> for YAML-based
145             configuration instead, or write your own XML configuration module;
146             see L<Workflow::Config/Creating Your Own Parser> for an explanation
147             of how.
148              
149             =head1 METHODS
150              
151             =head2 parse ( $type, @items )
152              
153             This method parses the configuration provided it is in XML format.
154              
155             Takes two parameters: a $type indication and an array of of items
156              
157             Returns a list of config parameters as a array upon success.
158              
159             =head1 SEE ALSO
160              
161             =over
162              
163             =item * L<XML::Simple>
164              
165             =item * L<Workflow::Config>
166              
167             =back
168              
169             =head1 COPYRIGHT
170              
171             Copyright (c) 2004-2021 Chris Winters. All rights reserved.
172              
173             This library is free software; you can redistribute it and/or modify
174             it under the same terms as Perl itself.
175              
176             Please see the F<LICENSE>
177              
178             =head1 AUTHORS
179              
180             Please see L<Workflow>
181              
182             =cut