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