Alvis-Convert
view release on metacpan or search on metacpan
bin/alvisXSL view on Meta::CPAN
# encoding pragmas follow any includes like "use"
use encoding 'utf8';
use open ':utf8';
my $USAGE = "alvisXSL [--gzip|--bzip2|--dir] [--xslargs ARGS] [--xsl XSL-FILE] XML-FILE+\n"
. " Runs xsltproc multiple times on inputs. To convert into\n"
. " into XML, use alvisDecollect as a post-processor.\n"
. " dir = descend into directories, but not recursively\n"
. " xsl = $XSL\n"
. " xslargs = $XSLARGS\n";
# command line inputs
my $usegzip = 0;
my $usebzip2 = 0;
my $usedir = 0;
#################################################################
#
# file feeder
#
#################################################################
my @files = ();
my @dirfiles = ();
my $usingdir = 0;
my $withdir = "";
sub morefiles () {
if ( $#files>=0 ) {
return 1;
}
if ( $usingdir ) {
return 1;
}
return 0;
}
sub nextfile () {
my $nf;
if ( $usingdir ) {
#print STDERR "Using dir\n";
while ( ($nf=shift(@dirfiles)) ) {
if ( -f $nf ) {
return $nf;
}
}
$usingdir = 0;
$withdir = "";
return &nextfile();
}
$nf = shift(@files);
#print STDERR "Got $nf\n";
if ( !$nf ) {
return $nf;
}
if ( -d $nf ) {
#print STDERR "Is dir\n";
if ( $usedir ) {
@dirfiles = sort(glob("$nf/*"));
$withdir = $nf;
$usingdir = 1;
return &nextfile();
} else {
#print STDERR "Open on $nf failed\n";
return &nextfile();
}
}
if ( -f $nf ) {
#print STDERR "Done\n";
return $nf;
}
#print STDERR "Recurse\n";
return &nextfile();
}
my $xslbuff = "";
my $reading = 0;
my $fullhead = "";
sub nextline () {
if ( $xslbuff ) {
# print STDERR "Nextline() restarting: $xslbuff\n";
$_ = $xslbuff;
$xslbuff = "";
} else {
if ( !$reading ) {
# when opening, have to filter initial stuff
$reading = 1;
if ( !$fullhead ) {
while ( defined($_ = <XIN>) &&
( /<\?xml/ || /<$GROUPELEMENT/ ) ) {
$fullhead .= $_;
}
# discard whatever was in the header element before
$fullhead =~ s/<$GROUPELEMENT([^>]*)>/<$GROUPELEMENT $GROUPELEMENTEXTRA>/;
print FOUT $fullhead;
} else {
while ( defined($_ = <XIN>) &&
( /<\?xml/ || /<$GROUPELEMENT/ ) ) {
;
}
}
} else {
$_ = <XIN>;
# clean off end element and close if needed
if ( /<\/$GROUPELEMENT>/ ) {
s/<\/$GROUPELEMENT>.*//;
close(XIN);
$reading = 0;
}
}
}
# print STDERR "Starting: $_";
return $_;
}
sub endofline() {
if ( eof(XIN) ) {
return 1;
( run in 1.185 second using v1.01-cache-2.11-cpan-39bf76dae61 )