merged tag ooo/DEV300_m102
[LibreOffice.git] / qadevOOo / runner / convwatch / MSOfficePrint.java
blob5be0cfe058ce1279764556bd87e61d251caf23b9
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 package convwatch;
30 import convwatch.FileHelper;
31 import java.io.File;
32 import java.io.FileWriter;
33 import java.io.RandomAccessFile;
34 import convwatch.GraphicalTestArguments;
35 import helper.ProcessHandler;
36 import java.util.ArrayList;
37 import helper.OSHelper;
39 /**
40 * This object gives all functionallity to print msoffice documents.
41 * It also offers functions to check what type of document it is.
42 * It handles *.doc as word documents and use word to print
43 * *.xls as excel
44 * *.ppt as powerpoint
47 class ProcessHelper
49 ArrayList m_aArray;
52 public class MSOfficePrint
54 private String m_sPrinterName; // within Windows the tools need a printer name;
56 public void setPrinterName(String _s) {m_sPrinterName = _s;}
58 // -----------------------------------------------------------------------------
59 static boolean isWordDocument(String _sSuffix)
61 if (_sSuffix.toLowerCase().endsWith(".doc") ||
62 _sSuffix.toLowerCase().endsWith(".rtf") ||
63 _sSuffix.toLowerCase().endsWith(".dot"))
65 return true;
67 return false;
70 static boolean isExcelDocument(String _sSuffix)
72 // xlt templates
73 // xlw
74 // xla addin
75 if (_sSuffix.toLowerCase().endsWith(".xls"))
77 return true;
79 /* temporal insertion by SUS
80 if (_sSuffix.endsWith(".xml"))
82 return true;
83 }*/
84 return false;
87 static boolean isPowerPointDocument(String _sSuffix)
89 if (_sSuffix.toLowerCase().endsWith(".pps") ||
90 _sSuffix.toLowerCase().endsWith(".ppt"))
92 return true;
94 return false;
97 /**
98 * returns true, if the given filename has a MS Office suffix.
100 public static boolean isMSOfficeDocumentFormat(String _sFile)
102 String sDocumentSuffix = FileHelper.getSuffix(_sFile);
103 if (isWordDocument(sDocumentSuffix)) return true;
104 if (isExcelDocument(sDocumentSuffix)) return true;
105 if (isPowerPointDocument(sDocumentSuffix)) return true;
106 // if suffix is xml, return also true, but we can't decide if word or excel
107 if (sDocumentSuffix.toLowerCase().endsWith(".xml")) return true;
108 return false;
111 public void storeToFileWithMSOffice( GraphicalTestArguments _aGTA,
112 String _sInputFile,
113 String _sOutputFile) throws ConvWatchCancelException, java.io.IOException
115 String sDocumentSuffix = FileHelper.getSuffix(_sInputFile);
116 String sFilterName = _aGTA.getExportFilterName();
117 ArrayList aStartCommand = new ArrayList();
118 if (isWordDocument(sDocumentSuffix))
120 aStartCommand = createWordStoreHelper();
122 else if (isExcelDocument(sDocumentSuffix))
124 aStartCommand = createExcelStoreHelper();
126 else if (isPowerPointDocument(sDocumentSuffix))
129 else if (sDocumentSuffix.toLowerCase().equals(".xml"))
131 // special case, if xml we prefer word, but with DEFAULT_XML_FORMAT_APP=excel it's changeable.
132 if (_aGTA.getDefaultXMLFormatApp().toLowerCase().equals("excel"))
134 aStartCommand = createExcelStoreHelper();
136 // else
137 // {
138 // }
140 else
142 GlobalLogWriter.get().println("No Microsoft Office document format found.");
143 // TODO: use a better Exception!!!
144 throw new ConvWatchCancelException/*WrongSuffixException*/("No MS office document format found.");
146 if (aStartCommand != null)
148 if (sFilterName == null)
150 // TODO: hardcoded FilterName in perl script
151 sFilterName = ""; // xlXMLSpreadsheet";
154 // String sCommand = sStartCommand + " " +
155 // _sInputFile + " " +
156 // StringHelper.doubleQuote(sFilterName) + " " +
157 // _sOutputFile;
159 aStartCommand.add(_sInputFile);
160 aStartCommand.add(sFilterName);
161 aStartCommand.add(_sOutputFile);
162 realStartCommand(aStartCommand);
166 // -----------------------------------------------------------------------------
168 * print the given file (_sInputFile) to the file name (_sPrintFile)
170 public void printToFileWithMSOffice( GraphicalTestArguments _aGTA,
171 String _sInputFile,
172 String _sPrintFilename) throws ConvWatchCancelException, java.io.IOException
174 String sDocumentSuffix = FileHelper.getSuffix(_sInputFile);
176 setPrinterName(_aGTA.getPrinterName());
178 ArrayList aStartCommand = new ArrayList();
179 if (isWordDocument(sDocumentSuffix))
181 aStartCommand = createWordPrintHelper();
183 else if (isExcelDocument(sDocumentSuffix))
185 aStartCommand = createExcelPrintHelper();
187 else if (isPowerPointDocument(sDocumentSuffix))
189 aStartCommand = createPowerPointPrintHelper();
191 else if (sDocumentSuffix.toLowerCase().equals(".xml"))
193 // TODO: Open XML File and check if we need excel or word
194 String sOfficeType = getOfficeType(_sInputFile);
196 // special case, if xml we prefer word, but with DEFAULT_XML_FORMAT_APP=excel it's changeable.
197 // if (_aGTA.getDefaultXMLFormatApp().toLowerCase().equals("excel"))
198 if (sOfficeType.equals("excel"))
200 aStartCommand = createExcelPrintHelper();
202 else if (sOfficeType.equals("word"))
204 aStartCommand = createWordPrintHelper();
206 else
208 return;
211 else
213 GlobalLogWriter.get().println("No Microsoft Office document format found.");
214 // TODO: use a better Exception!!!
215 throw new ConvWatchCancelException/*WrongSuffixException*/("No Mircosoft Office document format found.");
218 if (aStartCommand.isEmpty() == false)
220 String sPrinterName = m_sPrinterName;
221 if (sPrinterName == null)
223 sPrinterName = "";
226 // String sCommand = sStartCommand + " " +
227 // _sInputFile + " " +
228 // StringHelper.doubleQuote(m_sPrinterName) + " " +
229 // _sPrintFilename;
230 aStartCommand.add(_sInputFile);
231 aStartCommand.add(m_sPrinterName);
232 aStartCommand.add(_sPrintFilename);
234 realStartCommand(aStartCommand);
236 String sUserDir = System.getProperty("user.home");
237 String fs = System.getProperty("file.separator");
238 if (! sUserDir.endsWith(fs))
240 sUserDir = sUserDir + fs;
242 _aGTA.getPerformance().readWordValuesFromFile(sUserDir + "msofficeloadtimes.txt");
243 OfficePrint.createInfoFile(_sPrintFilename, _aGTA, "msoffice");
244 TimeHelper.waitInSeconds(2, "Give Microsoft Office some time to print.");
247 public void realStartCommand(ArrayList _aStartCommand) throws ConvWatchCancelException
249 if (_aStartCommand.isEmpty())
251 throw new ConvWatchCancelException/*WrongEnvironmentException*/("Given list is empty.");
256 // Convert the StartCommand ArrayList to a String List
257 int nValues = _aStartCommand.size();
258 String[] aList = new String[nValues];
259 for (int i=0;i<nValues;i++)
261 String aStr = (String) _aStartCommand.get(i);
262 if (aStr == null)
264 aStr = "";
266 if (aStr.length() == 0)
268 aStr = "\"\"";
270 aList[i] = new String(aStr);
273 // This is really the latest point where we can check if we are running within windows environment
274 if (! OSHelper.isWindows())
276 // TODO: use a better Exception!!!
277 throw new ConvWatchCancelException/*WrongEnvironmentException*/("We doesn't work within windows environment.");
281 ProcessHandler aHandler = new ProcessHandler(aList);
282 boolean bBackValue = aHandler.executeSynchronously();
284 catch (IndexOutOfBoundsException e)
286 throw new ConvWatchCancelException/*WrongEnvironmentException*/("Given list is too short.");
289 // return aHandler.getExitCode();
293 ArrayList createWordPrintHelper() throws java.io.IOException
295 // create a program in tmp file
296 String sTmpPath = util.utils.getUsersTempDir();
297 String ls = System.getProperty("line.separator");
298 String fs = System.getProperty("file.separator");
300 String sPrintViaWord = "printViaWord.pl";
302 ArrayList aList = searchLocalFile(sPrintViaWord);
303 if (aList.isEmpty() == false)
305 return aList;
308 String sName = sTmpPath + fs + sPrintViaWord;
309 File aFile = new File(sName);
310 FileWriter out = new FileWriter(aFile.toString());
313 out.write( "eval 'exec perl -wS $0 ${1+\"$@\"}' " + ls );
314 out.write( " if 0; " + ls );
315 out.write( "use strict; " + ls );
316 out.write( "use Time::HiRes; " + ls );
317 out.write( "if ( $^O ne \"MSWin32\") " + ls );
318 out.write( "{ " + ls );
319 out.write( " print 'Windows only.\\n'; " + ls );
320 out.write( " print_usage(); " + ls );
321 out.write( " exit(1); " + ls );
322 out.write( "} " + ls );
323 out.write( " " + ls );
324 out.write( "use Win32::OLE; " + ls );
325 out.write( "use Win32::OLE::Const 'Microsoft Word'; " + ls );
326 out.write( " " + ls );
327 out.write( "# ------ usage ------ " + ls );
328 out.write( "sub print_usage() " + ls );
329 out.write( "{ " + ls );
330 out.write( " print STDERR \"Usage: word_print.pl <Word file> <name of printer> <output file> .\\n " + ls );
331 out.write( " Please use the same string for the name of the printer as you can find \\n " + ls );
332 out.write( " under Start-Control Panel-Printer and Faxes \\n " + ls );
333 out.write( " The name could look like the the following line: \\n " + ls );
334 out.write( " Apple LaserWriter II NT v47.0 \\n " + ls );
335 out.write( " Sample command line: \\n " + ls );
336 out.write( " execl_print.pl c:\\book1.doc Apple LaserWriter II NT v47.0 c:\\output\\book1.ps \\n\"; " + ls );
337 out.write( "} " + ls );
338 out.write( " " + ls );
339 out.write( " " + ls );
340 out.write( "if ($#ARGV != 2) " + ls );
341 out.write( "{ " + ls );
342 out.write( " print 'Too less arguments.\\n'; " + ls );
343 out.write( " print_usage(); " + ls );
344 out.write( " exit(1); " + ls );
345 out.write( "} " + ls );
346 out.write( " " + ls );
347 out.write( "my $startWordTime = Time::HiRes::time(); " + ls );
348 out.write( "my $Word = Win32::OLE->new('Word.Application'); " + ls );
349 out.write( "my $stopWordTime = Time::HiRes::time() - $startWordTime; " + ls );
350 out.write( "# $Word->{'Visible'} = 1; # if you want to see what's going on " + ls );
351 out.write( "# , ReadOnly => 1})" + ls );
352 out.write(ls);
353 out.write( "my $startLoadWordTime = Time::HiRes::time(); " + ls );
354 out.write( "$Word->Documents->Open({Filename => $ARGV[0]}) " + ls );
355 out.write( " || die('Unable to open document ', Win32::OLE->LastError()); " + ls );
356 out.write( "my $stopLoadWordTime = Time::HiRes::time() - $startLoadWordTime; " + ls );
357 out.write(ls);
358 out.write( "my $startPrintWordTime = Time::HiRes::time(); " + ls);
359 out.write( "my $oldActivePrinte = $Word->{ActivePrinter} ; " + ls );
360 out.write( "$Word->{ActivePrinter} = $ARGV[1]; " + ls );
361 out.write( "$Word->ActiveDocument->PrintOut({ " + ls );
362 out.write( " Background => 0, " + ls );
363 out.write( " Append => 0, " + ls );
364 out.write( " Range => wdPrintAllDocument, " + ls );
365 out.write( " Item => wdPrintDocumentContent, " + ls );
366 out.write( " Copies => 1, " + ls );
367 out.write( " PageType => wdPrintAllPages, " + ls );
368 out.write( " PrintToFile => 1, " + ls );
369 out.write( " OutputFileName => $ARGV[2] " + ls );
370 out.write( " }); " + ls );
371 out.write( "$Word->{ActivePrinter} = $oldActivePrinte; " + ls );
372 out.write( "my $stopPrintWordTime = Time::HiRes::time() - $startPrintWordTime;" + ls);
374 out.write( "# ActiveDocument.Close(SaveChanges:=WdSaveOptions.wdDoNotSaveChanges)" + ls );
375 out.write( "my $sVersion = $Word->Application->Version();"+ls);
376 out.write( "$Word->ActiveDocument->Close({SaveChanges => 0}); " + ls );
377 out.write( "$Word->Quit(); " + ls );
379 out.write( "local *FILE;" + ls);
380 out.write( "if (open(FILE, \">$ENV{HOME}/msofficeloadtimes.txt\"))" + ls);
381 out.write( "{" + ls);
382 out.write( " print FILE \"name=$ARGV[0]\\n\";" + ls);
383 out.write( " print FILE \"WordVersion=$sVersion\\n\";" + ls);
384 out.write( " print FILE \"WordStartTime=$stopWordTime\\n\";" + ls);
385 out.write( " print FILE \"WordLoadTime=$stopLoadWordTime\\n\";" + ls);
386 out.write( " print FILE \"WordPrintTime=$stopPrintWordTime\\n\";" + ls);
387 out.write( " close(FILE);" + ls);
388 out.write( "}" + ls);
389 out.close();
391 aList.add("perl");
392 aList.add(sName);
393 return aList;
396 // TODO: Maybe give a possibility to say where search the script from outside
398 ArrayList searchLocalFile(String _sScriptName)
400 String userdir = System.getProperty("user.dir");
401 String fs = System.getProperty("file.separator");
403 ArrayList aList = new ArrayList();
404 File aPerlScript = new File(userdir + fs + _sScriptName);
405 if (FileHelper.isDebugEnabled())
407 GlobalLogWriter.get().println("Search for local existance of " + aPerlScript.getAbsolutePath());
410 if (aPerlScript.exists())
412 if (FileHelper.isDebugEnabled())
414 GlobalLogWriter.get().println("OK, found it, use this instead the internal one.");
417 String sName = aPerlScript.getAbsolutePath();
418 // String sCommand = "perl " + sName;
419 // System.out.println(sCommand);
420 aList.add("perl");
421 aList.add(sName);
422 return aList;
424 return aList;
427 ArrayList createWordStoreHelper() throws java.io.IOException
429 // create a program in tmp file
430 String sTmpPath = util.utils.getUsersTempDir();
431 String ls = System.getProperty("line.separator");
432 String fs = System.getProperty("file.separator");
434 // ArrayList aList = new ArrayList();
435 String sSaveViaWord = "saveViaWord.pl";
437 ArrayList aList = searchLocalFile(sSaveViaWord);
438 if (aList.isEmpty() == false)
440 return aList;
443 String sName = sTmpPath + fs + sSaveViaWord;
444 if (FileHelper.isDebugEnabled())
446 GlobalLogWriter.get().println("No local found, create a perl script: " + sName);
449 File aFile = new File(sName);
450 FileWriter out = new FileWriter(aFile.toString());
452 out.write( "eval 'exec perl -wS $0 ${1+\"$@\"}' " + ls );
453 out.write( " if 0; " + ls );
454 out.write( "use strict; " + ls );
455 out.write( " " + ls );
456 out.write( "if ( $^O ne \"MSWin32\") " + ls );
457 out.write( "{ " + ls );
458 out.write( " print 'Windows only.\\n'; " + ls );
459 out.write( " print_usage(); " + ls );
460 out.write( " exit(1); " + ls );
461 out.write( "} " + ls );
462 out.write( " " + ls );
463 out.write( "use Win32::OLE; " + ls );
464 out.write( "use Win32::OLE::Const 'Microsoft Word'; " + ls );
465 out.write( " " + ls );
466 out.write( "# ------ usage ------ " + ls );
467 out.write( "sub print_usage() " + ls );
468 out.write( "{ " + ls );
469 out.write( " print STDERR \"Usage: storeViaWord.pl <Word file> <output filer> <output file> \\n\" " + ls );
470 out.write( "} " + ls );
471 out.write( " " + ls );
472 out.write( " " + ls );
473 out.write( "if ($#ARGV != 2) " + ls );
474 out.write( "{ " + ls );
475 out.write( " print 'Too less arguments.\\n'; " + ls );
476 out.write( " print_usage(); " + ls );
477 out.write( " exit(1); " + ls );
478 out.write( "} " + ls );
479 out.write( " " + ls );
480 out.write( " " + ls );
481 out.write( "my $Word = Win32::OLE->new('Word.Application'); " + ls );
482 out.write( "# $Word->{'Visible'} = 1; # if you want to see what's going on " + ls );
483 out.write( "my $Book = $Word->Documents->Open($ARGV[0]) " + ls );
484 out.write( " || die('Unable to open document ', Win32::OLE->LastError()); " + ls );
485 out.write( "# my $oldActivePrinte = $Word->{ActivePrinter} ; " + ls );
486 out.write( "# $Word->{ActivePrinter} = $ARGV[1]; " + ls );
487 out.write( "# $Word->ActiveDocument->PrintOut({ " + ls );
488 out.write( "# Background => 0, " + ls );
489 out.write( "# Append => 0, " + ls );
490 out.write( "# Range => wdPrintAllDocument, " + ls );
491 out.write( "# Item => wdPrintDocumentContent, " + ls );
492 out.write( "# Copies => 1, " + ls );
493 out.write( "# PageType => wdPrintAllPages, " + ls );
494 out.write( "# PrintToFile => 1, " + ls );
495 out.write( "# OutputFileName => $ARGV[2] " + ls );
496 out.write( "# }); " + ls );
497 out.write( "# $Word->{ActivePrinter} = $oldActivePrinte; " + ls );
498 out.write( "$Book->savaAs($ARGV[2], $ARGV[1]); " + ls );
499 out.write( "# ActiveDocument.Close(SaveChanges:=WdSaveOptions.wdDoNotSaveChanges)" + ls );
500 out.write( "$Book->Close({SaveChanges => 0}); " + ls );
501 out.write( "$Word->Quit(); " + ls );
502 out.close();
504 aList.add("perl");
505 aList.add(sName);
506 return aList;
510 ArrayList createExcelPrintHelper() throws java.io.IOException
512 // create a program in tmp file
513 String sTmpPath = util.utils.getUsersTempDir();
514 String ls = System.getProperty("line.separator");
515 String fs = System.getProperty("file.separator");
517 String sPrintViaExcel = "printViaExcel.pl";
519 ArrayList aList = searchLocalFile(sPrintViaExcel);
520 if (aList.isEmpty() == false)
522 return aList;
524 String sName = sTmpPath + fs + sPrintViaExcel;
525 if (FileHelper.isDebugEnabled())
527 GlobalLogWriter.get().println("No local found, create a perl script: " + sName);
530 File aFile = new File(sName);
531 FileWriter out = new FileWriter(aFile.toString());
533 out.write( "eval 'exec perl -wS $0 ${1+\"$@\"}' " + ls );
534 out.write( " if 0; " + ls );
535 out.write( "use strict; " + ls );
536 out.write( " " + ls );
537 out.write( "if ( $^O ne \"MSWin32\") " + ls );
538 out.write( "{ " + ls );
539 out.write( " print \"Windows only.\\n\"; " + ls );
540 out.write( " print_usage(); " + ls );
541 out.write( " exit(1); " + ls );
542 out.write( "} " + ls );
543 out.write( " " + ls );
544 out.write( " " + ls );
545 out.write( "use Win32::OLE qw(in with); " + ls );
546 out.write( "use Win32::OLE::Const 'Microsoft Excel'; " + ls );
547 out.write( " " + ls );
548 out.write( "# ------ usage ------ " + ls );
549 out.write( "sub print_usage() " + ls );
550 out.write( "{ " + ls );
551 out.write( " print STDERR \"Usage: printViaExcel.pl <Excel file> <name of printer> <output file> .\\n " + ls );
552 out.write( " Please use the same string for the name of the printer as you can find \\n " + ls );
553 out.write( " under Start-Control Panel-Printer and Faxes \\n " + ls );
554 out.write( " The name could look like the the following line: \\n " + ls );
555 out.write( " Apple LaserWriter II NT v47.0 \\n " + ls );
556 out.write( " Sample command line: \\n " + ls );
557 out.write( " execl_print.pl c:\\book1.xls Apple LaserWriter II NT v47.0 c:\\output\\book1.ps \\n\"; " + ls );
558 out.write( "} " + ls );
559 out.write( " " + ls );
560 out.write( " " + ls );
561 out.write( " " + ls );
562 out.write( "$Win32::OLE::Warn = 3; # die on errors... " + ls );
563 out.write( " " + ls );
564 out.write( " " + ls );
565 out.write( "if ($#ARGV != 2) " + ls );
566 out.write( "{ " + ls );
567 out.write( " print STDERR \"Too less arguments.\\n\"; " + ls );
568 out.write( " print STDERR \"ARGV[0] $ARGV[0]\\n\"; " + ls );
569 out.write( " print STDERR \"ARGV[1] $ARGV[1]\\n\"; " + ls );
570 out.write( " print STDERR \"ARGV[2] $ARGV[2]\\n\"; " + ls );
571 out.write( " print_usage(); " + ls );
572 out.write( " exit(1); " + ls );
573 out.write( "} " + ls );
574 out.write( " " + ls );
575 out.write( "my $Excel = Win32::OLE->GetActiveObject('Excel.Application') " + ls );
576 out.write( " || Win32::OLE->new('Excel.Application', 'Quit'); # get already active Excel " + ls );
577 out.write( " # application or open new " + ls );
578 out.write( " " + ls );
579 out.write( " " + ls );
580 out.write( " " + ls );
581 out.write( "my $Book = $Excel->Workbooks->Open( $ARGV[0] ); " + ls );
582 out.write( " $Book->PrintOut({Copies => 1, " + ls );
583 out.write( " ActivePrinter => $ARGV[1], " + ls );
584 out.write( " PrToFileName => $ARGV[2], " + ls );
585 out.write( " Collate => 1 " + ls );
586 out.write( " }); " + ls );
587 out.write( "# Close worksheets without store changes" + ls );
588 out.write( "# $Book->Close({SaveChanges => 0}); " + ls );
589 out.write( "my $sVersion = $Excel->Application->Version();"+ls);
590 out.write( "$Excel->Quit(); " + ls );
591 out.write( "local *FILE;" + ls);
592 out.write( "if (open(FILE, \">$ENV{HOME}/msofficeloadtimes.txt\"))" + ls);
593 out.write( "{" + ls);
594 out.write( " print FILE \"name=$ARGV[0]\\n\";" + ls);
595 out.write( " print FILE \"ExcelVersion=$sVersion\\n\";" + ls);
596 // out.write( " print FILE \"WordStartTime=$stopWordTime\\n\";" + ls);
597 // out.write( " print FILE \"WordLoadTime=$stopLoadWordTime\\n\";" + ls);
598 // out.write( " print FILE \"WordPrintTime=$stopPrintWordTime\\n\";" + ls);
599 out.write( " close(FILE);" + ls);
600 out.write( "}" + ls);
601 out.close();
603 aList.add("perl");
604 aList.add(sName);
605 return aList;
608 ArrayList createExcelStoreHelper() throws java.io.IOException
610 // create a program in tmp file
611 String sTmpPath = util.utils.getUsersTempDir();
612 String ls = System.getProperty("line.separator");
613 String fs = System.getProperty("file.separator");
615 String sSaveViaExcel = "saveViaExcel.pl";
617 ArrayList aList = searchLocalFile(sSaveViaExcel);
618 if (aList.isEmpty() == false)
620 return aList;
622 String sName = sTmpPath + fs + sSaveViaExcel;
623 if (FileHelper.isDebugEnabled())
625 GlobalLogWriter.get().println("No local found, create a script: " + sName);
628 File aFile = new File(sName);
629 FileWriter out = new FileWriter(aFile.toString());
631 out.write( "eval 'exec perl -wS $0 ${1+\"$@\"}' " + ls );
632 out.write( " if 0; " + ls );
633 out.write( "use strict; " + ls );
634 out.write( "# This script is automatically created. " + ls );
635 out.write( " " + ls );
636 out.write( "use Win32::OLE qw(in with); " + ls );
637 out.write( "use Win32::OLE::Const 'Microsoft Excel'; " + ls );
638 out.write( " " + ls );
639 out.write( "# ------ usage ------ " + ls );
640 out.write( "sub print_usage() " + ls );
641 out.write( "{ " + ls );
642 out.write( " print STDERR \"Usage: savaViaExcel.pl <Excel file> <filefilter> <output file> .\\n " + ls );
643 out.write( " execl_print.pl c:\\book1.xls Apple LaserWriter II NT v47.0 c:\\output\\book1.ps \\n\"; " + ls );
644 out.write( "} " + ls );
645 out.write( " " + ls );
646 out.write( " " + ls );
647 out.write( " " + ls );
648 out.write( "$Win32::OLE::Warn = 3; # die on errors... " + ls );
649 out.write( " " + ls );
650 out.write( " " + ls );
651 out.write( "if ($#ARGV != 2) " + ls );
652 out.write( "{ " + ls );
653 out.write( " print \"Too less arguments.\\n\"; " + ls );
654 out.write( " print_usage(); " + ls );
655 out.write( " exit(1); " + ls );
656 out.write( "} " + ls );
657 out.write( " " + ls );
658 out.write( "my $Excel = Win32::OLE->GetActiveObject('Excel.Application') " + ls );
659 out.write( " || Win32::OLE->new('Excel.Application', 'Quit'); # get already active Excel " + ls );
660 out.write( " # application or open new " + ls );
661 out.write( "my $sFilterParameter = $ARGV[1]; " + ls );
662 out.write( "my $sFilterName = xlHTML; " + ls );
663 out.write( "if ($sFilterParameter eq 'xlXMLSpreadsheet') " + ls );
664 out.write( "{ " + ls );
665 out.write( " $sFilterName = xlXMLSpreadsheet; " + ls );
666 out.write( "} " + ls );
667 out.write( "elsif ($sFilterParameter eq 'xlHTML') " + ls );
668 out.write( "{ " + ls );
669 out.write( " $sFilterName = xlHTML; " + ls );
670 out.write( "} " + ls );
671 out.write( "else " + ls );
672 out.write( "{ " + ls );
673 out.write( " my $undefined; " + ls);
674 out.write( " $sFilterName = $undefined; " + ls );
675 out.write( "} " + ls );
676 out.write( " " + ls );
677 out.write( "my $Book = $Excel->Workbooks->Open( $ARGV[0] ); " + ls );
678 out.write( "$Excel->{DisplayAlerts} = 0; " + ls );
679 out.write( "$Book->saveAs($ARGV[2], " + ls );
680 out.write( " $sFilterName, " + ls );
681 out.write( " '', " + ls );
682 out.write( " '', " + ls );
683 out.write( " 0, " + ls );
684 out.write( " 0, " + ls );
685 out.write( " xlNoChange, " + ls );
686 out.write( " xlLocalSessionChanges, " + ls );
687 out.write( " 1); " + ls );
688 out.write( "# Close worksheets without store changes" + ls );
689 out.write( "# $Book->Close({SaveChanges => 0}); " + ls );
690 out.write( "$Excel->Quit(); " + ls );
691 out.close();
693 aList.add("perl");
694 aList.add(sName);
695 return aList;
698 ArrayList createPowerPointPrintHelper() throws java.io.IOException
700 // create a program in tmp file
701 String sTmpPath = util.utils.getUsersTempDir();
702 String ls = System.getProperty("line.separator");
703 String fs = System.getProperty("file.separator");
705 String sPrintViaPowerPoint = "printViaPowerPoint.pl";
707 ArrayList aList = searchLocalFile(sPrintViaPowerPoint);
708 if (aList.isEmpty() == false)
710 return aList;
712 String sName = sTmpPath + fs + sPrintViaPowerPoint;
713 if (FileHelper.isDebugEnabled())
715 GlobalLogWriter.get().println("No local found, create a script: " + sName);
718 File aFile = new File(sName);
719 FileWriter out = new FileWriter(aFile.toString());
722 out.write( "eval 'exec perl -wS $0 $1 $2 ' " + ls );
723 out.write( " if 0; " + ls );
724 out.write( "use strict; " + ls );
725 out.write( " " + ls );
726 out.write( "if ( $^O ne \"MSWin32\") " + ls );
727 out.write( "{ " + ls );
728 out.write( " print \"Windows only.\\n\"; " + ls );
729 out.write( " print_usage(); " + ls );
730 out.write( " exit(1); " + ls );
731 out.write( "} " + ls );
732 out.write( " " + ls );
733 out.write( " " + ls );
734 out.write( "use Win32::OLE qw(in with); " + ls );
735 out.write( "use Win32::OLE::Const 'Microsoft PowerPoint'; " + ls );
736 out.write( " " + ls );
737 out.write( "# ------ usage ------ " + ls );
738 out.write( "sub print_usage() " + ls );
739 out.write( "{ " + ls );
740 out.write( " print STDERR \"Usage: powerpoint_print.pl <PowerPoint file> <name of printer> <output file> .\\n " + ls );
741 out.write( " Please use the same string for the name of the printer as you can find \\n " + ls );
742 out.write( " under Start-Control Panel-Printer and Faxes \\n " + ls );
743 out.write( " The name could look like the the following line: \\n " + ls );
744 out.write( " Apple LaserWriter II NT v47.0 \\n " + ls );
745 out.write( " Sample command line: \\n " + ls );
746 out.write( " powerpoint_print.pl c:\\book.ppt Apple LaserWriter II NT v47.0 c:\\output\\book.ps \\n\"; " + ls );
747 out.write( "} " + ls );
748 out.write( " " + ls );
749 out.write( " " + ls );
750 out.write( " " + ls );
751 out.write( "$Win32::OLE::Warn = 3; # die on errors... " + ls );
752 out.write( " " + ls );
753 out.write( " " + ls );
754 out.write( "if ($#ARGV < 2) " + ls );
755 out.write( "{ " + ls );
756 out.write( " print \"Too less arguments.\\n\"; " + ls );
757 out.write( " print_usage(); " + ls );
758 out.write( " exit(1); " + ls );
759 out.write( "} " + ls );
760 out.write( " " + ls );
761 out.write( "my $PowerPoint = Win32::OLE->GetActiveObject('PowerPoint.Application') " + ls );
762 out.write( " || Win32::OLE->new('PowerPoint.Application', 'Quit'); # get already active Excel " + ls );
763 out.write( " # application or open new " + ls );
764 out.write( " " + ls );
765 out.write( " " + ls );
766 out.write( " " + ls );
767 out.write( " $PowerPoint->{'Visible'} = 1; " + ls );
768 out.write( " my $Presentation = $PowerPoint->Presentations->Add; " + ls );
769 out.write( " my $Presentation = $PowerPoint->Presentations->Open( $ARGV[0] ); " + ls );
770 out.write( "# we can't change active printer in powerpoint " + ls );
771 out.write( "# $Presentation->PrintOptions->{ActivePrinter} = $ARGV[1]; " + ls );
772 out.write( " print \"Active printer is: \" . $Presentation->PrintOptions->{ActivePrinter} . \"\\n\"; " + ls );
773 out.write( " $Presentation->PrintOptions->{PrintInBackground} = 0; " + ls );
774 out.write( " # PrintColorType = 1 means print in color and PrintColorType = 2 means print in gray " + ls );
775 out.write( " $Presentation->PrintOptions->{PrintColorType} = 1; " + ls );
776 out.write( " " + ls );
777 out.write( " $Presentation->PrintOut({PrintToFile => $ARGV[2]}); " + ls );
778 out.write( " sleep 5; " + ls );
779 out.write( " print \"Presentation has been printed\\n\"; " + ls );
780 out.write( "my $sVersion = $Presentation->Application->Version();"+ls);
781 out.write( " $PowerPoint->Quit(); " + ls );
783 out.write( "local *FILE;" + ls);
784 out.write( "if (open(FILE, \">$ENV{HOME}/msofficeloadtimes.txt\"))" + ls);
785 out.write( "{" + ls);
786 out.write( " print FILE \"name=$ARGV[0]\\n\";" + ls);
787 out.write( " print FILE \"PowerPointVersion=$sVersion\\n\";" + ls);
788 // out.write( " print FILE \"WordStartTime=$stopWordTime\\n\";" + ls);
789 // out.write( " print FILE \"WordLoadTime=$stopLoadWordTime\\n\";" + ls);
790 // out.write( " print FILE \"WordPrintTime=$stopPrintWordTime\\n\";" + ls);
791 out.write( " close(FILE);" + ls);
792 out.write( "}" + ls);
793 out.close();
795 aList.add("perl");
796 aList.add(sName);
797 return aList;
801 @param _sFilename a name to a ms office xml file
802 @return 'word' or 'excel' or '' if type not known
804 public String getOfficeType(String _sFilename)
806 File aFile = new File(_sFilename);
807 if (! aFile.exists())
809 GlobalLogWriter.get().println("couldn't find file " + _sFilename);
810 return "";
812 RandomAccessFile aReader = null;
813 String sOfficeType = "";
816 aReader = new RandomAccessFile(aFile,"r");
817 String aLine = "";
818 while (aLine != null)
820 aLine = aReader.readLine();
821 if (aLine != null)
823 aLine = aLine.trim();
824 if ( (! (aLine.length() < 2) ) &&
825 (! aLine.startsWith("#")) &&
826 (! aLine.startsWith(";")) )
828 int nIdx = aLine.indexOf("mso-application");
829 if (nIdx > 0)
831 if (aLine.indexOf("Word.Document") > 0)
833 sOfficeType = "word";
835 else if (aLine.indexOf("Excel") > 0)
837 sOfficeType = "excel";
839 else
841 GlobalLogWriter.get().println("Unknown/unsupported data file: " + aLine);
848 catch (java.io.FileNotFoundException fne)
850 System.out.println("couldn't open file " + _sFilename);
851 System.out.println("Message: " + fne.getMessage());
853 catch (java.io.IOException ie)
855 System.out.println("Exception while reading file " + _sFilename);
856 System.out.println("Message: " + ie.getMessage());
860 aReader.close();
862 catch (java.io.IOException ie)
864 System.out.println("Couldn't close file " + _sFilename);
865 System.out.println("Message: " + ie.getMessage());
867 return sOfficeType;