1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir package graphical; 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir import com.sun.star.frame.FrameSearchFlag; 31*cdf0e10cSrcweir import com.sun.star.util.XCloseable; 32*cdf0e10cSrcweir import helper.OfficeProvider; 33*cdf0e10cSrcweir import helper.OfficeWatcher; 34*cdf0e10cSrcweir import java.util.ArrayList; 35*cdf0e10cSrcweir 36*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 37*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 38*cdf0e10cSrcweir import com.sun.star.document.XTypeDetection; 39*cdf0e10cSrcweir import com.sun.star.container.XNameAccess; 40*cdf0e10cSrcweir import com.sun.star.frame.XDesktop; 41*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 42*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue; 43*cdf0e10cSrcweir import com.sun.star.frame.XComponentLoader; 44*cdf0e10cSrcweir import com.sun.star.lang.XComponent; 45*cdf0e10cSrcweir import com.sun.star.frame.XStorable; 46*cdf0e10cSrcweir import com.sun.star.view.XPrintable; 47*cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo; 48*cdf0e10cSrcweir import com.sun.star.frame.XModel; 49*cdf0e10cSrcweir import com.sun.star.uno.AnyConverter; 50*cdf0e10cSrcweir 51*cdf0e10cSrcweir import helper.URLHelper; 52*cdf0e10cSrcweir import helper.PropertyHelper; 53*cdf0e10cSrcweir import helper.OSHelper; 54*cdf0e10cSrcweir 55*cdf0e10cSrcweir // import helper.Parameter; 56*cdf0e10cSrcweir import java.io.File; 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir /** 59*cdf0e10cSrcweir * This Object is to print a given document with OpenOffice.org / StarOffice 60*cdf0e10cSrcweir * over the normal printer driver 61*cdf0e10cSrcweir * or over it's pdf exporter 62*cdf0e10cSrcweir */ 63*cdf0e10cSrcweir public class OpenOfficePostscriptCreator implements IOffice 64*cdf0e10cSrcweir { 65*cdf0e10cSrcweir private ParameterHelper m_aParameterHelper; 66*cdf0e10cSrcweir private String m_sOutputURL; 67*cdf0e10cSrcweir private String m_sBasename; 68*cdf0e10cSrcweir private String m_sDocumentName; 69*cdf0e10cSrcweir private XComponent m_aDocument; 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir public OpenOfficePostscriptCreator(ParameterHelper _aParam, String _sResult) 72*cdf0e10cSrcweir { 73*cdf0e10cSrcweir m_aParameterHelper = _aParam; 74*cdf0e10cSrcweir String sOutputURL = _sResult; 75*cdf0e10cSrcweir if (! sOutputURL.startsWith("file:")) 76*cdf0e10cSrcweir { 77*cdf0e10cSrcweir sOutputURL = URLHelper.getFileURLFromSystemPath(_sResult); 78*cdf0e10cSrcweir } 79*cdf0e10cSrcweir m_sOutputURL = sOutputURL; 80*cdf0e10cSrcweir m_aDocument = null; 81*cdf0e10cSrcweir } 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir 84*cdf0e10cSrcweir public void load(String _sDocumentName) throws OfficeException 85*cdf0e10cSrcweir { 86*cdf0e10cSrcweir m_sDocumentName = _sDocumentName; 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir String sInputFileURL = URLHelper.getFileURLFromSystemPath(m_sDocumentName); 89*cdf0e10cSrcweir m_aDocument = loadFromURL(m_aParameterHelper, sInputFileURL); 90*cdf0e10cSrcweir if (m_aDocument == null) 91*cdf0e10cSrcweir { 92*cdf0e10cSrcweir GlobalLogWriter.println("loadDocumentFromURL() failed with document: " + sInputFileURL); 93*cdf0e10cSrcweir throw new OfficeException("load(): failed with document" + sInputFileURL); 94*cdf0e10cSrcweir } 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir m_sBasename = FileHelper.getBasename(m_sDocumentName); 97*cdf0e10cSrcweir } 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir public void storeAsPostscript() throws OfficeException 100*cdf0e10cSrcweir { 101*cdf0e10cSrcweir if (m_aDocument != null) 102*cdf0e10cSrcweir { 103*cdf0e10cSrcweir String sDocumentName = FileHelper.appendPath(m_sOutputURL, m_sBasename); 104*cdf0e10cSrcweir if (m_aParameterHelper.getReferenceType().toLowerCase().equals("ooo") || 105*cdf0e10cSrcweir m_aParameterHelper.getReferenceType().toLowerCase().equals("o3") || 106*cdf0e10cSrcweir m_aParameterHelper.getReferenceType().toLowerCase().equals("ps") ) 107*cdf0e10cSrcweir { 108*cdf0e10cSrcweir String sPrintURL = sDocumentName + ".ps"; 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir impl_printToFileWithOOo(m_aParameterHelper, m_aDocument, sDocumentName, sPrintURL /*_sPrintFileURL*/); 111*cdf0e10cSrcweir String sBasename = FileHelper.getBasename(sPrintURL); 112*cdf0e10cSrcweir FileHelper.addBasenameToIndex(m_sOutputURL, sBasename, "OOo", "postscript", m_sDocumentName); 113*cdf0e10cSrcweir } 114*cdf0e10cSrcweir else if (m_aParameterHelper.getReferenceType().toLowerCase().equals("pdf")) 115*cdf0e10cSrcweir { 116*cdf0e10cSrcweir String sPDFURL = sDocumentName + ".pdf"; 117*cdf0e10cSrcweir storeAsPDF(m_aParameterHelper, m_aDocument, sPDFURL); 118*cdf0e10cSrcweir 119*cdf0e10cSrcweir String sBasename = FileHelper.getBasename(sPDFURL); 120*cdf0e10cSrcweir FileHelper.addBasenameToIndex(m_sOutputURL, sBasename, "pdf", "pdf-export", m_sDocumentName); 121*cdf0e10cSrcweir } 122*cdf0e10cSrcweir else 123*cdf0e10cSrcweir { 124*cdf0e10cSrcweir throw new OfficeException("unknown reference type"); 125*cdf0e10cSrcweir } 126*cdf0e10cSrcweir GlobalLogWriter.println("Close document."); 127*cdf0e10cSrcweir m_aDocument.dispose(); 128*cdf0e10cSrcweir } 129*cdf0e10cSrcweir } 130*cdf0e10cSrcweir 131*cdf0e10cSrcweir public void start() throws OfficeException 132*cdf0e10cSrcweir { 133*cdf0e10cSrcweir startOffice(); 134*cdf0e10cSrcweir } 135*cdf0e10cSrcweir 136*cdf0e10cSrcweir public void close() throws OfficeException 137*cdf0e10cSrcweir { 138*cdf0e10cSrcweir stopOffice(); 139*cdf0e10cSrcweir } 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir 142*cdf0e10cSrcweir 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir 145*cdf0e10cSrcweir private void showProperty(PropertyValue _aValue) 146*cdf0e10cSrcweir { 147*cdf0e10cSrcweir String sName = _aValue.Name; 148*cdf0e10cSrcweir String sValue; 149*cdf0e10cSrcweir try 150*cdf0e10cSrcweir { 151*cdf0e10cSrcweir sValue = AnyConverter.toString(_aValue.Value); 152*cdf0e10cSrcweir GlobalLogWriter.println("Property " + sName + ":=" + sValue); 153*cdf0e10cSrcweir } 154*cdf0e10cSrcweir catch (com.sun.star.lang.IllegalArgumentException e) 155*cdf0e10cSrcweir { 156*cdf0e10cSrcweir GlobalLogWriter.println("showProperty: can't convert a object to string. " + e.getMessage()); 157*cdf0e10cSrcweir } 158*cdf0e10cSrcweir } 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir /** 161*cdf0e10cSrcweir * shows the FilterName and MediaType from the given XComponent 162*cdf0e10cSrcweir */ 163*cdf0e10cSrcweir private String getDocumentType( XComponent _aDoc ) 164*cdf0e10cSrcweir { 165*cdf0e10cSrcweir XModel xModel = UnoRuntime.queryInterface( XModel.class, _aDoc); 166*cdf0e10cSrcweir PropertyValue[] aArgs = xModel.getArgs(); 167*cdf0e10cSrcweir for (int i=0;i<aArgs.length;i++) 168*cdf0e10cSrcweir { 169*cdf0e10cSrcweir PropertyValue aValue = aArgs[i]; 170*cdf0e10cSrcweir // System.out.print("Property: '" + aValue.Name); 171*cdf0e10cSrcweir // System.out.println("' := '" + aValue.Value + "'"); 172*cdf0e10cSrcweir if (aValue.Name.equals("FilterName") || 173*cdf0e10cSrcweir aValue.Name.equals("MediaType")) 174*cdf0e10cSrcweir { 175*cdf0e10cSrcweir String sNameValue = "'" + aValue.Name + "' := '" + aValue.Value + "'"; 176*cdf0e10cSrcweir return sNameValue; 177*cdf0e10cSrcweir } 178*cdf0e10cSrcweir } 179*cdf0e10cSrcweir return ""; 180*cdf0e10cSrcweir } 181*cdf0e10cSrcweir 182*cdf0e10cSrcweir private void showDocumentType( XComponent _aDoc ) 183*cdf0e10cSrcweir { 184*cdf0e10cSrcweir String sNameValue = getDocumentType(_aDoc); 185*cdf0e10cSrcweir GlobalLogWriter.println(" Property: '" + sNameValue); 186*cdf0e10cSrcweir } 187*cdf0e10cSrcweir /** 188*cdf0e10cSrcweir * load a OpenOffice.org document from a given URL (_sInputURL) 189*cdf0e10cSrcweir * the ParameterHelper must contain a living MultiServiceFactory object 190*cdf0e10cSrcweir * or we crash here. 191*cdf0e10cSrcweir * Be aware, the ownership of the document gets to you, you have to close it. 192*cdf0e10cSrcweir */ 193*cdf0e10cSrcweir private XComponent loadFromURL(ParameterHelper _aGTA, 194*cdf0e10cSrcweir String _sInputURL) 195*cdf0e10cSrcweir { 196*cdf0e10cSrcweir XComponent aDoc = null; 197*cdf0e10cSrcweir try 198*cdf0e10cSrcweir { 199*cdf0e10cSrcweir if (_aGTA.getMultiServiceFactory() == null) 200*cdf0e10cSrcweir { 201*cdf0e10cSrcweir GlobalLogWriter.println("MultiServiceFactory in GraphicalTestArgument not set."); 202*cdf0e10cSrcweir return null; 203*cdf0e10cSrcweir } 204*cdf0e10cSrcweir Object oDsk = _aGTA.getMultiServiceFactory().createInstance("com.sun.star.frame.Desktop"); 205*cdf0e10cSrcweir XDesktop aDesktop = UnoRuntime.queryInterface(XDesktop.class, oDsk); 206*cdf0e10cSrcweir 207*cdf0e10cSrcweir if (aDesktop != null) 208*cdf0e10cSrcweir { 209*cdf0e10cSrcweir GlobalLogWriter.println("com.sun.star.frame.Desktop created."); 210*cdf0e10cSrcweir // String sInputURL = aCurrentParameter.sInputURL; 211*cdf0e10cSrcweir // String sOutputURL = aCurrentParameter.sOutputURL; 212*cdf0e10cSrcweir // String sPrintFileURL = aCurrentParameter.sPrintToFileURL; 213*cdf0e10cSrcweir // System.out.println(_sInputURL); 214*cdf0e10cSrcweir 215*cdf0e10cSrcweir 216*cdf0e10cSrcweir // set here the loadComponentFromURL() properties 217*cdf0e10cSrcweir // at the moment only 'Hidden' is set, so no window is opened at work 218*cdf0e10cSrcweir 219*cdf0e10cSrcweir ArrayList<PropertyValue> aPropertyList = new ArrayList<PropertyValue>(); 220*cdf0e10cSrcweir 221*cdf0e10cSrcweir // check which properties should set and count it. 222*cdf0e10cSrcweir // if (_aGTA.isHidden()) 223*cdf0e10cSrcweir // { 224*cdf0e10cSrcweir // nPropertyCount ++; 225*cdf0e10cSrcweir // } 226*cdf0e10cSrcweir // if (_aGTA.getImportFilterName() != null && _aGTA.getImportFilterName().length() > 0) 227*cdf0e10cSrcweir // { 228*cdf0e10cSrcweir // nPropertyCount ++; 229*cdf0e10cSrcweir // } 230*cdf0e10cSrcweir 231*cdf0e10cSrcweir // initialize the propertyvalue 232*cdf0e10cSrcweir // int nPropertyIndex = 0; 233*cdf0e10cSrcweir // aProps = new PropertyValue[ nPropertyCount ]; 234*cdf0e10cSrcweir 235*cdf0e10cSrcweir // set all property values 236*cdf0e10cSrcweir if (_aGTA.isHidden()) 237*cdf0e10cSrcweir { 238*cdf0e10cSrcweir PropertyValue Arg = new PropertyValue(); 239*cdf0e10cSrcweir Arg.Name = "Hidden"; 240*cdf0e10cSrcweir Arg.Value = Boolean.TRUE; 241*cdf0e10cSrcweir aPropertyList.add(Arg); 242*cdf0e10cSrcweir showProperty(Arg); 243*cdf0e10cSrcweir } 244*cdf0e10cSrcweir if (_aGTA.getImportFilterName() != null && _aGTA.getImportFilterName().length() > 0) 245*cdf0e10cSrcweir { 246*cdf0e10cSrcweir PropertyValue Arg = new PropertyValue(); 247*cdf0e10cSrcweir Arg.Name = "FilterName"; 248*cdf0e10cSrcweir Arg.Value = _aGTA.getImportFilterName(); 249*cdf0e10cSrcweir aPropertyList.add(Arg); 250*cdf0e10cSrcweir showProperty(Arg); 251*cdf0e10cSrcweir } 252*cdf0e10cSrcweir PropertyValue ReadOnly = new PropertyValue(); 253*cdf0e10cSrcweir ReadOnly.Name = "ReadOnly"; 254*cdf0e10cSrcweir ReadOnly.Value = Boolean.TRUE; 255*cdf0e10cSrcweir aPropertyList.add(ReadOnly); 256*cdf0e10cSrcweir showProperty(ReadOnly); 257*cdf0e10cSrcweir 258*cdf0e10cSrcweir GlobalLogWriter.println(DateHelper.getDateTimeForHumanreadableLog() + " Load document"); 259*cdf0e10cSrcweir // GlobalLogWriter.flush(); 260*cdf0e10cSrcweir 261*cdf0e10cSrcweir XComponentLoader aCompLoader = UnoRuntime.queryInterface( XComponentLoader.class, aDesktop); 262*cdf0e10cSrcweir 263*cdf0e10cSrcweir // XComponent aDoc = null; 264*cdf0e10cSrcweir 265*cdf0e10cSrcweir _aGTA.getPerformance().startTime(PerformanceContainer.Load); 266*cdf0e10cSrcweir aDoc = aCompLoader.loadComponentFromURL(_sInputURL, "_blank", FrameSearchFlag.ALL, PropertyHelper.createPropertyValueArrayFormArrayList(aPropertyList) ); 267*cdf0e10cSrcweir _aGTA.getPerformance().stopTime(PerformanceContainer.Load); 268*cdf0e10cSrcweir if (aDoc != null) 269*cdf0e10cSrcweir { 270*cdf0e10cSrcweir GlobalLogWriter.println(DateHelper.getDateTimeForHumanreadableLog() + " Load document done."); 271*cdf0e10cSrcweir showDocumentType(aDoc); 272*cdf0e10cSrcweir _aGTA.setDocumentType(getDocumentType(aDoc)); 273*cdf0e10cSrcweir // TODO: TimeHelper.waitInSeconds(20, "Wait after load document. Maybe helps due to layouting problems."); 274*cdf0e10cSrcweir } 275*cdf0e10cSrcweir else 276*cdf0e10cSrcweir { 277*cdf0e10cSrcweir GlobalLogWriter.println(" Load document failed."); 278*cdf0e10cSrcweir if (_aGTA.getImportFilterName() != null && _aGTA.getImportFilterName().length() > 0) 279*cdf0e10cSrcweir { 280*cdf0e10cSrcweir GlobalLogWriter.println(" Please check FilterName := '" + _aGTA.getImportFilterName() + "'"); 281*cdf0e10cSrcweir } 282*cdf0e10cSrcweir GlobalLogWriter.println(""); 283*cdf0e10cSrcweir } 284*cdf0e10cSrcweir } 285*cdf0e10cSrcweir else 286*cdf0e10cSrcweir { 287*cdf0e10cSrcweir GlobalLogWriter.println("com.sun.star.frame.Desktop failed."); 288*cdf0e10cSrcweir } 289*cdf0e10cSrcweir } 290*cdf0e10cSrcweir catch ( com.sun.star.uno.Exception e ) 291*cdf0e10cSrcweir { 292*cdf0e10cSrcweir // Some exception occures.FAILED 293*cdf0e10cSrcweir GlobalLogWriter.println("UNO Exception caught."); 294*cdf0e10cSrcweir GlobalLogWriter.println("Message: " + e.getMessage()); 295*cdf0e10cSrcweir e.printStackTrace(); 296*cdf0e10cSrcweir aDoc = null; 297*cdf0e10cSrcweir } 298*cdf0e10cSrcweir return aDoc; 299*cdf0e10cSrcweir } 300*cdf0e10cSrcweir 301*cdf0e10cSrcweir private boolean exportToPDF(XComponent _xComponent, String _sDestinationName) 302*cdf0e10cSrcweir { 303*cdf0e10cSrcweir XServiceInfo xServiceInfo = 304*cdf0e10cSrcweir UnoRuntime.queryInterface( 305*cdf0e10cSrcweir XServiceInfo.class, _xComponent 306*cdf0e10cSrcweir ); 307*cdf0e10cSrcweir 308*cdf0e10cSrcweir ArrayList<PropertyValue> aPropertyList = new ArrayList<PropertyValue>(); 309*cdf0e10cSrcweir PropertyValue aFiltername = new PropertyValue(); 310*cdf0e10cSrcweir aFiltername.Name = "FilterName"; 311*cdf0e10cSrcweir aFiltername.Value = getFilterName_forPDF(xServiceInfo); 312*cdf0e10cSrcweir aPropertyList.add(aFiltername); 313*cdf0e10cSrcweir showProperty(aFiltername); 314*cdf0e10cSrcweir boolean bWorked = true; 315*cdf0e10cSrcweir 316*cdf0e10cSrcweir // TODO: TimeHelper.waitInSeconds(20, "Wait before storeToURL. Maybe helps due to layouting problems."); 317*cdf0e10cSrcweir try 318*cdf0e10cSrcweir { 319*cdf0e10cSrcweir XStorable store = 320*cdf0e10cSrcweir UnoRuntime.queryInterface( 321*cdf0e10cSrcweir XStorable.class, _xComponent 322*cdf0e10cSrcweir ); 323*cdf0e10cSrcweir store.storeToURL(_sDestinationName, PropertyHelper.createPropertyValueArrayFormArrayList(aPropertyList)); 324*cdf0e10cSrcweir } 325*cdf0e10cSrcweir catch (com.sun.star.io.IOException e) 326*cdf0e10cSrcweir { 327*cdf0e10cSrcweir GlobalLogWriter.println("IO Exception caught."); 328*cdf0e10cSrcweir GlobalLogWriter.println("Message: " + e.getMessage()); 329*cdf0e10cSrcweir bWorked = false; 330*cdf0e10cSrcweir } 331*cdf0e10cSrcweir 332*cdf0e10cSrcweir return bWorked; 333*cdf0e10cSrcweir } 334*cdf0e10cSrcweir 335*cdf0e10cSrcweir 336*cdf0e10cSrcweir private String getFilterName_forPDF(XServiceInfo xServiceInfo) 337*cdf0e10cSrcweir { 338*cdf0e10cSrcweir String filterName = ""; 339*cdf0e10cSrcweir 340*cdf0e10cSrcweir if (xServiceInfo.supportsService("com.sun.star.text.TextDocument")) 341*cdf0e10cSrcweir { 342*cdf0e10cSrcweir //writer 343*cdf0e10cSrcweir filterName = "writer_pdf_Export"; 344*cdf0e10cSrcweir } 345*cdf0e10cSrcweir else if ( xServiceInfo.supportsService( "com.sun.star.sheet.SpreadsheetDocument" ) ) 346*cdf0e10cSrcweir { 347*cdf0e10cSrcweir //calc 348*cdf0e10cSrcweir filterName = "calc_pdf_Export"; 349*cdf0e10cSrcweir } 350*cdf0e10cSrcweir else if ( xServiceInfo.supportsService( "com.sun.star.drawing.DrawingDocument" ) ) 351*cdf0e10cSrcweir { 352*cdf0e10cSrcweir //draw 353*cdf0e10cSrcweir filterName = "draw_pdf_Export"; 354*cdf0e10cSrcweir } 355*cdf0e10cSrcweir else if ( xServiceInfo.supportsService( "com.sun.star.presentation.PresentationDocument" ) ) 356*cdf0e10cSrcweir { 357*cdf0e10cSrcweir //impress 358*cdf0e10cSrcweir filterName = "impress_pdf_Export"; 359*cdf0e10cSrcweir } 360*cdf0e10cSrcweir else if (xServiceInfo.supportsService("com.sun.star.text.WebDocument")) 361*cdf0e10cSrcweir { 362*cdf0e10cSrcweir //html document 363*cdf0e10cSrcweir filterName = "writer_web_pdf_Export"; 364*cdf0e10cSrcweir } 365*cdf0e10cSrcweir else if ( xServiceInfo.supportsService("com.sun.star.text.GlobalDocument") ) 366*cdf0e10cSrcweir { 367*cdf0e10cSrcweir //master document 368*cdf0e10cSrcweir filterName = "writer_globaldocument_pdf_Export"; 369*cdf0e10cSrcweir } 370*cdf0e10cSrcweir else if ( xServiceInfo.supportsService( "com.sun.star.formulaFormulaProperties" ) ) 371*cdf0e10cSrcweir { 372*cdf0e10cSrcweir //math document 373*cdf0e10cSrcweir filterName = "math_pdf_Export"; 374*cdf0e10cSrcweir } 375*cdf0e10cSrcweir 376*cdf0e10cSrcweir return filterName; 377*cdf0e10cSrcweir } 378*cdf0e10cSrcweir 379*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 380*cdf0e10cSrcweir 381*cdf0e10cSrcweir // public boolean storeAsPDF(ParameterHelper _aGTA, 382*cdf0e10cSrcweir // String _sInputURL, 383*cdf0e10cSrcweir // String _sOutputURL) 384*cdf0e10cSrcweir // { 385*cdf0e10cSrcweir // boolean bBack = false; 386*cdf0e10cSrcweir // XComponent aDoc = loadFromURL(_aGTA, _sInputURL); 387*cdf0e10cSrcweir // 388*cdf0e10cSrcweir // if (aDoc == null) 389*cdf0e10cSrcweir // { 390*cdf0e10cSrcweir // GlobalLogWriter.println("Can't load document."); 391*cdf0e10cSrcweir // return bBack; 392*cdf0e10cSrcweir // } 393*cdf0e10cSrcweir // bBack = storeAsPDF(_aGTA, aDoc, _sOutputURL); 394*cdf0e10cSrcweir // FileHelper.createInfoFile(_sOutputURL, _aGTA, "as pdf"); 395*cdf0e10cSrcweir // 396*cdf0e10cSrcweir // GlobalLogWriter.println("Close document."); 397*cdf0e10cSrcweir // aDoc.dispose(); 398*cdf0e10cSrcweir // return bBack; 399*cdf0e10cSrcweir // } 400*cdf0e10cSrcweir 401*cdf0e10cSrcweir public boolean storeAsPDF(ParameterHelper _aGTA, 402*cdf0e10cSrcweir XComponent _aDoc, 403*cdf0e10cSrcweir String _sOutputURL) throws OfficeException 404*cdf0e10cSrcweir { 405*cdf0e10cSrcweir // try { 406*cdf0e10cSrcweir boolean bBack = true; 407*cdf0e10cSrcweir _aGTA.getPerformance().startTime(PerformanceContainer.StoreAsPDF); 408*cdf0e10cSrcweir bBack = exportToPDF(_aDoc, _sOutputURL); 409*cdf0e10cSrcweir _aGTA.getPerformance().stopTime(PerformanceContainer.StoreAsPDF); 410*cdf0e10cSrcweir 411*cdf0e10cSrcweir if (!bBack) 412*cdf0e10cSrcweir { 413*cdf0e10cSrcweir GlobalLogWriter.println("Can't store document as PDF."); 414*cdf0e10cSrcweir // bBack = false; 415*cdf0e10cSrcweir throw new OfficeException("Can't store document as PDF"); 416*cdf0e10cSrcweir } 417*cdf0e10cSrcweir else 418*cdf0e10cSrcweir { 419*cdf0e10cSrcweir FileHelper.createInfoFile(_sOutputURL, _aGTA, "as pdf"); 420*cdf0e10cSrcweir } 421*cdf0e10cSrcweir return bBack; 422*cdf0e10cSrcweir } 423*cdf0e10cSrcweir 424*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 425*cdf0e10cSrcweir 426*cdf0e10cSrcweir /** 427*cdf0e10cSrcweir * print the document found in file (_sInputURL) to as postscript to file (_sPrintFileURL) 428*cdf0e10cSrcweir * Due to the fact we use a printer to convert the file to postscript, the default printer 429*cdf0e10cSrcweir * to create such postscript format must be installed, this is not tested here. 430*cdf0e10cSrcweir * 431*cdf0e10cSrcweir * @return true, if print has been done. 432*cdf0e10cSrcweir * Be careful, true means only print returns with no errors, to be sure print is really done 433*cdf0e10cSrcweir * check existance of _sPrintFileURL 434*cdf0e10cSrcweir */ 435*cdf0e10cSrcweir 436*cdf0e10cSrcweir // public boolean printToFileWithOOo(ParameterHelper _aGTA, 437*cdf0e10cSrcweir // String _sInputURL, 438*cdf0e10cSrcweir // String _sOutputURL, 439*cdf0e10cSrcweir // String _sPrintFileURL) 440*cdf0e10cSrcweir // { 441*cdf0e10cSrcweir // // waitInSeconds(1); 442*cdf0e10cSrcweir // boolean bBack = false; 443*cdf0e10cSrcweir // 444*cdf0e10cSrcweir // XComponent aDoc = loadFromURL(_aGTA, _sInputURL); 445*cdf0e10cSrcweir // if (aDoc != null) 446*cdf0e10cSrcweir // { 447*cdf0e10cSrcweir // if ( _sInputURL.equals(_sOutputURL) ) 448*cdf0e10cSrcweir // { 449*cdf0e10cSrcweir // // don't store document 450*cdf0e10cSrcweir // // input and output are equal OR 451*cdf0e10cSrcweir // GlobalLogWriter.println("Warning: Inputpath and Outputpath are equal. Document will not stored again."); 452*cdf0e10cSrcweir // disallowStore(); 453*cdf0e10cSrcweir // } 454*cdf0e10cSrcweir // bBack = impl_printToFileWithOOo(_aGTA, aDoc, _sOutputURL, _sPrintFileURL); 455*cdf0e10cSrcweir // 456*cdf0e10cSrcweir // GlobalLogWriter.println("Close document."); 457*cdf0e10cSrcweir // aDoc.dispose(); 458*cdf0e10cSrcweir // } 459*cdf0e10cSrcweir // else 460*cdf0e10cSrcweir // { 461*cdf0e10cSrcweir // GlobalLogWriter.println("loadDocumentFromURL() failed with document: " + _sInputURL); 462*cdf0e10cSrcweir // } 463*cdf0e10cSrcweir // return bBack; 464*cdf0e10cSrcweir // } 465*cdf0e10cSrcweir 466*cdf0e10cSrcweir 467*cdf0e10cSrcweir 468*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 469*cdf0e10cSrcweir private boolean impl_printToFileWithOOo(ParameterHelper _aGTA, 470*cdf0e10cSrcweir XComponent _aDoc, 471*cdf0e10cSrcweir String _sOutputURL, 472*cdf0e10cSrcweir String _sPrintFileURL) 473*cdf0e10cSrcweir { 474*cdf0e10cSrcweir boolean bBack = false; 475*cdf0e10cSrcweir boolean bFailed = true; // always be a pessimist, 476*cdf0e10cSrcweir if (_aDoc == null) 477*cdf0e10cSrcweir { 478*cdf0e10cSrcweir GlobalLogWriter.println("No document is given."); 479*cdf0e10cSrcweir return bBack; 480*cdf0e10cSrcweir } 481*cdf0e10cSrcweir 482*cdf0e10cSrcweir try 483*cdf0e10cSrcweir { 484*cdf0e10cSrcweir if (_sOutputURL != null) 485*cdf0e10cSrcweir { 486*cdf0e10cSrcweir if (isStoreAllowed()) 487*cdf0e10cSrcweir { 488*cdf0e10cSrcweir // store the document in an other directory 489*cdf0e10cSrcweir XStorable aStorable = UnoRuntime.queryInterface( XStorable.class, _aDoc); 490*cdf0e10cSrcweir if (aStorable != null) 491*cdf0e10cSrcweir { 492*cdf0e10cSrcweir PropertyValue [] szEmptyArgs = new PropertyValue [0]; 493*cdf0e10cSrcweir 494*cdf0e10cSrcweir GlobalLogWriter.println(DateHelper.getDateTimeForHumanreadableLog() + " Store document."); 495*cdf0e10cSrcweir _aGTA.getPerformance().startTime(PerformanceContainer.Store); 496*cdf0e10cSrcweir aStorable.storeAsURL(_sOutputURL, szEmptyArgs); 497*cdf0e10cSrcweir _aGTA.getPerformance().stopTime(PerformanceContainer.Store); 498*cdf0e10cSrcweir 499*cdf0e10cSrcweir GlobalLogWriter.println(DateHelper.getDateTimeForHumanreadableLog() + " Store document done."); 500*cdf0e10cSrcweir // TimeHelper.waitInSeconds(1, "After store as URL to:" + _sOutputURL); 501*cdf0e10cSrcweir GlobalLogWriter.println("Reload stored file test."); 502*cdf0e10cSrcweir XComponent aDoc = loadFromURL(_aGTA, _sOutputURL); 503*cdf0e10cSrcweir if (aDoc == null) 504*cdf0e10cSrcweir { 505*cdf0e10cSrcweir GlobalLogWriter.println("Reload stored file test failed, can't reload file: " + _sOutputURL); 506*cdf0e10cSrcweir } 507*cdf0e10cSrcweir else 508*cdf0e10cSrcweir { 509*cdf0e10cSrcweir XCloseable xClose = UnoRuntime.queryInterface(XCloseable.class, aDoc); 510*cdf0e10cSrcweir if (xClose != null) 511*cdf0e10cSrcweir { 512*cdf0e10cSrcweir xClose.close(true); 513*cdf0e10cSrcweir } 514*cdf0e10cSrcweir else 515*cdf0e10cSrcweir { 516*cdf0e10cSrcweir aDoc.dispose(); 517*cdf0e10cSrcweir } 518*cdf0e10cSrcweir // TimeHelper.waitInSeconds(1, "after close temp document"); 519*cdf0e10cSrcweir } 520*cdf0e10cSrcweir } 521*cdf0e10cSrcweir } 522*cdf0e10cSrcweir else 523*cdf0e10cSrcweir { 524*cdf0e10cSrcweir // make sure to create the directory in 525*cdf0e10cSrcweir String sOutputFilename = FileHelper.getSystemPathFromFileURL(_sOutputURL); 526*cdf0e10cSrcweir String sOutputPath = FileHelper.getPath(sOutputFilename); 527*cdf0e10cSrcweir File aFile = new File(sOutputPath); 528*cdf0e10cSrcweir aFile.mkdirs(); 529*cdf0e10cSrcweir } 530*cdf0e10cSrcweir } 531*cdf0e10cSrcweir } 532*cdf0e10cSrcweir catch ( com.sun.star.uno.Exception e ) 533*cdf0e10cSrcweir { 534*cdf0e10cSrcweir // Some exception occures.FAILED 535*cdf0e10cSrcweir GlobalLogWriter.println("UNO Exception caught."); 536*cdf0e10cSrcweir GlobalLogWriter.println("Message: " + e.getMessage()); 537*cdf0e10cSrcweir 538*cdf0e10cSrcweir e.printStackTrace(); 539*cdf0e10cSrcweir bBack = false; 540*cdf0e10cSrcweir } 541*cdf0e10cSrcweir 542*cdf0e10cSrcweir try 543*cdf0e10cSrcweir { 544*cdf0e10cSrcweir 545*cdf0e10cSrcweir // System.out.println("Document loaded."); 546*cdf0e10cSrcweir // Change Pagesettings to DIN A4 547*cdf0e10cSrcweir 548*cdf0e10cSrcweir GlobalLogWriter.println(DateHelper.getDateTimeForHumanreadableLog() + " Print document."); 549*cdf0e10cSrcweir XPrintable aPrintable = UnoRuntime.queryInterface( XPrintable.class, _aDoc); 550*cdf0e10cSrcweir if (aPrintable != null) 551*cdf0e10cSrcweir { 552*cdf0e10cSrcweir // System.out.println(" Set PaperFormat to DIN A4"); 553*cdf0e10cSrcweir // { 554*cdf0e10cSrcweir // PropertyValue[] aPrinterProps = aPrintable.getPrinter(); 555*cdf0e10cSrcweir // System.out.println("PrinterProps size: " + String.valueOf(aPrinterProps.length)); 556*cdf0e10cSrcweir // int nPropIndex = 0; 557*cdf0e10cSrcweir // while (!"PaperFormat".equals(aPrinterProps[nPropIndex].Name)) 558*cdf0e10cSrcweir // { 559*cdf0e10cSrcweir // // System.out.println(aPrinterProps[nPropIndex].Name); 560*cdf0e10cSrcweir // nPropIndex++; 561*cdf0e10cSrcweir // } 562*cdf0e10cSrcweir // aPrinterProps[nPropIndex].Value = com.sun.star.view.PaperFormat.A4; 563*cdf0e10cSrcweir // aPrintable.setPrinter(aPrinterProps); 564*cdf0e10cSrcweir // } 565*cdf0e10cSrcweir 566*cdf0e10cSrcweir // configure Office to allow to execute macos 567*cdf0e10cSrcweir 568*cdf0e10cSrcweir // TODO: We need a possiblity to set the printer name also for StarOffice/OpenOffice 569*cdf0e10cSrcweir if (OSHelper.isWindows()) 570*cdf0e10cSrcweir { 571*cdf0e10cSrcweir if (_aGTA.getPrinterName() != null) 572*cdf0e10cSrcweir { 573*cdf0e10cSrcweir ArrayList<PropertyValue> aPropertyList = new ArrayList<PropertyValue>(); 574*cdf0e10cSrcweir // PropertyValue [] aPrintProps = new PropertyValue[1]; 575*cdf0e10cSrcweir PropertyValue Arg = new PropertyValue(); 576*cdf0e10cSrcweir Arg.Name = "Name"; 577*cdf0e10cSrcweir Arg.Value = _aGTA.getPrinterName(); 578*cdf0e10cSrcweir aPropertyList.add(Arg); 579*cdf0e10cSrcweir showProperty(Arg); 580*cdf0e10cSrcweir // GlobalLogWriter.println("Printername is not null, so set to " + _aGTA.getPrinterName()); 581*cdf0e10cSrcweir aPrintable.setPrinter(PropertyHelper.createPropertyValueArrayFormArrayList(aPropertyList)); 582*cdf0e10cSrcweir } 583*cdf0e10cSrcweir } 584*cdf0e10cSrcweir 585*cdf0e10cSrcweir // set property values for XPrintable.print() 586*cdf0e10cSrcweir // more can be found at "http://api.openoffice.org/docs/common/ref/com/sun/star/view/PrintOptions.html" 587*cdf0e10cSrcweir 588*cdf0e10cSrcweir // int nProperties = 1; // default for 'FileName' property 589*cdf0e10cSrcweir // if (_aGTA.printAllPages() == false) 590*cdf0e10cSrcweir // { 591*cdf0e10cSrcweir // // we don't want to print all pages, build Pages string by ourself 592*cdf0e10cSrcweir // nProperties ++; 593*cdf0e10cSrcweir // } 594*cdf0e10cSrcweir // int nPropsCount = 0; 595*cdf0e10cSrcweir 596*cdf0e10cSrcweir // If we are a SpreadSheet (calc), we need to set PrintAllSheets property to 'true' 597*cdf0e10cSrcweir XServiceInfo xServiceInfo = UnoRuntime.queryInterface( XServiceInfo.class, _aDoc ); 598*cdf0e10cSrcweir if ( xServiceInfo.supportsService( "com.sun.star.sheet.SpreadsheetDocument" ) ) 599*cdf0e10cSrcweir { 600*cdf0e10cSrcweir XMultiServiceFactory xMSF = _aGTA.getMultiServiceFactory(); 601*cdf0e10cSrcweir Object aSettings = xMSF.createInstance( "com.sun.star.sheet.GlobalSheetSettings" ); 602*cdf0e10cSrcweir if (aSettings != null) 603*cdf0e10cSrcweir { 604*cdf0e10cSrcweir XPropertySet xPropSet = UnoRuntime.queryInterface( XPropertySet.class, aSettings ); 605*cdf0e10cSrcweir xPropSet.setPropertyValue( "PrintAllSheets", new Boolean( true ) ); 606*cdf0e10cSrcweir GlobalLogWriter.println("PrintAllSheets := true"); 607*cdf0e10cSrcweir } 608*cdf0e10cSrcweir } 609*cdf0e10cSrcweir 610*cdf0e10cSrcweir ArrayList<PropertyValue> aPrintProps = new ArrayList<PropertyValue>(); 611*cdf0e10cSrcweir // GlobalLogWriter.println("Property FileName:=" + _sPrintFileURL); 612*cdf0e10cSrcweir 613*cdf0e10cSrcweir // PropertyValue [] aPrintProps = new PropertyValue[nProperties]; 614*cdf0e10cSrcweir PropertyValue Arg = new PropertyValue(); 615*cdf0e10cSrcweir Arg.Name = "FileName"; 616*cdf0e10cSrcweir Arg.Value = _sPrintFileURL; 617*cdf0e10cSrcweir // aPrintProps[nPropsCount ++] = Arg; 618*cdf0e10cSrcweir aPrintProps.add(Arg); 619*cdf0e10cSrcweir showProperty(Arg); 620*cdf0e10cSrcweir 621*cdf0e10cSrcweir 622*cdf0e10cSrcweir // generate pages string 623*cdf0e10cSrcweir if (_aGTA.printAllPages() == false) 624*cdf0e10cSrcweir { 625*cdf0e10cSrcweir String sPages = ""; 626*cdf0e10cSrcweir if (_aGTA.getMaxPages() > 0) 627*cdf0e10cSrcweir { 628*cdf0e10cSrcweir sPages = "1-" + String.valueOf(_aGTA.getMaxPages()); 629*cdf0e10cSrcweir } 630*cdf0e10cSrcweir if (_aGTA.getOnlyPages().length() != 0) 631*cdf0e10cSrcweir { 632*cdf0e10cSrcweir if (sPages.length() != 0) 633*cdf0e10cSrcweir { 634*cdf0e10cSrcweir sPages += ";"; 635*cdf0e10cSrcweir } 636*cdf0e10cSrcweir sPages += String.valueOf(_aGTA.getOnlyPages()); 637*cdf0e10cSrcweir } 638*cdf0e10cSrcweir 639*cdf0e10cSrcweir Arg = new PropertyValue(); 640*cdf0e10cSrcweir Arg.Name = "Pages"; 641*cdf0e10cSrcweir Arg.Value = sPages; 642*cdf0e10cSrcweir aPrintProps.add(Arg); 643*cdf0e10cSrcweir showProperty(Arg); 644*cdf0e10cSrcweir } 645*cdf0e10cSrcweir 646*cdf0e10cSrcweir // GlobalLogWriter.println("Start printing."); 647*cdf0e10cSrcweir 648*cdf0e10cSrcweir _aGTA.getPerformance().startTime(PerformanceContainer.Print); 649*cdf0e10cSrcweir aPrintable.print(PropertyHelper.createPropertyValueArrayFormArrayList(aPrintProps)); 650*cdf0e10cSrcweir TimeHelper.waitInSeconds(1, "Start waiting for print ready."); 651*cdf0e10cSrcweir 652*cdf0e10cSrcweir GlobalLogWriter.println("Wait until document is printed."); 653*cdf0e10cSrcweir boolean isBusy = true; 654*cdf0e10cSrcweir int nPrintCount = 0; 655*cdf0e10cSrcweir while (isBusy) 656*cdf0e10cSrcweir { 657*cdf0e10cSrcweir PropertyValue[] aPrinterProps = aPrintable.getPrinter(); 658*cdf0e10cSrcweir int nPropIndex = 0; 659*cdf0e10cSrcweir while (!"IsBusy".equals(aPrinterProps[nPropIndex].Name)) 660*cdf0e10cSrcweir { 661*cdf0e10cSrcweir // System.out.println(aPrinterProps[nPropIndex].Name); 662*cdf0e10cSrcweir nPropIndex++; 663*cdf0e10cSrcweir } 664*cdf0e10cSrcweir isBusy = (aPrinterProps[nPropIndex].Value == Boolean.TRUE) ? true : false; 665*cdf0e10cSrcweir TimeHelper.waitInSeconds(1, "is print ready?"); 666*cdf0e10cSrcweir nPrintCount++; 667*cdf0e10cSrcweir if (nPrintCount > 3600) 668*cdf0e10cSrcweir { 669*cdf0e10cSrcweir // we will never wait >1h until print is ready! 670*cdf0e10cSrcweir GlobalLogWriter.println("ERROR: Cancel print due to too long wait."); 671*cdf0e10cSrcweir throw new com.sun.star.uno.Exception("Convwatch exception, wait too long for printing."); 672*cdf0e10cSrcweir } 673*cdf0e10cSrcweir } 674*cdf0e10cSrcweir // TODO: 675*cdf0e10cSrcweir // TimeHelper.waitInSeconds(40, "Start waiting after print ready."); 676*cdf0e10cSrcweir 677*cdf0e10cSrcweir _aGTA.getPerformance().stopTime(PerformanceContainer.Print); 678*cdf0e10cSrcweir GlobalLogWriter.println(DateHelper.getDateTimeForHumanreadableLog() + " Print document done."); 679*cdf0e10cSrcweir 680*cdf0e10cSrcweir // Create a .info file near the printed '.ps' or '.prn' file. 681*cdf0e10cSrcweir FileHelper.createInfoFile(_sPrintFileURL, _aGTA); 682*cdf0e10cSrcweir } 683*cdf0e10cSrcweir else 684*cdf0e10cSrcweir { 685*cdf0e10cSrcweir GlobalLogWriter.println("Can't get XPrintable interface."); 686*cdf0e10cSrcweir } 687*cdf0e10cSrcweir bFailed = false; 688*cdf0e10cSrcweir bBack = true; 689*cdf0e10cSrcweir } 690*cdf0e10cSrcweir catch ( com.sun.star.uno.Exception e ) 691*cdf0e10cSrcweir { 692*cdf0e10cSrcweir // Some exception occures.FAILED 693*cdf0e10cSrcweir GlobalLogWriter.println("UNO Exception caught."); 694*cdf0e10cSrcweir GlobalLogWriter.println("Message: " + e.getMessage()); 695*cdf0e10cSrcweir 696*cdf0e10cSrcweir e.printStackTrace(); 697*cdf0e10cSrcweir bBack = false; 698*cdf0e10cSrcweir } 699*cdf0e10cSrcweir 700*cdf0e10cSrcweir if (bFailed == true) 701*cdf0e10cSrcweir { 702*cdf0e10cSrcweir GlobalLogWriter.println("convwatch.OfficePrint: FAILED"); 703*cdf0e10cSrcweir } 704*cdf0e10cSrcweir else 705*cdf0e10cSrcweir { 706*cdf0e10cSrcweir GlobalLogWriter.println("convwatch.OfficePrint: OK"); 707*cdf0e10cSrcweir } 708*cdf0e10cSrcweir return bBack; 709*cdf0e10cSrcweir } 710*cdf0e10cSrcweir 711*cdf0e10cSrcweir 712*cdf0e10cSrcweir /** 713*cdf0e10cSrcweir * @param _aGTA 714*cdf0e10cSrcweir * @param _sAbsoluteOutputPath 715*cdf0e10cSrcweir * @param _sAbsoluteInputFile 716*cdf0e10cSrcweir * @return true, if the reference (*.prrn file) based on given output path and given input path exist. 717*cdf0e10cSrcweir * If OVERWRITE_REFERENCE is set, always return false. 718*cdf0e10cSrcweir */ 719*cdf0e10cSrcweir public boolean isReferenceExists(ParameterHelper _aGTA, 720*cdf0e10cSrcweir String _sAbsoluteOutputPath, 721*cdf0e10cSrcweir String _sAbsoluteInputFile) 722*cdf0e10cSrcweir { 723*cdf0e10cSrcweir if (! FileHelper.exists(_sAbsoluteInputFile)) 724*cdf0e10cSrcweir { 725*cdf0e10cSrcweir // throw new ConvWatchCancelException("Input file: " + _sAbsoluteInputFile + " does not exist."); 726*cdf0e10cSrcweir return false; 727*cdf0e10cSrcweir } 728*cdf0e10cSrcweir 729*cdf0e10cSrcweir // String fs = System.getProperty("file.separator"); 730*cdf0e10cSrcweir 731*cdf0e10cSrcweir // String sInputFileURL = URLHelper.getFileURLFromSystemPath(_sAbsoluteInputFile); 732*cdf0e10cSrcweir 733*cdf0e10cSrcweir String sInputFileBasename = FileHelper.getBasename(_sAbsoluteInputFile); 734*cdf0e10cSrcweir // String sOutputFileURL = null; 735*cdf0e10cSrcweir String sOutputPath; 736*cdf0e10cSrcweir if (_sAbsoluteOutputPath != null) 737*cdf0e10cSrcweir { 738*cdf0e10cSrcweir sOutputPath = _sAbsoluteOutputPath; 739*cdf0e10cSrcweir // FileHelper.makeDirectories("", sOutputPath); 740*cdf0e10cSrcweir } 741*cdf0e10cSrcweir else 742*cdf0e10cSrcweir { 743*cdf0e10cSrcweir String sInputPath = FileHelper.getPath(_sAbsoluteInputFile); 744*cdf0e10cSrcweir sOutputPath = sInputPath; 745*cdf0e10cSrcweir } 746*cdf0e10cSrcweir // sOutputFileURL = URLHelper.getFileURLFromSystemPath(sOutputPath + fs + sInputFileBasename); 747*cdf0e10cSrcweir // sOutputFileURL = null; 748*cdf0e10cSrcweir 749*cdf0e10cSrcweir String sPrintFilename = FileHelper.getNameNoSuffix(sInputFileBasename); 750*cdf0e10cSrcweir // String sPrintFileURL; 751*cdf0e10cSrcweir 752*cdf0e10cSrcweir String sAbsolutePrintFilename = FileHelper.appendPath(sOutputPath, sPrintFilename + ".prn"); 753*cdf0e10cSrcweir if (FileHelper.exists(sAbsolutePrintFilename) && _aGTA.getOverwrite() == false) 754*cdf0e10cSrcweir { 755*cdf0e10cSrcweir GlobalLogWriter.println("Reference already exist, don't overwrite. Set " + PropertyName.DOC_COMPARATOR_OVERWRITE_REFERENCE + "=true to force overwrite."); 756*cdf0e10cSrcweir return true; 757*cdf0e10cSrcweir } 758*cdf0e10cSrcweir return false; 759*cdf0e10cSrcweir } 760*cdf0e10cSrcweir 761*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 762*cdf0e10cSrcweir /** 763*cdf0e10cSrcweir * create a reference file 764*cdf0e10cSrcweir * _sAbsoluteInputPath contains the source file, if not exists, return with failure. 765*cdf0e10cSrcweir * _sAbsoluteOutputPath contains the destination, where the file will store after load with StarOffice/OpenOffice.org 766*cdf0e10cSrcweir * if is null, print only near the Input file path 767*cdf0e10cSrcweir * _sPrintType ".prn" Print input file with StarOffice/OpenOffice.org and the default printer as PostScript 768*cdf0e10cSrcweir * 769*cdf0e10cSrcweir * @param _aGTA 770*cdf0e10cSrcweir * @return 771*cdf0e10cSrcweir */ 772*cdf0e10cSrcweir // public static boolean buildReference(ParameterHelper _aGTA, 773*cdf0e10cSrcweir // String _sAbsoluteOutputPath, 774*cdf0e10cSrcweir // String _sAbsoluteInputFile) 775*cdf0e10cSrcweir // throws OfficeException 776*cdf0e10cSrcweir // { 777*cdf0e10cSrcweir // if (! FileHelper.exists(_sAbsoluteInputFile)) 778*cdf0e10cSrcweir // { 779*cdf0e10cSrcweir // throw new OfficeException("buildReference(): Input file: " + _sAbsoluteInputFile + " does not exist."); 780*cdf0e10cSrcweir // } 781*cdf0e10cSrcweir // 782*cdf0e10cSrcweir // String fs = System.getProperty("file.separator"); 783*cdf0e10cSrcweir // 784*cdf0e10cSrcweir // String sInputFileURL = URLHelper.getFileURLFromSystemPath(_sAbsoluteInputFile); 785*cdf0e10cSrcweir // 786*cdf0e10cSrcweir // String sInputFileBasename = FileHelper.getBasename(_sAbsoluteInputFile); 787*cdf0e10cSrcweir // String sOutputFileURL = null; 788*cdf0e10cSrcweir // String sOutputPath; 789*cdf0e10cSrcweir // if (_sAbsoluteOutputPath != null) 790*cdf0e10cSrcweir // { 791*cdf0e10cSrcweir // sOutputPath = _sAbsoluteOutputPath; 792*cdf0e10cSrcweir // FileHelper.makeDirectories("", sOutputPath); 793*cdf0e10cSrcweir // } 794*cdf0e10cSrcweir // else 795*cdf0e10cSrcweir // { 796*cdf0e10cSrcweir // String sInputPath = FileHelper.getPath(_sAbsoluteInputFile); 797*cdf0e10cSrcweir // sOutputPath = sInputPath; 798*cdf0e10cSrcweir // } 799*cdf0e10cSrcweir // // sOutputFileURL = URLHelper.getFileURLFromSystemPath(sOutputPath + fs + sInputFileBasename); 800*cdf0e10cSrcweir // sOutputFileURL = null; 801*cdf0e10cSrcweir // 802*cdf0e10cSrcweir // String sPrintFilename = FileHelper.getNameNoSuffix(sInputFileBasename); 803*cdf0e10cSrcweir // String sPrintFileURL; 804*cdf0e10cSrcweir // 805*cdf0e10cSrcweir // String sAbsolutePrintFilename = sOutputPath + fs + sPrintFilename + ".prn"; 806*cdf0e10cSrcweir // if (FileHelper.exists(sAbsolutePrintFilename) && _aGTA.getOverwrite() == false) 807*cdf0e10cSrcweir // { 808*cdf0e10cSrcweir // GlobalLogWriter.println("Reference already exist, don't overwrite. Set " + PropertyName.DOC_COMPARATOR_OVERWRITE_REFERENCE + "=true to force overwrite."); 809*cdf0e10cSrcweir // return true; 810*cdf0e10cSrcweir // } 811*cdf0e10cSrcweir // 812*cdf0e10cSrcweir // if (_aGTA.getReferenceType().toLowerCase().equals("msoffice")) 813*cdf0e10cSrcweir // { 814*cdf0e10cSrcweir // sPrintFileURL = URLHelper.getFileURLFromSystemPath(sAbsolutePrintFilename); 815*cdf0e10cSrcweir // } 816*cdf0e10cSrcweir // else if (_aGTA.getReferenceType().toLowerCase().equals("pdf")) 817*cdf0e10cSrcweir // { 818*cdf0e10cSrcweir //// TODO: If we rename the stored file to *.pdf, we have to be sure that we use *.pdf also as a available reference 819*cdf0e10cSrcweir // sPrintFileURL = URLHelper.getFileURLFromSystemPath(sAbsolutePrintFilename ); 820*cdf0e10cSrcweir // } 821*cdf0e10cSrcweir // else if (_aGTA.getReferenceType().toLowerCase().equals("ooo")) 822*cdf0e10cSrcweir // { 823*cdf0e10cSrcweir // sPrintFileURL = URLHelper.getFileURLFromSystemPath(sAbsolutePrintFilename ); 824*cdf0e10cSrcweir // } 825*cdf0e10cSrcweir // else 826*cdf0e10cSrcweir // { 827*cdf0e10cSrcweir // GlobalLogWriter.println("OfficePrint.buildreference(): Unknown print type."); 828*cdf0e10cSrcweir // return false; 829*cdf0e10cSrcweir // } 830*cdf0e10cSrcweir // return printToFile(_aGTA, sInputFileURL, sOutputFileURL, sPrintFileURL); 831*cdf0e10cSrcweir // } 832*cdf0e10cSrcweir 833*cdf0e10cSrcweir 834*cdf0e10cSrcweir 835*cdf0e10cSrcweir // TODO: Das Teil muss hier raus! 836*cdf0e10cSrcweir 837*cdf0e10cSrcweir 838*cdf0e10cSrcweir // public static boolean printToFile(ParameterHelper _aGTA, 839*cdf0e10cSrcweir // String _sInputFileURL, 840*cdf0e10cSrcweir // String _sOutputFileURL, 841*cdf0e10cSrcweir // String _sPrintFileURL) throws OfficeException 842*cdf0e10cSrcweir // { 843*cdf0e10cSrcweir // boolean bBack = false; 844*cdf0e10cSrcweir // String sPrintFileURL = null; 845*cdf0e10cSrcweir // 846*cdf0e10cSrcweir // 847*cdf0e10cSrcweir // // remember the current timer, to know how long a print process need. 848*cdf0e10cSrcweir // // startTimer(); 849*cdf0e10cSrcweir // 850*cdf0e10cSrcweir // if (_aGTA.getReferenceType().toLowerCase().equals("ooo")) 851*cdf0e10cSrcweir // { 852*cdf0e10cSrcweir // bBack = printToFileWithOOo(_aGTA, _sInputFileURL, _sOutputFileURL, _sPrintFileURL); 853*cdf0e10cSrcweir // } 854*cdf0e10cSrcweir // else if (_aGTA.getReferenceType().toLowerCase().equals("pdf")) 855*cdf0e10cSrcweir // { 856*cdf0e10cSrcweir // GlobalLogWriter.println("USE PDF AS EXPORT FORMAT."); 857*cdf0e10cSrcweir // bBack = storeAsPDF(_aGTA, _sInputFileURL, _sPrintFileURL); 858*cdf0e10cSrcweir // } 859*cdf0e10cSrcweir // else if (_aGTA.getReferenceType().toLowerCase().equals("msoffice")) 860*cdf0e10cSrcweir // { 861*cdf0e10cSrcweir // if (MSOfficePostscriptCreator.isMSOfficeDocumentFormat(_sInputFileURL)) 862*cdf0e10cSrcweir // { 863*cdf0e10cSrcweir // GlobalLogWriter.println("USE MSOFFICE AS EXPORT FORMAT."); 864*cdf0e10cSrcweir // MSOfficePostscriptCreator a = new MSOfficePostscriptCreator(); 865*cdf0e10cSrcweir // try 866*cdf0e10cSrcweir // { 867*cdf0e10cSrcweir // a.printToFileWithMSOffice(_aGTA, FileHelper.getSystemPathFromFileURL(_sInputFileURL), 868*cdf0e10cSrcweir // FileHelper.getSystemPathFromFileURL(_sPrintFileURL)); 869*cdf0e10cSrcweir // } 870*cdf0e10cSrcweir // catch(OfficeException e) 871*cdf0e10cSrcweir // { 872*cdf0e10cSrcweir // e.printStackTrace(); 873*cdf0e10cSrcweir // GlobalLogWriter.println(e.getMessage()); 874*cdf0e10cSrcweir // throw new OfficeException("Exception caught. Problem with MSOffice printer methods."); 875*cdf0e10cSrcweir // } 876*cdf0e10cSrcweir // catch(java.io.IOException e) 877*cdf0e10cSrcweir // { 878*cdf0e10cSrcweir // GlobalLogWriter.println(e.getMessage()); 879*cdf0e10cSrcweir // throw new OfficeException("IOException caught. Problem with MSOffice printer methods."); 880*cdf0e10cSrcweir // } 881*cdf0e10cSrcweir // bBack = true; 882*cdf0e10cSrcweir // } 883*cdf0e10cSrcweir // else 884*cdf0e10cSrcweir // { 885*cdf0e10cSrcweir // GlobalLogWriter.println("This document type is not recognized as MSOffice format, as default fallback StarOffice/OpenOffice.org instead is used."); 886*cdf0e10cSrcweir // bBack = printToFileWithOOo(_aGTA, _sInputFileURL, _sOutputFileURL, _sPrintFileURL); 887*cdf0e10cSrcweir // } 888*cdf0e10cSrcweir // } 889*cdf0e10cSrcweir // else 890*cdf0e10cSrcweir // { 891*cdf0e10cSrcweir // // System.out.println(""); 892*cdf0e10cSrcweir // throw new OfficeException("OfficePrint.printToFile(): Unknown print type."); 893*cdf0e10cSrcweir // } 894*cdf0e10cSrcweir // return bBack; 895*cdf0e10cSrcweir // } 896*cdf0e10cSrcweir 897*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 898*cdf0e10cSrcweir // TODO: move this away! 899*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 900*cdf0e10cSrcweir void showType(String _sInputURL, XMultiServiceFactory _xMSF) 901*cdf0e10cSrcweir { 902*cdf0e10cSrcweir if (_sInputURL.length() == 0) 903*cdf0e10cSrcweir { 904*cdf0e10cSrcweir return; 905*cdf0e10cSrcweir } 906*cdf0e10cSrcweir 907*cdf0e10cSrcweir if (_xMSF == null) 908*cdf0e10cSrcweir { 909*cdf0e10cSrcweir GlobalLogWriter.println("MultiServiceFactory not set."); 910*cdf0e10cSrcweir return; 911*cdf0e10cSrcweir } 912*cdf0e10cSrcweir XTypeDetection aTypeDetection = null; 913*cdf0e10cSrcweir try 914*cdf0e10cSrcweir { 915*cdf0e10cSrcweir Object oObj = _xMSF.createInstance("com.sun.star.document.TypeDetection"); 916*cdf0e10cSrcweir aTypeDetection = UnoRuntime.queryInterface(XTypeDetection.class, oObj); 917*cdf0e10cSrcweir } 918*cdf0e10cSrcweir catch(com.sun.star.uno.Exception e) 919*cdf0e10cSrcweir { 920*cdf0e10cSrcweir GlobalLogWriter.println("Can't get com.sun.star.document.TypeDetection."); 921*cdf0e10cSrcweir return; 922*cdf0e10cSrcweir } 923*cdf0e10cSrcweir if (aTypeDetection != null) 924*cdf0e10cSrcweir { 925*cdf0e10cSrcweir String sType = aTypeDetection.queryTypeByURL(_sInputURL); 926*cdf0e10cSrcweir GlobalLogWriter.println("Type is: " + sType); 927*cdf0e10cSrcweir } 928*cdf0e10cSrcweir } 929*cdf0e10cSrcweir 930*cdf0e10cSrcweir 931*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 932*cdf0e10cSrcweir public String getInternalFilterName(String _sFilterName, XMultiServiceFactory _xMSF) 933*cdf0e10cSrcweir { 934*cdf0e10cSrcweir if (_sFilterName.length() == 0) 935*cdf0e10cSrcweir { 936*cdf0e10cSrcweir // System.out.println("No FilterName set."); 937*cdf0e10cSrcweir return null; 938*cdf0e10cSrcweir } 939*cdf0e10cSrcweir 940*cdf0e10cSrcweir if (_xMSF == null) 941*cdf0e10cSrcweir { 942*cdf0e10cSrcweir GlobalLogWriter.println("MultiServiceFactory not set."); 943*cdf0e10cSrcweir return null; 944*cdf0e10cSrcweir } 945*cdf0e10cSrcweir // XFilterFactory aFilterFactory = null; 946*cdf0e10cSrcweir Object aObj = null; 947*cdf0e10cSrcweir try 948*cdf0e10cSrcweir { 949*cdf0e10cSrcweir aObj = _xMSF.createInstance("com.sun.star.document.FilterFactory"); 950*cdf0e10cSrcweir } 951*cdf0e10cSrcweir catch(com.sun.star.uno.Exception e) 952*cdf0e10cSrcweir { 953*cdf0e10cSrcweir GlobalLogWriter.println("Can't get com.sun.star.document.FilterFactory."); 954*cdf0e10cSrcweir return null; 955*cdf0e10cSrcweir } 956*cdf0e10cSrcweir if (aObj != null) 957*cdf0e10cSrcweir { 958*cdf0e10cSrcweir XNameAccess aNameAccess = UnoRuntime.queryInterface(XNameAccess.class, aObj); 959*cdf0e10cSrcweir if (aNameAccess != null) 960*cdf0e10cSrcweir { 961*cdf0e10cSrcweir 962*cdf0e10cSrcweir // if (_sFilterName.toLowerCase().equals("help")) 963*cdf0e10cSrcweir // { 964*cdf0e10cSrcweir // System.out.println("Show all possible ElementNames from current version." ); 965*cdf0e10cSrcweir // String[] aElementNames = aNameAccess.getElementNames(); 966*cdf0e10cSrcweir // for (int i = 0; i<aElementNames.length; i++) 967*cdf0e10cSrcweir // { 968*cdf0e10cSrcweir // System.out.println(aElementNames[i]); 969*cdf0e10cSrcweir // } 970*cdf0e10cSrcweir // System.out.println("Must quit."); 971*cdf0e10cSrcweir // System.out.exit(1); 972*cdf0e10cSrcweir // } 973*cdf0e10cSrcweir 974*cdf0e10cSrcweir if (! aNameAccess.hasByName(_sFilterName)) 975*cdf0e10cSrcweir { 976*cdf0e10cSrcweir GlobalLogWriter.println("FilterFactory.hasByName() says there exist no '" + _sFilterName + "'" ); 977*cdf0e10cSrcweir return null; 978*cdf0e10cSrcweir } 979*cdf0e10cSrcweir 980*cdf0e10cSrcweir Object[] aElements = null; 981*cdf0e10cSrcweir String[] aExtensions; 982*cdf0e10cSrcweir try 983*cdf0e10cSrcweir { 984*cdf0e10cSrcweir aElements = (Object[]) aNameAccess.getByName(_sFilterName); 985*cdf0e10cSrcweir if (aElements != null) 986*cdf0e10cSrcweir { 987*cdf0e10cSrcweir String sInternalFilterName = null; 988*cdf0e10cSrcweir // System.out.println("getByName().length: " + String.valueOf(aElements.length)); 989*cdf0e10cSrcweir for (int i=0;i<aElements.length; i++) 990*cdf0e10cSrcweir { 991*cdf0e10cSrcweir PropertyValue aPropertyValue = (PropertyValue)aElements[i]; 992*cdf0e10cSrcweir // System.out.println("PropertyValue.Name: " + aPropertyValue.Name); 993*cdf0e10cSrcweir if (aPropertyValue.Name.equals("Type")) 994*cdf0e10cSrcweir { 995*cdf0e10cSrcweir String sValue = (String)aPropertyValue.Value; 996*cdf0e10cSrcweir // System.out.println("Type: " + sValue); 997*cdf0e10cSrcweir sInternalFilterName = sValue; 998*cdf0e10cSrcweir } 999*cdf0e10cSrcweir } 1000*cdf0e10cSrcweir return sInternalFilterName; 1001*cdf0e10cSrcweir } 1002*cdf0e10cSrcweir else 1003*cdf0e10cSrcweir { 1004*cdf0e10cSrcweir GlobalLogWriter.println("There are no elements for FilterName '" + _sFilterName + "'"); 1005*cdf0e10cSrcweir return null; 1006*cdf0e10cSrcweir } 1007*cdf0e10cSrcweir } 1008*cdf0e10cSrcweir catch (com.sun.star.container.NoSuchElementException e) 1009*cdf0e10cSrcweir { 1010*cdf0e10cSrcweir GlobalLogWriter.println("NoSuchElementException caught. " + e.getMessage()); 1011*cdf0e10cSrcweir } 1012*cdf0e10cSrcweir catch (com.sun.star.lang.WrappedTargetException e) 1013*cdf0e10cSrcweir { 1014*cdf0e10cSrcweir GlobalLogWriter.println("WrappedTargetException caught. " + e.getMessage()); 1015*cdf0e10cSrcweir } 1016*cdf0e10cSrcweir } 1017*cdf0e10cSrcweir } 1018*cdf0e10cSrcweir return null; 1019*cdf0e10cSrcweir } 1020*cdf0e10cSrcweir 1021*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 1022*cdf0e10cSrcweir 1023*cdf0e10cSrcweir String getServiceNameFromFilterName(String _sFilterName, XMultiServiceFactory _xMSF) 1024*cdf0e10cSrcweir { 1025*cdf0e10cSrcweir if (_sFilterName.length() == 0) 1026*cdf0e10cSrcweir { 1027*cdf0e10cSrcweir // System.out.println("No FilterName set."); 1028*cdf0e10cSrcweir return null; 1029*cdf0e10cSrcweir } 1030*cdf0e10cSrcweir 1031*cdf0e10cSrcweir if (_xMSF == null) 1032*cdf0e10cSrcweir { 1033*cdf0e10cSrcweir GlobalLogWriter.println("MultiServiceFactory not set."); 1034*cdf0e10cSrcweir return null; 1035*cdf0e10cSrcweir } 1036*cdf0e10cSrcweir // XFilterFactory aFilterFactory = null; 1037*cdf0e10cSrcweir Object aObj = null; 1038*cdf0e10cSrcweir try 1039*cdf0e10cSrcweir { 1040*cdf0e10cSrcweir aObj = _xMSF.createInstance("com.sun.star.document.FilterFactory"); 1041*cdf0e10cSrcweir } 1042*cdf0e10cSrcweir catch(com.sun.star.uno.Exception e) 1043*cdf0e10cSrcweir { 1044*cdf0e10cSrcweir GlobalLogWriter.println("Can't get com.sun.star.document.FilterFactory."); 1045*cdf0e10cSrcweir return null; 1046*cdf0e10cSrcweir } 1047*cdf0e10cSrcweir if (aObj != null) 1048*cdf0e10cSrcweir { 1049*cdf0e10cSrcweir XNameAccess aNameAccess = UnoRuntime.queryInterface(XNameAccess.class, aObj); 1050*cdf0e10cSrcweir if (aNameAccess != null) 1051*cdf0e10cSrcweir { 1052*cdf0e10cSrcweir if (! aNameAccess.hasByName(_sFilterName)) 1053*cdf0e10cSrcweir { 1054*cdf0e10cSrcweir GlobalLogWriter.println("FilterFactory.hasByName() says there exist no '" + _sFilterName + "'" ); 1055*cdf0e10cSrcweir return null; 1056*cdf0e10cSrcweir } 1057*cdf0e10cSrcweir 1058*cdf0e10cSrcweir Object[] aElements = null; 1059*cdf0e10cSrcweir String[] aExtensions; 1060*cdf0e10cSrcweir try 1061*cdf0e10cSrcweir { 1062*cdf0e10cSrcweir aElements = (Object[]) aNameAccess.getByName(_sFilterName); 1063*cdf0e10cSrcweir if (aElements != null) 1064*cdf0e10cSrcweir { 1065*cdf0e10cSrcweir String sServiceName = null; 1066*cdf0e10cSrcweir // System.out.println("getByName().length: " + String.valueOf(aElements.length)); 1067*cdf0e10cSrcweir for (int i=0;i<aElements.length; i++) 1068*cdf0e10cSrcweir { 1069*cdf0e10cSrcweir PropertyValue aPropertyValue = (PropertyValue)aElements[i]; 1070*cdf0e10cSrcweir if (aPropertyValue.Name.equals("DocumentService")) 1071*cdf0e10cSrcweir { 1072*cdf0e10cSrcweir String sValue = (String)aPropertyValue.Value; 1073*cdf0e10cSrcweir // System.out.println("DocumentService: " + sValue); 1074*cdf0e10cSrcweir sServiceName = sValue; 1075*cdf0e10cSrcweir break; 1076*cdf0e10cSrcweir } 1077*cdf0e10cSrcweir } 1078*cdf0e10cSrcweir return sServiceName; 1079*cdf0e10cSrcweir } 1080*cdf0e10cSrcweir else 1081*cdf0e10cSrcweir { 1082*cdf0e10cSrcweir GlobalLogWriter.println("There are no elements for FilterName '" + _sFilterName + "'"); 1083*cdf0e10cSrcweir return null; 1084*cdf0e10cSrcweir } 1085*cdf0e10cSrcweir } 1086*cdf0e10cSrcweir catch (com.sun.star.container.NoSuchElementException e) 1087*cdf0e10cSrcweir { 1088*cdf0e10cSrcweir GlobalLogWriter.println("NoSuchElementException caught. " + e.getMessage()); 1089*cdf0e10cSrcweir } 1090*cdf0e10cSrcweir catch (com.sun.star.lang.WrappedTargetException e) 1091*cdf0e10cSrcweir { 1092*cdf0e10cSrcweir GlobalLogWriter.println("WrappedTargetException caught. " + e.getMessage()); 1093*cdf0e10cSrcweir } 1094*cdf0e10cSrcweir } 1095*cdf0e10cSrcweir } 1096*cdf0e10cSrcweir return null; 1097*cdf0e10cSrcweir } 1098*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 1099*cdf0e10cSrcweir 1100*cdf0e10cSrcweir public static String getFileExtension(String _sInternalFilterName, XMultiServiceFactory _xMSF) 1101*cdf0e10cSrcweir { 1102*cdf0e10cSrcweir if (_sInternalFilterName.length() == 0) 1103*cdf0e10cSrcweir { 1104*cdf0e10cSrcweir // System.out.println("No FilterName set."); 1105*cdf0e10cSrcweir return null; 1106*cdf0e10cSrcweir } 1107*cdf0e10cSrcweir 1108*cdf0e10cSrcweir if (_xMSF == null) 1109*cdf0e10cSrcweir { 1110*cdf0e10cSrcweir GlobalLogWriter.println("MultiServiceFactory not set."); 1111*cdf0e10cSrcweir return null; 1112*cdf0e10cSrcweir } 1113*cdf0e10cSrcweir XTypeDetection aTypeDetection = null; 1114*cdf0e10cSrcweir try 1115*cdf0e10cSrcweir { 1116*cdf0e10cSrcweir Object oObj = _xMSF.createInstance("com.sun.star.document.TypeDetection"); 1117*cdf0e10cSrcweir aTypeDetection = UnoRuntime.queryInterface(XTypeDetection.class, oObj); 1118*cdf0e10cSrcweir } 1119*cdf0e10cSrcweir catch(com.sun.star.uno.Exception e) 1120*cdf0e10cSrcweir { 1121*cdf0e10cSrcweir GlobalLogWriter.println("Can't get com.sun.star.document.TypeDetection."); 1122*cdf0e10cSrcweir return null; 1123*cdf0e10cSrcweir } 1124*cdf0e10cSrcweir if (aTypeDetection != null) 1125*cdf0e10cSrcweir { 1126*cdf0e10cSrcweir XNameAccess aNameAccess = UnoRuntime.queryInterface(XNameAccess.class, aTypeDetection); 1127*cdf0e10cSrcweir if (aNameAccess != null) 1128*cdf0e10cSrcweir { 1129*cdf0e10cSrcweir 1130*cdf0e10cSrcweir // System.out.println("Show ElementNames" ); 1131*cdf0e10cSrcweir // String[] aElementNames = aNameAccess.getElementNames(); 1132*cdf0e10cSrcweir // for (int i = 0; i<aElementNames.length; i++) 1133*cdf0e10cSrcweir // { 1134*cdf0e10cSrcweir // System.out.println(aElementNames[i]); 1135*cdf0e10cSrcweir // } 1136*cdf0e10cSrcweir 1137*cdf0e10cSrcweir if (! aNameAccess.hasByName(_sInternalFilterName)) 1138*cdf0e10cSrcweir { 1139*cdf0e10cSrcweir GlobalLogWriter.println("TypeDetection.hasByName() says there exist no '" + _sInternalFilterName + "'" ); 1140*cdf0e10cSrcweir return null; 1141*cdf0e10cSrcweir } 1142*cdf0e10cSrcweir 1143*cdf0e10cSrcweir Object[] aElements = null; 1144*cdf0e10cSrcweir String[] aExtensions; 1145*cdf0e10cSrcweir try 1146*cdf0e10cSrcweir { 1147*cdf0e10cSrcweir aElements = (Object[]) aNameAccess.getByName(_sInternalFilterName); 1148*cdf0e10cSrcweir if (aElements != null) 1149*cdf0e10cSrcweir { 1150*cdf0e10cSrcweir String sExtension = null; 1151*cdf0e10cSrcweir // System.out.println("getByName().length: " + String.valueOf(aElements.length)); 1152*cdf0e10cSrcweir for (int i=0;i<aElements.length; i++) 1153*cdf0e10cSrcweir { 1154*cdf0e10cSrcweir PropertyValue aPropertyValue = (PropertyValue)aElements[i]; 1155*cdf0e10cSrcweir // System.out.println("PropertyValue.Name: " + aPropertyValue.Name); 1156*cdf0e10cSrcweir if (aPropertyValue.Name.equals("Extensions")) 1157*cdf0e10cSrcweir { 1158*cdf0e10cSrcweir aExtensions = (String[])aPropertyValue.Value; 1159*cdf0e10cSrcweir GlobalLogWriter.println(" Possible extensions are: " + String.valueOf(aExtensions.length)); 1160*cdf0e10cSrcweir if (aExtensions.length > 0) 1161*cdf0e10cSrcweir { 1162*cdf0e10cSrcweir for (int j=0;j<aExtensions.length;j++) 1163*cdf0e10cSrcweir { 1164*cdf0e10cSrcweir GlobalLogWriter.println(" " + aExtensions[j]); 1165*cdf0e10cSrcweir } 1166*cdf0e10cSrcweir sExtension = aExtensions[0]; 1167*cdf0e10cSrcweir GlobalLogWriter.println(""); 1168*cdf0e10cSrcweir } 1169*cdf0e10cSrcweir } 1170*cdf0e10cSrcweir } 1171*cdf0e10cSrcweir return sExtension; 1172*cdf0e10cSrcweir } 1173*cdf0e10cSrcweir else 1174*cdf0e10cSrcweir { 1175*cdf0e10cSrcweir GlobalLogWriter.println("There are no elements for FilterName '" + _sInternalFilterName + "'"); 1176*cdf0e10cSrcweir return null; 1177*cdf0e10cSrcweir } 1178*cdf0e10cSrcweir } 1179*cdf0e10cSrcweir catch (com.sun.star.container.NoSuchElementException e) 1180*cdf0e10cSrcweir { 1181*cdf0e10cSrcweir GlobalLogWriter.println("NoSuchElementException caught. " + e.getMessage()); 1182*cdf0e10cSrcweir } 1183*cdf0e10cSrcweir catch (com.sun.star.lang.WrappedTargetException e) 1184*cdf0e10cSrcweir { 1185*cdf0e10cSrcweir GlobalLogWriter.println("WrappedTargetException caught. " + e.getMessage()); 1186*cdf0e10cSrcweir } 1187*cdf0e10cSrcweir } 1188*cdf0e10cSrcweir } 1189*cdf0e10cSrcweir return null; 1190*cdf0e10cSrcweir } 1191*cdf0e10cSrcweir 1192*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 1193*cdf0e10cSrcweir public void convertDocument(String _sInputFile, String _sOutputPath, ParameterHelper _aGTA) throws OfficeException 1194*cdf0e10cSrcweir { 1195*cdf0e10cSrcweir XMultiServiceFactory xMSF = _aGTA.getMultiServiceFactory(); 1196*cdf0e10cSrcweir if (xMSF == null) 1197*cdf0e10cSrcweir { 1198*cdf0e10cSrcweir GlobalLogWriter.println("MultiServiceFactory in GraphicalTestArgument not set."); 1199*cdf0e10cSrcweir return; 1200*cdf0e10cSrcweir } 1201*cdf0e10cSrcweir 1202*cdf0e10cSrcweir String sInputURL = URLHelper.getFileURLFromSystemPath(_sInputFile); 1203*cdf0e10cSrcweir // showType(sInputURL, xMSF); 1204*cdf0e10cSrcweir XComponent aDoc = loadFromURL( _aGTA, sInputURL); 1205*cdf0e10cSrcweir if (aDoc == null) 1206*cdf0e10cSrcweir { 1207*cdf0e10cSrcweir GlobalLogWriter.println("Can't load document '"+ sInputURL + "'"); 1208*cdf0e10cSrcweir return; 1209*cdf0e10cSrcweir } 1210*cdf0e10cSrcweir 1211*cdf0e10cSrcweir if (_sOutputPath == null) 1212*cdf0e10cSrcweir { 1213*cdf0e10cSrcweir GlobalLogWriter.println("Outputpath not set."); 1214*cdf0e10cSrcweir return; 1215*cdf0e10cSrcweir } 1216*cdf0e10cSrcweir 1217*cdf0e10cSrcweir if (! isStoreAllowed()) 1218*cdf0e10cSrcweir { 1219*cdf0e10cSrcweir GlobalLogWriter.println("It's not allowed to store, check Input/Output path."); 1220*cdf0e10cSrcweir return; 1221*cdf0e10cSrcweir } 1222*cdf0e10cSrcweir // TODO: Do we need to wait? 1223*cdf0e10cSrcweir // TimeHelper.waitInSeconds(1, "wait after loadFromURL."); 1224*cdf0e10cSrcweir 1225*cdf0e10cSrcweir XServiceInfo xServiceInfo = UnoRuntime.queryInterface( XServiceInfo.class, aDoc ); 1226*cdf0e10cSrcweir // String sFilter = getFilterName_forExcel(xServiceInfo); 1227*cdf0e10cSrcweir // System.out.println("Filter is " + sFilter); 1228*cdf0e10cSrcweir 1229*cdf0e10cSrcweir // store the document in an other directory 1230*cdf0e10cSrcweir XStorable xStorable = UnoRuntime.queryInterface( XStorable.class, aDoc); 1231*cdf0e10cSrcweir if (xStorable == null) 1232*cdf0e10cSrcweir { 1233*cdf0e10cSrcweir GlobalLogWriter.println("com.sun.star.frame.XStorable is null"); 1234*cdf0e10cSrcweir return; 1235*cdf0e10cSrcweir } 1236*cdf0e10cSrcweir 1237*cdf0e10cSrcweir String sFilterName = _aGTA.getExportFilterName(); 1238*cdf0e10cSrcweir 1239*cdf0e10cSrcweir // check how many Properties should initialize 1240*cdf0e10cSrcweir int nPropertyCount = 0; 1241*cdf0e10cSrcweir // if (sFilterName != null && sFilterName.length() > 0) 1242*cdf0e10cSrcweir // { 1243*cdf0e10cSrcweir // nPropertyCount ++; 1244*cdf0e10cSrcweir // } 1245*cdf0e10cSrcweir 1246*cdf0e10cSrcweir // initialize PropertyArray 1247*cdf0e10cSrcweir // PropertyValue [] aStoreProps = new PropertyValue[ nPropertyCount ]; 1248*cdf0e10cSrcweir // int nPropertyIndex = 0; 1249*cdf0e10cSrcweir ArrayList<PropertyValue> aPropertyList = new ArrayList<PropertyValue>(); 1250*cdf0e10cSrcweir 1251*cdf0e10cSrcweir String sExtension = ""; 1252*cdf0e10cSrcweir 1253*cdf0e10cSrcweir if (sFilterName != null && sFilterName.length() > 0) 1254*cdf0e10cSrcweir { 1255*cdf0e10cSrcweir String sInternalFilterName = getInternalFilterName(sFilterName, xMSF); 1256*cdf0e10cSrcweir String sServiceName = getServiceNameFromFilterName(sFilterName, xMSF); 1257*cdf0e10cSrcweir 1258*cdf0e10cSrcweir GlobalLogWriter.println("Filter detection:"); 1259*cdf0e10cSrcweir // check if service name from file filter is the same as from the loaded document 1260*cdf0e10cSrcweir boolean bServiceFailed = false; 1261*cdf0e10cSrcweir if (sServiceName == null || sInternalFilterName == null) 1262*cdf0e10cSrcweir { 1263*cdf0e10cSrcweir GlobalLogWriter.println("Given FilterName '" + sFilterName + "' seems to be unknown."); 1264*cdf0e10cSrcweir bServiceFailed = true; 1265*cdf0e10cSrcweir } 1266*cdf0e10cSrcweir if (! xServiceInfo.supportsService(sServiceName)) 1267*cdf0e10cSrcweir { 1268*cdf0e10cSrcweir GlobalLogWriter.println("Service from FilterName '" + sServiceName + "' is not supported by loaded document."); 1269*cdf0e10cSrcweir bServiceFailed = true; 1270*cdf0e10cSrcweir } 1271*cdf0e10cSrcweir if (bServiceFailed == true) 1272*cdf0e10cSrcweir { 1273*cdf0e10cSrcweir GlobalLogWriter.println("Please check '" + PropertyName.DOC_CONVERTER_EXPORT_FILTER_NAME + "' in the property file."); 1274*cdf0e10cSrcweir return; 1275*cdf0e10cSrcweir } 1276*cdf0e10cSrcweir 1277*cdf0e10cSrcweir if (sInternalFilterName != null && sInternalFilterName.length() > 0) 1278*cdf0e10cSrcweir { 1279*cdf0e10cSrcweir // get the FileExtension, by the filter name, if we don't get a file extension 1280*cdf0e10cSrcweir // we assume the is also no right filter name. 1281*cdf0e10cSrcweir sExtension = getFileExtension(sInternalFilterName, xMSF); 1282*cdf0e10cSrcweir if (sExtension == null) 1283*cdf0e10cSrcweir { 1284*cdf0e10cSrcweir GlobalLogWriter.println("Can't found an extension for filtername, take it from the source."); 1285*cdf0e10cSrcweir } 1286*cdf0e10cSrcweir } 1287*cdf0e10cSrcweir 1288*cdf0e10cSrcweir PropertyValue Arg = new PropertyValue(); 1289*cdf0e10cSrcweir Arg.Name = "FilterName"; 1290*cdf0e10cSrcweir Arg.Value = sFilterName; 1291*cdf0e10cSrcweir // aStoreProps[nPropertyIndex ++] = Arg; 1292*cdf0e10cSrcweir aPropertyList.add(Arg); 1293*cdf0e10cSrcweir showProperty(Arg); 1294*cdf0e10cSrcweir GlobalLogWriter.println("FilterName is set to: " + sFilterName); 1295*cdf0e10cSrcweir } 1296*cdf0e10cSrcweir 1297*cdf0e10cSrcweir String sOutputURL = ""; 1298*cdf0e10cSrcweir try 1299*cdf0e10cSrcweir { 1300*cdf0e10cSrcweir // create the new filename with the extension, which is ok to the file format 1301*cdf0e10cSrcweir String sInputFileBasename = FileHelper.getBasename(_sInputFile); 1302*cdf0e10cSrcweir // System.out.println("InputFileBasename " + sInputFileBasename); 1303*cdf0e10cSrcweir String sInputFileNameNoSuffix = FileHelper.getNameNoSuffix(sInputFileBasename); 1304*cdf0e10cSrcweir // System.out.println("InputFilename no suffix " + sInputFileNameNoSuffix); 1305*cdf0e10cSrcweir String fs = System.getProperty("file.separator"); 1306*cdf0e10cSrcweir String sOutputFile = _sOutputPath; 1307*cdf0e10cSrcweir if (! sOutputFile.endsWith(fs)) 1308*cdf0e10cSrcweir { 1309*cdf0e10cSrcweir sOutputFile += fs; 1310*cdf0e10cSrcweir } 1311*cdf0e10cSrcweir if (sExtension != null && sExtension.length() > 0) 1312*cdf0e10cSrcweir { 1313*cdf0e10cSrcweir sOutputFile += sInputFileNameNoSuffix + "." + sExtension; 1314*cdf0e10cSrcweir } 1315*cdf0e10cSrcweir else 1316*cdf0e10cSrcweir { 1317*cdf0e10cSrcweir sOutputFile += sInputFileBasename; 1318*cdf0e10cSrcweir } 1319*cdf0e10cSrcweir 1320*cdf0e10cSrcweir if (FileHelper.exists(sOutputFile) && _aGTA.getOverwrite() == false) 1321*cdf0e10cSrcweir { 1322*cdf0e10cSrcweir GlobalLogWriter.println("File already exist, don't overwrite. Set " + PropertyName.DOC_COMPARATOR_OVERWRITE_REFERENCE + "=true to force overwrite."); 1323*cdf0e10cSrcweir return; 1324*cdf0e10cSrcweir } 1325*cdf0e10cSrcweir 1326*cdf0e10cSrcweir sOutputURL = URLHelper.getFileURLFromSystemPath(sOutputFile); 1327*cdf0e10cSrcweir 1328*cdf0e10cSrcweir GlobalLogWriter.println("Store document as '" + sOutputURL + "'"); 1329*cdf0e10cSrcweir xStorable.storeAsURL(sOutputURL, PropertyHelper.createPropertyValueArrayFormArrayList(aPropertyList)); 1330*cdf0e10cSrcweir GlobalLogWriter.println("Document stored."); 1331*cdf0e10cSrcweir } 1332*cdf0e10cSrcweir catch (com.sun.star.io.IOException e) 1333*cdf0e10cSrcweir { 1334*cdf0e10cSrcweir GlobalLogWriter.println("Can't store document '" + sOutputURL + "'. Message is :'" + e.getMessage() + "'"); 1335*cdf0e10cSrcweir } 1336*cdf0e10cSrcweir // TODO: Do we need to wait? 1337*cdf0e10cSrcweir // TimeHelper.waitInSeconds(1, "unknown in OfficePrint.convertDocument()"); 1338*cdf0e10cSrcweir 1339*cdf0e10cSrcweir } 1340*cdf0e10cSrcweir 1341*cdf0e10cSrcweir /** 1342*cdf0e10cSrcweir * 1343*cdf0e10cSrcweir * @return false, if 'NoOffice=yes' is given 1344*cdf0e10cSrcweir */ 1345*cdf0e10cSrcweir // private boolean shouldOfficeStart() 1346*cdf0e10cSrcweir // { 1347*cdf0e10cSrcweir // String sNoOffice = (String)m_aParameterHelper.getTestParameters().get( "NoOffice" ); 1348*cdf0e10cSrcweir // if (sNoOffice != null) 1349*cdf0e10cSrcweir // { 1350*cdf0e10cSrcweir // if (sNoOffice.toLowerCase().startsWith("t") || sNoOffice.toLowerCase().startsWith("y")) 1351*cdf0e10cSrcweir // { 1352*cdf0e10cSrcweir // return false; 1353*cdf0e10cSrcweir // } 1354*cdf0e10cSrcweir // } 1355*cdf0e10cSrcweir // return true; 1356*cdf0e10cSrcweir // } 1357*cdf0e10cSrcweir 1358*cdf0e10cSrcweir OfficeProvider m_aProvider = null; 1359*cdf0e10cSrcweir private void startOffice() 1360*cdf0e10cSrcweir { 1361*cdf0e10cSrcweir // SimpleFileSemaphore aSemaphore = new SimpleFileSemaphore(); 1362*cdf0e10cSrcweir // if (shouldOfficeStart()) 1363*cdf0e10cSrcweir // { 1364*cdf0e10cSrcweir // if (OSHelper.isWindows()) 1365*cdf0e10cSrcweir // { 1366*cdf0e10cSrcweir // aSemaphore.P(aSemaphore.getSemaphoreFile()); 1367*cdf0e10cSrcweir // } 1368*cdf0e10cSrcweir m_aParameterHelper.getTestParameters().put(util.PropertyName.DONT_BACKUP_USERLAYER, Boolean.TRUE); 1369*cdf0e10cSrcweir 1370*cdf0e10cSrcweir m_aParameterHelper.getPerformance().startTime(PerformanceContainer.OfficeStart); 1371*cdf0e10cSrcweir m_aProvider = new OfficeProvider(); 1372*cdf0e10cSrcweir XMultiServiceFactory xMSF = (XMultiServiceFactory) m_aProvider.getManager(m_aParameterHelper.getTestParameters()); 1373*cdf0e10cSrcweir m_aParameterHelper.getTestParameters().put("ServiceFactory", xMSF); 1374*cdf0e10cSrcweir m_aParameterHelper.getPerformance().stopTime(PerformanceContainer.OfficeStart); 1375*cdf0e10cSrcweir 1376*cdf0e10cSrcweir long nStartTime = m_aParameterHelper.getPerformance().getTime(PerformanceContainer.OfficeStart); 1377*cdf0e10cSrcweir // aGTA = getParameterHelper(); // get new TestArguments 1378*cdf0e10cSrcweir m_aParameterHelper.getPerformance().setTime(PerformanceContainer.OfficeStart, nStartTime); 1379*cdf0e10cSrcweir // } 1380*cdf0e10cSrcweir 1381*cdf0e10cSrcweir // Watcher Object is need in log object to give a simple way to say if a running office is alive. 1382*cdf0e10cSrcweir // As long as a log comes, it pings the Watcher and says the office is alive, if not an 1383*cdf0e10cSrcweir // internal counter increase and at a given point (300 seconds) the office is killed. 1384*cdf0e10cSrcweir if (GlobalLogWriter.get().getWatcher() == null) 1385*cdf0e10cSrcweir { 1386*cdf0e10cSrcweir GlobalLogWriter.println("Set office watcher"); 1387*cdf0e10cSrcweir OfficeWatcher aWatcher = (OfficeWatcher)m_aParameterHelper.getTestParameters().get("Watcher"); 1388*cdf0e10cSrcweir GlobalLogWriter.get().setWatcher(aWatcher); 1389*cdf0e10cSrcweir } 1390*cdf0e10cSrcweir } 1391*cdf0e10cSrcweir 1392*cdf0e10cSrcweir private void stopOffice() 1393*cdf0e10cSrcweir { 1394*cdf0e10cSrcweir // Office shutdown 1395*cdf0e10cSrcweir if (m_aProvider != null) 1396*cdf0e10cSrcweir { 1397*cdf0e10cSrcweir String sAppExecCmd = (String)m_aParameterHelper.getTestParameters().get("AppExecutionCommand"); 1398*cdf0e10cSrcweir if (sAppExecCmd != null && sAppExecCmd.length() > 0) 1399*cdf0e10cSrcweir { 1400*cdf0e10cSrcweir m_aProvider.closeExistingOffice(m_aParameterHelper.getTestParameters(), true); 1401*cdf0e10cSrcweir } 1402*cdf0e10cSrcweir // if (OSHelper.isWindows()) 1403*cdf0e10cSrcweir // { 1404*cdf0e10cSrcweir // aSemaphore.V(aSemaphore.getSemaphoreFile()); 1405*cdf0e10cSrcweir // aSemaphore.sleep(2); 1406*cdf0e10cSrcweir // // wait some time maybe an other process will take the semaphore 1407*cdf0e10cSrcweir // // I know, this is absolutly dirty, but the whole convwatch is dirty and need a big cleanup. 1408*cdf0e10cSrcweir // } 1409*cdf0e10cSrcweir } 1410*cdf0e10cSrcweir } 1411*cdf0e10cSrcweir 1412*cdf0e10cSrcweir private boolean m_bStoreFile; 1413*cdf0e10cSrcweir public void disallowStore() 1414*cdf0e10cSrcweir { 1415*cdf0e10cSrcweir m_bStoreFile = false; 1416*cdf0e10cSrcweir } 1417*cdf0e10cSrcweir public void allowStore() 1418*cdf0e10cSrcweir { 1419*cdf0e10cSrcweir m_bStoreFile = true; 1420*cdf0e10cSrcweir } 1421*cdf0e10cSrcweir public boolean isStoreAllowed() 1422*cdf0e10cSrcweir { 1423*cdf0e10cSrcweir return false; 1424*cdf0e10cSrcweir // return m_bStoreFile; 1425*cdf0e10cSrcweir } 1426*cdf0e10cSrcweir 1427*cdf0e10cSrcweir } 1428*cdf0e10cSrcweir 1429