1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
#
# Used by test scripts to compare 2 DOM subtrees.
#
# Usage:
#
# my $cmp = new CmpDOM;
# $node1->equals ($node2, $cmp) or
# print "Difference found! Context:" . $cmp->context . "\n";
#
package CmpDOM;
use XML::DOM;
use Carp;
sub new
{
my %args = (SkipReadOnly => 0, Context => []);
bless \%args, $_[0];
}
sub pushContext
{
my ($self, $str) = @_;
push @{$self->{Context}}, $str;
#print ":: " . $self->context . "\n";
}
sub popContext
{
pop @{$_[0]->{Context}};
}
sub skipReadOnly
{
my $self = shift;
my $prev = $self->{SkipReadOnly};
if (@_ > 0)
{
$self->{SkipReadOnly} = shift;
}
$prev;
}
sub sameType
{
shift if @_ > 2;
my ($x, $y) = @_;
return 1 if (ref ($x) eq ref ($y));
$self->fail ("wrong type " . ref($x) . " != " . ref($y));
}
sub sameReadOnly
{
my ($self, $x, $y) = @_;
return 1 if $self->{SkipReadOnly};
my $result = 1;
if (not defined $x)
{
$result = 0 if defined $y;
}
else
{
if (not defined $y)
{
$result = 0;
}
elsif ($x != $y)
{
$result = 0;
}
}
return 1 if ($result == 1);
$self->fail ("ReadOnly $x != $y");
}
sub fail
{
my ($self, $str) = @_;
$self->pushContext ($str);
0;
}
sub context
{
my $self = shift;
join (", ", @{$self->{Context}});
}
package XML::DOM::NamedNodeMap;
sub equals
{
my ($self, $other, $cmp) = @_;
return 0 unless $cmp->sameType ($self, $other);
# sanity checks
my $n1 = int (keys %$self);
my $n2 = int (keys %$other);
return $cmp->fail("same keys length") unless $n1 == $n2;
return $cmp->fail("#1 value length") unless ($n1-1 == $self->getLength);
return $cmp->fail("#2 value length") unless ($n2-1 == $other->getLength);
my $i = 0;
my $ov = $other->getValues;
for my $n (@{$self->getValues})
{
$cmp->pushContext ($n->getNodeName);
return 0 unless $n->equals ($ov->[$i], $cmp);
$i++;
$cmp->popContext;
}
return 0 unless $cmp->sameReadOnly ($self->isReadOnly,
$other->isReadOnly);
1;
}
package XML::DOM::NodeList;
sub equals
{
my ($self, $other, $cmp) = @_;
return 0 unless $cmp->sameType ($self, $other);
return $cmp->fail("wrong length")
unless $self->getLength == $other->getLength;
my $i = 0;
for my $n (@$self)
{
$cmp->pushContext ("[$i]");
return 0 unless $n->equals ($other->[$i], $cmp);
$i++;
$cmp->popContext;
}
1;
}
package XML::DOM::Node;
sub equals
{
my ($self, $other, $cmp) = @_;
return 0 unless $cmp->sameType ($self, $other);
my $hasKids = $self->hasChildNodes;
return $cmp->fail("hasChildNodes") unless $hasKids == $other->hasChildNodes;
if ($hasKids)
{
$cmp->pushContext ("C");
return 0 unless $self->{C}->equals ($other->{C}, $cmp);
$cmp->popContext;
}
return 0 unless $cmp->sameReadOnly ($self->isReadOnly,
$other->isReadOnly);
for my $prop (@{$self->getCmpProps})
{
$cmp->pushContext ($prop);
my $p1 = $self->{$prop};
my $p2 = $other->{$prop};
if (ref ($p1))
{
return 0 unless $p1->equals ($p2, $cmp);
}
elsif (! defined ($p1))
{
return 0 if defined $p2;
}
else
{
return $cmp->fail("$p1 != $p2") unless $p1 eq $p2;
}
$cmp->popContext;
}
1;
}
sub getCmpProps
{
return [];
}
package XML::DOM::Attr;
sub getCmpProps
{
[Name, Specified];
}
package XML::DOM::ProcessingInstruction;
sub getCmpProps
{
[Target, Data];
}
package XML::DOM::Notation;
sub getCmpProps
{
return [Name, Base, SysId, PubId];
}
package XML::DOM::Entity;
sub getCmpProps
{
return [NotationName, Parameter, Value, SysId, PubId];
}
package XML::DOM::EntityReference;
sub getCmpProps
{
return [EntityName, Parameter];
}
package XML::DOM::AttDef;
sub getCmpProps
{
return [Name, Type, Required, Implied, Quote, Default, Fixed];
}
package XML::DOM::AttlistDecl;
sub getCmpProps
{
return [Name, A];
}
package XML::DOM::ElementDecl;
sub getCmpProps
{
return [Name, Model];
}
package XML::DOM::Element;
sub getCmpProps
{
return [TagName, A];
}
package XML::DOM::CharacterData;
sub getCmpProps
{
return [Data];
}
package XML::DOM::XMLDecl;
sub getCmpProps
{
return [Version, Encoding, Standalone];
}
package XML::DOM::DocumentType;
sub getCmpProps
{
return [Entities, Notations, Name, SysId, PubId, Internal];
}
package XML::DOM::Document;
sub getCmpProps
{
return [XmlDecl, Doctype];
}
1;
|