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