Debian Codegraph @

tests/xmldiff.pl

raw
1 #!/usr/bin/env perl
2 use warnings;
3 #
4 # This is a modification of XML-SemanticDiff
5 # ------------------------------------------
6 #
7 # Semantically compare two XML files for equivalence. This is done while
8 # ignoring whitespace variations in the text as well as several
9 # permutations in the order of the tags.
10 #
11 # For original XML-SemanticDiff please see:
12 #
13 # Search CPAN
14 # http://search.cpan.org/dist/XML-SemanticDiff
15 #
16 # CPAN Request Tracker:
17 # http://rt.cpan.org/NoAuth/Bugs.html?Dist=XML-SemanticDiff
18 #
19 # AnnoCPAN, annotated CPAN documentation:
20 # http://annocpan.org/dist/XML-SemanticDiff
21 #
22 # CPAN Ratings:
23 # http://cpanratings.perl.org/d/XML-SemanticDiff
24 #
25 # COPYRIGHT AND LICENCE
26 #
27 # Copyright (c) 2000 Kip Hampton. All rights reserved. This program is
28 # free software; you can redistribute it and/or modify it under the same terms
29 # as Perl itself.
30 #
31 # openscap changes:
32 # 1) mpreisle - removed Digest::MD5 and Encode dependencies at the expense of performance
33
34 package SemanticDiff;
35
36 use strict;
37 use warnings;
38
39 use XML::Parser;
40
41 sub new {
42 my ($proto, %args) = @_;
43 my $class = ref($proto) || $proto;
44 my $self = \%args;
45
46 bless ($self, $class);
47 return $self;
48 }
49
50 sub _is_file
51 {
52 my ($self, $specifier) = @_;
53 return $specifier !~ /\n/g && -f $specifier;
54 }
55
56 sub _get_pathfinder_obj {
57 my $self = shift;
58
59 return SemanticDiff::PathFinder::Obj->new();
60 }
61
62 sub read_xml {
63 my $self = shift;
64
65 my ($xml_specifier) = @_;
66
67 if (ref($xml_specifier) eq 'HASH')
68 {
69 return $xml_specifier;
70 }
71 else
72 {
73 $self->{path_finder_obj} = $self->_get_pathfinder_obj();
74
75 my $p = XML::Parser->new(
76 Style => 'Stream',
77 Pkg => 'SemanticDiff::PathFinder',
78 'Non-Expat-Options' => $self,
79 Namespaces => 1
80 );
81
82 my $ret =
83 $self->_is_file($xml_specifier)
84 ? $p->parsefile($xml_specifier)
85 : $p->parse($xml_specifier)
86 ;
87
88 $self->{path_finder_obj} = undef;
89
90 return $ret;
91 }
92 }
93
94 sub _same_namespace
95 {
96 my ($self, $to, $from) = @_;
97
98 my $t_e = exists($to->{NamespaceURI});
99 my $f_e = exists($from->{NamespaceURI});
100 if (!$t_e && !$f_e)
101 {
102 return 1;
103 }
104 elsif ($t_e && $f_e)
105 {
106 return ($to->{NamespaceURI} eq $from->{NamespaceURI});
107 }
108 else
109 {
110 return 0;
111 }
112 }
113
114 sub _match_xpath {
115 my $self = shift;
116 my ($xpath, $flat_name) = @_;
117 my @x_way = split /\//, $xpath;
118 my @f_way = split /\//, $flat_name;
119 for my $i (0..$#x_way) {
120 $x_way[$i]=~s/.*?://g;
121 }
122 for my $i (0..$#f_way) {
123 $f_way[$i]=~s/\[.*?\]$//g;
124 }
125 return 0 if $#x_way > $#f_way;
126 for my $i (0..$#x_way) {
127 if ($x_way[$i] ne $f_way[$i]) {
128 return 0;
129 }
130 }
131 return 1;
132 }
133
134 sub compare {
135 my $self = shift;
136 my ($from_xml, $to_xml) = @_;
137
138 my $from_doc = $self->read_xml($from_xml);
139 my $to_doc = $self->read_xml($to_xml);
140
141 my @warnings = ();
142
143 my $handler = $self->{diffhandler} || SemanticDiff::BasicHandler->new(%$self);
144
145 if (defined $self->{ignorexpath}) {
146 my $ignore = $self->{ignorexpath};
147 for my $path (@$ignore) {
148 for my $ref ($from_doc, $to_doc) {
149 for my $key (keys %$ref) {
150 if ($self->_match_xpath($path, $key)) {
151 delete $ref->{$key};
152 }
153 }
154 }
155 }
156 }
157
158 push (@warnings, $handler->init($self)) if $handler->can('init');
159
160 foreach my $element (sort keys (%$from_doc)) {
161
162 if (defined $to_doc->{$element}) {
163
164 unless ($from_doc->{$element}->{TextChecksum} eq $to_doc->{$element}->{TextChecksum}) {
165 push (@warnings, $handler->element_value($element,
166 $to_doc->{$element},
167 $from_doc->{$element}))
168 if $handler->can('element_value');
169 }
170
171 unless ($self->_same_namespace($from_doc->{$element},$to_doc->{$element})) {
172 push (@warnings, $handler->namespace_uri($element,
173 $to_doc->{$element},
174 $from_doc->{$element}))
175 if $handler->can('namespace_uri');
176 }
177
178 foreach my $attr (keys(%{$from_doc->{$element}->{Attributes}})) {
179
180 if (defined ($to_doc->{$element}->{Attributes}->{$attr})) {
181
182 if ($to_doc->{$element}->{Attributes}->{$attr} ne $from_doc->{$element}->{Attributes}->{$attr}){
183 push (@warnings, $handler->attribute_value($attr,
184 $element,
185 $to_doc->{$element},
186 $from_doc->{$element}))
187 if $handler->can('attribute_value');
188 }
189 delete $to_doc->{$element}->{Attributes}->{$attr};
190 }
191 else {
192 push (@warnings, $handler->missing_attribute($attr,
193 $element,
194 $to_doc->{$element},
195 $from_doc->{$element}))
196 if $handler->can('missing_attribute');
197 }
198 }
199
200 foreach my $leftover (keys(%{$to_doc->{$element}->{Attributes}})) {
201 push (@warnings, $handler->rogue_attribute($leftover,
202 $element,
203 $to_doc->{$element},
204 $from_doc->{$element}))
205 if $handler->can('rogue_attribute');
206 }
207
208 delete $to_doc->{$element};
209 }
210 else {
211 push (@warnings, $handler->missing_element($element, $from_doc->{$element}))
212 if $handler->can('missing_element');
213 }
214 }
215
216 foreach my $leftover ( keys (%$to_doc) ) {
217 push (@warnings, $handler->rogue_element($leftover, $to_doc->{$leftover}))
218 if $handler->can('rogue_element');
219 }
220
221 push (@warnings, $handler->final($self)) if $handler->can('final');
222
223 return @warnings;
224 }
225
226 package SemanticDiff::BasicHandler;
227
228 use strict;
229 use warnings;
230
231 sub new {
232 my ($proto, %args) = @_;
233 my $class = ref($proto) || $proto;
234 my $self = \%args;
235 bless ($self, $class);
236 return $self;
237 }
238
239 sub rogue_element {
240 my $self = shift;
241 my ($element, $properties) = @_;
242 my ($element_name, $parent) = parent_and_name($element);
243 my $info = {context => $parent,
244 message => "Rogue element '$element_name' in element '$parent'."};
245
246 if ($self->{keeplinenums}) {
247 $info->{startline} = $properties->{TagStart};
248 $info->{endline} = $properties->{TagEnd};
249 }
250
251 if ($self->{keepdata}) {
252 $info->{new_value} = $properties->{CData};
253 }
254 return $info;
255 }
256
257 sub missing_element {
258 my $self = shift;
259 my ($element, $properties) = @_;
260 my ($element_name, $parent) = parent_and_name($element);
261 my $info = {context => $parent,
262 message => "Child element '$element_name' missing from element '$parent'."};
263
264 if ($self->{keeplinenums}) {
265 $info->{startline} = $properties->{TagStart};
266 $info->{endline} = $properties->{TagEnd};
267 }
268 if ($self->{keepdata}) {
269 $info->{old_value} = $properties->{CData};
270 }
271 return $info;
272 }
273
274 sub element_value {
275 my $self = shift;
276 my ($element, $new_properties, $old_properties) = @_;
277 my ($element_name, $parent) = parent_and_name($element);
278
279 my $info = {context => $element,
280 message => "Character differences in element '$element_name'."};
281
282 if ($self->{keeplinenums}) {
283 $info->{startline} = $new_properties->{TagStart};
284 $info->{endline} = $new_properties->{TagEnd};
285 }
286
287 if ($self->{keepdata}) {
288 $info->{old_value} = $old_properties->{CData};
289 $info->{new_value} = $new_properties->{CData};
290 }
291
292 return $info;
293 }
294
295 sub rogue_attribute {
296 my $self = shift;
297 my ($attr, $element, $properties) = @_;
298 my ($element_name, $parent) = parent_and_name($element);
299 my $info = {context => $element,
300 message => "Rogue attribute '$attr' in element '$element_name'."};
301
302 if ($self->{keeplinenums}) {
303 $info->{startline} = $properties->{TagStart};
304 $info->{endline} = $properties->{TagEnd};
305 }
306
307 if ($self->{keepdata}) {
308 $info->{new_value} = $properties->{Attributes}->{$attr};
309 }
310 return $info;
311 }
312
313 sub missing_attribute {
314 my $self = shift;
315 my ($attr, $element, $new_properties, $old_properties) = @_;
316 my ($element_name, $parent) = parent_and_name($element);
317 my $info = {context => $element,
318 message => "Attribute '$attr' missing from element '$element_name'."};
319
320 if ($self->{keeplinenums}) {
321 $info->{startline} = $new_properties->{TagStart};
322 $info->{endline} = $new_properties->{TagEnd};
323 }
324
325 if ($self->{keepdata}) {
326 $info->{old_value} = $old_properties->{Attributes}->{$attr};
327 }
328 return $info;
329 }
330
331 sub attribute_value {
332 my $self = shift;
333 my ($attr, $element, $new_properties, $old_properties) = @_;
334 my ($element_name, $parent) = parent_and_name($element);
335 my $info = {context => $element,
336 message => "Attribute '$attr' has different value in element '$element_name'."};
337
338 if ($self->{keeplinenums}) {
339 $info->{startline} = $new_properties->{TagStart};
340 $info->{endline} = $new_properties->{TagEnd};
341 }
342
343 if ($self->{keepdata}) {
344 $info->{old_value} = $old_properties->{Attributes}->{$attr};
345 $info->{new_value} = $new_properties->{Attributes}->{$attr};
346 }
347 return $info;
348 }
349
350 sub namespace_uri {
351 my $self = shift;
352 my ($element, $new_properties, $old_properties) = @_;
353 my ($element_name, $parent) = parent_and_name($element);
354 my $info = {context => $element,
355 message => "Element '$element_name' within different namespace."};
356
357 if ($self->{keeplinenums}) {
358 $info->{startline} = $new_properties->{TagStart};
359 $info->{endline} = $new_properties->{TagEnd};
360 }
361
362 if ($self->{keepdata}) {
363 $info->{old_value} = $old_properties->{NamspaceURI};
364 $info->{new_value} = $new_properties->{NamspaceURI};
365 }
366 return $info;
367 }
368
369 sub parent_and_name {
370 my $element = shift;
371 my @steps = split('/', $element);
372 my $element_name = pop (@steps);
373 my $parent = join '/', @steps;
374 $element_name =~ s/\[\d+\]$//;
375 return ($element_name, $parent);
376 }
377
378 package SemanticDiff::PathFinder;
379
380 foreach my $func (qw(StartTag EndTag Text StartDocument EndDocument PI))
381 {
382 no strict 'refs';
383 *{__PACKAGE__.'::'.$func} = sub {
384 my $expat = shift;
385 return $expat->{'Non-Expat-Options'}->{path_finder_obj}->$func(
386 $expat, @_
387 );
388 };
389 }
390
391 package SemanticDiff::PathFinder::Obj;
392
393 use strict;
394
395 foreach my $accessor (qw(descendents char_accumulator doc
396 opts xml_context PI_position_index))
397 {
398 no strict 'refs';
399 *{__PACKAGE__.'::'.$accessor} = sub {
400 my $self = shift;
401
402 if (@_)
403 {
404 $self->{$accessor} = shift;
405 }
406 return $self->{$accessor};
407 };
408 }
409
410 sub new {
411 my $class = shift;
412
413 my $self = {};
414 bless $self, $class;
415
416 $self->_init(@_);
417
418 return $self;
419 }
420
421 sub _init {
422 return 0;
423 }
424
425 sub StartTag {
426 my ($self, $expat, $element) = @_;
427
428
429 my %attrs = %_;
430
431 my @context = $expat->context;
432 my $context_length = scalar (@context);
433 my $parent = $context[$context_length -1];
434 push (@{$self->descendents()->{$parent}}, $element) if $parent;
435
436 my $last_ctx_elem = $self->xml_context()->[-1] || { position_index => {}};
437
438 push @{$self->xml_context()},
439 {
440 element => "$element",
441 'index' => ++$last_ctx_elem->{position_index}->{"$element"},
442 position_index => {},
443 };
444
445 my $test_context;
446
447 $test_context = $self->_calc_test_context();
448
449 $self->doc()->{$test_context} =
450 {
451 NamespaceURI => ($expat->namespace($element) || ""),
452 Attributes => \%attrs,
453 ($self->opts()->{keeplinenums}
454 ? ( TagStart => $expat->current_line)
455 : ()
456 ),
457 };
458 }
459
460 sub _calc_test_context
461 {
462 my $self = shift;
463
464 return
465 join("",
466 map { "/". $_->{'element'} . "[" . $_->{'index'} . "]" }
467 @{$self->xml_context()}
468 );
469 }
470
471 sub EndTag {
472 my ($self, $expat, $element) = @_;
473
474 my @context = $expat->context;
475
476 my $test_context = $self->_calc_test_context();
477
478 my $text;
479 if ( defined( $self->char_accumulator()->{$element} )) {
480 $text = $self->char_accumulator()->{$element};
481 delete $self->char_accumulator()->{$element};
482 }
483 $text ||= 'o';
484
485 # The following used to set TextChecksum to md5_base64 of utf8 encoded $text
486 # for performance reasons. Since we don't care about performance so much
487 # and want to avoid dependencies we just compare text character by character.
488 $self->doc()->{"$test_context"}->{TextChecksum} = $text;
489 if ($self->opts()->{keepdata}) {
490 $self->doc()->{"$test_context"}->{CData} = $text;
491 }
492
493
494 if (defined ( $self->descendents()->{$element})) {
495 my $seen = {};
496 foreach my $child (@{$self->descendents()->{$element}}) {
497 next if $seen->{$child};
498 $seen->{$child}++;
499 }
500 }
501
502 $self->doc()->{"$test_context"}->{TagEnd} = $expat->current_line if $self->opts()->{keeplinenums};
503
504 pop(@{$self->xml_context()});
505 }
506
507 sub Text {
508 my $self = shift;
509 my $expat = shift;
510
511 my $element = $expat->current_element;
512 my $char = $_;
513
514 $char =~ s/^\s*//;
515 $char =~ s/\s*$//;
516 $char =~ s/\s+/ /g;
517 $self->char_accumulator()->{$element} .= $char if $char;
518
519 }
520
521 sub StartDocument {
522 my $self = shift;
523 my $expat = shift;
524 $self->doc({});
525 $self->descendents({});
526 $self->char_accumulator({});
527 $self->opts($expat->{'Non-Expat-Options'});
528 $self->xml_context([]);
529 $self->PI_position_index({});
530 }
531
532 sub EndDocument {
533 my $self = shift;
534
535 return $self->doc();
536 }
537
538
539 sub PI {
540 my ($self, $expat, $target, $data) = @_;
541 my $attrs = {};
542 $self->PI_position_index()->{$target}++;
543
544 foreach my $pair (split /\s+/, $data) {
545 $attrs->{$1} = $2 if $pair =~ /^(.+?)=["'](.+?)["']$/;
546 }
547
548 my $slug = '?' . $target . '[' . $self->PI_position_index()->{$target} . ']';
549
550 $self->doc()->{$slug} =
551 {
552 Attributes => ($attrs || {}),
553 TextChecksum => "1",
554 NamespaceURI => "",
555 ( $self->opts()->{keeplinenums}
556 ? (
557 TagStart => $expat->current_line(),
558 TagEnd => $expat->current_line(),
559 )
560 : ()
561 ),
562 };
563 }
564
565 1;
566
567 sub usage {
568 die "usage: $0 one.xml two.xml \n";
569 }
570
571 use strict;
572
573 my $diff = SemanticDiff->new(keeplinenums => 1);
574
575 my ($file1, $file2) = @ARGV;
576 usage() unless defined $file1 and defined $file2;
577
578 my $ret_val = 2;
579
580 eval {
581 my @diffs = $diff->compare($file1, $file2);
582
583 foreach my $change (@diffs) {
584 print STDERR "$change->{message} ($change->{startline} <> $change->{endline})\n";
585 }
586
587 $ret_val = (scalar @diffs) == 0 ? 0 : 1;
588
589 }; warn $@ if $@;
590
591 exit $ret_val;