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