NLPPlatform/Convert.pm untested

filter: Statementstmt 0.0% 0 / 239 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 362 CC 23 SCAR 33.2
CC
23
Cov
0%
conversion_file_to_alvis_xml
90.0
get_type_file
30.0
outputting_alvis
30.0
CRAP 27.8
1 package Alvis::NLPPlatform::Convert;
2  
3 # The name
4 # of the directory if defined, in the proirity order, from the variable
5 # C<SPOOLDIR> in the section C<CONVERTERS>, C<SPOOLDIR> in the section
6 # C<alvis_connection>. If any name can be determine,
7  
8 # if (defined $config->{"CONVERTERS"}->{"SPOOLDIR"}) {
9 # $ODIR = $config->{"CONVERTERS"}->{"SPOOLDIR"};
10 # } else {
11 # if (defined $config->{"alvis_connection"}->{"SPOOLDIR"}) {
12 # $ODIR = $config->{"alvis_connection"}->{"SPOOLDIR"};
13 # } else {
14  
15  
16 use strict;
17 use warnings;
18 use utf8;
19 no utf8;
20  
21 use Alvis::NLPPlatform::Document;
22 use File::MMagic;
23 use File::Basename;
24 use File::Path qw(mkpath);
25 use File::Touch;
26  
27 use Data::Dumper;
28 use Cwd;
29  
30 our $VERSION=$Alvis::NLPPlatform::VERSION;
31  
32 sub load_MagicNumber
33 {
34 my $config = shift;
35  
36 my $mm = new File::MMagic; # use internal magic file
37  
38 print STDERR "Loading complementary magic number ... ";
39
40 open FILEM, $config->{"CONVERTERS"}->{"SupplMagicFile"} or die "No such file or directory\n";
41  
42 my $line;
43 while($line = <FILEM>) {
44 chomp $line;
45 $line =~ s/\s*\#.*//;
46 $line =~ s/^\s*//;
47
48 if ($line ne "") {
49 $mm->addMagicEntry($line);
50 }
51 }
52 print STDERR "done\n";
53  
54 return($mm);
55 }
56  
57 sub conversion_file_to_alvis_xml
58 {
59  
60 # warn join("|", @_) . "\n";
61 my $file = shift;
62 my $AlvisConv = shift;
63 my $config = shift;
64 my $mm = shift;
65  
66  
67 my $outfile = $config->{ALVISTMP} . "/" . basename($file) . ".out";
68 my $infile = $file;
69 my $outfile_path;
70  
71 my $outdata;
72
73 my $type = &get_type_file($infile, $mm);
74  
75 if (($type !~ /xml/i) && ($type !~ /html/i)) {
76  
77 print STDERR "Conversion of file ...\n";
78  
79 # print Dumper $config;
80
81 if ((defined $config->{"CONVERTERS"}->{$type}) && ($config->{"CONVERTERS"}->{$type} ne "")) {
82
83 print STDERR "Converting to HTML ...\n" ;
84
85  
86 my $commandline = $config->{"CONVERTERS"}->{$type} . " \"$infile\" > \"$outfile\"";
87
88 warn "Convertion line : $commandline\n";
89
90 `$commandline`;
91
92 print STDERR "done\n";
93 return &conversion_file_to_alvis_xml($outfile, $AlvisConv, $config, $mm);
94 } else {
95 print STDERR "No convertion to HMTL to do: already in HTML, unknown type or no specified converter\n";
96 return 0;
97 }
98 } else {
99 if ($type =~ /html/i) {
100 # calling Alvis convert
101 return &html2alvis($infile, $AlvisConv, $config);
102 } elsif ($type =~ /xml/i) {
103 # checking if it is a xhtml or not
104 my $xmlns = Alvis::NLPPlatform::Document::getnamespace($infile);
105  
106  
107 if (defined $xmlns) {
108 if ($xmlns eq "http://www.w3.org/1999/xhtml") {
109 # if xhtml calling Alvis convert
110 return &html2alvis($infile, $AlvisConv, $config);
111 } else {
112 # if xml,
113 # check if it is an alvis file
114 # if not, try to use a stylesheet
115 if ($xmlns eq "http://alvis.info/enriched/") {
116 print STDERR "Already in XML Alvis\n";
117 $outfile_path = Alvis::NLPPlatform::Document::get_language_from_file($infile, "$outfile.lang", $config);
118 # print STDERR "Outfile is $outfile (i.e. $outfile_path)\n";
119 return &outputting_alvis_from_file($outfile_path, $AlvisConv, $config);
120 } else {
121 # use of a stylesheet conversion
122 ($xmlns, $outdata) = &applying_stylesheet($infile, $xmlns, $config);
123 if ($xmlns ne "default") {
124 # CAREFUL not tested yet !
125 $outdata = Alvis::NLPPlatform::Document::get_language_from_data($outdata);
126 return &outputting_alvis($outdata, $AlvisConv, $config);
127 } else {
128 return &outputting_empty_xmlns_file($outdata, $outfile . ".out", $AlvisConv, $config, $mm);
129 }
130 }
131 }
132
133 } else {
134 # use of a default stylesheet conversion
135 ($xmlns,$outdata) = &applying_stylesheet($infile, "default", $config);
136 return &outputting_empty_xmlns_file($outdata, $outfile . ".out", $AlvisConv, $config, $mm);
137 }
138 }
139 }
140 }
141  
142 sub outputting_empty_xmlns_file
143 {
144 my $outdata = shift;
145 my $outfile = shift;
146 my $AlvisConv = shift ;
147 my $config = shift;
148 my $mm = shift;
149  
150 warn "Openning $outfile\n";
151 open OUTFILE, ">$outfile";
152 binmode(OUTFILE, ":utf8");
153 print OUTFILE $outdata;
154 close OUTFILE;
155 return &conversion_file_to_alvis_xml($outfile, $AlvisConv, $config, $mm);
156
157 }
158  
159  
160 sub applying_stylesheet
161 {
162 my $file = shift;
163 my $xmlns = shift;
164 my $config = shift;
165  
166 # my $xslt_proc = XML::XSLT->new ($stylesheet, warnings => 1);
167  
168 # $xslt_proc->transform ($file);
169 # print $xslt_proc->toString;
170  
171 # maybe change for XML::DOM::Lite::XSLT engine
172  
173 if (!exists $config->{"CONVERTERS"}->{"STYLESHEET"}->{$xmlns}) {
174 $xmlns = "default";
175 }
176 my $command = $config->{"CONVERTERS"}->{"STYLESHEET"}->{$xmlns} . " $file";
177 print STDERR "Applying the stylesheet : " . $config->{"CONVERTERS"}->{"STYLESHEET"}->{$xmlns} . "\n";
178  
179 my $outdata;
180 $outdata = `$command`;
181
182 return ($xmlns,$outdata);
183 # $xslt_proc->dispose();
184  
185 }
186  
187 sub get_type_file
188 {
189 my $file = shift;
190 my $mm = shift;
191  
192 print STDERR "Determining the type of the file " . $file . ": ";
193
194 my $type = $mm->checktype_filename($file);
195  
196 if ($file =~ /.ppt$/i) {
197 $type = "application/powerpoint";
198 warn "Getting the type thanks to the extension\n";
199 }
200 if ($file =~ /.xls$/i) {
201 $type = "application/vnd.ms-excel";
202 warn "Getting the type thanks to the extension\n";
203 }
204 # if msword may be it should be relevant to check the extension, to better determine the type
205 $type =~ s/;.*//;
206 if (($type eq "message/rfc822") || ($file =~ /^x-system\/x-unix;/)) {
207 if ($file =~ /.tex$/i) {
208 $type = "text/x-tex";
209 warn "Getting the type thanks to the extension\n";
210 }
211 }
212 print STDERR "Type file: $type\n";
213 return($type);
214  
215 }
216  
217 sub html2alvis_init
218 {
219 my $config = shift;
220  
221 my $ODir;
222  
223 my $NPerOurDir=1000;
224 # my $MetaEncoding='iso-8859-1';
225 my $MetaEncoding='UTF-8';
226 my $HTMLEncoding=undef;'iso-8859-1';
227 my $HTMLEncodingFromMeta='utf-8';
228 my $IncOrigDoc=1;
229  
230 if (defined $config->{"ALVISTMP"}) {
231 $ODir = $config->{"ALVISTMP"};
232 } else {
233 $ODir = ".";
234 }
235  
236  
237 warn "Outdir is $ODir\n";
238  
239 print STDERR "Initialisation of the Alvis converter ...";
240  
241 my $C=Alvis::Convert->new(outputRootDir=>$ODir,
242 outputNPerSubdir=>1000,
243 outputAtSameLocation=>0,
244 metaEncoding=>$MetaEncoding,
245 sourceEncoding=>$HTMLEncoding,
246 includeOriginalDocument=>$IncOrigDoc,
247 sourceEncodingFromMeta=>$HTMLEncodingFromMeta);
248  
249 $C->init_output();
250 my $i = 0;
251 while (-f "$ODir/0/$i.alvis") { $i++;};
252 warn "Starting at $i\n";
253 $C->{outputN} = $i;
254 print STDERR "done\n";
255 return($C);
256 }
257  
258 sub html2alvis
259 {
260 my $filename = shift;
261 my $Alvis_converter = shift;
262 my $config = shift;
263  
264 print STDERR "Converting $filename to ALVIS XML format\n";
265  
266 my $meta_txt = &make_meta($filename);
267  
268 my $html_txt=$Alvis_converter->read_HTML($filename);
269  
270 # print STDERR "==>" . utf8::is_utf8($html_txt) . "\n";
271  
272 if (!defined($html_txt))
273 {
274 warn "Reading the HTML for basename \"$filename\" failed. " .
275 $Alvis_converter->errmsg();
276 $Alvis_converter->clearerr();
277 return (1);;
278 }
279  
280 # print STDERR $html_txt;
281  
282 my $alvisXML=$Alvis_converter->HTML($html_txt,$meta_txt);
283  
284 if (!defined($alvisXML))
285 {
286 warn "Obtaining the Alvis version of the " .
287 "HTML version of an article failed. " . $Alvis_converter->errmsg();
288 $Alvis_converter->clearerr();
289 return 2;
290 }
291 # my $e=Alvis::Document::Encoding->new();
292 # my $type_guesser=Alvis::Document::Type->new();
293 # my ($doc_type,$doc_sub_type)=$type_guesser->guess($alvisXML);
294 # my $doc_encoding=$e->guess_and_convert($alvisXML,$doc_type,$doc_sub_type, "UTF-8");
295 # if (!defined($doc_encoding))
296 # {
297 # die('Cannot guess. ' . $e->errmsg());
298 # }
299 # print STDERR "$doc_type,$doc_sub_type,$doc_encoding\n";
300 # print STDERR $e->guess($alvisXML);
301 # warn "Checking the encoding\n";
302 # if (!Encode::is_utf8($alvisXML)) {
303 # warn "Not a UTF-8, assume to be a latin-1 document\n";
304 # print STDERR "Converting in UTF8...\n";
305 # Encode::from_to($alvisXML, "iso-8859-1", "UTF-8");
306 # print STDERR "done\n";
307 # }
308 # print STDERR $alvisXML;
309 # exit;
310
311 # my $decoder = Encode::Guess->guess_encoding($alvisXML, /UTF-8/);
312 # if (!ref($decoder)) {
313 # warn "Not a UTF-8, assume to be a latin-1 document\n";
314 # print STDERR "Converting in UTF8...\n";
315 # $alvisXML = $decoder->decode($alvisXML);
316 # # Encode::from_to($alvisXML, "iso-8859-1", "UTF-8");
317 # print STDERR "done\n";
318 # } else {
319 # warn "Document is already in UTF-8 :-)\n";
320 # }
321  
322 # TO SEE HOW TO REMOVE CONCAT of foot and head
323 # if ($alvisXML !~ /^\s*<\?xml /) {
324 # my $xmlhead = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<documentCollection xmlns=\"http://alvis.info/enriched/\" version=\"1.1\">\n";
325 # my $xmlfoot = "</documentCollection>\n";
326 # $alvisXML = Alvis::NLPPlatform::Document::get_language_from_data($xmlhead . $alvisXML . $xmlfoot);
327 # } else {
328 # print STDERR $alvisXML;
329  
330 $alvisXML = Alvis::NLPPlatform::Document::get_language_from_data($alvisXML);
331 # print STDERR $alvisXML;
332  
333 # }
334 # print STDERR $alvisXML;
335  
336 # return &outputting_alvis($xmlhead . $alvisXML . $xmlfoot, $Alvis_converter, $config);
337 return &outputting_alvis($alvisXML, $Alvis_converter, $config);
338  
339 # print STDERR "\t done\n";
340  
341 # return 0;
342 }
343  
344 sub outputting_alvis_from_file
345 {
346 my $alvisfile = shift;
347 my $Alvis_converter = shift;
348 my $config = shift;
349  
350 open ALVISFILE, $alvisfile or die "No such file: $alvisfile\n";
351 # binmode(ALVISFILE, ":utf8");
352 binmode ALVISFILE; # XXXX
353  
354 local $/ = undef;
355  
356 my $alvisfile_data = <ALVISFILE>;
357 close ALVISFILE;
358  
359 my $docs = Alvis::NLPPlatform::Document::get_documentRecords($alvisfile_data);
360  
361 # print STDERR "doc_list : $docs\n";
362  
363 # return &outputting_alvis($alvisfile_data, $Alvis_converter, $config);
364 return &outputting_alvis($docs, $Alvis_converter, $config);
365  
366 }
367  
368 sub outputting_alvis
369 {
370 my $alvisXML = shift;
371 my $Alvis_converter = shift;
372 my $config = shift;
373 my $xmlheadXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
374 my $xmlheaddC = "<documentCollection xmlns=\"http://alvis.info/enriched/\" version=\"1.1\">\n";
375 my $xmlfoot = "</documentCollection>\n";
376  
377 # print STDERR Dumper $config;
378  
379 # print STDERR $alvisXML;
380  
381 if ((defined $config->{"CONVERTERS"}->{"StoreInputFiles"}) && ($config->{"CONVERTERS"}->{"StoreInputFiles"} == 0)) {
382 print STDERR "Don't store files\n";
383 if ($alvisXML =~ /^\s*<documentRecord/) {
384 $alvisXML = $xmlheadXML . $xmlheaddC . $alvisXML . $xmlfoot;
385 }
386
387 if ($alvisXML !~ /^\s*<\?xml /) {
388 $alvisXML = $xmlheadXML . $alvisXML;
389 }
390 # print STDERR "$alvisXML\n";
391  
392 return $alvisXML;
393 }
394 else {
395 print STDERR "Store files\n";
396 # print STDERR $alvisXML;
397 if (!$Alvis_converter->output_Alvis([$alvisXML]))
398 {
399 warn "Outputting the Alvis records failed. " . $Alvis_converter->errmsg();
400 $Alvis_converter->clearerr();
401 return 3;
402 }
403 }
404 }
405  
406 sub make_meta
407 {
408  
409 my $filename = shift;
410  
411
412 my $meta_txt = "";
413 $meta_txt .= "title\ttitle\n";
414 $meta_txt .= "date\t";
415
416 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime();
417  
418 $meta_txt .= sprintf("%04d-%02d-%02d %02d:%02d:%02d\n", $year+1900, $mon+1, $mday, $hour, $min, $sec);
419  
420 while($filename =~ s/\.out$//){};
421  
422 $meta_txt .= "url\tfile://$filename\n";
423  
424 return($meta_txt);
425 }
426  
427 sub making_spool
428 {
429 my $config = shift;
430 my $outputRootDir = shift;
431  
432 my $xmlfile;
433 my $xml_rec_doc = "";
434 my $line;
435 my $working_dir = getcwd();
436
437 print STDERR "Making the spool ...\n";
438  
439 print STDERR "Working dir : $working_dir\n";
440  
441 mkpath($config->{"alvis_connection"}->{"SPOOLDIR"}) or warn "cannot create directory " . $config->{"alvis_connection"}->{"SPOOLDIR"} . "\n";
442 touch($config->{"alvis_connection"}->{"SPOOLDIR"} . "/seq");
443  
444 $|=1;
445  
446 my $pipe = new Alvis::Pipeline::Read(port => $config->{"alvis_connection"}->{"HARVESTER_PORT"}, spooldir => $config->{"alvis_connection"}->{"SPOOLDIR"},
447 loglevel=>10)
448 or die "can't create read-pipe on port " . $config->{"alvis_connection"}->{"HARVESTER_PORT"} . ": $!";
449  
450 my $pipe_out = new Alvis::Pipeline::Write(host => "localhost",
451 port => $config->{"alvis_connection"}->{"HARVESTER_PORT"},
452 loglevel => 10)
453 or die "can't create ALVIS write-pipe for port '" . $config->{"alvis_connection"}->{"HARVESTER_PORT"} . "': $!";
454  
455 my $tmp_spool_dir = $outputRootDir . "/0";
456  
457 opendir DIR, $tmp_spool_dir;
458 while($xmlfile = readdir DIR) {
459  
460 if (($xmlfile ne ".") && ($xmlfile ne "..")) {
461 open XMLFILE, "$tmp_spool_dir/$xmlfile" or die "Cannot open such file ($xmlfile)\n";
462 binmode(XMLFILE, ":utf8");
463 $xml_rec_doc = "";
464 while($line = <XMLFILE>) {
465 $xml_rec_doc .= $line;
466 }
467 $pipe_out->write($xml_rec_doc);
468 close XMLFILE;
469 unlink "$tmp_spool_dir/$xmlfile";
470 }
471 }
472 closedir(DIR);
473  
474 rmdir($tmp_spool_dir);
475 sleep 1;
476 $pipe_out->close() or die "Truly unbelievable (1)";
477 $pipe->close(); # or die "Truly unbelievable (2)";
478
479 print STDERR "Making the spool ... done\n";
480
481 chdir($working_dir);
482  
483 return 0;
484 }
485  
486  
487  
488 1;
489  
490 __END__