blib/lib/XML/Toolset/BestAvailable.pm untested

filter: Statementstmt 0.0% 0 / 42 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 74 CC 9 SCAR 31.9
CC
9
Cov
0%
_best_available
42.0
_init
20.0
BEGIN
2.0
CRAP 24.3
1 # Factory class providing a common interface to different XML validators
2  
3 package XML::Toolset::BestAvailable;
4  
5 use strict;
6 use XML::Toolset;
7 use App;
8 use vars qw($VERSION @ISA);
9  
10 $VERSION = sprintf"%d.%03d", q$Revision: 1.25 $ =~ /: (\d+)\.(\d+)/;
11 @ISA = ("XML::Toolset");
12  
13 # Xerces is preferred over MSXML as we've found in practice that MSXML4's schema validation occasionally
14 # lets through invalid documents which Xerces catches.
15 # At the time of writing, LibXML didn't have schema validation support.
16 my $DEFAULT_PRIORITISED_LIST = [ qw( Xerces MSXML LibXML XMLDOM ) ];
17  
18 sub _init {
19 &App::sub_entry if ($App::trace);
20 my ($self, $options) = @_;
21
22 if (!$options->{bestavailable_initialized}) { # protect against infinite recursion
23 my $list = $options->{prioritized_list} || $DEFAULT_PRIORITISED_LIST;
24 my $type = $self->_best_available($list) or die sprintf("None of the listed backends (%s) are available\n",join(", ",@$list));
25 my $class = "XML::Toolset::" . $type;
26  
27 my %options = ( %$options ); # make a copy
28 $options{bestavailable_initialized} = 1;
29 bless $self, $class;
30 $self->_init(\%options); # in case the newly blessed class want to do initialization
31 }
32  
33 &App::sub_exit($self) if ($App::trace);
34 return $self;
35 }
36  
37 sub _best_available {
38 &App::sub_entry if ($App::trace);
39 my ($self, $list) = @_;
40 $list = $self->{prioritized_list} if (!$list);
41 $list = $DEFAULT_PRIORITISED_LIST if (!$list);
42 my $backend_found = "";
43 foreach my $backend (@{$list}) {
44 eval "use XML::Toolset::$backend";
45 if (!$@) {
46 $backend_found = $backend;
47 last;
48 }
49 }
50 &App::sub_exit($backend_found) if ($App::trace);
51 return($backend_found);
52 }
53  
54 1;
55  
56 __END__