package Files::Xslt; ## an package to run xslt over a directory use strict; use warnings; use Carp qw(confess); use Data::Dumper; use File::Find; use File::Temp qw/ tempfile tempdir /; use List::Util qw(shuffle); use PubMed::Common; use PubMed::Files; sub new { my $this=shift; my $class=ref($this) || $this; my $x={}; bless $x, $class; my $params=shift; ## copy parameters into the structure foreach my $key (keys %{$params}) { $x->{$key}=$params->{$key}; } $x->{'verbose'}=&PubMed::Common::isatty(); return $x; } sub convert { my $x=shift; my $count=0; foreach my $file (@{$x->{'in_files'}}) { $x->convert_file($file,$x->{'out_files'}->[$count++]); } } sub convert_file { my $x=shift; my $in_file=shift; my $out_file=shift; my $xslt_file=$x->{'file'}; if(-z $out_file) { unlink $out_file; } if(not &PubMed::Files::does_file_need_renewal($out_file,$in_file,$xslt_file)) { return ; } if($in_file=~m|\.gz$|) { my ($fh, $tmp_file) = tempfile(); # my $tmp_file="/tmp/tmp"; system("zcat $in_file > $tmp_file"); if($x->{'pre_in'}) { my $s=$x->{'pre_in'}.' '.$tmp_file; system($s); } system("xsltproc $xslt_file $tmp_file > $out_file"); unlink $tmp_file; } elsif($x->{'pre_in'}) { my ($fh, $tmp_file) = tempfile(); system("cp $in_file $tmp_file"); my $s=$x->{'pre_in'}.' '.$tmp_file; system($s); system($x->{'pre_in'}.' '.$tmp_file); system("xsltproc $xslt_file $tmp_file > $out_file"); system("rm $tmp_file"); } else { system("xsltproc $xslt_file $in_file > $out_file"); } } ## check if parameters are set sub list_files { my $x=shift; my $in_dir=$x->in_dir(); my $out_dir=$x->out_dir(); opendir(my $in_dh,$in_dir); $x->{'in_files'}=[]; my $in_name=$x->{'in_name'}; my $count_in=0; while(my $in_file=readdir $in_dh) { if($in_name and not $in_file=~m/$in_name/) { if($x->{'verbose'}) { print "I skip in_file $in_file\n"; } next; } my $in_fufi=$in_dir.'/'.$in_file; $x->{'in_files'}->[$count_in++]=$in_fufi; my $out_file=$in_file; my $out_name=$x->out_name; if($x->{'re_name'}) { $out_file=&{$x->{'re_name'}}($out_file); } #if($in_name and defined($out_name)) { # $out_file=~s|$in_name|$out_name|; # #$out_file=~s|medline(\d{2}n\d{4})\.xml\.gz|$1|; #} my $out_fufi=$out_dir.'/'.$out_file; print "$out_fufi\n"; $x->{'out_files'}->[$count_in-1]=$out_fufi; } print Dumper $x->{'out_files'}; } sub set_re_name { my $x=shift; my $arg=shift; if(not $arg) { confess "I need an agument here."; } if(not ref $arg eq 'CODE') { confess "I need a codref as argument."; } $x->{'re_name'}=$arg; } sub pre_in { my $x=shift; my $arg=shift; if(not $arg) { return $x->{'pre_in'}; } $x->{'pre_in'}=$arg; } sub in_dir { my $x=shift; my $arg=shift; if(not $arg) { return $x->{'dir'}->{'in'}; } my $in_dir=$arg; if(not -d $in_dir) { confess "I can't see the in_dir '$in_dir'."; } $x->{'dir'}->{'in'}=$arg; } sub out_dir { my $x=shift; my $arg=shift; if(not $arg) { return $x->{'dir'}->{'out'}; } my $out_dir=$arg; if(not -d $out_dir) { confess "I can't see the out_dir '$out_dir'."; } $x->{'dir'}->{'out'}=$arg; } ## in part of changing regex sub in_name { my $x=shift; my $arg=shift; if(not $arg) { return $x->{'in_name'}; } $x->{'in_name'}=$arg; } ## out part of changing regex sub out_name { my $x=shift; my $arg=shift; if(not defined($arg)) { return $x->{'out_name'}; } $x->{'out_name'}=$arg; } ## xslt file sub file { my $x=shift; my $arg=shift; if(not $arg) { return $x->{'file'}; } my $file=$x->{'file'}; if(not -f $file) { if(-l $file) { my $link=readlink $file; if(not -f $link) { confess "I can't see the file '$file'."; } } } $x->{'file'}=$arg; } 1;