File: go-validate-xml

package info (click to toggle)
libgo-perl 0.15-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,112 kB
  • sloc: perl: 13,147; sh: 21; makefile: 7
file content (54 lines) | stat: -rwxr-xr-x 1,060 bytes parent folder | download | duplicates (8)
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
#!/usr/local/bin/perl


use strict;
use Getopt::Long;
use XML::Checker;
use XML::Checker::Parser;


my %expat_options = (KeepCDATA => 1);#, Handlers => { Unparsed => \&my_Unparsed_handler });
my $parser = new XML::Checker::Parser (%expat_options);
my %unique_h;

unless (@ARGV) {
    usage();
    exit;
}

local $XML::Checker::FAIL = \&fail_handler;
foreach my $f (@ARGV) {
    $parser->parsefile ($f);
}

map{print STDERR "$_\n"}values %unique_h;
print STDERR "DONE\n";

my $status = scalar(keys %unique_h);
exit $status;


# code < 200, invalid xml element/attr?
# code >= 200, a warning or info message?
sub fail_handler {
    my $code = shift;
    my $msg = shift;
    my @context = @_;
    my $k = "ERROR-CODE=$code: $msg";
    if ($code < 200) {
        $unique_h{$k} = sprintf("$k\n\tContext: %s",join(", ",@context));
    }
}

sub usage {
print <<EOM;

$0 xml_file

  to validate xml using dtd specified in the xml file.
  Error code and offending element are printed in STDERR stream.
  it returns exit status to indicate if any error in xml.

EOM

}