| 1 | # Factory class providing a common interface to different XML validators | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | package XML::Validate; | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | # Using XML::Validate::Base here is cheating somewhat. We do this because we're | ||||||||||||||||||||
| 6 | # dynamically loading the validation module, but we still want to be able to | ||||||||||||||||||||
| 7 | # use Log::Trace's deep import feature. Any better ideas welcomed. | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 2 | ▶ | use XML::Validate::Base; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 2 | ▶ | use vars qw($VERSION); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 1 | $VERSION = sprintf"%d.%03d", q$Revision: 1.25 $ =~ /: (\d+)\.(\d+)/; | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | # Xerces is preferred over MSXML as we've found in practice that MSXML4's schema validation occasionally | ||||||||||||||||||||
| 16 | # lets through invalid documents which Xerces catches. | ||||||||||||||||||||
| 17 | # At the time of writing, LibXML didn't have schema validation support. | ||||||||||||||||||||
| 18 | 1 | my $DEFAULT_PRIORITISED_LIST = [ qw( Xerces MSXML LibXML ) ]; | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 2 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=25.4
Pod
documented
| |||||||||||||||||||||
| 21 | 2 | my $class = shift; | |||||||||||||||||||
| 22 | 2 | my %args = @_; | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 2 | DUMP("Arguments", %args); | |||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 2 | my $self = { | |||||||||||||||||||
| 27 | backend => undef, | ||||||||||||||||||||
| 28 | }; | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 2 | bless($self,$class); | |||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 2 | ▶ | if (!$args{Type}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 33 | 0 | die "Required argument Type missing\n"; | |||||||||||||||||||
| 34 | } | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 2 | ▶ | if ($args{Type} !~ /^\w+$/) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 37 | 1 | die "Validator type name '$args{Type}' should only contain word characters.\n"; | |||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 1 | my $backend_class; | |||||||||||||||||||
| 41 | 1 | ▶ | if ($args{Type} eq 'BestAvailable') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | 0 | ▶ | my $list = $args{PrioritisedList} || $DEFAULT_PRIORITISED_LIST; | ||||||||||||||||||
|
Condition0%0/3
$args{'PrioritisedList'} || $DEFAULT_PRIORITISED_LIST
MC/DC0%0/2
$args{'PrioritisedList'} || $DEFAULT_PRIORITISED_LIST
$args{'PrioritisedList'}
$DEFAULT_PRIORITISED_LIST
Truth table0%0/3
A || B
A= $args{'PrioritisedList'}B= $DEFAULT_PRIORITISED_LIST
| |||||||||||||||||||||
| 43 | 0 | ▶ | my $type = $self->_best_available($list) or die sprintf("None of the listed backends (%s) are available\n",join(", ",@$list)); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 44 | 0 | $backend_class = "XML::Validate::" . $type; | |||||||||||||||||||
| 45 | } else { | ||||||||||||||||||||
| 46 | 1 | $backend_class = "XML::Validate::" . $args{Type}; | |||||||||||||||||||
| 47 | } | ||||||||||||||||||||
| 48 | 0 | ▶ | eval "use $backend_class"; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 49 | 1 | ▶ | die "Validator $backend_class not loadable: $@" if $@; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 0 | my $options = $args{Options}; | |||||||||||||||||||
| 52 | 0 | $self->{backend} = new $backend_class(%{$options}); | |||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 0 | return $self; | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 0 | ▶ | sub validate { | ||||||||||||||||||
|
Subroutine
validate: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 58 | 0 | my $self = shift; | |||||||||||||||||||
| 59 | 0 | $self->{backend}->validate(@_); | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 0 | ▶ | sub last_error { | ||||||||||||||||||
|
Subroutine
last_error: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 63 | 0 | my $self = shift; | |||||||||||||||||||
| 64 | 0 | $self->{backend}->last_error(@_); | |||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 0 | ▶ | sub type { | ||||||||||||||||||
|
Subroutine
type: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 68 | 0 | my $self = shift; | |||||||||||||||||||
| 69 | 0 | my $class = ref($self->{backend}); | |||||||||||||||||||
| 70 | 0 | $class =~ m/XML::Validate::(.*)/; | |||||||||||||||||||
| 71 | 0 | return $1; | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 0 | ▶ | sub version { | ||||||||||||||||||
|
Subroutine
version: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 75 | 0 | my $self = shift; | |||||||||||||||||||
| 76 | 0 | ▶ | return unless $self->{backend}->can('version'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | 0 | return $self->{backend}->version; | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 0 | ▶ | sub _best_available { | ||||||||||||||||||
|
Subroutine
_best_available: not called CC=3 SCAR=19.2
| |||||||||||||||||||||
| 81 | 0 | my $self = shift; | |||||||||||||||||||
| 82 | 0 | my ($list) = @_; | |||||||||||||||||||
| 83 | 0 | foreach my $backend (@{$list}) { | |||||||||||||||||||
| 84 | 0 | TRACE("Attempting to load $backend"); | |||||||||||||||||||
| 85 | 0 | eval "use XML::Validate::$backend"; | |||||||||||||||||||
| 86 | 0 | ▶ | next if $@; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 87 | 1 | TRACE("Loading succeeded. Returning $backend"); | |||||||||||||||||||
| 88 | 1 | return $backend; | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | 1 | return; | |||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 0 | ▶ | sub TRACE {} | ||||||||||||||||||
|
Subroutine
TRACE: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 94 | 2 | ▶ | sub DUMP {} | ||||||||||||||||||
|
Subroutine
DUMP: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 1 | 1; | |||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | __END__ | ||||||||||||||||||||