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