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 .
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;
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
;
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
);
115 throw new OfficeException("unknown reference type");
117 GlobalLogWriter
.println("Close document.");
118 m_aDocument
.dispose();
122 public void start() throws OfficeException
127 public void close() throws OfficeException
136 private void showProperty(PropertyValue _aValue
)
138 String sName
= _aValue
.Name
;
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
+ "'";
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
182 * Be aware, the ownership of the document gets to you, you have to close it.
184 private XComponent
loadFromURL(ParameterHelper _aGTA
,
187 XComponent aDoc
= null;
190 if (_aGTA
.getMultiServiceFactory() == null)
192 GlobalLogWriter
.println("MultiServiceFactory in GraphicalTestArgument not set.");
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())
215 // nPropertyCount ++;
217 // if (_aGTA.getImportFilterName() != null && _aGTA.getImportFilterName().length() > 0)
219 // nPropertyCount ++;
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();
231 Arg
.Value
= Boolean
.TRUE
;
232 aPropertyList
.add(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
);
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
);
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.");
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("");
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());
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.");
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());
327 private String
getFilterName_forPDF(XServiceInfo xServiceInfo
)
329 String filterName
= "";
331 if (xServiceInfo
.supportsService("com.sun.star.text.TextDocument"))
334 filterName
= "writer_pdf_Export";
336 else if ( xServiceInfo
.supportsService( "com.sun.star.sheet.SpreadsheetDocument" ) )
339 filterName
= "calc_pdf_Export";
341 else if ( xServiceInfo
.supportsService( "com.sun.star.drawing.DrawingDocument" ) )
344 filterName
= "draw_pdf_Export";
346 else if ( xServiceInfo
.supportsService( "com.sun.star.presentation.PresentationDocument" ) )
349 filterName
= "impress_pdf_Export";
351 else if (xServiceInfo
.supportsService("com.sun.star.text.WebDocument"))
354 filterName
= "writer_web_pdf_Export";
356 else if ( xServiceInfo
.supportsService("com.sun.star.text.GlobalDocument") )
359 filterName
= "writer_globaldocument_pdf_Export";
361 else if ( xServiceInfo
.supportsService( "com.sun.star.formulaFormulaProperties" ) )
364 filterName
= "math_pdf_Export";
370 // -----------------------------------------------------------------------------
372 // public boolean storeAsPDF(ParameterHelper _aGTA,
373 // String _sInputURL,
374 // String _sOutputURL)
376 // boolean bBack = false;
377 // XComponent aDoc = loadFromURL(_aGTA, _sInputURL);
381 // GlobalLogWriter.println("Can't load document.");
384 // bBack = storeAsPDF(_aGTA, aDoc, _sOutputURL);
385 // FileHelper.createInfoFile(_sOutputURL, _aGTA, "as pdf");
387 // GlobalLogWriter.println("Close document.");
392 public boolean storeAsPDF(ParameterHelper _aGTA
,
394 String _sOutputURL
) throws OfficeException
397 boolean bBack
= true;
398 _aGTA
.getPerformance().startTime(PerformanceContainer
.StoreAsPDF
);
399 bBack
= exportToPDF(_aDoc
, _sOutputURL
);
400 _aGTA
.getPerformance().stopTime(PerformanceContainer
.StoreAsPDF
);
404 GlobalLogWriter
.println("Can't store document as PDF.");
406 throw new OfficeException("Can't store document as PDF");
410 FileHelper
.createInfoFile(_sOutputURL
, _aGTA
, "as pdf");
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)
432 // // waitInSeconds(1);
433 // boolean bBack = false;
435 // XComponent aDoc = loadFromURL(_aGTA, _sInputURL);
438 // if ( _sInputURL.equals(_sOutputURL) )
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.");
445 // bBack = impl_printToFileWithOOo(_aGTA, aDoc, _sOutputURL, _sPrintFileURL);
447 // GlobalLogWriter.println("Close document.");
452 // GlobalLogWriter.println("loadDocumentFromURL() failed with document: " + _sInputURL);
459 // -----------------------------------------------------------------------------
460 private boolean impl_printToFileWithOOo(ParameterHelper _aGTA
,
463 String _sPrintFileURL
)
465 boolean bBack
= false;
466 boolean bFailed
= true; // always be a pessimist,
469 GlobalLogWriter
.println("No document is given.");
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
);
496 GlobalLogWriter
.println("Reload stored file test failed, can't reload file: " + _sOutputURL
);
500 XCloseable xClose
= UnoRuntime
.queryInterface(XCloseable
.class, aDoc
);
509 // TimeHelper.waitInSeconds(1, "after close temp document");
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
);
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());
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");
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))
550 // // System.out.println(aPrinterProps[nPropIndex].Name);
553 // aPrinterProps[nPropIndex].Value = com.sun.star.view.PaperFormat.A4;
554 // aPrintable.setPrinter(aPrinterProps);
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();
568 Arg
.Value
= _aGTA
.getPrinterName();
569 aPropertyList
.add(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)
582 // // we don't want to print all pages, build Pages string by ourself
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
);
613 // generate pages string
614 if (_aGTA
.printAllPages() == false)
617 if (_aGTA
.getMaxPages() > 0)
619 sPages
= "1-" + String
.valueOf(_aGTA
.getMaxPages());
621 if (_aGTA
.getOnlyPages().length() != 0)
623 if (sPages
.length() != 0)
627 sPages
+= String
.valueOf(_aGTA
.getOnlyPages());
630 Arg
= new PropertyValue();
633 aPrintProps
.add(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;
648 PropertyValue
[] aPrinterProps
= aPrintable
.getPrinter();
650 while (!"IsBusy".equals(aPrinterProps
[nPropIndex
].Name
))
652 // System.out.println(aPrinterProps[nPropIndex].Name);
655 isBusy
= (aPrinterProps
[nPropIndex
].Value
== Boolean
.TRUE
) ?
true : false;
656 TimeHelper
.waitInSeconds(1, "is print ready?");
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.");
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
);
676 GlobalLogWriter
.println("Can't get XPrintable interface.");
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());
693 GlobalLogWriter
.println("convwatch.OfficePrint: FAILED");
697 GlobalLogWriter
.println("convwatch.OfficePrint: OK");
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.");
720 // String fs = System.getProperty("file.separator");
722 // String sInputFileURL = URLHelper.getFileURLFromSystemPath(_sAbsoluteInputFile);
724 String sInputFileBasename
= FileHelper
.getBasename(_sAbsoluteInputFile
);
725 // String sOutputFileURL = null;
727 if (_sAbsoluteOutputPath
!= null)
729 sOutputPath
= _sAbsoluteOutputPath
;
730 // FileHelper.makeDirectories("", sOutputPath);
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.");
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
763 // public static boolean buildReference(ParameterHelper _aGTA,
764 // String _sAbsoluteOutputPath,
765 // String _sAbsoluteInputFile)
766 // throws OfficeException
768 // if (! FileHelper.exists(_sAbsoluteInputFile))
770 // throw new OfficeException("buildReference(): Input file: " + _sAbsoluteInputFile + " does not exist.");
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)
782 // sOutputPath = _sAbsoluteOutputPath;
783 // FileHelper.makeDirectories("", sOutputPath);
787 // String sInputPath = FileHelper.getPath(_sAbsoluteInputFile);
788 // sOutputPath = sInputPath;
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)
799 // GlobalLogWriter.println("Reference already exist, don't overwrite. Set " + PropertyName.DOC_COMPARATOR_OVERWRITE_REFERENCE + "=true to force overwrite.");
803 // if (_aGTA.getReferenceType().toLowerCase().equals("msoffice"))
805 // sPrintFileURL = URLHelper.getFileURLFromSystemPath(sAbsolutePrintFilename);
807 // else if (_aGTA.getReferenceType().toLowerCase().equals("pdf"))
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 );
812 // else if (_aGTA.getReferenceType().toLowerCase().equals("ooo"))
814 // sPrintFileURL = URLHelper.getFileURLFromSystemPath(sAbsolutePrintFilename );
818 // GlobalLogWriter.println("OfficePrint.buildreference(): Unknown print type.");
821 // return printToFile(_aGTA, sInputFileURL, sOutputFileURL, sPrintFileURL);
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
834 // boolean bBack = false;
835 // String sPrintFileURL = null;
838 // // remember the current timer, to know how long a print process need.
841 // if (_aGTA.getReferenceType().toLowerCase().equals("ooo"))
843 // bBack = printToFileWithOOo(_aGTA, _sInputFileURL, _sOutputFileURL, _sPrintFileURL);
845 // else if (_aGTA.getReferenceType().toLowerCase().equals("pdf"))
847 // GlobalLogWriter.println("USE PDF AS EXPORT FORMAT.");
848 // bBack = storeAsPDF(_aGTA, _sInputFileURL, _sPrintFileURL);
850 // else if (_aGTA.getReferenceType().toLowerCase().equals("msoffice"))
852 // if (MSOfficePostscriptCreator.isMSOfficeDocumentFormat(_sInputFileURL))
854 // GlobalLogWriter.println("USE MSOFFICE AS EXPORT FORMAT.");
855 // MSOfficePostscriptCreator a = new MSOfficePostscriptCreator();
858 // a.printToFileWithMSOffice(_aGTA, FileHelper.getSystemPathFromFileURL(_sInputFileURL),
859 // FileHelper.getSystemPathFromFileURL(_sPrintFileURL));
861 // catch(OfficeException e)
863 // e.printStackTrace();
864 // GlobalLogWriter.println(e.getMessage());
865 // throw new OfficeException("Exception caught. Problem with MSOffice printer methods.");
867 // catch(java.io.IOException e)
869 // GlobalLogWriter.println(e.getMessage());
870 // throw new OfficeException("IOException caught. Problem with MSOffice printer methods.");
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);
882 // // System.out.println("");
883 // throw new OfficeException("OfficePrint.printToFile(): Unknown print type.");
888 // -----------------------------------------------------------------------------
889 // TODO: move this away!
890 // -----------------------------------------------------------------------------
891 void showType(String _sInputURL
, XMultiServiceFactory _xMSF
)
893 if (_sInputURL
.length() == 0)
900 GlobalLogWriter
.println("MultiServiceFactory not set.");
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.");
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.");
933 GlobalLogWriter
.println("MultiServiceFactory not set.");
936 // XFilterFactory aFilterFactory = 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.");
949 XNameAccess aNameAccess
= UnoRuntime
.queryInterface(XNameAccess
.class, aObj
);
950 if (aNameAccess
!= null)
953 // if (_sFilterName.toLowerCase().equals("help"))
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++)
959 // System.out.println(aElementNames[i]);
961 // System.out.println("Must quit.");
962 // System.out.exit(1);
965 if (! aNameAccess
.hasByName(_sFilterName
))
967 GlobalLogWriter
.println("FilterFactory.hasByName() says there exist no '" + _sFilterName
+ "'" );
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
;
994 GlobalLogWriter
.println("There are no elements for FilterName '" + _sFilterName
+ "'");
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());
1011 // -----------------------------------------------------------------------------
1013 String
getServiceNameFromFilterName(String _sFilterName
, XMultiServiceFactory _xMSF
)
1015 if (_sFilterName
.length() == 0)
1017 // System.out.println("No FilterName set.");
1023 GlobalLogWriter
.println("MultiServiceFactory not set.");
1026 // XFilterFactory aFilterFactory = 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.");
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
+ "'" );
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
;
1067 return sServiceName
;
1071 GlobalLogWriter
.println("There are no elements for FilterName '" + _sFilterName
+ "'");
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());
1087 // -----------------------------------------------------------------------------
1089 public static String
getFileExtension(String _sInternalFilterName
, XMultiServiceFactory _xMSF
)
1091 if (_sInternalFilterName
.length() == 0)
1093 // System.out.println("No FilterName set.");
1099 GlobalLogWriter
.println("MultiServiceFactory not set.");
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.");
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++)
1123 // System.out.println(aElementNames[i]);
1126 if (! aNameAccess
.hasByName(_sInternalFilterName
))
1128 GlobalLogWriter
.println("TypeDetection.hasByName() says there exist no '" + _sInternalFilterName
+ "'" );
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("");
1164 GlobalLogWriter
.println("There are no elements for FilterName '" + _sInternalFilterName
+ "'");
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());
1181 // -----------------------------------------------------------------------------
1182 public void convertDocument(String _sInputFile
, String _sOutputPath
, ParameterHelper _aGTA
) throws OfficeException
1184 XMultiServiceFactory xMSF
= _aGTA
.getMultiServiceFactory();
1187 GlobalLogWriter
.println("MultiServiceFactory in GraphicalTestArgument not set.");
1191 String sInputURL
= URLHelper
.getFileURLFromSystemPath(_sInputFile
);
1192 // showType(sInputURL, xMSF);
1193 XComponent aDoc
= loadFromURL( _aGTA
, sInputURL
);
1196 GlobalLogWriter
.println("Can't load document '"+ sInputURL
+ "'");
1200 if (_sOutputPath
== null)
1202 GlobalLogWriter
.println("Outputpath not set.");
1206 if (! isStoreAllowed())
1208 GlobalLogWriter
.println("It's not allowed to store, check Input/Output path.");
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");
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.");
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
);
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
))
1293 if (sExtension
!= null && sExtension
.length() > 0)
1295 sOutputFile
+= sInputFileNameNoSuffix
+ "." + sExtension
;
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.");
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()
1329 // String sNoOffice = (String)m_aParameterHelper.getTestParameters().get( "NoOffice" );
1330 // if (sNoOffice != null)
1332 // if (sNoOffice.toLowerCase().startsWith("t") || sNoOffice.toLowerCase().startsWith("y"))
1340 OfficeProvider m_aProvider
= null;
1341 private void startOffice()
1343 // SimpleFileSemaphore aSemaphore = new SimpleFileSemaphore();
1344 // if (shouldOfficeStart())
1346 // if (OSHelper.isWindows())
1348 // aSemaphore.P(aSemaphore.getSemaphoreFile());
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
);
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()
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())
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.
1394 public void disallowStore()
1397 public void allowStore()
1400 public boolean isStoreAllowed()
1403 // return m_bStoreFile;