1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
30 import com
.sun
.star
.frame
.FrameSearchFlag
;
31 import com
.sun
.star
.util
.XCloseable
;
32 import helper
.OfficeProvider
;
33 import helper
.OfficeWatcher
;
34 import java
.util
.ArrayList
;
36 import com
.sun
.star
.uno
.UnoRuntime
;
37 import com
.sun
.star
.lang
.XMultiServiceFactory
;
38 import com
.sun
.star
.document
.XTypeDetection
;
39 import com
.sun
.star
.container
.XNameAccess
;
40 import com
.sun
.star
.frame
.XDesktop
;
41 import com
.sun
.star
.beans
.XPropertySet
;
42 import com
.sun
.star
.beans
.PropertyValue
;
43 import com
.sun
.star
.frame
.XComponentLoader
;
44 import com
.sun
.star
.lang
.XComponent
;
45 import com
.sun
.star
.frame
.XStorable
;
46 import com
.sun
.star
.view
.XPrintable
;
47 import com
.sun
.star
.lang
.XServiceInfo
;
48 import com
.sun
.star
.frame
.XModel
;
49 import com
.sun
.star
.uno
.AnyConverter
;
51 import helper
.URLHelper
;
52 import helper
.PropertyHelper
;
53 import helper
.OSHelper
;
55 // import helper.Parameter;
59 * This Object is to print a given document with OpenOffice.org / StarOffice
60 * over the normal printer driver
61 * or over it's pdf exporter
63 public class OpenOfficePostscriptCreator
implements IOffice
65 private ParameterHelper m_aParameterHelper
;
66 private String m_sOutputURL
;
67 private String m_sBasename
;
68 private String m_sDocumentName
;
69 private XComponent m_aDocument
;
71 public OpenOfficePostscriptCreator(ParameterHelper _aParam
, String _sResult
)
73 m_aParameterHelper
= _aParam
;
74 String sOutputURL
= _sResult
;
75 if (! sOutputURL
.startsWith("file:"))
77 sOutputURL
= URLHelper
.getFileURLFromSystemPath(_sResult
);
79 m_sOutputURL
= sOutputURL
;
84 public void load(String _sDocumentName
) throws OfficeException
86 m_sDocumentName
= _sDocumentName
;
88 String sInputFileURL
= URLHelper
.getFileURLFromSystemPath(m_sDocumentName
);
89 m_aDocument
= loadFromURL(m_aParameterHelper
, sInputFileURL
);
90 if (m_aDocument
== null)
92 GlobalLogWriter
.println("loadDocumentFromURL() failed with document: " + sInputFileURL
);
93 throw new OfficeException("load(): failed with document" + sInputFileURL
);
96 m_sBasename
= FileHelper
.getBasename(m_sDocumentName
);
99 public void storeAsPostscript() throws OfficeException
101 if (m_aDocument
!= null)
103 String sDocumentName
= FileHelper
.appendPath(m_sOutputURL
, m_sBasename
);
104 if (m_aParameterHelper
.getReferenceType().toLowerCase().equals("ooo") ||
105 m_aParameterHelper
.getReferenceType().toLowerCase().equals("o3") ||
106 m_aParameterHelper
.getReferenceType().toLowerCase().equals("ps") )
108 String sPrintURL
= sDocumentName
+ ".ps";
110 impl_printToFileWithOOo(m_aParameterHelper
, m_aDocument
, sDocumentName
, sPrintURL
/*_sPrintFileURL*/);
111 String sBasename
= FileHelper
.getBasename(sPrintURL
);
112 FileHelper
.addBasenameToIndex(m_sOutputURL
, sBasename
, "OOo", "postscript", m_sDocumentName
);
114 else if (m_aParameterHelper
.getReferenceType().toLowerCase().equals("pdf"))
116 String sPDFURL
= sDocumentName
+ ".pdf";
117 storeAsPDF(m_aParameterHelper
, m_aDocument
, sPDFURL
);
119 String sBasename
= FileHelper
.getBasename(sPDFURL
);
120 FileHelper
.addBasenameToIndex(m_sOutputURL
, sBasename
, "pdf", "pdf-export", m_sDocumentName
);
124 throw new OfficeException("unknown reference type");
126 GlobalLogWriter
.println("Close document.");
127 m_aDocument
.dispose();
131 public void start() throws OfficeException
136 public void close() throws OfficeException
145 private void showProperty(PropertyValue _aValue
)
147 String sName
= _aValue
.Name
;
151 sValue
= AnyConverter
.toString(_aValue
.Value
);
152 GlobalLogWriter
.println("Property " + sName
+ ":=" + sValue
);
154 catch (com
.sun
.star
.lang
.IllegalArgumentException e
)
156 GlobalLogWriter
.println("showProperty: can't convert a object to string. " + e
.getMessage());
161 * shows the FilterName and MediaType from the given XComponent
163 private String
getDocumentType( XComponent _aDoc
)
165 XModel xModel
= UnoRuntime
.queryInterface( XModel
.class, _aDoc
);
166 PropertyValue
[] aArgs
= xModel
.getArgs();
167 for (int i
=0;i
<aArgs
.length
;i
++)
169 PropertyValue aValue
= aArgs
[i
];
170 // System.out.print("Property: '" + aValue.Name);
171 // System.out.println("' := '" + aValue.Value + "'");
172 if (aValue
.Name
.equals("FilterName") ||
173 aValue
.Name
.equals("MediaType"))
175 String sNameValue
= "'" + aValue
.Name
+ "' := '" + aValue
.Value
+ "'";
182 private void showDocumentType( XComponent _aDoc
)
184 String sNameValue
= getDocumentType(_aDoc
);
185 GlobalLogWriter
.println(" Property: '" + sNameValue
);
188 * load a OpenOffice.org document from a given URL (_sInputURL)
189 * the ParameterHelper must contain a living MultiServiceFactory object
191 * Be aware, the ownership of the document gets to you, you have to close it.
193 private XComponent
loadFromURL(ParameterHelper _aGTA
,
196 XComponent aDoc
= null;
199 if (_aGTA
.getMultiServiceFactory() == null)
201 GlobalLogWriter
.println("MultiServiceFactory in GraphicalTestArgument not set.");
204 Object oDsk
= _aGTA
.getMultiServiceFactory().createInstance("com.sun.star.frame.Desktop");
205 XDesktop aDesktop
= UnoRuntime
.queryInterface(XDesktop
.class, oDsk
);
207 if (aDesktop
!= null)
209 GlobalLogWriter
.println("com.sun.star.frame.Desktop created.");
210 // String sInputURL = aCurrentParameter.sInputURL;
211 // String sOutputURL = aCurrentParameter.sOutputURL;
212 // String sPrintFileURL = aCurrentParameter.sPrintToFileURL;
213 // System.out.println(_sInputURL);
216 // set here the loadComponentFromURL() properties
217 // at the moment only 'Hidden' is set, so no window is opened at work
219 ArrayList
<PropertyValue
> aPropertyList
= new ArrayList
<PropertyValue
>();
221 // check which properties should set and count it.
222 // if (_aGTA.isHidden())
224 // nPropertyCount ++;
226 // if (_aGTA.getImportFilterName() != null && _aGTA.getImportFilterName().length() > 0)
228 // nPropertyCount ++;
231 // initialize the propertyvalue
232 // int nPropertyIndex = 0;
233 // aProps = new PropertyValue[ nPropertyCount ];
235 // set all property values
236 if (_aGTA
.isHidden())
238 PropertyValue Arg
= new PropertyValue();
240 Arg
.Value
= Boolean
.TRUE
;
241 aPropertyList
.add(Arg
);
244 if (_aGTA
.getImportFilterName() != null && _aGTA
.getImportFilterName().length() > 0)
246 PropertyValue Arg
= new PropertyValue();
247 Arg
.Name
= "FilterName";
248 Arg
.Value
= _aGTA
.getImportFilterName();
249 aPropertyList
.add(Arg
);
252 PropertyValue ReadOnly
= new PropertyValue();
253 ReadOnly
.Name
= "ReadOnly";
254 ReadOnly
.Value
= Boolean
.TRUE
;
255 aPropertyList
.add(ReadOnly
);
256 showProperty(ReadOnly
);
258 GlobalLogWriter
.println(DateHelper
.getDateTimeForHumanreadableLog() + " Load document");
259 // GlobalLogWriter.flush();
261 XComponentLoader aCompLoader
= UnoRuntime
.queryInterface( XComponentLoader
.class, aDesktop
);
263 // XComponent aDoc = null;
265 _aGTA
.getPerformance().startTime(PerformanceContainer
.Load
);
266 aDoc
= aCompLoader
.loadComponentFromURL(_sInputURL
, "_blank", FrameSearchFlag
.ALL
, PropertyHelper
.createPropertyValueArrayFormArrayList(aPropertyList
) );
267 _aGTA
.getPerformance().stopTime(PerformanceContainer
.Load
);
270 GlobalLogWriter
.println(DateHelper
.getDateTimeForHumanreadableLog() + " Load document done.");
271 showDocumentType(aDoc
);
272 _aGTA
.setDocumentType(getDocumentType(aDoc
));
273 // TODO: TimeHelper.waitInSeconds(20, "Wait after load document. Maybe helps due to layouting problems.");
277 GlobalLogWriter
.println(" Load document failed.");
278 if (_aGTA
.getImportFilterName() != null && _aGTA
.getImportFilterName().length() > 0)
280 GlobalLogWriter
.println(" Please check FilterName := '" + _aGTA
.getImportFilterName() + "'");
282 GlobalLogWriter
.println("");
287 GlobalLogWriter
.println("com.sun.star.frame.Desktop failed.");
290 catch ( com
.sun
.star
.uno
.Exception e
)
292 // Some exception occures.FAILED
293 GlobalLogWriter
.println("UNO Exception caught.");
294 GlobalLogWriter
.println("Message: " + e
.getMessage());
301 private boolean exportToPDF(XComponent _xComponent
, String _sDestinationName
)
303 XServiceInfo xServiceInfo
=
304 UnoRuntime
.queryInterface(
305 XServiceInfo
.class, _xComponent
308 ArrayList
<PropertyValue
> aPropertyList
= new ArrayList
<PropertyValue
>();
309 PropertyValue aFiltername
= new PropertyValue();
310 aFiltername
.Name
= "FilterName";
311 aFiltername
.Value
= getFilterName_forPDF(xServiceInfo
);
312 aPropertyList
.add(aFiltername
);
313 showProperty(aFiltername
);
314 boolean bWorked
= true;
316 // TODO: TimeHelper.waitInSeconds(20, "Wait before storeToURL. Maybe helps due to layouting problems.");
320 UnoRuntime
.queryInterface(
321 XStorable
.class, _xComponent
323 store
.storeToURL(_sDestinationName
, PropertyHelper
.createPropertyValueArrayFormArrayList(aPropertyList
));
325 catch (com
.sun
.star
.io
.IOException e
)
327 GlobalLogWriter
.println("IO Exception caught.");
328 GlobalLogWriter
.println("Message: " + e
.getMessage());
336 private String
getFilterName_forPDF(XServiceInfo xServiceInfo
)
338 String filterName
= "";
340 if (xServiceInfo
.supportsService("com.sun.star.text.TextDocument"))
343 filterName
= "writer_pdf_Export";
345 else if ( xServiceInfo
.supportsService( "com.sun.star.sheet.SpreadsheetDocument" ) )
348 filterName
= "calc_pdf_Export";
350 else if ( xServiceInfo
.supportsService( "com.sun.star.drawing.DrawingDocument" ) )
353 filterName
= "draw_pdf_Export";
355 else if ( xServiceInfo
.supportsService( "com.sun.star.presentation.PresentationDocument" ) )
358 filterName
= "impress_pdf_Export";
360 else if (xServiceInfo
.supportsService("com.sun.star.text.WebDocument"))
363 filterName
= "writer_web_pdf_Export";
365 else if ( xServiceInfo
.supportsService("com.sun.star.text.GlobalDocument") )
368 filterName
= "writer_globaldocument_pdf_Export";
370 else if ( xServiceInfo
.supportsService( "com.sun.star.formulaFormulaProperties" ) )
373 filterName
= "math_pdf_Export";
379 // -----------------------------------------------------------------------------
381 // public boolean storeAsPDF(ParameterHelper _aGTA,
382 // String _sInputURL,
383 // String _sOutputURL)
385 // boolean bBack = false;
386 // XComponent aDoc = loadFromURL(_aGTA, _sInputURL);
390 // GlobalLogWriter.println("Can't load document.");
393 // bBack = storeAsPDF(_aGTA, aDoc, _sOutputURL);
394 // FileHelper.createInfoFile(_sOutputURL, _aGTA, "as pdf");
396 // GlobalLogWriter.println("Close document.");
401 public boolean storeAsPDF(ParameterHelper _aGTA
,
403 String _sOutputURL
) throws OfficeException
406 boolean bBack
= true;
407 _aGTA
.getPerformance().startTime(PerformanceContainer
.StoreAsPDF
);
408 bBack
= exportToPDF(_aDoc
, _sOutputURL
);
409 _aGTA
.getPerformance().stopTime(PerformanceContainer
.StoreAsPDF
);
413 GlobalLogWriter
.println("Can't store document as PDF.");
415 throw new OfficeException("Can't store document as PDF");
419 FileHelper
.createInfoFile(_sOutputURL
, _aGTA
, "as pdf");
424 // -----------------------------------------------------------------------------
427 * print the document found in file (_sInputURL) to as postscript to file (_sPrintFileURL)
428 * Due to the fact we use a printer to convert the file to postscript, the default printer
429 * to create such postscript format must be installed, this is not tested here.
431 * @return true, if print has been done.
432 * Be careful, true means only print returns with no errors, to be sure print is really done
433 * check existance of _sPrintFileURL
436 // public boolean printToFileWithOOo(ParameterHelper _aGTA,
437 // String _sInputURL,
438 // String _sOutputURL,
439 // String _sPrintFileURL)
441 // // waitInSeconds(1);
442 // boolean bBack = false;
444 // XComponent aDoc = loadFromURL(_aGTA, _sInputURL);
447 // if ( _sInputURL.equals(_sOutputURL) )
449 // // don't store document
450 // // input and output are equal OR
451 // GlobalLogWriter.println("Warning: Inputpath and Outputpath are equal. Document will not stored again.");
454 // bBack = impl_printToFileWithOOo(_aGTA, aDoc, _sOutputURL, _sPrintFileURL);
456 // GlobalLogWriter.println("Close document.");
461 // GlobalLogWriter.println("loadDocumentFromURL() failed with document: " + _sInputURL);
468 // -----------------------------------------------------------------------------
469 private boolean impl_printToFileWithOOo(ParameterHelper _aGTA
,
472 String _sPrintFileURL
)
474 boolean bBack
= false;
475 boolean bFailed
= true; // always be a pessimist,
478 GlobalLogWriter
.println("No document is given.");
484 if (_sOutputURL
!= null)
486 if (isStoreAllowed())
488 // store the document in an other directory
489 XStorable aStorable
= UnoRuntime
.queryInterface( XStorable
.class, _aDoc
);
490 if (aStorable
!= null)
492 PropertyValue
[] szEmptyArgs
= new PropertyValue
[0];
494 GlobalLogWriter
.println(DateHelper
.getDateTimeForHumanreadableLog() + " Store document.");
495 _aGTA
.getPerformance().startTime(PerformanceContainer
.Store
);
496 aStorable
.storeAsURL(_sOutputURL
, szEmptyArgs
);
497 _aGTA
.getPerformance().stopTime(PerformanceContainer
.Store
);
499 GlobalLogWriter
.println(DateHelper
.getDateTimeForHumanreadableLog() + " Store document done.");
500 // TimeHelper.waitInSeconds(1, "After store as URL to:" + _sOutputURL);
501 GlobalLogWriter
.println("Reload stored file test.");
502 XComponent aDoc
= loadFromURL(_aGTA
, _sOutputURL
);
505 GlobalLogWriter
.println("Reload stored file test failed, can't reload file: " + _sOutputURL
);
509 XCloseable xClose
= UnoRuntime
.queryInterface(XCloseable
.class, aDoc
);
518 // TimeHelper.waitInSeconds(1, "after close temp document");
524 // make sure to create the directory in
525 String sOutputFilename
= FileHelper
.getSystemPathFromFileURL(_sOutputURL
);
526 String sOutputPath
= FileHelper
.getPath(sOutputFilename
);
527 File aFile
= new File(sOutputPath
);
532 catch ( com
.sun
.star
.uno
.Exception e
)
534 // Some exception occures.FAILED
535 GlobalLogWriter
.println("UNO Exception caught.");
536 GlobalLogWriter
.println("Message: " + e
.getMessage());
545 // System.out.println("Document loaded.");
546 // Change Pagesettings to DIN A4
548 GlobalLogWriter
.println(DateHelper
.getDateTimeForHumanreadableLog() + " Print document.");
549 XPrintable aPrintable
= UnoRuntime
.queryInterface( XPrintable
.class, _aDoc
);
550 if (aPrintable
!= null)
552 // System.out.println(" Set PaperFormat to DIN A4");
554 // PropertyValue[] aPrinterProps = aPrintable.getPrinter();
555 // System.out.println("PrinterProps size: " + String.valueOf(aPrinterProps.length));
556 // int nPropIndex = 0;
557 // while (!"PaperFormat".equals(aPrinterProps[nPropIndex].Name))
559 // // System.out.println(aPrinterProps[nPropIndex].Name);
562 // aPrinterProps[nPropIndex].Value = com.sun.star.view.PaperFormat.A4;
563 // aPrintable.setPrinter(aPrinterProps);
566 // configure Office to allow to execute macos
568 // TODO: We need a possiblity to set the printer name also for StarOffice/OpenOffice
569 if (OSHelper
.isWindows())
571 if (_aGTA
.getPrinterName() != null)
573 ArrayList
<PropertyValue
> aPropertyList
= new ArrayList
<PropertyValue
>();
574 // PropertyValue [] aPrintProps = new PropertyValue[1];
575 PropertyValue Arg
= new PropertyValue();
577 Arg
.Value
= _aGTA
.getPrinterName();
578 aPropertyList
.add(Arg
);
580 // GlobalLogWriter.println("Printername is not null, so set to " + _aGTA.getPrinterName());
581 aPrintable
.setPrinter(PropertyHelper
.createPropertyValueArrayFormArrayList(aPropertyList
));
585 // set property values for XPrintable.print()
586 // more can be found at "http://api.libreoffice.org/docs/common/ref/com/sun/star/view/PrintOptions.html"
588 // int nProperties = 1; // default for 'FileName' property
589 // if (_aGTA.printAllPages() == false)
591 // // we don't want to print all pages, build Pages string by ourself
594 // int nPropsCount = 0;
596 // If we are a SpreadSheet (calc), we need to set PrintAllSheets property to 'true'
597 XServiceInfo xServiceInfo
= UnoRuntime
.queryInterface( XServiceInfo
.class, _aDoc
);
598 if ( xServiceInfo
.supportsService( "com.sun.star.sheet.SpreadsheetDocument" ) )
600 XMultiServiceFactory xMSF
= _aGTA
.getMultiServiceFactory();
601 Object aSettings
= xMSF
.createInstance( "com.sun.star.sheet.GlobalSheetSettings" );
602 if (aSettings
!= null)
604 XPropertySet xPropSet
= UnoRuntime
.queryInterface( XPropertySet
.class, aSettings
);
605 xPropSet
.setPropertyValue( "PrintAllSheets", new Boolean( true ) );
606 GlobalLogWriter
.println("PrintAllSheets := true");
610 ArrayList
<PropertyValue
> aPrintProps
= new ArrayList
<PropertyValue
>();
611 // GlobalLogWriter.println("Property FileName:=" + _sPrintFileURL);
613 // PropertyValue [] aPrintProps = new PropertyValue[nProperties];
614 PropertyValue Arg
= new PropertyValue();
615 Arg
.Name
= "FileName";
616 Arg
.Value
= _sPrintFileURL
;
617 // aPrintProps[nPropsCount ++] = Arg;
618 aPrintProps
.add(Arg
);
622 // generate pages string
623 if (_aGTA
.printAllPages() == false)
626 if (_aGTA
.getMaxPages() > 0)
628 sPages
= "1-" + String
.valueOf(_aGTA
.getMaxPages());
630 if (_aGTA
.getOnlyPages().length() != 0)
632 if (sPages
.length() != 0)
636 sPages
+= String
.valueOf(_aGTA
.getOnlyPages());
639 Arg
= new PropertyValue();
642 aPrintProps
.add(Arg
);
646 // GlobalLogWriter.println("Start printing.");
648 _aGTA
.getPerformance().startTime(PerformanceContainer
.Print
);
649 aPrintable
.print(PropertyHelper
.createPropertyValueArrayFormArrayList(aPrintProps
));
650 TimeHelper
.waitInSeconds(1, "Start waiting for print ready.");
652 GlobalLogWriter
.println("Wait until document is printed.");
653 boolean isBusy
= true;
657 PropertyValue
[] aPrinterProps
= aPrintable
.getPrinter();
659 while (!"IsBusy".equals(aPrinterProps
[nPropIndex
].Name
))
661 // System.out.println(aPrinterProps[nPropIndex].Name);
664 isBusy
= (aPrinterProps
[nPropIndex
].Value
== Boolean
.TRUE
) ?
true : false;
665 TimeHelper
.waitInSeconds(1, "is print ready?");
667 if (nPrintCount
> 3600)
669 // we will never wait >1h until print is ready!
670 GlobalLogWriter
.println("ERROR: Cancel print due to too long wait.");
671 throw new com
.sun
.star
.uno
.Exception("Convwatch exception, wait too long for printing.");
675 // TimeHelper.waitInSeconds(40, "Start waiting after print ready.");
677 _aGTA
.getPerformance().stopTime(PerformanceContainer
.Print
);
678 GlobalLogWriter
.println(DateHelper
.getDateTimeForHumanreadableLog() + " Print document done.");
680 // Create a .info file near the printed '.ps' or '.prn' file.
681 FileHelper
.createInfoFile(_sPrintFileURL
, _aGTA
);
685 GlobalLogWriter
.println("Can't get XPrintable interface.");
690 catch ( com
.sun
.star
.uno
.Exception e
)
692 // Some exception occures.FAILED
693 GlobalLogWriter
.println("UNO Exception caught.");
694 GlobalLogWriter
.println("Message: " + e
.getMessage());
702 GlobalLogWriter
.println("convwatch.OfficePrint: FAILED");
706 GlobalLogWriter
.println("convwatch.OfficePrint: OK");
714 * @param _sAbsoluteOutputPath
715 * @param _sAbsoluteInputFile
716 * @return true, if the reference (*.prrn file) based on given output path and given input path exist.
717 * If OVERWRITE_REFERENCE is set, always return false.
719 public boolean isReferenceExists(ParameterHelper _aGTA
,
720 String _sAbsoluteOutputPath
,
721 String _sAbsoluteInputFile
)
723 if (! FileHelper
.exists(_sAbsoluteInputFile
))
725 // throw new ConvWatchCancelException("Input file: " + _sAbsoluteInputFile + " does not exist.");
729 // String fs = System.getProperty("file.separator");
731 // String sInputFileURL = URLHelper.getFileURLFromSystemPath(_sAbsoluteInputFile);
733 String sInputFileBasename
= FileHelper
.getBasename(_sAbsoluteInputFile
);
734 // String sOutputFileURL = null;
736 if (_sAbsoluteOutputPath
!= null)
738 sOutputPath
= _sAbsoluteOutputPath
;
739 // FileHelper.makeDirectories("", sOutputPath);
743 String sInputPath
= FileHelper
.getPath(_sAbsoluteInputFile
);
744 sOutputPath
= sInputPath
;
746 // sOutputFileURL = URLHelper.getFileURLFromSystemPath(sOutputPath + fs + sInputFileBasename);
747 // sOutputFileURL = null;
749 String sPrintFilename
= FileHelper
.getNameNoSuffix(sInputFileBasename
);
750 // String sPrintFileURL;
752 String sAbsolutePrintFilename
= FileHelper
.appendPath(sOutputPath
, sPrintFilename
+ ".prn");
753 if (FileHelper
.exists(sAbsolutePrintFilename
) && _aGTA
.getOverwrite() == false)
755 GlobalLogWriter
.println("Reference already exist, don't overwrite. Set " + PropertyName
.DOC_COMPARATOR_OVERWRITE_REFERENCE
+ "=true to force overwrite.");
761 // -----------------------------------------------------------------------------
763 * create a reference file
764 * _sAbsoluteInputPath contains the source file, if not exists, return with failure.
765 * _sAbsoluteOutputPath contains the destination, where the file will store after load with StarOffice/OpenOffice.org
766 * if is null, print only near the Input file path
767 * _sPrintType ".prn" Print input file with StarOffice/OpenOffice.org and the default printer as PostScript
772 // public static boolean buildReference(ParameterHelper _aGTA,
773 // String _sAbsoluteOutputPath,
774 // String _sAbsoluteInputFile)
775 // throws OfficeException
777 // if (! FileHelper.exists(_sAbsoluteInputFile))
779 // throw new OfficeException("buildReference(): Input file: " + _sAbsoluteInputFile + " does not exist.");
782 // String fs = System.getProperty("file.separator");
784 // String sInputFileURL = URLHelper.getFileURLFromSystemPath(_sAbsoluteInputFile);
786 // String sInputFileBasename = FileHelper.getBasename(_sAbsoluteInputFile);
787 // String sOutputFileURL = null;
788 // String sOutputPath;
789 // if (_sAbsoluteOutputPath != null)
791 // sOutputPath = _sAbsoluteOutputPath;
792 // FileHelper.makeDirectories("", sOutputPath);
796 // String sInputPath = FileHelper.getPath(_sAbsoluteInputFile);
797 // sOutputPath = sInputPath;
799 // // sOutputFileURL = URLHelper.getFileURLFromSystemPath(sOutputPath + fs + sInputFileBasename);
800 // sOutputFileURL = null;
802 // String sPrintFilename = FileHelper.getNameNoSuffix(sInputFileBasename);
803 // String sPrintFileURL;
805 // String sAbsolutePrintFilename = sOutputPath + fs + sPrintFilename + ".prn";
806 // if (FileHelper.exists(sAbsolutePrintFilename) && _aGTA.getOverwrite() == false)
808 // GlobalLogWriter.println("Reference already exist, don't overwrite. Set " + PropertyName.DOC_COMPARATOR_OVERWRITE_REFERENCE + "=true to force overwrite.");
812 // if (_aGTA.getReferenceType().toLowerCase().equals("msoffice"))
814 // sPrintFileURL = URLHelper.getFileURLFromSystemPath(sAbsolutePrintFilename);
816 // else if (_aGTA.getReferenceType().toLowerCase().equals("pdf"))
818 //// TODO: If we rename the stored file to *.pdf, we have to be sure that we use *.pdf also as a available reference
819 // sPrintFileURL = URLHelper.getFileURLFromSystemPath(sAbsolutePrintFilename );
821 // else if (_aGTA.getReferenceType().toLowerCase().equals("ooo"))
823 // sPrintFileURL = URLHelper.getFileURLFromSystemPath(sAbsolutePrintFilename );
827 // GlobalLogWriter.println("OfficePrint.buildreference(): Unknown print type.");
830 // return printToFile(_aGTA, sInputFileURL, sOutputFileURL, sPrintFileURL);
835 // TODO: Das Teil muss hier raus!
838 // public static boolean printToFile(ParameterHelper _aGTA,
839 // String _sInputFileURL,
840 // String _sOutputFileURL,
841 // String _sPrintFileURL) throws OfficeException
843 // boolean bBack = false;
844 // String sPrintFileURL = null;
847 // // remember the current timer, to know how long a print process need.
850 // if (_aGTA.getReferenceType().toLowerCase().equals("ooo"))
852 // bBack = printToFileWithOOo(_aGTA, _sInputFileURL, _sOutputFileURL, _sPrintFileURL);
854 // else if (_aGTA.getReferenceType().toLowerCase().equals("pdf"))
856 // GlobalLogWriter.println("USE PDF AS EXPORT FORMAT.");
857 // bBack = storeAsPDF(_aGTA, _sInputFileURL, _sPrintFileURL);
859 // else if (_aGTA.getReferenceType().toLowerCase().equals("msoffice"))
861 // if (MSOfficePostscriptCreator.isMSOfficeDocumentFormat(_sInputFileURL))
863 // GlobalLogWriter.println("USE MSOFFICE AS EXPORT FORMAT.");
864 // MSOfficePostscriptCreator a = new MSOfficePostscriptCreator();
867 // a.printToFileWithMSOffice(_aGTA, FileHelper.getSystemPathFromFileURL(_sInputFileURL),
868 // FileHelper.getSystemPathFromFileURL(_sPrintFileURL));
870 // catch(OfficeException e)
872 // e.printStackTrace();
873 // GlobalLogWriter.println(e.getMessage());
874 // throw new OfficeException("Exception caught. Problem with MSOffice printer methods.");
876 // catch(java.io.IOException e)
878 // GlobalLogWriter.println(e.getMessage());
879 // throw new OfficeException("IOException caught. Problem with MSOffice printer methods.");
885 // GlobalLogWriter.println("This document type is not recognized as MSOffice format, as default fallback StarOffice/OpenOffice.org instead is used.");
886 // bBack = printToFileWithOOo(_aGTA, _sInputFileURL, _sOutputFileURL, _sPrintFileURL);
891 // // System.out.println("");
892 // throw new OfficeException("OfficePrint.printToFile(): Unknown print type.");
897 // -----------------------------------------------------------------------------
898 // TODO: move this away!
899 // -----------------------------------------------------------------------------
900 void showType(String _sInputURL
, XMultiServiceFactory _xMSF
)
902 if (_sInputURL
.length() == 0)
909 GlobalLogWriter
.println("MultiServiceFactory not set.");
912 XTypeDetection aTypeDetection
= null;
915 Object oObj
= _xMSF
.createInstance("com.sun.star.document.TypeDetection");
916 aTypeDetection
= UnoRuntime
.queryInterface(XTypeDetection
.class, oObj
);
918 catch(com
.sun
.star
.uno
.Exception e
)
920 GlobalLogWriter
.println("Can't get com.sun.star.document.TypeDetection.");
923 if (aTypeDetection
!= null)
925 String sType
= aTypeDetection
.queryTypeByURL(_sInputURL
);
926 GlobalLogWriter
.println("Type is: " + sType
);
931 // -----------------------------------------------------------------------------
932 public String
getInternalFilterName(String _sFilterName
, XMultiServiceFactory _xMSF
)
934 if (_sFilterName
.length() == 0)
936 // System.out.println("No FilterName set.");
942 GlobalLogWriter
.println("MultiServiceFactory not set.");
945 // XFilterFactory aFilterFactory = null;
949 aObj
= _xMSF
.createInstance("com.sun.star.document.FilterFactory");
951 catch(com
.sun
.star
.uno
.Exception e
)
953 GlobalLogWriter
.println("Can't get com.sun.star.document.FilterFactory.");
958 XNameAccess aNameAccess
= UnoRuntime
.queryInterface(XNameAccess
.class, aObj
);
959 if (aNameAccess
!= null)
962 // if (_sFilterName.toLowerCase().equals("help"))
964 // System.out.println("Show all possible ElementNames from current version." );
965 // String[] aElementNames = aNameAccess.getElementNames();
966 // for (int i = 0; i<aElementNames.length; i++)
968 // System.out.println(aElementNames[i]);
970 // System.out.println("Must quit.");
971 // System.out.exit(1);
974 if (! aNameAccess
.hasByName(_sFilterName
))
976 GlobalLogWriter
.println("FilterFactory.hasByName() says there exist no '" + _sFilterName
+ "'" );
980 Object
[] aElements
= null;
981 String
[] aExtensions
;
984 aElements
= (Object
[]) aNameAccess
.getByName(_sFilterName
);
985 if (aElements
!= null)
987 String sInternalFilterName
= null;
988 // System.out.println("getByName().length: " + String.valueOf(aElements.length));
989 for (int i
=0;i
<aElements
.length
; i
++)
991 PropertyValue aPropertyValue
= (PropertyValue
)aElements
[i
];
992 // System.out.println("PropertyValue.Name: " + aPropertyValue.Name);
993 if (aPropertyValue
.Name
.equals("Type"))
995 String sValue
= (String
)aPropertyValue
.Value
;
996 // System.out.println("Type: " + sValue);
997 sInternalFilterName
= sValue
;
1000 return sInternalFilterName
;
1004 GlobalLogWriter
.println("There are no elements for FilterName '" + _sFilterName
+ "'");
1008 catch (com
.sun
.star
.container
.NoSuchElementException e
)
1010 GlobalLogWriter
.println("NoSuchElementException caught. " + e
.getMessage());
1012 catch (com
.sun
.star
.lang
.WrappedTargetException e
)
1014 GlobalLogWriter
.println("WrappedTargetException caught. " + e
.getMessage());
1021 // -----------------------------------------------------------------------------
1023 String
getServiceNameFromFilterName(String _sFilterName
, XMultiServiceFactory _xMSF
)
1025 if (_sFilterName
.length() == 0)
1027 // System.out.println("No FilterName set.");
1033 GlobalLogWriter
.println("MultiServiceFactory not set.");
1036 // XFilterFactory aFilterFactory = null;
1040 aObj
= _xMSF
.createInstance("com.sun.star.document.FilterFactory");
1042 catch(com
.sun
.star
.uno
.Exception e
)
1044 GlobalLogWriter
.println("Can't get com.sun.star.document.FilterFactory.");
1049 XNameAccess aNameAccess
= UnoRuntime
.queryInterface(XNameAccess
.class, aObj
);
1050 if (aNameAccess
!= null)
1052 if (! aNameAccess
.hasByName(_sFilterName
))
1054 GlobalLogWriter
.println("FilterFactory.hasByName() says there exist no '" + _sFilterName
+ "'" );
1058 Object
[] aElements
= null;
1059 String
[] aExtensions
;
1062 aElements
= (Object
[]) aNameAccess
.getByName(_sFilterName
);
1063 if (aElements
!= null)
1065 String sServiceName
= null;
1066 // System.out.println("getByName().length: " + String.valueOf(aElements.length));
1067 for (int i
=0;i
<aElements
.length
; i
++)
1069 PropertyValue aPropertyValue
= (PropertyValue
)aElements
[i
];
1070 if (aPropertyValue
.Name
.equals("DocumentService"))
1072 String sValue
= (String
)aPropertyValue
.Value
;
1073 // System.out.println("DocumentService: " + sValue);
1074 sServiceName
= sValue
;
1078 return sServiceName
;
1082 GlobalLogWriter
.println("There are no elements for FilterName '" + _sFilterName
+ "'");
1086 catch (com
.sun
.star
.container
.NoSuchElementException e
)
1088 GlobalLogWriter
.println("NoSuchElementException caught. " + e
.getMessage());
1090 catch (com
.sun
.star
.lang
.WrappedTargetException e
)
1092 GlobalLogWriter
.println("WrappedTargetException caught. " + e
.getMessage());
1098 // -----------------------------------------------------------------------------
1100 public static String
getFileExtension(String _sInternalFilterName
, XMultiServiceFactory _xMSF
)
1102 if (_sInternalFilterName
.length() == 0)
1104 // System.out.println("No FilterName set.");
1110 GlobalLogWriter
.println("MultiServiceFactory not set.");
1113 XTypeDetection aTypeDetection
= null;
1116 Object oObj
= _xMSF
.createInstance("com.sun.star.document.TypeDetection");
1117 aTypeDetection
= UnoRuntime
.queryInterface(XTypeDetection
.class, oObj
);
1119 catch(com
.sun
.star
.uno
.Exception e
)
1121 GlobalLogWriter
.println("Can't get com.sun.star.document.TypeDetection.");
1124 if (aTypeDetection
!= null)
1126 XNameAccess aNameAccess
= UnoRuntime
.queryInterface(XNameAccess
.class, aTypeDetection
);
1127 if (aNameAccess
!= null)
1130 // System.out.println("Show ElementNames" );
1131 // String[] aElementNames = aNameAccess.getElementNames();
1132 // for (int i = 0; i<aElementNames.length; i++)
1134 // System.out.println(aElementNames[i]);
1137 if (! aNameAccess
.hasByName(_sInternalFilterName
))
1139 GlobalLogWriter
.println("TypeDetection.hasByName() says there exist no '" + _sInternalFilterName
+ "'" );
1143 Object
[] aElements
= null;
1144 String
[] aExtensions
;
1147 aElements
= (Object
[]) aNameAccess
.getByName(_sInternalFilterName
);
1148 if (aElements
!= null)
1150 String sExtension
= null;
1151 // System.out.println("getByName().length: " + String.valueOf(aElements.length));
1152 for (int i
=0;i
<aElements
.length
; i
++)
1154 PropertyValue aPropertyValue
= (PropertyValue
)aElements
[i
];
1155 // System.out.println("PropertyValue.Name: " + aPropertyValue.Name);
1156 if (aPropertyValue
.Name
.equals("Extensions"))
1158 aExtensions
= (String
[])aPropertyValue
.Value
;
1159 GlobalLogWriter
.println(" Possible extensions are: " + String
.valueOf(aExtensions
.length
));
1160 if (aExtensions
.length
> 0)
1162 for (int j
=0;j
<aExtensions
.length
;j
++)
1164 GlobalLogWriter
.println(" " + aExtensions
[j
]);
1166 sExtension
= aExtensions
[0];
1167 GlobalLogWriter
.println("");
1175 GlobalLogWriter
.println("There are no elements for FilterName '" + _sInternalFilterName
+ "'");
1179 catch (com
.sun
.star
.container
.NoSuchElementException e
)
1181 GlobalLogWriter
.println("NoSuchElementException caught. " + e
.getMessage());
1183 catch (com
.sun
.star
.lang
.WrappedTargetException e
)
1185 GlobalLogWriter
.println("WrappedTargetException caught. " + e
.getMessage());
1192 // -----------------------------------------------------------------------------
1193 public void convertDocument(String _sInputFile
, String _sOutputPath
, ParameterHelper _aGTA
) throws OfficeException
1195 XMultiServiceFactory xMSF
= _aGTA
.getMultiServiceFactory();
1198 GlobalLogWriter
.println("MultiServiceFactory in GraphicalTestArgument not set.");
1202 String sInputURL
= URLHelper
.getFileURLFromSystemPath(_sInputFile
);
1203 // showType(sInputURL, xMSF);
1204 XComponent aDoc
= loadFromURL( _aGTA
, sInputURL
);
1207 GlobalLogWriter
.println("Can't load document '"+ sInputURL
+ "'");
1211 if (_sOutputPath
== null)
1213 GlobalLogWriter
.println("Outputpath not set.");
1217 if (! isStoreAllowed())
1219 GlobalLogWriter
.println("It's not allowed to store, check Input/Output path.");
1222 // TODO: Do we need to wait?
1223 // TimeHelper.waitInSeconds(1, "wait after loadFromURL.");
1225 XServiceInfo xServiceInfo
= UnoRuntime
.queryInterface( XServiceInfo
.class, aDoc
);
1226 // String sFilter = getFilterName_forExcel(xServiceInfo);
1227 // System.out.println("Filter is " + sFilter);
1229 // store the document in an other directory
1230 XStorable xStorable
= UnoRuntime
.queryInterface( XStorable
.class, aDoc
);
1231 if (xStorable
== null)
1233 GlobalLogWriter
.println("com.sun.star.frame.XStorable is null");
1237 String sFilterName
= _aGTA
.getExportFilterName();
1239 // check how many Properties should initialize
1240 int nPropertyCount
= 0;
1241 // if (sFilterName != null && sFilterName.length() > 0)
1243 // nPropertyCount ++;
1246 // initialize PropertyArray
1247 // PropertyValue [] aStoreProps = new PropertyValue[ nPropertyCount ];
1248 // int nPropertyIndex = 0;
1249 ArrayList
<PropertyValue
> aPropertyList
= new ArrayList
<PropertyValue
>();
1251 String sExtension
= "";
1253 if (sFilterName
!= null && sFilterName
.length() > 0)
1255 String sInternalFilterName
= getInternalFilterName(sFilterName
, xMSF
);
1256 String sServiceName
= getServiceNameFromFilterName(sFilterName
, xMSF
);
1258 GlobalLogWriter
.println("Filter detection:");
1259 // check if service name from file filter is the same as from the loaded document
1260 boolean bServiceFailed
= false;
1261 if (sServiceName
== null || sInternalFilterName
== null)
1263 GlobalLogWriter
.println("Given FilterName '" + sFilterName
+ "' seems to be unknown.");
1264 bServiceFailed
= true;
1266 if (! xServiceInfo
.supportsService(sServiceName
))
1268 GlobalLogWriter
.println("Service from FilterName '" + sServiceName
+ "' is not supported by loaded document.");
1269 bServiceFailed
= true;
1271 if (bServiceFailed
== true)
1273 GlobalLogWriter
.println("Please check '" + PropertyName
.DOC_CONVERTER_EXPORT_FILTER_NAME
+ "' in the property file.");
1277 if (sInternalFilterName
!= null && sInternalFilterName
.length() > 0)
1279 // get the FileExtension, by the filter name, if we don't get a file extension
1280 // we assume the is also no right filter name.
1281 sExtension
= getFileExtension(sInternalFilterName
, xMSF
);
1282 if (sExtension
== null)
1284 GlobalLogWriter
.println("Can't found an extension for filtername, take it from the source.");
1288 PropertyValue Arg
= new PropertyValue();
1289 Arg
.Name
= "FilterName";
1290 Arg
.Value
= sFilterName
;
1291 // aStoreProps[nPropertyIndex ++] = Arg;
1292 aPropertyList
.add(Arg
);
1294 GlobalLogWriter
.println("FilterName is set to: " + sFilterName
);
1297 String sOutputURL
= "";
1300 // create the new filename with the extension, which is ok to the file format
1301 String sInputFileBasename
= FileHelper
.getBasename(_sInputFile
);
1302 // System.out.println("InputFileBasename " + sInputFileBasename);
1303 String sInputFileNameNoSuffix
= FileHelper
.getNameNoSuffix(sInputFileBasename
);
1304 // System.out.println("InputFilename no suffix " + sInputFileNameNoSuffix);
1305 String fs
= System
.getProperty("file.separator");
1306 String sOutputFile
= _sOutputPath
;
1307 if (! sOutputFile
.endsWith(fs
))
1311 if (sExtension
!= null && sExtension
.length() > 0)
1313 sOutputFile
+= sInputFileNameNoSuffix
+ "." + sExtension
;
1317 sOutputFile
+= sInputFileBasename
;
1320 if (FileHelper
.exists(sOutputFile
) && _aGTA
.getOverwrite() == false)
1322 GlobalLogWriter
.println("File already exist, don't overwrite. Set " + PropertyName
.DOC_COMPARATOR_OVERWRITE_REFERENCE
+ "=true to force overwrite.");
1326 sOutputURL
= URLHelper
.getFileURLFromSystemPath(sOutputFile
);
1328 GlobalLogWriter
.println("Store document as '" + sOutputURL
+ "'");
1329 xStorable
.storeAsURL(sOutputURL
, PropertyHelper
.createPropertyValueArrayFormArrayList(aPropertyList
));
1330 GlobalLogWriter
.println("Document stored.");
1332 catch (com
.sun
.star
.io
.IOException e
)
1334 GlobalLogWriter
.println("Can't store document '" + sOutputURL
+ "'. Message is :'" + e
.getMessage() + "'");
1336 // TODO: Do we need to wait?
1337 // TimeHelper.waitInSeconds(1, "unknown in OfficePrint.convertDocument()");
1343 * @return false, if 'NoOffice=yes' is given
1345 // private boolean shouldOfficeStart()
1347 // String sNoOffice = (String)m_aParameterHelper.getTestParameters().get( "NoOffice" );
1348 // if (sNoOffice != null)
1350 // if (sNoOffice.toLowerCase().startsWith("t") || sNoOffice.toLowerCase().startsWith("y"))
1358 OfficeProvider m_aProvider
= null;
1359 private void startOffice()
1361 // SimpleFileSemaphore aSemaphore = new SimpleFileSemaphore();
1362 // if (shouldOfficeStart())
1364 // if (OSHelper.isWindows())
1366 // aSemaphore.P(aSemaphore.getSemaphoreFile());
1368 m_aParameterHelper
.getTestParameters().put(util
.PropertyName
.DONT_BACKUP_USERLAYER
, Boolean
.TRUE
);
1370 m_aParameterHelper
.getPerformance().startTime(PerformanceContainer
.OfficeStart
);
1371 m_aProvider
= new OfficeProvider();
1372 XMultiServiceFactory xMSF
= (XMultiServiceFactory
) m_aProvider
.getManager(m_aParameterHelper
.getTestParameters());
1373 m_aParameterHelper
.getTestParameters().put("ServiceFactory", xMSF
);
1374 m_aParameterHelper
.getPerformance().stopTime(PerformanceContainer
.OfficeStart
);
1376 long nStartTime
= m_aParameterHelper
.getPerformance().getTime(PerformanceContainer
.OfficeStart
);
1377 // aGTA = getParameterHelper(); // get new TestArguments
1378 m_aParameterHelper
.getPerformance().setTime(PerformanceContainer
.OfficeStart
, nStartTime
);
1381 // Watcher Object is need in log object to give a simple way to say if a running office is alive.
1382 // As long as a log comes, it pings the Watcher and says the office is alive, if not an
1383 // internal counter increase and at a given point (300 seconds) the office is killed.
1384 if (GlobalLogWriter
.get().getWatcher() == null)
1386 GlobalLogWriter
.println("Set office watcher");
1387 OfficeWatcher aWatcher
= (OfficeWatcher
)m_aParameterHelper
.getTestParameters().get("Watcher");
1388 GlobalLogWriter
.get().setWatcher(aWatcher
);
1392 private void stopOffice()
1395 if (m_aProvider
!= null)
1397 String sAppExecCmd
= (String
)m_aParameterHelper
.getTestParameters().get("AppExecutionCommand");
1398 if (sAppExecCmd
!= null && sAppExecCmd
.length() > 0)
1400 m_aProvider
.closeExistingOffice(m_aParameterHelper
.getTestParameters(), true);
1402 // if (OSHelper.isWindows())
1404 // aSemaphore.V(aSemaphore.getSemaphoreFile());
1405 // aSemaphore.sleep(2);
1406 // // wait some time maybe an other process will take the semaphore
1407 // // I know, this is absolutly dirty, but the whole convwatch is dirty and need a big cleanup.
1412 private boolean m_bStoreFile
;
1413 public void disallowStore()
1415 m_bStoreFile
= false;
1417 public void allowStore()
1419 m_bStoreFile
= true;
1421 public boolean isStoreAllowed()
1424 // return m_bStoreFile;