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