1ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5ef39d40dSAndrew Rist  * distributed with this work for additional information
6ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10ef39d40dSAndrew Rist  *
11ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12ef39d40dSAndrew Rist  *
13ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18ef39d40dSAndrew Rist  * under the License.
19ef39d40dSAndrew Rist  *
20ef39d40dSAndrew Rist  *************************************************************/
21ef39d40dSAndrew Rist 
22ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package convwatch;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import convwatch.GraphicalTestArguments;
27cdf0e10cSrcweir import convwatch.OfficePrint;
28cdf0e10cSrcweir import convwatch.ConvWatchCancelException;
29cdf0e10cSrcweir import convwatch.FileHelper;
30cdf0e10cSrcweir import java.io.File;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir import helper.URLHelper;
33cdf0e10cSrcweir import com.sun.star.lang.XComponent;
34cdf0e10cSrcweir import com.sun.star.frame.XStorable;
35cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
36cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
37cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir public class GraphicalDifferenceCheck
40cdf0e10cSrcweir {
41cdf0e10cSrcweir     private static void showVersion()
42cdf0e10cSrcweir         {
43cdf0e10cSrcweir             // DEBUG only
44cdf0e10cSrcweir             if (FileHelper.isDebugEnabled())
45cdf0e10cSrcweir             {
46cdf0e10cSrcweir                 GlobalLogWriter.get().println("");
47cdf0e10cSrcweir                 GlobalLogWriter.get().println("+##############################+");
48cdf0e10cSrcweir                 GlobalLogWriter.get().println("##### THIS IS CONVWATCH    #####");
49cdf0e10cSrcweir                 GlobalLogWriter.get().println("##### Debug Version 1.0015 #####");
50cdf0e10cSrcweir                 GlobalLogWriter.get().println("+##############################+");
51cdf0e10cSrcweir                 GlobalLogWriter.get().println("");
52cdf0e10cSrcweir             }
53cdf0e10cSrcweir         }
54cdf0e10cSrcweir 
55cdf0e10cSrcweir     /**
56cdf0e10cSrcweir      * Creates references form documents used by the graphical difference check
57cdf0e10cSrcweir      *
58cdf0e10cSrcweir      * @param _sInputPath       the original document path
59cdf0e10cSrcweir      * @param _sReferencePath   the directory where the document will print as file or export as pdf.
60cdf0e10cSrcweir      *
61cdf0e10cSrcweir      * @throws  ConvWatchException if there are problems, see message
62cdf0e10cSrcweir      *
63cdf0e10cSrcweir      * Stops rest, if one creation of reference fails.
64cdf0e10cSrcweir      */
65cdf0e10cSrcweir     public static void createReferences(String _sInputPath, String _sReferencePath, GraphicalTestArguments _aGTA) throws ConvWatchException
66cdf0e10cSrcweir         {
67cdf0e10cSrcweir //!
68cdf0e10cSrcweir //            System.out.println("createReferences() InputPath: " + _sInputPath + " refpath: " + _sReferencePath);
69cdf0e10cSrcweir             showVersion();
70cdf0e10cSrcweir             File aInputPath = new File(_sInputPath);
71cdf0e10cSrcweir 
72cdf0e10cSrcweir //            System.out.println("Inputpath in file: " + aInputPath.getAbsolutePath());
73cdf0e10cSrcweir //!
74cdf0e10cSrcweir //             if (aInputPath.exists())
75cdf0e10cSrcweir //             {
76cdf0e10cSrcweir //                 System.out.println("Inputpath exists");
77cdf0e10cSrcweir //             }
78cdf0e10cSrcweir //             else
79cdf0e10cSrcweir //             {
80cdf0e10cSrcweir //                 System.out.println("Inputpath doesn't exists");
81cdf0e10cSrcweir //                 return;
82cdf0e10cSrcweir //             }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir             if (aInputPath.isDirectory())
85cdf0e10cSrcweir             {
86cdf0e10cSrcweir                 String fs = System.getProperty("file.separator");
87cdf0e10cSrcweir 
88cdf0e10cSrcweir                 String sRemovePath = aInputPath.getAbsolutePath();
89cdf0e10cSrcweir                 // a whole directory
90cdf0e10cSrcweir 
91cdf0e10cSrcweir                 Object[] aList = DirectoryHelper.traverse(_sInputPath, FileHelper.getFileFilter(), _aGTA.includeSubDirectories());
92cdf0e10cSrcweir                 for (int i=0;i<aList.length;i++)
93cdf0e10cSrcweir                 {
94cdf0e10cSrcweir                     String sEntry = (String)aList[i];
95cdf0e10cSrcweir                     String sNewReferencePath = _sReferencePath + fs + FileHelper.removeFirstDirectorysAndBasenameFrom(sEntry, _sInputPath);
96cdf0e10cSrcweir                     createOneReferenceFile(sEntry, sNewReferencePath, _aGTA);
97cdf0e10cSrcweir                 }
98cdf0e10cSrcweir             }
99cdf0e10cSrcweir             else
100cdf0e10cSrcweir             {
101cdf0e10cSrcweir //!
102cdf0e10cSrcweir                 // System.out.println("No directory.");
103cdf0e10cSrcweir                 createOneReferenceFile(_sInputPath, _sReferencePath, _aGTA);
104cdf0e10cSrcweir             }
105cdf0e10cSrcweir         }
106cdf0e10cSrcweir 
107cdf0e10cSrcweir 
108cdf0e10cSrcweir     /**
109cdf0e10cSrcweir      * Creates a reference for a single document used by the graphical difference check
110cdf0e10cSrcweir      *
111cdf0e10cSrcweir      * @param _sInputFile       the original document
112cdf0e10cSrcweir      * @param _sReferencePath   the directory where the document will print as file or export as pdf.
113cdf0e10cSrcweir      *
114cdf0e10cSrcweir      * @throws  ConvWatchException if the are problems, see containing message
115cdf0e10cSrcweir      */
116cdf0e10cSrcweir     public static boolean createOneReferenceFile(String _sInputFile, String _sReferencePath, GraphicalTestArguments _aGTA) throws ConvWatchException
117cdf0e10cSrcweir         {
118cdf0e10cSrcweir             showVersion();
119cdf0e10cSrcweir             if (_aGTA != null)
120cdf0e10cSrcweir             {
121cdf0e10cSrcweir                 _aGTA.setInputFile(_sInputFile);
122cdf0e10cSrcweir             }
123cdf0e10cSrcweir             return OfficePrint.buildReference(_aGTA, _sReferencePath, _sInputFile);
124cdf0e10cSrcweir         }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir     /**
127cdf0e10cSrcweir      * Check if a reference exist
128cdf0e10cSrcweir      *
129cdf0e10cSrcweir      * @param _sInputFile       the original document
130cdf0e10cSrcweir      * @param _sReferencePath   the directory where the document will print as file or export as pdf.
131cdf0e10cSrcweir      */
132cdf0e10cSrcweir     public static boolean isReferenceExists(String _sInputFile, String _sReferencePath, GraphicalTestArguments _aGTA)
133cdf0e10cSrcweir         {
134cdf0e10cSrcweir             return OfficePrint.isReferenceExists(_aGTA, _sReferencePath, _sInputFile);
135cdf0e10cSrcweir         }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     /**
139cdf0e10cSrcweir      * Used for the comparance of graphical differences.
140cdf0e10cSrcweir      * Method compares one document (_sInputFile) with an older document of the same name in the provided directory (_sReferencePath).
141cdf0e10cSrcweir      *
142cdf0e10cSrcweir      * @param _sInputPath       the original document path
143cdf0e10cSrcweir      * @param _sOutputPath      path where the same directory structure of the given input path will create. All the result documents
144cdf0e10cSrcweir      *                          needed very much disk space (up to 10MB per page).
145cdf0e10cSrcweir      *                          The path _sOutputPath must be writeable.
146cdf0e10cSrcweir      * @param _sReferencePath   the directory where the document will print as file or export as pdf.
147*e6b649b5SPedro Giffuni      * @param _aGTA             Helper class for lot of parameter to control the office.
148cdf0e10cSrcweir      *
149cdf0e10cSrcweir      * Disadvantage: stops rest if one test file has a problem.
150cdf0e10cSrcweir      */
151cdf0e10cSrcweir     public static boolean check(String _sInputPath, String _sOutputPath, String _sReferencePath, GraphicalTestArguments _aGTA ) throws ConvWatchException
152cdf0e10cSrcweir         {
153cdf0e10cSrcweir             return check(_sInputPath, _sOutputPath, _sReferencePath, null, _aGTA);
154cdf0e10cSrcweir         }
155cdf0e10cSrcweir 
156cdf0e10cSrcweir     /**
157cdf0e10cSrcweir      * Used for the comparance of graphical differences.
158cdf0e10cSrcweir      * Method compares one document (_sInputFile) with an older document of the same name in the provided directory (_sReferencePath).
159cdf0e10cSrcweir      *
160cdf0e10cSrcweir      * @param _sInputPath       the original document path
161cdf0e10cSrcweir      * @param _sReferencePath   the directory where the document will print as file or export as pdf.
162cdf0e10cSrcweir      * @param _sOutputPath      path where the same directory structure of the given input path will create. All the result documents
163cdf0e10cSrcweir      *                          needed very much disk space (up to 10MB per page).
164cdf0e10cSrcweir      *                          The path _sOutputPath must be writeable.
165cdf0e10cSrcweir      * @param _sDiffPath        Path to older differences.
166*e6b649b5SPedro Giffuni      * @param _aGTA             Helper class for lot of parameter to control the office.
167cdf0e10cSrcweir      *
168cdf0e10cSrcweir      *
169cdf0e10cSrcweir      * Stops all, if one creation of reference fails
170cdf0e10cSrcweir      */
171cdf0e10cSrcweir     public static boolean check(String _sInputPath, String _sOutputPath, String _sReferencePath, String _sDiffPath, GraphicalTestArguments _aGTA ) throws ConvWatchException
172cdf0e10cSrcweir         {
173cdf0e10cSrcweir             showVersion();
174cdf0e10cSrcweir 
175cdf0e10cSrcweir             boolean bOk = true;
176cdf0e10cSrcweir 
177cdf0e10cSrcweir             File aInputPath = new File(_sInputPath);
178cdf0e10cSrcweir             if (aInputPath.isDirectory())
179cdf0e10cSrcweir             {
180cdf0e10cSrcweir                 String fs = System.getProperty("file.separator");
181cdf0e10cSrcweir                 // a whole directory
182cdf0e10cSrcweir                 Object[] aList = DirectoryHelper.traverse(_sInputPath, FileHelper.getFileFilter(), _aGTA.includeSubDirectories());
183cdf0e10cSrcweir                 if (aList.length != 0)
184cdf0e10cSrcweir                 {
185cdf0e10cSrcweir                     for (int i=0;i<aList.length;i++)
186cdf0e10cSrcweir                     {
187cdf0e10cSrcweir                         String sEntry = (String)aList[i];
188cdf0e10cSrcweir                         String sNewSubDir = FileHelper.removeFirstDirectorysAndBasenameFrom(sEntry, _sInputPath);
189cdf0e10cSrcweir                         String sNewReferencePath = _sReferencePath;
190cdf0e10cSrcweir                         String sNewOutputPath = _sOutputPath;
191cdf0e10cSrcweir                         String sNewDiffPath = _sDiffPath;
192cdf0e10cSrcweir                         if (sNewSubDir.length() > 0)
193cdf0e10cSrcweir                         {
194cdf0e10cSrcweir                             if (sNewReferencePath != null)
195cdf0e10cSrcweir                             {
196cdf0e10cSrcweir                                 sNewReferencePath = sNewReferencePath + fs + sNewSubDir;
197cdf0e10cSrcweir                             }
198cdf0e10cSrcweir 
199cdf0e10cSrcweir                             sNewOutputPath = sNewOutputPath + fs + sNewSubDir;
200cdf0e10cSrcweir                             if (sNewDiffPath != null)
201cdf0e10cSrcweir                             {
202cdf0e10cSrcweir                                 sNewDiffPath = sNewDiffPath + fs + sNewSubDir;
203cdf0e10cSrcweir                             }
204cdf0e10cSrcweir                         }
205cdf0e10cSrcweir                         bOk &= checkOneFile(sEntry, sNewOutputPath, sNewReferencePath, sNewDiffPath, _aGTA);
206cdf0e10cSrcweir                     }
207cdf0e10cSrcweir                 }
208cdf0e10cSrcweir             }
209cdf0e10cSrcweir             else
210cdf0e10cSrcweir             {
211cdf0e10cSrcweir                 bOk = /* GraphicalDifferenceCheck.*/ checkOneFile(_sInputPath, _sOutputPath, _sReferencePath, _sDiffPath, _aGTA);
212cdf0e10cSrcweir             }
213cdf0e10cSrcweir             return bOk;
214cdf0e10cSrcweir         }
215cdf0e10cSrcweir 
216cdf0e10cSrcweir     /**
217cdf0e10cSrcweir      * Used for the comparance of graphical differences.
218cdf0e10cSrcweir      * Method compares one document (_sInputFile) with an older document of the same name in the provided directory (_sReferencePath).
219cdf0e10cSrcweir      *
220cdf0e10cSrcweir      * The path _sOutputPath must be writeable
221cdf0e10cSrcweir      */
222cdf0e10cSrcweir     public static boolean checkOneFile(String _sInputFile, String _sOutputPath, String _sReferencePath, GraphicalTestArguments _aGTA)  throws ConvWatchException
223cdf0e10cSrcweir     {
224cdf0e10cSrcweir         return checkOneFile( _sInputFile, _sOutputPath, _sReferencePath, null, _aGTA);
225cdf0e10cSrcweir     }
226cdf0e10cSrcweir 
227cdf0e10cSrcweir 
228cdf0e10cSrcweir     /**
229cdf0e10cSrcweir      * Used for the comparance of graphical differences.
230cdf0e10cSrcweir      * Method compares one document (_sInputFile) with an older document of the same name in the provided directory (_sReferencePath).
231cdf0e10cSrcweir      *
232cdf0e10cSrcweir      * For scenarios, where a difference is known and further changes are of interest, differences itself can be compared.
233cdf0e10cSrcweir      * This functionality is provided by the difference path parameter (_sDiffPath). If set, the difference of the current comparance (between input and reference),
234cdf0e10cSrcweir      * will be compared with the (same named) difference document from a earlier comparance.
235cdf0e10cSrcweir      *
236cdf0e10cSrcweir      * The path _sOutputPath must be writeable
237cdf0e10cSrcweir      */
238cdf0e10cSrcweir     public static boolean checkOneFile(String _sInputFile, String _sOutputPath, String _sReferencePath, String _sDiffPath, GraphicalTestArguments _aGTA ) throws ConvWatchException
239cdf0e10cSrcweir         {
240cdf0e10cSrcweir             showVersion();
241cdf0e10cSrcweir             if (_aGTA != null)
242cdf0e10cSrcweir             {
243cdf0e10cSrcweir                 _aGTA.setInputFile(_sInputFile);
244cdf0e10cSrcweir             }
245cdf0e10cSrcweir 
246cdf0e10cSrcweir             boolean bOk = false;
247cdf0e10cSrcweir             if (_sDiffPath != null)
248cdf0e10cSrcweir             {
249cdf0e10cSrcweir                 // check with an old diff
250cdf0e10cSrcweir                 bOk = convwatch.ConvWatch.checkDiffDiff(_aGTA, _sOutputPath, _sInputFile, _sReferencePath, _sDiffPath);
251cdf0e10cSrcweir             }
252cdf0e10cSrcweir             else
253cdf0e10cSrcweir             {
254cdf0e10cSrcweir                 // one file
255cdf0e10cSrcweir                 bOk = convwatch.ConvWatch.check(_aGTA, _sOutputPath, _sInputFile, _sReferencePath);
256cdf0e10cSrcweir             }
257cdf0e10cSrcweir             return bOk;
258cdf0e10cSrcweir         }
259cdf0e10cSrcweir 
260cdf0e10cSrcweir     /**
261cdf0e10cSrcweir      * Instead of providing a saved document for graphical comparance a StarOffice xComponent
262cdf0e10cSrcweir      * will be saved and afterwards compared.
263cdf0e10cSrcweir      *
264cdf0e10cSrcweir      * @param xComponent        the test document to be compared as StarOffice component
265cdf0e10cSrcweir      * @param _sOutputPath      Path where test results are supposed to been saved. The path _sOutputPath must be writeable.
266cdf0e10cSrcweir      *                          These documents need sufficient disk space (up to 10MB per page).
267cdf0e10cSrcweir      *                          A directory structure will be created, which is a mirrored from input path.
268cdf0e10cSrcweir      *
269*e6b649b5SPedro Giffuni      * @param _resultDocName    Name by which the xComponent shall be saved as OpenOffice.org XML document.
270cdf0e10cSrcweir      *                          If provided without suffix, the suffix will be derived from the export filter.
271cdf0e10cSrcweir      * @param _sReferencePath   the directory where the document will print as file or export as pdf.
272*e6b649b5SPedro Giffuni      * @param _aGTA             Helper class for lot of parameter to control the office.
273cdf0e10cSrcweir      */
274cdf0e10cSrcweir     public static boolean checkOneFile(XComponent xComponent, String _sOutputPath, String _resultDocName, String _sReferencePath, GraphicalTestArguments _aGTA ) throws ConvWatchException
275cdf0e10cSrcweir         {
276cdf0e10cSrcweir             showVersion();
277cdf0e10cSrcweir 
278cdf0e10cSrcweir             // one file
279cdf0e10cSrcweir             String sInputFile;
280cdf0e10cSrcweir             sInputFile = createInputFile(xComponent, _sOutputPath, _resultDocName);
281cdf0e10cSrcweir             sInputFile = FileHelper.getSystemPathFromFileURL(sInputFile);
282cdf0e10cSrcweir             return convwatch.ConvWatch.check(_aGTA, _sOutputPath, sInputFile, _sReferencePath);
283cdf0e10cSrcweir         }
284cdf0e10cSrcweir 
285cdf0e10cSrcweir 
286cdf0e10cSrcweir // LLA: old!     /**
287cdf0e10cSrcweir // LLA: old!      * Returns 'true' if a reference document on the specific output path exists.
288cdf0e10cSrcweir // LLA: old!      * The name of the document is corresponding to the input document, which can be
289cdf0e10cSrcweir // LLA: old!      * provided by a single name or path.
290cdf0e10cSrcweir // LLA: old!      *
291cdf0e10cSrcweir // LLA: old!      * @param inputPath       the original document name (possibly including path)
292cdf0e10cSrcweir // LLA: old!      * @param referencePath   the directory where the reference document will be stored
293cdf0e10cSrcweir // LLA: old!
294cdf0e10cSrcweir // LLA: old!      */
295cdf0e10cSrcweir // LLA: old!     public static boolean isReferencExistent(String inputDocumentPath, String referencePath)
296cdf0e10cSrcweir // LLA: old!     {
297cdf0e10cSrcweir // LLA: old! 		// isolate the document name
298cdf0e10cSrcweir // LLA: old!         if(inputDocumentPath.indexOf(File.separator) != -1)
299cdf0e10cSrcweir // LLA: old!             inputDocumentPath = inputDocumentPath.substring(inputDocumentPath.lastIndexOf(File.separator) + 1, inputDocumentPath.length());
300cdf0e10cSrcweir // LLA: old!
301cdf0e10cSrcweir // LLA: old! 		// exchange any arbitray suffix against the refence suffix (.prn)
302cdf0e10cSrcweir // LLA: old!         if(inputDocumentPath.indexOf('.') != -1)
303cdf0e10cSrcweir // LLA: old!             inputDocumentPath = inputDocumentPath.substring(0, inputDocumentPath.lastIndexOf('.'));
304cdf0e10cSrcweir // LLA: old!         inputDocumentPath = inputDocumentPath + ".prn";
305cdf0e10cSrcweir // LLA: old! System.out.println("GraphicalDifference CheckReferenceDocument: " + inputDocumentPath);
306cdf0e10cSrcweir // LLA: old!
307cdf0e10cSrcweir // LLA: old!         File refFile = new File(referencePath + inputDocumentPath);
308cdf0e10cSrcweir // LLA: old!         if(refFile.exists()){
309cdf0e10cSrcweir // LLA: old!             return true;
310cdf0e10cSrcweir // LLA: old!         }else
311cdf0e10cSrcweir // LLA: old!             return false;
312cdf0e10cSrcweir // LLA: old! 	}
313cdf0e10cSrcweir 
314cdf0e10cSrcweir 
315cdf0e10cSrcweir     private static String createInputFile(XComponent xComponent, String _sOutputPath, String resultDocName)
316cdf0e10cSrcweir         throws ConvWatchCancelException
317cdf0e10cSrcweir     {
318cdf0e10cSrcweir 
319cdf0e10cSrcweir         // find the adequate XML StarOffice output filter to save the document and adequate suffix
320cdf0e10cSrcweir         StringBuffer suffix = new StringBuffer();
321cdf0e10cSrcweir         String exportFilter = getXMLOutputFilterforXComponent(xComponent, suffix);
322cdf0e10cSrcweir         if(resultDocName == null)
323cdf0e10cSrcweir             resultDocName = "OOoTestDocument";
324cdf0e10cSrcweir         if(resultDocName.indexOf('.') == -1)
325cdf0e10cSrcweir             resultDocName = suffix.insert(0, resultDocName).toString();
326cdf0e10cSrcweir 
327cdf0e10cSrcweir         // create a result URL for storing the office document
328cdf0e10cSrcweir         String resultURL = URLHelper.getFileURLFromSystemPath(ensureEndingFileSep(_sOutputPath) + resultDocName);
329cdf0e10cSrcweir 
330cdf0e10cSrcweir         XStorable xStorable = null;
331cdf0e10cSrcweir         xStorable  = (com.sun.star.frame.XStorable)UnoRuntime.queryInterface(com.sun.star.frame.XStorable.class, xComponent);
332cdf0e10cSrcweir         if(xStorable == null)
333cdf0e10cSrcweir         {
334cdf0e10cSrcweir             throw new ConvWatchCancelException("com.sun.star.frame.XStorable could not be instantiated from the office.");
335cdf0e10cSrcweir         }
336cdf0e10cSrcweir 
337cdf0e10cSrcweir         PropertyValue pvFilterName = new PropertyValue("FilterName", -1, exportFilter, com.sun.star.beans.PropertyState.getDefault());
338cdf0e10cSrcweir         PropertyValue pvOverwrite = new PropertyValue("Overwrite", -1, new Boolean(true), com.sun.star.beans.PropertyState.getDefault());
339cdf0e10cSrcweir 
340cdf0e10cSrcweir         try
341cdf0e10cSrcweir         {
342cdf0e10cSrcweir             xStorable.storeAsURL(resultURL, new PropertyValue[]{pvFilterName, pvOverwrite});
343cdf0e10cSrcweir         }
344cdf0e10cSrcweir         catch (com.sun.star.io.IOException e)
345cdf0e10cSrcweir         {
346cdf0e10cSrcweir             // wrap IOException
347cdf0e10cSrcweir             throw new ConvWatchCancelException("Wrap IOException caught, " + e.getMessage());
348cdf0e10cSrcweir         }
349cdf0e10cSrcweir 
350cdf0e10cSrcweir         GlobalLogWriter.get().println("Saving XComponent as " + resultURL);
351cdf0e10cSrcweir 
352cdf0e10cSrcweir         return resultURL;
353cdf0e10cSrcweir     }
354cdf0e10cSrcweir 
355cdf0e10cSrcweir 
356cdf0e10cSrcweir     private static String getXMLOutputFilterforXComponent(XComponent xComponent, StringBuffer suffix){
357cdf0e10cSrcweir         XServiceInfo xSI = (XServiceInfo) UnoRuntime.queryInterface(XServiceInfo.class, xComponent);
358cdf0e10cSrcweir         if (xSI.supportsService("com.sun.star.text.TextDocument")){
359cdf0e10cSrcweir             resetBuffer(suffix, ".sxw");
360cdf0e10cSrcweir             return "swriter: StarOffice XML (Writer)";
361cdf0e10cSrcweir         }else if (xSI.supportsService("com.sun.star.sheet.SpreadsheetDocument")){
362cdf0e10cSrcweir             resetBuffer(suffix, ".sxc");
363cdf0e10cSrcweir             return "scalc: StarOffice XML (Calc)";
364cdf0e10cSrcweir         }else if (xSI.supportsService("com.sun.star.presentation.PresentationDocument")){
365cdf0e10cSrcweir             resetBuffer(suffix, ".sxi");
366cdf0e10cSrcweir             return "simpress: StarOffice XML (Impress)";
367cdf0e10cSrcweir         }else if(xSI.supportsService("com.sun.star.drawing.DrawingDocument")){
368cdf0e10cSrcweir             resetBuffer(suffix, ".sxd");
369cdf0e10cSrcweir             return "sdraw: StarOffice XML (Draw)";
370cdf0e10cSrcweir         }else if (xSI.supportsService("com.sun.star.formula.FormulaProperties")){
371cdf0e10cSrcweir             resetBuffer(suffix, ".sxm");
372cdf0e10cSrcweir             return "smath: StarOffice XML (Math)";
373cdf0e10cSrcweir         }
374cdf0e10cSrcweir         return null;
375cdf0e10cSrcweir     }
376cdf0e10cSrcweir 
377cdf0e10cSrcweir     private static StringBuffer resetBuffer(StringBuffer sb, String suffix)
378cdf0e10cSrcweir         {
379cdf0e10cSrcweir             if(sb != null)
380cdf0e10cSrcweir             {
381cdf0e10cSrcweir                 sb.replace(0, sb.length(), suffix);
382cdf0e10cSrcweir             }
383cdf0e10cSrcweir             return sb;
384cdf0e10cSrcweir         }
385cdf0e10cSrcweir 
386cdf0e10cSrcweir     private static String ensureEndingFileSep(String s)
387cdf0e10cSrcweir         {
388cdf0e10cSrcweir 	    if(s != null && !s.equals("") && !s.endsWith(File.separator))
389cdf0e10cSrcweir             {
390cdf0e10cSrcweir             	s = s.trim() + File.separator;
391cdf0e10cSrcweir             }
392cdf0e10cSrcweir             else
393cdf0e10cSrcweir             {
394cdf0e10cSrcweir                 if(s == null)
395cdf0e10cSrcweir                 {
396cdf0e10cSrcweir                     s = "";
397cdf0e10cSrcweir                 }
398cdf0e10cSrcweir             }
399cdf0e10cSrcweir 
400cdf0e10cSrcweir 	    return s;
401cdf0e10cSrcweir 	}
402cdf0e10cSrcweir 
403cdf0e10cSrcweir 
404cdf0e10cSrcweir }
405