Source files of fsfe.org, pdfreaders.org, freeyourandroid.org, ilovefs.org, drm.info, and test.fsfe.org. Contribute: https://fsfe.org/contribute/web/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

comptree.pl 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #! /usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use constant false => 0;
  5. use constant true => 1;
  6. use XML::LibXML;
  7. #print areEqual("./thankgnus.en.xhtml", "./thankgnus.fr.xhtml")."\n";
  8. sub areEqual {
  9. my ($file1, $file2) = @_;
  10. #my $dom = XML::LibXML->load_xml(location => $file1); # the load_xml() function is not part of libxml 1.66 which is used on the server
  11. #my $don = XML::LibXML->load_xml(location => $file2);
  12. my $parser = XML::LibXML->new();
  13. my $dom = $parser->parse_file($file1);
  14. my $don = $parser->parse_file($file2);
  15. my $root = $dom->documentElement();
  16. my $roon = $don->documentElement();
  17. return nodesAreEqual($file1, $file2, $root, $roon, "/", '');
  18. }
  19. sub nodesAreEqual {
  20. my ($file1, $file2, $node1, $node2, $uptree, $nodenb) = @_;
  21. # test that two nodes at the same position have the same name
  22. if (not $node1->nodeName eq $node2->nodeName) {
  23. # short excerpts of these nodes' contents, to give a hint where the error is
  24. my $hint1 = $node1->textContent;
  25. $hint1 =~ s/\n//g; $hint1 =~ s/ //g; $hint1 =~ s/\t//g;
  26. $hint1 = substr($hint1,0,40)."...";
  27. my $hint2 = $node2->textContent;
  28. $hint2 =~ s/\n//g; $hint2 =~ s/ //g; $hint2 =~ s/\t//g;
  29. $hint2 = substr($hint2,0,40)."...";
  30. return (false, "$file2:\n
  31. en contains $uptree".$node1->nodeName."\t(".$hint1."), but\n".
  32. "** contains $uptree".$node2->nodeName."\t(".$hint2.")");
  33. }
  34. $uptree .= $node1->nodeName.$nodenb."/";
  35. # get all children of each node
  36. my @nodes = $node1->getChildrenByTagName('*');
  37. my @nodez = $node2->getChildrenByTagName('*');
  38. #print "\t".$node1->nodeName."(".scalar(@nodes).")", "\t", $node2->nodeName."(".scalar(@nodez).")", "\n";
  39. # remove <translator> and <timestamp> from the nodesz
  40. for (my $i=scalar(@nodes)-1; $i>=0; $i--) {
  41. if ( $nodes[$i]->nodeName eq "translator" or $nodes[$i]->nodeName eq "timestamp" ) {
  42. splice(@nodes,$i,1);
  43. }
  44. }
  45. for (my $i=scalar(@nodez)-1; $i>=0; $i--) {
  46. if ( $nodez[$i]->nodeName eq "translator" or $nodez[$i]->nodeName eq "timestamp" ) {
  47. splice(@nodez,$i,1);
  48. }
  49. }
  50. # test the size of the two lists of nodes
  51. if ( scalar(@nodes) != scalar(@nodez) ) {
  52. foreach (@nodes) { $_ = $_->nodeName };
  53. foreach (@nodez) { $_ = $_->nodeName };
  54. return (false, "$file1: ".join(",", @nodes)."\t\t<-children differences under $uptree\n"."$file2: ".join(",", @nodez) );
  55. }
  56. # comparing the two lists of nodes
  57. my $commons = "";
  58. for my $i (0 .. $#nodes) {
  59. if (not $nodes[$i]->nodeName eq $nodez[$i]->nodeName) {
  60. return (false, "$file2: after common $commons, " . $nodes[$i]->nodeName . " != " . $nodez[$i]->nodeName );
  61. }
  62. $commons .= $nodes[$i]->nodeName.",";
  63. }
  64. # recur!
  65. for my $i (0 .. $#nodes) {
  66. my ($bool, $err) = nodesAreEqual($file1, $file2, $nodes[$i], $nodez[$i], $uptree, $i);
  67. if (not $bool) {
  68. return ($bool, $err);
  69. }
  70. }
  71. # if we get here, it means we did not
  72. return (true, "trees are equal");
  73. }
  74. return 1;