bump product version to 4.1.6.2
[LibreOffice.git] / qadevOOo / runner / convwatch / MSOfficePrint.java
blobaa881b5087cfb6900ca0870c1ceecd2fd814e6d0
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package convwatch;
21 import convwatch.FileHelper;
22 import java.io.File;
23 import java.io.FileWriter;
24 import java.io.RandomAccessFile;
25 import convwatch.GraphicalTestArguments;
26 import helper.ProcessHandler;
27 import java.util.ArrayList;
28 import helper.OSHelper;
30 /**
31 * This object gives all functionallity to print msoffice documents.
32 * It also offers functions to check what type of document it is.
33 * It handles *.doc as word documents and use word to print
34 * *.xls as excel
35 * *.ppt as powerpoint
38 class ProcessHelper
40 ArrayList<String> m_aArray;
43 public class MSOfficePrint
45 private String m_sPrinterName; // within Windows the tools need a printer name;
47 public void setPrinterName(String _s) {m_sPrinterName = _s;}
49 // -----------------------------------------------------------------------------
50 static boolean isWordDocument(String _sSuffix)
52 if (_sSuffix.toLowerCase().endsWith(".doc") ||
53 _sSuffix.toLowerCase().endsWith(".rtf") ||
54 _sSuffix.toLowerCase().endsWith(".dot"))
56 return true;
58 return false;
61 static boolean isExcelDocument(String _sSuffix)
63 // xlt templates
64 // xlw
65 // xla addin
66 if (_sSuffix.toLowerCase().endsWith(".xls"))
68 return true;
70 /* temporal insertion by SUS
71 if (_sSuffix.endsWith(".xml"))
73 return true;
74 }*/
75 return false;
78 static boolean isPowerPointDocument(String _sSuffix)
80 if (_sSuffix.toLowerCase().endsWith(".pps") ||
81 _sSuffix.toLowerCase().endsWith(".ppt"))
83 return true;
85 return false;
88 /**
89 * returns true, if the given filename has a MS Office suffix.
91 public static boolean isMSOfficeDocumentFormat(String _sFile)
93 String sDocumentSuffix = FileHelper.getSuffix(_sFile);
94 if (isWordDocument(sDocumentSuffix)) return true;
95 if (isExcelDocument(sDocumentSuffix)) return true;
96 if (isPowerPointDocument(sDocumentSuffix)) return true;
97 // if suffix is xml, return also true, but we can't decide if word or excel
98 if (sDocumentSuffix.toLowerCase().endsWith(".xml")) return true;
99 return false;
102 public void storeToFileWithMSOffice( GraphicalTestArguments _aGTA,
103 String _sInputFile,
104 String _sOutputFile) throws ConvWatchCancelException, java.io.IOException
106 String sDocumentSuffix = FileHelper.getSuffix(_sInputFile);
107 String sFilterName = _aGTA.getExportFilterName();
108 ArrayList<String> aStartCommand = new ArrayList<String>();
109 if (isWordDocument(sDocumentSuffix))
111 aStartCommand = createWordStoreHelper();
113 else if (isExcelDocument(sDocumentSuffix))
115 aStartCommand = createExcelStoreHelper();
117 else if (isPowerPointDocument(sDocumentSuffix))
120 else if (sDocumentSuffix.toLowerCase().equals(".xml"))
122 // special case, if xml we prefer word, but with DEFAULT_XML_FORMAT_APP=excel it's changeable.
123 if (_aGTA.getDefaultXMLFormatApp().toLowerCase().equals("excel"))
125 aStartCommand = createExcelStoreHelper();
127 // else
128 // {
129 // }
131 else
133 GlobalLogWriter.get().println("No Microsoft Office document format found.");
134 // TODO: use a better Exception!!!
135 throw new ConvWatchCancelException/*WrongSuffixException*/("No MS office document format found.");
137 if (aStartCommand != null)
139 if (sFilterName == null)
141 // TODO: hardcoded FilterName in perl script
142 sFilterName = ""; // xlXMLSpreadsheet";
145 // String sCommand = sStartCommand + " " +
146 // _sInputFile + " " +
147 // StringHelper.doubleQuote(sFilterName) + " " +
148 // _sOutputFile;
150 aStartCommand.add(_sInputFile);
151 aStartCommand.add(sFilterName);
152 aStartCommand.add(_sOutputFile);
153 realStartCommand(aStartCommand);
157 // -----------------------------------------------------------------------------
159 * print the given file (_sInputFile) to the file name (_sPrintFile)
161 public void printToFileWithMSOffice( GraphicalTestArguments _aGTA,
162 String _sInputFile,
163 String _sPrintFilename) throws ConvWatchCancelException, java.io.IOException
165 String sDocumentSuffix = FileHelper.getSuffix(_sInputFile);
167 setPrinterName(_aGTA.getPrinterName());
169 ArrayList<String> aStartCommand = new ArrayList<String>();
170 if (isWordDocument(sDocumentSuffix))
172 aStartCommand = createWordPrintHelper();
174 else if (isExcelDocument(sDocumentSuffix))
176 aStartCommand = createExcelPrintHelper();
178 else if (isPowerPointDocument(sDocumentSuffix))
180 aStartCommand = createPowerPointPrintHelper();
182 else if (sDocumentSuffix.toLowerCase().equals(".xml"))
184 // TODO: Open XML File and check if we need excel or word
185 String sOfficeType = getOfficeType(_sInputFile);
187 // special case, if xml we prefer word, but with DEFAULT_XML_FORMAT_APP=excel it's changeable.
188 // if (_aGTA.getDefaultXMLFormatApp().toLowerCase().equals("excel"))
189 if (sOfficeType.equals("excel"))
191 aStartCommand = createExcelPrintHelper();
193 else if (sOfficeType.equals("word"))
195 aStartCommand = createWordPrintHelper();
197 else
199 return;
202 else
204 GlobalLogWriter.get().println("No Microsoft Office document format found.");
205 // TODO: use a better Exception!!!
206 throw new ConvWatchCancelException/*WrongSuffixException*/("No Mircosoft Office document format found.");
209 if (aStartCommand.isEmpty() == false)
211 String sPrinterName = m_sPrinterName;
212 if (sPrinterName == null)
214 sPrinterName = "";
217 // String sCommand = sStartCommand + " " +
218 // _sInputFile + " " +
219 // StringHelper.doubleQuote(m_sPrinterName) + " " +
220 // _sPrintFilename;
221 aStartCommand.add(_sInputFile);
222 aStartCommand.add(m_sPrinterName);
223 aStartCommand.add(_sPrintFilename);
225 realStartCommand(aStartCommand);
227 String sUserDir = System.getProperty("user.home");
228 String fs = System.getProperty("file.separator");
229 if (! sUserDir.endsWith(fs))
231 sUserDir = sUserDir + fs;
233 _aGTA.getPerformance().readWordValuesFromFile(sUserDir + "msofficeloadtimes.txt");
234 OfficePrint.createInfoFile(_sPrintFilename, _aGTA, "msoffice");
235 TimeHelper.waitInSeconds(2, "Give Microsoft Office some time to print.");
238 public void realStartCommand(ArrayList<String> _aStartCommand) throws ConvWatchCancelException
240 if (_aStartCommand.isEmpty())
242 throw new ConvWatchCancelException/*WrongEnvironmentException*/("Given list is empty.");
247 // Convert the StartCommand ArrayList to a String List
248 int nValues = _aStartCommand.size();
249 String[] aList = new String[nValues];
250 for (int i=0;i<nValues;i++)
252 String aStr = _aStartCommand.get(i);
253 if (aStr == null)
255 aStr = "";
257 if (aStr.length() == 0)
259 aStr = "\"\"";
261 aList[i] = new String(aStr);
264 // This is really the latest point where we can check if we are running within windows environment
265 if (! OSHelper.isWindows())
267 // TODO: use a better Exception!!!
268 throw new ConvWatchCancelException/*WrongEnvironmentException*/("We doesn't work within windows environment.");
272 ProcessHandler aHandler = new ProcessHandler(aList);
273 aHandler.executeSynchronously();
275 catch (IndexOutOfBoundsException e)
277 throw new ConvWatchCancelException/*WrongEnvironmentException*/("Given list is too short.");
280 // return aHandler.getExitCode();
284 ArrayList<String> createWordPrintHelper() throws java.io.IOException
286 // create a program in tmp file
287 String sTmpPath = util.utils.getUsersTempDir();
288 String ls = System.getProperty("line.separator");
289 String fs = System.getProperty("file.separator");
291 String sPrintViaWord = "printViaWord.pl";
293 ArrayList<String> aList = searchLocalFile(sPrintViaWord);
294 if (aList.isEmpty() == false)
296 return aList;
299 String sName = sTmpPath + fs + sPrintViaWord;
300 File aFile = new File(sName);
301 FileWriter out = new FileWriter(aFile.toString());
304 out.write( "eval 'exec perl -wS $0 ${1+\"$@\"}' " + ls );
305 out.write( " if 0; " + ls );
306 out.write( "use strict; " + ls );
307 out.write( "use Time::HiRes; " + ls );
308 out.write( "if ( $^O ne \"MSWin32\") " + ls );
309 out.write( "{ " + ls );
310 out.write( " print 'Windows only.\\n'; " + ls );
311 out.write( " print_usage(); " + ls );
312 out.write( " exit(1); " + ls );
313 out.write( "} " + ls );
314 out.write( " " + ls );
315 out.write( "use Win32::OLE; " + ls );
316 out.write( "use Win32::OLE::Const 'Microsoft Word'; " + ls );
317 out.write( " " + ls );
318 out.write( "# ------ usage ------ " + ls );
319 out.write( "sub print_usage() " + ls );
320 out.write( "{ " + ls );
321 out.write( " print STDERR \"Usage: word_print.pl <Word file> <name of printer> <output file> .\\n " + ls );
322 out.write( " Please use the same string for the name of the printer as you can find \\n " + ls );
323 out.write( " under Start-Control Panel-Printer and Faxes \\n " + ls );
324 out.write( " The name could look like the following line: \\n " + ls );
325 out.write( " Apple LaserWriter II NT v47.0 \\n " + ls );
326 out.write( " Sample command line: \\n " + ls );
327 out.write( " execl_print.pl c:\\book1.doc Apple LaserWriter II NT v47.0 c:\\output\\book1.ps \\n\"; " + ls );
328 out.write( "} " + ls );
329 out.write( " " + ls );
330 out.write( " " + ls );
331 out.write( "if ($#ARGV != 2) " + ls );
332 out.write( "{ " + ls );
333 out.write( " print 'Too less arguments.\\n'; " + ls );
334 out.write( " print_usage(); " + ls );
335 out.write( " exit(1); " + ls );
336 out.write( "} " + ls );
337 out.write( " " + ls );
338 out.write( "my $startWordTime = Time::HiRes::time(); " + ls );
339 out.write( "my $Word = Win32::OLE->new('Word.Application'); " + ls );
340 out.write( "my $stopWordTime = Time::HiRes::time() - $startWordTime; " + ls );
341 out.write( "# $Word->{'Visible'} = 1; # if you want to see what's going on " + ls );
342 out.write( "# , ReadOnly => 1})" + ls );
343 out.write(ls);
344 out.write( "my $startLoadWordTime = Time::HiRes::time(); " + ls );
345 out.write( "$Word->Documents->Open({Filename => $ARGV[0]}) " + ls );
346 out.write( " || die('Unable to open document ', Win32::OLE->LastError()); " + ls );
347 out.write( "my $stopLoadWordTime = Time::HiRes::time() - $startLoadWordTime; " + ls );
348 out.write(ls);
349 out.write( "my $startPrintWordTime = Time::HiRes::time(); " + ls);
350 out.write( "my $oldActivePrinte = $Word->{ActivePrinter} ; " + ls );
351 out.write( "$Word->{ActivePrinter} = $ARGV[1]; " + ls );
352 out.write( "$Word->ActiveDocument->PrintOut({ " + ls );
353 out.write( " Background => 0, " + ls );
354 out.write( " Append => 0, " + ls );
355 out.write( " Range => wdPrintAllDocument, " + ls );
356 out.write( " Item => wdPrintDocumentContent, " + ls );
357 out.write( " Copies => 1, " + ls );
358 out.write( " PageType => wdPrintAllPages, " + ls );
359 out.write( " PrintToFile => 1, " + ls );
360 out.write( " OutputFileName => $ARGV[2] " + ls );
361 out.write( " }); " + ls );
362 out.write( "$Word->{ActivePrinter} = $oldActivePrinte; " + ls );
363 out.write( "my $stopPrintWordTime = Time::HiRes::time() - $startPrintWordTime;" + ls);
365 out.write( "# ActiveDocument.Close(SaveChanges:=WdSaveOptions.wdDoNotSaveChanges)" + ls );
366 out.write( "my $sVersion = $Word->Application->Version();"+ls);
367 out.write( "$Word->ActiveDocument->Close({SaveChanges => 0}); " + ls );
368 out.write( "$Word->Quit(); " + ls );
370 out.write( "local *FILE;" + ls);
371 out.write( "if (open(FILE, \">$ENV{HOME}/msofficeloadtimes.txt\"))" + ls);
372 out.write( "{" + ls);
373 out.write( " print FILE \"name=$ARGV[0]\\n\";" + ls);
374 out.write( " print FILE \"WordVersion=$sVersion\\n\";" + ls);
375 out.write( " print FILE \"WordStartTime=$stopWordTime\\n\";" + ls);
376 out.write( " print FILE \"WordLoadTime=$stopLoadWordTime\\n\";" + ls);
377 out.write( " print FILE \"WordPrintTime=$stopPrintWordTime\\n\";" + ls);
378 out.write( " close(FILE);" + ls);
379 out.write( "}" + ls);
380 out.close();
382 aList.add("perl");
383 aList.add(sName);
384 return aList;
387 // TODO: Maybe give a possibility to say where search the script from outside
389 ArrayList<String> searchLocalFile(String _sScriptName)
391 String userdir = System.getProperty("user.dir");
392 String fs = System.getProperty("file.separator");
394 ArrayList<String> aList = new ArrayList<String>();
395 File aPerlScript = new File(userdir + fs + _sScriptName);
396 if (FileHelper.isDebugEnabled())
398 GlobalLogWriter.get().println("Search for local existance of " + aPerlScript.getAbsolutePath());
401 if (aPerlScript.exists())
403 if (FileHelper.isDebugEnabled())
405 GlobalLogWriter.get().println("OK, found it, use this instead the internal one.");
408 String sName = aPerlScript.getAbsolutePath();
409 // String sCommand = "perl " + sName;
410 // System.out.println(sCommand);
411 aList.add("perl");
412 aList.add(sName);
413 return aList;
415 return aList;
418 ArrayList<String> createWordStoreHelper() throws java.io.IOException
420 // create a program in tmp file
421 String sTmpPath = util.utils.getUsersTempDir();
422 String ls = System.getProperty("line.separator");
423 String fs = System.getProperty("file.separator");
425 // ArrayList aList = new ArrayList();
426 String sSaveViaWord = "saveViaWord.pl";
428 ArrayList<String> aList = searchLocalFile(sSaveViaWord);
429 if (aList.isEmpty() == false)
431 return aList;
434 String sName = sTmpPath + fs + sSaveViaWord;
435 if (FileHelper.isDebugEnabled())
437 GlobalLogWriter.get().println("No local found, create a perl script: " + sName);
440 File aFile = new File(sName);
441 FileWriter out = new FileWriter(aFile.toString());
443 out.write( "eval 'exec perl -wS $0 ${1+\"$@\"}' " + ls );
444 out.write( " if 0; " + ls );
445 out.write( "use strict; " + ls );
446 out.write( " " + ls );
447 out.write( "if ( $^O ne \"MSWin32\") " + ls );
448 out.write( "{ " + ls );
449 out.write( " print 'Windows only.\\n'; " + ls );
450 out.write( " print_usage(); " + ls );
451 out.write( " exit(1); " + ls );
452 out.write( "} " + ls );
453 out.write( " " + ls );
454 out.write( "use Win32::OLE; " + ls );
455 out.write( "use Win32::OLE::Const 'Microsoft Word'; " + ls );
456 out.write( " " + ls );
457 out.write( "# ------ usage ------ " + ls );
458 out.write( "sub print_usage() " + ls );
459 out.write( "{ " + ls );
460 out.write( " print STDERR \"Usage: storeViaWord.pl <Word file> <output filer> <output file> \\n\" " + ls );
461 out.write( "} " + ls );
462 out.write( " " + ls );
463 out.write( " " + ls );
464 out.write( "if ($#ARGV != 2) " + ls );
465 out.write( "{ " + ls );
466 out.write( " print 'Too less arguments.\\n'; " + ls );
467 out.write( " print_usage(); " + ls );
468 out.write( " exit(1); " + ls );
469 out.write( "} " + ls );
470 out.write( " " + ls );
471 out.write( " " + ls );
472 out.write( "my $Word = Win32::OLE->new('Word.Application'); " + ls );
473 out.write( "# $Word->{'Visible'} = 1; # if you want to see what's going on " + ls );
474 out.write( "my $Book = $Word->Documents->Open($ARGV[0]) " + ls );
475 out.write( " || die('Unable to open document ', Win32::OLE->LastError()); " + ls );
476 out.write( "# my $oldActivePrinte = $Word->{ActivePrinter} ; " + ls );
477 out.write( "# $Word->{ActivePrinter} = $ARGV[1]; " + ls );
478 out.write( "# $Word->ActiveDocument->PrintOut({ " + ls );
479 out.write( "# Background => 0, " + ls );
480 out.write( "# Append => 0, " + ls );
481 out.write( "# Range => wdPrintAllDocument, " + ls );
482 out.write( "# Item => wdPrintDocumentContent, " + ls );
483 out.write( "# Copies => 1, " + ls );
484 out.write( "# PageType => wdPrintAllPages, " + ls );
485 out.write( "# PrintToFile => 1, " + ls );
486 out.write( "# OutputFileName => $ARGV[2] " + ls );
487 out.write( "# }); " + ls );
488 out.write( "# $Word->{ActivePrinter} = $oldActivePrinte; " + ls );
489 out.write( "$Book->savaAs($ARGV[2], $ARGV[1]); " + ls );
490 out.write( "# ActiveDocument.Close(SaveChanges:=WdSaveOptions.wdDoNotSaveChanges)" + ls );
491 out.write( "$Book->Close({SaveChanges => 0}); " + ls );
492 out.write( "$Word->Quit(); " + ls );
493 out.close();
495 aList.add("perl");
496 aList.add(sName);
497 return aList;
501 ArrayList<String> createExcelPrintHelper() throws java.io.IOException
503 // create a program in tmp file
504 String sTmpPath = util.utils.getUsersTempDir();
505 String ls = System.getProperty("line.separator");
506 String fs = System.getProperty("file.separator");
508 String sPrintViaExcel = "printViaExcel.pl";
510 ArrayList<String> aList = searchLocalFile(sPrintViaExcel);
511 if (aList.isEmpty() == false)
513 return aList;
515 String sName = sTmpPath + fs + sPrintViaExcel;
516 if (FileHelper.isDebugEnabled())
518 GlobalLogWriter.get().println("No local found, create a perl script: " + sName);
521 File aFile = new File(sName);
522 FileWriter out = new FileWriter(aFile.toString());
524 out.write( "eval 'exec perl -wS $0 ${1+\"$@\"}' " + ls );
525 out.write( " if 0; " + ls );
526 out.write( "use strict; " + ls );
527 out.write( " " + ls );
528 out.write( "if ( $^O ne \"MSWin32\") " + ls );
529 out.write( "{ " + ls );
530 out.write( " print \"Windows only.\\n\"; " + ls );
531 out.write( " print_usage(); " + ls );
532 out.write( " exit(1); " + ls );
533 out.write( "} " + ls );
534 out.write( " " + ls );
535 out.write( " " + ls );
536 out.write( "use Win32::OLE qw(in with); " + ls );
537 out.write( "use Win32::OLE::Const 'Microsoft Excel'; " + ls );
538 out.write( " " + ls );
539 out.write( "# ------ usage ------ " + ls );
540 out.write( "sub print_usage() " + ls );
541 out.write( "{ " + ls );
542 out.write( " print STDERR \"Usage: printViaExcel.pl <Excel file> <name of printer> <output file> .\\n " + ls );
543 out.write( " Please use the same string for the name of the printer as you can find \\n " + ls );
544 out.write( " under Start-Control Panel-Printer and Faxes \\n " + ls );
545 out.write( " The name could look like the following line: \\n " + ls );
546 out.write( " Apple LaserWriter II NT v47.0 \\n " + ls );
547 out.write( " Sample command line: \\n " + ls );
548 out.write( " execl_print.pl c:\\book1.xls Apple LaserWriter II NT v47.0 c:\\output\\book1.ps \\n\"; " + ls );
549 out.write( "} " + ls );
550 out.write( " " + ls );
551 out.write( " " + ls );
552 out.write( " " + ls );
553 out.write( "$Win32::OLE::Warn = 3; # die on errors... " + ls );
554 out.write( " " + ls );
555 out.write( " " + ls );
556 out.write( "if ($#ARGV != 2) " + ls );
557 out.write( "{ " + ls );
558 out.write( " print STDERR \"Too less arguments.\\n\"; " + ls );
559 out.write( " print STDERR \"ARGV[0] $ARGV[0]\\n\"; " + ls );
560 out.write( " print STDERR \"ARGV[1] $ARGV[1]\\n\"; " + ls );
561 out.write( " print STDERR \"ARGV[2] $ARGV[2]\\n\"; " + ls );
562 out.write( " print_usage(); " + ls );
563 out.write( " exit(1); " + ls );
564 out.write( "} " + ls );
565 out.write( " " + ls );
566 out.write( "my $Excel = Win32::OLE->GetActiveObject('Excel.Application') " + ls );
567 out.write( " || Win32::OLE->new('Excel.Application', 'Quit'); # get already active Excel " + ls );
568 out.write( " # application or open new " + ls );
569 out.write( " " + ls );
570 out.write( " " + ls );
571 out.write( " " + ls );
572 out.write( "my $Book = $Excel->Workbooks->Open( $ARGV[0] ); " + ls );
573 out.write( " $Book->PrintOut({Copies => 1, " + ls );
574 out.write( " ActivePrinter => $ARGV[1], " + ls );
575 out.write( " PrToFileName => $ARGV[2], " + ls );
576 out.write( " Collate => 1 " + ls );
577 out.write( " }); " + ls );
578 out.write( "# Close worksheets without store changes" + ls );
579 out.write( "# $Book->Close({SaveChanges => 0}); " + ls );
580 out.write( "my $sVersion = $Excel->Application->Version();"+ls);
581 out.write( "$Excel->Quit(); " + ls );
582 out.write( "local *FILE;" + ls);
583 out.write( "if (open(FILE, \">$ENV{HOME}/msofficeloadtimes.txt\"))" + ls);
584 out.write( "{" + ls);
585 out.write( " print FILE \"name=$ARGV[0]\\n\";" + ls);
586 out.write( " print FILE \"ExcelVersion=$sVersion\\n\";" + ls);
587 // out.write( " print FILE \"WordStartTime=$stopWordTime\\n\";" + ls);
588 // out.write( " print FILE \"WordLoadTime=$stopLoadWordTime\\n\";" + ls);
589 // out.write( " print FILE \"WordPrintTime=$stopPrintWordTime\\n\";" + ls);
590 out.write( " close(FILE);" + ls);
591 out.write( "}" + ls);
592 out.close();
594 aList.add("perl");
595 aList.add(sName);
596 return aList;
599 ArrayList<String> createExcelStoreHelper() throws java.io.IOException
601 // create a program in tmp file
602 String sTmpPath = util.utils.getUsersTempDir();
603 String ls = System.getProperty("line.separator");
604 String fs = System.getProperty("file.separator");
606 String sSaveViaExcel = "saveViaExcel.pl";
608 ArrayList<String> aList = searchLocalFile(sSaveViaExcel);
609 if (aList.isEmpty() == false)
611 return aList;
613 String sName = sTmpPath + fs + sSaveViaExcel;
614 if (FileHelper.isDebugEnabled())
616 GlobalLogWriter.get().println("No local found, create a script: " + sName);
619 File aFile = new File(sName);
620 FileWriter out = new FileWriter(aFile.toString());
622 out.write( "eval 'exec perl -wS $0 ${1+\"$@\"}' " + ls );
623 out.write( " if 0; " + ls );
624 out.write( "use strict; " + ls );
625 out.write( "# This script is automatically created. " + ls );
626 out.write( " " + ls );
627 out.write( "use Win32::OLE qw(in with); " + ls );
628 out.write( "use Win32::OLE::Const 'Microsoft Excel'; " + ls );
629 out.write( " " + ls );
630 out.write( "# ------ usage ------ " + ls );
631 out.write( "sub print_usage() " + ls );
632 out.write( "{ " + ls );
633 out.write( " print STDERR \"Usage: savaViaExcel.pl <Excel file> <filefilter> <output file> .\\n " + ls );
634 out.write( " execl_print.pl c:\\book1.xls Apple LaserWriter II NT v47.0 c:\\output\\book1.ps \\n\"; " + ls );
635 out.write( "} " + ls );
636 out.write( " " + ls );
637 out.write( " " + ls );
638 out.write( " " + ls );
639 out.write( "$Win32::OLE::Warn = 3; # die on errors... " + ls );
640 out.write( " " + ls );
641 out.write( " " + ls );
642 out.write( "if ($#ARGV != 2) " + ls );
643 out.write( "{ " + ls );
644 out.write( " print \"Too less arguments.\\n\"; " + ls );
645 out.write( " print_usage(); " + ls );
646 out.write( " exit(1); " + ls );
647 out.write( "} " + ls );
648 out.write( " " + ls );
649 out.write( "my $Excel = Win32::OLE->GetActiveObject('Excel.Application') " + ls );
650 out.write( " || Win32::OLE->new('Excel.Application', 'Quit'); # get already active Excel " + ls );
651 out.write( " # application or open new " + ls );
652 out.write( "my $sFilterParameter = $ARGV[1]; " + ls );
653 out.write( "my $sFilterName = xlHTML; " + ls );
654 out.write( "if ($sFilterParameter eq 'xlXMLSpreadsheet') " + ls );
655 out.write( "{ " + ls );
656 out.write( " $sFilterName = xlXMLSpreadsheet; " + ls );
657 out.write( "} " + ls );
658 out.write( "elsif ($sFilterParameter eq 'xlHTML') " + ls );
659 out.write( "{ " + ls );
660 out.write( " $sFilterName = xlHTML; " + ls );
661 out.write( "} " + ls );
662 out.write( "else " + ls );
663 out.write( "{ " + ls );
664 out.write( " my $undefined; " + ls);
665 out.write( " $sFilterName = $undefined; " + ls );
666 out.write( "} " + ls );
667 out.write( " " + ls );
668 out.write( "my $Book = $Excel->Workbooks->Open( $ARGV[0] ); " + ls );
669 out.write( "$Excel->{DisplayAlerts} = 0; " + ls );
670 out.write( "$Book->saveAs($ARGV[2], " + ls );
671 out.write( " $sFilterName, " + ls );
672 out.write( " '', " + ls );
673 out.write( " '', " + ls );
674 out.write( " 0, " + ls );
675 out.write( " 0, " + ls );
676 out.write( " xlNoChange, " + ls );
677 out.write( " xlLocalSessionChanges, " + ls );
678 out.write( " 1); " + ls );
679 out.write( "# Close worksheets without store changes" + ls );
680 out.write( "# $Book->Close({SaveChanges => 0}); " + ls );
681 out.write( "$Excel->Quit(); " + ls );
682 out.close();
684 aList.add("perl");
685 aList.add(sName);
686 return aList;
689 ArrayList<String> createPowerPointPrintHelper() throws java.io.IOException
691 // create a program in tmp file
692 String sTmpPath = util.utils.getUsersTempDir();
693 String ls = System.getProperty("line.separator");
694 String fs = System.getProperty("file.separator");
696 String sPrintViaPowerPoint = "printViaPowerPoint.pl";
698 ArrayList<String> aList = searchLocalFile(sPrintViaPowerPoint);
699 if (aList.isEmpty() == false)
701 return aList;
703 String sName = sTmpPath + fs + sPrintViaPowerPoint;
704 if (FileHelper.isDebugEnabled())
706 GlobalLogWriter.get().println("No local found, create a script: " + sName);
709 File aFile = new File(sName);
710 FileWriter out = new FileWriter(aFile.toString());
713 out.write( "eval 'exec perl -wS $0 $1 $2 ' " + ls );
714 out.write( " if 0; " + ls );
715 out.write( "use strict; " + ls );
716 out.write( " " + ls );
717 out.write( "if ( $^O ne \"MSWin32\") " + ls );
718 out.write( "{ " + ls );
719 out.write( " print \"Windows only.\\n\"; " + ls );
720 out.write( " print_usage(); " + ls );
721 out.write( " exit(1); " + ls );
722 out.write( "} " + ls );
723 out.write( " " + ls );
724 out.write( " " + ls );
725 out.write( "use Win32::OLE qw(in with); " + ls );
726 out.write( "use Win32::OLE::Const 'Microsoft PowerPoint'; " + ls );
727 out.write( " " + ls );
728 out.write( "# ------ usage ------ " + ls );
729 out.write( "sub print_usage() " + ls );
730 out.write( "{ " + ls );
731 out.write( " print STDERR \"Usage: powerpoint_print.pl <PowerPoint file> <name of printer> <output file> .\\n " + ls );
732 out.write( " Please use the same string for the name of the printer as you can find \\n " + ls );
733 out.write( " under Start-Control Panel-Printer and Faxes \\n " + ls );
734 out.write( " The name could look like the following line: \\n " + ls );
735 out.write( " Apple LaserWriter II NT v47.0 \\n " + ls );
736 out.write( " Sample command line: \\n " + ls );
737 out.write( " powerpoint_print.pl c:\\book.ppt Apple LaserWriter II NT v47.0 c:\\output\\book.ps \\n\"; " + ls );
738 out.write( "} " + ls );
739 out.write( " " + ls );
740 out.write( " " + ls );
741 out.write( " " + ls );
742 out.write( "$Win32::OLE::Warn = 3; # die on errors... " + ls );
743 out.write( " " + ls );
744 out.write( " " + ls );
745 out.write( "if ($#ARGV < 2) " + ls );
746 out.write( "{ " + ls );
747 out.write( " print \"Too less arguments.\\n\"; " + ls );
748 out.write( " print_usage(); " + ls );
749 out.write( " exit(1); " + ls );
750 out.write( "} " + ls );
751 out.write( " " + ls );
752 out.write( "my $PowerPoint = Win32::OLE->GetActiveObject('PowerPoint.Application') " + ls );
753 out.write( " || Win32::OLE->new('PowerPoint.Application', 'Quit'); # get already active Excel " + ls );
754 out.write( " # application or open new " + ls );
755 out.write( " " + ls );
756 out.write( " " + ls );
757 out.write( " " + ls );
758 out.write( " $PowerPoint->{'Visible'} = 1; " + ls );
759 out.write( " my $Presentation = $PowerPoint->Presentations->Add; " + ls );
760 out.write( " my $Presentation = $PowerPoint->Presentations->Open( $ARGV[0] ); " + ls );
761 out.write( "# we can't change active printer in powerpoint " + ls );
762 out.write( "# $Presentation->PrintOptions->{ActivePrinter} = $ARGV[1]; " + ls );
763 out.write( " print \"Active printer is: \" . $Presentation->PrintOptions->{ActivePrinter} . \"\\n\"; " + ls );
764 out.write( " $Presentation->PrintOptions->{PrintInBackground} = 0; " + ls );
765 out.write( " # PrintColorType = 1 means print in color and PrintColorType = 2 means print in gray " + ls );
766 out.write( " $Presentation->PrintOptions->{PrintColorType} = 1; " + ls );
767 out.write( " " + ls );
768 out.write( " $Presentation->PrintOut({PrintToFile => $ARGV[2]}); " + ls );
769 out.write( " sleep 5; " + ls );
770 out.write( " print \"Presentation has been printed\\n\"; " + ls );
771 out.write( "my $sVersion = $Presentation->Application->Version();"+ls);
772 out.write( " $PowerPoint->Quit(); " + ls );
774 out.write( "local *FILE;" + ls);
775 out.write( "if (open(FILE, \">$ENV{HOME}/msofficeloadtimes.txt\"))" + ls);
776 out.write( "{" + ls);
777 out.write( " print FILE \"name=$ARGV[0]\\n\";" + ls);
778 out.write( " print FILE \"PowerPointVersion=$sVersion\\n\";" + ls);
779 // out.write( " print FILE \"WordStartTime=$stopWordTime\\n\";" + ls);
780 // out.write( " print FILE \"WordLoadTime=$stopLoadWordTime\\n\";" + ls);
781 // out.write( " print FILE \"WordPrintTime=$stopPrintWordTime\\n\";" + ls);
782 out.write( " close(FILE);" + ls);
783 out.write( "}" + ls);
784 out.close();
786 aList.add("perl");
787 aList.add(sName);
788 return aList;
792 @param _sFilename a name to a ms office xml file
793 @return 'word' or 'excel' or '' if type not known
795 public String getOfficeType(String _sFilename)
797 File aFile = new File(_sFilename);
798 if (! aFile.exists())
800 GlobalLogWriter.get().println("couldn't find file " + _sFilename);
801 return "";
803 RandomAccessFile aReader = null;
804 String sOfficeType = "";
807 aReader = new RandomAccessFile(aFile,"r");
808 String aLine = "";
809 while (aLine != null)
811 aLine = aReader.readLine();
812 if (aLine != null)
814 aLine = aLine.trim();
815 if ( (! (aLine.length() < 2) ) &&
816 (! aLine.startsWith("#")) &&
817 (! aLine.startsWith(";")) )
819 int nIdx = aLine.indexOf("mso-application");
820 if (nIdx > 0)
822 if (aLine.indexOf("Word.Document") > 0)
824 sOfficeType = "word";
826 else if (aLine.indexOf("Excel") > 0)
828 sOfficeType = "excel";
830 else
832 GlobalLogWriter.get().println("Unknown/unsupported data file: " + aLine);
839 catch (java.io.FileNotFoundException fne)
841 System.out.println("couldn't open file " + _sFilename);
842 System.out.println("Message: " + fne.getMessage());
844 catch (java.io.IOException ie)
846 System.out.println("Exception while reading file " + _sFilename);
847 System.out.println("Message: " + ie.getMessage());
851 aReader.close();
853 catch (java.io.IOException ie)
855 System.out.println("Couldn't close file " + _sFilename);
856 System.out.println("Message: " + ie.getMessage());
858 return sOfficeType;