Update ooo320-m1
[ooovba.git] / qadevOOo / runner / convwatch / MSOfficePrint.java
blob0c9ed034bd37b70d28030c140c323178e0aec228
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: MSOfficePrint.java,v $
10 * $Revision: 1.11.8.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package convwatch;
33 import convwatch.FileHelper;
34 import java.io.File;
35 import java.io.FileWriter;
36 import java.io.RandomAccessFile;
37 import convwatch.GraphicalTestArguments;
38 import helper.ProcessHandler;
39 import java.util.ArrayList;
40 import helper.OSHelper;
42 /**
43 * This object gives all functionallity to print msoffice documents.
44 * It also offers functions to check what type of document it is.
45 * It handles *.doc as word documents and use word to print
46 * *.xls as excel
47 * *.ppt as powerpoint
50 class ProcessHelper
52 ArrayList m_aArray;
55 public class MSOfficePrint
57 private String m_sPrinterName; // within Windows the tools need a printer name;
59 public void setPrinterName(String _s) {m_sPrinterName = _s;}
61 // -----------------------------------------------------------------------------
62 static boolean isWordDocument(String _sSuffix)
64 if (_sSuffix.toLowerCase().endsWith(".doc") ||
65 _sSuffix.toLowerCase().endsWith(".rtf") ||
66 _sSuffix.toLowerCase().endsWith(".dot"))
68 return true;
70 return false;
73 static boolean isExcelDocument(String _sSuffix)
75 // xlt templates
76 // xlw
77 // xla addin
78 if (_sSuffix.toLowerCase().endsWith(".xls"))
80 return true;
82 /* temporal insertion by SUS
83 if (_sSuffix.endsWith(".xml"))
85 return true;
86 }*/
87 return false;
90 static boolean isPowerPointDocument(String _sSuffix)
92 if (_sSuffix.toLowerCase().endsWith(".pps") ||
93 _sSuffix.toLowerCase().endsWith(".ppt"))
95 return true;
97 return false;
101 * returns true, if the given filename has a MS Office suffix.
103 public static boolean isMSOfficeDocumentFormat(String _sFile)
105 String sDocumentSuffix = FileHelper.getSuffix(_sFile);
106 if (isWordDocument(sDocumentSuffix)) return true;
107 if (isExcelDocument(sDocumentSuffix)) return true;
108 if (isPowerPointDocument(sDocumentSuffix)) return true;
109 // if suffix is xml, return also true, but we can't decide if word or excel
110 if (sDocumentSuffix.toLowerCase().endsWith(".xml")) return true;
111 return false;
114 public void storeToFileWithMSOffice( GraphicalTestArguments _aGTA,
115 String _sInputFile,
116 String _sOutputFile) throws ConvWatchCancelException, java.io.IOException
118 String sDocumentSuffix = FileHelper.getSuffix(_sInputFile);
119 String sFilterName = _aGTA.getExportFilterName();
120 ArrayList aStartCommand = new ArrayList();
121 if (isWordDocument(sDocumentSuffix))
123 aStartCommand = createWordStoreHelper();
125 else if (isExcelDocument(sDocumentSuffix))
127 aStartCommand = createExcelStoreHelper();
129 else if (isPowerPointDocument(sDocumentSuffix))
132 else if (sDocumentSuffix.toLowerCase().equals(".xml"))
134 // special case, if xml we prefer word, but with DEFAULT_XML_FORMAT_APP=excel it's changeable.
135 if (_aGTA.getDefaultXMLFormatApp().toLowerCase().equals("excel"))
137 aStartCommand = createExcelStoreHelper();
139 // else
140 // {
141 // }
143 else
145 GlobalLogWriter.get().println("No Microsoft Office document format found.");
146 // TODO: use a better Exception!!!
147 throw new ConvWatchCancelException/*WrongSuffixException*/("No MS office document format found.");
149 if (aStartCommand != null)
151 if (sFilterName == null)
153 // TODO: hardcoded FilterName in perl script
154 sFilterName = ""; // xlXMLSpreadsheet";
157 // String sCommand = sStartCommand + " " +
158 // _sInputFile + " " +
159 // StringHelper.doubleQuote(sFilterName) + " " +
160 // _sOutputFile;
162 aStartCommand.add(_sInputFile);
163 aStartCommand.add(sFilterName);
164 aStartCommand.add(_sOutputFile);
165 realStartCommand(aStartCommand);
169 // -----------------------------------------------------------------------------
171 * print the given file (_sInputFile) to the file name (_sPrintFile)
173 public void printToFileWithMSOffice( GraphicalTestArguments _aGTA,
174 String _sInputFile,
175 String _sPrintFilename) throws ConvWatchCancelException, java.io.IOException
177 String sDocumentSuffix = FileHelper.getSuffix(_sInputFile);
179 setPrinterName(_aGTA.getPrinterName());
181 ArrayList aStartCommand = new ArrayList();
182 if (isWordDocument(sDocumentSuffix))
184 aStartCommand = createWordPrintHelper();
186 else if (isExcelDocument(sDocumentSuffix))
188 aStartCommand = createExcelPrintHelper();
190 else if (isPowerPointDocument(sDocumentSuffix))
192 aStartCommand = createPowerPointPrintHelper();
194 else if (sDocumentSuffix.toLowerCase().equals(".xml"))
196 // TODO: Open XML File and check if we need excel or word
197 String sOfficeType = getOfficeType(_sInputFile);
199 // special case, if xml we prefer word, but with DEFAULT_XML_FORMAT_APP=excel it's changeable.
200 // if (_aGTA.getDefaultXMLFormatApp().toLowerCase().equals("excel"))
201 if (sOfficeType.equals("excel"))
203 aStartCommand = createExcelPrintHelper();
205 else if (sOfficeType.equals("word"))
207 aStartCommand = createWordPrintHelper();
209 else
211 return;
214 else
216 GlobalLogWriter.get().println("No Microsoft Office document format found.");
217 // TODO: use a better Exception!!!
218 throw new ConvWatchCancelException/*WrongSuffixException*/("No Mircosoft Office document format found.");
221 if (aStartCommand.isEmpty() == false)
223 String sPrinterName = m_sPrinterName;
224 if (sPrinterName == null)
226 sPrinterName = "";
229 // String sCommand = sStartCommand + " " +
230 // _sInputFile + " " +
231 // StringHelper.doubleQuote(m_sPrinterName) + " " +
232 // _sPrintFilename;
233 aStartCommand.add(_sInputFile);
234 aStartCommand.add(m_sPrinterName);
235 aStartCommand.add(_sPrintFilename);
237 realStartCommand(aStartCommand);
239 String sUserDir = System.getProperty("user.home");
240 String fs = System.getProperty("file.separator");
241 if (! sUserDir.endsWith(fs))
243 sUserDir = sUserDir + fs;
245 _aGTA.getPerformance().readWordValuesFromFile(sUserDir + "msofficeloadtimes.txt");
246 OfficePrint.createInfoFile(_sPrintFilename, _aGTA, "msoffice");
247 TimeHelper.waitInSeconds(2, "Give Microsoft Office some time to print.");
250 public void realStartCommand(ArrayList _aStartCommand) throws ConvWatchCancelException
252 if (_aStartCommand.isEmpty())
254 throw new ConvWatchCancelException/*WrongEnvironmentException*/("Given list is empty.");
259 // Convert the StartCommand ArrayList to a String List
260 int nValues = _aStartCommand.size();
261 String[] aList = new String[nValues];
262 for (int i=0;i<nValues;i++)
264 String aStr = (String) _aStartCommand.get(i);
265 if (aStr == null)
267 aStr = "";
269 if (aStr.length() == 0)
271 aStr = "\"\"";
273 aList[i] = new String(aStr);
276 // This is really the latest point where we can check if we are running within windows environment
277 if (! OSHelper.isWindows())
279 // TODO: use a better Exception!!!
280 throw new ConvWatchCancelException/*WrongEnvironmentException*/("We doesn't work within windows environment.");
284 ProcessHandler aHandler = new ProcessHandler(aList);
285 boolean bBackValue = aHandler.executeSynchronously();
287 catch (IndexOutOfBoundsException e)
289 throw new ConvWatchCancelException/*WrongEnvironmentException*/("Given list is too short.");
292 // return aHandler.getExitCode();
296 ArrayList createWordPrintHelper() throws java.io.IOException
298 // create a program in tmp file
299 String sTmpPath = util.utils.getUsersTempDir();
300 String ls = System.getProperty("line.separator");
301 String fs = System.getProperty("file.separator");
303 String sPrintViaWord = "printViaWord.pl";
305 ArrayList aList = searchLocalFile(sPrintViaWord);
306 if (aList.isEmpty() == false)
308 return aList;
311 String sName = sTmpPath + fs + sPrintViaWord;
312 File aFile = new File(sName);
313 FileWriter out = new FileWriter(aFile.toString());
316 out.write( "eval 'exec perl -wS $0 ${1+\"$@\"}' " + ls );
317 out.write( " if 0; " + ls );
318 out.write( "use strict; " + ls );
319 out.write( "use Time::HiRes; " + ls );
320 out.write( "if ( $^O ne \"MSWin32\") " + ls );
321 out.write( "{ " + ls );
322 out.write( " print 'Windows only.\\n'; " + ls );
323 out.write( " print_usage(); " + ls );
324 out.write( " exit(1); " + ls );
325 out.write( "} " + ls );
326 out.write( " " + ls );
327 out.write( "use Win32::OLE; " + ls );
328 out.write( "use Win32::OLE::Const 'Microsoft Word'; " + ls );
329 out.write( " " + ls );
330 out.write( "# ------ usage ------ " + ls );
331 out.write( "sub print_usage() " + ls );
332 out.write( "{ " + ls );
333 out.write( " print STDERR \"Usage: word_print.pl <Word file> <name of printer> <output file> .\\n " + ls );
334 out.write( " Please use the same string for the name of the printer as you can find \\n " + ls );
335 out.write( " under Start-Control Panel-Printer and Faxes \\n " + ls );
336 out.write( " The name could look like the the following line: \\n " + ls );
337 out.write( " Apple LaserWriter II NT v47.0 \\n " + ls );
338 out.write( " Sample command line: \\n " + ls );
339 out.write( " execl_print.pl c:\\book1.doc Apple LaserWriter II NT v47.0 c:\\output\\book1.ps \\n\"; " + ls );
340 out.write( "} " + ls );
341 out.write( " " + ls );
342 out.write( " " + ls );
343 out.write( "if ($#ARGV != 2) " + ls );
344 out.write( "{ " + ls );
345 out.write( " print 'Too less arguments.\\n'; " + ls );
346 out.write( " print_usage(); " + ls );
347 out.write( " exit(1); " + ls );
348 out.write( "} " + ls );
349 out.write( " " + ls );
350 out.write( "my $startWordTime = Time::HiRes::time(); " + ls );
351 out.write( "my $Word = Win32::OLE->new('Word.Application'); " + ls );
352 out.write( "my $stopWordTime = Time::HiRes::time() - $startWordTime; " + ls );
353 out.write( "# $Word->{'Visible'} = 1; # if you want to see what's going on " + ls );
354 out.write( "# , ReadOnly => 1})" + ls );
355 out.write(ls);
356 out.write( "my $startLoadWordTime = Time::HiRes::time(); " + ls );
357 out.write( "$Word->Documents->Open({Filename => $ARGV[0]}) " + ls );
358 out.write( " || die('Unable to open document ', Win32::OLE->LastError()); " + ls );
359 out.write( "my $stopLoadWordTime = Time::HiRes::time() - $startLoadWordTime; " + ls );
360 out.write(ls);
361 out.write( "my $startPrintWordTime = Time::HiRes::time(); " + ls);
362 out.write( "my $oldActivePrinte = $Word->{ActivePrinter} ; " + ls );
363 out.write( "$Word->{ActivePrinter} = $ARGV[1]; " + ls );
364 out.write( "$Word->ActiveDocument->PrintOut({ " + ls );
365 out.write( " Background => 0, " + ls );
366 out.write( " Append => 0, " + ls );
367 out.write( " Range => wdPrintAllDocument, " + ls );
368 out.write( " Item => wdPrintDocumentContent, " + ls );
369 out.write( " Copies => 1, " + ls );
370 out.write( " PageType => wdPrintAllPages, " + ls );
371 out.write( " PrintToFile => 1, " + ls );
372 out.write( " OutputFileName => $ARGV[2] " + ls );
373 out.write( " }); " + ls );
374 out.write( "$Word->{ActivePrinter} = $oldActivePrinte; " + ls );
375 out.write( "my $stopPrintWordTime = Time::HiRes::time() - $startPrintWordTime;" + ls);
377 out.write( "# ActiveDocument.Close(SaveChanges:=WdSaveOptions.wdDoNotSaveChanges)" + ls );
378 out.write( "my $sVersion = $Word->Application->Version();"+ls);
379 out.write( "$Word->ActiveDocument->Close({SaveChanges => 0}); " + ls );
380 out.write( "$Word->Quit(); " + ls );
382 out.write( "local *FILE;" + ls);
383 out.write( "if (open(FILE, \">$ENV{HOME}/msofficeloadtimes.txt\"))" + ls);
384 out.write( "{" + ls);
385 out.write( " print FILE \"name=$ARGV[0]\\n\";" + ls);
386 out.write( " print FILE \"WordVersion=$sVersion\\n\";" + ls);
387 out.write( " print FILE \"WordStartTime=$stopWordTime\\n\";" + ls);
388 out.write( " print FILE \"WordLoadTime=$stopLoadWordTime\\n\";" + ls);
389 out.write( " print FILE \"WordPrintTime=$stopPrintWordTime\\n\";" + ls);
390 out.write( " close(FILE);" + ls);
391 out.write( "}" + ls);
392 out.close();
394 aList.add("perl");
395 aList.add(sName);
396 return aList;
399 // TODO: Maybe give a possibility to say where search the script from outside
401 ArrayList searchLocalFile(String _sScriptName)
403 String userdir = System.getProperty("user.dir");
404 String fs = System.getProperty("file.separator");
406 ArrayList aList = new ArrayList();
407 File aPerlScript = new File(userdir + fs + _sScriptName);
408 if (FileHelper.isDebugEnabled())
410 GlobalLogWriter.get().println("Search for local existance of " + aPerlScript.getAbsolutePath());
413 if (aPerlScript.exists())
415 if (FileHelper.isDebugEnabled())
417 GlobalLogWriter.get().println("OK, found it, use this instead the internal one.");
420 String sName = aPerlScript.getAbsolutePath();
421 // String sCommand = "perl " + sName;
422 // System.out.println(sCommand);
423 aList.add("perl");
424 aList.add(sName);
425 return aList;
427 return aList;
430 ArrayList createWordStoreHelper() throws java.io.IOException
432 // create a program in tmp file
433 String sTmpPath = util.utils.getUsersTempDir();
434 String ls = System.getProperty("line.separator");
435 String fs = System.getProperty("file.separator");
437 // ArrayList aList = new ArrayList();
438 String sSaveViaWord = "saveViaWord.pl";
440 ArrayList aList = searchLocalFile(sSaveViaWord);
441 if (aList.isEmpty() == false)
443 return aList;
446 String sName = sTmpPath + fs + sSaveViaWord;
447 if (FileHelper.isDebugEnabled())
449 GlobalLogWriter.get().println("No local found, create a perl script: " + sName);
452 File aFile = new File(sName);
453 FileWriter out = new FileWriter(aFile.toString());
455 out.write( "eval 'exec perl -wS $0 ${1+\"$@\"}' " + ls );
456 out.write( " if 0; " + ls );
457 out.write( "use strict; " + ls );
458 out.write( " " + ls );
459 out.write( "if ( $^O ne \"MSWin32\") " + ls );
460 out.write( "{ " + ls );
461 out.write( " print 'Windows only.\\n'; " + ls );
462 out.write( " print_usage(); " + ls );
463 out.write( " exit(1); " + ls );
464 out.write( "} " + ls );
465 out.write( " " + ls );
466 out.write( "use Win32::OLE; " + ls );
467 out.write( "use Win32::OLE::Const 'Microsoft Word'; " + ls );
468 out.write( " " + ls );
469 out.write( "# ------ usage ------ " + ls );
470 out.write( "sub print_usage() " + ls );
471 out.write( "{ " + ls );
472 out.write( " print STDERR \"Usage: storeViaWord.pl <Word file> <output filer> <output file> \\n\" " + ls );
473 out.write( "} " + ls );
474 out.write( " " + ls );
475 out.write( " " + ls );
476 out.write( "if ($#ARGV != 2) " + ls );
477 out.write( "{ " + ls );
478 out.write( " print 'Too less arguments.\\n'; " + ls );
479 out.write( " print_usage(); " + ls );
480 out.write( " exit(1); " + ls );
481 out.write( "} " + ls );
482 out.write( " " + ls );
483 out.write( " " + ls );
484 out.write( "my $Word = Win32::OLE->new('Word.Application'); " + ls );
485 out.write( "# $Word->{'Visible'} = 1; # if you want to see what's going on " + ls );
486 out.write( "my $Book = $Word->Documents->Open($ARGV[0]) " + ls );
487 out.write( " || die('Unable to open document ', Win32::OLE->LastError()); " + ls );
488 out.write( "# my $oldActivePrinte = $Word->{ActivePrinter} ; " + ls );
489 out.write( "# $Word->{ActivePrinter} = $ARGV[1]; " + ls );
490 out.write( "# $Word->ActiveDocument->PrintOut({ " + ls );
491 out.write( "# Background => 0, " + ls );
492 out.write( "# Append => 0, " + ls );
493 out.write( "# Range => wdPrintAllDocument, " + ls );
494 out.write( "# Item => wdPrintDocumentContent, " + ls );
495 out.write( "# Copies => 1, " + ls );
496 out.write( "# PageType => wdPrintAllPages, " + ls );
497 out.write( "# PrintToFile => 1, " + ls );
498 out.write( "# OutputFileName => $ARGV[2] " + ls );
499 out.write( "# }); " + ls );
500 out.write( "# $Word->{ActivePrinter} = $oldActivePrinte; " + ls );
501 out.write( "$Book->savaAs($ARGV[2], $ARGV[1]); " + ls );
502 out.write( "# ActiveDocument.Close(SaveChanges:=WdSaveOptions.wdDoNotSaveChanges)" + ls );
503 out.write( "$Book->Close({SaveChanges => 0}); " + ls );
504 out.write( "$Word->Quit(); " + ls );
505 out.close();
507 aList.add("perl");
508 aList.add(sName);
509 return aList;
513 ArrayList createExcelPrintHelper() throws java.io.IOException
515 // create a program in tmp file
516 String sTmpPath = util.utils.getUsersTempDir();
517 String ls = System.getProperty("line.separator");
518 String fs = System.getProperty("file.separator");
520 String sPrintViaExcel = "printViaExcel.pl";
522 ArrayList aList = searchLocalFile(sPrintViaExcel);
523 if (aList.isEmpty() == false)
525 return aList;
527 String sName = sTmpPath + fs + sPrintViaExcel;
528 if (FileHelper.isDebugEnabled())
530 GlobalLogWriter.get().println("No local found, create a perl script: " + sName);
533 File aFile = new File(sName);
534 FileWriter out = new FileWriter(aFile.toString());
536 out.write( "eval 'exec perl -wS $0 ${1+\"$@\"}' " + ls );
537 out.write( " if 0; " + ls );
538 out.write( "use strict; " + ls );
539 out.write( " " + ls );
540 out.write( "if ( $^O ne \"MSWin32\") " + ls );
541 out.write( "{ " + ls );
542 out.write( " print \"Windows only.\\n\"; " + ls );
543 out.write( " print_usage(); " + ls );
544 out.write( " exit(1); " + ls );
545 out.write( "} " + ls );
546 out.write( " " + ls );
547 out.write( " " + ls );
548 out.write( "use Win32::OLE qw(in with); " + ls );
549 out.write( "use Win32::OLE::Const 'Microsoft Excel'; " + ls );
550 out.write( " " + ls );
551 out.write( "# ------ usage ------ " + ls );
552 out.write( "sub print_usage() " + ls );
553 out.write( "{ " + ls );
554 out.write( " print STDERR \"Usage: printViaExcel.pl <Excel file> <name of printer> <output file> .\\n " + ls );
555 out.write( " Please use the same string for the name of the printer as you can find \\n " + ls );
556 out.write( " under Start-Control Panel-Printer and Faxes \\n " + ls );
557 out.write( " The name could look like the the following line: \\n " + ls );
558 out.write( " Apple LaserWriter II NT v47.0 \\n " + ls );
559 out.write( " Sample command line: \\n " + ls );
560 out.write( " execl_print.pl c:\\book1.xls Apple LaserWriter II NT v47.0 c:\\output\\book1.ps \\n\"; " + ls );
561 out.write( "} " + ls );
562 out.write( " " + ls );
563 out.write( " " + ls );
564 out.write( " " + ls );
565 out.write( "$Win32::OLE::Warn = 3; # die on errors... " + ls );
566 out.write( " " + ls );
567 out.write( " " + ls );
568 out.write( "if ($#ARGV != 2) " + ls );
569 out.write( "{ " + ls );
570 out.write( " print STDERR \"Too less arguments.\\n\"; " + ls );
571 out.write( " print STDERR \"ARGV[0] $ARGV[0]\\n\"; " + ls );
572 out.write( " print STDERR \"ARGV[1] $ARGV[1]\\n\"; " + ls );
573 out.write( " print STDERR \"ARGV[2] $ARGV[2]\\n\"; " + ls );
574 out.write( " print_usage(); " + ls );
575 out.write( " exit(1); " + ls );
576 out.write( "} " + ls );
577 out.write( " " + ls );
578 out.write( "my $Excel = Win32::OLE->GetActiveObject('Excel.Application') " + ls );
579 out.write( " || Win32::OLE->new('Excel.Application', 'Quit'); # get already active Excel " + ls );
580 out.write( " # application or open new " + ls );
581 out.write( " " + ls );
582 out.write( " " + ls );
583 out.write( " " + ls );
584 out.write( "my $Book = $Excel->Workbooks->Open( $ARGV[0] ); " + ls );
585 out.write( " $Book->PrintOut({Copies => 1, " + ls );
586 out.write( " ActivePrinter => $ARGV[1], " + ls );
587 out.write( " PrToFileName => $ARGV[2], " + ls );
588 out.write( " Collate => 1 " + ls );
589 out.write( " }); " + ls );
590 out.write( "# Close worksheets without store changes" + ls );
591 out.write( "# $Book->Close({SaveChanges => 0}); " + ls );
592 out.write( "my $sVersion = $Excel->Application->Version();"+ls);
593 out.write( "$Excel->Quit(); " + ls );
594 out.write( "local *FILE;" + ls);
595 out.write( "if (open(FILE, \">$ENV{HOME}/msofficeloadtimes.txt\"))" + ls);
596 out.write( "{" + ls);
597 out.write( " print FILE \"name=$ARGV[0]\\n\";" + ls);
598 out.write( " print FILE \"ExcelVersion=$sVersion\\n\";" + ls);
599 // out.write( " print FILE \"WordStartTime=$stopWordTime\\n\";" + ls);
600 // out.write( " print FILE \"WordLoadTime=$stopLoadWordTime\\n\";" + ls);
601 // out.write( " print FILE \"WordPrintTime=$stopPrintWordTime\\n\";" + ls);
602 out.write( " close(FILE);" + ls);
603 out.write( "}" + ls);
604 out.close();
606 aList.add("perl");
607 aList.add(sName);
608 return aList;
611 ArrayList createExcelStoreHelper() throws java.io.IOException
613 // create a program in tmp file
614 String sTmpPath = util.utils.getUsersTempDir();
615 String ls = System.getProperty("line.separator");
616 String fs = System.getProperty("file.separator");
618 String sSaveViaExcel = "saveViaExcel.pl";
620 ArrayList aList = searchLocalFile(sSaveViaExcel);
621 if (aList.isEmpty() == false)
623 return aList;
625 String sName = sTmpPath + fs + sSaveViaExcel;
626 if (FileHelper.isDebugEnabled())
628 GlobalLogWriter.get().println("No local found, create a script: " + sName);
631 File aFile = new File(sName);
632 FileWriter out = new FileWriter(aFile.toString());
634 out.write( "eval 'exec perl -wS $0 ${1+\"$@\"}' " + ls );
635 out.write( " if 0; " + ls );
636 out.write( "use strict; " + ls );
637 out.write( "# This script is automatically created. " + ls );
638 out.write( " " + ls );
639 out.write( "use Win32::OLE qw(in with); " + ls );
640 out.write( "use Win32::OLE::Const 'Microsoft Excel'; " + ls );
641 out.write( " " + ls );
642 out.write( "# ------ usage ------ " + ls );
643 out.write( "sub print_usage() " + ls );
644 out.write( "{ " + ls );
645 out.write( " print STDERR \"Usage: savaViaExcel.pl <Excel file> <filefilter> <output file> .\\n " + ls );
646 out.write( " execl_print.pl c:\\book1.xls Apple LaserWriter II NT v47.0 c:\\output\\book1.ps \\n\"; " + ls );
647 out.write( "} " + ls );
648 out.write( " " + ls );
649 out.write( " " + ls );
650 out.write( " " + ls );
651 out.write( "$Win32::OLE::Warn = 3; # die on errors... " + ls );
652 out.write( " " + ls );
653 out.write( " " + ls );
654 out.write( "if ($#ARGV != 2) " + ls );
655 out.write( "{ " + ls );
656 out.write( " print \"Too less arguments.\\n\"; " + ls );
657 out.write( " print_usage(); " + ls );
658 out.write( " exit(1); " + ls );
659 out.write( "} " + ls );
660 out.write( " " + ls );
661 out.write( "my $Excel = Win32::OLE->GetActiveObject('Excel.Application') " + ls );
662 out.write( " || Win32::OLE->new('Excel.Application', 'Quit'); # get already active Excel " + ls );
663 out.write( " # application or open new " + ls );
664 out.write( "my $sFilterParameter = $ARGV[1]; " + ls );
665 out.write( "my $sFilterName = xlHTML; " + ls );
666 out.write( "if ($sFilterParameter eq 'xlXMLSpreadsheet') " + ls );
667 out.write( "{ " + ls );
668 out.write( " $sFilterName = xlXMLSpreadsheet; " + ls );
669 out.write( "} " + ls );
670 out.write( "elsif ($sFilterParameter eq 'xlHTML') " + ls );
671 out.write( "{ " + ls );
672 out.write( " $sFilterName = xlHTML; " + ls );
673 out.write( "} " + ls );
674 out.write( "else " + ls );
675 out.write( "{ " + ls );
676 out.write( " my $undefined; " + ls);
677 out.write( " $sFilterName = $undefined; " + ls );
678 out.write( "} " + ls );
679 out.write( " " + ls );
680 out.write( "my $Book = $Excel->Workbooks->Open( $ARGV[0] ); " + ls );
681 out.write( "$Excel->{DisplayAlerts} = 0; " + ls );
682 out.write( "$Book->saveAs($ARGV[2], " + ls );
683 out.write( " $sFilterName, " + ls );
684 out.write( " '', " + ls );
685 out.write( " '', " + ls );
686 out.write( " 0, " + ls );
687 out.write( " 0, " + ls );
688 out.write( " xlNoChange, " + ls );
689 out.write( " xlLocalSessionChanges, " + ls );
690 out.write( " 1); " + ls );
691 out.write( "# Close worksheets without store changes" + ls );
692 out.write( "# $Book->Close({SaveChanges => 0}); " + ls );
693 out.write( "$Excel->Quit(); " + ls );
694 out.close();
696 aList.add("perl");
697 aList.add(sName);
698 return aList;
701 ArrayList createPowerPointPrintHelper() throws java.io.IOException
703 // create a program in tmp file
704 String sTmpPath = util.utils.getUsersTempDir();
705 String ls = System.getProperty("line.separator");
706 String fs = System.getProperty("file.separator");
708 String sPrintViaPowerPoint = "printViaPowerPoint.pl";
710 ArrayList aList = searchLocalFile(sPrintViaPowerPoint);
711 if (aList.isEmpty() == false)
713 return aList;
715 String sName = sTmpPath + fs + sPrintViaPowerPoint;
716 if (FileHelper.isDebugEnabled())
718 GlobalLogWriter.get().println("No local found, create a script: " + sName);
721 File aFile = new File(sName);
722 FileWriter out = new FileWriter(aFile.toString());
725 out.write( "eval 'exec perl -wS $0 $1 $2 ' " + ls );
726 out.write( " if 0; " + ls );
727 out.write( "use strict; " + ls );
728 out.write( " " + ls );
729 out.write( "if ( $^O ne \"MSWin32\") " + ls );
730 out.write( "{ " + ls );
731 out.write( " print \"Windows only.\\n\"; " + ls );
732 out.write( " print_usage(); " + ls );
733 out.write( " exit(1); " + ls );
734 out.write( "} " + ls );
735 out.write( " " + ls );
736 out.write( " " + ls );
737 out.write( "use Win32::OLE qw(in with); " + ls );
738 out.write( "use Win32::OLE::Const 'Microsoft PowerPoint'; " + ls );
739 out.write( " " + ls );
740 out.write( "# ------ usage ------ " + ls );
741 out.write( "sub print_usage() " + ls );
742 out.write( "{ " + ls );
743 out.write( " print STDERR \"Usage: powerpoint_print.pl <PowerPoint file> <name of printer> <output file> .\\n " + ls );
744 out.write( " Please use the same string for the name of the printer as you can find \\n " + ls );
745 out.write( " under Start-Control Panel-Printer and Faxes \\n " + ls );
746 out.write( " The name could look like the the following line: \\n " + ls );
747 out.write( " Apple LaserWriter II NT v47.0 \\n " + ls );
748 out.write( " Sample command line: \\n " + ls );
749 out.write( " powerpoint_print.pl c:\\book.ppt Apple LaserWriter II NT v47.0 c:\\output\\book.ps \\n\"; " + ls );
750 out.write( "} " + ls );
751 out.write( " " + ls );
752 out.write( " " + ls );
753 out.write( " " + ls );
754 out.write( "$Win32::OLE::Warn = 3; # die on errors... " + ls );
755 out.write( " " + ls );
756 out.write( " " + ls );
757 out.write( "if ($#ARGV < 2) " + ls );
758 out.write( "{ " + ls );
759 out.write( " print \"Too less arguments.\\n\"; " + ls );
760 out.write( " print_usage(); " + ls );
761 out.write( " exit(1); " + ls );
762 out.write( "} " + ls );
763 out.write( " " + ls );
764 out.write( "my $PowerPoint = Win32::OLE->GetActiveObject('PowerPoint.Application') " + ls );
765 out.write( " || Win32::OLE->new('PowerPoint.Application', 'Quit'); # get already active Excel " + ls );
766 out.write( " # application or open new " + ls );
767 out.write( " " + ls );
768 out.write( " " + ls );
769 out.write( " " + ls );
770 out.write( " $PowerPoint->{'Visible'} = 1; " + ls );
771 out.write( " my $Presentation = $PowerPoint->Presentations->Add; " + ls );
772 out.write( " my $Presentation = $PowerPoint->Presentations->Open( $ARGV[0] ); " + ls );
773 out.write( "# we can't change active printer in powerpoint " + ls );
774 out.write( "# $Presentation->PrintOptions->{ActivePrinter} = $ARGV[1]; " + ls );
775 out.write( " print \"Active printer is: \" . $Presentation->PrintOptions->{ActivePrinter} . \"\\n\"; " + ls );
776 out.write( " $Presentation->PrintOptions->{PrintInBackground} = 0; " + ls );
777 out.write( " # PrintColorType = 1 means print in color and PrintColorType = 2 means print in gray " + ls );
778 out.write( " $Presentation->PrintOptions->{PrintColorType} = 1; " + ls );
779 out.write( " " + ls );
780 out.write( " $Presentation->PrintOut({PrintToFile => $ARGV[2]}); " + ls );
781 out.write( " sleep 5; " + ls );
782 out.write( " print \"Presentation has been printed\\n\"; " + ls );
783 out.write( "my $sVersion = $Presentation->Application->Version();"+ls);
784 out.write( " $PowerPoint->Quit(); " + ls );
786 out.write( "local *FILE;" + ls);
787 out.write( "if (open(FILE, \">$ENV{HOME}/msofficeloadtimes.txt\"))" + ls);
788 out.write( "{" + ls);
789 out.write( " print FILE \"name=$ARGV[0]\\n\";" + ls);
790 out.write( " print FILE \"PowerPointVersion=$sVersion\\n\";" + ls);
791 // out.write( " print FILE \"WordStartTime=$stopWordTime\\n\";" + ls);
792 // out.write( " print FILE \"WordLoadTime=$stopLoadWordTime\\n\";" + ls);
793 // out.write( " print FILE \"WordPrintTime=$stopPrintWordTime\\n\";" + ls);
794 out.write( " close(FILE);" + ls);
795 out.write( "}" + ls);
796 out.close();
798 aList.add("perl");
799 aList.add(sName);
800 return aList;
804 @param _sFilename a name to a ms office xml file
805 @return 'word' or 'excel' or '' if type not known
807 public String getOfficeType(String _sFilename)
809 File aFile = new File(_sFilename);
810 if (! aFile.exists())
812 GlobalLogWriter.get().println("couldn't find file " + _sFilename);
813 return "";
815 RandomAccessFile aReader = null;
816 String sOfficeType = "";
819 aReader = new RandomAccessFile(aFile,"r");
820 String aLine = "";
821 while (aLine != null)
823 aLine = aReader.readLine();
824 if (aLine != null)
826 aLine = aLine.trim();
827 if ( (! (aLine.length() < 2) ) &&
828 (! aLine.startsWith("#")) &&
829 (! aLine.startsWith(";")) )
831 int nIdx = aLine.indexOf("mso-application");
832 if (nIdx > 0)
834 if (aLine.indexOf("Word.Document") > 0)
836 sOfficeType = "word";
838 else if (aLine.indexOf("Excel") > 0)
840 sOfficeType = "excel";
842 else
844 GlobalLogWriter.get().println("Unknown/unsupported data file: " + aLine);
851 catch (java.io.FileNotFoundException fne)
853 System.out.println("couldn't open file " + _sFilename);
854 System.out.println("Message: " + fne.getMessage());
856 catch (java.io.IOException ie)
858 System.out.println("Exception while reading file " + _sFilename);
859 System.out.println("Message: " + ie.getMessage());
863 aReader.close();
865 catch (java.io.IOException ie)
867 System.out.println("Couldn't close file " + _sFilename);
868 System.out.println("Message: " + ie.getMessage());
870 return sOfficeType;