merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / runner / graphical / OpenOfficePostscriptCreator.java
blob66ed2b6eff1ed4b41099a6fcc3f142ef9d33df92
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: OpenOfficePostscriptCreator.java,v $
10 * $Revision: 1.1.2.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package graphical;
33 import com.sun.star.frame.FrameSearchFlag;
34 import com.sun.star.util.XCloseable;
35 import helper.OfficeProvider;
36 import helper.OfficeWatcher;
37 import java.util.ArrayList;
39 import com.sun.star.uno.UnoRuntime;
40 import com.sun.star.lang.XMultiServiceFactory;
41 import com.sun.star.document.XTypeDetection;
42 import com.sun.star.container.XNameAccess;
43 import com.sun.star.frame.XDesktop;
44 import com.sun.star.beans.XPropertySet;
45 import com.sun.star.beans.PropertyValue;
46 import com.sun.star.frame.XComponentLoader;
47 import com.sun.star.lang.XComponent;
48 import com.sun.star.frame.XStorable;
49 import com.sun.star.view.XPrintable;
50 import com.sun.star.lang.XServiceInfo;
51 import com.sun.star.frame.XModel;
52 import com.sun.star.uno.AnyConverter;
54 import helper.URLHelper;
55 import helper.PropertyHelper;
56 import helper.OSHelper;
58 // import helper.Parameter;
59 import java.io.File;
61 /**
62 * This Object is to print a given document with OpenOffice.org / StarOffice
63 * over the normal printer driver
64 * or over it's pdf exporter
66 public class OpenOfficePostscriptCreator implements IOffice
68 private ParameterHelper m_aParameterHelper;
69 private String m_sOutputURL;
70 private String m_sBasename;
71 private String m_sDocumentName;
72 private XComponent m_aDocument;
74 public OpenOfficePostscriptCreator(ParameterHelper _aParam, String _sResult)
76 m_aParameterHelper = _aParam;
77 String sOutputURL = _sResult;
78 if (! sOutputURL.startsWith("file:"))
80 sOutputURL = URLHelper.getFileURLFromSystemPath(_sResult);
82 m_sOutputURL = sOutputURL;
83 m_aDocument = null;
87 public void load(String _sDocumentName) throws OfficeException
89 m_sDocumentName = _sDocumentName;
91 String sInputFileURL = URLHelper.getFileURLFromSystemPath(m_sDocumentName);
92 m_aDocument = loadFromURL(m_aParameterHelper, sInputFileURL);
93 if (m_aDocument == null)
95 GlobalLogWriter.get().println("loadDocumentFromURL() failed with document: " + sInputFileURL);
96 throw new OfficeException("load(): failed with document" + sInputFileURL);
99 m_sBasename = FileHelper.getBasename(m_sDocumentName);
102 public void storeAsPostscript() throws OfficeException
104 if (m_aDocument != null)
106 String sDocumentName = FileHelper.appendPath(m_sOutputURL, m_sBasename);
107 if (m_aParameterHelper.getReferenceType().toLowerCase().equals("ooo"))
109 String sPrintURL = sDocumentName + ".ps";
111 impl_printToFileWithOOo(m_aParameterHelper, m_aDocument, sDocumentName, sPrintURL /*_sPrintFileURL*/);
112 String sBasename = FileHelper.getBasename(sPrintURL);
113 FileHelper.addBasenameToIndex(m_sOutputURL, sBasename, "OOo", "postscript", m_sDocumentName);
115 else if (m_aParameterHelper.getReferenceType().toLowerCase().equals("pdf"))
117 String sPDFURL = sDocumentName + ".pdf";
118 storeAsPDF(m_aParameterHelper, m_aDocument, sPDFURL);
120 String sBasename = FileHelper.getBasename(sPDFURL);
121 FileHelper.addBasenameToIndex(m_sOutputURL, sBasename, "OOo", "pdf-export", m_sDocumentName);
123 else
125 throw new OfficeException("unknown reference type");
127 GlobalLogWriter.get().println("Close document.");
128 m_aDocument.dispose();
132 public void start() throws OfficeException
134 startOffice();
137 public void close() throws OfficeException
139 stopOffice();
146 private void showProperty(PropertyValue _aValue)
148 String sName = _aValue.Name;
149 String sValue;
152 sValue = AnyConverter.toString(_aValue.Value);
153 GlobalLogWriter.get().println("Property " + sName + ":=" + sValue);
155 catch (com.sun.star.lang.IllegalArgumentException e)
157 GlobalLogWriter.get().println("showProperty: can't convert a object to string.");
161 /**
162 * shows the FilterName and MediaType from the given XComponent
164 private String getDocumentType( XComponent _aDoc )
166 XModel xModel = (XModel) UnoRuntime.queryInterface( XModel.class, _aDoc);
167 PropertyValue[] aArgs = xModel.getArgs();
168 for (int i=0;i<aArgs.length;i++)
170 PropertyValue aValue = aArgs[i];
171 // System.out.print("Property: '" + aValue.Name);
172 // System.out.println("' := '" + aValue.Value + "'");
173 if (aValue.Name.equals("FilterName") ||
174 aValue.Name.equals("MediaType"))
176 String sNameValue = "'" + aValue.Name + "' := '" + aValue.Value + "'";
177 return sNameValue;
180 return "";
183 private void showDocumentType( XComponent _aDoc )
185 String sNameValue = getDocumentType(_aDoc);
186 GlobalLogWriter.get().println(" Property: '" + sNameValue);
189 * load a OpenOffice.org document from a given URL (_sInputURL)
190 * the ParameterHelper must contain a living MultiServiceFactory object
191 * or we crash here.
192 * Be aware, the ownership of the document gets to you, you have to close it.
194 private XComponent loadFromURL(ParameterHelper _aGTA,
195 String _sInputURL)
197 XComponent aDoc = null;
198 try
200 if (_aGTA.getMultiServiceFactory() == null)
202 GlobalLogWriter.get().println("MultiServiceFactory in GraphicalTestArgument not set.");
203 return null;
205 Object oDsk = _aGTA.getMultiServiceFactory().createInstance("com.sun.star.frame.Desktop");
206 XDesktop aDesktop = (XDesktop)UnoRuntime.queryInterface(XDesktop.class, oDsk);
208 if (aDesktop != null)
210 GlobalLogWriter.get().println("com.sun.star.frame.Desktop created.");
211 // String sInputURL = aCurrentParameter.sInputURL;
212 // String sOutputURL = aCurrentParameter.sOutputURL;
213 // String sPrintFileURL = aCurrentParameter.sPrintToFileURL;
214 // System.out.println(_sInputURL);
217 // set here the loadComponentFromURL() properties
218 // at the moment only 'Hidden' is set, so no window is opened at work
220 ArrayList aPropertyList = new ArrayList();
222 // check which properties should set and count it.
223 // if (_aGTA.isHidden())
224 // {
225 // nPropertyCount ++;
226 // }
227 // if (_aGTA.getImportFilterName() != null && _aGTA.getImportFilterName().length() > 0)
228 // {
229 // nPropertyCount ++;
230 // }
232 // initialize the propertyvalue
233 // int nPropertyIndex = 0;
234 // aProps = new PropertyValue[ nPropertyCount ];
236 // set all property values
237 if (_aGTA.isHidden())
239 PropertyValue Arg = new PropertyValue();
240 Arg.Name = "Hidden";
241 Arg.Value = Boolean.TRUE;
242 aPropertyList.add(Arg);
243 showProperty(Arg);
245 if (_aGTA.getImportFilterName() != null && _aGTA.getImportFilterName().length() > 0)
247 PropertyValue Arg = new PropertyValue();
248 Arg.Name = "FilterName";
249 Arg.Value = _aGTA.getImportFilterName();
250 aPropertyList.add(Arg);
251 showProperty(Arg);
253 PropertyValue ReadOnly = new PropertyValue();
254 ReadOnly.Name = "ReadOnly";
255 ReadOnly.Value = Boolean.TRUE;
256 aPropertyList.add(ReadOnly);
257 showProperty(ReadOnly);
259 GlobalLogWriter.get().println(DateHelper.getDateTimeForHumanreadableLog() + " Load document");
260 // GlobalLogWriter.get().flush();
262 XComponentLoader aCompLoader = (XComponentLoader) UnoRuntime.queryInterface( XComponentLoader.class, aDesktop);
264 // XComponent aDoc = null;
266 _aGTA.getPerformance().startTime(PerformanceContainer.Load);
267 aDoc = aCompLoader.loadComponentFromURL(_sInputURL, "_blank", FrameSearchFlag.ALL, PropertyHelper.createPropertyValueArrayFormArrayList(aPropertyList) );
268 _aGTA.getPerformance().stopTime(PerformanceContainer.Load);
269 if (aDoc != null)
271 GlobalLogWriter.get().println(DateHelper.getDateTimeForHumanreadableLog() + " Load document done.");
272 showDocumentType(aDoc);
273 _aGTA.setDocumentType(getDocumentType(aDoc));
275 else
277 GlobalLogWriter.get().println(" Load document failed.");
278 if (_aGTA.getImportFilterName() != null && _aGTA.getImportFilterName().length() > 0)
280 GlobalLogWriter.get().println(" Please check FilterName := '" + _aGTA.getImportFilterName() + "'");
282 GlobalLogWriter.get().println("");
285 else
287 GlobalLogWriter.get().println("com.sun.star.frame.Desktop failed.");
290 catch ( com.sun.star.uno.Exception e )
292 // Some exception occures.FAILED
293 GlobalLogWriter.get().println("UNO Exception caught.");
294 GlobalLogWriter.get().println("Message: " + e.getMessage());
295 e.printStackTrace();
296 aDoc = null;
298 return aDoc;
301 private boolean exportToPDF(XComponent _xComponent, String _sDestinationName)
303 XServiceInfo xServiceInfo =
304 (XServiceInfo) UnoRuntime.queryInterface(
305 XServiceInfo.class, _xComponent
308 ArrayList aPropertyList = new ArrayList();
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;
318 XStorable store =
319 (XStorable) UnoRuntime.queryInterface(
320 XStorable.class, _xComponent
322 store.storeToURL(_sDestinationName, PropertyHelper.createPropertyValueArrayFormArrayList(aPropertyList));
324 catch (com.sun.star.io.IOException e)
326 GlobalLogWriter.get().println("IO Exception caught.");
327 GlobalLogWriter.get().println("Message: " + e.getMessage());
328 bWorked = false;
331 return bWorked;
335 private String getFilterName_forPDF(XServiceInfo xServiceInfo)
337 String filterName = "";
339 if (xServiceInfo.supportsService("com.sun.star.text.TextDocument"))
341 //writer
342 filterName = "writer_pdf_Export";
344 else if ( xServiceInfo.supportsService( "com.sun.star.sheet.SpreadsheetDocument" ) )
346 //calc
347 filterName = "calc_pdf_Export";
349 else if ( xServiceInfo.supportsService( "com.sun.star.drawing.DrawingDocument" ) )
351 //draw
352 filterName = "draw_pdf_Export";
354 else if ( xServiceInfo.supportsService( "com.sun.star.presentation.PresentationDocument" ) )
356 //impress
357 filterName = "impress_pdf_Export";
359 else if (xServiceInfo.supportsService("com.sun.star.text.WebDocument"))
361 //html document
362 filterName = "writer_web_pdf_Export";
364 else if ( xServiceInfo.supportsService("com.sun.star.text.GlobalDocument") )
366 //master document
367 filterName = "writer_globaldocument_pdf_Export";
369 else if ( xServiceInfo.supportsService( "com.sun.star.formulaFormulaProperties" ) )
371 //math document
372 filterName = "math_pdf_Export";
375 return filterName;
378 // -----------------------------------------------------------------------------
380 // public boolean storeAsPDF(ParameterHelper _aGTA,
381 // String _sInputURL,
382 // String _sOutputURL)
383 // {
384 // boolean bBack = false;
385 // XComponent aDoc = loadFromURL(_aGTA, _sInputURL);
387 // if (aDoc == null)
388 // {
389 // GlobalLogWriter.get().println("Can't load document.");
390 // return bBack;
391 // }
392 // bBack = storeAsPDF(_aGTA, aDoc, _sOutputURL);
393 // FileHelper.createInfoFile(_sOutputURL, _aGTA, "as pdf");
395 // GlobalLogWriter.get().println("Close document.");
396 // aDoc.dispose();
397 // return bBack;
398 // }
400 public boolean storeAsPDF(ParameterHelper _aGTA,
401 XComponent _aDoc,
402 String _sOutputURL) throws OfficeException
404 // try {
405 boolean bBack = true;
406 _aGTA.getPerformance().startTime(PerformanceContainer.StoreAsPDF);
407 bBack = exportToPDF(_aDoc, _sOutputURL);
408 _aGTA.getPerformance().stopTime(PerformanceContainer.StoreAsPDF);
410 if (!bBack)
412 GlobalLogWriter.get().println("Can't store document as PDF.");
413 // bBack = false;
414 throw new OfficeException("Can't store document as PDF");
416 else
418 FileHelper.createInfoFile(_sOutputURL, _aGTA, "as pdf");
420 return bBack;
423 // -----------------------------------------------------------------------------
426 * print the document found in file (_sInputURL) to as postscript to file (_sPrintFileURL)
427 * Due to the fact we use a printer to convert the file to postscript, the default printer
428 * to create such postscript format must be installed, this is not tested here.
430 * @return true, if print has been done.
431 * Be careful, true means only print returns with no errors, to be sure print is really done
432 * check existance of _sPrintFileURL
435 // public boolean printToFileWithOOo(ParameterHelper _aGTA,
436 // String _sInputURL,
437 // String _sOutputURL,
438 // String _sPrintFileURL)
439 // {
440 // // waitInSeconds(1);
441 // boolean bBack = false;
443 // XComponent aDoc = loadFromURL(_aGTA, _sInputURL);
444 // if (aDoc != null)
445 // {
446 // if ( _sInputURL.equals(_sOutputURL) )
447 // {
448 // // don't store document
449 // // input and output are equal OR
450 // GlobalLogWriter.get().println("Warning: Inputpath and Outputpath are equal. Document will not stored again.");
451 // disallowStore();
452 // }
453 // bBack = impl_printToFileWithOOo(_aGTA, aDoc, _sOutputURL, _sPrintFileURL);
455 // GlobalLogWriter.get().println("Close document.");
456 // aDoc.dispose();
457 // }
458 // else
459 // {
460 // GlobalLogWriter.get().println("loadDocumentFromURL() failed with document: " + _sInputURL);
461 // }
462 // return bBack;
463 // }
467 // -----------------------------------------------------------------------------
468 private boolean impl_printToFileWithOOo(ParameterHelper _aGTA,
469 XComponent _aDoc,
470 String _sOutputURL,
471 String _sPrintFileURL)
473 boolean bBack = false;
474 boolean bFailed = true; // always be a pessimist,
475 if (_aDoc == null)
477 GlobalLogWriter.get().println("No document is given.");
478 return bBack;
481 try
483 if (_sOutputURL != null)
485 if (isStoreAllowed())
487 // store the document in an other directory
488 XStorable aStorable = (XStorable) UnoRuntime.queryInterface( XStorable.class, _aDoc);
489 if (aStorable != null)
491 PropertyValue [] szEmptyArgs = new PropertyValue [0];
493 GlobalLogWriter.get().println(DateHelper.getDateTimeForHumanreadableLog() + " Store document.");
494 _aGTA.getPerformance().startTime(PerformanceContainer.Store);
495 aStorable.storeAsURL(_sOutputURL, szEmptyArgs);
496 _aGTA.getPerformance().stopTime(PerformanceContainer.Store);
498 GlobalLogWriter.get().println(DateHelper.getDateTimeForHumanreadableLog() + " Store document done.");
499 TimeHelper.waitInSeconds(1, "After store as URL to:" + _sOutputURL);
500 GlobalLogWriter.get().println("Reload stored file test.");
501 XComponent aDoc = loadFromURL(_aGTA, _sOutputURL);
502 if (aDoc == null)
504 GlobalLogWriter.get().println("Reload stored file test failed, can't reload file: " + _sOutputURL);
506 else
508 XCloseable xClose = (XCloseable)UnoRuntime.queryInterface(XCloseable.class, aDoc);
509 if (xClose != null)
511 xClose.close(true);
513 else
515 aDoc.dispose();
517 TimeHelper.waitInSeconds(1, "after close temp document");
521 else
523 // make sure to create the directory in
524 String sOutputFilename = FileHelper.getSystemPathFromFileURL(_sOutputURL);
525 String sOutputPath = FileHelper.getPath(sOutputFilename);
526 File aFile = new File(sOutputPath);
527 aFile.mkdirs();
531 catch ( com.sun.star.uno.Exception e )
533 // Some exception occures.FAILED
534 GlobalLogWriter.get().println("UNO Exception caught.");
535 GlobalLogWriter.get().println("Message: " + e.getMessage());
537 e.printStackTrace();
538 bBack = false;
541 try
544 // System.out.println("Document loaded.");
545 // Change Pagesettings to DIN A4
547 GlobalLogWriter.get().println(DateHelper.getDateTimeForHumanreadableLog() + " Print document.");
548 XPrintable aPrintable = (XPrintable) UnoRuntime.queryInterface( XPrintable.class, _aDoc);
549 if (aPrintable != null)
551 // System.out.println(" Set PaperFormat to DIN A4");
552 // {
553 // PropertyValue[] aPrinterProps = aPrintable.getPrinter();
554 // System.out.println("PrinterProps size: " + String.valueOf(aPrinterProps.length));
555 // int nPropIndex = 0;
556 // while (!"PaperFormat".equals(aPrinterProps[nPropIndex].Name))
557 // {
558 // // System.out.println(aPrinterProps[nPropIndex].Name);
559 // nPropIndex++;
560 // }
561 // aPrinterProps[nPropIndex].Value = com.sun.star.view.PaperFormat.A4;
562 // aPrintable.setPrinter(aPrinterProps);
563 // }
565 // configure Office to allow to execute macos
567 // TODO: We need a possiblity to set the printer name also for StarOffice/OpenOffice
568 if (OSHelper.isWindows())
570 if (_aGTA.getPrinterName() != null)
572 ArrayList aPropertyList = new ArrayList();
573 // PropertyValue [] aPrintProps = new PropertyValue[1];
574 PropertyValue Arg = new PropertyValue();
575 Arg.Name = "Name";
576 Arg.Value = _aGTA.getPrinterName();
577 aPropertyList.add(Arg);
578 showProperty(Arg);
579 // GlobalLogWriter.get().println("Printername is not null, so set to " + _aGTA.getPrinterName());
580 aPrintable.setPrinter(PropertyHelper.createPropertyValueArrayFormArrayList(aPropertyList));
584 // set property values for XPrintable.print()
585 // more can be found at "http://api.openoffice.org/docs/common/ref/com/sun/star/view/PrintOptions.html"
587 // int nProperties = 1; // default for 'FileName' property
588 // if (_aGTA.printAllPages() == false)
589 // {
590 // // we don't want to print all pages, build Pages string by ourself
591 // nProperties ++;
592 // }
593 // int nPropsCount = 0;
595 // If we are a SpreadSheet (calc), we need to set PrintAllSheets property to 'true'
596 XServiceInfo xServiceInfo = (XServiceInfo) UnoRuntime.queryInterface( XServiceInfo.class, _aDoc );
597 if ( xServiceInfo.supportsService( "com.sun.star.sheet.SpreadsheetDocument" ) )
599 XMultiServiceFactory xMSF = _aGTA.getMultiServiceFactory();
600 Object aSettings = xMSF.createInstance( "com.sun.star.sheet.GlobalSheetSettings" );
601 if (aSettings != null)
603 XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, aSettings );
604 xPropSet.setPropertyValue( "PrintAllSheets", new Boolean( true ) );
608 ArrayList aPrintProps = new ArrayList();
609 GlobalLogWriter.get().println("Property FileName:=" + _sPrintFileURL);
611 // PropertyValue [] aPrintProps = new PropertyValue[nProperties];
612 PropertyValue Arg = new PropertyValue();
613 Arg.Name = "FileName";
614 Arg.Value = _sPrintFileURL;
615 // aPrintProps[nPropsCount ++] = Arg;
616 aPrintProps.add(Arg);
617 // showProperty(Arg);
620 // generate pages string
621 if (_aGTA.printAllPages() == false)
623 String sPages = "";
624 if (_aGTA.getMaxPages() > 0)
626 sPages = "1-" + String.valueOf(_aGTA.getMaxPages());
628 if (_aGTA.getOnlyPages().length() != 0)
630 if (sPages.length() != 0)
632 sPages += ";";
634 sPages += String.valueOf(_aGTA.getOnlyPages());
637 Arg = new PropertyValue();
638 Arg.Name = "Pages";
639 Arg.Value = sPages;
640 aPrintProps.add(Arg);
642 showProperty(Arg);
644 // GlobalLogWriter.get().println("Start printing.");
646 _aGTA.getPerformance().startTime(PerformanceContainer.Print);
647 aPrintable.print(PropertyHelper.createPropertyValueArrayFormArrayList(aPrintProps));
648 TimeHelper.waitInSeconds(1, "Start waiting for print ready.");
650 GlobalLogWriter.get().println("Wait until document is printed.");
651 boolean isBusy = true;
652 int nPrintCount = 0;
653 while (isBusy)
655 PropertyValue[] aPrinterProps = aPrintable.getPrinter();
656 int nPropIndex = 0;
657 while (!"IsBusy".equals(aPrinterProps[nPropIndex].Name))
659 // System.out.println(aPrinterProps[nPropIndex].Name);
660 nPropIndex++;
662 isBusy = (aPrinterProps[nPropIndex].Value == Boolean.TRUE) ? true : false;
663 TimeHelper.waitInSeconds(1, "is print ready?");
664 nPrintCount++;
665 if (nPrintCount > 3600)
667 // we will never wait >1h until print is ready!
668 GlobalLogWriter.get().println("ERROR: Cancel print due to too long wait.");
669 throw new com.sun.star.uno.Exception("Convwatch exception, wait too long for printing.");
672 _aGTA.getPerformance().stopTime(PerformanceContainer.Print);
673 GlobalLogWriter.get().println(DateHelper.getDateTimeForHumanreadableLog() + " Print document done.");
675 // Create a .info file near the printed '.ps' or '.prn' file.
676 FileHelper.createInfoFile(_sPrintFileURL, _aGTA);
678 else
680 GlobalLogWriter.get().println("Can't get XPrintable interface.");
682 bFailed = false;
683 bBack = true;
685 catch ( com.sun.star.uno.Exception e )
687 // Some exception occures.FAILED
688 GlobalLogWriter.get().println("UNO Exception caught.");
689 GlobalLogWriter.get().println("Message: " + e.getMessage());
691 e.printStackTrace();
692 bBack = false;
695 if (bFailed == true)
697 GlobalLogWriter.get().println("convwatch.OfficePrint: FAILED");
699 else
701 GlobalLogWriter.get().println("convwatch.OfficePrint: OK");
703 return bBack;
708 * @return true, if the reference (*.prrn file) based on given output path and given input path exist.
709 * If OVERWRITE_REFERENCE is set, always return false.
711 public boolean isReferenceExists(ParameterHelper _aGTA,
712 String _sAbsoluteOutputPath,
713 String _sAbsoluteInputFile)
715 if (! FileHelper.exists(_sAbsoluteInputFile))
717 // throw new ConvWatchCancelException("Input file: " + _sAbsoluteInputFile + " does not exist.");
718 return false;
721 // String fs = System.getProperty("file.separator");
723 // String sInputFileURL = URLHelper.getFileURLFromSystemPath(_sAbsoluteInputFile);
725 String sInputFileBasename = FileHelper.getBasename(_sAbsoluteInputFile);
726 // String sOutputFileURL = null;
727 String sOutputPath;
728 if (_sAbsoluteOutputPath != null)
730 sOutputPath = _sAbsoluteOutputPath;
731 // FileHelper.makeDirectories("", sOutputPath);
733 else
735 String sInputPath = FileHelper.getPath(_sAbsoluteInputFile);
736 sOutputPath = sInputPath;
738 // sOutputFileURL = URLHelper.getFileURLFromSystemPath(sOutputPath + fs + sInputFileBasename);
739 // sOutputFileURL = null;
741 String sPrintFilename = FileHelper.getNameNoSuffix(sInputFileBasename);
742 // String sPrintFileURL;
744 String sAbsolutePrintFilename = FileHelper.appendPath(sOutputPath, sPrintFilename + ".prn");
745 if (FileHelper.exists(sAbsolutePrintFilename) && _aGTA.getOverwrite() == false)
747 GlobalLogWriter.get().println("Reference already exist, don't overwrite. Set " + PropertyName.DOC_COMPARATOR_OVERWRITE_REFERENCE + "=true to force overwrite.");
748 return true;
750 return false;
753 // -----------------------------------------------------------------------------
755 * create a reference file
756 * _sAbsoluteInputPath contains the source file, if not exists, return with failure.
757 * _sAbsoluteOutputPath contains the destination, where the file will store after load with StarOffice/OpenOffice.org
758 * if is null, print only near the Input file path
759 * _sPrintType ".prn" Print input file with StarOffice/OpenOffice.org and the default printer as PostScript
761 * @param _aGTA
762 * @return
764 // public static boolean buildReference(ParameterHelper _aGTA,
765 // String _sAbsoluteOutputPath,
766 // String _sAbsoluteInputFile)
767 // throws OfficeException
768 // {
769 // if (! FileHelper.exists(_sAbsoluteInputFile))
770 // {
771 // throw new OfficeException("buildReference(): Input file: " + _sAbsoluteInputFile + " does not exist.");
772 // }
774 // String fs = System.getProperty("file.separator");
776 // String sInputFileURL = URLHelper.getFileURLFromSystemPath(_sAbsoluteInputFile);
778 // String sInputFileBasename = FileHelper.getBasename(_sAbsoluteInputFile);
779 // String sOutputFileURL = null;
780 // String sOutputPath;
781 // if (_sAbsoluteOutputPath != null)
782 // {
783 // sOutputPath = _sAbsoluteOutputPath;
784 // FileHelper.makeDirectories("", sOutputPath);
785 // }
786 // else
787 // {
788 // String sInputPath = FileHelper.getPath(_sAbsoluteInputFile);
789 // sOutputPath = sInputPath;
790 // }
791 // // sOutputFileURL = URLHelper.getFileURLFromSystemPath(sOutputPath + fs + sInputFileBasename);
792 // sOutputFileURL = null;
794 // String sPrintFilename = FileHelper.getNameNoSuffix(sInputFileBasename);
795 // String sPrintFileURL;
797 // String sAbsolutePrintFilename = sOutputPath + fs + sPrintFilename + ".prn";
798 // if (FileHelper.exists(sAbsolutePrintFilename) && _aGTA.getOverwrite() == false)
799 // {
800 // GlobalLogWriter.get().println("Reference already exist, don't overwrite. Set " + PropertyName.DOC_COMPARATOR_OVERWRITE_REFERENCE + "=true to force overwrite.");
801 // return true;
802 // }
804 // if (_aGTA.getReferenceType().toLowerCase().equals("msoffice"))
805 // {
806 // sPrintFileURL = URLHelper.getFileURLFromSystemPath(sAbsolutePrintFilename);
807 // }
808 // else if (_aGTA.getReferenceType().toLowerCase().equals("pdf"))
809 // {
810 //// TODO: If we rename the stored file to *.pdf, we have to be sure that we use *.pdf also as a available reference
811 // sPrintFileURL = URLHelper.getFileURLFromSystemPath(sAbsolutePrintFilename );
812 // }
813 // else if (_aGTA.getReferenceType().toLowerCase().equals("ooo"))
814 // {
815 // sPrintFileURL = URLHelper.getFileURLFromSystemPath(sAbsolutePrintFilename );
816 // }
817 // else
818 // {
819 // GlobalLogWriter.get().println("OfficePrint.buildreference(): Unknown print type.");
820 // return false;
821 // }
822 // return printToFile(_aGTA, sInputFileURL, sOutputFileURL, sPrintFileURL);
823 // }
827 // TODO: Das Teil muss hier raus!
830 // public static boolean printToFile(ParameterHelper _aGTA,
831 // String _sInputFileURL,
832 // String _sOutputFileURL,
833 // String _sPrintFileURL) throws OfficeException
834 // {
835 // boolean bBack = false;
836 // String sPrintFileURL = null;
839 // // remember the current timer, to know how long a print process need.
840 // // startTimer();
842 // if (_aGTA.getReferenceType().toLowerCase().equals("ooo"))
843 // {
844 // bBack = printToFileWithOOo(_aGTA, _sInputFileURL, _sOutputFileURL, _sPrintFileURL);
845 // }
846 // else if (_aGTA.getReferenceType().toLowerCase().equals("pdf"))
847 // {
848 // GlobalLogWriter.get().println("USE PDF AS EXPORT FORMAT.");
849 // bBack = storeAsPDF(_aGTA, _sInputFileURL, _sPrintFileURL);
850 // }
851 // else if (_aGTA.getReferenceType().toLowerCase().equals("msoffice"))
852 // {
853 // if (MSOfficePostscriptCreator.isMSOfficeDocumentFormat(_sInputFileURL))
854 // {
855 // GlobalLogWriter.get().println("USE MSOFFICE AS EXPORT FORMAT.");
856 // MSOfficePostscriptCreator a = new MSOfficePostscriptCreator();
857 // try
858 // {
859 // a.printToFileWithMSOffice(_aGTA, FileHelper.getSystemPathFromFileURL(_sInputFileURL),
860 // FileHelper.getSystemPathFromFileURL(_sPrintFileURL));
861 // }
862 // catch(OfficeException e)
863 // {
864 // e.printStackTrace();
865 // GlobalLogWriter.get().println(e.getMessage());
866 // throw new OfficeException("Exception caught. Problem with MSOffice printer methods.");
867 // }
868 // catch(java.io.IOException e)
869 // {
870 // GlobalLogWriter.get().println(e.getMessage());
871 // throw new OfficeException("IOException caught. Problem with MSOffice printer methods.");
872 // }
873 // bBack = true;
874 // }
875 // else
876 // {
877 // GlobalLogWriter.get().println("This document type is not recognized as MSOffice format, as default fallback StarOffice/OpenOffice.org instead is used.");
878 // bBack = printToFileWithOOo(_aGTA, _sInputFileURL, _sOutputFileURL, _sPrintFileURL);
879 // }
880 // }
881 // else
882 // {
883 // // System.out.println("");
884 // throw new OfficeException("OfficePrint.printToFile(): Unknown print type.");
885 // }
886 // return bBack;
887 // }
889 // -----------------------------------------------------------------------------
890 // TODO: move this away!
891 // -----------------------------------------------------------------------------
892 void showType(String _sInputURL, XMultiServiceFactory _xMSF)
894 if (_sInputURL.length() == 0)
896 return;
899 if (_xMSF == null)
901 GlobalLogWriter.get().println("MultiServiceFactory not set.");
902 return;
904 XTypeDetection aTypeDetection = null;
907 Object oObj = _xMSF.createInstance("com.sun.star.document.TypeDetection");
908 aTypeDetection = (XTypeDetection)UnoRuntime.queryInterface(XTypeDetection.class, oObj);
910 catch(com.sun.star.uno.Exception e)
912 GlobalLogWriter.get().println("Can't get com.sun.star.document.TypeDetection.");
913 return;
915 if (aTypeDetection != null)
917 String sType = aTypeDetection.queryTypeByURL(_sInputURL);
918 GlobalLogWriter.get().println("Type is: " + sType);
923 // -----------------------------------------------------------------------------
924 public String getInternalFilterName(String _sFilterName, XMultiServiceFactory _xMSF)
926 if (_sFilterName.length() == 0)
928 // System.out.println("No FilterName set.");
929 return null;
932 if (_xMSF == null)
934 GlobalLogWriter.get().println("MultiServiceFactory not set.");
935 return null;
937 // XFilterFactory aFilterFactory = null;
938 Object aObj = null;
941 aObj = _xMSF.createInstance("com.sun.star.document.FilterFactory");
943 catch(com.sun.star.uno.Exception e)
945 GlobalLogWriter.get().println("Can't get com.sun.star.document.FilterFactory.");
946 return null;
948 if (aObj != null)
950 XNameAccess aNameAccess = (XNameAccess)UnoRuntime.queryInterface(XNameAccess.class, aObj);
951 if (aNameAccess != null)
954 // if (_sFilterName.toLowerCase().equals("help"))
955 // {
956 // System.out.println("Show all possible ElementNames from current version." );
957 // String[] aElementNames = aNameAccess.getElementNames();
958 // for (int i = 0; i<aElementNames.length; i++)
959 // {
960 // System.out.println(aElementNames[i]);
961 // }
962 // System.out.println("Must quit.");
963 // System.out.exit(1);
964 // }
966 if (! aNameAccess.hasByName(_sFilterName))
968 GlobalLogWriter.get().println("FilterFactory.hasByName() says there exist no '" + _sFilterName + "'" );
969 return null;
972 Object[] aElements = null;
973 String[] aExtensions;
976 aElements = (Object[]) aNameAccess.getByName(_sFilterName);
977 if (aElements != null)
979 String sInternalFilterName = null;
980 // System.out.println("getByName().length: " + String.valueOf(aElements.length));
981 for (int i=0;i<aElements.length; i++)
983 PropertyValue aPropertyValue = (PropertyValue)aElements[i];
984 // System.out.println("PropertyValue.Name: " + aPropertyValue.Name);
985 if (aPropertyValue.Name.equals("Type"))
987 String sValue = (String)aPropertyValue.Value;
988 // System.out.println("Type: " + sValue);
989 sInternalFilterName = sValue;
992 return sInternalFilterName;
994 else
996 GlobalLogWriter.get().println("There are no elements for FilterName '" + _sFilterName + "'");
997 return null;
1000 catch (com.sun.star.container.NoSuchElementException e)
1002 GlobalLogWriter.get().println("NoSuchElementException caught. " + e.getMessage());
1004 catch (com.sun.star.lang.WrappedTargetException e)
1006 GlobalLogWriter.get().println("WrappedTargetException caught. " + e.getMessage());
1010 return null;
1013 // -----------------------------------------------------------------------------
1015 String getServiceNameFromFilterName(String _sFilterName, XMultiServiceFactory _xMSF)
1017 if (_sFilterName.length() == 0)
1019 // System.out.println("No FilterName set.");
1020 return null;
1023 if (_xMSF == null)
1025 GlobalLogWriter.get().println("MultiServiceFactory not set.");
1026 return null;
1028 // XFilterFactory aFilterFactory = null;
1029 Object aObj = null;
1032 aObj = _xMSF.createInstance("com.sun.star.document.FilterFactory");
1034 catch(com.sun.star.uno.Exception e)
1036 GlobalLogWriter.get().println("Can't get com.sun.star.document.FilterFactory.");
1037 return null;
1039 if (aObj != null)
1041 XNameAccess aNameAccess = (XNameAccess)UnoRuntime.queryInterface(XNameAccess.class, aObj);
1042 if (aNameAccess != null)
1044 if (! aNameAccess.hasByName(_sFilterName))
1046 GlobalLogWriter.get().println("FilterFactory.hasByName() says there exist no '" + _sFilterName + "'" );
1047 return null;
1050 Object[] aElements = null;
1051 String[] aExtensions;
1054 aElements = (Object[]) aNameAccess.getByName(_sFilterName);
1055 if (aElements != null)
1057 String sServiceName = null;
1058 // System.out.println("getByName().length: " + String.valueOf(aElements.length));
1059 for (int i=0;i<aElements.length; i++)
1061 PropertyValue aPropertyValue = (PropertyValue)aElements[i];
1062 if (aPropertyValue.Name.equals("DocumentService"))
1064 String sValue = (String)aPropertyValue.Value;
1065 // System.out.println("DocumentService: " + sValue);
1066 sServiceName = sValue;
1067 break;
1070 return sServiceName;
1072 else
1074 GlobalLogWriter.get().println("There are no elements for FilterName '" + _sFilterName + "'");
1075 return null;
1078 catch (com.sun.star.container.NoSuchElementException e)
1080 GlobalLogWriter.get().println("NoSuchElementException caught. " + e.getMessage());
1082 catch (com.sun.star.lang.WrappedTargetException e)
1084 GlobalLogWriter.get().println("WrappedTargetException caught. " + e.getMessage());
1088 return null;
1090 // -----------------------------------------------------------------------------
1092 public static String getFileExtension(String _sInternalFilterName, XMultiServiceFactory _xMSF)
1094 if (_sInternalFilterName.length() == 0)
1096 // System.out.println("No FilterName set.");
1097 return null;
1100 if (_xMSF == null)
1102 GlobalLogWriter.get().println("MultiServiceFactory not set.");
1103 return null;
1105 XTypeDetection aTypeDetection = null;
1108 Object oObj = _xMSF.createInstance("com.sun.star.document.TypeDetection");
1109 aTypeDetection = (XTypeDetection)UnoRuntime.queryInterface(XTypeDetection.class, oObj);
1111 catch(com.sun.star.uno.Exception e)
1113 GlobalLogWriter.get().println("Can't get com.sun.star.document.TypeDetection.");
1114 return null;
1116 if (aTypeDetection != null)
1118 XNameAccess aNameAccess = (XNameAccess)UnoRuntime.queryInterface(XNameAccess.class, aTypeDetection);
1119 if (aNameAccess != null)
1122 // System.out.println("Show ElementNames" );
1123 // String[] aElementNames = aNameAccess.getElementNames();
1124 // for (int i = 0; i<aElementNames.length; i++)
1125 // {
1126 // System.out.println(aElementNames[i]);
1127 // }
1129 if (! aNameAccess.hasByName(_sInternalFilterName))
1131 GlobalLogWriter.get().println("TypeDetection.hasByName() says there exist no '" + _sInternalFilterName + "'" );
1132 return null;
1135 Object[] aElements = null;
1136 String[] aExtensions;
1139 aElements = (Object[]) aNameAccess.getByName(_sInternalFilterName);
1140 if (aElements != null)
1142 String sExtension = null;
1143 // System.out.println("getByName().length: " + String.valueOf(aElements.length));
1144 for (int i=0;i<aElements.length; i++)
1146 PropertyValue aPropertyValue = (PropertyValue)aElements[i];
1147 // System.out.println("PropertyValue.Name: " + aPropertyValue.Name);
1148 if (aPropertyValue.Name.equals("Extensions"))
1150 aExtensions = (String[])aPropertyValue.Value;
1151 GlobalLogWriter.get().println(" Possible extensions are: " + String.valueOf(aExtensions.length));
1152 if (aExtensions.length > 0)
1154 for (int j=0;j<aExtensions.length;j++)
1156 GlobalLogWriter.get().println(" " + aExtensions[j]);
1158 sExtension = aExtensions[0];
1159 GlobalLogWriter.get().println("");
1163 return sExtension;
1165 else
1167 GlobalLogWriter.get().println("There are no elements for FilterName '" + _sInternalFilterName + "'");
1168 return null;
1171 catch (com.sun.star.container.NoSuchElementException e)
1173 GlobalLogWriter.get().println("NoSuchElementException caught. " + e.getMessage());
1175 catch (com.sun.star.lang.WrappedTargetException e)
1177 GlobalLogWriter.get().println("WrappedTargetException caught. " + e.getMessage());
1181 return null;
1184 // -----------------------------------------------------------------------------
1185 public void convertDocument(String _sInputFile, String _sOutputPath, ParameterHelper _aGTA) throws OfficeException
1187 XMultiServiceFactory xMSF = _aGTA.getMultiServiceFactory();
1188 if (xMSF == null)
1190 GlobalLogWriter.get().println("MultiServiceFactory in GraphicalTestArgument not set.");
1191 return;
1194 String sInputURL = URLHelper.getFileURLFromSystemPath(_sInputFile);
1195 // showType(sInputURL, xMSF);
1196 XComponent aDoc = loadFromURL( _aGTA, sInputURL);
1197 if (aDoc == null)
1199 GlobalLogWriter.get().println("Can't load document '"+ sInputURL + "'");
1200 return;
1203 if (_sOutputPath == null)
1205 GlobalLogWriter.get().println("Outputpath not set.");
1206 return;
1209 if (! isStoreAllowed())
1211 GlobalLogWriter.get().println("It's not allowed to store, check Input/Output path.");
1212 return;
1214 // TODO: Do we need to wait?
1215 // TimeHelper.waitInSeconds(1, "wait after loadFromURL.");
1217 XServiceInfo xServiceInfo = (XServiceInfo) UnoRuntime.queryInterface( XServiceInfo.class, aDoc );
1218 // String sFilter = getFilterName_forExcel(xServiceInfo);
1219 // System.out.println("Filter is " + sFilter);
1221 // store the document in an other directory
1222 XStorable xStorable = (XStorable) UnoRuntime.queryInterface( XStorable.class, aDoc);
1223 if (xStorable == null)
1225 GlobalLogWriter.get().println("com.sun.star.frame.XStorable is null");
1226 return;
1229 String sFilterName = _aGTA.getExportFilterName();
1231 // check how many Properties should initialize
1232 int nPropertyCount = 0;
1233 // if (sFilterName != null && sFilterName.length() > 0)
1234 // {
1235 // nPropertyCount ++;
1236 // }
1238 // initialize PropertyArray
1239 // PropertyValue [] aStoreProps = new PropertyValue[ nPropertyCount ];
1240 // int nPropertyIndex = 0;
1241 ArrayList aPropertyList = new ArrayList();
1243 String sExtension = "";
1245 if (sFilterName != null && sFilterName.length() > 0)
1247 String sInternalFilterName = getInternalFilterName(sFilterName, xMSF);
1248 String sServiceName = getServiceNameFromFilterName(sFilterName, xMSF);
1250 GlobalLogWriter.get().println("Filter detection:");
1251 // check if service name from file filter is the same as from the loaded document
1252 boolean bServiceFailed = false;
1253 if (sServiceName == null || sInternalFilterName == null)
1255 GlobalLogWriter.get().println("Given FilterName '" + sFilterName + "' seems to be unknown.");
1256 bServiceFailed = true;
1258 if (! xServiceInfo.supportsService(sServiceName))
1260 GlobalLogWriter.get().println("Service from FilterName '" + sServiceName + "' is not supported by loaded document.");
1261 bServiceFailed = true;
1263 if (bServiceFailed == true)
1265 GlobalLogWriter.get().println("Please check '" + PropertyName.DOC_CONVERTER_EXPORT_FILTER_NAME + "' in the property file.");
1266 return;
1269 if (sInternalFilterName != null && sInternalFilterName.length() > 0)
1271 // get the FileExtension, by the filter name, if we don't get a file extension
1272 // we assume the is also no right filter name.
1273 sExtension = getFileExtension(sInternalFilterName, xMSF);
1274 if (sExtension == null)
1276 GlobalLogWriter.get().println("Can't found an extension for filtername, take it from the source.");
1280 PropertyValue Arg = new PropertyValue();
1281 Arg.Name = "FilterName";
1282 Arg.Value = sFilterName;
1283 // aStoreProps[nPropertyIndex ++] = Arg;
1284 aPropertyList.add(Arg);
1285 showProperty(Arg);
1286 GlobalLogWriter.get().println("FilterName is set to: " + sFilterName);
1289 String sOutputURL = "";
1292 // create the new filename with the extension, which is ok to the file format
1293 String sInputFileBasename = FileHelper.getBasename(_sInputFile);
1294 // System.out.println("InputFileBasename " + sInputFileBasename);
1295 String sInputFileNameNoSuffix = FileHelper.getNameNoSuffix(sInputFileBasename);
1296 // System.out.println("InputFilename no suffix " + sInputFileNameNoSuffix);
1297 String fs = System.getProperty("file.separator");
1298 String sOutputFile = _sOutputPath;
1299 if (! sOutputFile.endsWith(fs))
1301 sOutputFile += fs;
1303 if (sExtension != null && sExtension.length() > 0)
1305 sOutputFile += sInputFileNameNoSuffix + "." + sExtension;
1307 else
1309 sOutputFile += sInputFileBasename;
1312 if (FileHelper.exists(sOutputFile) && _aGTA.getOverwrite() == false)
1314 GlobalLogWriter.get().println("File already exist, don't overwrite. Set " + PropertyName.DOC_COMPARATOR_OVERWRITE_REFERENCE + "=true to force overwrite.");
1315 return;
1318 sOutputURL = URLHelper.getFileURLFromSystemPath(sOutputFile);
1320 GlobalLogWriter.get().println("Store document as '" + sOutputURL + "'");
1321 xStorable.storeAsURL(sOutputURL, PropertyHelper.createPropertyValueArrayFormArrayList(aPropertyList));
1322 GlobalLogWriter.get().println("Document stored.");
1324 catch (com.sun.star.io.IOException e)
1326 GlobalLogWriter.get().println("Can't store document '" + sOutputURL + "'. Message is :'" + e.getMessage() + "'");
1328 // TODO: Do we need to wait?
1329 // TimeHelper.waitInSeconds(1, "unknown in OfficePrint.convertDocument()");
1334 private boolean shouldOfficeStart()
1336 String sNoOffice = (String)m_aParameterHelper.getTestParameters().get( "NoOffice" );
1337 if (sNoOffice != null)
1339 if (sNoOffice.toLowerCase().startsWith("t") || sNoOffice.toLowerCase().startsWith("y"))
1341 return false;
1344 return true;
1347 OfficeProvider m_aProvider = null;
1348 private void startOffice()
1350 // SimpleFileSemaphore aSemaphore = new SimpleFileSemaphore();
1351 // if (shouldOfficeStart())
1352 // {
1353 // if (OSHelper.isWindows())
1354 // {
1355 // aSemaphore.P(aSemaphore.getSemaphoreFile());
1356 // }
1357 m_aParameterHelper.getTestParameters().put(util.PropertyName.DONT_BACKUP_USERLAYER, Boolean.TRUE);
1359 m_aParameterHelper.getPerformance().startTime(PerformanceContainer.OfficeStart);
1360 m_aProvider = new OfficeProvider();
1361 XMultiServiceFactory xMSF = (XMultiServiceFactory) m_aProvider.getManager(m_aParameterHelper.getTestParameters());
1362 m_aParameterHelper.getTestParameters().put("ServiceFactory", xMSF);
1363 m_aParameterHelper.getPerformance().stopTime(PerformanceContainer.OfficeStart);
1365 long nStartTime = m_aParameterHelper.getPerformance().getTime(PerformanceContainer.OfficeStart);
1366 // aGTA = getParameterHelper(); // get new TestArguments
1367 m_aParameterHelper.getPerformance().setTime(PerformanceContainer.OfficeStart, nStartTime);
1368 // }
1370 // Watcher Object is need in log object to give a simple way to say if a running office is alive.
1371 // As long as a log comes, it pings the Watcher and says the office is alive, if not an
1372 // internal counter increase and at a given point (300 seconds) the office is killed.
1373 GlobalLogWriter.get().println("Set office watcher");
1374 if (GlobalLogWriter.get().getWatcher() == null)
1376 OfficeWatcher aWatcher = (OfficeWatcher)m_aParameterHelper.getTestParameters().get("Watcher");
1377 GlobalLogWriter.get().setWatcher(aWatcher);
1381 private void stopOffice()
1383 // Office shutdown
1384 if (m_aProvider != null)
1386 m_aProvider.closeExistingOffice(m_aParameterHelper.getTestParameters(), true);
1387 // if (OSHelper.isWindows())
1388 // {
1389 // aSemaphore.V(aSemaphore.getSemaphoreFile());
1390 // aSemaphore.sleep(2);
1391 // // wait some time maybe an other process will take the semaphore
1392 // // I know, this is absolutly dirty, but the whole convwatch is dirty and need a big cleanup.
1393 // }
1397 private boolean m_bStoreFile;
1398 public void disallowStore()
1400 m_bStoreFile = false;
1402 public void allowStore()
1404 m_bStoreFile = true;
1406 public boolean isStoreAllowed()
1408 return false;
1409 // return m_bStoreFile;