1: #!/usr/bin/perl
2:
3: # Validates a file or directory against loncapa.xsd with libxml2
4:
5: use strict;
6: use utf8;
7: use warnings;
8:
9: use File::Basename;
10: use Try::Tiny;
11: use XML::LibXML;
12:
13:
14: binmode(STDOUT, ':encoding(UTF-8)');
15: binmode(STDERR, ':encoding(UTF-8)');
16:
17: if (scalar(@ARGV) != 1) {
18: print STDERR "Usage: perl validate_xml.pl file|directory\n";
19: exit(1);
20: }
21:
22: # find the command-line argument encoding
23: use I18N::Langinfo qw(langinfo CODESET);
24: my $codeset = langinfo(CODESET);
25: use Encode qw(decode);
26: @ARGV = map { decode $codeset, $_ } @ARGV;
27:
28: my $pathname = "$ARGV[0]";
29:
30: my $script_dir = dirname(__FILE__);
31: my $xmlschema = XML::LibXML::Schema->new(location => $script_dir.'/loncapa.xsd');
32:
33: if (-d "$pathname") {
34: validate_dir($pathname);
35: } elsif (-f $pathname) {
36: validate_file($pathname);
37: }
38:
39:
40: # Validates a directory recursively, selecting only .(problem|exam|survey|html|library).xml files.
41: sub validate_dir {
42: my ($dirpath) = @_;
43:
44: opendir (my $dh, $dirpath) or die $!;
45: while (my $entry = readdir($dh)) {
46: next if ($entry =~ m/^\./); # ignore entries starting with a period
47: my $pathname = $dirpath.'/'.$entry;
48: if (-d $pathname) {
49: validate_dir($pathname);
50: } elsif (-f $pathname) {
51: if ($pathname =~ /\.(problem|exam|survey|html?|library)\.xml$/) {
52: validate_file($pathname);
53: }
54: }
55: }
56: closedir($dh);
57: }
58:
59: # Validates a file against loncapa.xsd with libxml2
60: sub validate_file {
61: my ($pathname) = @_;
62:
63: my $doc = XML::LibXML->load_xml(location => $pathname);
64: try {
65: $xmlschema->validate($doc);
66: print "$pathname is valid\n";
67: } catch {
68: $_ =~ s/%20/ /g;
69: print "$_\n";
70: }
71: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>