bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / runner / graphical / OpenOfficePostscriptCreator.java
blobf6529df66a36f1b5467e5e20fe7caba86467b933
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package graphical;
21 import com.sun.star.frame.FrameSearchFlag;
22 import com.sun.star.util.XCloseable;
23 import helper.OfficeProvider;
24 import helper.OfficeWatcher;
25 import java.util.ArrayList;
27 import com.sun.star.uno.UnoRuntime;
28 import com.sun.star.lang.XMultiServiceFactory;
29 import com.sun.star.document.XTypeDetection;
30 import com.sun.star.container.XNameAccess;
31 import com.sun.star.frame.XDesktop;
32 import com.sun.star.beans.XPropertySet;
33 import com.sun.star.beans.PropertyValue;
34 import com.sun.star.frame.XComponentLoader;
35 import com.sun.star.lang.XComponent;
36 import com.sun.star.frame.XStorable;
37 import com.sun.star.view.XPrintable;
38 import com.sun.star.lang.XServiceInfo;
39 import com.sun.star.frame.XModel;
40 import com.sun.star.uno.AnyConverter;
42 import helper.URLHelper;
43 import helper.PropertyHelper;
44 import helper.OSHelper;
46 // import helper.Parameter;
47 import java.io.File;
49 /**
50 * This Object is to print a given document with OpenOffice.org / StarOffice
51 * over the normal printer driver
52 * or over it's pdf exporter
54 public class OpenOfficePostscriptCreator implements IOffice
56 private ParameterHelper m_aParameterHelper;
57 private String m_sOutputURL;
58 private String m_sBasename;
59 private String m_sDocumentName;
60 private XComponent m_aDocument;
62 public OpenOfficePostscriptCreator(ParameterHelper _aParam, String _sResult)
64 m_aParameterHelper = _aParam;
65 String sOutputURL = _sResult;
66 if (! sOutputURL.startsWith("file:"))
68 sOutputURL = URLHelper.getFileURLFromSystemPath(_sResult);
70 m_sOutputURL = sOutputURL;
71 m_aDocument = null;
75 public void load(String _sDocumentName) throws OfficeException
77 m_sDocumentName = _sDocumentName;
79 String sInputFileURL = URLHelper.getFileURLFromSystemPath(m_sDocumentName);
80 m_aDocument = loadFromURL(m_aParameterHelper, sInputFileURL);
81 if (m_aDocument == null)
83 GlobalLogWriter.println("loadDocumentFromURL() failed with document: " + sInputFileURL);
84 throw new OfficeException("load(): failed with document" + sInputFileURL);
87 m_sBasename = FileHelper.getBasename(m_sDocumentName);
90 public void storeAsPostscript() throws OfficeException
92 if (m_aDocument != null)
94 String sDocumentName = FileHelper.appendPath(m_sOutputURL, m_sBasename);
95 if (m_aParameterHelper.getReferenceType().toLowerCase().equals("ooo") ||
96 m_aParameterHelper.getReferenceType().toLowerCase().equals("o3") ||
97 m_aParameterHelper.getReferenceType().toLowerCase().equals("ps") )
99 String sPrintURL = sDocumentName + ".ps";
101 impl_printToFileWithOOo(m_aParameterHelper, m_aDocument, sDocumentName, sPrintURL /*_sPrintFileURL*/);
102 String sBasename = FileHelper.getBasename(sPrintURL);
103 FileHelper.addBasenameToIndex(m_sOutputURL, sBasename, "OOo", "postscript", m_sDocumentName);
105 else if (m_aParameterHelper.getReferenceType().toLowerCase().equals("pdf"))
107 String sPDFURL = sDocumentName + ".pdf";
108 storeAsPDF(m_aParameterHelper, m_aDocument, sPDFURL);
110 String sBasename = FileHelper.getBasename(sPDFURL);
111 FileHelper.addBasenameToIndex(m_sOutputURL, sBasename, "pdf", "pdf-export", m_sDocumentName);
113 else
115 throw new OfficeException("unknown reference type");
117 GlobalLogWriter.println("Close document.");
118 m_aDocument.dispose();
122 public void start() throws OfficeException
124 startOffice();
127 public void close() throws OfficeException
129 stopOffice();
136 private void showProperty(PropertyValue _aValue)
138 String sName = _aValue.Name;
139 String sValue;
142 sValue = AnyConverter.toString(_aValue.Value);
143 GlobalLogWriter.println("Property " + sName + ":=" + sValue);
145 catch (com.sun.star.lang.IllegalArgumentException e)
147 GlobalLogWriter.println("showProperty: can't convert a object to string. " + e.getMessage());
152 * shows the FilterName and MediaType from the given XComponent
154 private String getDocumentType( XComponent _aDoc )
156 XModel xModel = UnoRuntime.queryInterface( XModel.class, _aDoc);
157 PropertyValue[] aArgs = xModel.getArgs();
158 for (int i=0;i<aArgs.length;i++)
160 PropertyValue aValue = aArgs[i];
161 // System.out.print("Property: '" + aValue.Name);
162 // System.out.println("' := '" + aValue.Value + "'");
163 if (aValue.Name.equals("FilterName") ||
164 aValue.Name.equals("MediaType"))
166 String sNameValue = "'" + aValue.Name + "' := '" + aValue.Value + "'";
167 return sNameValue;
170 return "";
173 private void showDocumentType( XComponent _aDoc )
175 String sNameValue = getDocumentType(_aDoc);
176 GlobalLogWriter.println(" Property: '" + sNameValue);
179 * load a OpenOffice.org document from a given URL (_sInputURL)
180 * the ParameterHelper must contain a living MultiServiceFactory object
181 * or we crash here.
182 * Be aware, the ownership of the document gets to you, you have to close it.
184 private XComponent loadFromURL(ParameterHelper _aGTA,
185 String _sInputURL)
187 XComponent aDoc = null;
190 if (_aGTA.getMultiServiceFactory() == null)
192 GlobalLogWriter.println("MultiServiceFactory in GraphicalTestArgument not set.");
193 return null;
195 Object oDsk = _aGTA.getMultiServiceFactory().createInstance("com.sun.star.frame.Desktop");
196 XDesktop aDesktop = UnoRuntime.queryInterface(XDesktop.class, oDsk);
198 if (aDesktop != null)
200 GlobalLogWriter.println("com.sun.star.frame.Desktop created.");
201 // String sInputURL = aCurrentParameter.sInputURL;
202 // String sOutputURL = aCurrentParameter.sOutputURL;
203 // String sPrintFileURL = aCurrentParameter.sPrintToFileURL;
204 // System.out.println(_sInputURL);
207 // set here the loadComponentFromURL() properties
208 // at the moment only 'Hidden' is set, so no window is opened at work
210 ArrayList<PropertyValue> aPropertyList = new ArrayList<PropertyValue>();
212 // check which properties should set and count it.
213 // if (_aGTA.isHidden())
214 // {
215 // nPropertyCount ++;
216 // }
217 // if (_aGTA.getImportFilterName() != null && _aGTA.getImportFilterName().length() > 0)
218 // {
219 // nPropertyCount ++;
220 // }
222 // initialize the propertyvalue
223 // int nPropertyIndex = 0;
224 // aProps = new PropertyValue[ nPropertyCount ];
226 // set all property values
227 if (_aGTA.isHidden())
229 PropertyValue Arg = new PropertyValue();
230 Arg.Name = "Hidden";
231 Arg.Value = Boolean.TRUE;
232 aPropertyList.add(Arg);
233 showProperty(Arg);
235 if (_aGTA.getImportFilterName() != null && _aGTA.getImportFilterName().length() > 0)
237 PropertyValue Arg = new PropertyValue();
238 Arg.Name = "FilterName";
239 Arg.Value = _aGTA.getImportFilterName();
240 aPropertyList.add(Arg);
241 showProperty(Arg);
243 PropertyValue ReadOnly = new PropertyValue();
244 ReadOnly.Name = "ReadOnly";
245 ReadOnly.Value = Boolean.TRUE;
246 aPropertyList.add(ReadOnly);
247 showProperty(ReadOnly);
249 GlobalLogWriter.println(DateHelper.getDateTimeForHumanreadableLog() + " Load document");
250 // GlobalLogWriter.flush();
252 XComponentLoader aCompLoader = UnoRuntime.queryInterface( XComponentLoader.class, aDesktop);
254 // XComponent aDoc = null;
256 _aGTA.getPerformance().startTime(PerformanceContainer.Load);
257 aDoc = aCompLoader.loadComponentFromURL(_sInputURL, "_blank", FrameSearchFlag.ALL, PropertyHelper.createPropertyValueArrayFormArrayList(aPropertyList) );
258 _aGTA.getPerformance().stopTime(PerformanceContainer.Load);
259 if (aDoc != null)
261 GlobalLogWriter.println(DateHelper.getDateTimeForHumanreadableLog() + " Load document done.");
262 showDocumentType(aDoc);
263 _aGTA.setDocumentType(getDocumentType(aDoc));
264 // TODO: TimeHelper.waitInSeconds(20, "Wait after load document. Maybe helps due to layouting problems.");
266 else
268 GlobalLogWriter.println(" Load document failed.");
269 if (_aGTA.getImportFilterName() != null && _aGTA.getImportFilterName().length() > 0)
271 GlobalLogWriter.println(" Please check FilterName := '" + _aGTA.getImportFilterName() + "'");
273 GlobalLogWriter.println("");
276 else
278 GlobalLogWriter.println("com.sun.star.frame.Desktop failed.");
281 catch ( com.sun.star.uno.Exception e )
283 // Some exception occurs.FAILED
284 GlobalLogWriter.println("UNO Exception caught.");
285 GlobalLogWriter.println("Message: " + e.getMessage());
286 e.printStackTrace();
287 aDoc = null;
289 return aDoc;
292 private boolean exportToPDF(XComponent _xComponent, String _sDestinationName)
294 XServiceInfo xServiceInfo =
295 UnoRuntime.queryInterface(
296 XServiceInfo.class, _xComponent
299 ArrayList<PropertyValue> aPropertyList = new ArrayList<PropertyValue>();
300 PropertyValue aFiltername = new PropertyValue();
301 aFiltername.Name = "FilterName";
302 aFiltername.Value = getFilterName_forPDF(xServiceInfo);
303 aPropertyList.add(aFiltername);
304 showProperty(aFiltername);
305 boolean bWorked = true;
307 // TODO: TimeHelper.waitInSeconds(20, "Wait before storeToURL. Maybe helps due to layouting problems.");
310 XStorable store =
311 UnoRuntime.queryInterface(
312 XStorable.class, _xComponent
314 store.storeToURL(_sDestinationName, PropertyHelper.createPropertyValueArrayFormArrayList(aPropertyList));
316 catch (com.sun.star.io.IOException e)
318 GlobalLogWriter.println("IO Exception caught.");
319 GlobalLogWriter.println("Message: " + e.getMessage());
320 bWorked = false;
323 return bWorked;
327 private String getFilterName_forPDF(XServiceInfo xServiceInfo)
329 String filterName = "";
331 if (xServiceInfo.supportsService("com.sun.star.text.TextDocument"))
333 //writer
334 filterName = "writer_pdf_Export";
336 else if ( xServiceInfo.supportsService( "com.sun.star.sheet.SpreadsheetDocument" ) )
338 //calc
339 filterName = "calc_pdf_Export";
341 else if ( xServiceInfo.supportsService( "com.sun.star.drawing.DrawingDocument" ) )
343 //draw
344 filterName = "draw_pdf_Export";
346 else if ( xServiceInfo.supportsService( "com.sun.star.presentation.PresentationDocument" ) )
348 //impress
349 filterName = "impress_pdf_Export";
351 else if (xServiceInfo.supportsService("com.sun.star.text.WebDocument"))
353 //html document
354 filterName = "writer_web_pdf_Export";
356 else if ( xServiceInfo.supportsService("com.sun.star.text.GlobalDocument") )
358 //master document
359 filterName = "writer_globaldocument_pdf_Export";
361 else if ( xServiceInfo.supportsService( "com.sun.star.formulaFormulaProperties" ) )
363 //math document
364 filterName = "math_pdf_Export";
367 return filterName;
370 // -----------------------------------------------------------------------------
372 // public boolean storeAsPDF(ParameterHelper _aGTA,
373 // String _sInputURL,
374 // String _sOutputURL)
375 // {
376 // boolean bBack = false;
377 // XComponent aDoc = loadFromURL(_aGTA, _sInputURL);
379 // if (aDoc == null)
380 // {
381 // GlobalLogWriter.println("Can't load document.");
382 // return bBack;
383 // }
384 // bBack = storeAsPDF(_aGTA, aDoc, _sOutputURL);
385 // FileHelper.createInfoFile(_sOutputURL, _aGTA, "as pdf");
387 // GlobalLogWriter.println("Close document.");
388 // aDoc.dispose();
389 // return bBack;
390 // }
392 public boolean storeAsPDF(ParameterHelper _aGTA,
393 XComponent _aDoc,
394 String _sOutputURL) throws OfficeException
396 // try {
397 boolean bBack = true;
398 _aGTA.getPerformance().startTime(PerformanceContainer.StoreAsPDF);
399 bBack = exportToPDF(_aDoc, _sOutputURL);
400 _aGTA.getPerformance().stopTime(PerformanceContainer.StoreAsPDF);
402 if (!bBack)
404 GlobalLogWriter.println("Can't store document as PDF.");
405 // bBack = false;
406 throw new OfficeException("Can't store document as PDF");
408 else
410 FileHelper.createInfoFile(_sOutputURL, _aGTA, "as pdf");
412 return bBack;
415 // -----------------------------------------------------------------------------
418 * print the document found in file (_sInputURL) to as postscript to file (_sPrintFileURL)
419 * Due to the fact we use a printer to convert the file to postscript, the default printer
420 * to create such postscript format must be installed, this is not tested here.
422 * @return true, if print has been done.
423 * Be careful, true means only print returns with no errors, to be sure print is really done
424 * check existance of _sPrintFileURL
427 // public boolean printToFileWithOOo(ParameterHelper _aGTA,
428 // String _sInputURL,
429 // String _sOutputURL,
430 // String _sPrintFileURL)
431 // {
432 // // waitInSeconds(1);
433 // boolean bBack = false;
435 // XComponent aDoc = loadFromURL(_aGTA, _sInputURL);
436 // if (aDoc != null)
437 // {
438 // if ( _sInputURL.equals(_sOutputURL) )
439 // {
440 // // don't store document
441 // // input and output are equal OR
442 // GlobalLogWriter.println("Warning: Inputpath and Outputpath are equal. Document will not stored again.");
443 // disallowStore();
444 // }
445 // bBack = impl_printToFileWithOOo(_aGTA, aDoc, _sOutputURL, _sPrintFileURL);
447 // GlobalLogWriter.println("Close document.");
448 // aDoc.dispose();
449 // }
450 // else
451 // {
452 // GlobalLogWriter.println("loadDocumentFromURL() failed with document: " + _sInputURL);
453 // }
454 // return bBack;
455 // }
459 // -----------------------------------------------------------------------------
460 private boolean impl_printToFileWithOOo(ParameterHelper _aGTA,
461 XComponent _aDoc,
462 String _sOutputURL,
463 String _sPrintFileURL)
465 boolean bBack = false;
466 boolean bFailed = true; // always be a pessimist,
467 if (_aDoc == null)
469 GlobalLogWriter.println("No document is given.");
470 return bBack;
475 if (_sOutputURL != null)
477 if (isStoreAllowed())
479 // store the document in an other directory
480 XStorable aStorable = UnoRuntime.queryInterface( XStorable.class, _aDoc);
481 if (aStorable != null)
483 PropertyValue [] szEmptyArgs = new PropertyValue [0];
485 GlobalLogWriter.println(DateHelper.getDateTimeForHumanreadableLog() + " Store document.");
486 _aGTA.getPerformance().startTime(PerformanceContainer.Store);
487 aStorable.storeAsURL(_sOutputURL, szEmptyArgs);
488 _aGTA.getPerformance().stopTime(PerformanceContainer.Store);
490 GlobalLogWriter.println(DateHelper.getDateTimeForHumanreadableLog() + " Store document done.");
491 // TimeHelper.waitInSeconds(1, "After store as URL to:" + _sOutputURL);
492 GlobalLogWriter.println("Reload stored file test.");
493 XComponent aDoc = loadFromURL(_aGTA, _sOutputURL);
494 if (aDoc == null)
496 GlobalLogWriter.println("Reload stored file test failed, can't reload file: " + _sOutputURL);
498 else
500 XCloseable xClose = UnoRuntime.queryInterface(XCloseable.class, aDoc);
501 if (xClose != null)
503 xClose.close(true);
505 else
507 aDoc.dispose();
509 // TimeHelper.waitInSeconds(1, "after close temp document");
513 else
515 // make sure to create the directory in
516 String sOutputFilename = FileHelper.getSystemPathFromFileURL(_sOutputURL);
517 String sOutputPath = FileHelper.getPath(sOutputFilename);
518 File aFile = new File(sOutputPath);
519 aFile.mkdirs();
523 catch ( com.sun.star.uno.Exception e )
525 // Some exception occurs.FAILED
526 GlobalLogWriter.println("UNO Exception caught.");
527 GlobalLogWriter.println("Message: " + e.getMessage());
529 e.printStackTrace();
530 bBack = false;
536 // System.out.println("Document loaded.");
537 // Change Pagesettings to DIN A4
539 GlobalLogWriter.println(DateHelper.getDateTimeForHumanreadableLog() + " Print document.");
540 XPrintable aPrintable = UnoRuntime.queryInterface( XPrintable.class, _aDoc);
541 if (aPrintable != null)
543 // System.out.println(" Set PaperFormat to DIN A4");
544 // {
545 // PropertyValue[] aPrinterProps = aPrintable.getPrinter();
546 // System.out.println("PrinterProps size: " + String.valueOf(aPrinterProps.length));
547 // int nPropIndex = 0;
548 // while (!"PaperFormat".equals(aPrinterProps[nPropIndex].Name))
549 // {
550 // // System.out.println(aPrinterProps[nPropIndex].Name);
551 // nPropIndex++;
552 // }
553 // aPrinterProps[nPropIndex].Value = com.sun.star.view.PaperFormat.A4;
554 // aPrintable.setPrinter(aPrinterProps);
555 // }
557 // configure Office to allow to execute macos
559 // TODO: We need a possiblity to set the printer name also for StarOffice/OpenOffice
560 if (OSHelper.isWindows())
562 if (_aGTA.getPrinterName() != null)
564 ArrayList<PropertyValue> aPropertyList = new ArrayList<PropertyValue>();
565 // PropertyValue [] aPrintProps = new PropertyValue[1];
566 PropertyValue Arg = new PropertyValue();
567 Arg.Name = "Name";
568 Arg.Value = _aGTA.getPrinterName();
569 aPropertyList.add(Arg);
570 showProperty(Arg);
571 // GlobalLogWriter.println("Printername is not null, so set to " + _aGTA.getPrinterName());
572 aPrintable.setPrinter(PropertyHelper.createPropertyValueArrayFormArrayList(aPropertyList));
576 // set property values for XPrintable.print()
577 // more can be found at "http://api.libreoffice.org/docs/common/ref/com/sun/star/view/PrintOptions.html"
579 // int nProperties = 1; // default for 'FileName' property
580 // if (_aGTA.printAllPages() == false)
581 // {
582 // // we don't want to print all pages, build Pages string by ourself
583 // nProperties ++;
584 // }
585 // int nPropsCount = 0;
587 // If we are a SpreadSheet (calc), we need to set PrintAllSheets property to 'true'
588 XServiceInfo xServiceInfo = UnoRuntime.queryInterface( XServiceInfo.class, _aDoc );
589 if ( xServiceInfo.supportsService( "com.sun.star.sheet.SpreadsheetDocument" ) )
591 XMultiServiceFactory xMSF = _aGTA.getMultiServiceFactory();
592 Object aSettings = xMSF.createInstance( "com.sun.star.sheet.GlobalSheetSettings" );
593 if (aSettings != null)
595 XPropertySet xPropSet = UnoRuntime.queryInterface( XPropertySet.class, aSettings );
596 xPropSet.setPropertyValue( "PrintAllSheets", new Boolean( true ) );
597 GlobalLogWriter.println("PrintAllSheets := true");
601 ArrayList<PropertyValue> aPrintProps = new ArrayList<PropertyValue>();
602 // GlobalLogWriter.println("Property FileName:=" + _sPrintFileURL);
604 // PropertyValue [] aPrintProps = new PropertyValue[nProperties];
605 PropertyValue Arg = new PropertyValue();
606 Arg.Name = "FileName";
607 Arg.Value = _sPrintFileURL;
608 // aPrintProps[nPropsCount ++] = Arg;
609 aPrintProps.add(Arg);
610 showProperty(Arg);
613 // generate pages string
614 if (_aGTA.printAllPages() == false)
616 String sPages = "";
617 if (_aGTA.getMaxPages() > 0)
619 sPages = "1-" + String.valueOf(_aGTA.getMaxPages());
621 if (_aGTA.getOnlyPages().length() != 0)
623 if (sPages.length() != 0)
625 sPages += ";";
627 sPages += String.valueOf(_aGTA.getOnlyPages());
630 Arg = new PropertyValue();
631 Arg.Name = "Pages";
632 Arg.Value = sPages;
633 aPrintProps.add(Arg);
634 showProperty(Arg);
637 // GlobalLogWriter.println("Start printing.");
639 _aGTA.getPerformance().startTime(PerformanceContainer.Print);
640 aPrintable.print(PropertyHelper.createPropertyValueArrayFormArrayList(aPrintProps));
641 TimeHelper.waitInSeconds(1, "Start waiting for print ready.");
643 GlobalLogWriter.println("Wait until document is printed.");
644 boolean isBusy = true;
645 int nPrintCount = 0;
646 while (isBusy)
648 PropertyValue[] aPrinterProps = aPrintable.getPrinter();
649 int nPropIndex = 0;
650 while (!"IsBusy".equals(aPrinterProps[nPropIndex].Name))
652 // System.out.println(aPrinterProps[nPropIndex].Name);
653 nPropIndex++;
655 isBusy = (aPrinterProps[nPropIndex].Value == Boolean.TRUE) ? true : false;
656 TimeHelper.waitInSeconds(1, "is print ready?");
657 nPrintCount++;
658 if (nPrintCount > 3600)
660 // we will never wait >1h until print is ready!
661 GlobalLogWriter.println("ERROR: Cancel print due to too long wait.");
662 throw new com.sun.star.uno.Exception("Convwatch exception, wait too long for printing.");
665 // TODO:
666 // TimeHelper.waitInSeconds(40, "Start waiting after print ready.");
668 _aGTA.getPerformance().stopTime(PerformanceContainer.Print);
669 GlobalLogWriter.println(DateHelper.getDateTimeForHumanreadableLog() + " Print document done.");
671 // Create a .info file near the printed '.ps' or '.prn' file.
672 FileHelper.createInfoFile(_sPrintFileURL, _aGTA);
674 else
676 GlobalLogWriter.println("Can't get XPrintable interface.");
678 bFailed = false;
679 bBack = true;
681 catch ( com.sun.star.uno.Exception e )
683 // Some exception occurs.FAILED
684 GlobalLogWriter.println("UNO Exception caught.");
685 GlobalLogWriter.println("Message: " + e.getMessage());
687 e.printStackTrace();
688 bBack = false;
691 if (bFailed == true)
693 GlobalLogWriter.println("convwatch.OfficePrint: FAILED");
695 else
697 GlobalLogWriter.println("convwatch.OfficePrint: OK");
699 return bBack;
704 * @param _aGTA
705 * @param _sAbsoluteOutputPath
706 * @param _sAbsoluteInputFile
707 * @return true, if the reference (*.prrn file) based on given output path and given input path exist.
708 * If OVERWRITE_REFERENCE is set, always return false.
710 public boolean isReferenceExists(ParameterHelper _aGTA,
711 String _sAbsoluteOutputPath,
712 String _sAbsoluteInputFile)
714 if (! FileHelper.exists(_sAbsoluteInputFile))
716 // throw new ConvWatchCancelException("Input file: " + _sAbsoluteInputFile + " does not exist.");
717 return false;
720 // String fs = System.getProperty("file.separator");
722 // String sInputFileURL = URLHelper.getFileURLFromSystemPath(_sAbsoluteInputFile);
724 String sInputFileBasename = FileHelper.getBasename(_sAbsoluteInputFile);
725 // String sOutputFileURL = null;
726 String sOutputPath;
727 if (_sAbsoluteOutputPath != null)
729 sOutputPath = _sAbsoluteOutputPath;
730 // FileHelper.makeDirectories("", sOutputPath);
732 else
734 String sInputPath = FileHelper.getPath(_sAbsoluteInputFile);
735 sOutputPath = sInputPath;
737 // sOutputFileURL = URLHelper.getFileURLFromSystemPath(sOutputPath + fs + sInputFileBasename);
738 // sOutputFileURL = null;
740 String sPrintFilename = FileHelper.getNameNoSuffix(sInputFileBasename);
741 // String sPrintFileURL;
743 String sAbsolutePrintFilename = FileHelper.appendPath(sOutputPath, sPrintFilename + ".prn");
744 if (FileHelper.exists(sAbsolutePrintFilename) && _aGTA.getOverwrite() == false)
746 GlobalLogWriter.println("Reference already exist, don't overwrite. Set " + PropertyName.DOC_COMPARATOR_OVERWRITE_REFERENCE + "=true to force overwrite.");
747 return true;
749 return false;
752 // -----------------------------------------------------------------------------
754 * create a reference file
755 * _sAbsoluteInputPath contains the source file, if not exists, return with failure.
756 * _sAbsoluteOutputPath contains the destination, where the file will store after load with StarOffice/OpenOffice.org
757 * if is null, print only near the Input file path
758 * _sPrintType ".prn" Print input file with StarOffice/OpenOffice.org and the default printer as PostScript
760 * @param _aGTA
761 * @return
763 // public static boolean buildReference(ParameterHelper _aGTA,
764 // String _sAbsoluteOutputPath,
765 // String _sAbsoluteInputFile)
766 // throws OfficeException
767 // {
768 // if (! FileHelper.exists(_sAbsoluteInputFile))
769 // {
770 // throw new OfficeException("buildReference(): Input file: " + _sAbsoluteInputFile + " does not exist.");
771 // }
773 // String fs = System.getProperty("file.separator");
775 // String sInputFileURL = URLHelper.getFileURLFromSystemPath(_sAbsoluteInputFile);
777 // String sInputFileBasename = FileHelper.getBasename(_sAbsoluteInputFile);
778 // String sOutputFileURL = null;
779 // String sOutputPath;
780 // if (_sAbsoluteOutputPath != null)
781 // {
782 // sOutputPath = _sAbsoluteOutputPath;
783 // FileHelper.makeDirectories("", sOutputPath);
784 // }
785 // else
786 // {
787 // String sInputPath = FileHelper.getPath(_sAbsoluteInputFile);
788 // sOutputPath = sInputPath;
789 // }
790 // // sOutputFileURL = URLHelper.getFileURLFromSystemPath(sOutputPath + fs + sInputFileBasename);
791 // sOutputFileURL = null;
793 // String sPrintFilename = FileHelper.getNameNoSuffix(sInputFileBasename);
794 // String sPrintFileURL;
796 // String sAbsolutePrintFilename = sOutputPath + fs + sPrintFilename + ".prn";
797 // if (FileHelper.exists(sAbsolutePrintFilename) && _aGTA.getOverwrite() == false)
798 // {
799 // GlobalLogWriter.println("Reference already exist, don't overwrite. Set " + PropertyName.DOC_COMPARATOR_OVERWRITE_REFERENCE + "=true to force overwrite.");
800 // return true;
801 // }
803 // if (_aGTA.getReferenceType().toLowerCase().equals("msoffice"))
804 // {
805 // sPrintFileURL = URLHelper.getFileURLFromSystemPath(sAbsolutePrintFilename);
806 // }
807 // else if (_aGTA.getReferenceType().toLowerCase().equals("pdf"))
808 // {
809 //// TODO: If we rename the stored file to *.pdf, we have to be sure that we use *.pdf also as a available reference
810 // sPrintFileURL = URLHelper.getFileURLFromSystemPath(sAbsolutePrintFilename );
811 // }
812 // else if (_aGTA.getReferenceType().toLowerCase().equals("ooo"))
813 // {
814 // sPrintFileURL = URLHelper.getFileURLFromSystemPath(sAbsolutePrintFilename );
815 // }
816 // else
817 // {
818 // GlobalLogWriter.println("OfficePrint.buildreference(): Unknown print type.");
819 // return false;
820 // }
821 // return printToFile(_aGTA, sInputFileURL, sOutputFileURL, sPrintFileURL);
822 // }
826 // TODO: Das Teil muss hier raus!
829 // public static boolean printToFile(ParameterHelper _aGTA,
830 // String _sInputFileURL,
831 // String _sOutputFileURL,
832 // String _sPrintFileURL) throws OfficeException
833 // {
834 // boolean bBack = false;
835 // String sPrintFileURL = null;
838 // // remember the current timer, to know how long a print process need.
839 // // startTimer();
841 // if (_aGTA.getReferenceType().toLowerCase().equals("ooo"))
842 // {
843 // bBack = printToFileWithOOo(_aGTA, _sInputFileURL, _sOutputFileURL, _sPrintFileURL);
844 // }
845 // else if (_aGTA.getReferenceType().toLowerCase().equals("pdf"))
846 // {
847 // GlobalLogWriter.println("USE PDF AS EXPORT FORMAT.");
848 // bBack = storeAsPDF(_aGTA, _sInputFileURL, _sPrintFileURL);
849 // }
850 // else if (_aGTA.getReferenceType().toLowerCase().equals("msoffice"))
851 // {
852 // if (MSOfficePostscriptCreator.isMSOfficeDocumentFormat(_sInputFileURL))
853 // {
854 // GlobalLogWriter.println("USE MSOFFICE AS EXPORT FORMAT.");
855 // MSOfficePostscriptCreator a = new MSOfficePostscriptCreator();
856 // try
857 // {
858 // a.printToFileWithMSOffice(_aGTA, FileHelper.getSystemPathFromFileURL(_sInputFileURL),
859 // FileHelper.getSystemPathFromFileURL(_sPrintFileURL));
860 // }
861 // catch(OfficeException e)
862 // {
863 // e.printStackTrace();
864 // GlobalLogWriter.println(e.getMessage());
865 // throw new OfficeException("Exception caught. Problem with MSOffice printer methods.");
866 // }
867 // catch(java.io.IOException e)
868 // {
869 // GlobalLogWriter.println(e.getMessage());
870 // throw new OfficeException("IOException caught. Problem with MSOffice printer methods.");
871 // }
872 // bBack = true;
873 // }
874 // else
875 // {
876 // GlobalLogWriter.println("This document type is not recognized as MSOffice format, as default fallback StarOffice/OpenOffice.org instead is used.");
877 // bBack = printToFileWithOOo(_aGTA, _sInputFileURL, _sOutputFileURL, _sPrintFileURL);
878 // }
879 // }
880 // else
881 // {
882 // // System.out.println("");
883 // throw new OfficeException("OfficePrint.printToFile(): Unknown print type.");
884 // }
885 // return bBack;
886 // }
888 // -----------------------------------------------------------------------------
889 // TODO: move this away!
890 // -----------------------------------------------------------------------------
891 void showType(String _sInputURL, XMultiServiceFactory _xMSF)
893 if (_sInputURL.length() == 0)
895 return;
898 if (_xMSF == null)
900 GlobalLogWriter.println("MultiServiceFactory not set.");
901 return;
903 XTypeDetection aTypeDetection = null;
906 Object oObj = _xMSF.createInstance("com.sun.star.document.TypeDetection");
907 aTypeDetection = UnoRuntime.queryInterface(XTypeDetection.class, oObj);
909 catch(com.sun.star.uno.Exception e)
911 GlobalLogWriter.println("Can't get com.sun.star.document.TypeDetection.");
912 return;
914 if (aTypeDetection != null)
916 String sType = aTypeDetection.queryTypeByURL(_sInputURL);
917 GlobalLogWriter.println("Type is: " + sType);
922 // -----------------------------------------------------------------------------
923 public String getInternalFilterName(String _sFilterName, XMultiServiceFactory _xMSF)
925 if (_sFilterName.length() == 0)
927 // System.out.println("No FilterName set.");
928 return null;
931 if (_xMSF == null)
933 GlobalLogWriter.println("MultiServiceFactory not set.");
934 return null;
936 // XFilterFactory aFilterFactory = null;
937 Object aObj = null;
940 aObj = _xMSF.createInstance("com.sun.star.document.FilterFactory");
942 catch(com.sun.star.uno.Exception e)
944 GlobalLogWriter.println("Can't get com.sun.star.document.FilterFactory.");
945 return null;
947 if (aObj != null)
949 XNameAccess aNameAccess = UnoRuntime.queryInterface(XNameAccess.class, aObj);
950 if (aNameAccess != null)
953 // if (_sFilterName.toLowerCase().equals("help"))
954 // {
955 // System.out.println("Show all possible ElementNames from current version." );
956 // String[] aElementNames = aNameAccess.getElementNames();
957 // for (int i = 0; i<aElementNames.length; i++)
958 // {
959 // System.out.println(aElementNames[i]);
960 // }
961 // System.out.println("Must quit.");
962 // System.out.exit(1);
963 // }
965 if (! aNameAccess.hasByName(_sFilterName))
967 GlobalLogWriter.println("FilterFactory.hasByName() says there exist no '" + _sFilterName + "'" );
968 return null;
971 Object[] aElements = null;
974 aElements = (Object[]) aNameAccess.getByName(_sFilterName);
975 if (aElements != null)
977 String sInternalFilterName = null;
978 // System.out.println("getByName().length: " + String.valueOf(aElements.length));
979 for (int i=0;i<aElements.length; i++)
981 PropertyValue aPropertyValue = (PropertyValue)aElements[i];
982 // System.out.println("PropertyValue.Name: " + aPropertyValue.Name);
983 if (aPropertyValue.Name.equals("Type"))
985 String sValue = (String)aPropertyValue.Value;
986 // System.out.println("Type: " + sValue);
987 sInternalFilterName = sValue;
990 return sInternalFilterName;
992 else
994 GlobalLogWriter.println("There are no elements for FilterName '" + _sFilterName + "'");
995 return null;
998 catch (com.sun.star.container.NoSuchElementException e)
1000 GlobalLogWriter.println("NoSuchElementException caught. " + e.getMessage());
1002 catch (com.sun.star.lang.WrappedTargetException e)
1004 GlobalLogWriter.println("WrappedTargetException caught. " + e.getMessage());
1008 return null;
1011 // -----------------------------------------------------------------------------
1013 String getServiceNameFromFilterName(String _sFilterName, XMultiServiceFactory _xMSF)
1015 if (_sFilterName.length() == 0)
1017 // System.out.println("No FilterName set.");
1018 return null;
1021 if (_xMSF == null)
1023 GlobalLogWriter.println("MultiServiceFactory not set.");
1024 return null;
1026 // XFilterFactory aFilterFactory = null;
1027 Object aObj = null;
1030 aObj = _xMSF.createInstance("com.sun.star.document.FilterFactory");
1032 catch(com.sun.star.uno.Exception e)
1034 GlobalLogWriter.println("Can't get com.sun.star.document.FilterFactory.");
1035 return null;
1037 if (aObj != null)
1039 XNameAccess aNameAccess = UnoRuntime.queryInterface(XNameAccess.class, aObj);
1040 if (aNameAccess != null)
1042 if (! aNameAccess.hasByName(_sFilterName))
1044 GlobalLogWriter.println("FilterFactory.hasByName() says there exist no '" + _sFilterName + "'" );
1045 return null;
1048 Object[] aElements = null;
1051 aElements = (Object[]) aNameAccess.getByName(_sFilterName);
1052 if (aElements != null)
1054 String sServiceName = null;
1055 // System.out.println("getByName().length: " + String.valueOf(aElements.length));
1056 for (int i=0;i<aElements.length; i++)
1058 PropertyValue aPropertyValue = (PropertyValue)aElements[i];
1059 if (aPropertyValue.Name.equals("DocumentService"))
1061 String sValue = (String)aPropertyValue.Value;
1062 // System.out.println("DocumentService: " + sValue);
1063 sServiceName = sValue;
1064 break;
1067 return sServiceName;
1069 else
1071 GlobalLogWriter.println("There are no elements for FilterName '" + _sFilterName + "'");
1072 return null;
1075 catch (com.sun.star.container.NoSuchElementException e)
1077 GlobalLogWriter.println("NoSuchElementException caught. " + e.getMessage());
1079 catch (com.sun.star.lang.WrappedTargetException e)
1081 GlobalLogWriter.println("WrappedTargetException caught. " + e.getMessage());
1085 return null;
1087 // -----------------------------------------------------------------------------
1089 public static String getFileExtension(String _sInternalFilterName, XMultiServiceFactory _xMSF)
1091 if (_sInternalFilterName.length() == 0)
1093 // System.out.println("No FilterName set.");
1094 return null;
1097 if (_xMSF == null)
1099 GlobalLogWriter.println("MultiServiceFactory not set.");
1100 return null;
1102 XTypeDetection aTypeDetection = null;
1105 Object oObj = _xMSF.createInstance("com.sun.star.document.TypeDetection");
1106 aTypeDetection = UnoRuntime.queryInterface(XTypeDetection.class, oObj);
1108 catch(com.sun.star.uno.Exception e)
1110 GlobalLogWriter.println("Can't get com.sun.star.document.TypeDetection.");
1111 return null;
1113 if (aTypeDetection != null)
1115 XNameAccess aNameAccess = UnoRuntime.queryInterface(XNameAccess.class, aTypeDetection);
1116 if (aNameAccess != null)
1119 // System.out.println("Show ElementNames" );
1120 // String[] aElementNames = aNameAccess.getElementNames();
1121 // for (int i = 0; i<aElementNames.length; i++)
1122 // {
1123 // System.out.println(aElementNames[i]);
1124 // }
1126 if (! aNameAccess.hasByName(_sInternalFilterName))
1128 GlobalLogWriter.println("TypeDetection.hasByName() says there exist no '" + _sInternalFilterName + "'" );
1129 return null;
1132 Object[] aElements = null;
1133 String[] aExtensions;
1136 aElements = (Object[]) aNameAccess.getByName(_sInternalFilterName);
1137 if (aElements != null)
1139 String sExtension = null;
1140 // System.out.println("getByName().length: " + String.valueOf(aElements.length));
1141 for (int i=0;i<aElements.length; i++)
1143 PropertyValue aPropertyValue = (PropertyValue)aElements[i];
1144 // System.out.println("PropertyValue.Name: " + aPropertyValue.Name);
1145 if (aPropertyValue.Name.equals("Extensions"))
1147 aExtensions = (String[])aPropertyValue.Value;
1148 GlobalLogWriter.println(" Possible extensions are: " + String.valueOf(aExtensions.length));
1149 if (aExtensions.length > 0)
1151 for (int j=0;j<aExtensions.length;j++)
1153 GlobalLogWriter.println(" " + aExtensions[j]);
1155 sExtension = aExtensions[0];
1156 GlobalLogWriter.println("");
1160 return sExtension;
1162 else
1164 GlobalLogWriter.println("There are no elements for FilterName '" + _sInternalFilterName + "'");
1165 return null;
1168 catch (com.sun.star.container.NoSuchElementException e)
1170 GlobalLogWriter.println("NoSuchElementException caught. " + e.getMessage());
1172 catch (com.sun.star.lang.WrappedTargetException e)
1174 GlobalLogWriter.println("WrappedTargetException caught. " + e.getMessage());
1178 return null;
1181 // -----------------------------------------------------------------------------
1182 public void convertDocument(String _sInputFile, String _sOutputPath, ParameterHelper _aGTA) throws OfficeException
1184 XMultiServiceFactory xMSF = _aGTA.getMultiServiceFactory();
1185 if (xMSF == null)
1187 GlobalLogWriter.println("MultiServiceFactory in GraphicalTestArgument not set.");
1188 return;
1191 String sInputURL = URLHelper.getFileURLFromSystemPath(_sInputFile);
1192 // showType(sInputURL, xMSF);
1193 XComponent aDoc = loadFromURL( _aGTA, sInputURL);
1194 if (aDoc == null)
1196 GlobalLogWriter.println("Can't load document '"+ sInputURL + "'");
1197 return;
1200 if (_sOutputPath == null)
1202 GlobalLogWriter.println("Outputpath not set.");
1203 return;
1206 if (! isStoreAllowed())
1208 GlobalLogWriter.println("It's not allowed to store, check Input/Output path.");
1209 return;
1211 // TODO: Do we need to wait?
1212 // TimeHelper.waitInSeconds(1, "wait after loadFromURL.");
1214 XServiceInfo xServiceInfo = UnoRuntime.queryInterface( XServiceInfo.class, aDoc );
1215 // String sFilter = getFilterName_forExcel(xServiceInfo);
1216 // System.out.println("Filter is " + sFilter);
1218 // store the document in an other directory
1219 XStorable xStorable = UnoRuntime.queryInterface( XStorable.class, aDoc);
1220 if (xStorable == null)
1222 GlobalLogWriter.println("com.sun.star.frame.XStorable is null");
1223 return;
1226 String sFilterName = _aGTA.getExportFilterName();
1228 // initialize PropertyArray
1229 // PropertyValue [] aStoreProps = new PropertyValue[ nPropertyCount ];
1230 // int nPropertyIndex = 0;
1231 ArrayList<PropertyValue> aPropertyList = new ArrayList<PropertyValue>();
1233 String sExtension = "";
1235 if (sFilterName != null && sFilterName.length() > 0)
1237 String sInternalFilterName = getInternalFilterName(sFilterName, xMSF);
1238 String sServiceName = getServiceNameFromFilterName(sFilterName, xMSF);
1240 GlobalLogWriter.println("Filter detection:");
1241 // check if service name from file filter is the same as from the loaded document
1242 boolean bServiceFailed = false;
1243 if (sServiceName == null || sInternalFilterName == null)
1245 GlobalLogWriter.println("Given FilterName '" + sFilterName + "' seems to be unknown.");
1246 bServiceFailed = true;
1248 if (! xServiceInfo.supportsService(sServiceName))
1250 GlobalLogWriter.println("Service from FilterName '" + sServiceName + "' is not supported by loaded document.");
1251 bServiceFailed = true;
1253 if (bServiceFailed == true)
1255 GlobalLogWriter.println("Please check '" + PropertyName.DOC_CONVERTER_EXPORT_FILTER_NAME + "' in the property file.");
1256 return;
1259 if (sInternalFilterName != null && sInternalFilterName.length() > 0)
1261 // get the FileExtension, by the filter name, if we don't get a file extension
1262 // we assume the is also no right filter name.
1263 sExtension = getFileExtension(sInternalFilterName, xMSF);
1264 if (sExtension == null)
1266 GlobalLogWriter.println("Can't found an extension for filtername, take it from the source.");
1270 PropertyValue Arg = new PropertyValue();
1271 Arg.Name = "FilterName";
1272 Arg.Value = sFilterName;
1273 // aStoreProps[nPropertyIndex ++] = Arg;
1274 aPropertyList.add(Arg);
1275 showProperty(Arg);
1276 GlobalLogWriter.println("FilterName is set to: " + sFilterName);
1279 String sOutputURL = "";
1282 // create the new filename with the extension, which is ok to the file format
1283 String sInputFileBasename = FileHelper.getBasename(_sInputFile);
1284 // System.out.println("InputFileBasename " + sInputFileBasename);
1285 String sInputFileNameNoSuffix = FileHelper.getNameNoSuffix(sInputFileBasename);
1286 // System.out.println("InputFilename no suffix " + sInputFileNameNoSuffix);
1287 String fs = System.getProperty("file.separator");
1288 String sOutputFile = _sOutputPath;
1289 if (! sOutputFile.endsWith(fs))
1291 sOutputFile += fs;
1293 if (sExtension != null && sExtension.length() > 0)
1295 sOutputFile += sInputFileNameNoSuffix + "." + sExtension;
1297 else
1299 sOutputFile += sInputFileBasename;
1302 if (FileHelper.exists(sOutputFile) && _aGTA.getOverwrite() == false)
1304 GlobalLogWriter.println("File already exist, don't overwrite. Set " + PropertyName.DOC_COMPARATOR_OVERWRITE_REFERENCE + "=true to force overwrite.");
1305 return;
1308 sOutputURL = URLHelper.getFileURLFromSystemPath(sOutputFile);
1310 GlobalLogWriter.println("Store document as '" + sOutputURL + "'");
1311 xStorable.storeAsURL(sOutputURL, PropertyHelper.createPropertyValueArrayFormArrayList(aPropertyList));
1312 GlobalLogWriter.println("Document stored.");
1314 catch (com.sun.star.io.IOException e)
1316 GlobalLogWriter.println("Can't store document '" + sOutputURL + "'. Message is :'" + e.getMessage() + "'");
1318 // TODO: Do we need to wait?
1319 // TimeHelper.waitInSeconds(1, "unknown in OfficePrint.convertDocument()");
1325 * @return false, if 'NoOffice=yes' is given
1327 // private boolean shouldOfficeStart()
1328 // {
1329 // String sNoOffice = (String)m_aParameterHelper.getTestParameters().get( "NoOffice" );
1330 // if (sNoOffice != null)
1331 // {
1332 // if (sNoOffice.toLowerCase().startsWith("t") || sNoOffice.toLowerCase().startsWith("y"))
1333 // {
1334 // return false;
1335 // }
1336 // }
1337 // return true;
1338 // }
1340 OfficeProvider m_aProvider = null;
1341 private void startOffice()
1343 // SimpleFileSemaphore aSemaphore = new SimpleFileSemaphore();
1344 // if (shouldOfficeStart())
1345 // {
1346 // if (OSHelper.isWindows())
1347 // {
1348 // aSemaphore.P(aSemaphore.getSemaphoreFile());
1349 // }
1350 m_aParameterHelper.getTestParameters().put(util.PropertyName.DONT_BACKUP_USERLAYER, Boolean.TRUE);
1352 m_aParameterHelper.getPerformance().startTime(PerformanceContainer.OfficeStart);
1353 m_aProvider = new OfficeProvider();
1354 XMultiServiceFactory xMSF = (XMultiServiceFactory) m_aProvider.getManager(m_aParameterHelper.getTestParameters());
1355 m_aParameterHelper.getTestParameters().put("ServiceFactory", xMSF);
1356 m_aParameterHelper.getPerformance().stopTime(PerformanceContainer.OfficeStart);
1358 long nStartTime = m_aParameterHelper.getPerformance().getTime(PerformanceContainer.OfficeStart);
1359 // aGTA = getParameterHelper(); // get new TestArguments
1360 m_aParameterHelper.getPerformance().setTime(PerformanceContainer.OfficeStart, nStartTime);
1361 // }
1363 // Watcher Object is need in log object to give a simple way to say if a running office is alive.
1364 // As long as a log comes, it pings the Watcher and says the office is alive, if not an
1365 // internal counter increase and at a given point (300 seconds) the office is killed.
1366 if (GlobalLogWriter.get().getWatcher() == null)
1368 GlobalLogWriter.println("Set office watcher");
1369 OfficeWatcher aWatcher = (OfficeWatcher)m_aParameterHelper.getTestParameters().get("Watcher");
1370 GlobalLogWriter.get().setWatcher(aWatcher);
1374 private void stopOffice()
1376 // Office shutdown
1377 if (m_aProvider != null)
1379 String sAppExecCmd = (String)m_aParameterHelper.getTestParameters().get("AppExecutionCommand");
1380 if (sAppExecCmd != null && sAppExecCmd.length() > 0)
1382 m_aProvider.closeExistingOffice(m_aParameterHelper.getTestParameters(), true);
1384 // if (OSHelper.isWindows())
1385 // {
1386 // aSemaphore.V(aSemaphore.getSemaphoreFile());
1387 // aSemaphore.sleep(2);
1388 // // wait some time maybe an other process will take the semaphore
1389 // // I know, this is absolutly dirty, but the whole convwatch is dirty and need a big cleanup.
1390 // }
1394 public void disallowStore()
1397 public void allowStore()
1400 public boolean isStoreAllowed()
1402 return false;
1403 // return m_bStoreFile;