blib/lib/XML/Validate/LibXML.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« blib/lib/XML/Validate/MSXML.pm ↑ Summary blib/lib/XML/Validate/Xerces.pm »
1 # XML::Validate::LibXML
2  
3 package XML::Validate::LibXML;
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use XML::Validate::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use Carp;
9  
10 use vars qw($VERSION @ISA);
11 $VERSION = sprintf"%d.%03d", q$Revision: 1.20 $ =~ /: (\d+)\.(\d+)/;
12 @ISA = qw( XML::Validate::Base );
13  
14 my $VALID_OPTIONS = {
15 strict_validation => 1,
16 base_uri => '',
17 };
18  
19 sub new {
20 my $class = shift;
21 my %options = @_;
22 my $self = {
23 };
24 bless ($self, $class);
25
26 $self->clear_errors();
27 $self->set_options(\%options,$VALID_OPTIONS);
28
29 DUMP("Instantiating XML::Validate::LibXML", $self);
30
31 return $self;
32 }
33  
34 sub version {
35 return XML::LibXML->VERSION;
36 }
37  
38 sub validate {
39 my $self = shift;
40 my ($xml) = @_;
41 TRACE("Validating with LibXML. XML => " . defined($xml) ? $xml : 'undef' );
42
43 $self->clear_errors();
44 $self->{dom} = undef;
45  
46 die "validate called with no data to validate\n" unless defined $xml and length $xml > 0;
47  
48 my $parser = XML::LibXML->new();
49 $parser->line_numbers(1) if $parser->can('line_numbers'); # (XML::LibXML > 1.56)
50 $parser->load_ext_dtd(1);
51 $parser->expand_entities(1);
52
53 my $doc = eval { $parser->parse_string($xml) };
54  
55 if ($@) {
56 TRACE("We have a parsing error: $@");
57 $self->add_error({message => $@});
58 return;
59 }
60
61 TRACE("We have a doctype") if $doc->internalSubset;
62
63 if ($self->options->{strict_validation} && $doc->internalSubset) {
64 TRACE("Parsing with strict validation");
65 $parser->validation(1);
66 DUMP("Base uri",$self->options->{base_uri});
67 $doc = eval { $parser->parse_string($xml,$self->options->{base_uri}) };
68 if ($@) {
69 TRACE("We have a validation error: $@");
70 $self->add_error({message => $@});
71 return;
72 }
73 }
74
75 $self->{dom} = $doc;
76 return 1;
77 }
78  
79 sub last_dom {
80 my $self = shift;
81 return $self->{dom};
82 }
83  
84 # Note: Our use of TRACE and DUMP here is a bit weird. We explicitly pass to
85 # the TRACE and DUMP in the superclass (XML::Validate::Base) because we expect
86 # to be dynamically loaded and we assume that the calling class will have dealt
87 # with Base but not this module. (Note that Log::Trace now has some support for
88 # dynamic loading. It doesn't play well with some modules in 5.6.1, but it seems
89 # fine in 5.8. So someday this won't be necessary.)
90  
91 sub TRACE { XML::Validate::Base::TRACE(@_) }
92 sub DUMP { XML::Validate::Base::DUMP(@_) }
93  
94 1;
95  
96 __END__
« blib/lib/XML/Validate/MSXML.pm ↑ Summary blib/lib/XML/Validate/Xerces.pm »