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