| 1 |
#!/usr/local/bin/perl |
| 2 |
|
| 3 |
|
| 4 |
use strict; |
| 5 |
use Getopt::Long; |
| 6 |
use XML::Checker; |
| 7 |
use XML::Checker::Parser; |
| 8 |
|
| 9 |
|
| 10 |
my %expat_options = (KeepCDATA => 1);#, Handlers => { Unparsed => \&my_Unparsed_handler }); |
| 11 |
my $parser = new XML::Checker::Parser (%expat_options); |
| 12 |
my %unique_h; |
| 13 |
|
| 14 |
unless (@ARGV) { |
| 15 |
usage(); |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
local $XML::Checker::FAIL = \&fail_handler; |
| 20 |
foreach my $f (@ARGV) { |
| 21 |
$parser->parsefile ($f); |
| 22 |
} |
| 23 |
|
| 24 |
map{print STDERR "$_\n"}values %unique_h; |
| 25 |
print STDERR "DONE\n"; |
| 26 |
|
| 27 |
my $status = scalar(keys %unique_h); |
| 28 |
exit $status; |
| 29 |
|
| 30 |
|
| 31 |
# code < 200, invalid xml element/attr? |
| 32 |
# code >= 200, a warning or info message? |
| 33 |
sub fail_handler { |
| 34 |
my $code = shift; |
| 35 |
my $msg = shift; |
| 36 |
my @context = @_; |
| 37 |
my $k = "ERROR-CODE=$code: $msg"; |
| 38 |
if ($code < 200) { |
| 39 |
$unique_h{$k} = sprintf("$k\n\tContext: %s",join(", ",@context)); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
sub usage { |
| 44 |
print <<EOM; |
| 45 |
|
| 46 |
$0 xml_file |
| 47 |
|
| 48 |
to validate xml using dtd specified in the xml file. |
| 49 |
Error code and offending element are printed in STDERR stream. |
| 50 |
it returns exit status to indicate if any error in xml. |
| 51 |
|
| 52 |
EOM |
| 53 |
|
| 54 |
} |