| 1 | use warnings; | ||
| 2 | use strict; | ||
| 3 | # | ||
| 4 | # | ||
| 5 | # TO DO | ||
| 6 | # - update docs regarding PerlSAX interface | ||
| 7 | # - add current node to error context when checking DOM subtrees | ||
| 8 | # - add parsed Entity to test XML files | ||
| 9 | # - free circular references | ||
| 10 | # - Implied handler? | ||
| 11 | # - Notation, Entity, Unparsed checks, Default handler? | ||
| 12 | # - check no root element (it's checked by expat) ? | ||
| 13 | |||
| 14 | package XML::Checker::Term; | ||
| 15 | use strict; | ||
| 16 | |||
| 17 | sub new | ||
| 18 | { | ||
| 19 | my ($class, %h) = @_; | ||
| 20 | bless \%h, $class; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub str | ||
| 24 | { | ||
| 25 | '<' . $_[0]->{C} . $_[0]->{N} . '>' | ||
| 26 | } | ||
| 27 | |||
| 28 | sub re | ||
| 29 | { | ||
| 30 | $_[0]->{S} | ||
| 31 | } | ||
| 32 | |||
| 33 | sub rel | ||
| 34 | { | ||
| 35 | my $self = shift; | ||
| 36 | defined $self->{SL} ? @{ $self->{SL} } : ( $self->{S} ); | ||
| 37 | } | ||
| 38 | |||
| 39 | sub debug | ||
| 40 | { | ||
| 41 | my $t = shift; | ||
| 42 | my ($c, $n, $s) = ($t->{C}, $t->{N}, $t->{S}); | ||
| 43 | my @sl = $t->rel; | ||
| 44 | "{C=$c N=$n S=$s SL=@sl}"; | ||
| 45 | } | ||
| 46 | |||
| 47 | #------------------------------------------------------------------------- | ||
| 48 | |||
| 49 | package XML::Checker::Context; | ||
| 50 | |||
| 51 | sub new | ||
| 52 | { | ||
| 53 | my ($class) = @_; | ||
| 54 | my $scalar; | ||
| 55 | bless \$scalar, $class; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub Start {} | ||
| 59 | sub End {} | ||
| 60 | sub Char {} | ||
| 61 | |||
| 62 | # | ||
| 63 | # The initial Context when checking an entire XML Document | ||
| 64 | # | ||
| 65 | package XML::Checker::DocContext; | ||
| 66 | use vars qw( @ISA ); | ||
| 67 | @ISA = qw( XML::Checker::Context ); | ||
| 68 | |||
| 69 | sub new | ||
| 70 | { | ||
| 71 | #??checker not used | ||
| 72 | my ($class, $checker) = @_; | ||
| 73 | bless { }, $class; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub setRootElement | ||
| 77 | { | ||
| 78 | $_[0]->{RootElement} = $_[1]; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub Start | ||
| 82 | { | ||
| 83 | my ($self, $checker, $tag) = @_; | ||
| 84 | if (exists $self->{Elem}) | ||
| 85 | { | ||
| 86 | my $tags = join (", ", @{$self->{Elem}}); | ||
| 87 | $checker->fail (155, "more than one root Element [$tags]"); | ||
| 88 | push @{$self->{Elem}}, $tag; | ||
| 89 | } | ||
| 90 | else | ||
| 91 | { | ||
| 92 | $self->{Elem} = [ $tag ]; | ||
| 93 | } | ||
| 94 | |||
| 95 | my $exp_root = $self->{RootElement}; | ||
| 96 | $checker->fail (156, "unexpected root Element [$tag], expected [$exp_root]") | ||
| 97 | if defined ($exp_root) and $tag ne $exp_root; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub debug | ||
| 101 | { | ||
| 102 | my $self = shift; | ||
| 103 | "DocContext[Count=" . $self->{Count} . ",Root=" . | ||
| 104 | $self->{RootElement} . "]"; | ||
| 105 | } | ||
| 106 | |||
| 107 | package XML::Checker::Context::ANY; | ||
| 108 | use vars qw( @ISA ); | ||
| 109 | @ISA = qw( XML::Checker::Context ); | ||
| 110 | |||
| 111 | # No overrides, because everything is accepted | ||
| 112 | |||
| 113 | sub debug { "XML::Checker::Context::ANY" } | ||
| 114 | |||
| 115 | package XML::Checker::Context::EMPTY; | ||
| 116 | use vars qw( @ISA $ALLOW_WHITE_SPACE ); | ||
| 117 | @ISA = qw( XML::Checker::Context ); | ||
| 118 | |||
| 119 | $ALLOW_WHITE_SPACE = 0; | ||
| 120 | |||
| 121 | sub debug { "XML::Checker::Context::EMPTY" } | ||
| 122 | |||
| 123 | sub Start | ||
| 124 | { | ||
| 125 | my ($self, $checker, $tag) = @_; | ||
| 126 | $checker->fail (152, "Element should be EMPTY, found Element [$tag]"); | ||
| 127 | } | ||
| 128 | |||
| 129 | sub Char | ||
| 130 | { | ||
| 131 | my ($self, $checker, $str) = @_; | ||
| 132 | $checker->fail (153, "Element should be EMPTY, found text [$str]") | ||
| 133 | unless ($ALLOW_WHITE_SPACE and $checker->isWS ($str)); | ||
| 134 | |||
| 135 | # NOTE: if $ALLOW_WHITE_SPACE = 1, the isWS call does not only check | ||
| 136 | # whether it is whitespace, but it also informs the checker that this | ||
| 137 | # might be insignificant whitespace | ||
| 138 | } | ||
| 139 | |||
| 140 | #?? what about Comments | ||
| 141 | |||
| 142 | package XML::Checker::Context::Children; | ||
| 143 | use vars qw( @ISA ); | ||
| 144 | @ISA = qw( XML::Checker::Context ); | ||
| 145 | |||
| 146 | sub new | ||
| 147 | { | ||
| 148 | my ($class, $rule) = @_; | ||
| 149 | bless { Name => $rule->{Name}, RE => $rule->{RE}, Buf => "", N => 0 }, $class; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub phash | ||
| 153 | { | ||
| 154 | my $href = shift; | ||
| 155 | my $str = ""; | ||
| 156 | for (keys %$href) | ||
| 157 | { | ||
| 158 | $str .= ' ' if $str; | ||
| 159 | $str .= $_ . '=' . $href->{$_}; | ||
| 160 | } | ||
| 161 | $str; | ||
| 162 | } | ||
| 163 | |||
| 164 | sub debug | ||
| 165 | { | ||
| 166 | my $self = shift; | ||
| 167 | "Context::Children[Name=(" . phash ($self->{Name}) . ",N=" . $self->{N} . | ||
| 168 | ",RE=" . $self->{RE} . ",Buf=[" . $self->{Buf} . "]"; | ||
| 169 | } | ||
| 170 | |||
| 171 | sub Start | ||
| 172 | { | ||
| 173 | my ($self, $checker, $tag) = @_; | ||
| 174 | |||
| 175 | #print "Children.Start tag=$tag rule=$checker drule=" . $checker->debug . "\n"; | ||
| 176 | |||
| 177 | if (exists $self->{Name}->{$tag}) | ||
| 178 | { | ||
| 179 | #print "Buf=[".$self->{Buf}. "] tag=[" . $self->{Name}->{$tag}->{S} . "]\n"; | ||
| 180 | $self->{Buf} .= $self->{Name}->{$tag}->{S}; | ||
| 181 | } | ||
| 182 | else | ||
| 183 | { | ||
| 184 | $checker->fail (157, "unexpected Element [$tag]", | ||
| 185 | ChildElementIndex => $self->{N}) | ||
| 186 | } | ||
| 187 | $self->{N}++; | ||
| 188 | } | ||
| 189 | |||
| 190 | sub decode | ||
| 191 | { | ||
| 192 | my ($self) = @_; | ||
| 193 | my $re = $self->{RE}; | ||
| 194 | my $name = $self->{Name}; | ||
| 195 | my $buf = $self->{Buf}; | ||
| 196 | |||
| 197 | # length of token, in a content model all tokens are the same length | ||
| 198 | my $len = 0; | ||
| 199 | my %s = (); | ||
| 200 | while (my ($key, $val) = each %$name) | ||
| 201 | { | ||
| 202 | $len = length($val->{S}) unless $len; | ||
| 203 | $s{$val->{S}} = $key; | ||
| 204 | } | ||
| 205 | # ex. $key = C_31 and $name = 01 | ||
| 206 | #use warnings; | ||
| 207 | my $dots = "[^()*+?|]" x $len; | ||
| 208 | $buf =~ s/($dots)/$s{$1} . ","/ge; | ||
| 209 | chop $buf; | ||
| 210 | |||
| 211 | $re =~ s/($dots)/"(" . $s{$1} . ")"/ge; | ||
| 212 | |||
| 213 | "Found=[$buf] RE=[$re]" | ||
| 214 | } | ||
| 215 | |||
| 216 | sub End | ||
| 217 | { | ||
| 218 | my ($self, $checker) = @_; | ||
| 219 | my $re = $self->{RE}; | ||
| 220 | |||
| 221 | unless ( $self->{Buf} =~ /\S/ ) { | ||
| 222 | unless ( $self->{Buf} =~ /^$re$/ ) { | ||
| 223 | $checker->fail (170, "Element can't be empty " . $self->decode); | ||
| 224 | return; | ||
| 225 | } | ||
| 226 | } | ||
| 227 | unless ( $self->{Buf} =~ /^$re$/ ) { | ||
| 228 | $checker->fail (154, "bad order of Elements " . $self->decode); | ||
| 229 | } | ||
| 230 | } | ||
| 231 | |||
| 232 | sub Char | ||
| 233 | { | ||
| 234 | my ($self, $checker, $str) = @_; | ||
| 235 | $checker->fail (149, "Element should only contain sub elements, found text [$str]") | ||
| 236 | unless ($checker->isWS ($str)); | ||
| 237 | } | ||
| 238 | |||
| 239 | package XML::Checker::Context::Mixed; | ||
| 240 | use vars qw( @ISA ); | ||
| 241 | @ISA = qw( XML::Checker::Context ); | ||
| 242 | |||
| 243 | sub new | ||
| 244 | { | ||
| 245 | my ($class, $rule) = @_; | ||
| 246 | bless { Name => $rule->{Name}, N => 0 }, $class; | ||
| 247 | } | ||
| 248 | |||
| 249 | sub debug | ||
| 250 | { | ||
| 251 | my $self = shift; | ||
| 252 | "Context::Mixed[Name=" . $self->{Name} . ",N=" , $self->{N} . "]"; | ||
| 253 | } | ||
| 254 | |||
| 255 | sub Start | ||
| 256 | { | ||
| 257 | my ($self, $checker, $tag) = @_; | ||
| 258 | |||
| 259 | $checker->fail (157, "unexpected Element [$tag]", | ||
| 260 | ChildElementIndex => $self->{N}) | ||
| 261 | unless exists $self->{Name}->{$tag}; | ||
| 262 | $self->{N}++; | ||
| 263 | } | ||
| 264 | |||
| 265 | package XML::Checker::ERule; | ||
| 266 | |||
| 267 | package XML::Checker::ERule::EMPTY; | ||
| 268 | use vars qw( @ISA ); | ||
| 269 | @ISA = qw( XML::Checker::ERule ); | ||
| 270 | |||
| 271 | sub new | ||
| 272 | { | ||
| 273 | my ($class) = @_; | ||
| 274 | bless {}, $class; | ||
| 275 | } | ||
| 276 | |||
| 277 | my $context = new XML::Checker::Context::EMPTY; | ||
| 278 | sub context { $context } # share the context | ||
| 279 | |||
| 280 | sub debug { "EMPTY" } | ||
| 281 | |||
| 282 | package XML::Checker::ERule::ANY; | ||
| 283 | use vars qw( @ISA ); | ||
| 284 | @ISA = qw( XML::Checker::ERule ); | ||
| 285 | |||
| 286 | sub new | ||
| 287 | { | ||
| 288 | my ($class) = @_; | ||
| 289 | bless {}, $class; | ||
| 290 | } | ||
| 291 | |||
| 292 | my $any_context = new XML::Checker::Context::ANY; | ||
| 293 | sub context { $any_context } # share the context | ||
| 294 | |||
| 295 | sub debug { "ANY" } | ||
| 296 | |||
| 297 | package XML::Checker::ERule::Mixed; | ||
| 298 | use vars qw( @ISA ); | ||
| 299 | @ISA = qw( XML::Checker::ERule ); | ||
| 300 | |||
| 301 | sub new | ||
| 302 | { | ||
| 303 | my ($class) = @_; | ||
| 304 | bless { Name => {} }, $class; | ||
| 305 | } | ||
| 306 | |||
| 307 | sub context | ||
| 308 | { | ||
| 309 | my ($self) = @_; | ||
| 310 | new XML::Checker::Context::Mixed ($self); | ||
| 311 | } | ||
| 312 | |||
| 313 | sub setModel | ||
| 314 | { | ||
| 315 | my ($self, $model) = @_; | ||
| 316 | my $rule = $model; | ||
| 317 | |||
| 318 | # Mixed := '(' '#PCDATA' ')' '*'? | ||
| 319 | if ($rule =~ /^\(\s*#PCDATA\s*\)(\*)?$/) | ||
| 320 | { | ||
| 321 | #? how do we interpret the '*' ?? | ||
| 322 | return 1; | ||
| 323 | } | ||
| 324 | else # Mixed := '(' '#PCDATA' ('|' Name)* ')*' | ||
| 325 | { | ||
| 326 | return 0 unless $rule =~ s/^\(\s*#PCDATA\s*//; | ||
| 327 | return 0 unless $rule =~ s/\s*\)\*$//; | ||
| 328 | |||
| 329 | my %names = (); | ||
| 330 | while ($rule =~ s/^\s*\|\s*($XML::RegExp::Name)//) | ||
| 331 | { | ||
| 332 | $names{$1} = 1; | ||
| 333 | } | ||
| 334 | if ($rule eq "") | ||
| 335 | { | ||
| 336 | $self->{Name} = \%names; | ||
| 337 | return 1; | ||
| 338 | } | ||
| 339 | } | ||
| 340 | return 0; | ||
| 341 | } | ||
| 342 | |||
| 343 | sub debug | ||
| 344 | { | ||
| 345 | my ($self) = @_; | ||
| 346 | "Mixed[Names=" . join("|", keys %{$self->{Name}}) . "]"; | ||
| 347 | } | ||
| 348 | |||
| 349 | package XML::Checker::ERule::Children; | ||
| 350 | use vars qw( @ISA %_name %_map $_n ); | ||
| 351 | @ISA = qw( XML::Checker::ERule ); | ||
| 352 | |||
| 353 | sub new | ||
| 354 | { | ||
| 355 | my ($class) = @_; | ||
| 356 | bless {}, $class; | ||
| 357 | } | ||
| 358 | |||
| 359 | sub context | ||
| 360 | { | ||
| 361 | my ($self) = @_; | ||
| 362 | new XML::Checker::Context::Children ($self); | ||
| 363 | } | ||
| 364 | |||
| 365 | sub _add # static | ||
| 366 | { | ||
| 367 | my $exp = new XML::Checker::Term (@_); | ||
| 368 | $_map{$exp->{N}} = $exp; | ||
| 369 | $exp->str; | ||
| 370 | } | ||
| 371 | |||
| 372 | my $IDS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; | ||
| 373 | sub _tokenize | ||
| 374 | { | ||
| 375 | my ($self, $rule) = @_; | ||
| 376 | |||
| 377 | # Replace names with Terms of the form "<n#>", e.g. "<n2>". | ||
| 378 | # Lookup already used names and store new names in %_name. | ||
| 379 | # | ||
| 380 | $$rule =~ s/($XML::RegExp::Name)(?!>)/ | ||
| 381 | if (exists $_name{$1}) # name already used? | ||
| 382 | { | ||
| 383 | $_name{$1}->str; | ||
| 384 | } | ||
| 385 | else | ||
| 386 | { | ||
| 387 | my $exp = new XML::Checker::Term (C => 'n', N => $_n++, | ||
| 388 | Name => $1); | ||
| 389 | $_name{$1} = $_map{$exp->{N}} = $exp; | ||
| 390 | $exp->str; | ||
| 391 | } | ||
| 392 | /eg; | ||
| 393 | |||
| 394 | if ($_n < length $IDS) | ||
| 395 | { | ||
| 396 | # Generate regular expression for the name Term, i.e. | ||
| 397 | # a single character from $IDS | ||
| 398 | my $i = 0; | ||
| 399 | for (values %_name) | ||
| 400 | { | ||
| 401 | $_->{S} = substr ($IDS, $i++, 1); | ||
| 402 | #print "tokenized " . $_->{Name} . " num=" . $_->{N} . " to " . $_->{S} . "\n"; | ||
| 403 | } | ||
| 404 | } | ||
| 405 | else | ||
| 406 | { | ||
| 407 | # Generate RE, convert Term->{N} to hex string | ||
| 408 | # e.g. "03d". Calculate needed length of hex string first. | ||
| 409 | my $len = 0; | ||
| 410 | for (my $n = $_n - 1; $n > 0; $len++) { | ||
| 411 | $n = $n >> 4; | ||
| 412 | } | ||
| 413 | my $i = 0; | ||
| 414 | for (values %_name) | ||
| 415 | { | ||
| 416 | $_->{S} = sprintf ("%0${len}lx", $i++); | ||
| 417 | #print "tokenized " . $_->{Name} . " num=" . $_->{N} . " to " . $_->{S} . "\n"; | ||
| 418 | } | ||
| 419 | } | ||
| 420 | } | ||
| 421 | |||
| 422 | sub setModel | ||
| 423 | { | ||
| 424 | my ($self, $rule) = @_; | ||
| 425 | |||
| 426 | local $_n = 0; | ||
| 427 | local %_map = (); | ||
| 428 | local %_name = (); | ||
| 429 | |||
| 430 | $self->_tokenize (\$rule); | ||
| 431 | |||
| 432 | #?? check for single name - die "!ELEMENT contents can't be just a NAME" if $rule =~ /^$XML::RegExp::Name$/; | ||
| 433 | |||
| 434 | for ($rule) | ||
| 435 | { | ||
| 436 | my $n = 1; | ||
| 437 | while ($n) | ||
| 438 | { | ||
| 439 | $n = 0; | ||
| 440 | |||
| 441 | # cp := ( name | choice | seq ) ('?' | '*' | '+')? | ||
| 442 | $n++ while s/<[ncs](\d+)>([?*+]?)/_add | ||
| 443 | (C => 'a', N => $_n++, | ||
| 444 | S => ($_map{$1}->re . $2))/eg; | ||
| 445 | |||
| 446 | # choice := '(' ch_l ')' | ||
| 447 | $n++ while s/\(\s*<[ad](\d+)>\s*\)/_add | ||
| 448 | (C => 'c', N => $_n++, | ||
| 449 | S => "(" . join ("|", $_map{$1}->rel) . ")")/eg; | ||
| 450 | |||
| 451 | # ch_l := ( cp | ch_l ) '|' ( cp | ch_l ) | ||
| 452 | $n++ while s/<[ad](\d+)>\s*\|\s*<[ad](\d+)>/_add | ||
| 453 | (C => 'd', N => $_n++, | ||
| 454 | SL => [ $_map{$1}->rel, $_map{$2}->rel ])/eg; | ||
| 455 | |||
| 456 | # seq := '(' (seq_l ')' | ||
| 457 | $n++ while s/\(\s*<[at](\d+)>\s*\)/_add | ||
| 458 | (C => 's', N => $_n++, | ||
| 459 | S => "(".join("", $_map{$1}->rel).")")/eg; | ||
| 460 | |||
| 461 | # seq_l := ( cp | seq_l ) ',' ( cp | seq_l ) | ||
| 462 | $n++ while s/<[at](\d+)>\s*,\s*<[at](\d+)>/_add | ||
| 463 | (C => 't', N => $_n++, | ||
| 464 | SL => [ $_map{$1}->rel, $_map{$2}->rel ])/eg; | ||
| 465 | } | ||
| 466 | } | ||
| 467 | |||
| 468 | return 0 if ($rule !~ /^<a(\d+)>$/); | ||
| 469 | |||
| 470 | $self->{Name} = \%_name; | ||
| 471 | $self->{RE} = $_map{$1}->re; | ||
| 472 | |||
| 473 | return 1; | ||
| 474 | } | ||
| 475 | |||
| 476 | sub debug | ||
| 477 | { | ||
| 478 | my ($self) = @_; | ||
| 479 | "Children[RE=" . $self->{RE} . "]"; | ||
| 480 | } | ||
| 481 | |||
| 482 | |||
| 483 | package XML::Checker::ARule; | ||
| 484 | use XML::RegExp; | ||
| 485 | |||
| 486 | sub new | ||
| 487 | { | ||
| 488 | my ($class, $elem, $checker) = @_; | ||
| 489 | bless { Elem => $elem, Checker => $checker, Required => {} }, $class; | ||
| 490 | } | ||
| 491 | |||
| 492 | sub Attlist | ||
| 493 | { | ||
| 494 | my ($self, $attr, $type, $default, $fixed, $checker) = @_; | ||
| 495 | my ($c1, $c2); | ||
| 496 | |||
| 497 | if ($self->{Defined}->{$attr}) | ||
| 498 | { | ||
| 499 | my $tag = $self->{Elem}; | ||
| 500 | $self->fail ($attr, 110, "attribute [$attr] of element [$tag] already defined"); | ||
| 501 | } | ||
| 502 | else | ||
| 503 | { | ||
| 504 | $self->{Defined}->{$attr} = 1; | ||
| 505 | } | ||
| 506 | |||
| 507 | if ($default =~ /^\#(REQUIRED|IMPLIED)$/) | ||
| 508 | { | ||
| 509 | $c1 = $1; | ||
| 510 | |||
| 511 | # Keep list of all required attributes | ||
| 512 | if ($default eq '#REQUIRED') | ||
| 513 | { | ||
| 514 | $self->{Required}->{$attr} = 1; | ||
| 515 | } | ||
| 516 | } | ||
| 517 | else | ||
| 518 | { | ||
| 519 | $self->fail ($attr, 122, "invalid default attribute value [$default]") | ||
| 520 | unless $default =~ /^$XML::RegExp::AttValue$/; | ||
| 521 | |||
| 522 | $default = substr ($default, 1, length($default)-2); | ||
| 523 | $self->{Default}->{$attr} = $default; | ||
| 524 | $c1 = 'FIXED' if $fixed; | ||
| 525 | } | ||
| 526 | |||
| 527 | if ($type eq 'ID') | ||
| 528 | { | ||
| 529 | $self->fail ($attr, 123, "invalid default ID [$default], must be #REQUIRED or #IMPLIED") | ||
| 530 | unless $default =~ /^#(REQUIRED|IMPLIED)$/; | ||
| 531 | |||
| 532 | if (exists ($self->{ID}) && $self->{ID} ne $attr) | ||
| 533 | { | ||
| 534 | $self->fail ($attr, 151, "only one ID allowed per ELEMENT " . | ||
| 535 | "first=[" . $self->{ID} . "]"); | ||
| 536 | } | ||
| 537 | else | ||
| 538 | { | ||
| 539 | $self->{ID} = $attr; | ||
| 540 | } | ||
| 541 | $c2 = 'ID'; | ||
| 542 | } | ||
| 543 | elsif ($type =~ /^(IDREF|IDREFS|ENTITY|ENTITIES|NMTOKEN|NMTOKENS)$/) | ||
| 544 | { | ||
| 545 | my $def = $self->{Default}->{$attr}; | ||
| 546 | if (defined $def) | ||
| 547 | { | ||
| 548 | my $re = ($type =~ /^[IE]/) ? $XML::RegExp::Name : $XML::RegExp::NmToken; | ||
| 549 | if ($type =~ /S$/) | ||
| 550 | { | ||
| 551 | for (split (/\s+/, $def)) | ||
| 552 | { | ||
| 553 | $self->fail ($attr, 121, | ||
| 554 | "invalid default [$_] in $type [$def]") | ||
| 555 | unless $_ =~ /^$re$/; | ||
| 556 | } | ||
| 557 | } | ||
| 558 | else # singular | ||
| 559 | { | ||
| 560 | $self->fail ($attr, 120, "invalid default $type [$def]") | ||
| 561 | unless $def =~ /^$re$/; | ||
| 562 | } | ||
| 563 | } | ||
| 564 | $c2 = $type; | ||
| 565 | } | ||
| 566 | elsif ($type ne 'CDATA') # Enumerated := NotationType | Enumeration | ||
| 567 | { | ||
| 568 | if ($type =~ /^\s*NOTATION\s*\(\s*($XML::RegExp::Name(\s*\|\s*$XML::RegExp::Name)*)\s*\)\s*$/) | ||
| 569 | { | ||
| 570 | $self->fail ($attr, 135, "empty NOTATION list in ATTLIST") | ||
| 571 | unless defined $1; | ||
| 572 | |||
| 573 | my @tok = split (/\s*\|\s*/, $1); | ||
| 574 | for (@tok) | ||
| 575 | { | ||
| 576 | $self->fail ($attr, 100, "undefined NOTATION [$_] in ATTLIST") | ||
| 577 | unless exists $checker->{NOTATION}->{$_}; | ||
| 578 | } | ||
| 579 | |||
| 580 | my $re = join ("|", @tok); | ||
| 581 | $self->{NotationRE} = "^($re)\$"; | ||
| 582 | $c2 = 'NotationType'; | ||
| 583 | } | ||
| 584 | elsif ($type =~ /^\s*\(\s*($XML::RegExp::NmToken(\s*\|\s*$XML::RegExp::NmToken)*)\s*\)\s*$/) | ||
| 585 | { | ||
| 586 | # Enumeration | ||
| 587 | |||
| 588 | $self->fail ($attr, 136, "empty Enumeration list in ATTLIST") | ||
| 589 | unless defined $1; | ||
| 590 | |||
| 591 | my @tok = split (/\s*\|\s*/, $1); | ||
| 592 | for (@tok) | ||
| 593 | { | ||
| 594 | $self->fail ($attr, 134, | ||
| 595 | "invalid Enumeration value [$_] in ATTLIST") | ||
| 596 | unless $_ =~ /^$XML::RegExp::NmToken$/; | ||
| 597 | } | ||
| 598 | $self->{EnumRE}->{$attr} = '^(' . join ("|", @tok) . ')$'; #'; | ||
| 599 | $c2 = 'Enumeration'; | ||
| 600 | } | ||
| 601 | else | ||
| 602 | { | ||
| 603 | $self->fail ($attr, 137, "invalid ATTLIST type [$type]"); | ||
| 604 | } | ||
| 605 | } | ||
| 606 | |||
| 607 | $self->{Check1}->{$attr} = $c1 if $c1; | ||
| 608 | $self->{Check2}->{$attr} = $c2 if $c2; | ||
| 609 | } | ||
| 610 | |||
| 611 | sub fail | ||
| 612 | { | ||
| 613 | my $self = shift; | ||
| 614 | my $attr = shift; | ||
| 615 | $self->{Checker}->fail (@_, Element => $self->{Elem}, Attr => $attr); | ||
| 616 | } | ||
| 617 | |||
| 618 | sub check | ||
| 619 | { | ||
| 620 | my ($self, $attr) = @_; | ||
| 621 | my $func1 = $self->{Check1}->{$attr}; | ||
| 622 | my $func2 = $self->{Check2}->{$attr}; | ||
| 623 | # print "check func1=$func1 func2=$func2 @_\n"; | ||
| 624 | |||
| 625 | if (exists $self->{ReqNotSeen}->{$attr}) | ||
| 626 | { | ||
| 627 | delete $self->{ReqNotSeen}->{$attr}; | ||
| 628 | } | ||
| 629 | no strict; | ||
| 630 | |||
| 631 | &$func1 (@_) if defined $func1; | ||
| 632 | &$func2 (@_) if defined $func2; | ||
| 633 | } | ||
| 634 | |||
| 635 | # Copies the list of all required attributes from $self->{Required} to | ||
| 636 | # $self->{ReqNotSeen}. | ||
| 637 | # When check() encounters a required attribute, it is removed from ReqNotSeen. | ||
| 638 | # In EndAttr we look at which attribute names are still in ReqNotSeen - those | ||
| 639 | # are the ones that were not specified and are, therefore, in error. | ||
| 640 | sub StartAttr | ||
| 641 | { | ||
| 642 | my $self = shift; | ||
| 643 | my %not_seen = %{ $self->{Required} }; | ||
| 644 | $self->{ReqNotSeen} = \%not_seen; | ||
| 645 | } | ||
| 646 | |||
| 647 | # Checks which of the #REQUIRED attributes were not specified | ||
| 648 | sub EndAttr | ||
| 649 | { | ||
| 650 | my $self = shift; | ||
| 651 | |||
| 652 | for my $attr (keys %{ $self->{ReqNotSeen} }) | ||
| 653 | { | ||
| 654 | $self->fail ($attr, 159, | ||
| 655 | "unspecified value for \#REQUIRED attribute [$attr]"); | ||
| 656 | } | ||
| 657 | } | ||
| 658 | |||
| 659 | sub FIXED | ||
| 660 | { | ||
| 661 | my ($self, $attr, $val, $specified) = @_; | ||
| 662 | |||
| 663 | my $default = $self->{Default}->{$attr}; | ||
| 664 | $self->fail ($attr, 150, | ||
| 665 | "bad \#FIXED attribute value [$val], it should be [$default]") | ||
| 666 | unless ($val eq $default); | ||
| 667 | } | ||
| 668 | |||
| 669 | sub IMPLIED | ||
| 670 | { | ||
| 671 | my ($self, $attr, $val, $specified) = @_; | ||
| 672 | |||
| 673 | #?? should #IMPLIED be specified? | ||
| 674 | $self->fail ($attr, 158, | ||
| 675 | "unspecified value for \#IMPLIED attribute [$attr]") | ||
| 676 | unless $specified; | ||
| 677 | |||
| 678 | #?? Implied handler ? | ||
| 679 | } | ||
| 680 | |||
| 681 | # This is called when an attribute is passed to the check() method by | ||
| 682 | # XML::Checker::Attr(), i.e. when the attribute was specified explicitly | ||
| 683 | # or defaulted by the parser (which should never happen), *NOT* when the | ||
| 684 | # attribute was omitted. (The latter is checked by StartAttr/EndAttr) | ||
| 685 | sub REQUIRED | ||
| 686 | { | ||
| 687 | my ($self, $attr, $val, $specified) = @_; | ||
| 688 | # print "REQUIRED attr=$attr val=$val spec=$specified\n"; | ||
| 689 | |||
| 690 | $self->fail ($attr, 159, | ||
| 691 | "unspecified value for \#REQUIRED attribute [$attr]") | ||
| 692 | unless $specified; | ||
| 693 | } | ||
| 694 | |||
| 695 | sub ID # must be #IMPLIED or #REQUIRED | ||
| 696 | { | ||
| 697 | my ($self, $attr, $val, $specified) = @_; | ||
| 698 | |||
| 699 | $self->fail ($attr, 131, "invalid ID [$val]") | ||
| 700 | unless $val =~ /^$XML::RegExp::Name$/; | ||
| 701 | |||
| 702 | $self->fail ($attr, 111, "ID [$val] already defined") | ||
| 703 | if $self->{Checker}->{ID}->{$val}++; | ||
| 704 | } | ||
| 705 | |||
| 706 | sub IDREF | ||
| 707 | { | ||
| 708 | my ($self, $attr, $val, $specified) = @_; | ||
| 709 | |||
| 710 | $self->fail ($attr, 132, "invalid IDREF [$val]") | ||
| 711 | unless $val =~ /^$XML::RegExp::Name$/; | ||
| 712 | |||
| 713 | $self->{Checker}->{IDREF}->{$val}++; | ||
| 714 | } | ||
| 715 | |||
| 716 | sub IDREFS | ||
| 717 | { | ||
| 718 | my ($self, $attr, $val, $specified) = @_; | ||
| 719 | for (split /\s+/, $val) | ||
| 720 | { | ||
| 721 | $self->IDREF ($attr, $_); | ||
| 722 | } | ||
| 723 | } | ||
| 724 | |||
| 725 | sub ENTITY | ||
| 726 | { | ||
| 727 | my ($self, $attr, $val, $specified) = @_; | ||
| 728 | #?? should it be specified? | ||
| 729 | |||
| 730 | $self->fail ($attr, 133, "invalid ENTITY name [$val]") | ||
| 731 | unless $val =~ /^$XML::RegExp::Name$/; | ||
| 732 | |||
| 733 | $self->fail ($attr, 102, "undefined unparsed ENTITY [$val]") | ||
| 734 | unless exists $self->{Checker}->{Unparsed}->{$val}; | ||
| 735 | } | ||
| 736 | |||
| 737 | sub ENTITIES | ||
| 738 | { | ||
| 739 | my ($self, $attr, $val, $specified) = @_; | ||
| 740 | for (split /\s+/, $val) | ||
| 741 | { | ||
| 742 | $self->ENTITY ($attr, $_); | ||
| 743 | } | ||
| 744 | } | ||
| 745 | |||
| 746 | sub NMTOKEN | ||
| 747 | { | ||
| 748 | my ($self, $attr, $val, $specified) = @_; | ||
| 749 | $self->fail ($attr, 130, "invalid NMTOKEN [$val]") | ||
| 750 | unless $val =~ /^$XML::RegExp::NmToken$/; | ||
| 751 | } | ||
| 752 | |||
| 753 | sub NMTOKENS | ||
| 754 | { | ||
| 755 | my ($self, $attr, $val, $specified) = @_; | ||
| 756 | for (split /\s+/, $val) | ||
| 757 | { | ||
| 758 | $self->NMTOKEN ($attr, $_, $specified); | ||
| 759 | } | ||
| 760 | } | ||
| 761 | |||
| 762 | sub Enumeration | ||
| 763 | { | ||
| 764 | my ($self, $attr, $val, $specified) = @_; | ||
| 765 | my $re = $self->{EnumRE}->{$attr}; | ||
| 766 | |||
| 767 | $self->fail ($attr, 160, "invalid Enumeration value [$val]") | ||
| 768 | unless $val =~ /$re/; | ||
| 769 | } | ||
| 770 | |||
| 771 | sub NotationType | ||
| 772 | { | ||
| 773 | my ($self, $attr, $val, $specified) = @_; | ||
| 774 | my $re = $self->{NotationRE}; | ||
| 775 | |||
| 776 | $self->fail ($attr, 161, "invalid NOTATION value [$val]") | ||
| 777 | unless $val =~ /$re/; | ||
| 778 | |||
| 779 | $self->fail ($attr, 162, "undefined NOTATION [$val]") | ||
| 780 | unless exists $self->{Checker}->{NOTATION}->{$val}; | ||
| 781 | } | ||
| 782 | |||
| 783 | package XML::Checker; | ||
| 784 | use vars qw ( $VERSION $FAIL $INSIGNIF_WS ); | ||
| 785 | |||
| 786 | BEGIN | ||
| 787 | { | ||
| 788 | $VERSION = '0.13'; | ||
| 789 | } | ||
| 790 | |||
| 791 | $FAIL = \&print_error; | ||
| 792 | |||
| 793 | # Whether the last seen Char data was insignicant whitespace | ||
| 794 | $INSIGNIF_WS = 0; | ||
| 795 | |||
| 796 | sub new | ||
| 797 | { | ||
| 798 | my ($class, %args) = @_; | ||
| 799 | |||
| 800 | $args{ERule} = {}; | ||
| 801 | $args{ARule} = {}; | ||
| 802 | $args{InCDATA} = 0; | ||
| 803 | |||
| 804 | #$args{Debug} = 1; | ||
| 805 | bless \%args, $class; | ||
| 806 | } | ||
| 807 | |||
| 808 | # PerlSAX API | ||
| 809 | sub element_decl | ||
| 810 | { | ||
| 811 | my ($self, $hash) = @_; | ||
| 812 | $self->Element ($hash->{Name}, $hash->{Model}); | ||
| 813 | } | ||
| 814 | |||
| 815 | # Same parameter order as the Element handler in XML::Parser module | ||
| 816 | sub Element | ||
| 817 | { | ||
| 818 | my ($self, $name, $model) = @_; | ||
| 819 | |||
| 820 | if (defined $self->{ERule}->{$name}) | ||
| 821 | { | ||
| 822 | $self->fail (115, "ELEMENT [$name] already defined", | ||
| 823 | Element => $name); | ||
| 824 | } | ||
| 825 | |||
| 826 | if ($model eq "EMPTY") | ||
| 827 | { | ||
| 828 | $self->{ERule}->{$name} = new XML::Checker::ERule::EMPTY; | ||
| 829 | } | ||
| 830 | elsif ($model eq "ANY") | ||
| 831 | { | ||
| 832 | $self->{ERule}->{$name} = new XML::Checker::ERule::ANY; | ||
| 833 | } | ||
| 834 | elsif ($model =~ /#PCDATA/) | ||
| 835 | { | ||
| 836 | my $rule = new XML::Checker::ERule::Mixed; | ||
| 837 | if ($rule->setModel ($model)) | ||
| 838 | { | ||
| 839 | $self->{ERule}->{$name} = $rule; | ||
| 840 | } | ||
| 841 | else | ||
| 842 | { | ||
| 843 | $self->fail (124, "bad model [$model] for ELEMENT [$name]", | ||
| 844 | Element => $name); | ||
| 845 | } | ||
| 846 | } | ||
| 847 | else | ||
| 848 | { | ||
| 849 | my $rule = new XML::Checker::ERule::Children; | ||
| 850 | if ($rule->setModel ($model)) | ||
| 851 | { | ||
| 852 | $self->{ERule}->{$name} = $rule; | ||
| 853 | } | ||
| 854 | else | ||
| 855 | { | ||
| 856 | $self->fail (124, "bad model [$model] for ELEMENT [$name]", | ||
| 857 | Element => $name); | ||
| 858 | } | ||
| 859 | } | ||
| 860 | my $rule = $self->{ERule}->{$name}; | ||
| 861 | print "added ELEMENT model for $name: " . $rule->debug . "\n" | ||
| 862 | if $rule and $self->{Debug}; | ||
| 863 | } | ||
| 864 | |||
| 865 | # PerlSAX API | ||
| 866 | sub attlist_decl | ||
| 867 | { | ||
| 868 | my ($self, $hash) = @_; | ||
| 869 | $self->Attlist ($hash->{ElementName}, $hash->{AttributeName}, | ||
| 870 | $hash->{Type}, $hash->{Default}, $hash->{Fixed}); | ||
| 871 | } | ||
| 872 | |||
| 873 | sub Attlist | ||
| 874 | { | ||
| 875 | my ($self, $tag, $attrName, $type, $default, $fixed) = @_; | ||
| 876 | my $arule = $self->{ARule}->{$tag} ||= | ||
| 877 | new XML::Checker::ARule ($tag, $self); | ||
| 878 | |||
| 879 | $arule->Attlist ($attrName, $type, $default, $fixed, $self); | ||
| 880 | } | ||
| 881 | |||
| 882 | # Initializes the context stack to check an XML::DOM::Element | ||
| 883 | sub InitDomElem | ||
| 884 | { | ||
| 885 | my $self = shift; | ||
| 886 | |||
| 887 | # initialize Context stack | ||
| 888 | $self->{Context} = [ new XML::Checker::Context::ANY ($self) ]; | ||
| 889 | $self->{InCDATA} = 0; | ||
| 890 | } | ||
| 891 | |||
| 892 | # Clears the context stack after checking an XML::DOM::Element | ||
| 893 | sub FinalDomElem | ||
| 894 | { | ||
| 895 | my $self = shift; | ||
| 896 | delete $self->{Context}; | ||
| 897 | } | ||
| 898 | |||
| 899 | # PerlSAX API | ||
| 900 | sub start_document | ||
| 901 | { | ||
| 902 | shift->Init; | ||
| 903 | } | ||
| 904 | |||
| 905 | sub Init | ||
| 906 | { | ||
| 907 | my $self = shift; | ||
| 908 | |||
| 909 | # initialize Context stack | ||
| 910 | $self->{Context} = [ new XML::Checker::DocContext ($self) ]; | ||
| 911 | $self->{InCDATA} = 0; | ||
| 912 | } | ||
| 913 | |||
| 914 | # PerlSAX API | ||
| 915 | sub end_document | ||
| 916 | { | ||
| 917 | shift->Final; | ||
| 918 | } | ||
| 919 | |||
| 920 | sub Final | ||
| 921 | { | ||
| 922 | my $self = shift; | ||
| 923 | #?? could add more statistics: unreferenced Unparsed, ID | ||
| 924 | |||
| 925 | for (keys %{ $self->{IDREF} }) | ||
| 926 | { | ||
| 927 | my $n = $self->{IDREF}->{$_}; | ||
| 928 | $self->fail (200, "undefined ID [$_] was referenced [$n] times") | ||
| 929 | unless defined $self->{ID}->{$_}; | ||
| 930 | } | ||
| 931 | |||
| 932 | for (keys %{ $self->{ID} }) | ||
| 933 | { | ||
| 934 | my $n = $self->{IDREF}->{$_} || 0; | ||
| 935 | $self->fail (300, "[$n] references to ID [$_]"); | ||
| 936 | } | ||
| 937 | |||
| 938 | delete $self->{Context}; | ||
| 939 | } | ||
| 940 | |||
| 941 | sub getRootElement | ||
| 942 | { | ||
| 943 | my $self = shift; | ||
| 944 | # print "getRoot $self " . $self->{RootElement} . "\n"; | ||
| 945 | $_[0]->{RootElement}; | ||
| 946 | } | ||
| 947 | |||
| 948 | # PerlSAX API | ||
| 949 | sub doctype_decl | ||
| 950 | { | ||
| 951 | my ($self, $hash) = @_; | ||
| 952 | $self->Doctype ($hash->{Name}, $hash->{SystemId}, | ||
| 953 | $hash->{PublicId}, $hash->{Internal}); | ||
| 954 | } | ||
| 955 | |||
| 956 | sub Doctype | ||
| 957 | { | ||
| 958 | my ($self, $name, $sysid, $pubid, $internal) = @_; | ||
| 959 | $self->{RootElement} = $name; | ||
| 960 | |||
| 961 | my $context = $self->{Context}->[0]; | ||
| 962 | $context->setRootElement ($name); | ||
| 963 | |||
| 964 | #?? what else | ||
| 965 | } | ||
| 966 | |||
| 967 | sub Attr | ||
| 968 | { | ||
| 969 | my ($self, $tag, $attr, $val, $specified) = @_; | ||
| 970 | |||
| 971 | #print "Attr for tag=$tag attr=$attr val=$val spec=$specified\n"; | ||
| 972 | |||
| 973 | my $arule = $self->{ARule}->{$tag}; | ||
| 974 | if (defined $arule && $arule->{Defined}->{$attr}) | ||
| 975 | { | ||
| 976 | $arule->check ($attr, $val, $specified); | ||
| 977 | } | ||
| 978 | else | ||
| 979 | { | ||
| 980 | $self->fail (103, "undefined attribute [$attr]", Element => $tag); | ||
| 981 | } | ||
| 982 | } | ||
| 983 | |||
| 984 | sub EndAttr | ||
| 985 | { | ||
| 986 | my $self = shift; | ||
| 987 | |||
| 988 | my $arule = $self->{CurrARule}; | ||
| 989 | if (defined $arule) | ||
| 990 | { | ||
| 991 | $arule->EndAttr; | ||
| 992 | } | ||
| 993 | } | ||
| 994 | |||
| 995 | # PerlSAX API | ||
| 996 | sub start_element | ||
| 997 | { | ||
| 998 | my ($self, $hash) = @_; | ||
| 999 | my $tag = $hash->{Name}; | ||
| 1000 | my $attr = $hash->{Attributes}; | ||
| 1001 | |||
| 1002 | $self->Start ($tag); | ||
| 1003 | |||
| 1004 | if (exists $hash->{AttributeOrder}) | ||
| 1005 | { | ||
| 1006 | my $defaulted = $hash->{Defaulted}; | ||
| 1007 | my @order = @{ $hash->{AttributeOrder} }; | ||
| 1008 | |||
| 1009 | # Specified attributes | ||
| 1010 | for (my $i = 0; $i < $defaulted; $i++) | ||
| 1011 | { | ||
| 1012 | my $a = $order[$i]; | ||
| 1013 | $self->Attr ($tag, $a, $attr->{$a}, 1); | ||
| 1014 | } | ||
| 1015 | |||
| 1016 | # Defaulted attributes | ||
| 1017 | for (my $i = $defaulted; $i < @order; $i++) | ||
| 1018 | { | ||
| 1019 | my $attr = $order[$i]; | ||
| 1020 | $self->Attr ($tag, $a, $attr->{$a}, 0); | ||
| 1021 | } | ||
| 1022 | } | ||
| 1023 | else | ||
| 1024 | { | ||
| 1025 | # Assume all attributes were specified | ||
| 1026 | my @attr = %$attr; | ||
| 1027 | my ($key, $val); | ||
| 1028 | while ($key = shift @attr) | ||
| 1029 | { | ||
| 1030 | $val = shift @attr; | ||
| 1031 | |||
| 1032 | $self->Attr ($tag, $key, $val, 1); | ||
| 1033 | } | ||
| 1034 | } | ||
| 1035 | $self->EndAttr; | ||
| 1036 | } | ||
| 1037 | |||
| 1038 | sub Start | ||
| 1039 | { | ||
| 1040 | my ($self, $tag) = @_; | ||
| 1041 | #?? if first tag, check with root element - or does expat check this already? | ||
| 1042 | |||
| 1043 | my $context = $self->{Context}; | ||
| 1044 | $context->[0]->Start ($self, $tag); | ||
| 1045 | |||
| 1046 | my $erule = $self->{ERule}->{$tag}; | ||
| 1047 | if (defined $erule) | ||
| 1048 | { | ||
| 1049 | unshift @$context, $erule->context; | ||
| 1050 | } | ||
| 1051 | else | ||
| 1052 | { | ||
| 1053 | # It's not a real error according to the XML Spec. | ||
| 1054 | $self->fail (101, "undefined ELEMENT [$tag]"); | ||
| 1055 | unshift @$context, new XML::Checker::Context::ANY; | ||
| 1056 | } | ||
| 1057 | |||
| 1058 | #?? what about ARule ?? | ||
| 1059 | my $arule = $self->{ARule}->{$tag}; | ||
| 1060 | if (defined $arule) | ||
| 1061 | { | ||
| 1062 | $self->{CurrARule} = $arule; | ||
| 1063 | $arule->StartAttr; | ||
| 1064 | } | ||
| 1065 | } | ||
| 1066 | |||
| 1067 | # PerlSAX API | ||
| 1068 | sub end_element | ||
| 1069 | { | ||
| 1070 | shift->End; | ||
| 1071 | } | ||
| 1072 | |||
| 1073 | sub End | ||
| 1074 | { | ||
| 1075 | my ($self) = @_; | ||
| 1076 | my $context = $self->{Context}; | ||
| 1077 | |||
| 1078 | $context->[0]->End ($self); | ||
| 1079 | shift @$context; | ||
| 1080 | } | ||
| 1081 | |||
| 1082 | # PerlSAX API | ||
| 1083 | sub characters | ||
| 1084 | { | ||
| 1085 | my ($self, $hash) = @_; | ||
| 1086 | my $data = $hash->{Data}; | ||
| 1087 | |||
| 1088 | if ($self->{InCDATA}) | ||
| 1089 | { | ||
| 1090 | $self->CData ($data); | ||
| 1091 | } | ||
| 1092 | else | ||
| 1093 | { | ||
| 1094 | $self->Char ($data); | ||
| 1095 | } | ||
| 1096 | } | ||
| 1097 | |||
| 1098 | # PerlSAX API | ||
| 1099 | sub start_cdata | ||
| 1100 | { | ||
| 1101 | $_[0]->{InCDATA} = 1; | ||
| 1102 | } | ||
| 1103 | |||
| 1104 | # PerlSAX API | ||
| 1105 | sub end_cdata | ||
| 1106 | { | ||
| 1107 | $_[0]->{InCDATA} = 0; | ||
| 1108 | } | ||
| 1109 | |||
| 1110 | sub Char | ||
| 1111 | { | ||
| 1112 | my ($self, $text) = @_; | ||
| 1113 | my $context = $self->{Context}; | ||
| 1114 | |||
| 1115 | # NOTE: calls to isWS may set this to 1. | ||
| 1116 | $INSIGNIF_WS = 0; | ||
| 1117 | |||
| 1118 | $context->[0]->Char ($self, $text); | ||
| 1119 | } | ||
| 1120 | |||
| 1121 | # Treat CDATASection same as Char (Text) | ||
| 1122 | sub CData | ||
| 1123 | { | ||
| 1124 | my ($self, $cdata) = @_; | ||
| 1125 | my $context = $self->{Context}; | ||
| 1126 | |||
| 1127 | $context->[0]->Char ($self, $cdata); | ||
| 1128 | |||
| 1129 | # CDATASection can never be insignificant whitespace | ||
| 1130 | $INSIGNIF_WS = 0; | ||
| 1131 | #?? I'm not sure if this assumption is correct | ||
| 1132 | } | ||
| 1133 | |||
| 1134 | # PerlSAX API | ||
| 1135 | sub comment | ||
| 1136 | { | ||
| 1137 | my ($self, $hash) = @_; | ||
| 1138 | $self->Comment ($hash->{Data}); | ||
| 1139 | } | ||
| 1140 | |||
| 1141 | sub Comment | ||
| 1142 | { | ||
| 1143 | # ?? what can be checked here? | ||
| 1144 | } | ||
| 1145 | |||
| 1146 | # PerlSAX API | ||
| 1147 | sub entity_reference | ||
| 1148 | { | ||
| 1149 | my ($self, $hash) = @_; | ||
| 1150 | $self->EntityRef ($hash->{Name}, 0); | ||
| 1151 | #?? parameter entities (like %par;) are NOT supported! | ||
| 1152 | # PerlSAX::handle_default should be fixed! | ||
| 1153 | } | ||
| 1154 | |||
| 1155 | sub EntityRef | ||
| 1156 | { | ||
| 1157 | my ($self, $ref, $isParam) = @_; | ||
| 1158 | |||
| 1159 | if ($isParam) | ||
| 1160 | { | ||
| 1161 | # expand to "%name;" | ||
| 1162 | print STDERR "XML::Checker::Entity - parameter Entity (%ent;) not implemented\n"; | ||
| 1163 | } | ||
| 1164 | else | ||
| 1165 | { | ||
| 1166 | # Treat same as Char - for now | ||
| 1167 | my $context = $self->{Context}; | ||
| 1168 | $context->[0]->Char ($self, "&$ref;"); | ||
| 1169 | $INSIGNIF_WS = 0; | ||
| 1170 | #?? I could count the number of times each Entity is referenced | ||
| 1171 | } | ||
| 1172 | } | ||
| 1173 | |||
| 1174 | # PerlSAX API | ||
| 1175 | sub unparsed_entity_decl | ||
| 1176 | { | ||
| 1177 | my ($self, $hash) = @_; | ||
| 1178 | $self->Unparsed ($hash->{Name}); | ||
| 1179 | #?? what about Base, SytemId, PublicId ? | ||
| 1180 | } | ||
| 1181 | |||
| 1182 | sub Unparsed | ||
| 1183 | { | ||
| 1184 | my ($self, $entity) = @_; | ||
| 1185 | # print "ARule::Unparsed $entity\n"; | ||
| 1186 | if ($self->{Unparsed}->{$entity}) | ||
| 1187 | { | ||
| 1188 | $self->fail (112, "unparsed ENTITY [$entity] already defined"); | ||
| 1189 | } | ||
| 1190 | else | ||
| 1191 | { | ||
| 1192 | $self->{Unparsed}->{$entity} = 1; | ||
| 1193 | } | ||
| 1194 | } | ||
| 1195 | |||
| 1196 | # PerlSAX API | ||
| 1197 | sub notation_decl | ||
| 1198 | { | ||
| 1199 | my ($self, $hash) = @_; | ||
| 1200 | $self->Notation ($hash->{Name}); | ||
| 1201 | #?? what about Base, SytemId, PublicId ? | ||
| 1202 | } | ||
| 1203 | |||
| 1204 | sub Notation | ||
| 1205 | { | ||
| 1206 | my ($self, $notation) = @_; | ||
| 1207 | if ($self->{NOTATION}->{$notation}) | ||
| 1208 | { | ||
| 1209 | $self->fail (113, "NOTATION [$notation] already defined"); | ||
| 1210 | } | ||
| 1211 | else | ||
| 1212 | { | ||
| 1213 | $self->{NOTATION}->{$notation} = 1; | ||
| 1214 | } | ||
| 1215 | } | ||
| 1216 | |||
| 1217 | # PerlSAX API | ||
| 1218 | sub entity_decl | ||
| 1219 | { | ||
| 1220 | my ($self, $hash) = @_; | ||
| 1221 | |||
| 1222 | $self->Entity ($hash->{Name}, $hash->{Value}, $hash->{SystemId}, | ||
| 1223 | $hash->{PublicId}, $hash->{'Notation'}); | ||
| 1224 | } | ||
| 1225 | |||
| 1226 | sub Entity | ||
| 1227 | { | ||
| 1228 | my ($self, $name, $val, $sysId, $pubId, $ndata) = @_; | ||
| 1229 | |||
| 1230 | if (exists $self->{ENTITY}->{$name}) | ||
| 1231 | { | ||
| 1232 | $self->fail (114, "ENTITY [$name] already defined"); | ||
| 1233 | } | ||
| 1234 | else | ||
| 1235 | { | ||
| 1236 | $self->{ENTITY}->{$name} = $val; | ||
| 1237 | } | ||
| 1238 | } | ||
| 1239 | |||
| 1240 | # PerlSAX API | ||
| 1241 | #sub xml_decl {} $hash=> Version, Encoding, Standalone | ||
| 1242 | # Don't implement resolve_entity() which is called by ExternEnt! | ||
| 1243 | #sub processing_instruction {} $hash=> Target, Data | ||
| 1244 | |||
| 1245 | # Returns whether the Char data is whitespace and also updates the | ||
| 1246 | # $INSIGNIF_WS variable to indicate whether it is insignificant whitespace. | ||
| 1247 | # Note that this method is only called in places where potential whitespace | ||
| 1248 | # can be insignificant (i.e. when the ERule is Children or EMPTY) | ||
| 1249 | sub isWS | ||
| 1250 | { | ||
| 1251 | # pverdret: why not /\A\s+\Z/ ??? | ||
| 1252 | $INSIGNIF_WS = ($_[1] =~ /^\s*$/); | ||
| 1253 | } | ||
| 1254 | |||
| 1255 | sub isInsignifWS | ||
| 1256 | { | ||
| 1257 | $INSIGNIF_WS; | ||
| 1258 | } | ||
| 1259 | |||
| 1260 | sub fail | ||
| 1261 | { | ||
| 1262 | my $self = shift; | ||
| 1263 | &$FAIL (@_); | ||
| 1264 | } | ||
| 1265 | |||
| 1266 | sub print_error # static | ||
| 1267 | { | ||
| 1268 | my $str = error_string (@_); | ||
| 1269 | print STDERR $str; | ||
| 1270 | } | ||
| 1271 | |||
| 1272 | sub error_string # static | ||
| 1273 | { | ||
| 1274 | my $code = shift; | ||
| 1275 | my $msg = shift; | ||
| 1276 | |||
| 1277 | my @a = (); | ||
| 1278 | my ($key, $val); | ||
| 1279 | while ($key = shift) | ||
| 1280 | { | ||
| 1281 | $val = shift; | ||
| 1282 | push @a, ("$key " . (defined $val ? $val : "(undef)")); | ||
| 1283 | } | ||
| 1284 | |||
| 1285 | my $cat = $code >= 200 ? ($code >= 300 ? "INFO" : "WARNING") : "ERROR"; | ||
| 1286 | my $str = join (", ", @a); | ||
| 1287 | $str = length($str) ? "\tContext: $str\n" : ""; | ||
| 1288 | |||
| 1289 | "XML::Checker $cat-$code: $msg\n$str"; | ||
| 1290 | } | ||
| 1291 | |||
| 1292 | sub debug | ||
| 1293 | { | ||
| 1294 | my ($self) = @_; | ||
| 1295 | my $context = $self->{Context}->[0]; | ||
| 1296 | my $c = $context ? $context->debug : "no context"; | ||
| 1297 | my $root = $self->{RootElement}; | ||
| 1298 | |||
| 1299 | "Checker[$c,RootElement=$root]"; | ||
| 1300 | } | ||
| 1301 | |||
| 1302 | 1; # package return code | ||
| 1303 | |||
| 1304 | __END__ |