| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | # Reads a list of files, either all gzipped, all bzip2ed or text. | ||
| 4 | # Runs XSL translation on them using the input XSL script, | ||
| 5 | # and outputs the result to a file. | ||
| 6 | # | ||
| 7 | # Uses xsltproc run using stdin. | ||
| 8 | # | ||
| 9 | # | ||
| 10 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use POSIX; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use Encode; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 1 | ▶ | use IO::Handle; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | |||
| 15 | 1 | ▶ | use Alvis::Utils; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | |||
| 17 | ###################### CONFIGURATION ##################### | ||
| 18 | |||
| 19 | my $XSL = "xsl/alvisLinks.xsl"; | ||
| 20 | my $XSLARGS = "-param CUTOFF 8.5 -stringparam SCORETYPE 'standard'"; | ||
| 21 | |||
| 22 | # records per group sent to one XSLTPROC instance | ||
| 23 | my $MAXSIZE = 10000000; | ||
| 24 | |||
| 25 | my $RECORDELEMENT = "documentRecord"; | ||
| 26 | my $GROUPELEMENT = "documentCollection"; | ||
| 27 | # toss out whatever else was included, and add this | ||
| 28 | my $GROUPELEMENTEXTRA = " xmlns=\"http://alvis.info/enriched/\" version=\"1.1\""; | ||
| 29 | |||
| 30 | |||
| 31 | ############ END CONFIGURATION ###################### | ||
| 32 | |||
| 33 | # autoflush | ||
| 34 | select((select(STDERR), $| = 1)[0]); | ||
| 35 | |||
| 36 | # encoding pragmas follow any includes like "use" | ||
| 37 | 1 | ▶ | use encoding 'utf8'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 38 | use open ':utf8'; | ||
| 39 | |||
| 40 | |||
| 41 | my $USAGE = "alvisXSL [--gzip|--bzip2|--dir] [--xslargs ARGS] [--xsl XSL-FILE] XML-FILE+\n" | ||
| 42 | . " Runs xsltproc multiple times on inputs. To convert into\n" | ||
| 43 | . " into XML, use alvisDecollect as a post-processor.\n" | ||
| 44 | . " dir = descend into directories, but not recursively\n" | ||
| 45 | . " xsl = $XSL\n" | ||
| 46 | . " xslargs = $XSLARGS\n"; | ||
| 47 | |||
| 48 | # command line inputs | ||
| 49 | my $usegzip = 0; | ||
| 50 | my $usebzip2 = 0; | ||
| 51 | my $usedir = 0; | ||
| 52 | |||
| 53 | ################################################################# | ||
| 54 | # | ||
| 55 | # file feeder | ||
| 56 | # | ||
| 57 | ################################################################# | ||
| 58 | |||
| 59 | my @files = (); | ||
| 60 | my @dirfiles = (); | ||
| 61 | my $usingdir = 0; | ||
| 62 | my $withdir = ""; | ||
| 63 | |||
| 64 | sub morefiles () { | ||
| 65 | if ( $#files>=0 ) { | ||
| 66 | return 1; | ||
| 67 | } | ||
| 68 | if ( $usingdir ) { | ||
| 69 | return 1; | ||
| 70 | } | ||
| 71 | return 0; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub nextfile () { | ||
| 75 | my $nf; | ||
| 76 | if ( $usingdir ) { | ||
| 77 | #print STDERR "Using dir\n"; | ||
| 78 | while ( ($nf=shift(@dirfiles)) ) { | ||
| 79 | if ( -f $nf ) { | ||
| 80 | return $nf; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | $usingdir = 0; | ||
| 84 | $withdir = ""; | ||
| 85 | return &nextfile(); | ||
| 86 | } | ||
| 87 | $nf = shift(@files); | ||
| 88 | #print STDERR "Got $nf\n"; | ||
| 89 | if ( !$nf ) { | ||
| 90 | return $nf; | ||
| 91 | } | ||
| 92 | if ( -d $nf ) { | ||
| 93 | #print STDERR "Is dir\n"; | ||
| 94 | if ( $usedir ) { | ||
| 95 | @dirfiles = sort(glob("$nf/*")); | ||
| 96 | $withdir = $nf; | ||
| 97 | $usingdir = 1; | ||
| 98 | return &nextfile(); | ||
| 99 | } else { | ||
| 100 | #print STDERR "Open on $nf failed\n"; | ||
| 101 | return &nextfile(); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | if ( -f $nf ) { | ||
| 105 | #print STDERR "Done\n"; | ||
| 106 | return $nf; | ||
| 107 | } | ||
| 108 | #print STDERR "Recurse\n"; | ||
| 109 | return &nextfile(); | ||
| 110 | } | ||
| 111 | |||
| 112 | my $xslbuff = ""; | ||
| 113 | my $reading = 0; | ||
| 114 | my $fullhead = ""; | ||
| 115 | |||
| 116 | sub nextline () { | ||
| 117 | if ( $xslbuff ) { | ||
| 118 | # print STDERR "Nextline() restarting: $xslbuff\n"; | ||
| 119 | $_ = $xslbuff; | ||
| 120 | $xslbuff = ""; | ||
| 121 | } else { | ||
| 122 | if ( !$reading ) { | ||
| 123 | # when opening, have to filter initial stuff | ||
| 124 | $reading = 1; | ||
| 125 | if ( !$fullhead ) { | ||
| 126 | while ( defined($_ = <XIN>) && | ||
| 127 | ( /<\?xml/ || /<$GROUPELEMENT/ ) ) { | ||
| 128 | $fullhead .= $_; | ||
| 129 | } | ||
| 130 | # discard whatever was in the header element before | ||
| 131 | $fullhead =~ s/<$GROUPELEMENT([^>]*)>/<$GROUPELEMENT $GROUPELEMENTEXTRA>/; | ||
| 132 | print FOUT $fullhead; | ||
| 133 | } else { | ||
| 134 | while ( defined($_ = <XIN>) && | ||
| 135 | ( /<\?xml/ || /<$GROUPELEMENT/ ) ) { | ||
| 136 | ; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | } else { | ||
| 140 | $_ = <XIN>; | ||
| 141 | # clean off end element and close if needed | ||
| 142 | if ( /<\/$GROUPELEMENT>/ ) { | ||
| 143 | s/<\/$GROUPELEMENT>.*//; | ||
| 144 | close(XIN); | ||
| 145 | $reading = 0; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | } | ||
| 149 | # print STDERR "Starting: $_"; | ||
| 150 | return $_; | ||
| 151 | } | ||
| 152 | |||
| 153 | sub endofline() { | ||
| 154 | if ( eof(XIN) ) { | ||
| 155 | return 1; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | ################################################################# | ||
| 160 | # | ||
| 161 | # XSLT routine | ||
| 162 | # | ||
| 163 | # This is kind of stupid,, couldn't find an XSLT + Sax + Perl | ||
| 164 | # setup, so we split the records into small chunks and pass | ||
| 165 | # them off to a separate XSLT program without SAX. | ||
| 166 | # | ||
| 167 | # Badly needs to be rewritten. | ||
| 168 | # | ||
| 169 | ################################################################# | ||
| 170 | |||
| 171 | |||
| 172 | # each call is one execution of XSLTPROC | ||
| 173 | sub XSLfill { | ||
| 174 | my $size = 0; | ||
| 175 | #print STDERR "Starting XSLfill\n"; | ||
| 176 | |||
| 177 | # set up new XSLT instance | ||
| 178 | # note we need to set this up with open/waitpid | ||
| 179 | # combination say that one STDOUT is finished before | ||
| 180 | # the next one is started | ||
| 181 | my $pid = 0; | ||
| 182 | if ($pid = open(FOUT, "|-") ) { | ||
| 183 | # goal is to fill up the XSLTPROC with one XML file | ||
| 184 | my $filled = 0; | ||
| 185 | if ( eof(XIN) ) { | ||
| 186 | close(XIN); | ||
| 187 | my $nf; | ||
| 188 | my $closed = 1; | ||
| 189 | while ( $closed && ($nf=&nextfile()) ) { | ||
| 190 | # more input XML | ||
| 191 | $closed = 0; | ||
| 192 | if ( $usegzip ) { | ||
| 193 | open(XIN,"zcat $nf |"); | ||
| 194 | } elsif ( $usebzip2 ) { | ||
| 195 | open(XIN,"bzcat $nf |"); | ||
| 196 | } else { | ||
| 197 | *XIN = Alvis::Utils::open_file($nf); | ||
| 198 | #open(XIN, "<$nf"); | ||
| 199 | } | ||
| 200 | if ( eof(XIN) ) { | ||
| 201 | $closed = 1; | ||
| 202 | print STDERR "Open on $nf failed\n"; | ||
| 203 | } | ||
| 204 | } | ||
| 205 | if ( $usedir == 0 ) { | ||
| 206 | print STDERR "Reading $nf for $XSL\n"; | ||
| 207 | } | ||
| 208 | $reading = 0; | ||
| 209 | } | ||
| 210 | print FOUT $fullhead; | ||
| 211 | $_ = &nextline(); | ||
| 212 | while ( $_ && $filled==0 ) { | ||
| 213 | if ( /<\/$RECORDELEMENT>/o ) { | ||
| 214 | if ( $size>$MAXSIZE ) { | ||
| 215 | # termination condition: too many records, so | ||
| 216 | # force a file break | ||
| 217 | $filled = 1; | ||
| 218 | my $ending = $_; | ||
| 219 | $ending =~ s/<\/$RECORDELEMENT>.*/<\/$RECORDELEMENT>\n/o; | ||
| 220 | # print STDERR "FOUT: $ending"; | ||
| 221 | print FOUT $ending; | ||
| 222 | $size += length($ending); | ||
| 223 | # build a new start for the next file | ||
| 224 | s/.*<\/$RECORDELEMENT>//o; | ||
| 225 | s/.*<$RECORDELEMENT /<$RECORDELEMENT /o; | ||
| 226 | $xslbuff = $_; | ||
| 227 | # print STDERR "Ending file set for $XSL\n"; | ||
| 228 | # print STDERR "Restart buff line set to $xslbuff\n==========\n"; | ||
| 229 | } | ||
| 230 | } | ||
| 231 | if ( $filled == 0 ) { | ||
| 232 | # print STDERR "FOUT: $_"; | ||
| 233 | print FOUT; | ||
| 234 | $size += length($_); | ||
| 235 | $_ = &nextline(); | ||
| 236 | if ( &endofline() ) { | ||
| 237 | # termination condition: end of current file | ||
| 238 | # print STDERR "FOUT: $_\n"; | ||
| 239 | print FOUT; | ||
| 240 | $size += length($_); | ||
| 241 | $filled = 1; | ||
| 242 | } | ||
| 243 | } | ||
| 244 | } | ||
| 245 | # print STDERR "SIZE: $size\n"; | ||
| 246 | print FOUT "</$GROUPELEMENT>\n"; | ||
| 247 | close(FOUT); | ||
| 248 | } else { | ||
| 249 | # print STDERR "Starting xsltproc with PID $pid\n"; | ||
| 250 | # print "#######################################\n"; | ||
| 251 | # exec("cat "); | ||
| 252 | exec("xsltproc $XSLARGS $XSL - "); | ||
| 253 | } | ||
| 254 | waitpid($pid,0); | ||
| 255 | if ( WEXITSTATUS($?) && WEXITSTATUS($?)!=255 ) { | ||
| 256 | print STDERR "Finished one xsltproc with " . WEXITSTATUS($?) . "\n"; | ||
| 257 | exit(1); | ||
| 258 | } | ||
| 259 | } | ||
| 260 | |||
| 261 | ################################################################# | ||
| 262 | # | ||
| 263 | # Run | ||
| 264 | # | ||
| 265 | ################################################################# | ||
| 266 | |||
| 267 | my $arg1=shift(); | ||
| 268 | while ( $arg1 =~ /^-/ ) { | ||
| 269 | if ( $arg1 eq "--xslargs" ) { | ||
| 270 | $XSLARGS = shift(); | ||
| 271 | } elsif ( $arg1 eq "--xsl" ) { | ||
| 272 | $XSL = shift(); | ||
| 273 | } elsif ( $arg1 eq "--gzip" ) { | ||
| 274 | $usegzip = 1; | ||
| 275 | } elsif ( $arg1 eq "--dir" ) { | ||
| 276 | $usedir = 1; | ||
| 277 | } elsif ( $arg1 eq "--bzip2" ) { | ||
| 278 | $usebzip2 = 1; | ||
| 279 | } elsif ( $arg1 eq "-h" || $arg1 eq "--help" ) { | ||
| 280 | print $USAGE; | ||
| 281 | exit(0); | ||
| 282 | } else { | ||
| 283 | print $USAGE; | ||
| 284 | exit(1); | ||
| 285 | } | ||
| 286 | $arg1=shift(); | ||
| 287 | } | ||
| 288 | |||
| 289 | if ( !$arg1 ) { | ||
| 290 | print $USAGE; | ||
| 291 | exit(1); | ||
| 292 | } | ||
| 293 | push(@files,$arg1); | ||
| 294 | push(@files,@ARGV); | ||
| 295 | # print STDERR "Files: " . join(" ",@files) . "\n"; | ||
| 296 | if ( ! $XSL || $#files<0 ) { | ||
| 297 | print $USAGE; | ||
| 298 | exit(1); | ||
| 299 | } | ||
| 300 | |||
| 301 | &XSLfill(); | ||
| 302 | while ( ! eof(XIN) || $xslbuff || &morefiles() ) { | ||
| 303 | &XSLfill(); | ||
| 304 | } | ||
| 305 | |||
| 306 | 1; | ||