/usr/share/perl5/XML/Checker.pm is in libxml-checker-perl 0.13-6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 | use warnings;
use strict;
#
#
# TO DO
# - update docs regarding PerlSAX interface
# - add current node to error context when checking DOM subtrees
# - add parsed Entity to test XML files
# - free circular references
# - Implied handler?
# - Notation, Entity, Unparsed checks, Default handler?
# - check no root element (it's checked by expat) ?
package XML::Checker::Term;
use strict;
sub new
{
my ($class, %h) = @_;
bless \%h, $class;
}
sub str
{
'<' . $_[0]->{C} . $_[0]->{N} . '>'
}
sub re
{
$_[0]->{S}
}
sub rel
{
my $self = shift;
defined $self->{SL} ? @{ $self->{SL} } : ( $self->{S} );
}
sub debug
{
my $t = shift;
my ($c, $n, $s) = ($t->{C}, $t->{N}, $t->{S});
my @sl = $t->rel;
"{C=$c N=$n S=$s SL=@sl}";
}
#-------------------------------------------------------------------------
package XML::Checker::Context;
sub new
{
my ($class) = @_;
my $scalar;
bless \$scalar, $class;
}
sub Start {}
sub End {}
sub Char {}
#
# The initial Context when checking an entire XML Document
#
package XML::Checker::DocContext;
use vars qw( @ISA );
@ISA = qw( XML::Checker::Context );
sub new
{
#??checker not used
my ($class, $checker) = @_;
bless { }, $class;
}
sub setRootElement
{
$_[0]->{RootElement} = $_[1];
}
sub Start
{
my ($self, $checker, $tag) = @_;
if (exists $self->{Elem})
{
my $tags = join (", ", @{$self->{Elem}});
$checker->fail (155, "more than one root Element [$tags]");
push @{$self->{Elem}}, $tag;
}
else
{
$self->{Elem} = [ $tag ];
}
my $exp_root = $self->{RootElement};
$checker->fail (156, "unexpected root Element [$tag], expected [$exp_root]")
if defined ($exp_root) and $tag ne $exp_root;
}
sub debug
{
my $self = shift;
"DocContext[Count=" . $self->{Count} . ",Root=" .
$self->{RootElement} . "]";
}
package XML::Checker::Context::ANY;
use vars qw( @ISA );
@ISA = qw( XML::Checker::Context );
# No overrides, because everything is accepted
sub debug { "XML::Checker::Context::ANY" }
package XML::Checker::Context::EMPTY;
use vars qw( @ISA $ALLOW_WHITE_SPACE );
@ISA = qw( XML::Checker::Context );
$ALLOW_WHITE_SPACE = 0;
sub debug { "XML::Checker::Context::EMPTY" }
sub Start
{
my ($self, $checker, $tag) = @_;
$checker->fail (152, "Element should be EMPTY, found Element [$tag]");
}
sub Char
{
my ($self, $checker, $str) = @_;
$checker->fail (153, "Element should be EMPTY, found text [$str]")
unless ($ALLOW_WHITE_SPACE and $checker->isWS ($str));
# NOTE: if $ALLOW_WHITE_SPACE = 1, the isWS call does not only check
# whether it is whitespace, but it also informs the checker that this
# might be insignificant whitespace
}
#?? what about Comments
package XML::Checker::Context::Children;
use vars qw( @ISA );
@ISA = qw( XML::Checker::Context );
sub new
{
my ($class, $rule) = @_;
bless { Name => $rule->{Name}, RE => $rule->{RE}, Buf => "", N => 0 }, $class;
}
sub phash
{
my $href = shift;
my $str = "";
for (keys %$href)
{
$str .= ' ' if $str;
$str .= $_ . '=' . $href->{$_};
}
$str;
}
sub debug
{
my $self = shift;
"Context::Children[Name=(" . phash ($self->{Name}) . ",N=" . $self->{N} .
",RE=" . $self->{RE} . ",Buf=[" . $self->{Buf} . "]";
}
sub Start
{
my ($self, $checker, $tag) = @_;
#print "Children.Start tag=$tag rule=$checker drule=" . $checker->debug . "\n";
if (exists $self->{Name}->{$tag})
{
#print "Buf=[".$self->{Buf}. "] tag=[" . $self->{Name}->{$tag}->{S} . "]\n";
$self->{Buf} .= $self->{Name}->{$tag}->{S};
}
else
{
$checker->fail (157, "unexpected Element [$tag]",
ChildElementIndex => $self->{N})
}
$self->{N}++;
}
sub decode
{
my ($self) = @_;
my $re = $self->{RE};
my $name = $self->{Name};
my $buf = $self->{Buf};
# length of token, in a content model all tokens are the same length
my $len = 0;
my %s = ();
while (my ($key, $val) = each %$name)
{
$len = length($val->{S}) unless $len;
$s{$val->{S}} = $key;
}
# ex. $key = C_31 and $name = 01
#use warnings;
my $dots = "[^()*+?|]" x $len;
$buf =~ s/($dots)/$s{$1} . ","/ge;
chop $buf;
$re =~ s/($dots)/"(" . $s{$1} . ")"/ge;
"Found=[$buf] RE=[$re]"
}
sub End
{
my ($self, $checker) = @_;
my $re = $self->{RE};
unless ( $self->{Buf} =~ /\S/ ) {
unless ( $self->{Buf} =~ /^$re$/ ) {
$checker->fail (170, "Element can't be empty " . $self->decode);
return;
}
}
unless ( $self->{Buf} =~ /^$re$/ ) {
$checker->fail (154, "bad order of Elements " . $self->decode);
}
}
sub Char
{
my ($self, $checker, $str) = @_;
$checker->fail (149, "Element should only contain sub elements, found text [$str]")
unless ($checker->isWS ($str));
}
package XML::Checker::Context::Mixed;
use vars qw( @ISA );
@ISA = qw( XML::Checker::Context );
sub new
{
my ($class, $rule) = @_;
bless { Name => $rule->{Name}, N => 0 }, $class;
}
sub debug
{
my $self = shift;
"Context::Mixed[Name=" . $self->{Name} . ",N=" , $self->{N} . "]";
}
sub Start
{
my ($self, $checker, $tag) = @_;
$checker->fail (157, "unexpected Element [$tag]",
ChildElementIndex => $self->{N})
unless exists $self->{Name}->{$tag};
$self->{N}++;
}
package XML::Checker::ERule;
package XML::Checker::ERule::EMPTY;
use vars qw( @ISA );
@ISA = qw( XML::Checker::ERule );
sub new
{
my ($class) = @_;
bless {}, $class;
}
my $context = new XML::Checker::Context::EMPTY;
sub context { $context } # share the context
sub debug { "EMPTY" }
package XML::Checker::ERule::ANY;
use vars qw( @ISA );
@ISA = qw( XML::Checker::ERule );
sub new
{
my ($class) = @_;
bless {}, $class;
}
my $any_context = new XML::Checker::Context::ANY;
sub context { $any_context } # share the context
sub debug { "ANY" }
package XML::Checker::ERule::Mixed;
use vars qw( @ISA );
@ISA = qw( XML::Checker::ERule );
sub new
{
my ($class) = @_;
bless { Name => {} }, $class;
}
sub context
{
my ($self) = @_;
new XML::Checker::Context::Mixed ($self);
}
sub setModel
{
my ($self, $model) = @_;
my $rule = $model;
# Mixed := '(' '#PCDATA' ')' '*'?
if ($rule =~ /^\(\s*#PCDATA\s*\)(\*)?$/)
{
#? how do we interpret the '*' ??
return 1;
}
else # Mixed := '(' '#PCDATA' ('|' Name)* ')*'
{
return 0 unless $rule =~ s/^\(\s*#PCDATA\s*//;
return 0 unless $rule =~ s/\s*\)\*$//;
my %names = ();
while ($rule =~ s/^\s*\|\s*($XML::RegExp::Name)//)
{
$names{$1} = 1;
}
if ($rule eq "")
{
$self->{Name} = \%names;
return 1;
}
}
return 0;
}
sub debug
{
my ($self) = @_;
"Mixed[Names=" . join("|", keys %{$self->{Name}}) . "]";
}
package XML::Checker::ERule::Children;
use vars qw( @ISA %_name %_map $_n );
@ISA = qw( XML::Checker::ERule );
sub new
{
my ($class) = @_;
bless {}, $class;
}
sub context
{
my ($self) = @_;
new XML::Checker::Context::Children ($self);
}
sub _add # static
{
my $exp = new XML::Checker::Term (@_);
$_map{$exp->{N}} = $exp;
$exp->str;
}
my $IDS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
sub _tokenize
{
my ($self, $rule) = @_;
# Replace names with Terms of the form "<n#>", e.g. "<n2>".
# Lookup already used names and store new names in %_name.
#
$$rule =~ s/($XML::RegExp::Name)(?!>)/
if (exists $_name{$1}) # name already used?
{
$_name{$1}->str;
}
else
{
my $exp = new XML::Checker::Term (C => 'n', N => $_n++,
Name => $1);
$_name{$1} = $_map{$exp->{N}} = $exp;
$exp->str;
}
/eg;
if ($_n < length $IDS)
{
# Generate regular expression for the name Term, i.e.
# a single character from $IDS
my $i = 0;
for (values %_name)
{
$_->{S} = substr ($IDS, $i++, 1);
#print "tokenized " . $_->{Name} . " num=" . $_->{N} . " to " . $_->{S} . "\n";
}
}
else
{
# Generate RE, convert Term->{N} to hex string
# e.g. "03d". Calculate needed length of hex string first.
my $len = 0;
for (my $n = $_n - 1; $n > 0; $len++) {
$n = $n >> 4;
}
my $i = 0;
for (values %_name)
{
$_->{S} = sprintf ("%0${len}lx", $i++);
#print "tokenized " . $_->{Name} . " num=" . $_->{N} . " to " . $_->{S} . "\n";
}
}
}
sub setModel
{
my ($self, $rule) = @_;
local $_n = 0;
local %_map = ();
local %_name = ();
$self->_tokenize (\$rule);
#?? check for single name - die "!ELEMENT contents can't be just a NAME" if $rule =~ /^$XML::RegExp::Name$/;
for ($rule)
{
my $n = 1;
while ($n)
{
$n = 0;
# cp := ( name | choice | seq ) ('?' | '*' | '+')?
$n++ while s/<[ncs](\d+)>([?*+]?)/_add
(C => 'a', N => $_n++,
S => ($_map{$1}->re . $2))/eg;
# choice := '(' ch_l ')'
$n++ while s/\(\s*<[ad](\d+)>\s*\)/_add
(C => 'c', N => $_n++,
S => "(" . join ("|", $_map{$1}->rel) . ")")/eg;
# ch_l := ( cp | ch_l ) '|' ( cp | ch_l )
$n++ while s/<[ad](\d+)>\s*\|\s*<[ad](\d+)>/_add
(C => 'd', N => $_n++,
SL => [ $_map{$1}->rel, $_map{$2}->rel ])/eg;
# seq := '(' (seq_l ')'
$n++ while s/\(\s*<[at](\d+)>\s*\)/_add
(C => 's', N => $_n++,
S => "(".join("", $_map{$1}->rel).")")/eg;
# seq_l := ( cp | seq_l ) ',' ( cp | seq_l )
$n++ while s/<[at](\d+)>\s*,\s*<[at](\d+)>/_add
(C => 't', N => $_n++,
SL => [ $_map{$1}->rel, $_map{$2}->rel ])/eg;
}
}
return 0 if ($rule !~ /^<a(\d+)>$/);
$self->{Name} = \%_name;
$self->{RE} = $_map{$1}->re;
return 1;
}
sub debug
{
my ($self) = @_;
"Children[RE=" . $self->{RE} . "]";
}
package XML::Checker::ARule;
use XML::RegExp;
sub new
{
my ($class, $elem, $checker) = @_;
bless { Elem => $elem, Checker => $checker, Required => {} }, $class;
}
sub Attlist
{
my ($self, $attr, $type, $default, $fixed, $checker) = @_;
my ($c1, $c2);
if ($self->{Defined}->{$attr})
{
my $tag = $self->{Elem};
$self->fail ($attr, 110, "attribute [$attr] of element [$tag] already defined");
}
else
{
$self->{Defined}->{$attr} = 1;
}
if ($default =~ /^\#(REQUIRED|IMPLIED)$/)
{
$c1 = $1;
# Keep list of all required attributes
if ($default eq '#REQUIRED')
{
$self->{Required}->{$attr} = 1;
}
}
else
{
$self->fail ($attr, 122, "invalid default attribute value [$default]")
unless $default =~ /^$XML::RegExp::AttValue$/;
$default = substr ($default, 1, length($default)-2);
$self->{Default}->{$attr} = $default;
$c1 = 'FIXED' if $fixed;
}
if ($type eq 'ID')
{
$self->fail ($attr, 123, "invalid default ID [$default], must be #REQUIRED or #IMPLIED")
unless $default =~ /^#(REQUIRED|IMPLIED)$/;
if (exists ($self->{ID}) && $self->{ID} ne $attr)
{
$self->fail ($attr, 151, "only one ID allowed per ELEMENT " .
"first=[" . $self->{ID} . "]");
}
else
{
$self->{ID} = $attr;
}
$c2 = 'ID';
}
elsif ($type =~ /^(IDREF|IDREFS|ENTITY|ENTITIES|NMTOKEN|NMTOKENS)$/)
{
my $def = $self->{Default}->{$attr};
if (defined $def)
{
my $re = ($type =~ /^[IE]/) ? $XML::RegExp::Name : $XML::RegExp::NmToken;
if ($type =~ /S$/)
{
for (split (/\s+/, $def))
{
$self->fail ($attr, 121,
"invalid default [$_] in $type [$def]")
unless $_ =~ /^$re$/;
}
}
else # singular
{
$self->fail ($attr, 120, "invalid default $type [$def]")
unless $def =~ /^$re$/;
}
}
$c2 = $type;
}
elsif ($type ne 'CDATA') # Enumerated := NotationType | Enumeration
{
if ($type =~ /^\s*NOTATION\s*\(\s*($XML::RegExp::Name(\s*\|\s*$XML::RegExp::Name)*)\s*\)\s*$/)
{
$self->fail ($attr, 135, "empty NOTATION list in ATTLIST")
unless defined $1;
my @tok = split (/\s*\|\s*/, $1);
for (@tok)
{
$self->fail ($attr, 100, "undefined NOTATION [$_] in ATTLIST")
unless exists $checker->{NOTATION}->{$_};
}
my $re = join ("|", @tok);
$self->{NotationRE} = "^($re)\$";
$c2 = 'NotationType';
}
elsif ($type =~ /^\s*\(\s*($XML::RegExp::NmToken(\s*\|\s*$XML::RegExp::NmToken)*)\s*\)\s*$/)
{
# Enumeration
$self->fail ($attr, 136, "empty Enumeration list in ATTLIST")
unless defined $1;
my @tok = split (/\s*\|\s*/, $1);
for (@tok)
{
$self->fail ($attr, 134,
"invalid Enumeration value [$_] in ATTLIST")
unless $_ =~ /^$XML::RegExp::NmToken$/;
}
$self->{EnumRE}->{$attr} = '^(' . join ("|", @tok) . ')$'; #';
$c2 = 'Enumeration';
}
else
{
$self->fail ($attr, 137, "invalid ATTLIST type [$type]");
}
}
$self->{Check1}->{$attr} = $c1 if $c1;
$self->{Check2}->{$attr} = $c2 if $c2;
}
sub fail
{
my $self = shift;
my $attr = shift;
$self->{Checker}->fail (@_, Element => $self->{Elem}, Attr => $attr);
}
sub check
{
my ($self, $attr) = @_;
my $func1 = $self->{Check1}->{$attr};
my $func2 = $self->{Check2}->{$attr};
# print "check func1=$func1 func2=$func2 @_\n";
if (exists $self->{ReqNotSeen}->{$attr})
{
delete $self->{ReqNotSeen}->{$attr};
}
no strict;
&$func1 (@_) if defined $func1;
&$func2 (@_) if defined $func2;
}
# Copies the list of all required attributes from $self->{Required} to
# $self->{ReqNotSeen}.
# When check() encounters a required attribute, it is removed from ReqNotSeen.
# In EndAttr we look at which attribute names are still in ReqNotSeen - those
# are the ones that were not specified and are, therefore, in error.
sub StartAttr
{
my $self = shift;
my %not_seen = %{ $self->{Required} };
$self->{ReqNotSeen} = \%not_seen;
}
# Checks which of the #REQUIRED attributes were not specified
sub EndAttr
{
my $self = shift;
for my $attr (keys %{ $self->{ReqNotSeen} })
{
$self->fail ($attr, 159,
"unspecified value for \#REQUIRED attribute [$attr]");
}
}
sub FIXED
{
my ($self, $attr, $val, $specified) = @_;
my $default = $self->{Default}->{$attr};
$self->fail ($attr, 150,
"bad \#FIXED attribute value [$val], it should be [$default]")
unless ($val eq $default);
}
sub IMPLIED
{
my ($self, $attr, $val, $specified) = @_;
#?? should #IMPLIED be specified?
$self->fail ($attr, 158,
"unspecified value for \#IMPLIED attribute [$attr]")
unless $specified;
#?? Implied handler ?
}
# This is called when an attribute is passed to the check() method by
# XML::Checker::Attr(), i.e. when the attribute was specified explicitly
# or defaulted by the parser (which should never happen), *NOT* when the
# attribute was omitted. (The latter is checked by StartAttr/EndAttr)
sub REQUIRED
{
my ($self, $attr, $val, $specified) = @_;
# print "REQUIRED attr=$attr val=$val spec=$specified\n";
$self->fail ($attr, 159,
"unspecified value for \#REQUIRED attribute [$attr]")
unless $specified;
}
sub ID # must be #IMPLIED or #REQUIRED
{
my ($self, $attr, $val, $specified) = @_;
$self->fail ($attr, 131, "invalid ID [$val]")
unless $val =~ /^$XML::RegExp::Name$/;
$self->fail ($attr, 111, "ID [$val] already defined")
if $self->{Checker}->{ID}->{$val}++;
}
sub IDREF
{
my ($self, $attr, $val, $specified) = @_;
$self->fail ($attr, 132, "invalid IDREF [$val]")
unless $val =~ /^$XML::RegExp::Name$/;
$self->{Checker}->{IDREF}->{$val}++;
}
sub IDREFS
{
my ($self, $attr, $val, $specified) = @_;
for (split /\s+/, $val)
{
$self->IDREF ($attr, $_);
}
}
sub ENTITY
{
my ($self, $attr, $val, $specified) = @_;
#?? should it be specified?
$self->fail ($attr, 133, "invalid ENTITY name [$val]")
unless $val =~ /^$XML::RegExp::Name$/;
$self->fail ($attr, 102, "undefined unparsed ENTITY [$val]")
unless exists $self->{Checker}->{Unparsed}->{$val};
}
sub ENTITIES
{
my ($self, $attr, $val, $specified) = @_;
for (split /\s+/, $val)
{
$self->ENTITY ($attr, $_);
}
}
sub NMTOKEN
{
my ($self, $attr, $val, $specified) = @_;
$self->fail ($attr, 130, "invalid NMTOKEN [$val]")
unless $val =~ /^$XML::RegExp::NmToken$/;
}
sub NMTOKENS
{
my ($self, $attr, $val, $specified) = @_;
for (split /\s+/, $val)
{
$self->NMTOKEN ($attr, $_, $specified);
}
}
sub Enumeration
{
my ($self, $attr, $val, $specified) = @_;
my $re = $self->{EnumRE}->{$attr};
$self->fail ($attr, 160, "invalid Enumeration value [$val]")
unless $val =~ /$re/;
}
sub NotationType
{
my ($self, $attr, $val, $specified) = @_;
my $re = $self->{NotationRE};
$self->fail ($attr, 161, "invalid NOTATION value [$val]")
unless $val =~ /$re/;
$self->fail ($attr, 162, "undefined NOTATION [$val]")
unless exists $self->{Checker}->{NOTATION}->{$val};
}
package XML::Checker;
use vars qw ( $VERSION $FAIL $INSIGNIF_WS );
BEGIN
{
$VERSION = '0.13';
}
$FAIL = \&print_error;
# Whether the last seen Char data was insignicant whitespace
$INSIGNIF_WS = 0;
sub new
{
my ($class, %args) = @_;
$args{ERule} = {};
$args{ARule} = {};
$args{InCDATA} = 0;
#$args{Debug} = 1;
bless \%args, $class;
}
# PerlSAX API
sub element_decl
{
my ($self, $hash) = @_;
$self->Element ($hash->{Name}, $hash->{Model});
}
# Same parameter order as the Element handler in XML::Parser module
sub Element
{
my ($self, $name, $model) = @_;
if (defined $self->{ERule}->{$name})
{
$self->fail (115, "ELEMENT [$name] already defined",
Element => $name);
}
if ($model eq "EMPTY")
{
$self->{ERule}->{$name} = new XML::Checker::ERule::EMPTY;
}
elsif ($model eq "ANY")
{
$self->{ERule}->{$name} = new XML::Checker::ERule::ANY;
}
elsif ($model =~ /#PCDATA/)
{
my $rule = new XML::Checker::ERule::Mixed;
if ($rule->setModel ($model))
{
$self->{ERule}->{$name} = $rule;
}
else
{
$self->fail (124, "bad model [$model] for ELEMENT [$name]",
Element => $name);
}
}
else
{
my $rule = new XML::Checker::ERule::Children;
if ($rule->setModel ($model))
{
$self->{ERule}->{$name} = $rule;
}
else
{
$self->fail (124, "bad model [$model] for ELEMENT [$name]",
Element => $name);
}
}
my $rule = $self->{ERule}->{$name};
print "added ELEMENT model for $name: " . $rule->debug . "\n"
if $rule and $self->{Debug};
}
# PerlSAX API
sub attlist_decl
{
my ($self, $hash) = @_;
$self->Attlist ($hash->{ElementName}, $hash->{AttributeName},
$hash->{Type}, $hash->{Default}, $hash->{Fixed});
}
sub Attlist
{
my ($self, $tag, $attrName, $type, $default, $fixed) = @_;
my $arule = $self->{ARule}->{$tag} ||=
new XML::Checker::ARule ($tag, $self);
$arule->Attlist ($attrName, $type, $default, $fixed, $self);
}
# Initializes the context stack to check an XML::DOM::Element
sub InitDomElem
{
my $self = shift;
# initialize Context stack
$self->{Context} = [ new XML::Checker::Context::ANY ($self) ];
$self->{InCDATA} = 0;
}
# Clears the context stack after checking an XML::DOM::Element
sub FinalDomElem
{
my $self = shift;
delete $self->{Context};
}
# PerlSAX API
sub start_document
{
shift->Init;
}
sub Init
{
my $self = shift;
# initialize Context stack
$self->{Context} = [ new XML::Checker::DocContext ($self) ];
$self->{InCDATA} = 0;
}
# PerlSAX API
sub end_document
{
shift->Final;
}
sub Final
{
my $self = shift;
#?? could add more statistics: unreferenced Unparsed, ID
for (keys %{ $self->{IDREF} })
{
my $n = $self->{IDREF}->{$_};
$self->fail (200, "undefined ID [$_] was referenced [$n] times")
unless defined $self->{ID}->{$_};
}
for (keys %{ $self->{ID} })
{
my $n = $self->{IDREF}->{$_} || 0;
$self->fail (300, "[$n] references to ID [$_]");
}
delete $self->{Context};
}
sub getRootElement
{
my $self = shift;
# print "getRoot $self " . $self->{RootElement} . "\n";
$_[0]->{RootElement};
}
# PerlSAX API
sub doctype_decl
{
my ($self, $hash) = @_;
$self->Doctype ($hash->{Name}, $hash->{SystemId},
$hash->{PublicId}, $hash->{Internal});
}
sub Doctype
{
my ($self, $name, $sysid, $pubid, $internal) = @_;
$self->{RootElement} = $name;
my $context = $self->{Context}->[0];
$context->setRootElement ($name);
#?? what else
}
sub Attr
{
my ($self, $tag, $attr, $val, $specified) = @_;
#print "Attr for tag=$tag attr=$attr val=$val spec=$specified\n";
my $arule = $self->{ARule}->{$tag};
if (defined $arule && $arule->{Defined}->{$attr})
{
$arule->check ($attr, $val, $specified);
}
else
{
$self->fail (103, "undefined attribute [$attr]", Element => $tag);
}
}
sub EndAttr
{
my $self = shift;
my $arule = $self->{CurrARule};
if (defined $arule)
{
$arule->EndAttr;
}
}
# PerlSAX API
sub start_element
{
my ($self, $hash) = @_;
my $tag = $hash->{Name};
my $attr = $hash->{Attributes};
$self->Start ($tag);
if (exists $hash->{AttributeOrder})
{
my $defaulted = $hash->{Defaulted};
my @order = @{ $hash->{AttributeOrder} };
# Specified attributes
for (my $i = 0; $i < $defaulted; $i++)
{
my $a = $order[$i];
$self->Attr ($tag, $a, $attr->{$a}, 1);
}
# Defaulted attributes
for (my $i = $defaulted; $i < @order; $i++)
{
my $attr = $order[$i];
$self->Attr ($tag, $a, $attr->{$a}, 0);
}
}
else
{
# Assume all attributes were specified
my @attr = %$attr;
my ($key, $val);
while ($key = shift @attr)
{
$val = shift @attr;
$self->Attr ($tag, $key, $val, 1);
}
}
$self->EndAttr;
}
sub Start
{
my ($self, $tag) = @_;
#?? if first tag, check with root element - or does expat check this already?
my $context = $self->{Context};
$context->[0]->Start ($self, $tag);
my $erule = $self->{ERule}->{$tag};
if (defined $erule)
{
unshift @$context, $erule->context;
}
else
{
# It's not a real error according to the XML Spec.
$self->fail (101, "undefined ELEMENT [$tag]");
unshift @$context, new XML::Checker::Context::ANY;
}
#?? what about ARule ??
my $arule = $self->{ARule}->{$tag};
if (defined $arule)
{
$self->{CurrARule} = $arule;
$arule->StartAttr;
}
}
# PerlSAX API
sub end_element
{
shift->End;
}
sub End
{
my ($self) = @_;
my $context = $self->{Context};
$context->[0]->End ($self);
shift @$context;
}
# PerlSAX API
sub characters
{
my ($self, $hash) = @_;
my $data = $hash->{Data};
if ($self->{InCDATA})
{
$self->CData ($data);
}
else
{
$self->Char ($data);
}
}
# PerlSAX API
sub start_cdata
{
$_[0]->{InCDATA} = 1;
}
# PerlSAX API
sub end_cdata
{
$_[0]->{InCDATA} = 0;
}
sub Char
{
my ($self, $text) = @_;
my $context = $self->{Context};
# NOTE: calls to isWS may set this to 1.
$INSIGNIF_WS = 0;
$context->[0]->Char ($self, $text);
}
# Treat CDATASection same as Char (Text)
sub CData
{
my ($self, $cdata) = @_;
my $context = $self->{Context};
$context->[0]->Char ($self, $cdata);
# CDATASection can never be insignificant whitespace
$INSIGNIF_WS = 0;
#?? I'm not sure if this assumption is correct
}
# PerlSAX API
sub comment
{
my ($self, $hash) = @_;
$self->Comment ($hash->{Data});
}
sub Comment
{
# ?? what can be checked here?
}
# PerlSAX API
sub entity_reference
{
my ($self, $hash) = @_;
$self->EntityRef ($hash->{Name}, 0);
#?? parameter entities (like %par;) are NOT supported!
# PerlSAX::handle_default should be fixed!
}
sub EntityRef
{
my ($self, $ref, $isParam) = @_;
if ($isParam)
{
# expand to "%name;"
print STDERR "XML::Checker::Entity - parameter Entity (%ent;) not implemented\n";
}
else
{
# Treat same as Char - for now
my $context = $self->{Context};
$context->[0]->Char ($self, "&$ref;");
$INSIGNIF_WS = 0;
#?? I could count the number of times each Entity is referenced
}
}
# PerlSAX API
sub unparsed_entity_decl
{
my ($self, $hash) = @_;
$self->Unparsed ($hash->{Name});
#?? what about Base, SytemId, PublicId ?
}
sub Unparsed
{
my ($self, $entity) = @_;
# print "ARule::Unparsed $entity\n";
if ($self->{Unparsed}->{$entity})
{
$self->fail (112, "unparsed ENTITY [$entity] already defined");
}
else
{
$self->{Unparsed}->{$entity} = 1;
}
}
# PerlSAX API
sub notation_decl
{
my ($self, $hash) = @_;
$self->Notation ($hash->{Name});
#?? what about Base, SytemId, PublicId ?
}
sub Notation
{
my ($self, $notation) = @_;
if ($self->{NOTATION}->{$notation})
{
$self->fail (113, "NOTATION [$notation] already defined");
}
else
{
$self->{NOTATION}->{$notation} = 1;
}
}
# PerlSAX API
sub entity_decl
{
my ($self, $hash) = @_;
$self->Entity ($hash->{Name}, $hash->{Value}, $hash->{SystemId},
$hash->{PublicId}, $hash->{'Notation'});
}
sub Entity
{
my ($self, $name, $val, $sysId, $pubId, $ndata) = @_;
if (exists $self->{ENTITY}->{$name})
{
$self->fail (114, "ENTITY [$name] already defined");
}
else
{
$self->{ENTITY}->{$name} = $val;
}
}
# PerlSAX API
#sub xml_decl {} $hash=> Version, Encoding, Standalone
# Don't implement resolve_entity() which is called by ExternEnt!
#sub processing_instruction {} $hash=> Target, Data
# Returns whether the Char data is whitespace and also updates the
# $INSIGNIF_WS variable to indicate whether it is insignificant whitespace.
# Note that this method is only called in places where potential whitespace
# can be insignificant (i.e. when the ERule is Children or EMPTY)
sub isWS
{
# pverdret: why not /\A\s+\Z/ ???
$INSIGNIF_WS = ($_[1] =~ /^\s*$/);
}
sub isInsignifWS
{
$INSIGNIF_WS;
}
sub fail
{
my $self = shift;
&$FAIL (@_);
}
sub print_error # static
{
my $str = error_string (@_);
print STDERR $str;
}
sub error_string # static
{
my $code = shift;
my $msg = shift;
my @a = ();
my ($key, $val);
while ($key = shift)
{
$val = shift;
push @a, ("$key " . (defined $val ? $val : "(undef)"));
}
my $cat = $code >= 200 ? ($code >= 300 ? "INFO" : "WARNING") : "ERROR";
my $str = join (", ", @a);
$str = length($str) ? "\tContext: $str\n" : "";
"XML::Checker $cat-$code: $msg\n$str";
}
sub debug
{
my ($self) = @_;
my $context = $self->{Context}->[0];
my $c = $context ? $context->debug : "no context";
my $root = $self->{RootElement};
"Checker[$c,RootElement=$root]";
}
1; # package return code
__END__
=head1 NAME
XML::Checker - A perl module for validating XML
=head1 SYNOPSIS
L<XML::Checker::Parser> - an L<XML::Parser> that validates at parse time
L<XML::DOM::ValParser> - an L<XML::DOM::Parser> that validates at parse time
(Some of the package names may change! This is only an alpha release...)
=head1 DESCRIPTION
XML::Checker can be used in different ways to validate XML. See the manual
pages of L<XML::Checker::Parser> and L<XML::DOM::ValParser>
for more information.
This document only describes common topics like error handling
and the XML::Checker class itself.
WARNING: Not all errors are currently checked. Almost everything is subject to
change. Some reported errors may not be real errors. For production code,
it is recommended that you use L<XML::LibXML> or L<XML::GDOME> instead of
L<XML::Checker>. Both modules share the same DTD validation code with libxml2
and L<XML::LibXML> is easier to install.
=head1 ERROR HANDLING
Whenever XML::Checker (or one of the packages that uses XML::Checker) detects a
potential error, the 'fail handler' is called. It is currently also called
to report information, like how many times an Entity was referenced.
(The whole error handling mechanism is subject to change, I'm afraid...)
The default fail handler is XML::Checker::print_error(), which prints an error
message to STDERR. It does not stop the XML::Checker, so it will continue
looking for other errors.
The error message is created with XML::Checker::error_string().
You can define your
own fail handler in two ways, locally and globally. Use a local variable to
temporarily override the fail handler. This way the default fail handler is restored
when the local variable goes out of scope, esp. when exceptions are thrown e.g.
# Using a local variable to temporarily override the fail handler (preferred)
{ # new block - start of local scope
local $XML::Checker::FAIL = \&my_fail;
... your code here ...
} # end of block - the previous fail handler is restored
You can also set the error handler globally, risking that your code may not
be reusable or may clash with other modules that use XML::Checker.
# Globally setting the fail handler (not recommended)
$XML::Checker::FAIL = \&my_fail;
... rest of your code ...
The fail handler is called with the following parameters ($code, $msg, @context),
where $code is the error code, $msg is the error description and
@context contains information on where the error occurred. The @context is
a (ordered) list of (key,value) pairs and can easily be turned into a hash.
It contains the following information:
Element - tag name of Element node (if applicable)
Attr - attribute name (if applicable)
ChildElementIndex - if applicable (see error 157)
line - only when parsing
column - only when parsing
byte - only when parsing (-1 means: end of file)
Some examples of fail handlers:
# Don't print info messages
sub my_fail
{
my $code = shift;
print STDERR XML::Checker::error_message ($code, @_)
if $code < 300;
}
# Die when the first error is encountered - this will stop
# the parsing process. Ignore information messages.
sub my_fail
{
my $code = shift;
die XML::Checker::error_message ($code, @_) if $code < 300;
}
# Count the number of undefined NOTATION references
# and print the error as usual
sub my_fail
{
my $code = shift;
$count_undef_notations++ if $code == 100;
XML::Checker::print_error ($code, @_);
}
# Die when an error is encountered.
# Don't die if a warning or info message is encountered, just print a message.
sub my_fail {
my $code = shift;
die XML::Checker::error_string ($code, @_) if $code < 200;
XML::Checker::print_error ($code, @_);
}
=head1 INSIGNIFICANT WHITESPACE
XML::Checker keeps track of whether whitespace found in character data
is significant or not. It is considered insignicant if it is found inside
an element that has a ELEMENT rule that is not of type Mixed or of type ANY.
(A Mixed ELEMENT rule does contains the #PCDATA keyword.
An ANY rule contains the ANY keyword. See the XML spec for more info.)
XML::Checker can not determine whether whitespace is insignificant in those two
cases, because they both allow regular character data to appear within
XML elements and XML::Checker can therefore not deduce whether whitespace
is part of the actual data or was just added for readability of the XML file.
XML::Checker::Parser and XML::DOM::ValParser both have the option to skip
insignificant whitespace when setting B<SkipInsignifWS> to 1 in their constructor.
If set, they will not call the Char handler when insignificant whitespace is
encountered. This means that in XML::DOM::ValParser no Text nodes are created
for insignificant whitespace.
Regardless of whether the SkipInsignifWS options is set, XML::Checker always
keeps track of whether whitespace is insignificant. After making a call to
XML::Checker's Char handler, you can find out if it was insignificant whitespace
by calling the isInsignifWS method.
When using multiple (nested) XML::Checker instances or when using XML::Checker
without using XML::Checker::Parser or XML::DOM::ValParser (which hardly anybody
probably will), make sure to set a local variable in the scope of your checking
code, e.g.
{ # new block - start of local scope
local $XML::Checker::INSIGNIF_WS = 0;
... insert your code here ...
} # end of scope
=head1 ERROR CODES
There are 3 categories, errors, warnings and info messages.
(The codes are still subject to change, as well the error descriptions.)
Most errors have a link to the appropriate Validaty Constraint (B<VC>)
or other section in the XML specification.
=head2 ERROR Messages
=head2 100 - 109
=over 4
=item *
B<100> - undefined NOTATION [$notation] in ATTLIST
The ATTLIST contained a Notation reference that was not defined in a
NOTATION definition.
B<VC:> L<Notation Attributes|http://www.w3.org/TR/REC-xml#notatn>
=item *
B<101> - undefined ELEMENT [$tagName]
The specified Element was never defined in an ELEMENT definition.
This is not an error according to the XML spec.
See L<Element Type Declarations|http://www.w3.org/TR/REC-xml#elemdecls>
=item *
B<102> - undefined unparsed ENTITY [$entity]
The attribute value referenced an undefined unparsed entity.
B<VC:> L<Entity Name|http://www.w3.org/TR/REC-xml#entname>
=item *
B<103> - undefined attribute [$attrName]
The specified attribute was not defined in an ATTLIST for that Element.
B<VC:> L<Attribute Value Type|http://www.w3.org/TR/REC-xml#ValueType>
=back
=head2 110 - 119
=over 4
=item *
B<110> - attribute [$attrName] of element [$tagName] already defined
The specified attribute was already defined in this ATTLIST definition or
in a previous one.
This is not an error according to the XML spec.
See L<Attribute-List Declarations|http://www.w3.org/TR/REC-xml#attdecls>
=item *
B<111> - ID [$value] already defined
An ID with the specified value was already defined in an attribute
within the same document.
B<VC:> L<ID|http://www.w3.org/TR/REC-xml#id>
=item *
B<112> - unparsed ENTITY [$entity] already defined
This is not an error according to the XML spec.
See L<Entity Declarations|http://www.w3.org/TR/REC-xml#sec-entity-decl>
=item *
B<113> - NOTATION [$notation] already defined
=item *
B<114> - ENTITY [$entity] already defined
This is not an error according to the XML spec.
See L<Entity Declarations|http://www.w3.org/TR/REC-xml#sec-entity-decl>
=item *
B<115> - ELEMENT [$name] already defined
B<VC:> L<Unique Element Type Declaration|http://www.w3.org/TR/REC-xml#EDUnique>
=back
=head2 120 - 129
=over 4
=item *
B<120> - invalid default ENTITY [$default]
(Or IDREF or NMTOKEN instead of ENTITY.)
The ENTITY, IDREF or NMTOKEN reference in the default attribute
value for an attribute with types ENTITY, IDREF or NMTOKEN was not
valid.
B<VC:> L<Attribute Default Legal|http://www.w3.org/TR/REC-xml#defattrvalid>
=item *
B<121> - invalid default [$token] in ENTITIES [$default]
(Or IDREFS or NMTOKENS instead of ENTITIES)
One of the ENTITY, IDREF or NMTOKEN references in the default attribute
value for an attribute with types ENTITIES, IDREFS or NMTOKENS was not
valid.
B<VC:> L<Attribute Default Legal|http://www.w3.org/TR/REC-xml#defattrvalid>
=item *
B<122> - invalid default attribute value [$default]
The specified default attribute value is not a valid attribute value.
B<VC:> L<Attribute Default Legal|http://www.w3.org/TR/REC-xml#defattrvalid>
=item *
B<123> - invalid default ID [$default], must be #REQUIRED or #IMPLIED
The default attribute value for an attribute of type ID has to be
#REQUIRED or #IMPLIED.
B<VC:> L<ID Attribute Default|http://www.w3.org/TR/REC-xml#id-default>
=item *
B<124> - bad model [$model] for ELEMENT [$name]
The model in the ELEMENT definition did not conform to the XML syntax
for Mixed models.
See L<Mixed Content|http://www.w3.org/TR/REC-xml#sec-mixed-content>
=back
=head2 130 - 139
=over 4
=item *
B<130> - invalid NMTOKEN [$attrValue]
The attribute value is not a valid NmToken token.
B<VC:> L<Enumeration|http://www.w3.org/TR/REC-xml#enum>
=item *
B<131> - invalid ID [$attrValue]
The specified attribute value is not a valid Name token.
B<VC:> L<ID|http://www.w3.org/TR/REC-xml#id>
=item *
B<132> - invalid IDREF [$value]
The specified attribute value is not a valid Name token.
B<VC:> L<IDREF|http://www.w3.org/TR/REC-xml#idref>
=item *
B<133> - invalid ENTITY name [$name]
The specified attribute value is not a valid Name token.
B<VC:> L<Entity Name|http://www.w3.org/TR/REC-xml#entname>
=item *
B<134> - invalid Enumeration value [$value] in ATTLIST
The specified value is not a valid NmToken (see XML spec for def.)
See definition of L<NmToken|http://www.w3.org/TR/REC-xml#NT-Nmtoken>
=item *
B<135> - empty NOTATION list in ATTLIST
The NOTATION list of the ATTLIST definition did not contain any NOTATION
references.
See definition of L<NotationType|http://www.w3.org/TR/REC-xml#NT-NotationType>
=item *
B<136> - empty Enumeration list in ATTLIST
The ATTLIST definition of the attribute of type Enumeration did not
contain any values.
See definition of L<Enumeration|http://www.w3.org/TR/REC-xml#NT-Enumeration>
=item *
B<137> - invalid ATTLIST type [$type]
The attribute type has to be one of: ID, IDREF, IDREFS, ENTITY, ENTITIES,
NMTOKEN, NMTOKENS, CDATA, NOTATION or an Enumeration.
See definition of L<AttType|http://www.w3.org/TR/REC-xml#NT-AttType>
=back
=head2 149 - 159
=over 4
=item *
B<149> - invalid text content [$value]
A text was found in an element that should only include sub-elements
The text is not made of non-significant whitespace.
B<150> - bad #FIXED attribute value [$value], it should be [$default]
The specified attribute was defined as #FIXED in the ATTLIST definition
and the found attribute $value differs from the specified $default value.
B<VC:> L<Fixed Attribute Default|http://www.w3.org/TR/REC-xml#FixedAttr>
=item *
B<151> - only one ID allowed in ATTLIST per element first=[$attrName]
The ATTLIST definitions for an Element may contain only one attribute
with the type ID. The specified $attrName is the one that was found first.
B<VC:> L<One ID per Element Type|http://www.w3.org/TR/REC-xml#one-id-per-el>
=item *
B<152> - Element should be EMPTY, found Element [$tagName]
The ELEMENT definition for the specified Element said it should be
EMPTY, but a child Element was found.
B<VC:> L<Element Valid (sub1)|http://www.w3.org/TR/REC-xml#elementvalid>
=item *
B<153> - Element should be EMPTY, found text [$text]
The ELEMENT definition for the specified Element said it should be
EMPTY, but text was found. Currently, whitespace is not allowed between the
open and close tag. (This may be wrong, please give feedback.)
To allow whitespace (subject to change), set:
$XML::Checker::Context::EMPTY::ALLOW_WHITE_SPACE = 1;
B<VC:> L<Element Valid (sub1)|http://www.w3.org/TR/REC-xml#elementvalid>
=item *
B<154> - bad order of Elements Found=[$found] RE=[$re]
The child elements of the specified Element did not match the
regular expression found in the ELEMENT definition. $found contains
a comma separated list of all the child element tag names that were found.
$re contains the (decoded) regular expression that was used internally.
B<VC:> L<Element Valid|http://www.w3.org/TR/REC-xml#elementvalid>
=item *
B<155> - more than one root Element [$tags]
An XML Document may only contain one Element.
$tags is a comma separated list of element tag names encountered sofar.
L<XML::Parser> (expat) throws 'no element found' exception.
See two_roots.xml for an example.
See definition of L<document|http://www.w3.org/TR/REC-xml#dt-root>
=item *
B<156> - unexpected root Element [$tagName], expected [$rootTagName]
The tag name of the root Element of the XML Document differs from the name
specified in the DOCTYPE section.
L<XML::Parser> (expat) throws 'not well-formed' exception.
See bad_root.xml for an example.
B<VC:> L<Root Element Type|http://www.w3.org/TR/REC-xml#vc-roottype>
=item *
B<157> - unexpected Element [$tagName]
The ELEMENT definition for the specified Element does not allow child
Elements with the specified $tagName.
B<VC:> L<Element Valid|http://www.w3.org/TR/REC-xml#elementvalid>
The error context contains ChildElementIndex which is the index within
its parent Element (counting only Element nodes.)
=item *
B<158> - unspecified value for #IMPLIED attribute [$attrName]
The ATTLIST for the specified attribute said the attribute was #IMPLIED,
which means the user application should supply a value, but the attribute
value was not specified. (User applications should pass a value and set
$specified to 1 in the Attr handler.)
=item *
B<159> - unspecified value for #REQUIRED attribute [$attrName]
The ATTLIST for the specified attribute said the attribute was #REQUIRED,
which means that a value should have been specified.
B<VC:> L<Required Attribute|http://www.w3.org/TR/REC-xml#RequiredAttr>
=back
=head2 160 - 169
=over 4
=item *
B<160> - invalid Enumeration value [$attrValue]
The specified attribute value does not match one of the Enumeration values
in the ATTLIST.
B<VC:> L<Enumeration|http://www.w3.org/TR/REC-xml#enum>
=item *
B<161> - invalid NOTATION value [$attrValue]
The specified attribute value was not found in the list of possible NOTATION
references as found in the ATTLIST definition.
B<VC:> L<Notation Attributes|http://www.w3.org/TR/REC-xml#notatn>
=item *
B<162> - undefined NOTATION [$attrValue]
The NOTATION referenced by the specified attribute value was not defined.
B<VC:> L<Notation Attributes|http://www.w3.org/TR/REC-xml#notatn>
=back
=head2 WARNING Messages (200 and up)
=over 4
=item *
B<200> - undefined ID [$id] was referenced [$n] times
The specified ID was referenced $n times, but never defined in an attribute
value with type ID.
B<VC:> L<IDREF|http://www.w3.org/TR/REC-xml#idref>
=back
=head2 INFO Messages (300 and up)
=over 4
=item *
B<300> - [$n] references to ID [$id]
The specified ID was referenced $n times.
=back
=head2 Not checked
The following errors are already checked by L<XML::Parser> (expat) and
are currently not checked by XML::Checker:
(?? TODO - add more info)
=over 4
=item root element is missing
L<XML::Parser> (expat) throws 'no element found' exception.
See no_root.xml for an example.
=back
=head1 XML::Checker
XML::Checker can be easily plugged into your application.
It uses mostly the same style of event handlers (or callbacks) as L<XML::Parser>.
See L<XML::Parser> manual page for descriptions of most handlers.
It also implements PerlSAX style event handlers. See L<PerlSAX interface>.
Currently, the XML::Checker object is a blessed hash with the following
(potentially useful) entries:
$checker->{RootElement} - root element name as found in the DOCTYPE
$checker->{NOTATION}->{$notation} - is 1 if the NOTATION was defined
$checker->{ENTITY}->{$name} - contains the (first) ENTITY value if defined
$checker->{Unparsed}->{$entity} - is 1 if the unparsed ENTITY was defined
$checker->{ID}->{$id} - is 1 if the ID was defined
$checker->{IDREF}->{$id} - number of times the ID was referenced
# Less useful:
$checker->{ERule}->{$tag} - the ELEMENT rules by Element tag name
$checker->{ARule}->{$tag} - the ATTLIST rules by Element tag name
$checker->{Context} - context stack used internally
$checker->{CurrARule} - current ATTLIST rule for the current Element
=head2 XML:Checker methods
This section is only interesting when using XML::Checker directly.
XML::Checker supports most event handlers that L<XML::Parser> supports with minor
differences. Note that the XML::Checker event handler methods are
instance methods and not static, so don't forget to call them like this,
without passing $expat (as in the L<XML::Parser>) handlers:
$checker->Start($tagName);
=over 4
=item Constructor
$checker = new XML::Checker;
$checker = new XML::Checker (%user_args);
User data may be stored by client applications. Only $checker->{User} is
guaranteed not to clash with internal hash keys.
=item getRootElement ()
$tagName = $checker->getRootElement;
Returns the root element name as found in the DOCTYPE
=back
=head2 Expat interface
XML::Checker supports what I call the I<Expat> interface, which is
the collection of methods you normally specify as the callback handlers
when using XML::Parser.
Only the following L<XML::Parser> handlers are currently supported:
Init, Final, Char, Start, End, Element, Attlist, Doctype,
Unparsed, Entity, Notation.
I don't know how to correctly support the Default handler for all L<XML::Parser>
releases. The Start handler works a little different (see below) and I
added Attr, InitDomElem, FinalDomElem, CDATA and EntityRef handlers.
See L<XML::Parser> for a description of the handlers that are not listed below.
Note that this interface may disappear, when the PerlSAX interface stabilizes.
=over 4
=item Start ($tag)
$checker->Start($tag);
Call this when an Element with the specified $tag name is encountered.
Different from the Start handler in L<XML::Parser>, in that no attributes
are passed in (use the Attr handler for those.)
=item Attr ($tag, $attrName, $attrValue, $isSpecified)
$checker->Attr($tag,$attrName,$attrValue,$spec);
Checks an attribute with the specified $attrName and $attrValue against the
ATTLIST definition of the element with the specified $tag name.
$isSpecified means whether the attribute was specified (1) or defaulted (0).
=item EndAttr ()
$checker->EndAttr;
This should be called after all attributes are passed with Attr().
It will check which of the #REQUIRED attributes were not specified and generate
the appropriate error (159) for each one that is missing.
=item CDATA ($text)
$checker->CDATA($text);
This should be called whenever CDATASections are encountered.
Similar to Char handler (but might perform different checks later...)
=item EntityRef ($entity, $isParameterEntity)
$checker->EntityRef($entity,$isParameterEntity);
Checks the ENTITY reference. Set $isParameterEntity to 1 for
entity references that start with '%'.
=item InitDomElem () and FinalDomElem ()
Used by XML::DOM::Element::check() to initialize (and cleanup) the
context stack when checking a single element.
=back
=head2 PerlSAX interface
XML::Checker now also supports the PerlSAX interface, so you can use XML::Checker
wherever you use PerlSAX handlers.
XML::Checker implements the following methods: start_document, end_document,
start_element, end_element, characters, processing_instruction, comment,
start_cdata, end_cdata, entity_reference, notation_decl, unparsed_entity_decl,
entity_decl, element_decl, attlist_decl, doctype_decl, xml_decl
Not implemented: set_document_locator, ignorable_whitespace
See PerlSAX.pod for details. (It is called lib/PerlSAX.pod in the libxml-perl
distribution which can be found at CPAN.)
=head1 CAVEATS
This is an alpha release. It is not actively maintained, patches are accepted and
incoporated in new releases, but that's about it. If you are interested in taking
over maintimance of the module, email tjmather@tjmather.com.
For a much faster, and correct DTD validator, see L<XML::LibXML>. It
uses the libxml2 library to validate DTD.
=head1 AUTHOR
Enno Derksen is the original author.
Send patches to T.J. Mather at
<F<tjmather@tjmather.com>>.
=head1 SEE ALSO
L<XML::LibXML> provides validating parsers against a DTD
and is recommended over XML::Checker since it uses the libxml2 library which is
fast and well-tested.
The XML spec (Extensible Markup Language 1.0) at L<http://www.w3.org/TR/REC-xml>
The L<XML::Parser> and L<XML::Parser::Expat> manual pages.
The other packages that come with XML::Checker:
L<XML::Checker::Parser>, L<XML::DOM::ValParser>
The DOM Level 1 specification at L<http://www.w3.org/TR/REC-DOM-Level-1>
The PerlSAX specification. It is currently in lib/PerlSAX.pod in the
libxml-perl distribution by Ken MacLeod.
The original SAX specification (Simple API for XML) can be found at
L<http://www.megginson.com/SAX> and L<http://www.megginson.com/SAX/SAX2>
|