merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / runner / graphical / MSOfficePostscriptCreator.java
blob49d6498fcb990df2f15fb2c53f2328cd1545f956
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: MSOfficePostscriptCreator.java,v $
10 * $Revision: 1.1.2.2 $
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 graphical;
33 import java.io.File;
34 import java.io.FileWriter;
35 import java.io.RandomAccessFile;
36 import helper.ProcessHandler;
37 import java.util.ArrayList;
38 import helper.OSHelper;
39 import javax.xml.parsers.DocumentBuilder;
40 import javax.xml.parsers.DocumentBuilderFactory;
41 import org.w3c.dom.Document;
42 import org.w3c.dom.Node;
44 /**
45 * This object gives all functionallity to print msoffice documents.
46 * It also offers functions to check what type of document it is.
47 * It handles *.doc as word documents and use word to print
48 * *.xls as excel
49 * *.ppt as powerpoint
52 //class ProcessHelper
53 //{
54 // ArrayList m_aArray;
55 //}
57 public class MSOfficePostscriptCreator implements IOffice
59 private String m_sPrinterName; // within Windows the tools need a printer name;
61 public void setPrinterName(String _s)
63 m_sPrinterName = _s;
66 private ParameterHelper m_aParameterHelper;
67 private String m_sDocumentName;
68 private String m_sResult;
70 // CTor
71 public MSOfficePostscriptCreator(ParameterHelper _aParam, String _sResult)
73 m_aParameterHelper = _aParam;
74 m_sResult = _sResult;
75 // String sKillCommand = (String)_aParam.getTestParameters().get(util.PropertyName.APP_KILL_COMMAND);
76 // if (sKillCommand == null)
77 // {
78 // sKillCommand = "";
79 // }
80 // if (sKillCommand.length() > 0)
81 // {
82 // sKillCommand += ";";
83 // }
84 String sKillCommand = "C:/bin/kill.exe -9 winword;C:/bin/kill.exe -9 excel";
85 _aParam.getTestParameters().put(util.PropertyName.APP_KILL_COMMAND, sKillCommand);
88 public void load(String _sDocumentName) throws OfficeException
90 m_sDocumentName = _sDocumentName;
92 if (! isMSOfficeDocumentFormat(m_sDocumentName))
94 GlobalLogWriter.get().println("This document type is not recognized as MSOffice format, as default fallback StarOffice/OpenOffice.org instead is used.");
95 throw new OfficeException("This document type is not recognized as MSOffice format, as default fallback StarOffice/OpenOffice.org instead is used.");
99 public void storeAsPostscript() throws OfficeException
101 GlobalLogWriter.get().println("USE MSOFFICE AS EXPORT FORMAT.");
104 String sDocumentName = m_sDocumentName + ".ps";
105 printToFileWithMSOffice(m_aParameterHelper,
106 m_sDocumentName,
107 m_sResult);
108 File aFile = new File(sDocumentName);
109 if (aFile.exists())
111 String sBasename = FileHelper.getBasename(sDocumentName);
112 FileHelper.addBasenameToIndex(m_sResult, sBasename, "msoffice", "postscript", m_sDocumentName);
115 catch(OfficeException e)
117 e.printStackTrace();
118 GlobalLogWriter.get().println(e.getMessage());
119 throw new OfficeException("Exception caught. Problem with MSOffice printer methods.");
121 catch(java.io.IOException e)
123 GlobalLogWriter.get().println(e.getMessage());
124 throw new OfficeException("IOException caught. Problem with MSOffice printer methods.");
128 public void start() throws OfficeException
130 // we don't have an office to start
133 public void close() throws OfficeException
135 // we don't have an office to stop
138 // -----------------------------------------------------------------------------
139 private boolean isWordDocument(String _sSuffix)
141 if (_sSuffix.toLowerCase().endsWith(".doc") ||
142 _sSuffix.toLowerCase().endsWith(".rtf") ||
143 _sSuffix.toLowerCase().endsWith(".dot"))
145 return true;
147 return false;
150 private boolean isExcelDocument(String _sSuffix)
152 // xlt templates
153 // xlw
154 // xla addin
155 if (_sSuffix.toLowerCase().endsWith(".xls"))
157 return true;
159 /* temporal insertion by SUS
160 if (_sSuffix.endsWith(".xml"))
162 return true;
164 return false;
167 private boolean isPowerPointDocument(String _sSuffix)
169 if (_sSuffix.toLowerCase().endsWith(".pps") ||
170 _sSuffix.toLowerCase().endsWith(".ppt"))
172 return true;
174 return false;
178 * returns true, if the given filename has a MS Office suffix.
180 private boolean isMSOfficeDocumentFormat(String _sFile)
182 String sDocumentSuffix = FileHelper.getSuffix(_sFile);
183 if (isWordDocument(sDocumentSuffix)) return true;
184 if (isExcelDocument(sDocumentSuffix)) return true;
185 if (isPowerPointDocument(sDocumentSuffix)) return true;
186 // if suffix is xml, return also true, but we can't decide if word or excel
187 if (sDocumentSuffix.toLowerCase().endsWith(".xml")) return true;
188 return false;
191 public void storeToFileWithMSOffice( ParameterHelper _aGTA,
192 String _sInputFile,
193 String _sOutputFile) throws OfficeException, java.io.IOException
195 String sDocumentSuffix = FileHelper.getSuffix(_sInputFile);
196 String sFilterName = _aGTA.getExportFilterName();
197 ArrayList aStartCommand = new ArrayList();
198 if (isWordDocument(sDocumentSuffix))
200 aStartCommand = createWordStoreHelper();
202 else if (isExcelDocument(sDocumentSuffix))
204 aStartCommand = createExcelStoreHelper();
206 else if (isPowerPointDocument(sDocumentSuffix))
209 else if (sDocumentSuffix.toLowerCase().equals(".xml"))
211 // special case, if xml we prefer word, but with DEFAULT_XML_FORMAT_APP=excel it's changeable.
212 String sDocFormat = getXMLDocumentFormat(_sInputFile);
213 // if (_aGTA.getDefaultXMLFormatApp().toLowerCase().equals("excel"))
214 if (sDocFormat.equals("excel"))
216 aStartCommand = createExcelStoreHelper();
218 else
220 aStartCommand = createWordStoreHelper();
222 // else
223 // {
224 // }
226 else
228 GlobalLogWriter.get().println("No Microsoft Office document format found.");
230 throw new WrongSuffixException("No MS office document format found.");
232 if (aStartCommand != null)
234 if (sFilterName == null)
236 // TODO: hardcoded FilterName in perl script
237 sFilterName = ""; // xlXMLSpreadsheet";
240 // String sCommand = sStartCommand + " " +
241 // _sInputFile + " " +
242 // StringHelper.doubleQuote(sFilterName) + " " +
243 // _sOutputFile;
245 aStartCommand.add(_sInputFile);
246 aStartCommand.add(sFilterName);
247 aStartCommand.add(_sOutputFile);
248 realStartCommand(aStartCommand);
252 // -----------------------------------------------------------------------------
254 * print the given file (_sInputFile) to the file name (_sPrintFile)
256 public void printToFileWithMSOffice( ParameterHelper _aGTA,
257 String _sInputFile,
258 String _sPrintFilename) throws OfficeException, java.io.IOException
260 String sDocumentSuffix = FileHelper.getSuffix(_sInputFile);
262 setPrinterName(_aGTA.getPrinterName());
264 ArrayList aStartCommand = new ArrayList();
265 if (isWordDocument(sDocumentSuffix))
267 aStartCommand = createWordPrintHelper();
269 else if (isExcelDocument(sDocumentSuffix))
271 aStartCommand = createExcelPrintHelper();
273 else if (isPowerPointDocument(sDocumentSuffix))
275 aStartCommand = createPowerPointPrintHelper();
277 else if (sDocumentSuffix.toLowerCase().equals(".xml"))
279 // TODO: Open XML File and check if we need excel or word
280 String sOfficeType = getOfficeType(_sInputFile);
282 // special case, if xml we prefer word, but with DEFAULT_XML_FORMAT_APP=excel it's changeable.
283 // if (_aGTA.getDefaultXMLFormatApp().toLowerCase().equals("excel"))
284 if (sOfficeType.equals("excel"))
286 aStartCommand = createExcelPrintHelper();
288 else if (sOfficeType.equals("word"))
290 aStartCommand = createWordPrintHelper();
292 else
294 return;
297 else
299 GlobalLogWriter.get().println("No Microsoft Office document format found.");
300 // TODO: use a better Exception!!!
301 throw new WrongSuffixException("No Mircosoft Office document format found.");
304 if (aStartCommand.isEmpty() == false)
306 String sPrinterName = m_sPrinterName;
307 if (sPrinterName == null)
309 sPrinterName = "";
312 // String sCommand = sStartCommand + " " +
313 // _sInputFile + " " +
314 // StringHelper.doubleQuote(m_sPrinterName) + " " +
315 // _sPrintFilename;
316 aStartCommand.add(_sInputFile);
317 aStartCommand.add(m_sPrinterName);
318 aStartCommand.add(_sPrintFilename);
320 realStartCommand(aStartCommand);
322 String sUserDir = System.getProperty("user.home");
323 _aGTA.getPerformance().readWordValuesFromFile(FileHelper.appendPath(sUserDir, "msofficeloadtimes.txt"));
324 FileHelper.createInfoFile(_sPrintFilename, _aGTA, "msoffice");
325 TimeHelper.waitInSeconds(2, "Give Microsoft Office some time to print.");
328 public void realStartCommand(ArrayList _aStartCommand) throws OfficeException
330 if (_aStartCommand.isEmpty())
332 throw new OfficeException/*WrongEnvironmentException*/("Given list is empty.");
337 // Convert the StartCommand ArrayList to a String List
338 int nValues = _aStartCommand.size();
339 String[] aList = new String[nValues];
340 for (int i=0;i<nValues;i++)
342 String aStr = (String) _aStartCommand.get(i);
343 if (aStr == null)
345 aStr = "";
347 if (aStr.length() == 0)
349 aStr = "\"\"";
351 aList[i] = new String(aStr);
354 // This is really the latest point where we can check if we are running within windows environment
355 if (! OSHelper.isWindows())
357 // TODO: use a better Exception!!!
358 throw new WrongEnvironmentException("We doesn't work within windows environment.");
362 ProcessHandler aHandler = new ProcessHandler(aList);
363 boolean bBackValue = aHandler.executeSynchronously();
365 catch (IndexOutOfBoundsException e)
367 throw new WrongEnvironmentException("Given list is too short.");
370 // return aHandler.getExitCode();
374 ArrayList createWordPrintHelper() throws java.io.IOException
376 // create a program in tmp file
377 String sTmpPath = util.utils.getUsersTempDir();
378 String ls = System.getProperty("line.separator");
380 String sPrintViaWord = "printViaWord.pl";
382 ArrayList aList = searchLocalFile(sPrintViaWord);
383 if (aList.isEmpty() == false)
385 return aList;
388 String sFileName = FileHelper.appendPath(sTmpPath, sPrintViaWord);
389 File aFile = new File(sFileName);
390 FileWriter out = new FileWriter(aFile);
393 out.write( "eval 'exec perl -wS $0 ${1+\"$@\"}' " + ls );
394 out.write( " if 0; " + ls );
395 out.write( "use strict; " + ls );
396 out.write( "use Time::HiRes; " + ls );
397 out.write( "if ( $^O ne \"MSWin32\") " + ls );
398 out.write( "{ " + ls );
399 out.write( " print 'Windows only.\\n'; " + ls );
400 out.write( " print_usage(); " + ls );
401 out.write( " exit(1); " + ls );
402 out.write( "} " + ls );
403 out.write( " " + ls );
404 out.write( "use Win32::OLE; " + ls );
405 out.write( "use Win32::OLE::Const 'Microsoft Word'; " + ls );
406 out.write( " " + ls );
407 out.write( "# ------ usage ------ " + ls );
408 out.write( "sub print_usage() " + ls );
409 out.write( "{ " + ls );
410 out.write( " print STDERR \"Usage: word_print.pl <Word file> <name of printer> <output file> .\\n " + ls );
411 out.write( " Please use the same string for the name of the printer as you can find \\n " + ls );
412 out.write( " under Start-Control Panel-Printer and Faxes \\n " + ls );
413 out.write( " The name could look like the the following line: \\n " + ls );
414 out.write( " Apple LaserWriter II NT v47.0 \\n " + ls );
415 out.write( " Sample command line: \\n " + ls );
416 out.write( " execl_print.pl c:\\book1.doc Apple LaserWriter II NT v47.0 c:\\output\\book1.ps \\n\"; " + ls );
417 out.write( "} " + ls );
418 out.write( " " + ls );
419 out.write( " " + ls );
420 out.write( "if ($#ARGV != 2) " + ls );
421 out.write( "{ " + ls );
422 out.write( " print 'Too less arguments.\\n'; " + ls );
423 out.write( " print_usage(); " + ls );
424 out.write( " exit(1); " + ls );
425 out.write( "} " + ls );
426 out.write( " " + ls );
427 out.write( "my $startWordTime = Time::HiRes::time(); " + ls );
428 out.write( "my $Word = Win32::OLE->new('Word.Application'); " + ls );
429 out.write( "my $stopWordTime = Time::HiRes::time() - $startWordTime; " + ls );
430 out.write( "# $Word->{'Visible'} = 1; # if you want to see what's going on " + ls );
431 out.write( "# , ReadOnly => 1})" + ls );
432 out.write(ls);
433 out.write( "my $startLoadWordTime = Time::HiRes::time(); " + ls );
434 out.write( "$Word->Documents->Open({Filename => $ARGV[0]}) " + ls );
435 out.write( " || die('Unable to open document ', Win32::OLE->LastError()); " + ls );
436 out.write( "my $stopLoadWordTime = Time::HiRes::time() - $startLoadWordTime; " + ls );
437 out.write(ls);
438 out.write( "my $startPrintWordTime = Time::HiRes::time(); " + ls);
439 out.write( "my $oldActivePrinte = $Word->{ActivePrinter} ; " + ls );
440 out.write( "$Word->{ActivePrinter} = $ARGV[1]; " + ls );
441 out.write( "$Word->ActiveDocument->PrintOut({ " + ls );
442 out.write( " Background => 0, " + ls );
443 out.write( " Append => 0, " + ls );
444 out.write( " Range => wdPrintAllDocument, " + ls );
445 out.write( " Item => wdPrintDocumentContent, " + ls );
446 out.write( " Copies => 1, " + ls );
447 out.write( " PageType => wdPrintAllPages, " + ls );
448 out.write( " PrintToFile => 1, " + ls );
449 out.write( " OutputFileName => $ARGV[2] " + ls );
450 out.write( " }); " + ls );
451 out.write( "$Word->{ActivePrinter} = $oldActivePrinte; " + ls );
452 out.write( "my $stopPrintWordTime = Time::HiRes::time() - $startPrintWordTime;" + ls);
454 out.write( "# ActiveDocument.Close(SaveChanges:=WdSaveOptions.wdDoNotSaveChanges)" + ls );
455 out.write( "my $sVersion = $Word->Application->Version();"+ls);
456 out.write( "$Word->ActiveDocument->Close({SaveChanges => 0}); " + ls );
457 out.write( "$Word->Quit(); " + ls );
459 out.write( "local *FILE;" + ls);
460 out.write( "if (open(FILE, \">$ENV{HOME}/msofficeloadtimes.txt\"))" + ls);
461 out.write( "{" + ls);
462 out.write( " print FILE \"name=$ARGV[0]\\n\";" + ls);
463 out.write( " print FILE \"WordVersion=$sVersion\\n\";" + ls);
464 out.write( " print FILE \"WordStartTime=$stopWordTime\\n\";" + ls);
465 out.write( " print FILE \"WordLoadTime=$stopLoadWordTime\\n\";" + ls);
466 out.write( " print FILE \"WordPrintTime=$stopPrintWordTime\\n\";" + ls);
467 out.write( " close(FILE);" + ls);
468 out.write( "}" + ls);
469 out.close();
471 aList.add("perl");
472 aList.add(sFileName);
473 return aList;
476 // TODO: Maybe give a possibility to say where search the script from outside
478 ArrayList searchLocalFile(String _sScriptName)
480 String userdir = System.getProperty("user.dir");
482 ArrayList aList = new ArrayList();
483 String sFileName = FileHelper.appendPath(userdir, _sScriptName);
484 File aPerlScript = new File(sFileName);
485 if (FileHelper.isDebugEnabled())
487 GlobalLogWriter.get().println("Search for local existance of " + aPerlScript.getAbsolutePath());
490 if (aPerlScript.exists())
492 if (FileHelper.isDebugEnabled())
494 GlobalLogWriter.get().println("OK, found it, use this instead the internal one.");
497 String sName = aPerlScript.getAbsolutePath();
498 // String sCommand = "perl " + sName;
499 // System.out.println(sCommand);
500 aList.add("perl");
501 aList.add(sName);
502 return aList;
504 return aList;
507 ArrayList createWordStoreHelper() throws java.io.IOException
509 // create a program in tmp file
510 String sTmpPath = util.utils.getUsersTempDir();
511 String ls = System.getProperty("line.separator");
513 // ArrayList aList = new ArrayList();
514 String sSaveViaWord = "saveViaWord.pl";
516 ArrayList aList = searchLocalFile(sSaveViaWord);
517 if (aList.isEmpty() == false)
519 return aList;
522 String sName = FileHelper.appendPath(sTmpPath, sSaveViaWord);
523 if (FileHelper.isDebugEnabled())
525 GlobalLogWriter.get().println("No local found, create a perl script: " + sName);
528 File aFile = new File(sName);
529 FileWriter out = new FileWriter(aFile);
531 out.write( "eval 'exec perl -wS $0 ${1+\"$@\"}' " + ls );
532 out.write( " if 0; " + ls );
533 out.write( "use strict; " + ls );
534 out.write( " " + ls );
535 out.write( "if ( $^O ne \"MSWin32\") " + ls );
536 out.write( "{ " + ls );
537 out.write( " print 'Windows only.\\n'; " + ls );
538 out.write( " print_usage(); " + ls );
539 out.write( " exit(1); " + ls );
540 out.write( "} " + ls );
541 out.write( " " + ls );
542 out.write( "use Win32::OLE; " + ls );
543 out.write( "use Win32::OLE::Const 'Microsoft Word'; " + ls );
544 out.write( " " + ls );
545 out.write( "# ------ usage ------ " + ls );
546 out.write( "sub print_usage() " + ls );
547 out.write( "{ " + ls );
548 out.write( " print STDERR \"Usage: storeViaWord.pl <Word file> <output filer> <output file> \\n\" " + ls );
549 out.write( "} " + ls );
550 out.write( " " + ls );
551 out.write( " " + ls );
552 out.write( "if ($#ARGV != 2) " + ls );
553 out.write( "{ " + ls );
554 out.write( " print 'Too less arguments.\\n'; " + ls );
555 out.write( " print_usage(); " + ls );
556 out.write( " exit(1); " + ls );
557 out.write( "} " + ls );
558 out.write( " " + ls );
559 out.write( " " + ls );
560 out.write( "my $Word = Win32::OLE->new('Word.Application'); " + ls );
561 out.write( "# $Word->{'Visible'} = 1; # if you want to see what's going on " + ls );
562 out.write( "my $Book = $Word->Documents->Open($ARGV[0]) " + ls );
563 out.write( " || die('Unable to open document ', Win32::OLE->LastError()); " + ls );
564 out.write( "# my $oldActivePrinte = $Word->{ActivePrinter} ; " + ls );
565 out.write( "# $Word->{ActivePrinter} = $ARGV[1]; " + ls );
566 out.write( "# $Word->ActiveDocument->PrintOut({ " + ls );
567 out.write( "# Background => 0, " + ls );
568 out.write( "# Append => 0, " + ls );
569 out.write( "# Range => wdPrintAllDocument, " + ls );
570 out.write( "# Item => wdPrintDocumentContent, " + ls );
571 out.write( "# Copies => 1, " + ls );
572 out.write( "# PageType => wdPrintAllPages, " + ls );
573 out.write( "# PrintToFile => 1, " + ls );
574 out.write( "# OutputFileName => $ARGV[2] " + ls );
575 out.write( "# }); " + ls );
576 out.write( "# $Word->{ActivePrinter} = $oldActivePrinte; " + ls );
577 out.write( "$Book->savaAs($ARGV[2], $ARGV[1]); " + ls );
578 out.write( "# ActiveDocument.Close(SaveChanges:=WdSaveOptions.wdDoNotSaveChanges)" + ls );
579 out.write( "$Book->Close({SaveChanges => 0}); " + ls );
580 out.write( "$Word->Quit(); " + ls );
581 out.close();
583 aList.add("perl");
584 aList.add(sName);
585 return aList;
589 ArrayList createExcelPrintHelper() throws java.io.IOException
591 // create a program in tmp file
592 String sTmpPath = util.utils.getUsersTempDir();
593 String ls = System.getProperty("line.separator");
595 String sPrintViaExcel = "printViaExcel.pl";
597 ArrayList aList = searchLocalFile(sPrintViaExcel);
598 if (aList.isEmpty() == false)
600 return aList;
602 String sName = FileHelper.appendPath(sTmpPath, sPrintViaExcel);
603 if (FileHelper.isDebugEnabled())
605 GlobalLogWriter.get().println("No local found, create a perl script: " + sName);
608 File aFile = new File(sName);
609 FileWriter out = new FileWriter(aFile);
611 out.write( "eval 'exec perl -wS $0 ${1+\"$@\"}' " + ls );
612 out.write( " if 0; " + ls );
613 out.write( "use strict; " + ls );
614 out.write( " " + ls );
615 out.write( "if ( $^O ne \"MSWin32\") " + ls );
616 out.write( "{ " + ls );
617 out.write( " print \"Windows only.\\n\"; " + ls );
618 out.write( " print_usage(); " + ls );
619 out.write( " exit(1); " + ls );
620 out.write( "} " + ls );
621 out.write( " " + ls );
622 out.write( " " + ls );
623 out.write( "use Win32::OLE qw(in with); " + ls );
624 out.write( "use Win32::OLE::Const 'Microsoft Excel'; " + ls );
625 out.write( " " + ls );
626 out.write( "# ------ usage ------ " + ls );
627 out.write( "sub print_usage() " + ls );
628 out.write( "{ " + ls );
629 out.write( " print STDERR \"Usage: printViaExcel.pl <Excel file> <name of printer> <output file> .\\n " + ls );
630 out.write( " Please use the same string for the name of the printer as you can find \\n " + ls );
631 out.write( " under Start-Control Panel-Printer and Faxes \\n " + ls );
632 out.write( " The name could look like the the following line: \\n " + ls );
633 out.write( " Apple LaserWriter II NT v47.0 \\n " + ls );
634 out.write( " Sample command line: \\n " + ls );
635 out.write( " execl_print.pl c:\\book1.xls Apple LaserWriter II NT v47.0 c:\\output\\book1.ps \\n\"; " + ls );
636 out.write( "} " + ls );
637 out.write( " " + ls );
638 out.write( " " + ls );
639 out.write( " " + ls );
640 out.write( "$Win32::OLE::Warn = 3; # die on errors... " + ls );
641 out.write( " " + ls );
642 out.write( " " + ls );
643 out.write( "if ($#ARGV != 2) " + ls );
644 out.write( "{ " + ls );
645 out.write( " print STDERR \"Too less arguments.\\n\"; " + ls );
646 out.write( " print STDERR \"ARGV[0] $ARGV[0]\\n\"; " + ls );
647 out.write( " print STDERR \"ARGV[1] $ARGV[1]\\n\"; " + ls );
648 out.write( " print STDERR \"ARGV[2] $ARGV[2]\\n\"; " + ls );
649 out.write( " print_usage(); " + ls );
650 out.write( " exit(1); " + ls );
651 out.write( "} " + ls );
652 out.write( " " + ls );
653 out.write( "my $Excel = Win32::OLE->GetActiveObject('Excel.Application') " + ls );
654 out.write( " || Win32::OLE->new('Excel.Application', 'Quit'); # get already active Excel " + ls );
655 out.write( " # application or open new " + ls );
656 out.write( " " + ls );
657 out.write( " " + ls );
658 out.write( " " + ls );
659 out.write( "my $Book = $Excel->Workbooks->Open( $ARGV[0] ); " + ls );
660 out.write( " $Book->PrintOut({Copies => 1, " + ls );
661 out.write( " ActivePrinter => $ARGV[1], " + ls );
662 out.write( " PrToFileName => $ARGV[2], " + ls );
663 out.write( " Collate => 1 " + ls );
664 out.write( " }); " + ls );
665 out.write( "# Close worksheets without store changes" + ls );
666 out.write( "# $Book->Close({SaveChanges => 0}); " + ls );
667 out.write( "my $sVersion = $Excel->Application->Version();"+ls);
668 out.write( "$Excel->Quit(); " + ls );
669 out.write( "local *FILE;" + ls);
670 out.write( "if (open(FILE, \">$ENV{HOME}/msofficeloadtimes.txt\"))" + ls);
671 out.write( "{" + ls);
672 out.write( " print FILE \"name=$ARGV[0]\\n\";" + ls);
673 out.write( " print FILE \"ExcelVersion=$sVersion\\n\";" + ls);
674 // out.write( " print FILE \"WordStartTime=$stopWordTime\\n\";" + ls);
675 // out.write( " print FILE \"WordLoadTime=$stopLoadWordTime\\n\";" + ls);
676 // out.write( " print FILE \"WordPrintTime=$stopPrintWordTime\\n\";" + ls);
677 out.write( " close(FILE);" + ls);
678 out.write( "}" + ls);
679 out.close();
681 aList.add("perl");
682 aList.add(sName);
683 return aList;
686 ArrayList createExcelStoreHelper() throws java.io.IOException
688 // create a program in tmp file
689 String sTmpPath = util.utils.getUsersTempDir();
690 String ls = System.getProperty("line.separator");
692 String sSaveViaExcel = "saveViaExcel.pl";
694 ArrayList aList = searchLocalFile(sSaveViaExcel);
695 if (aList.isEmpty() == false)
697 return aList;
699 String sName = FileHelper.appendPath(sTmpPath, sSaveViaExcel);
700 if (FileHelper.isDebugEnabled())
702 GlobalLogWriter.get().println("No local found, create a script: " + sName);
705 File aFile = new File(sName);
706 FileWriter out = new FileWriter(aFile);
708 out.write( "eval 'exec perl -wS $0 ${1+\"$@\"}' " + ls );
709 out.write( " if 0; " + ls );
710 out.write( "use strict; " + ls );
711 out.write( "# This script is automatically created. " + ls );
712 out.write( " " + ls );
713 out.write( "use Win32::OLE qw(in with); " + ls );
714 out.write( "use Win32::OLE::Const 'Microsoft Excel'; " + ls );
715 out.write( " " + ls );
716 out.write( "# ------ usage ------ " + ls );
717 out.write( "sub print_usage() " + ls );
718 out.write( "{ " + ls );
719 out.write( " print STDERR \"Usage: savaViaExcel.pl <Excel file> <filefilter> <output file> .\\n " + ls );
720 out.write( " execl_print.pl c:\\book1.xls Apple LaserWriter II NT v47.0 c:\\output\\book1.ps \\n\"; " + ls );
721 out.write( "} " + ls );
722 out.write( " " + ls );
723 out.write( " " + ls );
724 out.write( " " + ls );
725 out.write( "$Win32::OLE::Warn = 3; # die on errors... " + ls );
726 out.write( " " + ls );
727 out.write( " " + ls );
728 out.write( "if ($#ARGV != 2) " + ls );
729 out.write( "{ " + ls );
730 out.write( " print \"Too less arguments.\\n\"; " + ls );
731 out.write( " print_usage(); " + ls );
732 out.write( " exit(1); " + ls );
733 out.write( "} " + ls );
734 out.write( " " + ls );
735 out.write( "my $Excel = Win32::OLE->GetActiveObject('Excel.Application') " + ls );
736 out.write( " || Win32::OLE->new('Excel.Application', 'Quit'); # get already active Excel " + ls );
737 out.write( " # application or open new " + ls );
738 out.write( "my $sFilterParameter = $ARGV[1]; " + ls );
739 out.write( "my $sFilterName = xlHTML; " + ls );
740 out.write( "if ($sFilterParameter eq 'xlXMLSpreadsheet') " + ls );
741 out.write( "{ " + ls );
742 out.write( " $sFilterName = xlXMLSpreadsheet; " + ls );
743 out.write( "} " + ls );
744 out.write( "elsif ($sFilterParameter eq 'xlHTML') " + ls );
745 out.write( "{ " + ls );
746 out.write( " $sFilterName = xlHTML; " + ls );
747 out.write( "} " + ls );
748 out.write( "else " + ls );
749 out.write( "{ " + ls );
750 out.write( " my $undefined; " + ls);
751 out.write( " $sFilterName = $undefined; " + ls );
752 out.write( "} " + ls );
753 out.write( " " + ls );
754 out.write( "my $Book = $Excel->Workbooks->Open( $ARGV[0] ); " + ls );
755 out.write( "$Excel->{DisplayAlerts} = 0; " + ls );
756 out.write( "$Book->saveAs($ARGV[2], " + ls );
757 out.write( " $sFilterName, " + ls );
758 out.write( " '', " + ls );
759 out.write( " '', " + ls );
760 out.write( " 0, " + ls );
761 out.write( " 0, " + ls );
762 out.write( " xlNoChange, " + ls );
763 out.write( " xlLocalSessionChanges, " + ls );
764 out.write( " 1); " + ls );
765 out.write( "# Close worksheets without store changes" + ls );
766 out.write( "# $Book->Close({SaveChanges => 0}); " + ls );
767 out.write( "$Excel->Quit(); " + ls );
768 out.close();
770 aList.add("perl");
771 aList.add(sName);
772 return aList;
775 ArrayList createPowerPointPrintHelper() throws java.io.IOException
777 // create a program in tmp file
778 String sTmpPath = util.utils.getUsersTempDir();
779 String ls = System.getProperty("line.separator");
781 String sPrintViaPowerPoint = "printViaPowerPoint.pl";
783 ArrayList aList = searchLocalFile(sPrintViaPowerPoint);
784 if (aList.isEmpty() == false)
786 return aList;
788 String sName = FileHelper.appendPath(sTmpPath, sPrintViaPowerPoint);
789 if (FileHelper.isDebugEnabled())
791 GlobalLogWriter.get().println("No local found, create a script: " + sName);
794 File aFile = new File(sName);
795 FileWriter out = new FileWriter(aFile);
798 out.write( "eval 'exec perl -wS $0 $1 $2 ' " + ls );
799 out.write( " if 0; " + ls );
800 out.write( "use strict; " + ls );
801 out.write( " " + ls );
802 out.write( "if ( $^O ne \"MSWin32\") " + ls );
803 out.write( "{ " + ls );
804 out.write( " print \"Windows only.\\n\"; " + ls );
805 out.write( " print_usage(); " + ls );
806 out.write( " exit(1); " + ls );
807 out.write( "} " + ls );
808 out.write( " " + ls );
809 out.write( " " + ls );
810 out.write( "use Win32::OLE qw(in with); " + ls );
811 out.write( "use Win32::OLE::Const 'Microsoft PowerPoint'; " + ls );
812 out.write( " " + ls );
813 out.write( "# ------ usage ------ " + ls );
814 out.write( "sub print_usage() " + ls );
815 out.write( "{ " + ls );
816 out.write( " print STDERR \"Usage: powerpoint_print.pl <PowerPoint file> <name of printer> <output file> .\\n " + ls );
817 out.write( " Please use the same string for the name of the printer as you can find \\n " + ls );
818 out.write( " under Start-Control Panel-Printer and Faxes \\n " + ls );
819 out.write( " The name could look like the the following line: \\n " + ls );
820 out.write( " Apple LaserWriter II NT v47.0 \\n " + ls );
821 out.write( " Sample command line: \\n " + ls );
822 out.write( " powerpoint_print.pl c:\\book.ppt Apple LaserWriter II NT v47.0 c:\\output\\book.ps \\n\"; " + ls );
823 out.write( "} " + ls );
824 out.write( " " + ls );
825 out.write( " " + ls );
826 out.write( " " + ls );
827 out.write( "$Win32::OLE::Warn = 3; # die on errors... " + ls );
828 out.write( " " + ls );
829 out.write( " " + ls );
830 out.write( "if ($#ARGV < 2) " + ls );
831 out.write( "{ " + ls );
832 out.write( " print \"Too less arguments.\\n\"; " + ls );
833 out.write( " print_usage(); " + ls );
834 out.write( " exit(1); " + ls );
835 out.write( "} " + ls );
836 out.write( " " + ls );
837 out.write( "my $PowerPoint = Win32::OLE->GetActiveObject('PowerPoint.Application') " + ls );
838 out.write( " || Win32::OLE->new('PowerPoint.Application', 'Quit'); # get already active Excel " + ls );
839 out.write( " # application or open new " + ls );
840 out.write( " " + ls );
841 out.write( " " + ls );
842 out.write( " " + ls );
843 out.write( " $PowerPoint->{'Visible'} = 1; " + ls );
844 out.write( " my $Presentation = $PowerPoint->Presentations->Add; " + ls );
845 out.write( " my $Presentation = $PowerPoint->Presentations->Open( $ARGV[0] ); " + ls );
846 out.write( "# we can't change active printer in powerpoint " + ls );
847 out.write( "# $Presentation->PrintOptions->{ActivePrinter} = $ARGV[1]; " + ls );
848 out.write( " print \"Active printer is: \" . $Presentation->PrintOptions->{ActivePrinter} . \"\\n\"; " + ls );
849 out.write( " $Presentation->PrintOptions->{PrintInBackground} = 0; " + ls );
850 out.write( " # PrintColorType = 1 means print in color and PrintColorType = 2 means print in gray " + ls );
851 out.write( " $Presentation->PrintOptions->{PrintColorType} = 1; " + ls );
852 out.write( " " + ls );
853 out.write( " $Presentation->PrintOut({PrintToFile => $ARGV[2]}); " + ls );
854 out.write( " sleep 5; " + ls );
855 out.write( " print \"Presentation has been printed\\n\"; " + ls );
856 out.write( "my $sVersion = $Presentation->Application->Version();"+ls);
857 out.write( " $PowerPoint->Quit(); " + ls );
859 out.write( "local *FILE;" + ls);
860 out.write( "if (open(FILE, \">$ENV{HOME}/msofficeloadtimes.txt\"))" + ls);
861 out.write( "{" + ls);
862 out.write( " print FILE \"name=$ARGV[0]\\n\";" + ls);
863 out.write( " print FILE \"PowerPointVersion=$sVersion\\n\";" + ls);
864 // out.write( " print FILE \"WordStartTime=$stopWordTime\\n\";" + ls);
865 // out.write( " print FILE \"WordLoadTime=$stopLoadWordTime\\n\";" + ls);
866 // out.write( " print FILE \"WordPrintTime=$stopPrintWordTime\\n\";" + ls);
867 out.write( " close(FILE);" + ls);
868 out.write( "}" + ls);
869 out.close();
871 aList.add("perl");
872 aList.add(sName);
873 return aList;
877 @param _sFilename a name to a ms office xml file
878 @return 'word' or 'excel' or '' if type not known
880 public String getOfficeType(String _sFilename)
882 File aFile = new File(_sFilename);
883 if (! aFile.exists())
885 GlobalLogWriter.get().println("couldn't find file " + _sFilename);
886 return "";
888 RandomAccessFile aReader = null;
889 String sOfficeType = "";
892 aReader = new RandomAccessFile(aFile,"r");
893 String aLine = "";
894 while (aLine != null)
896 aLine = aReader.readLine();
897 if (aLine != null)
899 aLine = aLine.trim();
900 if ( (! (aLine.length() < 2) ) &&
901 (! aLine.startsWith("#")) &&
902 (! aLine.startsWith(";")) )
904 int nIdx = aLine.indexOf("mso-application");
905 if (nIdx > 0)
907 if (aLine.indexOf("Word.Document") > 0)
909 sOfficeType = "word";
911 else if (aLine.indexOf("Excel") > 0)
913 sOfficeType = "excel";
915 else
917 GlobalLogWriter.get().println("Unknown/unsupported data file: " + aLine);
924 catch (java.io.FileNotFoundException fne)
926 System.out.println("couldn't open file " + _sFilename);
927 System.out.println("Message: " + fne.getMessage());
929 catch (java.io.IOException ie)
931 System.out.println("Exception while reading file " + _sFilename);
932 System.out.println("Message: " + ie.getMessage());
936 aReader.close();
938 catch (java.io.IOException ie)
940 System.out.println("Couldn't close file " + _sFilename);
941 System.out.println("Message: " + ie.getMessage());
943 return sOfficeType;
946 private static String getXMLDocumentFormat(String _sInputFile)
948 String sType = "word"; // default
951 // ---- Parse XML file ----
952 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
953 // factory.setNamespaceAware( true );
954 // factory.setValidating( true );
955 DocumentBuilder builder = factory.newDocumentBuilder();
956 Document document = builder.parse( new File (_sInputFile) );
957 Node rootNode = document.getDocumentElement();
959 // ---- Get list of nodes to given tag ----
960 // document.
961 // NodeList ndList = document.getElementsByTagName( sToSearch /* argv[2] */ );
962 // System.out.println( "\nNode list at the beginning:" );
963 String sRootNodeName = rootNode.getNodeName();
964 if (sRootNodeName.equals("w:wordDocument"))
966 sType = "word";
968 else if (sRootNodeName.equals("WorkBook"))
970 sType = "excel";
972 // there exists no powerpoint xml representation in MSOffice 2003
973 else
975 GlobalLogWriter.println("Error: unknown root node: '" + sRootNodeName + "' please check the document. Try to use Word as default.");
976 sType = "word"; // default
978 // printNodesFromList( ndList );
980 catch (java.lang.Exception e)
983 return sType;
986 public static void main(String [] _args)
988 String sTest = getXMLDocumentFormat("c:/cws/temp/input/Blah Fasel.xml");