1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package graphical;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.frame.FrameSearchFlag;
27cdf0e10cSrcweir import com.sun.star.util.XCloseable;
28cdf0e10cSrcweir import helper.OfficeProvider;
29cdf0e10cSrcweir import helper.OfficeWatcher;
30cdf0e10cSrcweir import java.util.ArrayList;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
33cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
34cdf0e10cSrcweir import com.sun.star.document.XTypeDetection;
35cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
36cdf0e10cSrcweir import com.sun.star.frame.XDesktop;
37cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
38cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
39cdf0e10cSrcweir import com.sun.star.frame.XComponentLoader;
40cdf0e10cSrcweir import com.sun.star.lang.XComponent;
41cdf0e10cSrcweir import com.sun.star.frame.XStorable;
42cdf0e10cSrcweir import com.sun.star.view.XPrintable;
43cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
44cdf0e10cSrcweir import com.sun.star.frame.XModel;
45cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
46cdf0e10cSrcweir 
47cdf0e10cSrcweir import helper.URLHelper;
48cdf0e10cSrcweir import helper.PropertyHelper;
49cdf0e10cSrcweir import helper.OSHelper;
50cdf0e10cSrcweir 
51cdf0e10cSrcweir // import helper.Parameter;
52cdf0e10cSrcweir import java.io.File;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir /**
55cdf0e10cSrcweir  * This Object is to print a given document with OpenOffice.org / StarOffice
56cdf0e10cSrcweir  * over the normal printer driver
57cdf0e10cSrcweir  * or over it's pdf exporter
58cdf0e10cSrcweir  */
59cdf0e10cSrcweir public class OpenOfficePostscriptCreator implements IOffice
60cdf0e10cSrcweir {
61cdf0e10cSrcweir     private ParameterHelper m_aParameterHelper;
62cdf0e10cSrcweir     private String m_sOutputURL;
63cdf0e10cSrcweir     private String m_sBasename;
64cdf0e10cSrcweir     private String m_sDocumentName;
65cdf0e10cSrcweir     private XComponent m_aDocument;
66cdf0e10cSrcweir 
OpenOfficePostscriptCreator(ParameterHelper _aParam, String _sResult)67cdf0e10cSrcweir     public OpenOfficePostscriptCreator(ParameterHelper _aParam, String _sResult)
68cdf0e10cSrcweir     {
69cdf0e10cSrcweir         m_aParameterHelper = _aParam;
70cdf0e10cSrcweir         String sOutputURL = _sResult;
71cdf0e10cSrcweir         if (! sOutputURL.startsWith("file:"))
72cdf0e10cSrcweir         {
73cdf0e10cSrcweir             sOutputURL = URLHelper.getFileURLFromSystemPath(_sResult);
74cdf0e10cSrcweir         }
75cdf0e10cSrcweir         m_sOutputURL = sOutputURL;
76cdf0e10cSrcweir         m_aDocument = null;
77cdf0e10cSrcweir     }
78cdf0e10cSrcweir 
79cdf0e10cSrcweir 
load(String _sDocumentName)80cdf0e10cSrcweir     public void load(String _sDocumentName) throws OfficeException
81cdf0e10cSrcweir     {
82cdf0e10cSrcweir         m_sDocumentName = _sDocumentName;
83cdf0e10cSrcweir 
84cdf0e10cSrcweir         String sInputFileURL = URLHelper.getFileURLFromSystemPath(m_sDocumentName);
85cdf0e10cSrcweir         m_aDocument = loadFromURL(m_aParameterHelper, sInputFileURL);
86cdf0e10cSrcweir         if (m_aDocument == null)
87cdf0e10cSrcweir         {
88cdf0e10cSrcweir             GlobalLogWriter.println("loadDocumentFromURL() failed with document: " + sInputFileURL);
89cdf0e10cSrcweir             throw new OfficeException("load(): failed with document" + sInputFileURL);
90cdf0e10cSrcweir         }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir         m_sBasename = FileHelper.getBasename(m_sDocumentName);
93cdf0e10cSrcweir     }
94cdf0e10cSrcweir 
storeAsPostscript()95cdf0e10cSrcweir     public void storeAsPostscript() throws OfficeException
96cdf0e10cSrcweir     {
97cdf0e10cSrcweir         if (m_aDocument != null)
98cdf0e10cSrcweir         {
99cdf0e10cSrcweir             String sDocumentName = FileHelper.appendPath(m_sOutputURL, m_sBasename);
100cdf0e10cSrcweir             if (m_aParameterHelper.getReferenceType().toLowerCase().equals("ooo") ||
101cdf0e10cSrcweir                 m_aParameterHelper.getReferenceType().toLowerCase().equals("o3") ||
102cdf0e10cSrcweir                 m_aParameterHelper.getReferenceType().toLowerCase().equals("ps") )
103cdf0e10cSrcweir             {
104cdf0e10cSrcweir                 String sPrintURL = sDocumentName + ".ps";
105cdf0e10cSrcweir 
106cdf0e10cSrcweir                 impl_printToFileWithOOo(m_aParameterHelper, m_aDocument, sDocumentName, sPrintURL /*_sPrintFileURL*/);
107cdf0e10cSrcweir                 String sBasename = FileHelper.getBasename(sPrintURL);
108cdf0e10cSrcweir                 FileHelper.addBasenameToIndex(m_sOutputURL, sBasename, "OOo", "postscript", m_sDocumentName);
109cdf0e10cSrcweir             }
110cdf0e10cSrcweir             else if (m_aParameterHelper.getReferenceType().toLowerCase().equals("pdf"))
111cdf0e10cSrcweir             {
112cdf0e10cSrcweir                 String sPDFURL = sDocumentName + ".pdf";
113cdf0e10cSrcweir                 storeAsPDF(m_aParameterHelper, m_aDocument, sPDFURL);
114cdf0e10cSrcweir 
115cdf0e10cSrcweir                 String sBasename = FileHelper.getBasename(sPDFURL);
116cdf0e10cSrcweir                 FileHelper.addBasenameToIndex(m_sOutputURL, sBasename, "pdf", "pdf-export", m_sDocumentName);
117cdf0e10cSrcweir             }
118cdf0e10cSrcweir             else
119cdf0e10cSrcweir             {
120cdf0e10cSrcweir                 throw new OfficeException("unknown reference type");
121cdf0e10cSrcweir             }
122cdf0e10cSrcweir             GlobalLogWriter.println("Close document.");
123cdf0e10cSrcweir             m_aDocument.dispose();
124cdf0e10cSrcweir         }
125cdf0e10cSrcweir     }
126cdf0e10cSrcweir 
start()127cdf0e10cSrcweir     public void start() throws OfficeException
128cdf0e10cSrcweir     {
129cdf0e10cSrcweir         startOffice();
130cdf0e10cSrcweir     }
131cdf0e10cSrcweir 
close()132cdf0e10cSrcweir     public void close() throws OfficeException
133cdf0e10cSrcweir     {
134cdf0e10cSrcweir         stopOffice();
135cdf0e10cSrcweir     }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir 
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 
showProperty(PropertyValue _aValue)141cdf0e10cSrcweir     private void showProperty(PropertyValue _aValue)
142cdf0e10cSrcweir         {
143cdf0e10cSrcweir             String sName = _aValue.Name;
144cdf0e10cSrcweir             String sValue;
145cdf0e10cSrcweir             try
146cdf0e10cSrcweir             {
147cdf0e10cSrcweir                 sValue = AnyConverter.toString(_aValue.Value);
148cdf0e10cSrcweir                 GlobalLogWriter.println("Property " + sName + ":=" + sValue);
149cdf0e10cSrcweir             }
150cdf0e10cSrcweir             catch (com.sun.star.lang.IllegalArgumentException e)
151cdf0e10cSrcweir             {
152cdf0e10cSrcweir                 GlobalLogWriter.println("showProperty: can't convert a object to string. " + e.getMessage());
153cdf0e10cSrcweir             }
154cdf0e10cSrcweir         }
155cdf0e10cSrcweir 
156cdf0e10cSrcweir     /**
157cdf0e10cSrcweir      * shows the FilterName and MediaType from the given XComponent
158cdf0e10cSrcweir      */
getDocumentType( XComponent _aDoc )159cdf0e10cSrcweir     private String getDocumentType( XComponent _aDoc )
160cdf0e10cSrcweir         {
161cdf0e10cSrcweir             XModel xModel = UnoRuntime.queryInterface( XModel.class, _aDoc);
162cdf0e10cSrcweir             PropertyValue[] aArgs = xModel.getArgs();
163cdf0e10cSrcweir             for (int i=0;i<aArgs.length;i++)
164cdf0e10cSrcweir             {
165cdf0e10cSrcweir                 PropertyValue aValue = aArgs[i];
166cdf0e10cSrcweir                 // System.out.print("Property: '" + aValue.Name);
167cdf0e10cSrcweir                 // System.out.println("' := '" + aValue.Value + "'");
168cdf0e10cSrcweir                 if (aValue.Name.equals("FilterName") ||
169cdf0e10cSrcweir 					aValue.Name.equals("MediaType"))
170cdf0e10cSrcweir                 {
171cdf0e10cSrcweir                     String sNameValue = "'" + aValue.Name + "' := '" + aValue.Value + "'";
172cdf0e10cSrcweir                     return sNameValue;
173cdf0e10cSrcweir                 }
174cdf0e10cSrcweir             }
175cdf0e10cSrcweir             return "";
176cdf0e10cSrcweir         }
177cdf0e10cSrcweir 
showDocumentType( XComponent _aDoc )178cdf0e10cSrcweir     private void showDocumentType( XComponent _aDoc )
179cdf0e10cSrcweir         {
180cdf0e10cSrcweir             String sNameValue = getDocumentType(_aDoc);
181cdf0e10cSrcweir             GlobalLogWriter.println("  Property: '" + sNameValue);
182cdf0e10cSrcweir         }
183cdf0e10cSrcweir     /**
184cdf0e10cSrcweir      * load a OpenOffice.org document from a given URL (_sInputURL)
185cdf0e10cSrcweir      * the ParameterHelper must contain a living MultiServiceFactory object
186cdf0e10cSrcweir      * or we crash here.
187cdf0e10cSrcweir      * Be aware, the ownership of the document gets to you, you have to close it.
188cdf0e10cSrcweir      */
loadFromURL(ParameterHelper _aGTA, String _sInputURL)189cdf0e10cSrcweir     private XComponent loadFromURL(ParameterHelper _aGTA,
190cdf0e10cSrcweir                                          String _sInputURL)
191cdf0e10cSrcweir         {
192cdf0e10cSrcweir             XComponent aDoc = null;
193cdf0e10cSrcweir             try
194cdf0e10cSrcweir             {
195cdf0e10cSrcweir                 if (_aGTA.getMultiServiceFactory() == null)
196cdf0e10cSrcweir                 {
197cdf0e10cSrcweir                     GlobalLogWriter.println("MultiServiceFactory in GraphicalTestArgument not set.");
198cdf0e10cSrcweir                     return null;
199cdf0e10cSrcweir                 }
200cdf0e10cSrcweir                 Object oDsk = _aGTA.getMultiServiceFactory().createInstance("com.sun.star.frame.Desktop");
201cdf0e10cSrcweir                 XDesktop aDesktop = UnoRuntime.queryInterface(XDesktop.class, oDsk);
202cdf0e10cSrcweir 
203cdf0e10cSrcweir                 if (aDesktop != null)
204cdf0e10cSrcweir                 {
205cdf0e10cSrcweir                     GlobalLogWriter.println("com.sun.star.frame.Desktop created.");
206cdf0e10cSrcweir                     // String sInputURL = aCurrentParameter.sInputURL;
207cdf0e10cSrcweir                     // String sOutputURL = aCurrentParameter.sOutputURL;
208cdf0e10cSrcweir                     // String sPrintFileURL = aCurrentParameter.sPrintToFileURL;
209cdf0e10cSrcweir                     // System.out.println(_sInputURL);
210cdf0e10cSrcweir 
211cdf0e10cSrcweir 
212cdf0e10cSrcweir                     // set here the loadComponentFromURL() properties
213cdf0e10cSrcweir                     // at the moment only 'Hidden' is set, so no window is opened at work
214cdf0e10cSrcweir 
215cdf0e10cSrcweir                     ArrayList<PropertyValue> aPropertyList = new ArrayList<PropertyValue>();
216cdf0e10cSrcweir 
217cdf0e10cSrcweir                     // check which properties should set and count it.
218cdf0e10cSrcweir                     // if (_aGTA.isHidden())
219cdf0e10cSrcweir                     // {
220cdf0e10cSrcweir                     //     nPropertyCount ++;
221cdf0e10cSrcweir                     // }
222cdf0e10cSrcweir                     // if (_aGTA.getImportFilterName() != null && _aGTA.getImportFilterName().length() > 0)
223cdf0e10cSrcweir                     // {
224cdf0e10cSrcweir                     //     nPropertyCount ++;
225cdf0e10cSrcweir                     // }
226cdf0e10cSrcweir 
227cdf0e10cSrcweir                     // initialize the propertyvalue
228cdf0e10cSrcweir                     // int nPropertyIndex = 0;
229cdf0e10cSrcweir                     // aProps = new PropertyValue[ nPropertyCount ];
230cdf0e10cSrcweir 
231cdf0e10cSrcweir                     // set all property values
232cdf0e10cSrcweir                     if (_aGTA.isHidden())
233cdf0e10cSrcweir                     {
234cdf0e10cSrcweir                         PropertyValue Arg = new PropertyValue();
235cdf0e10cSrcweir                         Arg.Name = "Hidden";
236cdf0e10cSrcweir                         Arg.Value = Boolean.TRUE;
237cdf0e10cSrcweir                         aPropertyList.add(Arg);
238cdf0e10cSrcweir                         showProperty(Arg);
239cdf0e10cSrcweir                     }
240cdf0e10cSrcweir                     if (_aGTA.getImportFilterName() != null && _aGTA.getImportFilterName().length() > 0)
241cdf0e10cSrcweir                     {
242cdf0e10cSrcweir                         PropertyValue Arg = new PropertyValue();
243cdf0e10cSrcweir                         Arg.Name = "FilterName";
244cdf0e10cSrcweir                         Arg.Value = _aGTA.getImportFilterName();
245cdf0e10cSrcweir                         aPropertyList.add(Arg);
246cdf0e10cSrcweir                         showProperty(Arg);
247cdf0e10cSrcweir                     }
248cdf0e10cSrcweir                     PropertyValue ReadOnly = new PropertyValue();
249cdf0e10cSrcweir                     ReadOnly.Name = "ReadOnly";
250cdf0e10cSrcweir                     ReadOnly.Value = Boolean.TRUE;
251cdf0e10cSrcweir                     aPropertyList.add(ReadOnly);
252cdf0e10cSrcweir                     showProperty(ReadOnly);
253cdf0e10cSrcweir 
254cdf0e10cSrcweir                     GlobalLogWriter.println(DateHelper.getDateTimeForHumanreadableLog() + " Load document");
255cdf0e10cSrcweir                     // GlobalLogWriter.flush();
256cdf0e10cSrcweir 
257cdf0e10cSrcweir                     XComponentLoader aCompLoader = UnoRuntime.queryInterface( XComponentLoader.class, aDesktop);
258cdf0e10cSrcweir 
259cdf0e10cSrcweir                     // XComponent aDoc = null;
260cdf0e10cSrcweir 
261cdf0e10cSrcweir                     _aGTA.getPerformance().startTime(PerformanceContainer.Load);
262cdf0e10cSrcweir                     aDoc = aCompLoader.loadComponentFromURL(_sInputURL, "_blank", FrameSearchFlag.ALL, PropertyHelper.createPropertyValueArrayFormArrayList(aPropertyList) );
263cdf0e10cSrcweir                     _aGTA.getPerformance().stopTime(PerformanceContainer.Load);
264cdf0e10cSrcweir                     if (aDoc != null)
265cdf0e10cSrcweir                     {
266cdf0e10cSrcweir                         GlobalLogWriter.println(DateHelper.getDateTimeForHumanreadableLog() + " Load document done.");
267cdf0e10cSrcweir                         showDocumentType(aDoc);
268cdf0e10cSrcweir                         _aGTA.setDocumentType(getDocumentType(aDoc));
269cdf0e10cSrcweir // TODO:                        TimeHelper.waitInSeconds(20, "Wait after load document. Maybe helps due to layouting problems.");
270cdf0e10cSrcweir                     }
271cdf0e10cSrcweir                     else
272cdf0e10cSrcweir                     {
273cdf0e10cSrcweir                         GlobalLogWriter.println(" Load document failed.");
274cdf0e10cSrcweir                         if (_aGTA.getImportFilterName() != null && _aGTA.getImportFilterName().length() > 0)
275cdf0e10cSrcweir                         {
276cdf0e10cSrcweir                             GlobalLogWriter.println(" Please check FilterName := '" + _aGTA.getImportFilterName() + "'");
277cdf0e10cSrcweir                         }
278cdf0e10cSrcweir                         GlobalLogWriter.println("");
279cdf0e10cSrcweir                     }
280cdf0e10cSrcweir                 }
281cdf0e10cSrcweir                 else
282cdf0e10cSrcweir                 {
283cdf0e10cSrcweir                     GlobalLogWriter.println("com.sun.star.frame.Desktop failed.");
284cdf0e10cSrcweir                 }
285cdf0e10cSrcweir             }
286cdf0e10cSrcweir             catch ( com.sun.star.uno.Exception e )
287cdf0e10cSrcweir             {
288cdf0e10cSrcweir                 // Some exception occures.FAILED
289cdf0e10cSrcweir                 GlobalLogWriter.println("UNO Exception caught.");
290cdf0e10cSrcweir                 GlobalLogWriter.println("Message: " + e.getMessage());
291cdf0e10cSrcweir                 e.printStackTrace();
292cdf0e10cSrcweir                 aDoc = null;
293cdf0e10cSrcweir             }
294cdf0e10cSrcweir             return aDoc;
295cdf0e10cSrcweir         }
296cdf0e10cSrcweir 
exportToPDF(XComponent _xComponent, String _sDestinationName)297cdf0e10cSrcweir     private boolean exportToPDF(XComponent _xComponent, String _sDestinationName)
298cdf0e10cSrcweir         {
299cdf0e10cSrcweir             XServiceInfo xServiceInfo =
300cdf0e10cSrcweir                  UnoRuntime.queryInterface(
301cdf0e10cSrcweir                     XServiceInfo.class, _xComponent
302cdf0e10cSrcweir                     );
303cdf0e10cSrcweir 
304cdf0e10cSrcweir             ArrayList<PropertyValue> aPropertyList = new ArrayList<PropertyValue>();
305cdf0e10cSrcweir             PropertyValue aFiltername = new PropertyValue();
306cdf0e10cSrcweir             aFiltername.Name = "FilterName";
307cdf0e10cSrcweir             aFiltername.Value = getFilterName_forPDF(xServiceInfo);
308cdf0e10cSrcweir             aPropertyList.add(aFiltername);
309cdf0e10cSrcweir             showProperty(aFiltername);
310cdf0e10cSrcweir             boolean bWorked = true;
311cdf0e10cSrcweir 
312cdf0e10cSrcweir // TODO:             TimeHelper.waitInSeconds(20, "Wait before storeToURL. Maybe helps due to layouting problems.");
313cdf0e10cSrcweir             try
314cdf0e10cSrcweir             {
315cdf0e10cSrcweir                 XStorable store =
316cdf0e10cSrcweir                      UnoRuntime.queryInterface(
317cdf0e10cSrcweir                         XStorable.class, _xComponent
318cdf0e10cSrcweir                         );
319cdf0e10cSrcweir                 store.storeToURL(_sDestinationName, PropertyHelper.createPropertyValueArrayFormArrayList(aPropertyList));
320cdf0e10cSrcweir             }
321cdf0e10cSrcweir             catch (com.sun.star.io.IOException e)
322cdf0e10cSrcweir             {
323cdf0e10cSrcweir                 GlobalLogWriter.println("IO Exception caught.");
324cdf0e10cSrcweir                 GlobalLogWriter.println("Message: " + e.getMessage());
325cdf0e10cSrcweir                 bWorked = false;
326cdf0e10cSrcweir             }
327cdf0e10cSrcweir 
328cdf0e10cSrcweir             return bWorked;
329cdf0e10cSrcweir         }
330cdf0e10cSrcweir 
331cdf0e10cSrcweir 
getFilterName_forPDF(XServiceInfo xServiceInfo)332cdf0e10cSrcweir     private String getFilterName_forPDF(XServiceInfo xServiceInfo)
333cdf0e10cSrcweir         {
334cdf0e10cSrcweir             String filterName = "";
335cdf0e10cSrcweir 
336cdf0e10cSrcweir             if (xServiceInfo.supportsService("com.sun.star.text.TextDocument"))
337cdf0e10cSrcweir             {
338cdf0e10cSrcweir                 //writer
339cdf0e10cSrcweir                 filterName = "writer_pdf_Export";
340cdf0e10cSrcweir             }
341cdf0e10cSrcweir             else if ( xServiceInfo.supportsService( "com.sun.star.sheet.SpreadsheetDocument" ) )
342cdf0e10cSrcweir             {
343cdf0e10cSrcweir                 //calc
344cdf0e10cSrcweir                 filterName = "calc_pdf_Export";
345cdf0e10cSrcweir             }
346cdf0e10cSrcweir             else if ( xServiceInfo.supportsService( "com.sun.star.drawing.DrawingDocument" ) )
347cdf0e10cSrcweir             {
348cdf0e10cSrcweir                 //draw
349cdf0e10cSrcweir                 filterName = "draw_pdf_Export";
350cdf0e10cSrcweir             }
351cdf0e10cSrcweir             else if ( xServiceInfo.supportsService( "com.sun.star.presentation.PresentationDocument" ) )
352cdf0e10cSrcweir             {
353cdf0e10cSrcweir                 //impress
354cdf0e10cSrcweir                 filterName = "impress_pdf_Export";
355cdf0e10cSrcweir             }
356cdf0e10cSrcweir             else if (xServiceInfo.supportsService("com.sun.star.text.WebDocument"))
357cdf0e10cSrcweir             {
358cdf0e10cSrcweir                 //html document
359cdf0e10cSrcweir                 filterName = "writer_web_pdf_Export";
360cdf0e10cSrcweir             }
361cdf0e10cSrcweir             else if ( xServiceInfo.supportsService("com.sun.star.text.GlobalDocument") )
362cdf0e10cSrcweir             {
363cdf0e10cSrcweir                 //master document
364cdf0e10cSrcweir                 filterName = "writer_globaldocument_pdf_Export";
365cdf0e10cSrcweir             }
366cdf0e10cSrcweir             else if ( xServiceInfo.supportsService( "com.sun.star.formulaFormulaProperties" ) )
367cdf0e10cSrcweir             {
368cdf0e10cSrcweir                 //math document
369cdf0e10cSrcweir                 filterName = "math_pdf_Export";
370cdf0e10cSrcweir             }
371cdf0e10cSrcweir 
372cdf0e10cSrcweir             return filterName;
373cdf0e10cSrcweir         }
374cdf0e10cSrcweir 
375cdf0e10cSrcweir     // -----------------------------------------------------------------------------
376cdf0e10cSrcweir 
377cdf0e10cSrcweir //    public boolean storeAsPDF(ParameterHelper _aGTA,
378cdf0e10cSrcweir //                                     String _sInputURL,
379cdf0e10cSrcweir //                                     String _sOutputURL)
380cdf0e10cSrcweir //        {
381cdf0e10cSrcweir //            boolean bBack = false;
382cdf0e10cSrcweir //            XComponent aDoc = loadFromURL(_aGTA, _sInputURL);
383cdf0e10cSrcweir //
384cdf0e10cSrcweir //            if (aDoc == null)
385cdf0e10cSrcweir //            {
386cdf0e10cSrcweir //                GlobalLogWriter.println("Can't load document.");
387cdf0e10cSrcweir //                return bBack;
388cdf0e10cSrcweir //            }
389cdf0e10cSrcweir //            bBack = storeAsPDF(_aGTA, aDoc, _sOutputURL);
390cdf0e10cSrcweir //            FileHelper.createInfoFile(_sOutputURL, _aGTA, "as pdf");
391cdf0e10cSrcweir //
392cdf0e10cSrcweir //            GlobalLogWriter.println("Close document.");
393cdf0e10cSrcweir //            aDoc.dispose();
394cdf0e10cSrcweir //            return bBack;
395cdf0e10cSrcweir //        }
396cdf0e10cSrcweir 
storeAsPDF(ParameterHelper _aGTA, XComponent _aDoc, String _sOutputURL)397cdf0e10cSrcweir     public boolean storeAsPDF(ParameterHelper _aGTA,
398cdf0e10cSrcweir                                      XComponent _aDoc,
399cdf0e10cSrcweir                                      String _sOutputURL) throws OfficeException
400cdf0e10cSrcweir         {
401cdf0e10cSrcweir             // try {
402cdf0e10cSrcweir             boolean bBack = true;
403cdf0e10cSrcweir             _aGTA.getPerformance().startTime(PerformanceContainer.StoreAsPDF);
404cdf0e10cSrcweir             bBack = exportToPDF(_aDoc, _sOutputURL);
405cdf0e10cSrcweir             _aGTA.getPerformance().stopTime(PerformanceContainer.StoreAsPDF);
406cdf0e10cSrcweir 
407cdf0e10cSrcweir             if (!bBack)
408cdf0e10cSrcweir             {
409cdf0e10cSrcweir                 GlobalLogWriter.println("Can't store document as PDF.");
410cdf0e10cSrcweir //                bBack = false;
411cdf0e10cSrcweir                 throw new OfficeException("Can't store document as PDF");
412cdf0e10cSrcweir             }
413cdf0e10cSrcweir             else
414cdf0e10cSrcweir             {
415cdf0e10cSrcweir                 FileHelper.createInfoFile(_sOutputURL, _aGTA, "as pdf");
416cdf0e10cSrcweir             }
417cdf0e10cSrcweir             return bBack;
418cdf0e10cSrcweir         }
419cdf0e10cSrcweir 
420cdf0e10cSrcweir     // -----------------------------------------------------------------------------
421cdf0e10cSrcweir 
422cdf0e10cSrcweir     /**
423cdf0e10cSrcweir      * print the document found in file (_sInputURL) to as postscript to file (_sPrintFileURL)
424cdf0e10cSrcweir      * Due to the fact we use a printer to convert the file to postscript, the default printer
425cdf0e10cSrcweir      * to create such postscript format must be installed, this is not tested here.
426cdf0e10cSrcweir      *
427cdf0e10cSrcweir      * @return true, if print has been done.
428cdf0e10cSrcweir      *         Be careful, true means only print returns with no errors, to be sure print is really done
429cdf0e10cSrcweir      *         check existance of _sPrintFileURL
430cdf0e10cSrcweir      */
431cdf0e10cSrcweir 
432cdf0e10cSrcweir //    public boolean printToFileWithOOo(ParameterHelper _aGTA,
433cdf0e10cSrcweir //                                             String _sInputURL,
434cdf0e10cSrcweir //                                             String _sOutputURL,
435cdf0e10cSrcweir //                                             String _sPrintFileURL)
436cdf0e10cSrcweir //        {
437cdf0e10cSrcweir //            // waitInSeconds(1);
438cdf0e10cSrcweir //            boolean bBack = false;
439cdf0e10cSrcweir //
440cdf0e10cSrcweir //            XComponent aDoc = loadFromURL(_aGTA, _sInputURL);
441cdf0e10cSrcweir //            if (aDoc != null)
442cdf0e10cSrcweir //            {
443cdf0e10cSrcweir //                if ( _sInputURL.equals(_sOutputURL) )
444cdf0e10cSrcweir //                {
445cdf0e10cSrcweir //                    // don't store document
446cdf0e10cSrcweir //                    // input and output are equal OR
447cdf0e10cSrcweir //                    GlobalLogWriter.println("Warning: Inputpath and Outputpath are equal. Document will not stored again.");
448cdf0e10cSrcweir //                    disallowStore();
449cdf0e10cSrcweir //                }
450cdf0e10cSrcweir //                bBack = impl_printToFileWithOOo(_aGTA, aDoc, _sOutputURL, _sPrintFileURL);
451cdf0e10cSrcweir //
452cdf0e10cSrcweir //                GlobalLogWriter.println("Close document.");
453cdf0e10cSrcweir //                aDoc.dispose();
454cdf0e10cSrcweir //            }
455cdf0e10cSrcweir //            else
456cdf0e10cSrcweir //            {
457cdf0e10cSrcweir //                GlobalLogWriter.println("loadDocumentFromURL() failed with document: " + _sInputURL);
458cdf0e10cSrcweir //            }
459cdf0e10cSrcweir //            return bBack;
460cdf0e10cSrcweir //        }
461cdf0e10cSrcweir 
462cdf0e10cSrcweir 
463cdf0e10cSrcweir 
464cdf0e10cSrcweir     // -----------------------------------------------------------------------------
impl_printToFileWithOOo(ParameterHelper _aGTA, XComponent _aDoc, String _sOutputURL, String _sPrintFileURL)465cdf0e10cSrcweir     private boolean impl_printToFileWithOOo(ParameterHelper _aGTA,
466cdf0e10cSrcweir                                                    XComponent _aDoc,
467cdf0e10cSrcweir                                                    String _sOutputURL,
468cdf0e10cSrcweir                                                    String _sPrintFileURL)
469cdf0e10cSrcweir         {
470cdf0e10cSrcweir             boolean bBack = false;
471cdf0e10cSrcweir             boolean bFailed = true;              // always be a pessimist,
472cdf0e10cSrcweir             if (_aDoc == null)
473cdf0e10cSrcweir             {
474cdf0e10cSrcweir                 GlobalLogWriter.println("No document is given.");
475cdf0e10cSrcweir                 return bBack;
476cdf0e10cSrcweir             }
477cdf0e10cSrcweir 
478cdf0e10cSrcweir             try
479cdf0e10cSrcweir             {
480cdf0e10cSrcweir                 if (_sOutputURL != null)
481cdf0e10cSrcweir                 {
482cdf0e10cSrcweir                     if (isStoreAllowed())
483cdf0e10cSrcweir                     {
484cdf0e10cSrcweir                         // store the document in an other directory
485cdf0e10cSrcweir                         XStorable aStorable = UnoRuntime.queryInterface( XStorable.class, _aDoc);
486cdf0e10cSrcweir                         if (aStorable != null)
487cdf0e10cSrcweir                         {
488cdf0e10cSrcweir                             PropertyValue [] szEmptyArgs = new PropertyValue [0];
489cdf0e10cSrcweir 
490cdf0e10cSrcweir                             GlobalLogWriter.println(DateHelper.getDateTimeForHumanreadableLog() + " Store document.");
491cdf0e10cSrcweir                             _aGTA.getPerformance().startTime(PerformanceContainer.Store);
492cdf0e10cSrcweir                             aStorable.storeAsURL(_sOutputURL, szEmptyArgs);
493cdf0e10cSrcweir                             _aGTA.getPerformance().stopTime(PerformanceContainer.Store);
494cdf0e10cSrcweir 
495cdf0e10cSrcweir                             GlobalLogWriter.println(DateHelper.getDateTimeForHumanreadableLog() + " Store document done.");
496cdf0e10cSrcweir                             // TimeHelper.waitInSeconds(1, "After store as URL to:" + _sOutputURL);
497cdf0e10cSrcweir                             GlobalLogWriter.println("Reload stored file test.");
498cdf0e10cSrcweir                             XComponent aDoc = loadFromURL(_aGTA, _sOutputURL);
499cdf0e10cSrcweir                             if (aDoc == null)
500cdf0e10cSrcweir                             {
501cdf0e10cSrcweir                                 GlobalLogWriter.println("Reload stored file test failed, can't reload file: " + _sOutputURL);
502cdf0e10cSrcweir                             }
503cdf0e10cSrcweir                             else
504cdf0e10cSrcweir                             {
505cdf0e10cSrcweir                                 XCloseable xClose = UnoRuntime.queryInterface(XCloseable.class, aDoc);
506cdf0e10cSrcweir                                 if (xClose != null)
507cdf0e10cSrcweir                                 {
508cdf0e10cSrcweir                                     xClose.close(true);
509cdf0e10cSrcweir                                 }
510cdf0e10cSrcweir                                 else
511cdf0e10cSrcweir                                 {
512cdf0e10cSrcweir                                     aDoc.dispose();
513cdf0e10cSrcweir                                 }
514cdf0e10cSrcweir                                 // TimeHelper.waitInSeconds(1, "after close temp document");
515cdf0e10cSrcweir                             }
516cdf0e10cSrcweir                         }
517cdf0e10cSrcweir                     }
518cdf0e10cSrcweir                     else
519cdf0e10cSrcweir                     {
520cdf0e10cSrcweir                         // make sure to create the directory in
521cdf0e10cSrcweir                         String sOutputFilename = FileHelper.getSystemPathFromFileURL(_sOutputURL);
522cdf0e10cSrcweir                         String sOutputPath = FileHelper.getPath(sOutputFilename);
523cdf0e10cSrcweir                         File aFile = new File(sOutputPath);
524cdf0e10cSrcweir                         aFile.mkdirs();
525cdf0e10cSrcweir                     }
526cdf0e10cSrcweir                 }
527cdf0e10cSrcweir             }
528cdf0e10cSrcweir             catch ( com.sun.star.uno.Exception e )
529cdf0e10cSrcweir             {
530cdf0e10cSrcweir                 // Some exception occures.FAILED
531cdf0e10cSrcweir                 GlobalLogWriter.println("UNO Exception caught.");
532cdf0e10cSrcweir                 GlobalLogWriter.println("Message: " + e.getMessage());
533cdf0e10cSrcweir 
534cdf0e10cSrcweir                 e.printStackTrace();
535cdf0e10cSrcweir                 bBack = false;
536cdf0e10cSrcweir             }
537cdf0e10cSrcweir 
538cdf0e10cSrcweir             try
539cdf0e10cSrcweir             {
540cdf0e10cSrcweir 
541cdf0e10cSrcweir                 // System.out.println("Document loaded.");
542cdf0e10cSrcweir                 // Change Pagesettings to DIN A4
543cdf0e10cSrcweir 
544cdf0e10cSrcweir                 GlobalLogWriter.println(DateHelper.getDateTimeForHumanreadableLog() + " Print document.");
545cdf0e10cSrcweir                 XPrintable aPrintable =  UnoRuntime.queryInterface( XPrintable.class, _aDoc);
546cdf0e10cSrcweir                 if (aPrintable != null)
547cdf0e10cSrcweir                 {
548cdf0e10cSrcweir                     // System.out.println("  Set PaperFormat to DIN A4");
549cdf0e10cSrcweir                     // {
550cdf0e10cSrcweir                     //     PropertyValue[] aPrinterProps = aPrintable.getPrinter();
551cdf0e10cSrcweir                     //     System.out.println("PrinterProps size: " + String.valueOf(aPrinterProps.length));
552cdf0e10cSrcweir                     //     int nPropIndex = 0;
553cdf0e10cSrcweir                     //     while (!"PaperFormat".equals(aPrinterProps[nPropIndex].Name))
554cdf0e10cSrcweir                     //     {
555cdf0e10cSrcweir                     //         // System.out.println(aPrinterProps[nPropIndex].Name);
556cdf0e10cSrcweir                     //         nPropIndex++;
557cdf0e10cSrcweir                     //     }
558cdf0e10cSrcweir                     //     aPrinterProps[nPropIndex].Value = com.sun.star.view.PaperFormat.A4;
559cdf0e10cSrcweir                     //     aPrintable.setPrinter(aPrinterProps);
560cdf0e10cSrcweir                     // }
561cdf0e10cSrcweir 
562cdf0e10cSrcweir                     // configure Office to allow to execute macos
563cdf0e10cSrcweir 
564cdf0e10cSrcweir // TODO: We need a possiblity to set the printer name also for StarOffice/OpenOffice
565cdf0e10cSrcweir                     if (OSHelper.isWindows())
566cdf0e10cSrcweir                     {
567cdf0e10cSrcweir                         if (_aGTA.getPrinterName() != null)
568cdf0e10cSrcweir                         {
569cdf0e10cSrcweir                             ArrayList<PropertyValue> aPropertyList = new ArrayList<PropertyValue>();
570cdf0e10cSrcweir                             // PropertyValue [] aPrintProps = new PropertyValue[1];
571cdf0e10cSrcweir                             PropertyValue Arg = new PropertyValue();
572cdf0e10cSrcweir                             Arg.Name = "Name";
573cdf0e10cSrcweir                             Arg.Value = _aGTA.getPrinterName();
574cdf0e10cSrcweir                             aPropertyList.add(Arg);
575cdf0e10cSrcweir                             showProperty(Arg);
576cdf0e10cSrcweir                             // GlobalLogWriter.println("Printername is not null, so set to " + _aGTA.getPrinterName());
577cdf0e10cSrcweir                             aPrintable.setPrinter(PropertyHelper.createPropertyValueArrayFormArrayList(aPropertyList));
578cdf0e10cSrcweir                         }
579cdf0e10cSrcweir                     }
580cdf0e10cSrcweir 
581cdf0e10cSrcweir                     // set property values for XPrintable.print()
582cdf0e10cSrcweir                     // more can be found at "http://api.openoffice.org/docs/common/ref/com/sun/star/view/PrintOptions.html"
583cdf0e10cSrcweir 
584cdf0e10cSrcweir                     // int nProperties = 1;                    // default for 'FileName' property
585cdf0e10cSrcweir                     // if (_aGTA.printAllPages() == false)
586cdf0e10cSrcweir                     // {
587cdf0e10cSrcweir                     //     // we don't want to print all pages, build Pages string by ourself
588cdf0e10cSrcweir                     //     nProperties ++;
589cdf0e10cSrcweir                     // }
590cdf0e10cSrcweir                     // int nPropsCount = 0;
591cdf0e10cSrcweir 
592cdf0e10cSrcweir                     // If we are a SpreadSheet (calc), we need to set PrintAllSheets property to 'true'
593cdf0e10cSrcweir                     XServiceInfo xServiceInfo =  UnoRuntime.queryInterface( XServiceInfo.class, _aDoc );
594cdf0e10cSrcweir                     if ( xServiceInfo.supportsService( "com.sun.star.sheet.SpreadsheetDocument" ) )
595cdf0e10cSrcweir                     {
596cdf0e10cSrcweir                         XMultiServiceFactory xMSF = _aGTA.getMultiServiceFactory();
597cdf0e10cSrcweir                         Object aSettings = xMSF.createInstance( "com.sun.star.sheet.GlobalSheetSettings" );
598cdf0e10cSrcweir                         if (aSettings != null)
599cdf0e10cSrcweir                         {
600cdf0e10cSrcweir                             XPropertySet xPropSet = UnoRuntime.queryInterface( XPropertySet.class, aSettings );
601cdf0e10cSrcweir                             xPropSet.setPropertyValue( "PrintAllSheets", new Boolean( true ) );
602cdf0e10cSrcweir                             GlobalLogWriter.println("PrintAllSheets := true");
603cdf0e10cSrcweir                         }
604cdf0e10cSrcweir                     }
605cdf0e10cSrcweir 
606cdf0e10cSrcweir                     ArrayList<PropertyValue> aPrintProps = new ArrayList<PropertyValue>();
607cdf0e10cSrcweir                     // GlobalLogWriter.println("Property FileName:=" + _sPrintFileURL);
608cdf0e10cSrcweir 
609cdf0e10cSrcweir                     // PropertyValue [] aPrintProps = new PropertyValue[nProperties];
610cdf0e10cSrcweir                     PropertyValue Arg = new PropertyValue();
611cdf0e10cSrcweir                     Arg.Name = "FileName";
612cdf0e10cSrcweir                     Arg.Value = _sPrintFileURL;
613cdf0e10cSrcweir                     // aPrintProps[nPropsCount ++] = Arg;
614cdf0e10cSrcweir                     aPrintProps.add(Arg);
615cdf0e10cSrcweir                     showProperty(Arg);
616cdf0e10cSrcweir 
617cdf0e10cSrcweir 
618cdf0e10cSrcweir                     // generate pages string
619cdf0e10cSrcweir                     if (_aGTA.printAllPages() == false)
620cdf0e10cSrcweir                     {
621cdf0e10cSrcweir                         String sPages = "";
622cdf0e10cSrcweir                         if (_aGTA.getMaxPages() > 0)
623cdf0e10cSrcweir                         {
624cdf0e10cSrcweir                             sPages = "1-" + String.valueOf(_aGTA.getMaxPages());
625cdf0e10cSrcweir                         }
626cdf0e10cSrcweir                         if (_aGTA.getOnlyPages().length() != 0)
627cdf0e10cSrcweir                         {
628cdf0e10cSrcweir                             if (sPages.length() != 0)
629cdf0e10cSrcweir                             {
630cdf0e10cSrcweir                                 sPages += ";";
631cdf0e10cSrcweir                             }
632cdf0e10cSrcweir                             sPages += String.valueOf(_aGTA.getOnlyPages());
633cdf0e10cSrcweir                         }
634cdf0e10cSrcweir 
635cdf0e10cSrcweir                         Arg = new PropertyValue();
636cdf0e10cSrcweir                         Arg.Name = "Pages";
637cdf0e10cSrcweir                         Arg.Value = sPages;
638cdf0e10cSrcweir                         aPrintProps.add(Arg);
639cdf0e10cSrcweir                         showProperty(Arg);
640cdf0e10cSrcweir                     }
641cdf0e10cSrcweir 
642cdf0e10cSrcweir                     // GlobalLogWriter.println("Start printing.");
643cdf0e10cSrcweir 
644cdf0e10cSrcweir                     _aGTA.getPerformance().startTime(PerformanceContainer.Print);
645cdf0e10cSrcweir                     aPrintable.print(PropertyHelper.createPropertyValueArrayFormArrayList(aPrintProps));
646cdf0e10cSrcweir                     TimeHelper.waitInSeconds(1, "Start waiting for print ready.");
647cdf0e10cSrcweir 
648cdf0e10cSrcweir                     GlobalLogWriter.println("Wait until document is printed.");
649cdf0e10cSrcweir                     boolean isBusy = true;
650cdf0e10cSrcweir                     int nPrintCount = 0;
651cdf0e10cSrcweir                     while (isBusy)
652cdf0e10cSrcweir                     {
653cdf0e10cSrcweir                         PropertyValue[] aPrinterProps = aPrintable.getPrinter();
654cdf0e10cSrcweir                         int nPropIndex = 0;
655cdf0e10cSrcweir                         while (!"IsBusy".equals(aPrinterProps[nPropIndex].Name))
656cdf0e10cSrcweir                         {
657cdf0e10cSrcweir                             // System.out.println(aPrinterProps[nPropIndex].Name);
658cdf0e10cSrcweir                             nPropIndex++;
659cdf0e10cSrcweir                         }
660cdf0e10cSrcweir                         isBusy = (aPrinterProps[nPropIndex].Value == Boolean.TRUE) ? true : false;
661cdf0e10cSrcweir                         TimeHelper.waitInSeconds(1, "is print ready?");
662cdf0e10cSrcweir                         nPrintCount++;
663cdf0e10cSrcweir                         if (nPrintCount > 3600)
664cdf0e10cSrcweir                         {
665cdf0e10cSrcweir                             // we will never wait >1h until print is ready!
666cdf0e10cSrcweir                             GlobalLogWriter.println("ERROR: Cancel print due to too long wait.");
667cdf0e10cSrcweir                             throw new com.sun.star.uno.Exception("Convwatch exception, wait too long for printing.");
668cdf0e10cSrcweir                         }
669cdf0e10cSrcweir                     }
670cdf0e10cSrcweir // TODO:
671cdf0e10cSrcweir //                    TimeHelper.waitInSeconds(40, "Start waiting after print ready.");
672cdf0e10cSrcweir 
673cdf0e10cSrcweir                     _aGTA.getPerformance().stopTime(PerformanceContainer.Print);
674cdf0e10cSrcweir                     GlobalLogWriter.println(DateHelper.getDateTimeForHumanreadableLog() + " Print document done.");
675cdf0e10cSrcweir 
676cdf0e10cSrcweir                     // Create a .info file near the printed '.ps' or '.prn' file.
677cdf0e10cSrcweir                     FileHelper.createInfoFile(_sPrintFileURL, _aGTA);
678cdf0e10cSrcweir                 }
679cdf0e10cSrcweir                 else
680cdf0e10cSrcweir                 {
681cdf0e10cSrcweir                     GlobalLogWriter.println("Can't get XPrintable interface.");
682cdf0e10cSrcweir                 }
683cdf0e10cSrcweir                 bFailed = false;
684cdf0e10cSrcweir                 bBack = true;
685cdf0e10cSrcweir             }
686cdf0e10cSrcweir             catch ( com.sun.star.uno.Exception e )
687cdf0e10cSrcweir             {
688cdf0e10cSrcweir                 // Some exception occures.FAILED
689cdf0e10cSrcweir                 GlobalLogWriter.println("UNO Exception caught.");
690cdf0e10cSrcweir                 GlobalLogWriter.println("Message: " + e.getMessage());
691cdf0e10cSrcweir 
692cdf0e10cSrcweir                 e.printStackTrace();
693cdf0e10cSrcweir                 bBack = false;
694cdf0e10cSrcweir             }
695cdf0e10cSrcweir 
696cdf0e10cSrcweir             if (bFailed == true)
697cdf0e10cSrcweir             {
698cdf0e10cSrcweir                 GlobalLogWriter.println("convwatch.OfficePrint: FAILED");
699cdf0e10cSrcweir             }
700cdf0e10cSrcweir             else
701cdf0e10cSrcweir             {
702cdf0e10cSrcweir                 GlobalLogWriter.println("convwatch.OfficePrint: OK");
703cdf0e10cSrcweir             }
704cdf0e10cSrcweir             return bBack;
705cdf0e10cSrcweir         }
706cdf0e10cSrcweir 
707cdf0e10cSrcweir 
708cdf0e10cSrcweir     /**
709cdf0e10cSrcweir      * @param _aGTA
710cdf0e10cSrcweir      * @param _sAbsoluteOutputPath
711cdf0e10cSrcweir      * @param _sAbsoluteInputFile
712cdf0e10cSrcweir      * @return true, if the reference (*.prrn file) based on given output path and given input path exist.
713cdf0e10cSrcweir      *               If OVERWRITE_REFERENCE is set, always return false.
714cdf0e10cSrcweir      */
isReferenceExists(ParameterHelper _aGTA, String _sAbsoluteOutputPath, String _sAbsoluteInputFile)715cdf0e10cSrcweir     public boolean isReferenceExists(ParameterHelper _aGTA,
716cdf0e10cSrcweir                                             String _sAbsoluteOutputPath,
717cdf0e10cSrcweir                                             String _sAbsoluteInputFile)
718cdf0e10cSrcweir         {
719cdf0e10cSrcweir             if (! FileHelper.exists(_sAbsoluteInputFile))
720cdf0e10cSrcweir             {
721cdf0e10cSrcweir                 // throw new ConvWatchCancelException("Input file: " + _sAbsoluteInputFile + " does not exist.");
722cdf0e10cSrcweir                 return false;
723cdf0e10cSrcweir             }
724cdf0e10cSrcweir 
725cdf0e10cSrcweir             // String fs = System.getProperty("file.separator");
726cdf0e10cSrcweir 
727cdf0e10cSrcweir             // String sInputFileURL = URLHelper.getFileURLFromSystemPath(_sAbsoluteInputFile);
728cdf0e10cSrcweir 
729cdf0e10cSrcweir             String sInputFileBasename = FileHelper.getBasename(_sAbsoluteInputFile);
730cdf0e10cSrcweir             // String sOutputFileURL = null;
731cdf0e10cSrcweir             String sOutputPath;
732cdf0e10cSrcweir             if (_sAbsoluteOutputPath != null)
733cdf0e10cSrcweir             {
734cdf0e10cSrcweir                 sOutputPath    = _sAbsoluteOutputPath;
735cdf0e10cSrcweir                 // FileHelper.makeDirectories("", sOutputPath);
736cdf0e10cSrcweir             }
737cdf0e10cSrcweir             else
738cdf0e10cSrcweir             {
739cdf0e10cSrcweir                 String sInputPath = FileHelper.getPath(_sAbsoluteInputFile);
740cdf0e10cSrcweir                 sOutputPath    = sInputPath;
741cdf0e10cSrcweir             }
742cdf0e10cSrcweir             // sOutputFileURL = URLHelper.getFileURLFromSystemPath(sOutputPath + fs + sInputFileBasename);
743cdf0e10cSrcweir             // sOutputFileURL = null;
744cdf0e10cSrcweir 
745cdf0e10cSrcweir             String sPrintFilename = FileHelper.getNameNoSuffix(sInputFileBasename);
746cdf0e10cSrcweir             // String sPrintFileURL;
747cdf0e10cSrcweir 
748cdf0e10cSrcweir             String sAbsolutePrintFilename = FileHelper.appendPath(sOutputPath, sPrintFilename + ".prn");
749cdf0e10cSrcweir             if (FileHelper.exists(sAbsolutePrintFilename) && _aGTA.getOverwrite() == false)
750cdf0e10cSrcweir             {
751cdf0e10cSrcweir                 GlobalLogWriter.println("Reference already exist, don't overwrite. Set " + PropertyName.DOC_COMPARATOR_OVERWRITE_REFERENCE + "=true to force overwrite.");
752cdf0e10cSrcweir                 return true;
753cdf0e10cSrcweir             }
754cdf0e10cSrcweir             return false;
755cdf0e10cSrcweir         }
756cdf0e10cSrcweir 
757cdf0e10cSrcweir     // -----------------------------------------------------------------------------
758cdf0e10cSrcweir     /**
759cdf0e10cSrcweir      * create a reference file
760cdf0e10cSrcweir      * _sAbsoluteInputPath  contains the source file, if not exists, return with failure.
761cdf0e10cSrcweir      * _sAbsoluteOutputPath contains the destination, where the file will store after load with StarOffice/OpenOffice.org
762cdf0e10cSrcweir      *                      if is null, print only near the Input file path
763cdf0e10cSrcweir      * _sPrintType ".prn" Print input file with StarOffice/OpenOffice.org and the default printer as PostScript
764cdf0e10cSrcweir      *
765cdf0e10cSrcweir      * @param _aGTA
766cdf0e10cSrcweir      * @return
767cdf0e10cSrcweir      */
768cdf0e10cSrcweir //    public static boolean buildReference(ParameterHelper _aGTA,
769cdf0e10cSrcweir //                                         String _sAbsoluteOutputPath,
770cdf0e10cSrcweir //                                         String _sAbsoluteInputFile)
771cdf0e10cSrcweir //        throws OfficeException
772cdf0e10cSrcweir //        {
773cdf0e10cSrcweir //            if (! FileHelper.exists(_sAbsoluteInputFile))
774cdf0e10cSrcweir //            {
775cdf0e10cSrcweir //                throw new OfficeException("buildReference(): Input file: " + _sAbsoluteInputFile + " does not exist.");
776cdf0e10cSrcweir //            }
777cdf0e10cSrcweir //
778cdf0e10cSrcweir //            String fs = System.getProperty("file.separator");
779cdf0e10cSrcweir //
780cdf0e10cSrcweir //            String sInputFileURL = URLHelper.getFileURLFromSystemPath(_sAbsoluteInputFile);
781cdf0e10cSrcweir //
782cdf0e10cSrcweir //            String sInputFileBasename = FileHelper.getBasename(_sAbsoluteInputFile);
783cdf0e10cSrcweir //            String sOutputFileURL = null;
784cdf0e10cSrcweir //            String sOutputPath;
785cdf0e10cSrcweir //            if (_sAbsoluteOutputPath != null)
786cdf0e10cSrcweir //            {
787cdf0e10cSrcweir //                sOutputPath    = _sAbsoluteOutputPath;
788cdf0e10cSrcweir //                FileHelper.makeDirectories("", sOutputPath);
789cdf0e10cSrcweir //            }
790cdf0e10cSrcweir //            else
791cdf0e10cSrcweir //            {
792cdf0e10cSrcweir //                String sInputPath = FileHelper.getPath(_sAbsoluteInputFile);
793cdf0e10cSrcweir //                sOutputPath    = sInputPath;
794cdf0e10cSrcweir //            }
795cdf0e10cSrcweir //            // sOutputFileURL = URLHelper.getFileURLFromSystemPath(sOutputPath + fs + sInputFileBasename);
796cdf0e10cSrcweir //            sOutputFileURL = null;
797cdf0e10cSrcweir //
798cdf0e10cSrcweir //            String sPrintFilename = FileHelper.getNameNoSuffix(sInputFileBasename);
799cdf0e10cSrcweir //            String sPrintFileURL;
800cdf0e10cSrcweir //
801cdf0e10cSrcweir //            String sAbsolutePrintFilename = sOutputPath + fs + sPrintFilename + ".prn";
802cdf0e10cSrcweir //            if (FileHelper.exists(sAbsolutePrintFilename) && _aGTA.getOverwrite() == false)
803cdf0e10cSrcweir //            {
804cdf0e10cSrcweir //                GlobalLogWriter.println("Reference already exist, don't overwrite. Set " + PropertyName.DOC_COMPARATOR_OVERWRITE_REFERENCE + "=true to force overwrite.");
805cdf0e10cSrcweir //                return true;
806cdf0e10cSrcweir //            }
807cdf0e10cSrcweir //
808cdf0e10cSrcweir //            if (_aGTA.getReferenceType().toLowerCase().equals("msoffice"))
809cdf0e10cSrcweir //            {
810cdf0e10cSrcweir //                sPrintFileURL = URLHelper.getFileURLFromSystemPath(sAbsolutePrintFilename);
811cdf0e10cSrcweir //            }
812cdf0e10cSrcweir //            else if (_aGTA.getReferenceType().toLowerCase().equals("pdf"))
813cdf0e10cSrcweir //            {
814cdf0e10cSrcweir ////  TODO: If we rename the stored file to *.pdf, we have to be sure that we use *.pdf also as a available reference
815cdf0e10cSrcweir //                sPrintFileURL = URLHelper.getFileURLFromSystemPath(sAbsolutePrintFilename );
816cdf0e10cSrcweir //            }
817cdf0e10cSrcweir //            else if (_aGTA.getReferenceType().toLowerCase().equals("ooo"))
818cdf0e10cSrcweir //            {
819cdf0e10cSrcweir //                sPrintFileURL = URLHelper.getFileURLFromSystemPath(sAbsolutePrintFilename );
820cdf0e10cSrcweir //            }
821cdf0e10cSrcweir //            else
822cdf0e10cSrcweir //            {
823cdf0e10cSrcweir //                GlobalLogWriter.println("OfficePrint.buildreference(): Unknown print type.");
824cdf0e10cSrcweir //                return false;
825cdf0e10cSrcweir //            }
826cdf0e10cSrcweir //            return printToFile(_aGTA, sInputFileURL, sOutputFileURL, sPrintFileURL);
827cdf0e10cSrcweir //        }
828cdf0e10cSrcweir 
829cdf0e10cSrcweir 
830cdf0e10cSrcweir 
831cdf0e10cSrcweir     // TODO: Das Teil muss hier raus!
832cdf0e10cSrcweir 
833cdf0e10cSrcweir 
834cdf0e10cSrcweir //    public static boolean printToFile(ParameterHelper _aGTA,
835cdf0e10cSrcweir //                                      String _sInputFileURL,
836cdf0e10cSrcweir //                                      String _sOutputFileURL,
837cdf0e10cSrcweir //                                      String _sPrintFileURL) throws OfficeException
838cdf0e10cSrcweir //        {
839cdf0e10cSrcweir //            boolean bBack = false;
840cdf0e10cSrcweir //            String sPrintFileURL = null;
841cdf0e10cSrcweir //
842cdf0e10cSrcweir //
843cdf0e10cSrcweir //            // remember the current timer, to know how long a print process need.
844cdf0e10cSrcweir //            // startTimer();
845cdf0e10cSrcweir //
846cdf0e10cSrcweir //            if (_aGTA.getReferenceType().toLowerCase().equals("ooo"))
847cdf0e10cSrcweir //            {
848cdf0e10cSrcweir //                bBack = printToFileWithOOo(_aGTA, _sInputFileURL, _sOutputFileURL, _sPrintFileURL);
849cdf0e10cSrcweir //            }
850cdf0e10cSrcweir //            else if (_aGTA.getReferenceType().toLowerCase().equals("pdf"))
851cdf0e10cSrcweir //            {
852cdf0e10cSrcweir //                GlobalLogWriter.println("USE PDF AS EXPORT FORMAT.");
853cdf0e10cSrcweir //                bBack = storeAsPDF(_aGTA, _sInputFileURL, _sPrintFileURL);
854cdf0e10cSrcweir //            }
855cdf0e10cSrcweir //            else if (_aGTA.getReferenceType().toLowerCase().equals("msoffice"))
856cdf0e10cSrcweir //            {
857cdf0e10cSrcweir //                if (MSOfficePostscriptCreator.isMSOfficeDocumentFormat(_sInputFileURL))
858cdf0e10cSrcweir //                {
859cdf0e10cSrcweir //                    GlobalLogWriter.println("USE MSOFFICE AS EXPORT FORMAT.");
860cdf0e10cSrcweir //                    MSOfficePostscriptCreator a = new MSOfficePostscriptCreator();
861cdf0e10cSrcweir //                    try
862cdf0e10cSrcweir //                    {
863cdf0e10cSrcweir //                        a.printToFileWithMSOffice(_aGTA, FileHelper.getSystemPathFromFileURL(_sInputFileURL),
864cdf0e10cSrcweir //                                                  FileHelper.getSystemPathFromFileURL(_sPrintFileURL));
865cdf0e10cSrcweir //                    }
866cdf0e10cSrcweir //                    catch(OfficeException e)
867cdf0e10cSrcweir //                    {
868cdf0e10cSrcweir //                        e.printStackTrace();
869cdf0e10cSrcweir //                        GlobalLogWriter.println(e.getMessage());
870cdf0e10cSrcweir //                        throw new OfficeException("Exception caught. Problem with MSOffice printer methods.");
871cdf0e10cSrcweir //                    }
872cdf0e10cSrcweir //                    catch(java.io.IOException e)
873cdf0e10cSrcweir //                    {
874cdf0e10cSrcweir //                        GlobalLogWriter.println(e.getMessage());
875cdf0e10cSrcweir //                        throw new OfficeException("IOException caught. Problem with MSOffice printer methods.");
876cdf0e10cSrcweir //                    }
877cdf0e10cSrcweir //                    bBack = true;
878cdf0e10cSrcweir //                }
879cdf0e10cSrcweir //                else
880cdf0e10cSrcweir //                {
881cdf0e10cSrcweir //                    GlobalLogWriter.println("This document type is not recognized as MSOffice format, as default fallback StarOffice/OpenOffice.org instead is used.");
882cdf0e10cSrcweir //                    bBack = printToFileWithOOo(_aGTA, _sInputFileURL, _sOutputFileURL, _sPrintFileURL);
883cdf0e10cSrcweir //                }
884cdf0e10cSrcweir //            }
885cdf0e10cSrcweir //            else
886cdf0e10cSrcweir //            {
887cdf0e10cSrcweir //                // System.out.println("");
888cdf0e10cSrcweir //                throw new OfficeException("OfficePrint.printToFile(): Unknown print type.");
889cdf0e10cSrcweir //            }
890cdf0e10cSrcweir //            return bBack;
891cdf0e10cSrcweir //        }
892cdf0e10cSrcweir 
893cdf0e10cSrcweir     // -----------------------------------------------------------------------------
894cdf0e10cSrcweir     // TODO: move this away!
895cdf0e10cSrcweir     // -----------------------------------------------------------------------------
showType(String _sInputURL, XMultiServiceFactory _xMSF)896cdf0e10cSrcweir     void showType(String _sInputURL, XMultiServiceFactory _xMSF)
897cdf0e10cSrcweir         {
898cdf0e10cSrcweir             if (_sInputURL.length() == 0)
899cdf0e10cSrcweir             {
900cdf0e10cSrcweir                 return;
901cdf0e10cSrcweir             }
902cdf0e10cSrcweir 
903cdf0e10cSrcweir             if (_xMSF == null)
904cdf0e10cSrcweir             {
905cdf0e10cSrcweir                 GlobalLogWriter.println("MultiServiceFactory not set.");
906cdf0e10cSrcweir                 return;
907cdf0e10cSrcweir             }
908cdf0e10cSrcweir             XTypeDetection aTypeDetection = null;
909cdf0e10cSrcweir             try
910cdf0e10cSrcweir             {
911cdf0e10cSrcweir                 Object oObj = _xMSF.createInstance("com.sun.star.document.TypeDetection");
912cdf0e10cSrcweir                 aTypeDetection = UnoRuntime.queryInterface(XTypeDetection.class, oObj);
913cdf0e10cSrcweir             }
914cdf0e10cSrcweir             catch(com.sun.star.uno.Exception e)
915cdf0e10cSrcweir             {
916cdf0e10cSrcweir                 GlobalLogWriter.println("Can't get com.sun.star.document.TypeDetection.");
917cdf0e10cSrcweir                 return;
918cdf0e10cSrcweir             }
919cdf0e10cSrcweir             if (aTypeDetection != null)
920cdf0e10cSrcweir             {
921cdf0e10cSrcweir                 String sType = aTypeDetection.queryTypeByURL(_sInputURL);
922cdf0e10cSrcweir                 GlobalLogWriter.println("Type is: " + sType);
923cdf0e10cSrcweir             }
924cdf0e10cSrcweir         }
925cdf0e10cSrcweir 
926cdf0e10cSrcweir 
927cdf0e10cSrcweir     // -----------------------------------------------------------------------------
getInternalFilterName(String _sFilterName, XMultiServiceFactory _xMSF)928cdf0e10cSrcweir     public String getInternalFilterName(String _sFilterName, XMultiServiceFactory _xMSF)
929cdf0e10cSrcweir         {
930cdf0e10cSrcweir             if (_sFilterName.length() == 0)
931cdf0e10cSrcweir             {
932cdf0e10cSrcweir                 // System.out.println("No FilterName set.");
933cdf0e10cSrcweir                 return null;
934cdf0e10cSrcweir             }
935cdf0e10cSrcweir 
936cdf0e10cSrcweir             if (_xMSF == null)
937cdf0e10cSrcweir             {
938cdf0e10cSrcweir                 GlobalLogWriter.println("MultiServiceFactory not set.");
939cdf0e10cSrcweir                 return null;
940cdf0e10cSrcweir             }
941cdf0e10cSrcweir             // XFilterFactory aFilterFactory = null;
942cdf0e10cSrcweir             Object aObj = null;
943cdf0e10cSrcweir             try
944cdf0e10cSrcweir             {
945cdf0e10cSrcweir                 aObj = _xMSF.createInstance("com.sun.star.document.FilterFactory");
946cdf0e10cSrcweir             }
947cdf0e10cSrcweir             catch(com.sun.star.uno.Exception e)
948cdf0e10cSrcweir             {
949cdf0e10cSrcweir                 GlobalLogWriter.println("Can't get com.sun.star.document.FilterFactory.");
950cdf0e10cSrcweir                 return null;
951cdf0e10cSrcweir             }
952cdf0e10cSrcweir             if (aObj != null)
953cdf0e10cSrcweir             {
954cdf0e10cSrcweir                 XNameAccess aNameAccess = UnoRuntime.queryInterface(XNameAccess.class, aObj);
955cdf0e10cSrcweir                 if (aNameAccess != null)
956cdf0e10cSrcweir                 {
957cdf0e10cSrcweir 
958cdf0e10cSrcweir                     // if (_sFilterName.toLowerCase().equals("help"))
959cdf0e10cSrcweir                     // {
960cdf0e10cSrcweir                     //     System.out.println("Show all possible ElementNames from current version." );
961cdf0e10cSrcweir                     // String[] aElementNames = aNameAccess.getElementNames();
962cdf0e10cSrcweir                     // for (int i = 0; i<aElementNames.length; i++)
963cdf0e10cSrcweir                     // {
964cdf0e10cSrcweir                     //     System.out.println(aElementNames[i]);
965cdf0e10cSrcweir                     // }
966cdf0e10cSrcweir                     //     System.out.println("Must quit.");
967cdf0e10cSrcweir                     //     System.out.exit(1);
968cdf0e10cSrcweir                     // }
969cdf0e10cSrcweir 
970cdf0e10cSrcweir                     if (! aNameAccess.hasByName(_sFilterName))
971cdf0e10cSrcweir                     {
972cdf0e10cSrcweir                         GlobalLogWriter.println("FilterFactory.hasByName() says there exist no '" + _sFilterName + "'" );
973cdf0e10cSrcweir                         return null;
974cdf0e10cSrcweir                     }
975cdf0e10cSrcweir 
976cdf0e10cSrcweir                     Object[] aElements = null;
977cdf0e10cSrcweir                     String[] aExtensions;
978cdf0e10cSrcweir                     try
979cdf0e10cSrcweir                     {
980cdf0e10cSrcweir                         aElements = (Object[]) aNameAccess.getByName(_sFilterName);
981cdf0e10cSrcweir                         if (aElements != null)
982cdf0e10cSrcweir                         {
983cdf0e10cSrcweir                             String sInternalFilterName = null;
984cdf0e10cSrcweir                             // System.out.println("getByName().length: " + String.valueOf(aElements.length));
985cdf0e10cSrcweir                             for (int i=0;i<aElements.length; i++)
986cdf0e10cSrcweir                             {
987cdf0e10cSrcweir                                 PropertyValue aPropertyValue = (PropertyValue)aElements[i];
988cdf0e10cSrcweir                                 // System.out.println("PropertyValue.Name: " + aPropertyValue.Name);
989cdf0e10cSrcweir                                 if (aPropertyValue.Name.equals("Type"))
990cdf0e10cSrcweir                                 {
991cdf0e10cSrcweir                                     String sValue = (String)aPropertyValue.Value;
992cdf0e10cSrcweir                                     // System.out.println("Type: " + sValue);
993cdf0e10cSrcweir                                     sInternalFilterName = sValue;
994cdf0e10cSrcweir                                 }
995cdf0e10cSrcweir                             }
996cdf0e10cSrcweir                             return sInternalFilterName;
997cdf0e10cSrcweir                         }
998cdf0e10cSrcweir                         else
999cdf0e10cSrcweir                         {
1000cdf0e10cSrcweir                             GlobalLogWriter.println("There are no elements for FilterName '" + _sFilterName + "'");
1001cdf0e10cSrcweir                             return null;
1002cdf0e10cSrcweir                         }
1003cdf0e10cSrcweir                     }
1004cdf0e10cSrcweir                     catch (com.sun.star.container.NoSuchElementException e)
1005cdf0e10cSrcweir                     {
1006cdf0e10cSrcweir                         GlobalLogWriter.println("NoSuchElementException caught. " + e.getMessage());
1007cdf0e10cSrcweir                     }
1008cdf0e10cSrcweir                     catch (com.sun.star.lang.WrappedTargetException e)
1009cdf0e10cSrcweir                     {
1010cdf0e10cSrcweir                         GlobalLogWriter.println("WrappedTargetException caught. " + e.getMessage());
1011cdf0e10cSrcweir                     }
1012cdf0e10cSrcweir                 }
1013cdf0e10cSrcweir             }
1014cdf0e10cSrcweir             return null;
1015cdf0e10cSrcweir         }
1016cdf0e10cSrcweir 
1017cdf0e10cSrcweir     // -----------------------------------------------------------------------------
1018cdf0e10cSrcweir 
getServiceNameFromFilterName(String _sFilterName, XMultiServiceFactory _xMSF)1019cdf0e10cSrcweir     String getServiceNameFromFilterName(String _sFilterName, XMultiServiceFactory _xMSF)
1020cdf0e10cSrcweir         {
1021cdf0e10cSrcweir             if (_sFilterName.length() == 0)
1022cdf0e10cSrcweir             {
1023cdf0e10cSrcweir                 // System.out.println("No FilterName set.");
1024cdf0e10cSrcweir                 return null;
1025cdf0e10cSrcweir             }
1026cdf0e10cSrcweir 
1027cdf0e10cSrcweir             if (_xMSF == null)
1028cdf0e10cSrcweir             {
1029cdf0e10cSrcweir                 GlobalLogWriter.println("MultiServiceFactory not set.");
1030cdf0e10cSrcweir                 return null;
1031cdf0e10cSrcweir             }
1032cdf0e10cSrcweir             // XFilterFactory aFilterFactory = null;
1033cdf0e10cSrcweir             Object aObj = null;
1034cdf0e10cSrcweir             try
1035cdf0e10cSrcweir             {
1036cdf0e10cSrcweir                 aObj = _xMSF.createInstance("com.sun.star.document.FilterFactory");
1037cdf0e10cSrcweir             }
1038cdf0e10cSrcweir             catch(com.sun.star.uno.Exception e)
1039cdf0e10cSrcweir             {
1040cdf0e10cSrcweir                 GlobalLogWriter.println("Can't get com.sun.star.document.FilterFactory.");
1041cdf0e10cSrcweir                 return null;
1042cdf0e10cSrcweir             }
1043cdf0e10cSrcweir             if (aObj != null)
1044cdf0e10cSrcweir             {
1045cdf0e10cSrcweir                 XNameAccess aNameAccess = UnoRuntime.queryInterface(XNameAccess.class, aObj);
1046cdf0e10cSrcweir                 if (aNameAccess != null)
1047cdf0e10cSrcweir                 {
1048cdf0e10cSrcweir                     if (! aNameAccess.hasByName(_sFilterName))
1049cdf0e10cSrcweir                     {
1050cdf0e10cSrcweir                         GlobalLogWriter.println("FilterFactory.hasByName() says there exist no '" + _sFilterName + "'" );
1051cdf0e10cSrcweir                         return null;
1052cdf0e10cSrcweir                     }
1053cdf0e10cSrcweir 
1054cdf0e10cSrcweir                     Object[] aElements = null;
1055cdf0e10cSrcweir                     String[] aExtensions;
1056cdf0e10cSrcweir                     try
1057cdf0e10cSrcweir                     {
1058cdf0e10cSrcweir                         aElements = (Object[]) aNameAccess.getByName(_sFilterName);
1059cdf0e10cSrcweir                         if (aElements != null)
1060cdf0e10cSrcweir                         {
1061cdf0e10cSrcweir                             String sServiceName = null;
1062cdf0e10cSrcweir                             // System.out.println("getByName().length: " + String.valueOf(aElements.length));
1063cdf0e10cSrcweir                             for (int i=0;i<aElements.length; i++)
1064cdf0e10cSrcweir                             {
1065cdf0e10cSrcweir                                 PropertyValue aPropertyValue = (PropertyValue)aElements[i];
1066cdf0e10cSrcweir                                 if (aPropertyValue.Name.equals("DocumentService"))
1067cdf0e10cSrcweir                                 {
1068cdf0e10cSrcweir                                     String sValue = (String)aPropertyValue.Value;
1069cdf0e10cSrcweir                                     // System.out.println("DocumentService: " + sValue);
1070cdf0e10cSrcweir                                     sServiceName = sValue;
1071cdf0e10cSrcweir                                     break;
1072cdf0e10cSrcweir                                 }
1073cdf0e10cSrcweir                             }
1074cdf0e10cSrcweir                             return sServiceName;
1075cdf0e10cSrcweir                         }
1076cdf0e10cSrcweir                         else
1077cdf0e10cSrcweir                         {
1078cdf0e10cSrcweir                             GlobalLogWriter.println("There are no elements for FilterName '" + _sFilterName + "'");
1079cdf0e10cSrcweir                             return null;
1080cdf0e10cSrcweir                         }
1081cdf0e10cSrcweir                     }
1082cdf0e10cSrcweir                     catch (com.sun.star.container.NoSuchElementException e)
1083cdf0e10cSrcweir                     {
1084cdf0e10cSrcweir                         GlobalLogWriter.println("NoSuchElementException caught. " + e.getMessage());
1085cdf0e10cSrcweir                     }
1086cdf0e10cSrcweir                     catch (com.sun.star.lang.WrappedTargetException e)
1087cdf0e10cSrcweir                     {
1088cdf0e10cSrcweir                         GlobalLogWriter.println("WrappedTargetException caught. " + e.getMessage());
1089cdf0e10cSrcweir                     }
1090cdf0e10cSrcweir                 }
1091cdf0e10cSrcweir             }
1092cdf0e10cSrcweir             return null;
1093cdf0e10cSrcweir         }
1094cdf0e10cSrcweir     // -----------------------------------------------------------------------------
1095cdf0e10cSrcweir 
getFileExtension(String _sInternalFilterName, XMultiServiceFactory _xMSF)1096cdf0e10cSrcweir     public static String getFileExtension(String _sInternalFilterName, XMultiServiceFactory _xMSF)
1097cdf0e10cSrcweir         {
1098cdf0e10cSrcweir             if (_sInternalFilterName.length() == 0)
1099cdf0e10cSrcweir             {
1100cdf0e10cSrcweir                 // System.out.println("No FilterName set.");
1101cdf0e10cSrcweir                 return null;
1102cdf0e10cSrcweir             }
1103cdf0e10cSrcweir 
1104cdf0e10cSrcweir             if (_xMSF == null)
1105cdf0e10cSrcweir             {
1106cdf0e10cSrcweir                 GlobalLogWriter.println("MultiServiceFactory not set.");
1107cdf0e10cSrcweir                 return null;
1108cdf0e10cSrcweir             }
1109cdf0e10cSrcweir             XTypeDetection aTypeDetection = null;
1110cdf0e10cSrcweir             try
1111cdf0e10cSrcweir             {
1112cdf0e10cSrcweir                 Object oObj = _xMSF.createInstance("com.sun.star.document.TypeDetection");
1113cdf0e10cSrcweir                 aTypeDetection = UnoRuntime.queryInterface(XTypeDetection.class, oObj);
1114cdf0e10cSrcweir             }
1115cdf0e10cSrcweir             catch(com.sun.star.uno.Exception e)
1116cdf0e10cSrcweir             {
1117cdf0e10cSrcweir                 GlobalLogWriter.println("Can't get com.sun.star.document.TypeDetection.");
1118cdf0e10cSrcweir                 return null;
1119cdf0e10cSrcweir             }
1120cdf0e10cSrcweir             if (aTypeDetection != null)
1121cdf0e10cSrcweir             {
1122cdf0e10cSrcweir                 XNameAccess aNameAccess = UnoRuntime.queryInterface(XNameAccess.class, aTypeDetection);
1123cdf0e10cSrcweir                 if (aNameAccess != null)
1124cdf0e10cSrcweir                 {
1125cdf0e10cSrcweir 
1126cdf0e10cSrcweir                     // System.out.println("Show ElementNames" );
1127cdf0e10cSrcweir                     // String[] aElementNames = aNameAccess.getElementNames();
1128cdf0e10cSrcweir                     // for (int i = 0; i<aElementNames.length; i++)
1129cdf0e10cSrcweir                     // {
1130cdf0e10cSrcweir                     //     System.out.println(aElementNames[i]);
1131cdf0e10cSrcweir                     // }
1132cdf0e10cSrcweir 
1133cdf0e10cSrcweir                     if (! aNameAccess.hasByName(_sInternalFilterName))
1134cdf0e10cSrcweir                     {
1135cdf0e10cSrcweir                         GlobalLogWriter.println("TypeDetection.hasByName() says there exist no '" + _sInternalFilterName + "'" );
1136cdf0e10cSrcweir                         return null;
1137cdf0e10cSrcweir                     }
1138cdf0e10cSrcweir 
1139cdf0e10cSrcweir                     Object[] aElements = null;
1140cdf0e10cSrcweir                     String[] aExtensions;
1141cdf0e10cSrcweir                     try
1142cdf0e10cSrcweir                     {
1143cdf0e10cSrcweir                         aElements = (Object[]) aNameAccess.getByName(_sInternalFilterName);
1144cdf0e10cSrcweir                         if (aElements != null)
1145cdf0e10cSrcweir                         {
1146cdf0e10cSrcweir                             String sExtension = null;
1147cdf0e10cSrcweir                             // System.out.println("getByName().length: " + String.valueOf(aElements.length));
1148cdf0e10cSrcweir                             for (int i=0;i<aElements.length; i++)
1149cdf0e10cSrcweir                             {
1150cdf0e10cSrcweir                                 PropertyValue aPropertyValue = (PropertyValue)aElements[i];
1151cdf0e10cSrcweir                                 // System.out.println("PropertyValue.Name: " + aPropertyValue.Name);
1152cdf0e10cSrcweir                                 if (aPropertyValue.Name.equals("Extensions"))
1153cdf0e10cSrcweir                                 {
1154cdf0e10cSrcweir                                     aExtensions = (String[])aPropertyValue.Value;
1155cdf0e10cSrcweir                                     GlobalLogWriter.println("   Possible extensions are: " + String.valueOf(aExtensions.length));
1156cdf0e10cSrcweir                                     if (aExtensions.length > 0)
1157cdf0e10cSrcweir                                     {
1158cdf0e10cSrcweir                                         for (int j=0;j<aExtensions.length;j++)
1159cdf0e10cSrcweir                                         {
1160cdf0e10cSrcweir                                             GlobalLogWriter.println(" " + aExtensions[j]);
1161cdf0e10cSrcweir                                         }
1162cdf0e10cSrcweir                                         sExtension = aExtensions[0];
1163cdf0e10cSrcweir                                         GlobalLogWriter.println("");
1164cdf0e10cSrcweir                                     }
1165cdf0e10cSrcweir                                 }
1166cdf0e10cSrcweir                             }
1167cdf0e10cSrcweir                             return sExtension;
1168cdf0e10cSrcweir                         }
1169cdf0e10cSrcweir                         else
1170cdf0e10cSrcweir                         {
1171cdf0e10cSrcweir                             GlobalLogWriter.println("There are no elements for FilterName '" + _sInternalFilterName + "'");
1172cdf0e10cSrcweir                             return null;
1173cdf0e10cSrcweir                         }
1174cdf0e10cSrcweir                     }
1175cdf0e10cSrcweir                     catch (com.sun.star.container.NoSuchElementException e)
1176cdf0e10cSrcweir                     {
1177cdf0e10cSrcweir                         GlobalLogWriter.println("NoSuchElementException caught. " + e.getMessage());
1178cdf0e10cSrcweir                     }
1179cdf0e10cSrcweir                     catch (com.sun.star.lang.WrappedTargetException e)
1180cdf0e10cSrcweir                     {
1181cdf0e10cSrcweir                         GlobalLogWriter.println("WrappedTargetException caught. " + e.getMessage());
1182cdf0e10cSrcweir                     }
1183cdf0e10cSrcweir }
1184cdf0e10cSrcweir             }
1185cdf0e10cSrcweir             return null;
1186cdf0e10cSrcweir         }
1187cdf0e10cSrcweir 
1188cdf0e10cSrcweir     // -----------------------------------------------------------------------------
convertDocument(String _sInputFile, String _sOutputPath, ParameterHelper _aGTA)1189cdf0e10cSrcweir     public void convertDocument(String _sInputFile, String _sOutputPath, ParameterHelper _aGTA) throws OfficeException
1190cdf0e10cSrcweir         {
1191cdf0e10cSrcweir             XMultiServiceFactory xMSF = _aGTA.getMultiServiceFactory();
1192cdf0e10cSrcweir             if (xMSF == null)
1193cdf0e10cSrcweir             {
1194cdf0e10cSrcweir                 GlobalLogWriter.println("MultiServiceFactory in GraphicalTestArgument not set.");
1195cdf0e10cSrcweir                 return;
1196cdf0e10cSrcweir             }
1197cdf0e10cSrcweir 
1198cdf0e10cSrcweir             String sInputURL = URLHelper.getFileURLFromSystemPath(_sInputFile);
1199cdf0e10cSrcweir             // showType(sInputURL, xMSF);
1200cdf0e10cSrcweir             XComponent aDoc = loadFromURL( _aGTA, sInputURL);
1201cdf0e10cSrcweir             if (aDoc == null)
1202cdf0e10cSrcweir             {
1203cdf0e10cSrcweir                 GlobalLogWriter.println("Can't load document '"+ sInputURL + "'");
1204cdf0e10cSrcweir                 return;
1205cdf0e10cSrcweir             }
1206cdf0e10cSrcweir 
1207cdf0e10cSrcweir             if (_sOutputPath == null)
1208cdf0e10cSrcweir             {
1209cdf0e10cSrcweir                 GlobalLogWriter.println("Outputpath not set.");
1210cdf0e10cSrcweir                 return;
1211cdf0e10cSrcweir             }
1212cdf0e10cSrcweir 
1213cdf0e10cSrcweir             if (! isStoreAllowed())
1214cdf0e10cSrcweir             {
1215cdf0e10cSrcweir                 GlobalLogWriter.println("It's not allowed to store, check Input/Output path.");
1216cdf0e10cSrcweir                 return;
1217cdf0e10cSrcweir             }
1218cdf0e10cSrcweir //  TODO: Do we need to wait?
1219cdf0e10cSrcweir //            TimeHelper.waitInSeconds(1, "wait after loadFromURL.");
1220cdf0e10cSrcweir 
1221cdf0e10cSrcweir             XServiceInfo xServiceInfo =  UnoRuntime.queryInterface( XServiceInfo.class, aDoc );
1222cdf0e10cSrcweir             // String sFilter = getFilterName_forExcel(xServiceInfo);
1223cdf0e10cSrcweir             // System.out.println("Filter is " + sFilter);
1224cdf0e10cSrcweir 
1225cdf0e10cSrcweir             // store the document in an other directory
1226cdf0e10cSrcweir             XStorable xStorable =  UnoRuntime.queryInterface( XStorable.class, aDoc);
1227cdf0e10cSrcweir             if (xStorable == null)
1228cdf0e10cSrcweir             {
1229cdf0e10cSrcweir                 GlobalLogWriter.println("com.sun.star.frame.XStorable is null");
1230cdf0e10cSrcweir                 return;
1231cdf0e10cSrcweir             }
1232cdf0e10cSrcweir 
1233cdf0e10cSrcweir             String sFilterName = _aGTA.getExportFilterName();
1234cdf0e10cSrcweir 
1235cdf0e10cSrcweir             // check how many Properties should initialize
1236cdf0e10cSrcweir             int nPropertyCount = 0;
1237cdf0e10cSrcweir             // if (sFilterName != null && sFilterName.length() > 0)
1238cdf0e10cSrcweir             // {
1239cdf0e10cSrcweir             //     nPropertyCount ++;
1240cdf0e10cSrcweir             // }
1241cdf0e10cSrcweir 
1242cdf0e10cSrcweir             // initialize PropertyArray
1243cdf0e10cSrcweir             // PropertyValue [] aStoreProps = new PropertyValue[ nPropertyCount ];
1244cdf0e10cSrcweir             // int nPropertyIndex = 0;
1245cdf0e10cSrcweir             ArrayList<PropertyValue> aPropertyList = new ArrayList<PropertyValue>();
1246cdf0e10cSrcweir 
1247cdf0e10cSrcweir             String sExtension = "";
1248cdf0e10cSrcweir 
1249cdf0e10cSrcweir             if (sFilterName != null && sFilterName.length() > 0)
1250cdf0e10cSrcweir             {
1251cdf0e10cSrcweir                 String sInternalFilterName = getInternalFilterName(sFilterName, xMSF);
1252cdf0e10cSrcweir                 String sServiceName = getServiceNameFromFilterName(sFilterName, xMSF);
1253cdf0e10cSrcweir 
1254cdf0e10cSrcweir                 GlobalLogWriter.println("Filter detection:");
1255cdf0e10cSrcweir                 // check if service name from file filter is the same as from the loaded document
1256cdf0e10cSrcweir                 boolean bServiceFailed = false;
1257cdf0e10cSrcweir                 if (sServiceName == null || sInternalFilterName == null)
1258cdf0e10cSrcweir                 {
1259cdf0e10cSrcweir                     GlobalLogWriter.println("Given FilterName '" + sFilterName + "' seems to be unknown.");
1260cdf0e10cSrcweir                     bServiceFailed = true;
1261cdf0e10cSrcweir                 }
1262cdf0e10cSrcweir                 if (! xServiceInfo.supportsService(sServiceName))
1263cdf0e10cSrcweir                 {
1264cdf0e10cSrcweir                     GlobalLogWriter.println("Service from FilterName '" + sServiceName + "' is not supported by loaded document.");
1265cdf0e10cSrcweir                     bServiceFailed = true;
1266cdf0e10cSrcweir                 }
1267cdf0e10cSrcweir                 if (bServiceFailed == true)
1268cdf0e10cSrcweir                 {
1269cdf0e10cSrcweir                     GlobalLogWriter.println("Please check '" + PropertyName.DOC_CONVERTER_EXPORT_FILTER_NAME + "' in the property file.");
1270cdf0e10cSrcweir                     return;
1271cdf0e10cSrcweir                 }
1272cdf0e10cSrcweir 
1273cdf0e10cSrcweir                 if (sInternalFilterName != null && sInternalFilterName.length() > 0)
1274cdf0e10cSrcweir                 {
1275cdf0e10cSrcweir                     // get the FileExtension, by the filter name, if we don't get a file extension
1276cdf0e10cSrcweir                     // we assume the is also no right filter name.
1277cdf0e10cSrcweir                     sExtension = getFileExtension(sInternalFilterName, xMSF);
1278cdf0e10cSrcweir                     if (sExtension == null)
1279cdf0e10cSrcweir                     {
1280cdf0e10cSrcweir                         GlobalLogWriter.println("Can't found an extension for filtername, take it from the source.");
1281cdf0e10cSrcweir                     }
1282cdf0e10cSrcweir                 }
1283cdf0e10cSrcweir 
1284cdf0e10cSrcweir                 PropertyValue Arg = new PropertyValue();
1285cdf0e10cSrcweir                 Arg.Name = "FilterName";
1286cdf0e10cSrcweir                 Arg.Value = sFilterName;
1287cdf0e10cSrcweir                 // aStoreProps[nPropertyIndex ++] = Arg;
1288cdf0e10cSrcweir                 aPropertyList.add(Arg);
1289cdf0e10cSrcweir                 showProperty(Arg);
1290cdf0e10cSrcweir                 GlobalLogWriter.println("FilterName is set to: " + sFilterName);
1291cdf0e10cSrcweir             }
1292cdf0e10cSrcweir 
1293cdf0e10cSrcweir             String sOutputURL = "";
1294cdf0e10cSrcweir             try
1295cdf0e10cSrcweir             {
1296cdf0e10cSrcweir                 // create the new filename with the extension, which is ok to the file format
1297cdf0e10cSrcweir                 String sInputFileBasename = FileHelper.getBasename(_sInputFile);
1298cdf0e10cSrcweir                 // System.out.println("InputFileBasename " + sInputFileBasename);
1299cdf0e10cSrcweir                 String sInputFileNameNoSuffix = FileHelper.getNameNoSuffix(sInputFileBasename);
1300cdf0e10cSrcweir                 // System.out.println("InputFilename no suffix " + sInputFileNameNoSuffix);
1301cdf0e10cSrcweir                 String fs = System.getProperty("file.separator");
1302cdf0e10cSrcweir                 String sOutputFile = _sOutputPath;
1303cdf0e10cSrcweir                 if (! sOutputFile.endsWith(fs))
1304cdf0e10cSrcweir                 {
1305cdf0e10cSrcweir                     sOutputFile += fs;
1306cdf0e10cSrcweir                 }
1307cdf0e10cSrcweir                 if (sExtension != null && sExtension.length() > 0)
1308cdf0e10cSrcweir                 {
1309cdf0e10cSrcweir                     sOutputFile += sInputFileNameNoSuffix + "." + sExtension;
1310cdf0e10cSrcweir                 }
1311cdf0e10cSrcweir                 else
1312cdf0e10cSrcweir                 {
1313cdf0e10cSrcweir                     sOutputFile += sInputFileBasename;
1314cdf0e10cSrcweir                 }
1315cdf0e10cSrcweir 
1316cdf0e10cSrcweir                 if (FileHelper.exists(sOutputFile) && _aGTA.getOverwrite() == false)
1317cdf0e10cSrcweir                 {
1318cdf0e10cSrcweir                     GlobalLogWriter.println("File already exist, don't overwrite. Set " + PropertyName.DOC_COMPARATOR_OVERWRITE_REFERENCE + "=true to force overwrite.");
1319cdf0e10cSrcweir                     return;
1320cdf0e10cSrcweir                 }
1321cdf0e10cSrcweir 
1322cdf0e10cSrcweir                 sOutputURL = URLHelper.getFileURLFromSystemPath(sOutputFile);
1323cdf0e10cSrcweir 
1324cdf0e10cSrcweir                 GlobalLogWriter.println("Store document as '" + sOutputURL + "'");
1325cdf0e10cSrcweir                 xStorable.storeAsURL(sOutputURL, PropertyHelper.createPropertyValueArrayFormArrayList(aPropertyList));
1326cdf0e10cSrcweir                 GlobalLogWriter.println("Document stored.");
1327cdf0e10cSrcweir             }
1328cdf0e10cSrcweir             catch (com.sun.star.io.IOException e)
1329cdf0e10cSrcweir             {
1330cdf0e10cSrcweir                 GlobalLogWriter.println("Can't store document '" + sOutputURL + "'. Message is :'" + e.getMessage() + "'");
1331cdf0e10cSrcweir             }
1332cdf0e10cSrcweir //  TODO: Do we need to wait?
1333cdf0e10cSrcweir //            TimeHelper.waitInSeconds(1, "unknown in OfficePrint.convertDocument()");
1334cdf0e10cSrcweir 
1335cdf0e10cSrcweir         }
1336cdf0e10cSrcweir 
1337cdf0e10cSrcweir     /**
1338cdf0e10cSrcweir      *
1339cdf0e10cSrcweir      * @return false, if 'NoOffice=yes' is given
1340cdf0e10cSrcweir      */
1341cdf0e10cSrcweir //    private boolean shouldOfficeStart()
1342cdf0e10cSrcweir //    {
1343cdf0e10cSrcweir //        String sNoOffice = (String)m_aParameterHelper.getTestParameters().get( "NoOffice" );
1344cdf0e10cSrcweir //        if (sNoOffice != null)
1345cdf0e10cSrcweir //        {
1346cdf0e10cSrcweir //            if (sNoOffice.toLowerCase().startsWith("t") || sNoOffice.toLowerCase().startsWith("y"))
1347cdf0e10cSrcweir //            {
1348cdf0e10cSrcweir //                return false;
1349cdf0e10cSrcweir //            }
1350cdf0e10cSrcweir //        }
1351cdf0e10cSrcweir //        return true;
1352cdf0e10cSrcweir //    }
1353cdf0e10cSrcweir 
1354cdf0e10cSrcweir     OfficeProvider m_aProvider = null;
startOffice()1355cdf0e10cSrcweir     private void startOffice()
1356cdf0e10cSrcweir     {
1357cdf0e10cSrcweir         // SimpleFileSemaphore aSemaphore = new SimpleFileSemaphore();
1358cdf0e10cSrcweir //        if (shouldOfficeStart())
1359cdf0e10cSrcweir //        {
1360cdf0e10cSrcweir             // if (OSHelper.isWindows())
1361cdf0e10cSrcweir             // {
1362cdf0e10cSrcweir             //     aSemaphore.P(aSemaphore.getSemaphoreFile());
1363cdf0e10cSrcweir             // }
1364cdf0e10cSrcweir             m_aParameterHelper.getTestParameters().put(util.PropertyName.DONT_BACKUP_USERLAYER, Boolean.TRUE);
1365cdf0e10cSrcweir 
1366cdf0e10cSrcweir             m_aParameterHelper.getPerformance().startTime(PerformanceContainer.OfficeStart);
1367cdf0e10cSrcweir             m_aProvider = new OfficeProvider();
1368cdf0e10cSrcweir             XMultiServiceFactory xMSF = (XMultiServiceFactory) m_aProvider.getManager(m_aParameterHelper.getTestParameters());
1369cdf0e10cSrcweir             m_aParameterHelper.getTestParameters().put("ServiceFactory", xMSF);
1370cdf0e10cSrcweir             m_aParameterHelper.getPerformance().stopTime(PerformanceContainer.OfficeStart);
1371cdf0e10cSrcweir 
1372cdf0e10cSrcweir             long nStartTime = m_aParameterHelper.getPerformance().getTime(PerformanceContainer.OfficeStart);
1373cdf0e10cSrcweir             // aGTA = getParameterHelper(); // get new TestArguments
1374cdf0e10cSrcweir             m_aParameterHelper.getPerformance().setTime(PerformanceContainer.OfficeStart, nStartTime);
1375cdf0e10cSrcweir //        }
1376cdf0e10cSrcweir 
1377cdf0e10cSrcweir         // Watcher Object is need in log object to give a simple way to say if a running office is alive.
1378cdf0e10cSrcweir         // As long as a log comes, it pings the Watcher and says the office is alive, if not an
1379cdf0e10cSrcweir         // internal counter increase and at a given point (300 seconds) the office is killed.
1380cdf0e10cSrcweir         if (GlobalLogWriter.get().getWatcher() == null)
1381cdf0e10cSrcweir         {
1382cdf0e10cSrcweir             GlobalLogWriter.println("Set office watcher");
1383cdf0e10cSrcweir             OfficeWatcher aWatcher = (OfficeWatcher)m_aParameterHelper.getTestParameters().get("Watcher");
1384cdf0e10cSrcweir             GlobalLogWriter.get().setWatcher(aWatcher);
1385cdf0e10cSrcweir         }
1386cdf0e10cSrcweir     }
1387cdf0e10cSrcweir 
stopOffice()1388cdf0e10cSrcweir     private void stopOffice()
1389cdf0e10cSrcweir     {
1390cdf0e10cSrcweir         // Office shutdown
1391cdf0e10cSrcweir         if (m_aProvider != null)
1392cdf0e10cSrcweir         {
1393cdf0e10cSrcweir             String sAppExecCmd = (String)m_aParameterHelper.getTestParameters().get("AppExecutionCommand");
1394cdf0e10cSrcweir             if (sAppExecCmd != null && sAppExecCmd.length() > 0)
1395cdf0e10cSrcweir             {
1396cdf0e10cSrcweir                 m_aProvider.closeExistingOffice(m_aParameterHelper.getTestParameters(), true);
1397cdf0e10cSrcweir             }
1398cdf0e10cSrcweir             // if (OSHelper.isWindows())
1399cdf0e10cSrcweir             // {
1400cdf0e10cSrcweir             //     aSemaphore.V(aSemaphore.getSemaphoreFile());
1401cdf0e10cSrcweir             //     aSemaphore.sleep(2);
1402cdf0e10cSrcweir             //     // wait some time maybe an other process will take the semaphore
1403cdf0e10cSrcweir             //     // I know, this is absolutly dirty, but the whole convwatch is dirty and need a big cleanup.
1404cdf0e10cSrcweir             // }
1405cdf0e10cSrcweir         }
1406cdf0e10cSrcweir     }
1407cdf0e10cSrcweir 
1408cdf0e10cSrcweir     private boolean m_bStoreFile;
disallowStore()1409cdf0e10cSrcweir     public void disallowStore()
1410cdf0e10cSrcweir         {
1411cdf0e10cSrcweir             m_bStoreFile = false;
1412cdf0e10cSrcweir         }
allowStore()1413cdf0e10cSrcweir     public void allowStore()
1414cdf0e10cSrcweir         {
1415cdf0e10cSrcweir             m_bStoreFile = true;
1416cdf0e10cSrcweir         }
isStoreAllowed()1417cdf0e10cSrcweir     public boolean isStoreAllowed()
1418cdf0e10cSrcweir         {
1419cdf0e10cSrcweir         return false;
1420cdf0e10cSrcweir         // return m_bStoreFile;
1421cdf0e10cSrcweir         }
1422cdf0e10cSrcweir 
1423cdf0e10cSrcweir }
1424cdf0e10cSrcweir 
1425