blib/lib/XML/Validate/MSXML.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« blib/script/validxml.pl ↑ Summary blib/lib/XML/Validate/LibXML.pm »
1 # XML::Validate::MSXML
2  
3 package XML::Validate::MSXML;
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 0 ▶use Win32::OLE;
Subroutine BEGIN: called CC=1 SCAR=2.6
7 use XML::Validate::Base;
8 use vars qw( $VERSION @ISA $MSXML_VERSION);
9  
10 @ISA = qw( XML::Validate::Base );
11 $VERSION = sprintf'%d.%03d', q$Revision: 1.18 $ =~ /: (\d+)\.(\d+)/;
12  
13 use constant XSI_NS => 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"';
14  
15 my $VALID_OPTIONS = { strict_validation => 1 };
16  
17 sub new {
18 my $class = shift;
19 my %options = @_;
20 my $self = {};
21 bless ($self, $class);
22
23 $self->clear_errors();
24 $self->set_options(\%options,$VALID_OPTIONS);
25
26 DUMP("Instantiating XML::Validate::MSXML", $self);
27
28 return $self;
29 }
30  
31 sub version {
32 eval {create_doc_and_cache()};
33 return $MSXML_VERSION;
34 }
35  
36 sub validate {
37 my ($self, $xml) = @_;
38
39 $self->clear_errors();
40 $self->{dom} = undef;
41
42 die "validate called with no data to validate\n" unless defined $xml and length $xml > 0;
43  
44 DUMP("the xml to validate : $xml ", $self);
45  
46 my ($msxml,$msxmlcache) = create_doc_and_cache();
47  
48 $msxml->{async} = 0;
49 $msxml->{validateOnParse} = 0;
50 $msxml->{resolveExternals} = 1;
51
52 TRACE("Starting to parse");
53 $msxml->LoadXML($xml);
54 TRACE("Parsed the document");
55
56 my $xmlroot = $msxml->{documentElement};
57
58 if ($msxml->parseError()->{errorCode} != 0) {
59 TRACE("XML Parse Error (not syntactically valid)");
60 my $error = $msxml->parseError();
61 DUMP("Error", $error);
62 $self->add_error({
63 message => $error->{reason},
64 line => $error->{line},
65 column => $error->{linepos}
66 });
67 return;
68 }
69
70 if ($self->options->{strict_validation}) {
71 TRACE("XML Syntactically valid");
72 load_schemas($msxml, $msxmlcache);
73 TRACE("Validate against schema/DTD ");
74 if ($msxml->{doctype} || $msxml->{schemas}) {
75 #DUMP($msxml->{doctype}, $msxml->{schemas});
76 $msxml->{validateOnParse} = 1;
77 $msxml->LoadXML($xml);
78 my $error = $msxml->parseError();
79 if ($error->{errorCode} != 0) {
80 $self->add_error({
81 message => $error->{reason},
82 line => $error->{line},
83 column => $error->{linepos}
84 });
85 return;
86 }
87 $error = $msxml->validate();
88 if ($error->{errorCode} != 0) {
89 $self->add_error({
90 message => $error->{reason},
91 line => $error->{line},
92 column => $error->{linepos}
93 });
94 return;
95 }
96 } else {
97 # If there is nothing to validate against, treat it as valid.
98 TRACE("No doctype or schema");
99 }
100 }
101  
102 #Valid
103 $self->{dom} = $msxml;
104 return 1;
105 }
106  
107 sub last_dom {
108 my $self = shift;
109 return $self->{dom};
110 }
111  
112 sub load_schemas {
113 my ($xml, $schema_cache) = @_;
114
115 my %schemas;
116
117 $xml->setProperty('SelectionNamespaces', XSI_NS);
118 my $no_ns_schema_xpath = q[//*/@xsi:noNamespaceSchemaLocation];
119 my $no_ns_schema_nodes = $xml->{documentElement}->selectNodes($no_ns_schema_xpath);
120
121 for (my $i=0; $i < $no_ns_schema_nodes->{length}; $i++){
122 my $schema_txt = $no_ns_schema_nodes->item($i)->{text};
123 my %add_schemas = ('', $schema_txt);
124 %schemas = (%schemas, %add_schemas);
125 }
126
127 my $schema_xpath = q[//*/@xsi:schemaLocation];
128 my $schema_location_nodes = $xml->{documentElement}->selectNodes($schema_xpath);
129  
130 for (my $i=0; $i < $schema_location_nodes->{length}; $i++){
131 my $schema_txt = $schema_location_nodes->item($i)->{text};
132 my %add_schemas = split ' ', $schema_txt;
133 %schemas = (%schemas, %add_schemas);
134 }
135
136 while (my ($ns, $schema) = each %schemas) {
137 TRACE("Loading schema [$ns] -> $schema");
138 $schema_cache->add($ns, $schema);
139 if (my $schema_error = Win32::OLE::LastError()) {
140 $schema_error =~ s/OLE exception .*\n\n//m;
141 $schema_error =~ s/Win32::OLE.*//s;
142 die $schema_error;
143 }
144 }
145 $xml->{schemas} = $schema_cache if %schemas;
146 return;
147 }
148  
149 sub dependencies_available {
150 create_doc_and_cache();
151 return 1;
152 }
153  
154 sub create_doc_and_cache {
155 # Stop Win32::OLE from being noisy
156 my $warn_level = Win32::OLE->Option('Warn');
157 Win32::OLE->Option(Warn => 0);
158  
159 foreach my $version ('5.0', '4.0') {
160 my $doc = Win32::OLE->new('MSXML2.DOMDocument.' . $version) or next;
161 my $cache = Win32::OLE->new('MSXML2.XMLSchemaCache.' . $version) or next;
162 $MSXML_VERSION = $version;
163 Win32::OLE->Option(Warn => $warn_level); # restore warn level
164 return ($doc,$cache);
165 }
166 die "Unable to instantiate MSXML DOMDocument and SchemaCache. (Do you have a compatible version of MSXML installed?)";
167 }
168  
169 # Note: Our use of TRACE and DUMP here is a bit weird. We explicitly pass to
170 # the TRACE and DUMP in the superclass (XML::Validate::Base) because we expect
171 # to be dynamically loaded and we assume that the calling class will have dealt
172 # with Base but not this module. (Note that Log::Trace now has some support for
173 # dynamic loading. It doesn't play well with some modules in 5.6.1, but it seems
174 # fine in 5.8. So someday this won't be necessary.)
175  
176 sub TRACE { XML::Validate::Base::TRACE(@_) }
177 sub DUMP { XML::Validate::Base::DUMP(@_) }
178  
179 dependencies_available();
180  
181 __END__
« blib/script/validxml.pl ↑ Summary blib/lib/XML/Validate/LibXML.pm »