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