forked from sputnick-dev/saxon-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
saxon-lint.pl
executable file
·254 lines (206 loc) · 6.47 KB
/
saxon-lint.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
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
#!/usr/bin/perl
my $myversion = "20230109";
# Gilles Quenot <[email protected]>
use strict; use warnings;
use FindBin '$RealBin';
use XML::LibXML;
use Getopt::Long;
use autodie;
use utf8;
use open qw/:std :utf8/;
my $htmlparser = 'lib/tagsoup-1.2.jar';
my $xmlparser = 'lib/saxon9he.jar';
my $queryclass = 'net.sf.saxon.Query';
my $htmlclass = 'org.ccil.cowan.tagsoup.Parser';
my $transformclass = 'net.sf.saxon.Transform';
my $help = my $html = my $indent = my $res = my $xslt = my $nopi = my $version = my $err = 0;
my $oDel = "\n"; # default output-separator
my $mainclass = my $xpath = my $query = my $xquery = my $verbose = '';
my @extra = ();
GetOptions (
"h|help" => \$help, # flag
"html" => \$html, # flag
"xslt=s" => \$xslt, # string
"output-separator=s" => \$oDel, # string
"xpath=s" => \$xpath, # string
"xquery=s" => \$xquery, # string
"indent" => \$indent, # flag
"no-pi" => \$nopi, # flag
"saxon-opt=s" => \@extra, # array
"v|verbose" => \$verbose, # flag
"version" => \$version, # string
) or die("Error in command line arguments\n");
if ($version) {
print "saxon-lint version $myversion\n";
exit 0;
}
my $sep = $^O =~ /(?:MSWin|cygwin)/i ? ";" : ":";
my $classpath = $html ? "$RealBin/$htmlparser${sep}$RealBin/$xmlparser" : "$RealBin/$xmlparser";
$indent = $indent ? 'yes' : 'no';
if ($nopi == 0) {
$nopi = 1 if $html and not length $xquery;
}
$verbose = $verbose ? 'set -x' : 'set +x';
if ($xslt) {
$mainclass = $transformclass;
$query = $xslt;
}
else {
$mainclass = $queryclass;
}
$query = $xpath unless length $query;
# Base command
my $basecmd = qq#java -cp "$classpath" "$mainclass" !encoding=utf-8 !indent=$indent -quit:on !item-separator='$oDel'#;
# Add all Saxon extra opts
$basecmd .= qq@ $_@ for @extra;
help(0) if $help == 1;
if (not $xslt and not length $xquery and not length $xpath) {
warn "Missing mandatory --xpath --xslt or --xquery argument\n\n";
help(1);
}
if (length $xquery) {
{ # $xquery can be a file or a string and -s complains when it's a string with multi-lines
no warnings;
if (-s $xquery) {
$basecmd .= qq! '-q:$xquery'!
}
else{
$query = $xquery;
if ($html) {
$basecmd .= qq# '-qs:declare default element namespace "http://www.w3.org/1999/xhtml";$query'#;
}
else {
$basecmd .= qq! '-qs:$query'!;
}
}
}
}
if (length $xquery and not @ARGV) {
execute($basecmd, undef, undef, undef, $nopi, undef) or $err++;
}
foreach my $input (@ARGV) {
my $https = 0;
if ($input =~ m!^https://!) {
$input = GET_https($input);
$https = 1;
}
my $cmd = $basecmd;
$cmd .= qq# -s:$input#;
if ($html and not length $xquery) {
$cmd .= qq# -x:$htmlclass '-qs:declare default element namespace "http://www.w3.org/1999/xhtml";$query'#;
}
elsif ($html and length $xquery) {
$cmd .= qq# -x:$htmlclass#;
}
elsif ($xslt) {
$cmd .= qq/ '-xsl:$query'/;
}
elsif (not length $xquery) {
$cmd .= qq/ '-qs:$query'/;
}
execute($cmd, $html, $https, $input, $nopi, $xpath) or $err++;
}
exit 1 if $err > 0;
sub help {
my $error = shift;
print STDERR <<EOF;
Usage:
$0 <opts> <file(s)>
Parse the XML files and output the result of the parsing
--help -h, this help
--xpath, XPath expression
--xquery, Xquery expression or file
--html, use the HTML parser
--xslt, use XSL transformation
--output-separator, set default separator to character ("\\n", ","...)
--indent, indent the output
--no-pi, remove Processing Instruction (<?xml ...>)
--saxon-opt, Saxon extra argument
--verbose -v, verbose mode
EOF
exit $error;
}
# Call the Java command-line with built command
sub execute {
my ($cmd, $html, $https, $input, $nopi, $xpath) = @_;
my $xml = qx(
$verbose
$cmd
);
$res = $?;
remove_PI(\$xml) if $nopi or (defined $xpath and length $xpath > 1);
remove_NS(\$xpath, \$xml) if $html;
print $xml;
if ($https) {
chomp $input;
unlink $input;
}
print "\n";
return ($res > 0) ? 0 : 1;
}
sub remove_PI {
my $xml = shift;
# can't find a better way to do this with XML::LibXML
$$xml =~ s/^\<\?xml\s*version=.\d+\.\d+.\s*encoding=.[^"]+.\?\>//i;
$$xml =~ s/(^|\n)[\n\s]*/$1/g;
return;
}
sub remove_NS {
my ($xpath, $xml) = @_;
if (length $$xpath and $$xpath ne "/") {
my $parser = XML::LibXML->new();
my $doc = $parser->parse_balanced_chunk($xml);
# remove namespaces for the whole document
for my $el ($doc->findnodes('//*')) {
if ($el->getNamespaces){
replace_without_ns($el);
}
}
return $doc->toString();
}
else {
return $$xml;
}
}
# http://stackoverflow.com/questions/17756926/remove-xml-namespaces-with-xmllibxml
# Replaces the given element with an identical one without the namespace
# also does this with attributes
sub replace_without_ns {
my ($el) = @_;
# new element has same name, minus namespace
my $new = XML::LibXML::Element->new( $el->localname );
#copy attributes (minus namespace namespace)
for my $att($el->attributes) {
if($att->nodeName !~ /xmlns(?::|$)/){
$new->setAttribute($att->localname, $att->value);
}
}
#move children
for my $child($el->childNodes){
$new->appendChild($child);
}
$el->parentNode->insertAfter($new, $el);
$el->unbindNode;
return;
}
sub GET_https {
my $arg = shift;
my $path;
require LWP::UserAgent;
use File::Temp;
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new(GET => $arg);
my $res = $ua->request($req);
die 'HTTPS error: ' . $res->status_line . "\n" unless $res->is_success;
my $fh = File::Temp->new();
$fh->unlink_on_destroy(0);
print $fh $res->content;
my $filename = $fh->filename;
if ($^O =~/cygwin/i) {
$path = qx(cygpath -m "/cygdrive/c/cygwin$filename");
}
else {
$path = $filename;
}
return $path;
}