-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathvalidate-parlamint.pl
executable file
·169 lines (157 loc) · 5.93 KB
/
validate-parlamint.pl
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
#!/usr/bin/env perl
# Validate ParlaMint files
# Tomaž Erjavec <[email protected]>
# License: GNU GPL
use warnings;
use utf8;
use open ':utf8';
use File::Temp qw/ tempfile tempdir /; #creation of tmp files and directory
my $tempdirroot = "$Bin/tmp";
mkdir($tempdirroot) unless(-d $tempdirroot);
my $tmpDir = tempdir(DIR => $tempdirroot, CLEANUP => 1);
binmode(STDIN, ':utf8');
binmode(STDOUT, ':utf8');
binmode(STDERR, ':utf8');
sub usage
{
print STDERR ("Usage: validate-parlamint.pl <SchemaDirectory> '<InputDirectories>'\n");
print STDERR (" Produces a validation report on ParlaMint XML files in the <InputDirectories>:\n");
print STDERR (" * validation for illegal characters (like soft hyphen, PUA)\n");
print STDERR (" * validation against ParlaMint RNG schemas in <SchemaDirectory> (with jing)\n");
print STDERR (" * validation against ParlaMint ODD schema in <SchemaDirectory> (with jing)\n");
print STDERR (" * link (IDREF) checking (with saxon, check-links.xsl)\n");
print STDERR (" * content checking (with saxon, validate-parlamint.xsl)\n");
print STDERR (" - still separately, Dan's UD validation of CoNLL-U files (cf. Makefile)\n");
}
use Getopt::Long;
use FindBin qw($Bin);
use File::Spec;
$schemaDir = File::Spec->rel2abs(shift);
$inDirs = File::Spec->rel2abs(shift);
$Jing = "java -jar $Bin/bin/jing.jar";
$Saxon = "java -jar $Bin/bin/saxon.jar";
$Compose = "$Bin/parlamint-composite-teiHeader.xsl";
$Links = "$Bin/check-links.xsl";
$Valid = "$Bin/validate-parlamint.xsl";
$Valid_particDesc = "$Bin/validate-parlamint-particDesc.xsl";
$Includes = "$Bin/get-includes.xsl";
foreach my $inDir (glob "$inDirs") {
next unless -d $inDir;
print STDERR "INFO: Validating directory $inDir\n";
my $rootFile = '';
my $rootAnaFile = '';
my @compFiles = ();
my @compAnaFiles = ();
foreach $inFile (glob "$inDir/*.xml") {
if ($inFile =~ m|(ParlaMint-[A-Z]{2}(?:-[A-Z0-9]{1,3})?(?:-[a-z]{2,3})?\.xml)|) {
$fileName = $1;
$rootFile = $inFile;
}
elsif ($inFile =~ m|(ParlaMint-[A-Z]{2}(?:-[A-Z0-9]{1,3})?(?:-[a-z]{2,3})?\.ana\.xml)|) {
$fileNameAna = $1;
$rootAnaFile = $inFile
}
}
$/ = '>';
if (not $rootFile and not $rootAnaFile) {
die "FATAL ERROR: Cannot find root file in $inDir!\n"
}
if ($rootFile) {
validate($inDir,$rootFile,$fileName,'TEI');
}
else {
# print STDERR "WARN: No text root file found in $inDir\n"
}
if ($rootAnaFile) {
validate($inDir,$rootAnaFile,$fileNameAna,'TEI.ana');
}
else {
# print STDERR "WARN: No root .ana. file found in $inDir\n"
}
}
sub validate {
my $inDir = shift;
my $rootFile = shift;
my $fileName = shift;
my $type = shift;
my $interfix = $type;
$interfix =~ s/^TEI//;
print STDERR "INFO: Validating $type root $rootFile\n";
&chars($rootFile);
&run("$Jing $schemaDir/ParlaMint-teiCorpus$interfix.rng", $rootFile);
&run("$Saxon outDir=$tmpDir -xsl:$Compose", $rootFile);
&run("$Jing $schemaDir/ParlaMint.odd.rng", "$tmpDir/$fileName");
&run("$Saxon -xsl:$Valid", $rootFile);
&run("$Saxon -xsl:$Valid_particDesc", $rootFile);
&run("$Saxon -xsl:$Links", $rootFile);
@includes = split(/\n/, `$Saxon -xsl:$Includes $rootFile`);
while (my $f = shift @includes) {
$file = "$inDir/$f";
if (-e $file) {
if($file =~ m/ParlaMint-(?:[A-Z]{2}(?:-[A-Z0-9]{1,3})?(?:-[a-z]{2,3})?)?.?(taxonomy|listPerson|listOrg).*\.xml/){
print STDERR "INFO: Validating file included in teiHeader $file\n";
&chars($file);
&run("$Jing $schemaDir/ParlaMint-$1.rng", $file);
&run("$Saxon meta=$rootFile -xsl:$Links", $file);
} else {
print STDERR "INFO: Validating component $type file $file\n";
&chars($file);
&run("$Jing $schemaDir/ParlaMint-TEI$interfix.rng", $file);
&run("$Jing $schemaDir/ParlaMint.odd.rng", $file);
&run("$Saxon -xsl:$Valid", $file);
&run("$Saxon meta=$rootFile -xsl:$Links", $file);
}
}
else {print STDERR "ERROR: $rootFile XIncluded file $file does not exist!\n"}
}
}
# Check if $file contains bad characters
sub chars {
my $file = shift;
my %c;
my @bad = ();
my ($fName) = $file =~ m|([^/]+)$|
or die "FATAL ERROR: Bad file '$file'\n";
print STDERR "INFO: Char validation for $fName\n";
open(IN, '<:utf8', $file);
undef $/;
my $txt = <IN>;
undef %c;
for $c (split(//, $txt)) {$c{$c}++}
for $c (sort keys %c) {
if (ord($c) == hex('00A0') or #NO-BREAK SPACE
ord($c) == hex('2011') or #NON-BREAKING HYPHEN
ord($c) == hex('00AD') or #SOFT HYPHEN
ord($c) == hex('FFFD') or #REPLACEMENT CHAR
(ord($c) >= hex('2000') and ord($c) <= hex('200A')) or #NON-STANDARD SPACES
(ord($c) >= hex('E000') and ord($c) <= hex('F8FF')) #PUA
) {
$message = sprintf("U+%X (%dx)", ord($c), $c{$c});
push(@bad, $message)
}
}
print STDERR "WARN: File $fName contains bad chars: " . join('; ', @bad) . "\n"
if @bad
}
sub run {
my $command = shift;
my $file = shift;
my ($fName) = $file =~ m|([^/]+)$|
or die "FATAL ERROR: Bad file '$file'\n";
if ($command =~ /$Jing/) {
print STDERR "INFO: XML validation for $fName\n"
}
elsif ($command =~ /$Compose/) {
}
elsif ($command =~ /$Valid/) {
print STDERR "INFO: Content validaton for $fName\n"
}
elsif ($command =~ /$Valid_particDesc/) {
print STDERR "INFO: particDesc content validaton for $fName\n"
}
elsif ($command =~ /$Links/) {
print STDERR "INFO: Link checking for $fName\n"
}
else {die "FATAL ERROR: Weird command $command!\n"}
`$command $file 1>&2`;
}