1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir package util.compare;
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir import convwatch.GraphicalDifferenceCheck;
30*cdf0e10cSrcweir import convwatch.GraphicalTestArguments;
31*cdf0e10cSrcweir import convwatch.DirectoryHelper;
32*cdf0e10cSrcweir import convwatch.FileHelper;
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir import lib.TestParameters;
35*cdf0e10cSrcweir import java.io.File;
36*cdf0e10cSrcweir import java.io.FileFilter;
37*cdf0e10cSrcweir import java.io.IOException;
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir import util.compare.DocComparator;
40*cdf0e10cSrcweir import convwatch.ConvWatchException;
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir // -----------------------------------------------------------------------------
43*cdf0e10cSrcweir class GraphicalComparator implements DocComparator
44*cdf0e10cSrcweir {
45*cdf0e10cSrcweir     GraphicalTestArguments m_aArguments;
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir     protected GraphicalComparator(TestParameters aParams)
48*cdf0e10cSrcweir         {
49*cdf0e10cSrcweir             m_aArguments = new GraphicalTestArguments(aParams);
50*cdf0e10cSrcweir         }
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir     /**
53*cdf0e10cSrcweir      * @return an instance of this object, but only it's interface
54*cdf0e10cSrcweir      */
55*cdf0e10cSrcweir     static DocComparator getInstance(TestParameters aParams)
56*cdf0e10cSrcweir         {
57*cdf0e10cSrcweir             // setting the default test parameter
58*cdf0e10cSrcweir             // TEST aParams
59*cdf0e10cSrcweir             GraphicalComparator a = new GraphicalComparator(aParams);
60*cdf0e10cSrcweir             return a;
61*cdf0e10cSrcweir         }
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir     /**
64*cdf0e10cSrcweir      * return a (FileFilter) function, which returns true, if the filename is a '*.prn' file
65*cdf0e10cSrcweir      */
66*cdf0e10cSrcweir     FileFilter getTrueIfPRNFile_FileFilter()
67*cdf0e10cSrcweir         {
68*cdf0e10cSrcweir             FileFilter aFileFilter = new FileFilter()
69*cdf0e10cSrcweir                 {
70*cdf0e10cSrcweir                     public boolean accept( File pathname )
71*cdf0e10cSrcweir                         {
72*cdf0e10cSrcweir                             if (pathname.getName().endsWith(".prn"))
73*cdf0e10cSrcweir                             {
74*cdf0e10cSrcweir                                 return true;
75*cdf0e10cSrcweir                             }
76*cdf0e10cSrcweir                             return false;
77*cdf0e10cSrcweir                         }
78*cdf0e10cSrcweir                 };
79*cdf0e10cSrcweir             return aFileFilter;
80*cdf0e10cSrcweir         }
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir     /**
83*cdf0e10cSrcweir      * build a new file from _sEntry by
84*cdf0e10cSrcweir      * replacing the path equals to _sInputPath with _sReferencePath and replace it's suffix by _sNewSuffix.
85*cdf0e10cSrcweir      *  If _sInputPath is empty, replace the whole path by _sReferencePath.
86*cdf0e10cSrcweir      */
87*cdf0e10cSrcweir     protected String createSpecialFile(String _sEntry, String _sInputPath, String _sReferencePath, String _sNewSuffix)
88*cdf0e10cSrcweir         {
89*cdf0e10cSrcweir             String fs = System.getProperty("file.separator");
90*cdf0e10cSrcweir             String sNewSubDir = "";
91*cdf0e10cSrcweir             if (_sInputPath.length() > 0)
92*cdf0e10cSrcweir             {
93*cdf0e10cSrcweir                 sNewSubDir = FileHelper.removeFirstDirectorysAndBasenameFrom(_sEntry, _sInputPath);
94*cdf0e10cSrcweir             }
95*cdf0e10cSrcweir             String sNameNoSuffix = FileHelper.getNameNoSuffix(FileHelper.getBasename(_sEntry));
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir             // add the sub path to the difference path
98*cdf0e10cSrcweir             String sNewReferencePath;
99*cdf0e10cSrcweir             if (sNewSubDir.length() > 0)
100*cdf0e10cSrcweir             {
101*cdf0e10cSrcweir                 sNewReferencePath = _sReferencePath + fs + sNewSubDir;
102*cdf0e10cSrcweir             }
103*cdf0e10cSrcweir             else
104*cdf0e10cSrcweir             {
105*cdf0e10cSrcweir                 sNewReferencePath = _sReferencePath;
106*cdf0e10cSrcweir             }
107*cdf0e10cSrcweir             // add the difference name
108*cdf0e10cSrcweir             sNewReferencePath += fs + sNameNoSuffix + _sNewSuffix;
109*cdf0e10cSrcweir             return sNewReferencePath;
110*cdf0e10cSrcweir         }
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir     boolean isReferenceOrDiffExistent(String _sNewSuffix)
113*cdf0e10cSrcweir         {
114*cdf0e10cSrcweir             boolean isExistent = false;
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir             // LLA? What if sReferencePath is a directory, but directory is empty? is the result then true or false;
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir             // wir muessen durch den InputPath durch und dann fuer jedes Dokument prufen, ob im angegebenen ReferencePath eine Reference existiert.
119*cdf0e10cSrcweir             String sInputPath = m_aArguments.getInputPath();
120*cdf0e10cSrcweir             if (FileHelper.isDir(sInputPath))
121*cdf0e10cSrcweir             {
122*cdf0e10cSrcweir                 Object[] aList = DirectoryHelper.traverse(sInputPath, FileHelper.getFileFilter(), m_aArguments.includeSubDirectories());
123*cdf0e10cSrcweir                 for (int i=0;i<aList.length;i++)
124*cdf0e10cSrcweir                 {
125*cdf0e10cSrcweir                     // get document + path
126*cdf0e10cSrcweir                     String sEntry = (String)aList[i];
127*cdf0e10cSrcweir                     String sNewReferencePath = createSpecialFile(sEntry, sInputPath, m_aArguments.getReferencePath(), _sNewSuffix);
128*cdf0e10cSrcweir                     // split path from document path which only is equal to sInputPath (sub path)
129*cdf0e10cSrcweir                     if (FileHelper.exists(sNewReferencePath))
130*cdf0e10cSrcweir                     {
131*cdf0e10cSrcweir                         isExistent = true;
132*cdf0e10cSrcweir                     }
133*cdf0e10cSrcweir                 }
134*cdf0e10cSrcweir             }
135*cdf0e10cSrcweir             else
136*cdf0e10cSrcweir             {
137*cdf0e10cSrcweir                 // sInputPath is a file
138*cdf0e10cSrcweir                 String sNewReferencePath = createSpecialFile(sInputPath, "", m_aArguments.getReferencePath(), _sNewSuffix);
139*cdf0e10cSrcweir                 if (FileHelper.exists(sNewReferencePath))
140*cdf0e10cSrcweir                 {
141*cdf0e10cSrcweir                     isExistent = true;
142*cdf0e10cSrcweir                 }
143*cdf0e10cSrcweir             }
144*cdf0e10cSrcweir             return isExistent;
145*cdf0e10cSrcweir         }
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir     /**
148*cdf0e10cSrcweir      *  REFERENCE_PATH must set to directory/file, where the reference (*.prn files) (should) exist
149*cdf0e10cSrcweir      */
150*cdf0e10cSrcweir     public boolean isReferenceExistent()
151*cdf0e10cSrcweir         {
152*cdf0e10cSrcweir             return isReferenceOrDiffExistent(".prn");
153*cdf0e10cSrcweir         }
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir     /**
156*cdf0e10cSrcweir      *  INPUT_PATH must set, to directory/file, where the documents exist.
157*cdf0e10cSrcweir      *  REFERENCE_PATH must set to directory/file, where the created references (*.prn files) will create.
158*cdf0e10cSrcweir      */
159*cdf0e10cSrcweir     public void createReference() throws IOException
160*cdf0e10cSrcweir         {
161*cdf0e10cSrcweir             // woher kommt das TestDocument
162*cdf0e10cSrcweir             // INPUT_PATH
163*cdf0e10cSrcweir             // wohin
164*cdf0e10cSrcweir             // REFERENCE_PATH
165*cdf0e10cSrcweir             // mit was (Reference Application)
166*cdf0e10cSrcweir             // AppExecutionCmd
167*cdf0e10cSrcweir             try
168*cdf0e10cSrcweir             {
169*cdf0e10cSrcweir                 String referenceInputPath = null;
170*cdf0e10cSrcweir                 if(m_aArguments.getReferenceInputPath() == null)
171*cdf0e10cSrcweir                 {
172*cdf0e10cSrcweir                     GraphicalDifferenceCheck.createReferences(m_aArguments.getInputPath(), m_aArguments.getReferencePath(), m_aArguments);
173*cdf0e10cSrcweir                 }
174*cdf0e10cSrcweir                 else
175*cdf0e10cSrcweir                 {
176*cdf0e10cSrcweir                     referenceInputPath = m_aArguments.getReferenceInputPath();
177*cdf0e10cSrcweir                     GraphicalDifferenceCheck.createReferences(referenceInputPath, m_aArguments.getReferencePath(), m_aArguments);
178*cdf0e10cSrcweir                 }
179*cdf0e10cSrcweir             }
180*cdf0e10cSrcweir             catch (ConvWatchException e)
181*cdf0e10cSrcweir             {
182*cdf0e10cSrcweir                 // wrap it to IOException
183*cdf0e10cSrcweir                 throw new java.io.IOException(e.getMessage());
184*cdf0e10cSrcweir             }
185*cdf0e10cSrcweir         }
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir     /**
188*cdf0e10cSrcweir      *  INPUT_PATH must set, to directory/file, where the documents exist.
189*cdf0e10cSrcweir      *  REFERENCE_PATH must set to directory/file, where the created references (*.prn files) will create.
190*cdf0e10cSrcweir      *  OUTPUT_PATH must set to a directory, there the whole ouptut will create
191*cdf0e10cSrcweir      */
192*cdf0e10cSrcweir     public boolean compare() throws IOException
193*cdf0e10cSrcweir         {
194*cdf0e10cSrcweir             try
195*cdf0e10cSrcweir             {
196*cdf0e10cSrcweir                 if (FileHelper.isDebugEnabled())
197*cdf0e10cSrcweir                 {
198*cdf0e10cSrcweir                     System.err.println("    Inputpath: '" + m_aArguments.getInputPath() + "'");
199*cdf0e10cSrcweir                     System.err.println("   Outputpath: '" + m_aArguments.getOutputPath() + "'");
200*cdf0e10cSrcweir                     System.err.println("Referencepath: '" + m_aArguments.getReferencePath() + "'");
201*cdf0e10cSrcweir                 }
202*cdf0e10cSrcweir                 return GraphicalDifferenceCheck.check(m_aArguments.getInputPath(), m_aArguments.getOutputPath(), m_aArguments.getReferencePath(), m_aArguments);
203*cdf0e10cSrcweir             }
204*cdf0e10cSrcweir             catch(ConvWatchException e)
205*cdf0e10cSrcweir             {
206*cdf0e10cSrcweir                 // wrap it to IOException
207*cdf0e10cSrcweir                 if (FileHelper.isDebugEnabled())
208*cdf0e10cSrcweir                 {
209*cdf0e10cSrcweir                     System.err.println("Exception caught");
210*cdf0e10cSrcweir                     System.err.println("    Inputpath: '" + m_aArguments.getInputPath() + "'");
211*cdf0e10cSrcweir                     System.err.println("   Outputpath: '" + m_aArguments.getOutputPath() + "'");
212*cdf0e10cSrcweir                     System.err.println("Referencepath: '" + m_aArguments.getReferencePath() + "'");
213*cdf0e10cSrcweir                 }
214*cdf0e10cSrcweir                 throw new java.io.IOException(e.getMessage());
215*cdf0e10cSrcweir             }
216*cdf0e10cSrcweir         }
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir     /**
219*cdf0e10cSrcweir      *
220*cdf0e10cSrcweir      * INPUT_PATH must set to the original documents the directory structure is taken to see if the references exist in the DIFF_PATH
221*cdf0e10cSrcweir      * DIFF_PATH must set to the diff references
222*cdf0e10cSrcweir      */
223*cdf0e10cSrcweir     public boolean isDiffReferenceExistent() throws IOException
224*cdf0e10cSrcweir         {
225*cdf0e10cSrcweir             return isReferenceOrDiffExistent(".prn.diff0001.jpg");
226*cdf0e10cSrcweir         }
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir     /**
229*cdf0e10cSrcweir      *  INPUT_PATH must set, to directory/file, where the documents exist.
230*cdf0e10cSrcweir      *  REFERENCE_PATH must set to directory/file, where the created references (*.prn files) exists.
231*cdf0e10cSrcweir      *  OUTPUT_PATH must set to a directory, where the whole ouptut will create. Here the diffReference will create.
232*cdf0e10cSrcweir      *              At the momemt it's not possible to say only where the diffreferences will create.
233*cdf0e10cSrcweir      */
234*cdf0e10cSrcweir     public void createDiffReference() throws IOException
235*cdf0e10cSrcweir         {
236*cdf0e10cSrcweir             // this is the same like compareDiff(), but trash the result.
237*cdf0e10cSrcweir             compareDiff();
238*cdf0e10cSrcweir         }
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir     /**
241*cdf0e10cSrcweir      *  INPUT_PATH must set, to directory/file, where the documents exist.
242*cdf0e10cSrcweir      *  REFERENCE_PATH must set to directory/file, where the created references (*.prn files) exists.
243*cdf0e10cSrcweir      *  OUTPUT_PATH must set to a directory, where the whole ouptut will create.
244*cdf0e10cSrcweir      *  DIFF_PATH must set to a directory, where the older difference references exist, it's possible to set this to the same as REFERENCE_PATH
245*cdf0e10cSrcweir      *  but this is not the default and will not automatically set.
246*cdf0e10cSrcweir      */
247*cdf0e10cSrcweir     public boolean compareDiff() throws IOException
248*cdf0e10cSrcweir         {
249*cdf0e10cSrcweir             try
250*cdf0e10cSrcweir             {
251*cdf0e10cSrcweir                 return GraphicalDifferenceCheck.check(m_aArguments.getInputPath(), m_aArguments.getOutputPath(), m_aArguments.getReferencePath(), m_aArguments.getDiffPath(), m_aArguments);
252*cdf0e10cSrcweir             }
253*cdf0e10cSrcweir             catch(ConvWatchException e)
254*cdf0e10cSrcweir             {
255*cdf0e10cSrcweir                 // wrap it to IOException
256*cdf0e10cSrcweir                 throw new java.io.IOException(e.getMessage());
257*cdf0e10cSrcweir             }
258*cdf0e10cSrcweir         }
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir }
261