bump product version to 4.1.6.2
[LibreOffice.git] / qadevOOo / runner / graphical / MSOfficePostscriptCreator.java
blob8206f8edfba574424a9288a1f30716fdd672c070
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 graphical;
21 import java.io.File;
22 import java.io.FileWriter;
23 import java.io.RandomAccessFile;
24 import helper.ProcessHandler;
25 import java.util.ArrayList;
26 import helper.OSHelper;
27 import javax.xml.parsers.DocumentBuilder;
28 import javax.xml.parsers.DocumentBuilderFactory;
29 import org.w3c.dom.Document;
30 import org.w3c.dom.Node;
32 /**
33 * This object gives all functionallity to print msoffice documents.
34 * It also offers functions to check what type of document it is.
35 * It handles *.doc as word documents and use word to print
36 * *.xls as excel
37 * *.ppt as powerpoint
40 //class ProcessHelper
41 //{
42 // ArrayList m_aArray;
43 //}
45 public class MSOfficePostscriptCreator implements IOffice
47 private String m_sPrinterName; // within Windows the tools need a printer name;
49 public void setPrinterName(String _s)
51 m_sPrinterName = _s;
54 private ParameterHelper m_aParameterHelper;
55 private String m_sDocumentName;
56 private String m_sResult;
58 // CTor
59 public MSOfficePostscriptCreator(ParameterHelper _aParam, String _sResult)
61 m_aParameterHelper = _aParam;
62 m_sResult = _sResult;
63 // String sKillCommand = (String)_aParam.getTestParameters().get(util.PropertyName.APP_KILL_COMMAND);
64 // if (sKillCommand == null)
65 // {
66 // sKillCommand = "";
67 // }
68 // if (sKillCommand.length() > 0)
69 // {
70 // sKillCommand += ";";
71 // }
72 String sKillCommand = "C:/bin/kill.exe -9 winword;C:/bin/kill.exe -9 excel";
73 _aParam.getTestParameters().put(util.PropertyName.APP_KILL_COMMAND, sKillCommand);
76 public void load(String _sDocumentName) throws OfficeException
78 m_sDocumentName = _sDocumentName;
80 if (! isMSOfficeDocumentFormat(m_sDocumentName))
82 GlobalLogWriter.println("This document type is not recognized as MSOffice format, as default fallback StarOffice/OpenOffice.org instead is used.");
83 throw new OfficeException("This document type is not recognized as MSOffice format, as default fallback StarOffice/OpenOffice.org instead is used.");
87 public void storeAsPostscript() throws OfficeException
89 GlobalLogWriter.println("USE MSOFFICE AS EXPORT FORMAT.");
90 try
92 String sDocumentName = m_sDocumentName + ".ps";
93 printToFileWithMSOffice(m_aParameterHelper,
94 m_sDocumentName,
95 m_sResult);
96 File aFile = new File(sDocumentName);
97 if (aFile.exists())
99 String sBasename = FileHelper.getBasename(sDocumentName);
100 FileHelper.addBasenameToIndex(m_sResult, sBasename, "msoffice", "postscript", m_sDocumentName);
103 catch(OfficeException e)
105 e.printStackTrace();
106 GlobalLogWriter.println(e.getMessage());
107 throw new OfficeException("Exception caught. Problem with MSOffice printer methods.");
109 catch(java.io.IOException e)
111 GlobalLogWriter.println(e.getMessage());
112 throw new OfficeException("IOException caught. Problem with MSOffice printer methods.");
116 public void start() throws OfficeException
118 // we don't have an office to start
121 public void close() throws OfficeException
123 // we don't have an office to stop
126 // -----------------------------------------------------------------------------
127 private boolean isWordDocument(String _sSuffix)
129 if (_sSuffix.toLowerCase().endsWith(".doc") ||
130 _sSuffix.toLowerCase().endsWith(".rtf") ||
131 _sSuffix.toLowerCase().endsWith(".dot"))
133 return true;
135 return false;
138 private boolean isExcelDocument(String _sSuffix)
140 // xlt templates
141 // xlw
142 // xla addin
143 if (_sSuffix.toLowerCase().endsWith(".xls"))
145 return true;
147 /* temporal insertion by SUS
148 if (_sSuffix.endsWith(".xml"))
150 return true;
152 return false;
155 private boolean isPowerPointDocument(String _sSuffix)
157 if (_sSuffix.toLowerCase().endsWith(".pps") ||
158 _sSuffix.toLowerCase().endsWith(".ppt"))
160 return true;
162 return false;
166 * returns true, if the given filename has a MS Office suffix.
168 private boolean isMSOfficeDocumentFormat(String _sFile)
170 String sDocumentSuffix = FileHelper.getSuffix(_sFile);
171 if (isWordDocument(sDocumentSuffix)) {return true;}
172 if (isExcelDocument(sDocumentSuffix)) {return true;}
173 if (isPowerPointDocument(sDocumentSuffix)) {return true;}
174 // if suffix is xml, return also true, but we can't decide if word or excel
175 if (sDocumentSuffix.toLowerCase().endsWith(".xml")) {return true;}
176 return false;
179 public void storeToFileWithMSOffice( ParameterHelper _aGTA,
180 String _sInputFile,
181 String _sOutputFile) throws OfficeException, java.io.IOException
183 String sDocumentSuffix = FileHelper.getSuffix(_sInputFile);
184 String sFilterName = _aGTA.getExportFilterName();
185 ArrayList<String> aStartCommand = new ArrayList<String>();
186 if (isWordDocument(sDocumentSuffix))
188 aStartCommand = createWordStoreHelper();
190 else if (isExcelDocument(sDocumentSuffix))
192 aStartCommand = createExcelStoreHelper();
194 else if (isPowerPointDocument(sDocumentSuffix))
197 else if (sDocumentSuffix.toLowerCase().equals(".xml"))
199 // special case, if xml we prefer word, but with DEFAULT_XML_FORMAT_APP=excel it's changeable.
200 String sDocFormat = getXMLDocumentFormat(_sInputFile);
201 // if (_aGTA.getDefaultXMLFormatApp().toLowerCase().equals("excel"))
202 if (sDocFormat.equals("excel"))
204 aStartCommand = createExcelStoreHelper();
206 else
208 aStartCommand = createWordStoreHelper();
210 // else
211 // {
212 // }
214 else
216 GlobalLogWriter.println("No Microsoft Office document format found.");
218 throw new WrongSuffixException("No MS office document format found.");
220 if (aStartCommand != null)
222 if (sFilterName == null)
224 // TODO: hardcoded FilterName in perl script
225 sFilterName = ""; // xlXMLSpreadsheet";
228 // String sCommand = sStartCommand + " " +
229 // _sInputFile + " " +
230 // StringHelper.doubleQuote(sFilterName) + " " +
231 // _sOutputFile;
233 aStartCommand.add(_sInputFile);
234 aStartCommand.add(sFilterName);
235 aStartCommand.add(_sOutputFile);
236 realStartCommand(aStartCommand);
240 // -----------------------------------------------------------------------------
242 * print the given file (_sInputFile) to the file name (_sPrintFile)
243 * @param _aGTA
244 * @param _sInputFile
245 * @param _sPrintFilename
246 * @throws OfficeException
247 * @throws java.io.IOException
249 public void printToFileWithMSOffice( ParameterHelper _aGTA,
250 String _sInputFile,
251 String _sPrintFilename) throws OfficeException, java.io.IOException
253 String sDocumentSuffix = FileHelper.getSuffix(_sInputFile);
255 setPrinterName(_aGTA.getPrinterName());
257 ArrayList<String> aStartCommand = new ArrayList<String>();
258 if (isWordDocument(sDocumentSuffix))
260 aStartCommand = createWordPrintHelper();
262 else if (isExcelDocument(sDocumentSuffix))
264 aStartCommand = createExcelPrintHelper();
266 else if (isPowerPointDocument(sDocumentSuffix))
268 aStartCommand = createPowerPointPrintHelper();
270 else if (sDocumentSuffix.toLowerCase().equals(".xml"))
272 // TODO: Open XML File and check if we need excel or word
273 String sOfficeType = getOfficeType(_sInputFile);
275 // special case, if xml we prefer word, but with DEFAULT_XML_FORMAT_APP=excel it's changeable.
276 // if (_aGTA.getDefaultXMLFormatApp().toLowerCase().equals("excel"))
277 if (sOfficeType.equals("excel"))
279 aStartCommand = createExcelPrintHelper();
281 else if (sOfficeType.equals("word"))
283 aStartCommand = createWordPrintHelper();
285 else
287 return;
290 else
292 GlobalLogWriter.println("No Microsoft Office document format found.");
293 // TODO: use a better Exception!!!
294 throw new WrongSuffixException("No Mircosoft Office document format found.");
297 if (aStartCommand.isEmpty() == false)
299 String sPrinterName = m_sPrinterName;
300 if (sPrinterName == null)
302 sPrinterName = "";
305 // String sCommand = sStartCommand + " " +
306 // _sInputFile + " " +
307 // StringHelper.doubleQuote(m_sPrinterName) + " " +
308 // _sPrintFilename;
309 aStartCommand.add(_sInputFile);
310 aStartCommand.add(m_sPrinterName);
311 aStartCommand.add(_sPrintFilename);
313 realStartCommand(aStartCommand);
315 String sUserDir = System.getProperty("user.home");
316 _aGTA.getPerformance().readWordValuesFromFile(FileHelper.appendPath(sUserDir, "msofficeloadtimes.txt"));
317 FileHelper.createInfoFile(_sPrintFilename, _aGTA, "msoffice");
318 TimeHelper.waitInSeconds(2, "Give Microsoft Office some time to print.");
321 public void realStartCommand(ArrayList<String> _aStartCommand) throws OfficeException
323 if (_aStartCommand.isEmpty())
325 throw new OfficeException/*WrongEnvironmentException*/("Given list is empty.");
330 // Convert the StartCommand ArrayList to a String List
331 int nValues = _aStartCommand.size();
332 String[] aList = new String[nValues];
333 for (int i=0;i<nValues;i++)
335 String aStr = _aStartCommand.get(i);
336 if (aStr == null)
338 aStr = "";
340 if (aStr.length() == 0)
342 aStr = "\"\"";
344 aList[i] = new String(aStr);
347 // This is really the latest point where we can check if we are running within windows environment
348 if (! OSHelper.isWindows())
350 // TODO: use a better Exception!!!
351 throw new WrongEnvironmentException("We doesn't work within windows environment.");
355 ProcessHandler aHandler = new ProcessHandler(aList);
356 aHandler.executeSynchronously();
358 catch (IndexOutOfBoundsException e)
360 throw new WrongEnvironmentException("Given list is too short.");
363 // return aHandler.getExitCode();
367 private String getPerlExe()
369 final String sPerlExe = System.getProperty("perl.exe", "perl");
370 return sPerlExe;
373 ArrayList<String> createWordPrintHelper() throws java.io.IOException
375 // create a program in tmp file
376 String sTmpPath = util.utils.getUsersTempDir();
377 String ls = System.getProperty("line.separator");
379 String sPrintViaWord = "printViaWord.pl";
381 ArrayList<String> aList = searchLocalFile(sPrintViaWord);
382 if (aList.isEmpty() == false)
384 return aList;
387 String sFileName = FileHelper.appendPath(sTmpPath, sPrintViaWord);
388 File aFile = new File(sFileName);
389 FileWriter out = new FileWriter(aFile);
392 out.write( "eval 'exec perl -wS $0 ${1+\"$@\"}' " + ls );
393 out.write( " if 0; " + ls );
394 out.write( "use strict; " + ls );
395 out.write( "use Time::HiRes; " + ls );
396 out.write( "if ( $^O ne \"MSWin32\") " + ls );
397 out.write( "{ " + ls );
398 out.write( " print 'Windows only.\\n'; " + ls );
399 out.write( " print_usage(); " + ls );
400 out.write( " exit(1); " + ls );
401 out.write( "} " + ls );
402 out.write( " " + ls );
403 out.write( "use Win32::OLE; " + ls );
404 out.write( "use Win32::OLE::Const 'Microsoft Word'; " + ls );
405 out.write( " " + ls );
406 out.write( "# ------ usage ------ " + ls );
407 out.write( "sub print_usage() " + ls );
408 out.write( "{ " + ls );
409 out.write( " print STDERR \"Usage: word_print.pl <Word file> <name of printer> <output file> .\\n " + ls );
410 out.write( " Please use the same string for the name of the printer as you can find \\n " + ls );
411 out.write( " under Start-Control Panel-Printer and Faxes \\n " + ls );
412 out.write( " The name could look like the following line: \\n " + ls );
413 out.write( " Apple LaserWriter II NT v47.0 \\n " + ls );
414 out.write( " Sample command line: \\n " + ls );
415 out.write( " execl_print.pl c:\\book1.doc Apple LaserWriter II NT v47.0 c:\\output\\book1.ps \\n\"; " + ls );
416 out.write( "} " + ls );
417 out.write( " " + ls );
418 out.write( " " + ls );
419 out.write( "if ($#ARGV != 2) " + ls );
420 out.write( "{ " + ls );
421 out.write( " print 'Too less arguments.\\n'; " + ls );
422 out.write( " print_usage(); " + ls );
423 out.write( " exit(1); " + ls );
424 out.write( "} " + ls );
425 out.write( " " + ls );
426 out.write( "my $startWordTime = Time::HiRes::time(); " + ls );
427 out.write( "my $Word = Win32::OLE->new('Word.Application'); " + ls );
428 out.write( "my $stopWordTime = Time::HiRes::time() - $startWordTime; " + ls );
429 out.write( "# $Word->{'Visible'} = 1; # if you want to see what's going on " + ls );
430 out.write( "# , ReadOnly => 1})" + ls );
431 out.write(ls);
432 out.write( "my $startLoadWordTime = Time::HiRes::time(); " + ls );
433 out.write( "$Word->Documents->Open({Filename => $ARGV[0]}) " + ls );
434 out.write( " || die('Unable to open document ', Win32::OLE->LastError()); " + ls );
435 out.write( "my $stopLoadWordTime = Time::HiRes::time() - $startLoadWordTime; " + ls );
436 out.write(ls);
437 out.write( "my $startPrintWordTime = Time::HiRes::time(); " + ls);
438 out.write( "my $oldActivePrinte = $Word->{ActivePrinter} ; " + ls );
439 out.write( "$Word->{ActivePrinter} = $ARGV[1]; " + ls );
440 out.write( "$Word->ActiveDocument->PrintOut({ " + ls );
441 out.write( " Background => 0, " + ls );
442 out.write( " Append => 0, " + ls );
443 out.write( " Range => wdPrintAllDocument, " + ls );
444 out.write( " Item => wdPrintDocumentContent, " + ls );
445 out.write( " Copies => 1, " + ls );
446 out.write( " PageType => wdPrintAllPages, " + ls );
447 out.write( " PrintToFile => 1, " + ls );
448 out.write( " OutputFileName => $ARGV[2] " + ls );
449 out.write( " }); " + ls );
450 out.write( "$Word->{ActivePrinter} = $oldActivePrinte; " + ls );
451 out.write( "my $stopPrintWordTime = Time::HiRes::time() - $startPrintWordTime;" + ls);
453 out.write( "# ActiveDocument.Close(SaveChanges:=WdSaveOptions.wdDoNotSaveChanges)" + ls );
454 out.write( "my $sVersion = $Word->Application->Version();"+ls);
455 out.write( "$Word->ActiveDocument->Close({SaveChanges => 0}); " + ls );
456 out.write( "$Word->Quit(); " + ls );
458 out.write( "local *FILE;" + ls);
459 out.write( "if (open(FILE, \">$ENV{HOME}/msofficeloadtimes.txt\"))" + ls);
460 out.write( "{" + ls);
461 out.write( " print FILE \"name=$ARGV[0]\\n\";" + ls);
462 out.write( " print FILE \"WordVersion=$sVersion\\n\";" + ls);
463 out.write( " print FILE \"WordStartTime=$stopWordTime\\n\";" + ls);
464 out.write( " print FILE \"WordLoadTime=$stopLoadWordTime\\n\";" + ls);
465 out.write( " print FILE \"WordPrintTime=$stopPrintWordTime\\n\";" + ls);
466 out.write( " close(FILE);" + ls);
467 out.write( "}" + ls);
468 out.close();
470 aList.add(getPerlExe());
471 aList.add(sFileName);
472 return aList;
475 // TODO: Maybe give a possibility to say where search the script from outside
477 ArrayList<String> searchLocalFile(String _sScriptName)
479 String userdir = System.getProperty("user.dir");
481 ArrayList<String> aList = new ArrayList<String>();
482 String sFileName = FileHelper.appendPath(userdir, _sScriptName);
483 File aPerlScript = new File(sFileName);
484 if (FileHelper.isDebugEnabled())
486 GlobalLogWriter.println("Search for local existance of " + aPerlScript.getAbsolutePath());
489 if (aPerlScript.exists())
491 if (FileHelper.isDebugEnabled())
493 GlobalLogWriter.println("OK, found it, use this instead the internal one.");
496 String sName = aPerlScript.getAbsolutePath();
497 // String sCommand = "perl " + sName;
498 // System.out.println(sCommand);
499 aList.add("perl");
500 aList.add(sName);
501 return aList;
503 return aList;
506 ArrayList<String> createWordStoreHelper() throws java.io.IOException
508 // create a program in tmp file
509 String sTmpPath = util.utils.getUsersTempDir();
510 String ls = System.getProperty("line.separator");
512 // ArrayList aList = new ArrayList();
513 String sSaveViaWord = "saveViaWord.pl";
515 ArrayList<String> aList = searchLocalFile(sSaveViaWord);
516 if (aList.isEmpty() == false)
518 return aList;
521 String sName = FileHelper.appendPath(sTmpPath, sSaveViaWord);
522 if (FileHelper.isDebugEnabled())
524 GlobalLogWriter.println("No local found, create a perl script: " + sName);
527 File aFile = new File(sName);
528 FileWriter out = new FileWriter(aFile);
530 out.write( "eval 'exec perl -wS $0 ${1+\"$@\"}' " + ls );
531 out.write( " if 0; " + ls );
532 out.write( "use strict; " + ls );
533 out.write( " " + ls );
534 out.write( "if ( $^O ne \"MSWin32\") " + ls );
535 out.write( "{ " + ls );
536 out.write( " print 'Windows only.\\n'; " + ls );
537 out.write( " print_usage(); " + ls );
538 out.write( " exit(1); " + ls );
539 out.write( "} " + ls );
540 out.write( " " + ls );
541 out.write( "use Win32::OLE; " + ls );
542 out.write( "use Win32::OLE::Const 'Microsoft Word'; " + ls );
543 out.write( " " + ls );
544 out.write( "# ------ usage ------ " + ls );
545 out.write( "sub print_usage() " + ls );
546 out.write( "{ " + ls );
547 out.write( " print STDERR \"Usage: storeViaWord.pl <Word file> <output filer> <output file> \\n\" " + ls );
548 out.write( "} " + ls );
549 out.write( " " + ls );
550 out.write( " " + ls );
551 out.write( "if ($#ARGV != 2) " + ls );
552 out.write( "{ " + ls );
553 out.write( " print 'Too less arguments.\\n'; " + ls );
554 out.write( " print_usage(); " + ls );
555 out.write( " exit(1); " + ls );
556 out.write( "} " + ls );
557 out.write( " " + ls );
558 out.write( " " + ls );
559 out.write( "my $Word = Win32::OLE->new('Word.Application'); " + ls );
560 out.write( "# $Word->{'Visible'} = 1; # if you want to see what's going on " + ls );
561 out.write( "my $Book = $Word->Documents->Open($ARGV[0]) " + ls );
562 out.write( " || die('Unable to open document ', Win32::OLE->LastError()); " + ls );
563 out.write( "# my $oldActivePrinte = $Word->{ActivePrinter} ; " + ls );
564 out.write( "# $Word->{ActivePrinter} = $ARGV[1]; " + ls );
565 out.write( "# $Word->ActiveDocument->PrintOut({ " + ls );
566 out.write( "# Background => 0, " + ls );
567 out.write( "# Append => 0, " + ls );
568 out.write( "# Range => wdPrintAllDocument, " + ls );
569 out.write( "# Item => wdPrintDocumentContent, " + ls );
570 out.write( "# Copies => 1, " + ls );
571 out.write( "# PageType => wdPrintAllPages, " + ls );
572 out.write( "# PrintToFile => 1, " + ls );
573 out.write( "# OutputFileName => $ARGV[2] " + ls );
574 out.write( "# }); " + ls );
575 out.write( "# $Word->{ActivePrinter} = $oldActivePrinte; " + ls );
576 out.write( "$Book->savaAs($ARGV[2], $ARGV[1]); " + ls );
577 out.write( "# ActiveDocument.Close(SaveChanges:=WdSaveOptions.wdDoNotSaveChanges)" + ls );
578 out.write( "$Book->Close({SaveChanges => 0}); " + ls );
579 out.write( "$Word->Quit(); " + ls );
580 out.close();
582 aList.add(getPerlExe());
583 aList.add(sName);
584 return aList;
588 ArrayList<String> createExcelPrintHelper() throws java.io.IOException
590 // create a program in tmp file
591 String sTmpPath = util.utils.getUsersTempDir();
592 String ls = System.getProperty("line.separator");
594 String sPrintViaExcel = "printViaExcel.pl";
596 ArrayList<String> aList = searchLocalFile(sPrintViaExcel);
597 if (aList.isEmpty() == false)
599 return aList;
601 String sName = FileHelper.appendPath(sTmpPath, sPrintViaExcel);
602 if (FileHelper.isDebugEnabled())
604 GlobalLogWriter.println("No local found, create a perl script: " + sName);
607 File aFile = new File(sName);
608 FileWriter out = new FileWriter(aFile);
610 // out.write( "eval 'exec perl -wS $0 ${1+\"$@\"}' " + ls );
611 // out.write( " if 0; " + ls );
612 out.write("#BEGIN" + ls);
613 out.write("#{" + ls);
614 out.write("#" + ls);
615 out.write("# # insert HACK" + ls);
616 out.write("# unshift(@INC, '');" + ls);
617 out.write("#}" + ls);
618 out.write( "use strict; " + ls );
619 out.write( " " + ls );
620 out.write( "if ( $^O ne \"MSWin32\") " + ls );
621 out.write( "{ " + ls );
622 out.write( " print \"Windows only.\\n\"; " + ls );
623 out.write( " print_usage(); " + ls );
624 out.write( " exit(1); " + ls );
625 out.write( "} " + ls );
626 out.write( " " + ls );
627 out.write( " " + ls );
628 out.write( "use Win32::OLE qw(in with); " + ls );
629 out.write( "use Win32::OLE::Const 'Microsoft Excel'; " + ls );
630 out.write( " " + ls );
631 out.write( "# ------ usage ------ " + ls );
632 out.write( "sub print_usage() " + ls );
633 out.write( "{ " + ls );
634 out.write( " print STDERR \"Usage: printViaExcel.pl <Excel file> <name of printer> <output file> .\\n " + ls );
635 out.write( " Please use the same string for the name of the printer as you can find \\n " + ls );
636 out.write( " under Start-Control Panel-Printer and Faxes \\n " + ls );
637 out.write( " The name could look like the following line: \\n " + ls );
638 out.write( " Apple LaserWriter II NT v47.0 \\n " + ls );
639 out.write( " Sample command line: \\n " + ls );
640 out.write( " execl_print.pl c:\\book1.xls Apple LaserWriter II NT v47.0 c:\\output\\book1.ps \\n\"; " + ls );
641 out.write( "} " + ls );
642 out.write( " " + ls );
643 out.write( " " + ls );
644 out.write( " " + ls );
645 out.write( "$Win32::OLE::Warn = 3; # die on errors... " + ls );
646 out.write( " " + ls );
647 out.write( " " + ls );
648 out.write( "if ($#ARGV != 2) " + ls );
649 out.write( "{ " + ls );
650 out.write( " print STDERR \"Too less arguments.\\n\"; " + ls );
651 out.write( " print STDERR \"ARGV[0] $ARGV[0]\\n\"; " + ls );
652 out.write( " print STDERR \"ARGV[1] $ARGV[1]\\n\"; " + ls );
653 out.write( " print STDERR \"ARGV[2] $ARGV[2]\\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( " " + ls );
662 out.write( " " + ls );
663 out.write( " " + ls );
664 out.write( "my $Book = $Excel->Workbooks->Open( $ARGV[0] ); " + ls );
665 out.write( " $Book->PrintOut({Copies => 1, " + ls );
666 out.write( " ActivePrinter => $ARGV[1], " + ls );
667 out.write( " PrToFileName => $ARGV[2], " + ls );
668 out.write( " Collate => 1 " + ls );
669 out.write( " }); " + ls );
670 out.write( "# Close worksheets without store changes" + ls );
671 out.write( "# $Book->Close({SaveChanges => 0}); " + ls );
672 out.write( "my $sVersion = $Excel->Application->Version();"+ls);
673 out.write( "$Excel->Quit(); " + ls );
674 out.write( "local *FILE;" + ls);
675 out.write( "if (open(FILE, \">$ENV{HOME}/msofficeloadtimes.txt\"))" + ls);
676 out.write( "{" + ls);
677 out.write( " print FILE \"name=$ARGV[0]\\n\";" + ls);
678 out.write( " print FILE \"ExcelVersion=$sVersion\\n\";" + ls);
679 // out.write( " print FILE \"WordStartTime=$stopWordTime\\n\";" + ls);
680 // out.write( " print FILE \"WordLoadTime=$stopLoadWordTime\\n\";" + ls);
681 // out.write( " print FILE \"WordPrintTime=$stopPrintWordTime\\n\";" + ls);
682 out.write( " close(FILE);" + ls);
683 out.write( "}" + ls);
684 out.close();
686 aList.add(getPerlExe());
687 aList.add(sName);
688 return aList;
691 ArrayList<String> createExcelStoreHelper() throws java.io.IOException
693 // create a program in tmp file
694 String sTmpPath = util.utils.getUsersTempDir();
695 String ls = System.getProperty("line.separator");
697 String sSaveViaExcel = "saveViaExcel.pl";
699 ArrayList<String> aList = searchLocalFile(sSaveViaExcel);
700 if (aList.isEmpty() == false)
702 return aList;
704 String sName = FileHelper.appendPath(sTmpPath, sSaveViaExcel);
705 if (FileHelper.isDebugEnabled())
707 GlobalLogWriter.println("No local found, create a script: " + sName);
710 File aFile = new File(sName);
711 FileWriter out = new FileWriter(aFile);
713 out.write( "eval 'exec perl -wS $0 ${1+\"$@\"}' " + ls );
714 out.write( " if 0; " + ls );
715 out.write( "use strict; " + ls );
716 out.write( "# This script is automatically created. " + ls );
717 out.write( " " + ls );
718 out.write( "use Win32::OLE qw(in with); " + ls );
719 out.write( "use Win32::OLE::Const 'Microsoft Excel'; " + ls );
720 out.write( " " + ls );
721 out.write( "# ------ usage ------ " + ls );
722 out.write( "sub print_usage() " + ls );
723 out.write( "{ " + ls );
724 out.write( " print STDERR \"Usage: savaViaExcel.pl <Excel file> <filefilter> <output file> .\\n " + ls );
725 out.write( " execl_print.pl c:\\book1.xls Apple LaserWriter II NT v47.0 c:\\output\\book1.ps \\n\"; " + ls );
726 out.write( "} " + ls );
727 out.write( " " + ls );
728 out.write( " " + ls );
729 out.write( " " + ls );
730 out.write( "$Win32::OLE::Warn = 3; # die on errors... " + ls );
731 out.write( " " + ls );
732 out.write( " " + ls );
733 out.write( "if ($#ARGV != 2) " + ls );
734 out.write( "{ " + ls );
735 out.write( " print \"Too less arguments.\\n\"; " + ls );
736 out.write( " print_usage(); " + ls );
737 out.write( " exit(1); " + ls );
738 out.write( "} " + ls );
739 out.write( " " + ls );
740 out.write( "my $Excel = Win32::OLE->GetActiveObject('Excel.Application') " + ls );
741 out.write( " || Win32::OLE->new('Excel.Application', 'Quit'); # get already active Excel " + ls );
742 out.write( " # application or open new " + ls );
743 out.write( "my $sFilterParameter = $ARGV[1]; " + ls );
744 out.write( "my $sFilterName = xlHTML; " + ls );
745 out.write( "if ($sFilterParameter eq 'xlXMLSpreadsheet') " + ls );
746 out.write( "{ " + ls );
747 out.write( " $sFilterName = xlXMLSpreadsheet; " + ls );
748 out.write( "} " + ls );
749 out.write( "elsif ($sFilterParameter eq 'xlHTML') " + ls );
750 out.write( "{ " + ls );
751 out.write( " $sFilterName = xlHTML; " + ls );
752 out.write( "} " + ls );
753 out.write( "else " + ls );
754 out.write( "{ " + ls );
755 out.write( " my $undefined; " + ls);
756 out.write( " $sFilterName = $undefined; " + ls );
757 out.write( "} " + ls );
758 out.write( " " + ls );
759 out.write( "my $Book = $Excel->Workbooks->Open( $ARGV[0] ); " + ls );
760 out.write( "$Excel->{DisplayAlerts} = 0; " + ls );
761 out.write( "$Book->saveAs($ARGV[2], " + ls );
762 out.write( " $sFilterName, " + ls );
763 out.write( " '', " + ls );
764 out.write( " '', " + ls );
765 out.write( " 0, " + ls );
766 out.write( " 0, " + ls );
767 out.write( " xlNoChange, " + ls );
768 out.write( " xlLocalSessionChanges, " + ls );
769 out.write( " 1); " + ls );
770 out.write( "# Close worksheets without store changes" + ls );
771 out.write( "# $Book->Close({SaveChanges => 0}); " + ls );
772 out.write( "$Excel->Quit(); " + ls );
773 out.close();
775 aList.add(getPerlExe());
776 aList.add(sName);
777 return aList;
780 ArrayList<String> createPowerPointPrintHelper() throws java.io.IOException
782 // create a program in tmp file
783 String sTmpPath = util.utils.getUsersTempDir();
784 String ls = System.getProperty("line.separator");
786 String sPrintViaPowerPoint = "printViaPowerPoint.pl";
788 ArrayList<String> aList = searchLocalFile(sPrintViaPowerPoint);
789 if (aList.isEmpty() == false)
791 return aList;
793 String sName = FileHelper.appendPath(sTmpPath, sPrintViaPowerPoint);
794 if (FileHelper.isDebugEnabled())
796 GlobalLogWriter.println("No local found, create a script: " + sName);
799 File aFile = new File(sName);
800 FileWriter out = new FileWriter(aFile);
803 out.write( "eval 'exec perl -wS $0 $1 $2 ' " + ls );
804 out.write( " if 0; " + ls );
805 out.write( "use strict; " + ls );
806 out.write( " " + ls );
807 out.write( "if ( $^O ne \"MSWin32\") " + ls );
808 out.write( "{ " + ls );
809 out.write( " print \"Windows only.\\n\"; " + ls );
810 out.write( " print_usage(); " + ls );
811 out.write( " exit(1); " + ls );
812 out.write( "} " + ls );
813 out.write( " " + ls );
814 out.write( " " + ls );
815 out.write( "use Win32::OLE qw(in with); " + ls );
816 out.write( "use Win32::OLE::Const 'Microsoft PowerPoint'; " + ls );
817 out.write( " " + ls );
818 out.write( "# ------ usage ------ " + ls );
819 out.write( "sub print_usage() " + ls );
820 out.write( "{ " + ls );
821 out.write( " print STDERR \"Usage: powerpoint_print.pl <PowerPoint file> <name of printer> <output file> .\\n " + ls );
822 out.write( " Please use the same string for the name of the printer as you can find \\n " + ls );
823 out.write( " under Start-Control Panel-Printer and Faxes \\n " + ls );
824 out.write( " The name could look like the following line: \\n " + ls );
825 out.write( " Apple LaserWriter II NT v47.0 \\n " + ls );
826 out.write( " Sample command line: \\n " + ls );
827 out.write( " powerpoint_print.pl c:\\book.ppt Apple LaserWriter II NT v47.0 c:\\output\\book.ps \\n\"; " + ls );
828 out.write( "} " + ls );
829 out.write( " " + ls );
830 out.write( " " + ls );
831 out.write( " " + ls );
832 out.write( "$Win32::OLE::Warn = 3; # die on errors... " + ls );
833 out.write( " " + ls );
834 out.write( " " + ls );
835 out.write( "if ($#ARGV < 2) " + ls );
836 out.write( "{ " + ls );
837 out.write( " print \"Too less arguments.\\n\"; " + ls );
838 out.write( " print_usage(); " + ls );
839 out.write( " exit(1); " + ls );
840 out.write( "} " + ls );
841 out.write( " " + ls );
842 out.write( "my $PowerPoint = Win32::OLE->GetActiveObject('PowerPoint.Application') " + ls );
843 out.write( " || Win32::OLE->new('PowerPoint.Application', 'Quit'); # get already active Excel " + ls );
844 out.write( " # application or open new " + ls );
845 out.write( " " + ls );
846 out.write( " " + ls );
847 out.write( " " + ls );
848 out.write( " $PowerPoint->{'Visible'} = 1; " + ls );
849 out.write( " my $Presentation = $PowerPoint->Presentations->Add; " + ls );
850 out.write( " my $Presentation = $PowerPoint->Presentations->Open( $ARGV[0] ); " + ls );
851 out.write( "# we can't change active printer in powerpoint " + ls );
852 out.write( "# $Presentation->PrintOptions->{ActivePrinter} = $ARGV[1]; " + ls );
853 out.write( " print \"Active printer is: \" . $Presentation->PrintOptions->{ActivePrinter} . \"\\n\"; " + ls );
854 out.write( " $Presentation->PrintOptions->{PrintInBackground} = 0; " + ls );
855 out.write( " # PrintColorType = 1 means print in color and PrintColorType = 2 means print in gray " + ls );
856 out.write( " $Presentation->PrintOptions->{PrintColorType} = 1; " + ls );
857 out.write( " " + ls );
858 out.write( " $Presentation->PrintOut({PrintToFile => $ARGV[2]}); " + ls );
859 out.write( " sleep 5; " + ls );
860 out.write( " print \"Presentation has been printed\\n\"; " + ls );
861 out.write( "my $sVersion = $Presentation->Application->Version();"+ls);
862 out.write( " $PowerPoint->Quit(); " + ls );
864 out.write( "local *FILE;" + ls);
865 out.write( "if (open(FILE, \">$ENV{HOME}/msofficeloadtimes.txt\"))" + ls);
866 out.write( "{" + ls);
867 out.write( " print FILE \"name=$ARGV[0]\\n\";" + ls);
868 out.write( " print FILE \"PowerPointVersion=$sVersion\\n\";" + ls);
869 // out.write( " print FILE \"WordStartTime=$stopWordTime\\n\";" + ls);
870 // out.write( " print FILE \"WordLoadTime=$stopLoadWordTime\\n\";" + ls);
871 // out.write( " print FILE \"WordPrintTime=$stopPrintWordTime\\n\";" + ls);
872 out.write( " close(FILE);" + ls);
873 out.write( "}" + ls);
874 out.close();
876 aList.add(getPerlExe());
877 aList.add(sName);
878 return aList;
882 @param _sFilename a name to a ms office xml file
883 @return 'word' or 'excel' or '' if type not known
885 public String getOfficeType(String _sFilename)
887 File aFile = new File(_sFilename);
888 if (! aFile.exists())
890 GlobalLogWriter.println("couldn't find file " + _sFilename);
891 return "";
893 RandomAccessFile aReader = null;
894 String sOfficeType = "";
897 aReader = new RandomAccessFile(aFile,"r");
898 String aLine = "";
899 while (aLine != null)
901 aLine = aReader.readLine();
902 if (aLine != null)
904 aLine = aLine.trim();
905 if ( (! (aLine.length() < 2) ) &&
906 (! aLine.startsWith("#")) &&
907 (! aLine.startsWith(";")) )
909 int nIdx = aLine.indexOf("mso-application");
910 if (nIdx > 0)
912 if (aLine.indexOf("Word.Document") > 0)
914 sOfficeType = "word";
916 else if (aLine.indexOf("Excel") > 0)
918 sOfficeType = "excel";
920 else
922 GlobalLogWriter.println("Unknown/unsupported data file: " + aLine);
929 catch (java.io.FileNotFoundException fne)
931 System.out.println("couldn't open file " + _sFilename);
932 System.out.println("Message: " + fne.getMessage());
934 catch (java.io.IOException ie)
936 System.out.println("Exception while reading file " + _sFilename);
937 System.out.println("Message: " + ie.getMessage());
941 aReader.close();
943 catch (java.io.IOException ie)
945 System.out.println("Couldn't close file " + _sFilename);
946 System.out.println("Message: " + ie.getMessage());
948 return sOfficeType;
951 private static String getXMLDocumentFormat(String _sInputFile)
953 String sType = "word"; // default
956 // ---- Parse XML file ----
957 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
958 // factory.setNamespaceAware( true );
959 // factory.setValidating( true );
960 DocumentBuilder builder = factory.newDocumentBuilder();
961 Document document = builder.parse( new File (_sInputFile) );
962 Node rootNode = document.getDocumentElement();
964 // ---- Get list of nodes to given tag ----
965 // document.
966 // NodeList ndList = document.getElementsByTagName( sToSearch /* argv[2] */ );
967 // System.out.println( "\nNode list at the beginning:" );
968 String sRootNodeName = rootNode.getNodeName();
969 if (sRootNodeName.equals("w:wordDocument"))
971 sType = "word";
973 else if (sRootNodeName.equals("WorkBook"))
975 sType = "excel";
977 // there exists no powerpoint xml representation in MSOffice 2003
978 else
980 GlobalLogWriter.println("Error: unknown root node: '" + sRootNodeName + "' please check the document. Try to use Word as default.");
981 sType = "word"; // default
983 // printNodesFromList( ndList );
985 catch (java.lang.Exception e)
988 return sType;
991 // public static void main(String [] _args)
992 // {
993 // String sTest = getXMLDocumentFormat("c:/cws/temp/input/Blah Fasel.xml");
994 // }