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