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