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 .
22 import java
.util
.ArrayList
;
23 import java
.io
.FileWriter
;
25 import com
.sun
.star
.uno
.UnoRuntime
;
26 import com
.sun
.star
.lang
.XMultiServiceFactory
;
27 import com
.sun
.star
.document
.XTypeDetection
;
28 import com
.sun
.star
.container
.XNameAccess
;
29 import com
.sun
.star
.frame
.XDesktop
;
30 import com
.sun
.star
.beans
.XPropertySet
;
31 import com
.sun
.star
.beans
.PropertyValue
;
32 import com
.sun
.star
.frame
.XComponentLoader
;
33 import com
.sun
.star
.lang
.XComponent
;
34 import com
.sun
.star
.frame
.XStorable
;
35 import com
.sun
.star
.view
.XPrintable
;
36 import com
.sun
.star
.lang
.XServiceInfo
;
37 import com
.sun
.star
.frame
.XModel
;
38 import com
.sun
.star
.uno
.AnyConverter
;
40 import helper
.URLHelper
;
41 import helper
.PropertyHelper
;
42 import helper
.OSHelper
;
43 // import convwatch.FileHelper;
44 // import convwatch.MSOfficePrint;
45 // import convwatch.GraphicalTestArguments;
46 // import convwatch.ConvWatchCancelException;
48 // import helper.Parameter;
51 * This Object is to print a given document with OpenOffice.org / StarOffice
52 * over the normal printer driver
53 * or over it's pdf exporter
55 public class OfficePrint
{
58 // static long m_nStartTime;
59 // // static Date m_aDateCache = null;
62 // simple helper functions to start/stop a timer, to know how long a process need in milliseconds
64 // public static void startTimer()
66 // // if (m_aDateCache == null)
68 // // m_aDateCache = new Date();
70 // // m_nStartTime = m_aDateCache.getTime();
71 // m_nStartTime = System.currentTimeMillis();
73 // public static long stopTimer()
75 // // if (m_aDateCache == null)
77 // // System.out.println("Forgotten to initialise start timer.");
80 // // long m_nStopTime = m_aDateCache.getTime();
81 // if (m_nStartTime == 0)
83 // System.out.println("Forgotten to initialise start timer.");
86 // long m_nStopTime = System.currentTimeMillis();
87 // return m_nStopTime - m_nStartTime;
91 private static void showProperty(PropertyValue _aValue
)
93 String sName
= _aValue
.Name
;
97 sValue
= AnyConverter
.toString(_aValue
.Value
);
98 GlobalLogWriter
.get().println("Property " + sName
+ ":=" + sValue
);
100 catch (com
.sun
.star
.lang
.IllegalArgumentException e
)
102 // GlobalLogWriter.get().println("showProperty: can't convert a object to string.");
103 GlobalLogWriter
.get().println("Property " + sName
+ ":= a Object which can't convert by AnyConverter()");
108 * shows the FilterName and MediaType from the given XComponent
110 static String
getDocumentType( XComponent _aDoc
)
112 XModel xModel
= UnoRuntime
.queryInterface( XModel
.class, _aDoc
);
113 PropertyValue
[] aArgs
= xModel
.getArgs();
114 for (int i
=0;i
<aArgs
.length
;i
++)
116 PropertyValue aValue
= aArgs
[i
];
117 // System.out.print("Property: '" + aValue.Name);
118 // System.out.println("' := '" + aValue.Value + "'");
119 if (aValue
.Name
.equals("FilterName") ||
120 aValue
.Name
.equals("MediaType"))
122 String sNameValue
= "'" + aValue
.Name
+ "' := '" + aValue
.Value
+ "'";
129 static void showDocumentType( XComponent _aDoc
)
131 String sNameValue
= getDocumentType(_aDoc
);
132 GlobalLogWriter
.get().println(" Property: '" + sNameValue
);
135 * load a OpenOffice.org document from a given URL (_sInputURL)
136 * the GraphicalTestArguments must contain a living MultiServiceFactory object
138 * Be aware, the ownership of the document gets to you, you have to close it.
143 public static XComponent
loadFromURL(GraphicalTestArguments _aGTA
,
146 XComponent aDoc
= null;
149 if (_aGTA
.getMultiServiceFactory() == null)
151 GlobalLogWriter
.get().println("MultiServiceFactory in GraphicalTestArgument not set.");
154 Object oDsk
= _aGTA
.getMultiServiceFactory().createInstance("com.sun.star.frame.Desktop");
155 XDesktop aDesktop
= UnoRuntime
.queryInterface(XDesktop
.class, oDsk
);
157 if (aDesktop
!= null)
159 GlobalLogWriter
.get().println("com.sun.star.frame.Desktop created.");
160 // String sInputURL = aCurrentParameter.sInputURL;
161 // String sOutputURL = aCurrentParameter.sOutputURL;
162 // String sPrintFileURL = aCurrentParameter.sPrintToFileURL;
163 // System.out.println(_sInputURL);
166 // set here the loadComponentFromURL() properties
167 // at the moment only 'Hidden' is set, so no window is opened at work
169 ArrayList
<PropertyValue
> aPropertyList
= new ArrayList
<PropertyValue
>();
171 // check which properties should set and count it.
172 // if (_aGTA.isHidden())
174 // nPropertyCount ++;
176 // if (_aGTA.getImportFilterName() != null && _aGTA.getImportFilterName().length() > 0)
178 // nPropertyCount ++;
181 // initialize the propertyvalue
182 // int nPropertyIndex = 0;
183 // aProps = new PropertyValue[ nPropertyCount ];
185 // set all property values
186 if (_aGTA
.isHidden())
188 PropertyValue Arg
= new PropertyValue();
190 Arg
.Value
= Boolean
.TRUE
;
191 aPropertyList
.add(Arg
);
194 if (_aGTA
.getImportFilterName() != null && _aGTA
.getImportFilterName().length() > 0)
196 PropertyValue Arg
= new PropertyValue();
197 Arg
.Name
= "FilterName";
198 Arg
.Value
= _aGTA
.getImportFilterName();
199 aPropertyList
.add(Arg
);
203 GlobalLogWriter
.get().println(DateHelper
.getDateTimeForHumanreadableLog() + " Load document");
204 // GlobalLogWriter.get().flush();
206 XComponentLoader aCompLoader
= UnoRuntime
.queryInterface( XComponentLoader
.class, aDesktop
);
208 // XComponent aDoc = null;
210 _aGTA
.getPerformance().startTime(PerformanceContainer
.Load
);
211 aDoc
= aCompLoader
.loadComponentFromURL(_sInputURL
, "_blank", 0, PropertyHelper
.createPropertyValueArrayFormArrayList(aPropertyList
) );
212 _aGTA
.getPerformance().stopTime(PerformanceContainer
.Load
);
215 GlobalLogWriter
.get().println(DateHelper
.getDateTimeForHumanreadableLog() + " Load document done.");
216 showDocumentType(aDoc
);
217 _aGTA
.setDocumentType(getDocumentType(aDoc
));
221 GlobalLogWriter
.get().println(" Load document failed.");
222 if (_aGTA
.getImportFilterName() != null && _aGTA
.getImportFilterName().length() > 0)
224 GlobalLogWriter
.get().println(" Please check FilterName := '" + _aGTA
.getImportFilterName() + "'");
226 GlobalLogWriter
.get().println("");
231 GlobalLogWriter
.get().println("com.sun.star.frame.Desktop failed.");
234 catch ( com
.sun
.star
.uno
.Exception e
)
236 // Some exception occures.FAILED
237 GlobalLogWriter
.get().println("UNO Exception caught.");
238 GlobalLogWriter
.get().println("Message: " + e
.getMessage());
245 static boolean exportToPDF(XComponent _xComponent
, String _sDestinationName
)
247 XServiceInfo xServiceInfo
=
248 UnoRuntime
.queryInterface(
249 XServiceInfo
.class, _xComponent
252 ArrayList
<PropertyValue
> aPropertyList
= new ArrayList
<PropertyValue
>();
253 PropertyValue aFiltername
= new PropertyValue();
254 aFiltername
.Name
= "FilterName";
255 aFiltername
.Value
= getFilterName_forPDF(xServiceInfo
);
256 aPropertyList
.add(aFiltername
);
257 showProperty(aFiltername
);
258 boolean bWorked
= true;
263 UnoRuntime
.queryInterface(
264 XStorable
.class, _xComponent
266 store
.storeToURL(_sDestinationName
, PropertyHelper
.createPropertyValueArrayFormArrayList(aPropertyList
));
268 catch (com
.sun
.star
.io
.IOException e
)
270 GlobalLogWriter
.get().println("IO Exception caught.");
271 GlobalLogWriter
.get().println("Message: " + e
.getMessage());
278 static String
getFilterName_forPDF(XServiceInfo xServiceInfo
)
280 String filterName
= "";
282 if (xServiceInfo
.supportsService("com.sun.star.text.TextDocument"))
285 filterName
= "writer_pdf_Export";
287 else if ( xServiceInfo
.supportsService( "com.sun.star.sheet.SpreadsheetDocument" ) )
290 filterName
= "calc_pdf_Export";
292 else if ( xServiceInfo
.supportsService( "com.sun.star.drawing.DrawingDocument" ) )
295 filterName
= "draw_pdf_Export";
297 else if ( xServiceInfo
.supportsService( "com.sun.star.presentation.PresentationDocument" ) )
300 filterName
= "impress_pdf_Export";
302 else if (xServiceInfo
.supportsService("com.sun.star.text.WebDocument"))
305 filterName
= "writer_web_pdf_Export";
307 else if ( xServiceInfo
.supportsService("com.sun.star.text.GlobalDocument") )
310 filterName
= "writer_globaldocument_pdf_Export";
312 else if ( xServiceInfo
.supportsService( "com.sun.star.formulaFormulaProperties" ) )
315 filterName
= "math_pdf_Export";
321 // -----------------------------------------------------------------------------
323 public static boolean storeAsPDF(GraphicalTestArguments _aGTA
,
327 boolean bBack
= false;
328 XComponent aDoc
= loadFromURL(_aGTA
, _sInputURL
);
332 GlobalLogWriter
.get().println("Can't load document.");
335 bBack
= storeAsPDF(_aGTA
, aDoc
, _sOutputURL
);
336 createInfoFile(_sOutputURL
, _aGTA
, "as pdf");
338 GlobalLogWriter
.get().println("Close document.");
343 public static boolean storeAsPDF(GraphicalTestArguments _aGTA
,
348 boolean bBack
= true;
349 _aGTA
.getPerformance().startTime(PerformanceContainer
.StoreAsPDF
);
350 bBack
= exportToPDF(_aDoc
, _sOutputURL
);
351 _aGTA
.getPerformance().stopTime(PerformanceContainer
.StoreAsPDF
);
355 GlobalLogWriter
.get().println("Can't store document as PDF.");
361 // -----------------------------------------------------------------------------
364 * print the document found in file (_sInputURL) to as postscript to file (_sPrintFileURL)
365 * Due to the fact we use a printer to convert the file to postscript, the default printer
366 * to create such postscript format must be installed, this is not tested here.
371 * @param _sPrintFileURL
372 * @return true, if print has been done.
373 * Be careful, true means only print returns with no errors, to be sure print is really done
374 * check existance of _sPrintFileURL
377 public static boolean printToFileWithOOo(GraphicalTestArguments _aGTA
,
380 String _sPrintFileURL
)
383 boolean bBack
= false;
385 XComponent aDoc
= loadFromURL(_aGTA
, _sInputURL
);
388 if ( _sInputURL
.equals(_sOutputURL
) )
390 // don't store document
391 // input and output are equal OR
392 GlobalLogWriter
.get().println("Warning: Inputpath and Outputpath are equal. Document will not stored again.");
393 _aGTA
.disallowStore();
395 bBack
= impl_printToFileWithOOo(_aGTA
, aDoc
, _sOutputURL
, _sPrintFileURL
);
397 GlobalLogWriter
.get().println("Close document.");
402 GlobalLogWriter
.get().println("loadDocumentFromURL() failed with document: " + _sInputURL
);
407 // -----------------------------------------------------------------------------
408 public static void createInfoFile(String _sFile
, GraphicalTestArguments _aGTA
)
410 createInfoFile(_sFile
, _aGTA
, "");
413 public static void createInfoFile(String _sFile
, GraphicalTestArguments _aGTA
, String _sSpecial
)
416 if (_sFile
.startsWith("file://"))
418 sFilename
= FileHelper
.getSystemPathFromFileURL(_sFile
);
419 GlobalLogWriter
.get().println("CreateInfoFile: '" + sFilename
+ "'" );
425 String sFileDir
= FileHelper
.getPath(sFilename
);
426 String sBasename
= FileHelper
.getBasename(sFilename
);
427 String sNameNoSuffix
= FileHelper
.getNameNoSuffix(sBasename
);
429 String fs
= System
.getProperty("file.separator");
430 String ls
= System
.getProperty("line.separator");
431 String sInfoFilename
= sFileDir
+ fs
+ sNameNoSuffix
+ ".info";
432 File aInfoFile
= new File(sInfoFilename
);
434 String sBuildID
= "";
438 FileWriter out
= new FileWriter(aInfoFile
.toString());
439 out
.write("# automatically created file by graphical compare" + ls
);
442 if (_sSpecial
!= null && _sSpecial
.equals("msoffice"))
444 out
.write("# buildid from wordloadfile" + ls
);
445 sBuildID
= _aGTA
.getPerformance().getMSOfficeVersion();
446 out
.write("buildid=" + sBuildID
+ ls
);
450 out
.write("# buildid is read out of the bootstrap file" + ls
);
451 sBuildID
= _aGTA
.getBuildID();
452 out
.write("buildid=" + sBuildID
+ ls
);
454 // if (_sSpecial != null && _sSpecial.length() > 0)
456 // out.write("special=" + _sSpecial + ls);
459 out
.write("# resolution given in DPI" + ls
);
460 out
.write("resolution=" + _aGTA
.getResolutionInDPI() + ls
);
464 out
.write("buildid=" + _sSpecial
+ ls
);
466 // long nTime = stopTimer();
469 // out.write("# time is given in milli seconds" + ls);
470 // out.write("time=" + nTime + ls);
474 out
.write("# Values out of System.getProperty(...)" + ls
);
475 out
.write("os.name=" + System
.getProperty("os.name") + ls
);
476 out
.write("os.arch=" + System
.getProperty("os.arch") + ls
);
477 out
.write("os.version=" + System
.getProperty("os.version") + ls
);
482 out
.write("# Performance output, values are given in milli sec." + ls
);
483 _aGTA
.getPerformance().print(out
);
489 catch (java
.io
.IOException e
)
491 GlobalLogWriter
.get().println("can't create Info file.");
495 String sExtension
= FileHelper
.getSuffix(_aGTA
.getInputFile());
496 if (sExtension
.startsWith("."))
498 sExtension
= sExtension
.substring(1);
501 DB
.writeToDB(_aGTA
.getInputFile(),
505 _aGTA
.getReferenceType(),
506 _aGTA
.getResolutionInDPI()
512 // -----------------------------------------------------------------------------
513 private static boolean impl_printToFileWithOOo(GraphicalTestArguments _aGTA
,
516 String _sPrintFileURL
)
518 boolean bBack
= false;
519 boolean bFailed
= true; // always be a pessimist,
522 GlobalLogWriter
.get().println("No document is given.");
528 if (_sOutputURL
!= null)
530 if (_aGTA
.isStoreAllowed())
532 // store the document in an other directory
533 XStorable aStorable
= UnoRuntime
.queryInterface( XStorable
.class, _aDoc
);
534 if (aStorable
!= null)
536 PropertyValue
[] szEmptyArgs
= new PropertyValue
[0];
538 GlobalLogWriter
.get().println(DateHelper
.getDateTimeForHumanreadableLog() + " Store document.");
539 _aGTA
.getPerformance().startTime(PerformanceContainer
.Store
);
540 aStorable
.storeAsURL(_sOutputURL
, szEmptyArgs
);
541 _aGTA
.getPerformance().stopTime(PerformanceContainer
.Store
);
543 GlobalLogWriter
.get().println(DateHelper
.getDateTimeForHumanreadableLog() + " Store document done.");
544 TimeHelper
.waitInSeconds(2, "After store as URL to:" + _sOutputURL
);
545 GlobalLogWriter
.get().println("Reload stored file test.");
546 XComponent aDoc
= loadFromURL(_aGTA
, _sOutputURL
);
549 GlobalLogWriter
.get().println("Reload stored file test failed, can't reload file: " + _sOutputURL
);
555 catch ( com
.sun
.star
.uno
.Exception e
)
557 // Some exception occures.FAILED
558 GlobalLogWriter
.get().println("UNO Exception caught.");
559 GlobalLogWriter
.get().println("Message: " + e
.getMessage());
568 // System.out.println("Document loaded.");
569 // Change Pagesettings to DIN A4
571 GlobalLogWriter
.get().println(DateHelper
.getDateTimeForHumanreadableLog() + " Print document.");
572 XPrintable aPrintable
= UnoRuntime
.queryInterface( XPrintable
.class, _aDoc
);
573 if (aPrintable
!= null)
575 // System.out.println(" Set PaperFormat to DIN A4");
577 // PropertyValue[] aPrinterProps = aPrintable.getPrinter();
578 // System.out.println("PrinterProps size: " + String.valueOf(aPrinterProps.length));
579 // int nPropIndex = 0;
580 // while (!"PaperFormat".equals(aPrinterProps[nPropIndex].Name))
582 // // System.out.println(aPrinterProps[nPropIndex].Name);
585 // aPrinterProps[nPropIndex].Value = com.sun.star.view.PaperFormat.A4;
586 // aPrintable.setPrinter(aPrinterProps);
589 // configure Office to allow to execute macos
591 // TODO: We need a possiblity to set the printer name also for StarOffice/OpenOffice
592 if (OSHelper
.isWindows())
594 if (_aGTA
.getPrinterName() != null)
596 ArrayList
<PropertyValue
> aPropertyList
= new ArrayList
<PropertyValue
>();
597 // PropertyValue [] aPrintProps = new PropertyValue[1];
598 PropertyValue Arg
= new PropertyValue();
600 Arg
.Value
= _aGTA
.getPrinterName();
601 aPropertyList
.add(Arg
);
603 // GlobalLogWriter.get().println("Printername is not null, so set to " + _aGTA.getPrinterName());
604 aPrintable
.setPrinter(PropertyHelper
.createPropertyValueArrayFormArrayList(aPropertyList
));
608 // set property values for XPrintable.print()
609 // more can be found at "http://api.libreoffice.org/docs/common/ref/com/sun/star/view/PrintOptions.html"
611 // int nProperties = 1; // default for 'FileName' property
612 // if (_aGTA.printAllPages() == false)
614 // // we don't want to print all pages, build Pages string by ourself
617 // int nPropsCount = 0;
619 // If we are a SpreadSheet (calc), we need to set PrintAllSheets property to 'true'
620 XServiceInfo xServiceInfo
= UnoRuntime
.queryInterface( XServiceInfo
.class, _aDoc
);
621 if ( xServiceInfo
.supportsService( "com.sun.star.sheet.SpreadsheetDocument" ) )
623 XMultiServiceFactory xMSF
= _aGTA
.getMultiServiceFactory();
624 Object aSettings
= xMSF
.createInstance( "com.sun.star.sheet.GlobalSheetSettings" );
625 if (aSettings
!= null)
627 XPropertySet xPropSet
= UnoRuntime
.queryInterface( XPropertySet
.class, aSettings
);
628 xPropSet
.setPropertyValue( "PrintAllSheets", new Boolean( true ) );
629 GlobalLogWriter
.get().println("PrintAllSheets := true");
633 ArrayList
<PropertyValue
> aPrintProps
= new ArrayList
<PropertyValue
>();
634 // GlobalLogWriter.get().println("Property FileName:=" + _sPrintFileURL);
636 // PropertyValue [] aPrintProps = new PropertyValue[nProperties];
637 PropertyValue Arg
= new PropertyValue();
638 Arg
.Name
= "FileName";
639 Arg
.Value
= _sPrintFileURL
;
640 // aPrintProps[nPropsCount ++] = Arg;
641 aPrintProps
.add(Arg
);
644 if (_aGTA
.printAllPages() == false)
647 if (_aGTA
.getMaxPages() > 0)
649 sPages
= "1-" + String
.valueOf(_aGTA
.getMaxPages());
651 if (_aGTA
.getOnlyPages().length() != 0)
653 if (sPages
.length() != 0)
657 sPages
+= String
.valueOf(_aGTA
.getOnlyPages());
660 Arg
= new PropertyValue();
663 aPrintProps
.add(Arg
);
667 // GlobalLogWriter.get().println("Start printing.");
669 _aGTA
.getPerformance().startTime(PerformanceContainer
.Print
);
670 aPrintable
.print(PropertyHelper
.createPropertyValueArrayFormArrayList(aPrintProps
));
671 TimeHelper
.waitInSeconds(1, "Start waiting for print ready.");
673 GlobalLogWriter
.get().println("Wait until document is printed.");
674 boolean isBusy
= true;
678 PropertyValue
[] aPrinterProps
= aPrintable
.getPrinter();
680 while (!"IsBusy".equals(aPrinterProps
[nPropIndex
].Name
))
682 // System.out.println(aPrinterProps[nPropIndex].Name);
685 isBusy
= (aPrinterProps
[nPropIndex
].Value
== Boolean
.TRUE
) ?
true : false;
686 TimeHelper
.waitInSeconds(1, "is print ready?");
688 if (nPrintCount
> 3600)
690 // we will never wait >1h until print is ready!
691 GlobalLogWriter
.get().println("ERROR: Cancel print due to too long wait.");
692 throw new com
.sun
.star
.uno
.Exception("Convwatch exception, wait too long for printing.");
695 _aGTA
.getPerformance().stopTime(PerformanceContainer
.Print
);
696 GlobalLogWriter
.get().println(DateHelper
.getDateTimeForHumanreadableLog() + " Print document done.");
698 // Create a .info file near the printed '.ps' or '.prn' file.
699 createInfoFile(_sPrintFileURL
, _aGTA
);
703 GlobalLogWriter
.get().println("Can't get XPrintable interface.");
708 catch ( com
.sun
.star
.uno
.Exception e
)
710 // Some exception occures.FAILED
711 GlobalLogWriter
.get().println("UNO Exception caught.");
712 GlobalLogWriter
.get().println("Message: " + e
.getMessage());
720 GlobalLogWriter
.get().println("convwatch.OfficePrint: FAILED");
724 GlobalLogWriter
.get().println("convwatch.OfficePrint: OK");
732 * @param _sAbsoluteOutputPath
733 * @param _sAbsoluteInputFile
734 * @return true, if the reference (*.prrn file) based on given output path and given input path exist.
735 * If OVERWRITE_REFERENCE is set, always return false.
737 public static boolean isReferenceExists(GraphicalTestArguments _aGTA
,
738 String _sAbsoluteOutputPath
,
739 String _sAbsoluteInputFile
)
741 if (! FileHelper
.exists(_sAbsoluteInputFile
))
743 // throw new ConvWatchCancelException("Input file: " + _sAbsoluteInputFile + " does not exist.");
747 String fs
= System
.getProperty("file.separator");
749 // String sInputFileURL = URLHelper.getFileURLFromSystemPath(_sAbsoluteInputFile);
751 String sInputFileBasename
= FileHelper
.getBasename(_sAbsoluteInputFile
);
752 // String sOutputFileURL = null;
754 if (_sAbsoluteOutputPath
!= null)
756 sOutputPath
= _sAbsoluteOutputPath
;
757 // FileHelper.makeDirectories("", sOutputPath);
761 String sInputPath
= FileHelper
.getPath(_sAbsoluteInputFile
);
762 sOutputPath
= sInputPath
;
764 // sOutputFileURL = URLHelper.getFileURLFromSystemPath(sOutputPath + fs + sInputFileBasename);
765 // sOutputFileURL = null;
767 String sPrintFilename
= FileHelper
.getNameNoSuffix(sInputFileBasename
);
768 // String sPrintFileURL;
770 String sAbsolutePrintFilename
= sOutputPath
+ fs
+ sPrintFilename
+ ".prn";
771 if (FileHelper
.exists(sAbsolutePrintFilename
) && _aGTA
.getOverwrite() == false)
773 GlobalLogWriter
.get().println("Reference already exist, don't overwrite. Set " + PropertyName
.DOC_COMPARATOR_OVERWRITE_REFERENCE
+ "=true to force overwrite.");
779 // -----------------------------------------------------------------------------
781 * create a reference file
782 * _sAbsoluteInputPath contains the source file, if not exists, return with failure.
783 * _sAbsoluteOutputPath contains the destination, where the file will store after load with StarOffice/OpenOffice.org
784 * if is null, print only near the Input file path
785 * _sPrintType ".prn" Print input file with StarOffice/OpenOffice.org and the default printer as PostScript
788 * @param _sAbsoluteOutputPath
789 * @param _sAbsoluteInputFile
791 * @throws ConvWatchCancelException
793 public static boolean buildReference(GraphicalTestArguments _aGTA
,
794 String _sAbsoluteOutputPath
,
795 String _sAbsoluteInputFile
)
796 throws ConvWatchCancelException
798 if (! FileHelper
.exists(_sAbsoluteInputFile
))
800 throw new ConvWatchCancelException("buildReference(): Input file: " + _sAbsoluteInputFile
+ " does not exist.");
803 String fs
= System
.getProperty("file.separator");
805 String sInputFileURL
= URLHelper
.getFileURLFromSystemPath(_sAbsoluteInputFile
);
807 String sInputFileBasename
= FileHelper
.getBasename(_sAbsoluteInputFile
);
808 String sOutputFileURL
= null;
810 if (_sAbsoluteOutputPath
!= null)
812 sOutputPath
= _sAbsoluteOutputPath
;
813 FileHelper
.makeDirectories("", sOutputPath
);
817 String sInputPath
= FileHelper
.getPath(_sAbsoluteInputFile
);
818 sOutputPath
= sInputPath
;
820 // sOutputFileURL = URLHelper.getFileURLFromSystemPath(sOutputPath + fs + sInputFileBasename);
821 sOutputFileURL
= null;
823 String sPrintFilename
= FileHelper
.getNameNoSuffix(sInputFileBasename
);
824 String sPrintFileURL
;
826 String sAbsolutePrintFilename
= sOutputPath
+ fs
+ sPrintFilename
+ ".prn";
827 if (FileHelper
.exists(sAbsolutePrintFilename
) && _aGTA
.getOverwrite() == false)
829 GlobalLogWriter
.get().println("Reference already exist, don't overwrite. Set " + PropertyName
.DOC_COMPARATOR_OVERWRITE_REFERENCE
+ "=true to force overwrite.");
833 if (_aGTA
.getReferenceType().toLowerCase().equals("msoffice"))
835 sPrintFileURL
= URLHelper
.getFileURLFromSystemPath(sAbsolutePrintFilename
);
837 else if (_aGTA
.getReferenceType().toLowerCase().equals("pdf"))
839 // TODO: If we rename the stored file to *.pdf, we have to be sure that we use *.pdf also as a available reference
840 sPrintFileURL
= URLHelper
.getFileURLFromSystemPath(sAbsolutePrintFilename
);
842 else if (_aGTA
.getReferenceType().toLowerCase().equals("ooo"))
844 sPrintFileURL
= URLHelper
.getFileURLFromSystemPath(sAbsolutePrintFilename
);
848 GlobalLogWriter
.get().println("OfficePrint.buildreference(): Unknown print type.");
851 return printToFile(_aGTA
, sInputFileURL
, sOutputFileURL
, sPrintFileURL
);
854 public static boolean printToFile(GraphicalTestArguments _aGTA
,
855 String _sInputFileURL
,
856 String _sOutputFileURL
,
857 String _sPrintFileURL
) throws ConvWatchCancelException
859 boolean bBack
= false;
860 // check if given file is a picture, then do nothing
861 String sDocumentSuffix
= FileHelper
.getSuffix(_sInputFileURL
);
862 if (sDocumentSuffix
.toLowerCase().endsWith(".png") ||
863 sDocumentSuffix
.toLowerCase().endsWith(".gif") ||
864 sDocumentSuffix
.toLowerCase().endsWith(".jpg") ||
865 sDocumentSuffix
.toLowerCase().endsWith(".bmp"))
871 // remember the current timer, to know how long a print process need.
874 if (_aGTA
.getReferenceType().toLowerCase().equals("ooo"))
876 bBack
= printToFileWithOOo(_aGTA
, _sInputFileURL
, _sOutputFileURL
, _sPrintFileURL
);
878 else if (_aGTA
.getReferenceType().toLowerCase().equals("pdf"))
880 GlobalLogWriter
.get().println("USE PDF AS EXPORT FORMAT.");
881 bBack
= storeAsPDF(_aGTA
, _sInputFileURL
, _sPrintFileURL
);
883 else if (_aGTA
.getReferenceType().toLowerCase().equals("msoffice"))
885 if (MSOfficePrint
.isMSOfficeDocumentFormat(_sInputFileURL
))
887 GlobalLogWriter
.get().println("USE MSOFFICE AS EXPORT FORMAT.");
888 MSOfficePrint a
= new MSOfficePrint();
891 a
.printToFileWithMSOffice(_aGTA
, FileHelper
.getSystemPathFromFileURL(_sInputFileURL
),
892 FileHelper
.getSystemPathFromFileURL(_sPrintFileURL
));
894 catch(ConvWatchCancelException e
)
897 GlobalLogWriter
.get().println(e
.getMessage());
898 throw new ConvWatchCancelException("Exception caught. Problem with MSOffice printer methods.");
900 catch(java
.io
.IOException e
)
902 GlobalLogWriter
.get().println(e
.getMessage());
903 throw new ConvWatchCancelException("IOException caught. Problem with MSOffice printer methods.");
909 GlobalLogWriter
.get().println("This document type is not recognized as MSOffice format, as default fallback StarOffice/OpenOffice.org instead is used.");
910 bBack
= printToFileWithOOo(_aGTA
, _sInputFileURL
, _sOutputFileURL
, _sPrintFileURL
);
915 // System.out.println("");
916 throw new ConvWatchCancelException("OfficePrint.printToFile(): Unknown print type.");
921 // -----------------------------------------------------------------------------
922 // TODO: move this away!
923 // -----------------------------------------------------------------------------
924 static void showType(String _sInputURL
, XMultiServiceFactory _xMSF
)
926 if (_sInputURL
.length() == 0)
933 GlobalLogWriter
.get().println("MultiServiceFactory not set.");
936 XTypeDetection aTypeDetection
= null;
939 Object oObj
= _xMSF
.createInstance("com.sun.star.document.TypeDetection");
940 aTypeDetection
= UnoRuntime
.queryInterface(XTypeDetection
.class, oObj
);
942 catch(com
.sun
.star
.uno
.Exception e
)
944 GlobalLogWriter
.get().println("Can't get com.sun.star.document.TypeDetection.");
947 if (aTypeDetection
!= null)
949 String sType
= aTypeDetection
.queryTypeByURL(_sInputURL
);
950 GlobalLogWriter
.get().println("Type is: " + sType
);
955 // -----------------------------------------------------------------------------
956 public static String
getInternalFilterName(String _sFilterName
, XMultiServiceFactory _xMSF
)
958 if (_sFilterName
.length() == 0)
960 // System.out.println("No FilterName set.");
966 GlobalLogWriter
.get().println("MultiServiceFactory not set.");
969 // XFilterFactory aFilterFactory = null;
973 aObj
= _xMSF
.createInstance("com.sun.star.document.FilterFactory");
975 catch(com
.sun
.star
.uno
.Exception e
)
977 GlobalLogWriter
.get().println("Can't get com.sun.star.document.FilterFactory.");
982 XNameAccess aNameAccess
= UnoRuntime
.queryInterface(XNameAccess
.class, aObj
);
983 if (aNameAccess
!= null)
986 // if (_sFilterName.toLowerCase().equals("help"))
988 // System.out.println("Show all possible ElementNames from current version." );
989 // String[] aElementNames = aNameAccess.getElementNames();
990 // for (int i = 0; i<aElementNames.length; i++)
992 // System.out.println(aElementNames[i]);
994 // System.out.println("Must quit.");
995 // System.out.exit(1);
998 if (! aNameAccess
.hasByName(_sFilterName
))
1000 GlobalLogWriter
.get().println("FilterFactory.hasByName() says there exist no '" + _sFilterName
+ "'" );
1004 Object
[] aElements
= null;
1007 aElements
= (Object
[]) aNameAccess
.getByName(_sFilterName
);
1008 if (aElements
!= null)
1010 String sInternalFilterName
= null;
1011 // System.out.println("getByName().length: " + String.valueOf(aElements.length));
1012 for (int i
=0;i
<aElements
.length
; i
++)
1014 PropertyValue aPropertyValue
= (PropertyValue
)aElements
[i
];
1015 // System.out.println("PropertyValue.Name: " + aPropertyValue.Name);
1016 if (aPropertyValue
.Name
.equals("Type"))
1018 String sValue
= (String
)aPropertyValue
.Value
;
1019 // System.out.println("Type: " + sValue);
1020 sInternalFilterName
= sValue
;
1023 return sInternalFilterName
;
1027 GlobalLogWriter
.get().println("There are no elements for FilterName '" + _sFilterName
+ "'");
1031 catch (com
.sun
.star
.container
.NoSuchElementException e
)
1033 GlobalLogWriter
.get().println("NoSuchElementException caught. " + e
.getMessage());
1035 catch (com
.sun
.star
.lang
.WrappedTargetException e
)
1037 GlobalLogWriter
.get().println("WrappedTargetException caught. " + e
.getMessage());
1044 // -----------------------------------------------------------------------------
1046 static String
getServiceNameFromFilterName(String _sFilterName
, XMultiServiceFactory _xMSF
)
1048 if (_sFilterName
.length() == 0)
1050 // System.out.println("No FilterName set.");
1056 GlobalLogWriter
.get().println("MultiServiceFactory not set.");
1059 // XFilterFactory aFilterFactory = null;
1063 aObj
= _xMSF
.createInstance("com.sun.star.document.FilterFactory");
1065 catch(com
.sun
.star
.uno
.Exception e
)
1067 GlobalLogWriter
.get().println("Can't get com.sun.star.document.FilterFactory.");
1072 XNameAccess aNameAccess
= UnoRuntime
.queryInterface(XNameAccess
.class, aObj
);
1073 if (aNameAccess
!= null)
1075 if (! aNameAccess
.hasByName(_sFilterName
))
1077 GlobalLogWriter
.get().println("FilterFactory.hasByName() says there exist no '" + _sFilterName
+ "'" );
1081 Object
[] aElements
= null;
1084 aElements
= (Object
[]) aNameAccess
.getByName(_sFilterName
);
1085 if (aElements
!= null)
1087 String sServiceName
= null;
1088 // System.out.println("getByName().length: " + String.valueOf(aElements.length));
1089 for (int i
=0;i
<aElements
.length
; i
++)
1091 PropertyValue aPropertyValue
= (PropertyValue
)aElements
[i
];
1092 if (aPropertyValue
.Name
.equals("DocumentService"))
1094 String sValue
= (String
)aPropertyValue
.Value
;
1095 // System.out.println("DocumentService: " + sValue);
1096 sServiceName
= sValue
;
1100 return sServiceName
;
1104 GlobalLogWriter
.get().println("There are no elements for FilterName '" + _sFilterName
+ "'");
1108 catch (com
.sun
.star
.container
.NoSuchElementException e
)
1110 GlobalLogWriter
.get().println("NoSuchElementException caught. " + e
.getMessage());
1112 catch (com
.sun
.star
.lang
.WrappedTargetException e
)
1114 GlobalLogWriter
.get().println("WrappedTargetException caught. " + e
.getMessage());
1120 // -----------------------------------------------------------------------------
1122 public static String
getFileExtension(String _sInternalFilterName
, XMultiServiceFactory _xMSF
)
1124 if (_sInternalFilterName
.length() == 0)
1126 // System.out.println("No FilterName set.");
1132 GlobalLogWriter
.get().println("MultiServiceFactory not set.");
1135 XTypeDetection aTypeDetection
= null;
1138 Object oObj
= _xMSF
.createInstance("com.sun.star.document.TypeDetection");
1139 aTypeDetection
=UnoRuntime
.queryInterface(XTypeDetection
.class, oObj
);
1141 catch(com
.sun
.star
.uno
.Exception e
)
1143 GlobalLogWriter
.get().println("Can't get com.sun.star.document.TypeDetection.");
1146 if (aTypeDetection
!= null)
1148 XNameAccess aNameAccess
= UnoRuntime
.queryInterface(XNameAccess
.class, aTypeDetection
);
1149 if (aNameAccess
!= null)
1152 // System.out.println("Show ElementNames" );
1153 // String[] aElementNames = aNameAccess.getElementNames();
1154 // for (int i = 0; i<aElementNames.length; i++)
1156 // System.out.println(aElementNames[i]);
1159 if (! aNameAccess
.hasByName(_sInternalFilterName
))
1161 GlobalLogWriter
.get().println("TypeDetection.hasByName() says there exist no '" + _sInternalFilterName
+ "'" );
1165 Object
[] aElements
= null;
1166 String
[] aExtensions
;
1169 aElements
= (Object
[]) aNameAccess
.getByName(_sInternalFilterName
);
1170 if (aElements
!= null)
1172 String sExtension
= null;
1173 // System.out.println("getByName().length: " + String.valueOf(aElements.length));
1174 for (int i
=0;i
<aElements
.length
; i
++)
1176 PropertyValue aPropertyValue
= (PropertyValue
)aElements
[i
];
1177 // System.out.println("PropertyValue.Name: " + aPropertyValue.Name);
1178 if (aPropertyValue
.Name
.equals("Extensions"))
1180 aExtensions
= (String
[])aPropertyValue
.Value
;
1181 GlobalLogWriter
.get().println(" Possible extensions are: " + String
.valueOf(aExtensions
.length
));
1182 if (aExtensions
.length
> 0)
1184 for (int j
=0;j
<aExtensions
.length
;j
++)
1186 GlobalLogWriter
.get().println(" " + aExtensions
[j
]);
1188 sExtension
= aExtensions
[0];
1189 GlobalLogWriter
.get().println("");
1197 GlobalLogWriter
.get().println("There are no elements for FilterName '" + _sInternalFilterName
+ "'");
1201 catch (com
.sun
.star
.container
.NoSuchElementException e
)
1203 GlobalLogWriter
.get().println("NoSuchElementException caught. " + e
.getMessage());
1205 catch (com
.sun
.star
.lang
.WrappedTargetException e
)
1207 GlobalLogWriter
.get().println("WrappedTargetException caught. " + e
.getMessage());
1214 // -----------------------------------------------------------------------------
1215 public static void convertDocument(String _sInputFile
, String _sOutputPath
, GraphicalTestArguments _aGTA
) throws ConvWatchCancelException
1217 XMultiServiceFactory xMSF
= _aGTA
.getMultiServiceFactory();
1220 GlobalLogWriter
.get().println("MultiServiceFactory in GraphicalTestArgument not set.");
1224 String sInputURL
= URLHelper
.getFileURLFromSystemPath(_sInputFile
);
1225 // showType(sInputURL, xMSF);
1226 XComponent aDoc
= loadFromURL( _aGTA
, sInputURL
);
1229 GlobalLogWriter
.get().println("Can't load document '"+ sInputURL
+ "'");
1233 if (_sOutputPath
== null)
1235 GlobalLogWriter
.get().println("Outputpath not set.");
1239 if (! _aGTA
.isStoreAllowed())
1241 GlobalLogWriter
.get().println("It's not allowed to store, check Input/Output path.");
1244 // TODO: Do we need to wait?
1245 TimeHelper
.waitInSeconds(1, "wait after loadFromURL.");
1247 XServiceInfo xServiceInfo
= UnoRuntime
.queryInterface( XServiceInfo
.class, aDoc
);
1248 // String sFilter = getFilterName_forExcel(xServiceInfo);
1249 // System.out.println("Filter is " + sFilter);
1251 // store the document in an other directory
1252 XStorable xStorable
= UnoRuntime
.queryInterface( XStorable
.class, aDoc
);
1253 if (xStorable
== null)
1255 GlobalLogWriter
.get().println("com.sun.star.frame.XStorable is null");
1259 String sFilterName
= _aGTA
.getExportFilterName();
1261 // initialize PropertyArray
1262 // PropertyValue [] aStoreProps = new PropertyValue[ nPropertyCount ];
1263 // int nPropertyIndex = 0;
1264 ArrayList
<PropertyValue
> aPropertyList
= new ArrayList
<PropertyValue
>();
1266 String sExtension
= "";
1268 if (sFilterName
!= null && sFilterName
.length() > 0)
1270 String sInternalFilterName
= getInternalFilterName(sFilterName
, xMSF
);
1271 String sServiceName
= getServiceNameFromFilterName(sFilterName
, xMSF
);
1273 GlobalLogWriter
.get().println("Filter detection:");
1274 // check if service name from file filter is the same as from the loaded document
1275 boolean bServiceFailed
= false;
1276 if (sServiceName
== null || sInternalFilterName
== null)
1278 GlobalLogWriter
.get().println("Given FilterName '" + sFilterName
+ "' seems to be unknown.");
1279 bServiceFailed
= true;
1281 if (! xServiceInfo
.supportsService(sServiceName
))
1283 GlobalLogWriter
.get().println("Service from FilterName '" + sServiceName
+ "' is not supported by loaded document.");
1284 bServiceFailed
= true;
1286 if (bServiceFailed
== true)
1288 GlobalLogWriter
.get().println("Please check '" + PropertyName
.DOC_CONVERTER_EXPORT_FILTER_NAME
+ "' in the property file.");
1292 if (sInternalFilterName
!= null && sInternalFilterName
.length() > 0)
1294 // get the FileExtension, by the filter name, if we don't get a file extension
1295 // we assume the is also no right filter name.
1296 sExtension
= getFileExtension(sInternalFilterName
, xMSF
);
1297 if (sExtension
== null)
1299 GlobalLogWriter
.get().println("Can't found an extension for filtername, take it from the source.");
1303 PropertyValue Arg
= new PropertyValue();
1304 Arg
.Name
= "FilterName";
1305 Arg
.Value
= sFilterName
;
1306 // aStoreProps[nPropertyIndex ++] = Arg;
1307 aPropertyList
.add(Arg
);
1309 GlobalLogWriter
.get().println("FilterName is set to: " + sFilterName
);
1312 String sOutputURL
= "";
1315 // create the new filename with the extension, which is ok to the file format
1316 String sInputFileBasename
= FileHelper
.getBasename(_sInputFile
);
1317 // System.out.println("InputFileBasename " + sInputFileBasename);
1318 String sInputFileNameNoSuffix
= FileHelper
.getNameNoSuffix(sInputFileBasename
);
1319 // System.out.println("InputFilename no suffix " + sInputFileNameNoSuffix);
1320 String fs
= System
.getProperty("file.separator");
1321 String sOutputFile
= _sOutputPath
;
1322 if (! sOutputFile
.endsWith(fs
))
1326 if (sExtension
!= null && sExtension
.length() > 0)
1328 sOutputFile
+= sInputFileNameNoSuffix
+ "." + sExtension
;
1332 sOutputFile
+= sInputFileBasename
;
1335 if (FileHelper
.exists(sOutputFile
) && _aGTA
.getOverwrite() == false)
1337 GlobalLogWriter
.get().println("File already exist, don't overwrite. Set " + PropertyName
.DOC_COMPARATOR_OVERWRITE_REFERENCE
+ "=true to force overwrite.");
1341 sOutputURL
= URLHelper
.getFileURLFromSystemPath(sOutputFile
);
1343 GlobalLogWriter
.get().println("Store document as '" + sOutputURL
+ "'");
1344 xStorable
.storeAsURL(sOutputURL
, PropertyHelper
.createPropertyValueArrayFormArrayList(aPropertyList
));
1345 GlobalLogWriter
.get().println("Document stored.");
1347 catch (com
.sun
.star
.io
.IOException e
)
1349 GlobalLogWriter
.get().println("Can't store document '" + sOutputURL
+ "'. Message is :'" + e
.getMessage() + "'");
1351 // TODO: Do we need to wait?
1352 TimeHelper
.waitInSeconds(1, "unknown in OfficePrint.convertDocument()");