1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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 ************************************************************************/
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;
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
;
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
);
125 throw new OfficeException("unknown reference type");
127 GlobalLogWriter
.get().println("Close document.");
128 m_aDocument
.dispose();
132 public void start() throws OfficeException
137 public void close() throws OfficeException
146 private void showProperty(PropertyValue _aValue
)
148 String sName
= _aValue
.Name
;
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.");
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
+ "'";
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
192 * Be aware, the ownership of the document gets to you, you have to close it.
194 private XComponent
loadFromURL(ParameterHelper _aGTA
,
197 XComponent aDoc
= null;
200 if (_aGTA
.getMultiServiceFactory() == null)
202 GlobalLogWriter
.get().println("MultiServiceFactory in GraphicalTestArgument not set.");
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())
225 // nPropertyCount ++;
227 // if (_aGTA.getImportFilterName() != null && _aGTA.getImportFilterName().length() > 0)
229 // nPropertyCount ++;
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();
241 Arg
.Value
= Boolean
.TRUE
;
242 aPropertyList
.add(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
);
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
);
271 GlobalLogWriter
.get().println(DateHelper
.getDateTimeForHumanreadableLog() + " Load document done.");
272 showDocumentType(aDoc
);
273 _aGTA
.setDocumentType(getDocumentType(aDoc
));
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("");
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());
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;
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());
335 private String
getFilterName_forPDF(XServiceInfo xServiceInfo
)
337 String filterName
= "";
339 if (xServiceInfo
.supportsService("com.sun.star.text.TextDocument"))
342 filterName
= "writer_pdf_Export";
344 else if ( xServiceInfo
.supportsService( "com.sun.star.sheet.SpreadsheetDocument" ) )
347 filterName
= "calc_pdf_Export";
349 else if ( xServiceInfo
.supportsService( "com.sun.star.drawing.DrawingDocument" ) )
352 filterName
= "draw_pdf_Export";
354 else if ( xServiceInfo
.supportsService( "com.sun.star.presentation.PresentationDocument" ) )
357 filterName
= "impress_pdf_Export";
359 else if (xServiceInfo
.supportsService("com.sun.star.text.WebDocument"))
362 filterName
= "writer_web_pdf_Export";
364 else if ( xServiceInfo
.supportsService("com.sun.star.text.GlobalDocument") )
367 filterName
= "writer_globaldocument_pdf_Export";
369 else if ( xServiceInfo
.supportsService( "com.sun.star.formulaFormulaProperties" ) )
372 filterName
= "math_pdf_Export";
378 // -----------------------------------------------------------------------------
380 // public boolean storeAsPDF(ParameterHelper _aGTA,
381 // String _sInputURL,
382 // String _sOutputURL)
384 // boolean bBack = false;
385 // XComponent aDoc = loadFromURL(_aGTA, _sInputURL);
389 // GlobalLogWriter.get().println("Can't load document.");
392 // bBack = storeAsPDF(_aGTA, aDoc, _sOutputURL);
393 // FileHelper.createInfoFile(_sOutputURL, _aGTA, "as pdf");
395 // GlobalLogWriter.get().println("Close document.");
400 public boolean storeAsPDF(ParameterHelper _aGTA
,
402 String _sOutputURL
) throws OfficeException
405 boolean bBack
= true;
406 _aGTA
.getPerformance().startTime(PerformanceContainer
.StoreAsPDF
);
407 bBack
= exportToPDF(_aDoc
, _sOutputURL
);
408 _aGTA
.getPerformance().stopTime(PerformanceContainer
.StoreAsPDF
);
412 GlobalLogWriter
.get().println("Can't store document as PDF.");
414 throw new OfficeException("Can't store document as PDF");
418 FileHelper
.createInfoFile(_sOutputURL
, _aGTA
, "as pdf");
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)
440 // // waitInSeconds(1);
441 // boolean bBack = false;
443 // XComponent aDoc = loadFromURL(_aGTA, _sInputURL);
446 // if ( _sInputURL.equals(_sOutputURL) )
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.");
453 // bBack = impl_printToFileWithOOo(_aGTA, aDoc, _sOutputURL, _sPrintFileURL);
455 // GlobalLogWriter.get().println("Close document.");
460 // GlobalLogWriter.get().println("loadDocumentFromURL() failed with document: " + _sInputURL);
467 // -----------------------------------------------------------------------------
468 private boolean impl_printToFileWithOOo(ParameterHelper _aGTA
,
471 String _sPrintFileURL
)
473 boolean bBack
= false;
474 boolean bFailed
= true; // always be a pessimist,
477 GlobalLogWriter
.get().println("No document is given.");
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
);
504 GlobalLogWriter
.get().println("Reload stored file test failed, can't reload file: " + _sOutputURL
);
508 XCloseable xClose
= (XCloseable
)UnoRuntime
.queryInterface(XCloseable
.class, aDoc
);
517 TimeHelper
.waitInSeconds(1, "after close temp document");
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
);
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());
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");
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))
558 // // System.out.println(aPrinterProps[nPropIndex].Name);
561 // aPrinterProps[nPropIndex].Value = com.sun.star.view.PaperFormat.A4;
562 // aPrintable.setPrinter(aPrinterProps);
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();
576 Arg
.Value
= _aGTA
.getPrinterName();
577 aPropertyList
.add(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)
590 // // we don't want to print all pages, build Pages string by ourself
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)
624 if (_aGTA
.getMaxPages() > 0)
626 sPages
= "1-" + String
.valueOf(_aGTA
.getMaxPages());
628 if (_aGTA
.getOnlyPages().length() != 0)
630 if (sPages
.length() != 0)
634 sPages
+= String
.valueOf(_aGTA
.getOnlyPages());
637 Arg
= new PropertyValue();
640 aPrintProps
.add(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;
655 PropertyValue
[] aPrinterProps
= aPrintable
.getPrinter();
657 while (!"IsBusy".equals(aPrinterProps
[nPropIndex
].Name
))
659 // System.out.println(aPrinterProps[nPropIndex].Name);
662 isBusy
= (aPrinterProps
[nPropIndex
].Value
== Boolean
.TRUE
) ?
true : false;
663 TimeHelper
.waitInSeconds(1, "is print ready?");
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
);
680 GlobalLogWriter
.get().println("Can't get XPrintable interface.");
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());
697 GlobalLogWriter
.get().println("convwatch.OfficePrint: FAILED");
701 GlobalLogWriter
.get().println("convwatch.OfficePrint: OK");
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.");
721 // String fs = System.getProperty("file.separator");
723 // String sInputFileURL = URLHelper.getFileURLFromSystemPath(_sAbsoluteInputFile);
725 String sInputFileBasename
= FileHelper
.getBasename(_sAbsoluteInputFile
);
726 // String sOutputFileURL = null;
728 if (_sAbsoluteOutputPath
!= null)
730 sOutputPath
= _sAbsoluteOutputPath
;
731 // FileHelper.makeDirectories("", sOutputPath);
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.");
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
764 // public static boolean buildReference(ParameterHelper _aGTA,
765 // String _sAbsoluteOutputPath,
766 // String _sAbsoluteInputFile)
767 // throws OfficeException
769 // if (! FileHelper.exists(_sAbsoluteInputFile))
771 // throw new OfficeException("buildReference(): Input file: " + _sAbsoluteInputFile + " does not exist.");
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)
783 // sOutputPath = _sAbsoluteOutputPath;
784 // FileHelper.makeDirectories("", sOutputPath);
788 // String sInputPath = FileHelper.getPath(_sAbsoluteInputFile);
789 // sOutputPath = sInputPath;
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)
800 // GlobalLogWriter.get().println("Reference already exist, don't overwrite. Set " + PropertyName.DOC_COMPARATOR_OVERWRITE_REFERENCE + "=true to force overwrite.");
804 // if (_aGTA.getReferenceType().toLowerCase().equals("msoffice"))
806 // sPrintFileURL = URLHelper.getFileURLFromSystemPath(sAbsolutePrintFilename);
808 // else if (_aGTA.getReferenceType().toLowerCase().equals("pdf"))
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 );
813 // else if (_aGTA.getReferenceType().toLowerCase().equals("ooo"))
815 // sPrintFileURL = URLHelper.getFileURLFromSystemPath(sAbsolutePrintFilename );
819 // GlobalLogWriter.get().println("OfficePrint.buildreference(): Unknown print type.");
822 // return printToFile(_aGTA, sInputFileURL, sOutputFileURL, sPrintFileURL);
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
835 // boolean bBack = false;
836 // String sPrintFileURL = null;
839 // // remember the current timer, to know how long a print process need.
842 // if (_aGTA.getReferenceType().toLowerCase().equals("ooo"))
844 // bBack = printToFileWithOOo(_aGTA, _sInputFileURL, _sOutputFileURL, _sPrintFileURL);
846 // else if (_aGTA.getReferenceType().toLowerCase().equals("pdf"))
848 // GlobalLogWriter.get().println("USE PDF AS EXPORT FORMAT.");
849 // bBack = storeAsPDF(_aGTA, _sInputFileURL, _sPrintFileURL);
851 // else if (_aGTA.getReferenceType().toLowerCase().equals("msoffice"))
853 // if (MSOfficePostscriptCreator.isMSOfficeDocumentFormat(_sInputFileURL))
855 // GlobalLogWriter.get().println("USE MSOFFICE AS EXPORT FORMAT.");
856 // MSOfficePostscriptCreator a = new MSOfficePostscriptCreator();
859 // a.printToFileWithMSOffice(_aGTA, FileHelper.getSystemPathFromFileURL(_sInputFileURL),
860 // FileHelper.getSystemPathFromFileURL(_sPrintFileURL));
862 // catch(OfficeException e)
864 // e.printStackTrace();
865 // GlobalLogWriter.get().println(e.getMessage());
866 // throw new OfficeException("Exception caught. Problem with MSOffice printer methods.");
868 // catch(java.io.IOException e)
870 // GlobalLogWriter.get().println(e.getMessage());
871 // throw new OfficeException("IOException caught. Problem with MSOffice printer methods.");
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);
883 // // System.out.println("");
884 // throw new OfficeException("OfficePrint.printToFile(): Unknown print type.");
889 // -----------------------------------------------------------------------------
890 // TODO: move this away!
891 // -----------------------------------------------------------------------------
892 void showType(String _sInputURL
, XMultiServiceFactory _xMSF
)
894 if (_sInputURL
.length() == 0)
901 GlobalLogWriter
.get().println("MultiServiceFactory not set.");
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.");
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.");
934 GlobalLogWriter
.get().println("MultiServiceFactory not set.");
937 // XFilterFactory aFilterFactory = 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.");
950 XNameAccess aNameAccess
= (XNameAccess
)UnoRuntime
.queryInterface(XNameAccess
.class, aObj
);
951 if (aNameAccess
!= null)
954 // if (_sFilterName.toLowerCase().equals("help"))
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++)
960 // System.out.println(aElementNames[i]);
962 // System.out.println("Must quit.");
963 // System.out.exit(1);
966 if (! aNameAccess
.hasByName(_sFilterName
))
968 GlobalLogWriter
.get().println("FilterFactory.hasByName() says there exist no '" + _sFilterName
+ "'" );
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
;
996 GlobalLogWriter
.get().println("There are no elements for FilterName '" + _sFilterName
+ "'");
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());
1013 // -----------------------------------------------------------------------------
1015 String
getServiceNameFromFilterName(String _sFilterName
, XMultiServiceFactory _xMSF
)
1017 if (_sFilterName
.length() == 0)
1019 // System.out.println("No FilterName set.");
1025 GlobalLogWriter
.get().println("MultiServiceFactory not set.");
1028 // XFilterFactory aFilterFactory = 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.");
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
+ "'" );
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
;
1070 return sServiceName
;
1074 GlobalLogWriter
.get().println("There are no elements for FilterName '" + _sFilterName
+ "'");
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());
1090 // -----------------------------------------------------------------------------
1092 public static String
getFileExtension(String _sInternalFilterName
, XMultiServiceFactory _xMSF
)
1094 if (_sInternalFilterName
.length() == 0)
1096 // System.out.println("No FilterName set.");
1102 GlobalLogWriter
.get().println("MultiServiceFactory not set.");
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.");
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++)
1126 // System.out.println(aElementNames[i]);
1129 if (! aNameAccess
.hasByName(_sInternalFilterName
))
1131 GlobalLogWriter
.get().println("TypeDetection.hasByName() says there exist no '" + _sInternalFilterName
+ "'" );
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("");
1167 GlobalLogWriter
.get().println("There are no elements for FilterName '" + _sInternalFilterName
+ "'");
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());
1184 // -----------------------------------------------------------------------------
1185 public void convertDocument(String _sInputFile
, String _sOutputPath
, ParameterHelper _aGTA
) throws OfficeException
1187 XMultiServiceFactory xMSF
= _aGTA
.getMultiServiceFactory();
1190 GlobalLogWriter
.get().println("MultiServiceFactory in GraphicalTestArgument not set.");
1194 String sInputURL
= URLHelper
.getFileURLFromSystemPath(_sInputFile
);
1195 // showType(sInputURL, xMSF);
1196 XComponent aDoc
= loadFromURL( _aGTA
, sInputURL
);
1199 GlobalLogWriter
.get().println("Can't load document '"+ sInputURL
+ "'");
1203 if (_sOutputPath
== null)
1205 GlobalLogWriter
.get().println("Outputpath not set.");
1209 if (! isStoreAllowed())
1211 GlobalLogWriter
.get().println("It's not allowed to store, check Input/Output path.");
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");
1229 String sFilterName
= _aGTA
.getExportFilterName();
1231 // check how many Properties should initialize
1232 int nPropertyCount
= 0;
1233 // if (sFilterName != null && sFilterName.length() > 0)
1235 // nPropertyCount ++;
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.");
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
);
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
))
1303 if (sExtension
!= null && sExtension
.length() > 0)
1305 sOutputFile
+= sInputFileNameNoSuffix
+ "." + sExtension
;
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.");
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"))
1347 OfficeProvider m_aProvider
= null;
1348 private void startOffice()
1350 // SimpleFileSemaphore aSemaphore = new SimpleFileSemaphore();
1351 // if (shouldOfficeStart())
1353 // if (OSHelper.isWindows())
1355 // aSemaphore.P(aSemaphore.getSemaphoreFile());
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
);
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()
1384 if (m_aProvider
!= null)
1386 m_aProvider
.closeExistingOffice(m_aParameterHelper
.getTestParameters(), true);
1387 // if (OSHelper.isWindows())
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.
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()
1409 // return m_bStoreFile;