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