1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: GraphicalDifferenceCheck.java,v $
10 * $Revision: 1.8.8.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 import convwatch
.GraphicalTestArguments
;
34 import convwatch
.OfficePrint
;
35 import convwatch
.ConvWatchCancelException
;
36 import convwatch
.FileHelper
;
39 import helper
.URLHelper
;
40 import com
.sun
.star
.lang
.XComponent
;
41 import com
.sun
.star
.frame
.XStorable
;
42 import com
.sun
.star
.beans
.PropertyValue
;
43 import com
.sun
.star
.lang
.XServiceInfo
;
44 import com
.sun
.star
.uno
.UnoRuntime
;
46 public class GraphicalDifferenceCheck
48 private static void showVersion()
51 if (FileHelper
.isDebugEnabled())
53 GlobalLogWriter
.get().println("");
54 GlobalLogWriter
.get().println("+##############################+");
55 GlobalLogWriter
.get().println("##### THIS IS CONVWATCH #####");
56 GlobalLogWriter
.get().println("##### Debug Version 1.0015 #####");
57 GlobalLogWriter
.get().println("+##############################+");
58 GlobalLogWriter
.get().println("");
63 * Creates references form documents used by the graphical difference check
65 * @param _sInputPath the original document path
66 * @param _sReferencePath the directory where the document will print as file or export as pdf.
68 * @throws ConvWatchException if there are problems, see message
70 * Stops rest, if one creation of reference fails.
72 public static void createReferences(String _sInputPath
, String _sReferencePath
, GraphicalTestArguments _aGTA
) throws ConvWatchException
75 // System.out.println("createReferences() InputPath: " + _sInputPath + " refpath: " + _sReferencePath);
77 File aInputPath
= new File(_sInputPath
);
79 // System.out.println("Inputpath in file: " + aInputPath.getAbsolutePath());
81 // if (aInputPath.exists())
83 // System.out.println("Inputpath exists");
87 // System.out.println("Inputpath doesn't exists");
91 if (aInputPath
.isDirectory())
93 String fs
= System
.getProperty("file.separator");
95 String sRemovePath
= aInputPath
.getAbsolutePath();
98 Object
[] aList
= DirectoryHelper
.traverse(_sInputPath
, FileHelper
.getFileFilter(), _aGTA
.includeSubDirectories());
99 for (int i
=0;i
<aList
.length
;i
++)
101 String sEntry
= (String
)aList
[i
];
102 String sNewReferencePath
= _sReferencePath
+ fs
+ FileHelper
.removeFirstDirectorysAndBasenameFrom(sEntry
, _sInputPath
);
103 createOneReferenceFile(sEntry
, sNewReferencePath
, _aGTA
);
109 // System.out.println("No directory.");
110 createOneReferenceFile(_sInputPath
, _sReferencePath
, _aGTA
);
116 * Creates a reference for a single document used by the graphical difference check
118 * @param _sInputFile the original document
119 * @param _sReferencePath the directory where the document will print as file or export as pdf.
121 * @throws ConvWatchException if the are problems, see containing message
123 public static boolean createOneReferenceFile(String _sInputFile
, String _sReferencePath
, GraphicalTestArguments _aGTA
) throws ConvWatchException
128 _aGTA
.setInputFile(_sInputFile
);
130 return OfficePrint
.buildReference(_aGTA
, _sReferencePath
, _sInputFile
);
134 * Check if a reference exist
136 * @param _sInputFile the original document
137 * @param _sReferencePath the directory where the document will print as file or export as pdf.
139 * @throws ConvWatchException if the are problems, see containing message
141 public static boolean isReferenceExists(String _sInputFile
, String _sReferencePath
, GraphicalTestArguments _aGTA
)
143 return OfficePrint
.isReferenceExists(_aGTA
, _sReferencePath
, _sInputFile
);
148 * Used for the comparance of graphical differences.
149 * Method compares one document (_sInputFile) with an older document of the same name in the provided directory (_sReferencePath).
151 * @param _sInputPath the original document path
152 * @param _sOutputPath path where the same directory structure of the given input path will create. All the result documents
153 * needed very much disk space (up to 10MB per page).
154 * The path _sOutputPath must be writeable.
155 * @param _sReferencePath the directory where the document will print as file or export as pdf.
156 * @param _GTA Helper class for lot of parameter to control the office.
158 * Disadvantage: stops rest if one test file has a problem.
160 public static boolean check(String _sInputPath
, String _sOutputPath
, String _sReferencePath
, GraphicalTestArguments _aGTA
) throws ConvWatchException
162 return check(_sInputPath
, _sOutputPath
, _sReferencePath
, null, _aGTA
);
166 * Used for the comparance of graphical differences.
167 * Method compares one document (_sInputFile) with an older document of the same name in the provided directory (_sReferencePath).
169 * @param _sInputPath the original document path
170 * @param _sReferencePath the directory where the document will print as file or export as pdf.
171 * @param _sOutputPath path where the same directory structure of the given input path will create. All the result documents
172 * needed very much disk space (up to 10MB per page).
173 * The path _sOutputPath must be writeable.
174 * @param _sDiffPath Path to older differences.
175 * @param _GTA Helper class for lot of parameter to control the office.
178 * Stops all, if one creation of reference fails
180 public static boolean check(String _sInputPath
, String _sOutputPath
, String _sReferencePath
, String _sDiffPath
, GraphicalTestArguments _aGTA
) throws ConvWatchException
186 File aInputPath
= new File(_sInputPath
);
187 if (aInputPath
.isDirectory())
189 String fs
= System
.getProperty("file.separator");
191 Object
[] aList
= DirectoryHelper
.traverse(_sInputPath
, FileHelper
.getFileFilter(), _aGTA
.includeSubDirectories());
192 if (aList
.length
!= 0)
194 for (int i
=0;i
<aList
.length
;i
++)
196 String sEntry
= (String
)aList
[i
];
197 String sNewSubDir
= FileHelper
.removeFirstDirectorysAndBasenameFrom(sEntry
, _sInputPath
);
198 String sNewReferencePath
= _sReferencePath
;
199 String sNewOutputPath
= _sOutputPath
;
200 String sNewDiffPath
= _sDiffPath
;
201 if (sNewSubDir
.length() > 0)
203 if (sNewReferencePath
!= null)
205 sNewReferencePath
= sNewReferencePath
+ fs
+ sNewSubDir
;
208 sNewOutputPath
= sNewOutputPath
+ fs
+ sNewSubDir
;
209 if (sNewDiffPath
!= null)
211 sNewDiffPath
= sNewDiffPath
+ fs
+ sNewSubDir
;
214 bOk
&= checkOneFile(sEntry
, sNewOutputPath
, sNewReferencePath
, sNewDiffPath
, _aGTA
);
220 bOk
= /* GraphicalDifferenceCheck.*/ checkOneFile(_sInputPath
, _sOutputPath
, _sReferencePath
, _sDiffPath
, _aGTA
);
226 * Used for the comparance of graphical differences.
227 * Method compares one document (_sInputFile) with an older document of the same name in the provided directory (_sReferencePath).
229 * The path _sOutputPath must be writeable
231 public static boolean checkOneFile(String _sInputFile
, String _sOutputPath
, String _sReferencePath
, GraphicalTestArguments _aGTA
) throws ConvWatchException
233 return checkOneFile( _sInputFile
, _sOutputPath
, _sReferencePath
, null, _aGTA
);
238 * Used for the comparance of graphical differences.
239 * Method compares one document (_sInputFile) with an older document of the same name in the provided directory (_sReferencePath).
241 * For scenarios, where a difference is known and further changes are of interest, differences itself can be compared.
242 * This functionality is provided by the difference path parameter (_sDiffPath). If set, the difference of the current comparance (between input and reference),
243 * will be compared with the (same named) difference document from a earlier comparance.
245 * The path _sOutputPath must be writeable
247 public static boolean checkOneFile(String _sInputFile
, String _sOutputPath
, String _sReferencePath
, String _sDiffPath
, GraphicalTestArguments _aGTA
) throws ConvWatchException
252 _aGTA
.setInputFile(_sInputFile
);
256 if (_sDiffPath
!= null)
258 // check with an old diff
259 bOk
= convwatch
.ConvWatch
.checkDiffDiff(_aGTA
, _sOutputPath
, _sInputFile
, _sReferencePath
, _sDiffPath
);
264 bOk
= convwatch
.ConvWatch
.check(_aGTA
, _sOutputPath
, _sInputFile
, _sReferencePath
);
270 * Instead of providing a saved document for graphical comparance a StarOffice xComponent
271 * will be saved and afterwards compared.
273 * @param xComponent the test document to be compared as StarOffice component
274 * @param _sOutputPath Path where test results are supposed to been saved. The path _sOutputPath must be writeable.
275 * These documents need sufficient disk space (up to 10MB per page).
276 * A directory structure will be created, which is a mirrored from input path.
278 * @param resultDocName Name by which the xComponent shall be saved as OpenOffice.org XML document.
279 * If provided without suffix, the suffix will be derived from the export filter.
280 * @param _sReferencePath the directory where the document will print as file or export as pdf.
281 * @param _GTA Helper class for lot of parameter to control the office.
283 public static boolean checkOneFile(XComponent xComponent
, String _sOutputPath
, String _resultDocName
, String _sReferencePath
, GraphicalTestArguments _aGTA
) throws ConvWatchException
289 sInputFile
= createInputFile(xComponent
, _sOutputPath
, _resultDocName
);
290 sInputFile
= FileHelper
.getSystemPathFromFileURL(sInputFile
);
291 return convwatch
.ConvWatch
.check(_aGTA
, _sOutputPath
, sInputFile
, _sReferencePath
);
296 // LLA: old! * Returns 'true' if a reference document on the specific output path exists.
297 // LLA: old! * The name of the document is corresponding to the input document, which can be
298 // LLA: old! * provided by a single name or path.
300 // LLA: old! * @param inputPath the original document name (possibly including path)
301 // LLA: old! * @param referencePath the directory where the reference document will be stored
304 // LLA: old! public static boolean isReferencExistent(String inputDocumentPath, String referencePath)
306 // LLA: old! // isolate the document name
307 // LLA: old! if(inputDocumentPath.indexOf(File.separator) != -1)
308 // LLA: old! inputDocumentPath = inputDocumentPath.substring(inputDocumentPath.lastIndexOf(File.separator) + 1, inputDocumentPath.length());
310 // LLA: old! // exchange any arbitray suffix against the refence suffix (.prn)
311 // LLA: old! if(inputDocumentPath.indexOf('.') != -1)
312 // LLA: old! inputDocumentPath = inputDocumentPath.substring(0, inputDocumentPath.lastIndexOf('.'));
313 // LLA: old! inputDocumentPath = inputDocumentPath + ".prn";
314 // LLA: old! System.out.println("GraphicalDifference CheckReferenceDocument: " + inputDocumentPath);
316 // LLA: old! File refFile = new File(referencePath + inputDocumentPath);
317 // LLA: old! if(refFile.exists()){
318 // LLA: old! return true;
320 // LLA: old! return false;
324 private static String
createInputFile(XComponent xComponent
, String _sOutputPath
, String resultDocName
)
325 throws ConvWatchCancelException
328 // find the adequate XML StarOffice output filter to save the document and adequate suffix
329 StringBuffer suffix
= new StringBuffer();
330 String exportFilter
= getXMLOutputFilterforXComponent(xComponent
, suffix
);
331 if(resultDocName
== null)
332 resultDocName
= "OOoTestDocument";
333 if(resultDocName
.indexOf('.') == -1)
334 resultDocName
= suffix
.insert(0, resultDocName
).toString();
336 // create a result URL for storing the office document
337 String resultURL
= URLHelper
.getFileURLFromSystemPath(ensureEndingFileSep(_sOutputPath
) + resultDocName
);
339 XStorable xStorable
= null;
340 xStorable
= (com
.sun
.star
.frame
.XStorable
)UnoRuntime
.queryInterface(com
.sun
.star
.frame
.XStorable
.class, xComponent
);
341 if(xStorable
== null)
343 throw new ConvWatchCancelException("com.sun.star.frame.XStorable could not be instantiated from the office.");
346 PropertyValue pvFilterName
= new PropertyValue("FilterName", -1, exportFilter
, com
.sun
.star
.beans
.PropertyState
.getDefault());
347 PropertyValue pvOverwrite
= new PropertyValue("Overwrite", -1, new Boolean(true), com
.sun
.star
.beans
.PropertyState
.getDefault());
351 xStorable
.storeAsURL(resultURL
, new PropertyValue
[]{pvFilterName
, pvOverwrite
});
353 catch (com
.sun
.star
.io
.IOException e
)
356 throw new ConvWatchCancelException("Wrap IOException caught, " + e
.getMessage());
359 GlobalLogWriter
.get().println("Saving XComponent as " + resultURL
);
365 private static String
getXMLOutputFilterforXComponent(XComponent xComponent
, StringBuffer suffix
){
366 XServiceInfo xSI
= (XServiceInfo
) UnoRuntime
.queryInterface(XServiceInfo
.class, xComponent
);
367 if (xSI
.supportsService("com.sun.star.text.TextDocument")){
368 resetBuffer(suffix
, ".sxw");
369 return "swriter: StarOffice XML (Writer)";
370 }else if (xSI
.supportsService("com.sun.star.sheet.SpreadsheetDocument")){
371 resetBuffer(suffix
, ".sxc");
372 return "scalc: StarOffice XML (Calc)";
373 }else if (xSI
.supportsService("com.sun.star.presentation.PresentationDocument")){
374 resetBuffer(suffix
, ".sxi");
375 return "simpress: StarOffice XML (Impress)";
376 }else if(xSI
.supportsService("com.sun.star.drawing.DrawingDocument")){
377 resetBuffer(suffix
, ".sxd");
378 return "sdraw: StarOffice XML (Draw)";
379 }else if (xSI
.supportsService("com.sun.star.formula.FormulaProperties")){
380 resetBuffer(suffix
, ".sxm");
381 return "smath: StarOffice XML (Math)";
386 private static StringBuffer
resetBuffer(StringBuffer sb
, String suffix
)
390 sb
.replace(0, sb
.length(), suffix
);
395 private static String
ensureEndingFileSep(String s
)
397 if(s
!= null && !s
.equals("") && !s
.endsWith(File
.separator
))
399 s
= s
.trim() + File
.separator
;