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