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 
24cdf0e10cSrcweir package graphical;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.io.File;
27cdf0e10cSrcweir import java.io.FileFilter;
28cdf0e10cSrcweir import java.io.FileInputStream;
29cdf0e10cSrcweir import java.io.FileOutputStream;
30cdf0e10cSrcweir import java.io.InputStream;
31cdf0e10cSrcweir import java.io.OutputStream;
32cdf0e10cSrcweir import java.util.StringTokenizer;
33cdf0e10cSrcweir import helper.OSHelper;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir import java.io.PrintStream;
36cdf0e10cSrcweir import javax.swing.JOptionPane;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir public class FileHelper
39cdf0e10cSrcweir {
FileHelper()40cdf0e10cSrcweir     public FileHelper()
41cdf0e10cSrcweir         {
42cdf0e10cSrcweir             // fs = System.getProperty("file.separator");
43cdf0e10cSrcweir 
44cdf0e10cSrcweir 
45cdf0e10cSrcweir             String sOSName = System.getProperty("os.name");
46cdf0e10cSrcweir             String sOSArch = System.getProperty("os.arch");
47cdf0e10cSrcweir             String sOSVersion = System.getProperty("os.version");
48cdf0e10cSrcweir 
49cdf0e10cSrcweir             GlobalLogWriter.println(sOSName);
50cdf0e10cSrcweir             GlobalLogWriter.println(sOSArch);
51cdf0e10cSrcweir             GlobalLogWriter.println(sOSVersion);
52cdf0e10cSrcweir 
53cdf0e10cSrcweir         }
54cdf0e10cSrcweir 
MessageBox(String _sStr)55cdf0e10cSrcweir     public static void MessageBox(String _sStr)
56cdf0e10cSrcweir         {
57cdf0e10cSrcweir             String sVersion = System.getProperty("java.version");
58cdf0e10cSrcweir             String sOSName  = System.getProperty("os.name");
59cdf0e10cSrcweir             JOptionPane.showMessageDialog( null, _sStr, sVersion + " " + sOSName + " Hello World Debugger", JOptionPane.INFORMATION_MESSAGE );
60cdf0e10cSrcweir         }
61cdf0e10cSrcweir 
exists(String _sFile)62cdf0e10cSrcweir     public static boolean exists(String _sFile)
63cdf0e10cSrcweir         {
64cdf0e10cSrcweir             if (_sFile == null)
65cdf0e10cSrcweir             {
66cdf0e10cSrcweir                 return false;
67cdf0e10cSrcweir             }
68cdf0e10cSrcweir 
69cdf0e10cSrcweir             File aFile = new File(_sFile);
70cdf0e10cSrcweir             if (aFile.exists())
71cdf0e10cSrcweir             {
72cdf0e10cSrcweir                 return true;
73cdf0e10cSrcweir             }
74cdf0e10cSrcweir             // This is just nice for DEBUG behaviour
75cdf0e10cSrcweir             // due to the fact this is absolutly context dependency no one should use it.
76cdf0e10cSrcweir             // else
77cdf0e10cSrcweir             // {
78cdf0e10cSrcweir             //     System.out.println("FileHelper:exists() tell this path doesn't exists. Check it. path is:" );
79cdf0e10cSrcweir             //     System.out.println( _sFile );
80cdf0e10cSrcweir             //     System.out.println( aFile.getAbsolutePath() );
81cdf0e10cSrcweir             //     MessageBox("Der JavaProzess wartet auf eine interaktion ihrerseits.");
82cdf0e10cSrcweir             //
83cdf0e10cSrcweir             //     File aFile2 = new File(_sFile);
84cdf0e10cSrcweir             //     if (aFile2.exists())
85cdf0e10cSrcweir             //     {
86cdf0e10cSrcweir             //         System.out.println("Thanks, file exists." );
87cdf0e10cSrcweir             //         return true;
88cdf0e10cSrcweir             //     }
89cdf0e10cSrcweir             // }
90cdf0e10cSrcweir             return false;
91cdf0e10cSrcweir         }
92cdf0e10cSrcweir 
isDir(String _sDir)93cdf0e10cSrcweir     public static boolean isDir(String _sDir)
94cdf0e10cSrcweir         {
95cdf0e10cSrcweir             if (_sDir == null)
96cdf0e10cSrcweir             {
97cdf0e10cSrcweir                 return false;
98cdf0e10cSrcweir             }
99cdf0e10cSrcweir             try
100cdf0e10cSrcweir             {
101cdf0e10cSrcweir                 File aFile = new File(_sDir);
102cdf0e10cSrcweir                 if (aFile.exists() && aFile.isDirectory())
103cdf0e10cSrcweir                 {
104cdf0e10cSrcweir                     return true;
105cdf0e10cSrcweir                 }
106cdf0e10cSrcweir             }
107cdf0e10cSrcweir             catch (NullPointerException e)
108cdf0e10cSrcweir             {
109cdf0e10cSrcweir                 GlobalLogWriter.println("Exception caught. FileHelper.isDir('" + _sDir + "')");
110cdf0e10cSrcweir                 e.printStackTrace();
111cdf0e10cSrcweir             }
112cdf0e10cSrcweir             return false;
113cdf0e10cSrcweir         }
114cdf0e10cSrcweir 
getBasename(String _sFilename)115cdf0e10cSrcweir     public static String getBasename(String _sFilename)
116cdf0e10cSrcweir         {
117cdf0e10cSrcweir             if (_sFilename == null)
118cdf0e10cSrcweir             {
119cdf0e10cSrcweir                 return "";
120cdf0e10cSrcweir             }
121cdf0e10cSrcweir             // String fs = System.getProperty("file.separator");
122cdf0e10cSrcweir 
123cdf0e10cSrcweir             int nIdx = _sFilename.lastIndexOf("\\");
124cdf0e10cSrcweir             if (nIdx == -1)
125cdf0e10cSrcweir             {
126cdf0e10cSrcweir                 nIdx = _sFilename.lastIndexOf("/");
127cdf0e10cSrcweir             }
128cdf0e10cSrcweir             if (nIdx > 0)
129cdf0e10cSrcweir             {
130cdf0e10cSrcweir                 return _sFilename.substring(nIdx + 1);
131cdf0e10cSrcweir             }
132cdf0e10cSrcweir             return _sFilename;
133cdf0e10cSrcweir         }
134cdf0e10cSrcweir 
getNameNoSuffix(String _sFilename)135cdf0e10cSrcweir     public static String getNameNoSuffix(String _sFilename)
136cdf0e10cSrcweir         {
137cdf0e10cSrcweir             if (_sFilename == null)
138cdf0e10cSrcweir             {
139cdf0e10cSrcweir                 return "";
140cdf0e10cSrcweir             }
141cdf0e10cSrcweir             int nIdx = _sFilename.lastIndexOf(".");
142cdf0e10cSrcweir             if (nIdx > 0)
143cdf0e10cSrcweir             {
144cdf0e10cSrcweir                 return _sFilename.substring(0, nIdx);
145cdf0e10cSrcweir             }
146cdf0e10cSrcweir             return _sFilename;
147cdf0e10cSrcweir         }
148cdf0e10cSrcweir 
getSuffix(String _sFilename)149cdf0e10cSrcweir     public static String getSuffix(String _sFilename)
150cdf0e10cSrcweir         {
151cdf0e10cSrcweir             if (_sFilename == null)
152cdf0e10cSrcweir             {
153cdf0e10cSrcweir                 return "";
154cdf0e10cSrcweir             }
155cdf0e10cSrcweir             int nIdx = _sFilename.lastIndexOf(".");
156cdf0e10cSrcweir             if (nIdx > 0)
157cdf0e10cSrcweir             {
158cdf0e10cSrcweir                 return _sFilename.substring(nIdx );
159cdf0e10cSrcweir             }
160cdf0e10cSrcweir             return "";
161cdf0e10cSrcweir         }
162cdf0e10cSrcweir 
getPath(String _sFilename)163cdf0e10cSrcweir     public static String getPath(String _sFilename)
164cdf0e10cSrcweir         {
165cdf0e10cSrcweir             if (_sFilename == null)
166cdf0e10cSrcweir             {
167cdf0e10cSrcweir                 return "";
168cdf0e10cSrcweir             }
169cdf0e10cSrcweir             // String fs = System.getProperty("file.separator");
170cdf0e10cSrcweir 
171cdf0e10cSrcweir             int nIdx = _sFilename.lastIndexOf("\\");
172cdf0e10cSrcweir             if (nIdx == -1)
173cdf0e10cSrcweir             {
174cdf0e10cSrcweir                 nIdx = _sFilename.lastIndexOf("/");
175cdf0e10cSrcweir             }
176cdf0e10cSrcweir             if (nIdx > 0)
177cdf0e10cSrcweir             {
178cdf0e10cSrcweir                 return _sFilename.substring(0, nIdx);
179cdf0e10cSrcweir             }
180cdf0e10cSrcweir             return "";
181cdf0e10cSrcweir         }
182cdf0e10cSrcweir 
183cdf0e10cSrcweir /*
184cdf0e10cSrcweir     static ArrayList files = new ArrayList();
185cdf0e10cSrcweir     public static Object[] traverse( String afileDirectory )
186cdf0e10cSrcweir         {
187cdf0e10cSrcweir 
188cdf0e10cSrcweir             File fileDirectory = new File(afileDirectory);
189cdf0e10cSrcweir             // Testing, if the file is a directory, and if so, it throws an exception
190cdf0e10cSrcweir             if ( !fileDirectory.isDirectory() )
191cdf0e10cSrcweir             {
192cdf0e10cSrcweir                 throw new IllegalArgumentException( "not a directory: " + fileDirectory.getName() );
193cdf0e10cSrcweir             }
194cdf0e10cSrcweir 
195cdf0e10cSrcweir             // Getting all files and directories in the current directory
196cdf0e10cSrcweir             File[] entries = fileDirectory.listFiles();
197cdf0e10cSrcweir 
198cdf0e10cSrcweir             // Iterating for each file and directory
199cdf0e10cSrcweir             for ( int i = 0; i < entries.length; ++i )
200cdf0e10cSrcweir             {
201cdf0e10cSrcweir                 // adding file to List
202cdf0e10cSrcweir                 try
203cdf0e10cSrcweir                 {
204cdf0e10cSrcweir                     // Composing the URL by replacing all backslashs
205cdf0e10cSrcweir                     String stringUrl = "file:///"
206cdf0e10cSrcweir                         + entries[ i ].getAbsolutePath().replace( '\\', '/' );
207cdf0e10cSrcweir                     files.add(stringUrl);
208cdf0e10cSrcweir                 }
209cdf0e10cSrcweir                 catch( Exception exception )
210cdf0e10cSrcweir                 {
211cdf0e10cSrcweir                     exception.printStackTrace();
212cdf0e10cSrcweir                 }
213cdf0e10cSrcweir             }
214cdf0e10cSrcweir             return files.toArray();
215cdf0e10cSrcweir         }
216cdf0e10cSrcweir */
217cdf0e10cSrcweir 
218cdf0e10cSrcweir     // makeDirectories("", "/tmp/a/b");
219cdf0e10cSrcweir     // creates all directories /tmp/a/b
220cdf0e10cSrcweir     //
makeDirectories(String first, String path)221cdf0e10cSrcweir     public static void makeDirectories(String first, String path)
222cdf0e10cSrcweir         {
223cdf0e10cSrcweir             makeDirectories(first, path, "0777");
224cdf0e10cSrcweir         }
225cdf0e10cSrcweir 
makeDirectories(String first, String path, String _sMode)226cdf0e10cSrcweir     public static void makeDirectories(String first, String path, String _sMode)
227cdf0e10cSrcweir         {
228cdf0e10cSrcweir             String fs = System.getProperty("file.separator");
229cdf0e10cSrcweir             if (path.startsWith(fs + fs)) // starts with UNC Path
230cdf0e10cSrcweir             {
231cdf0e10cSrcweir                 int n = path.indexOf(fs, 2);
232cdf0e10cSrcweir                 n = path.indexOf(fs, n + 1);
233cdf0e10cSrcweir                 first = path.substring(0, n);
234cdf0e10cSrcweir                 path = path.substring(n + 1);
235cdf0e10cSrcweir             }
236cdf0e10cSrcweir 
237cdf0e10cSrcweir             String already_done = null;
238cdf0e10cSrcweir             StringTokenizer path_tokenizer = new StringTokenizer(path,fs,false);
239cdf0e10cSrcweir             already_done = first;
240cdf0e10cSrcweir             while (path_tokenizer.hasMoreTokens())
241cdf0e10cSrcweir             {
242cdf0e10cSrcweir                 String part = path_tokenizer.nextToken();
243cdf0e10cSrcweir                 File new_dir = new File(already_done + File.separatorChar + part);
244cdf0e10cSrcweir                 already_done = new_dir.toString();
245cdf0e10cSrcweir                 // System.out.println(already_done);
246cdf0e10cSrcweir                 //create the directory
247cdf0e10cSrcweir                 new_dir.mkdirs();
248cdf0e10cSrcweir                 if (OSHelper.isUnix() &&
249cdf0e10cSrcweir                     _sMode.length() > 0)
250cdf0e10cSrcweir                 {
251cdf0e10cSrcweir                     try
252cdf0e10cSrcweir                     {
253cdf0e10cSrcweir                         chmod(new_dir, _sMode);
254cdf0e10cSrcweir                     }
255cdf0e10cSrcweir                     catch (java.io.IOException e)
256cdf0e10cSrcweir                     {
257cdf0e10cSrcweir                         GlobalLogWriter.println("Exception caught. FileHelper.makeDirectories('" + new_dir.getAbsolutePath() + "')");
258cdf0e10cSrcweir                     }
259cdf0e10cSrcweir                 }
260cdf0e10cSrcweir             }
261cdf0e10cSrcweir             // return;
262cdf0e10cSrcweir         }
263cdf0e10cSrcweir 
chmod(File file, String mode)264cdf0e10cSrcweir     public static void chmod(File file, String mode) throws java.io.IOException
265cdf0e10cSrcweir         {
266cdf0e10cSrcweir             Runtime.getRuntime().exec
267cdf0e10cSrcweir                 (new String[]
268cdf0e10cSrcweir                     {"chmod", mode, file.getAbsolutePath()});
269cdf0e10cSrcweir         }
270cdf0e10cSrcweir 
removeFirstDirectorysAndBasenameFrom(String _sName, String _sRemovePath)271cdf0e10cSrcweir     public static String removeFirstDirectorysAndBasenameFrom(String _sName, String _sRemovePath)
272cdf0e10cSrcweir         {
273cdf0e10cSrcweir             // pre: _sName: /a/b/c/d/e/f.g _sRemovePath /a/b/c
274cdf0e10cSrcweir             // result: d/e
275cdf0e10cSrcweir             String fs = System.getProperty("file.separator");
276cdf0e10cSrcweir 
277cdf0e10cSrcweir             String sBasename = FileHelper.getBasename(_sName);
278cdf0e10cSrcweir             String sSubDirs = "";
279cdf0e10cSrcweir             if (_sName.startsWith(_sRemovePath))
280cdf0e10cSrcweir             {
281cdf0e10cSrcweir                 // if _sName starts with _sRemovePath
282cdf0e10cSrcweir                 int nRemovePathIndex = _sRemovePath.length();
283cdf0e10cSrcweir                 if (! _sRemovePath.endsWith(fs))
284cdf0e10cSrcweir                 {
285cdf0e10cSrcweir                     // add 1 if we not ends with file separator
286cdf0e10cSrcweir                     nRemovePathIndex ++;
287cdf0e10cSrcweir                 }
288cdf0e10cSrcweir                 int nBasenameIndex = _sName.length() - sBasename.length() - 1;
289cdf0e10cSrcweir                 if (nRemovePathIndex < nBasenameIndex)
290cdf0e10cSrcweir                 {
291cdf0e10cSrcweir                     sSubDirs = _sName.substring(nRemovePathIndex, nBasenameIndex);
292cdf0e10cSrcweir                 }
293cdf0e10cSrcweir             }
294cdf0e10cSrcweir             else
295cdf0e10cSrcweir             {
296cdf0e10cSrcweir                 // special case, the _sRemovePath is not part of _sName
297cdf0e10cSrcweir                 sSubDirs = FileHelper.getPath(_sName);
298cdf0e10cSrcweir                 if (sSubDirs.startsWith(fs))
299cdf0e10cSrcweir                 {
300cdf0e10cSrcweir                     // remove leading file separator
301cdf0e10cSrcweir                     sSubDirs = sSubDirs.substring(1);
302cdf0e10cSrcweir                 }
303cdf0e10cSrcweir             }
304cdf0e10cSrcweir 
305cdf0e10cSrcweir             return sSubDirs;
306cdf0e10cSrcweir         }
307cdf0e10cSrcweir 
test_removeFirstDirectorysAndBasenameFrom()308cdf0e10cSrcweir     public static void test_removeFirstDirectorysAndBasenameFrom()
309cdf0e10cSrcweir         {
310cdf0e10cSrcweir             String a = removeFirstDirectorysAndBasenameFrom("/a/b/c/d/e/f.g", "/a/b/c");
311cdf0e10cSrcweir             // assure("", a.equals("d/e"));
312cdf0e10cSrcweir             String b = removeFirstDirectorysAndBasenameFrom("/a/b/c/d/e/f.g", "/a/b/c/");
313cdf0e10cSrcweir             // assure("", b.equals("d/e"));
314cdf0e10cSrcweir             String c = removeFirstDirectorysAndBasenameFrom("/a/b/c/d/e/f.g", "/b/c");
315cdf0e10cSrcweir             // assure("", c.equals("a/b/c/d/e"));
316cdf0e10cSrcweir         }
317cdf0e10cSrcweir 
318cdf0e10cSrcweir 
getSystemPathFromFileURL( String _sFileURL )319cdf0e10cSrcweir     public static String getSystemPathFromFileURL( String _sFileURL )
320cdf0e10cSrcweir     {
321cdf0e10cSrcweir         String sSystemFile = null;
322cdf0e10cSrcweir 
323cdf0e10cSrcweir         if(_sFileURL.startsWith("file:///"))
324cdf0e10cSrcweir         {
325cdf0e10cSrcweir             if (OSHelper.isWindows())
326cdf0e10cSrcweir             {
327cdf0e10cSrcweir                 sSystemFile = _sFileURL.substring(8);
328cdf0e10cSrcweir             }
329cdf0e10cSrcweir             else
330cdf0e10cSrcweir             {
331cdf0e10cSrcweir                 sSystemFile = _sFileURL.substring(7);
332cdf0e10cSrcweir             }
333cdf0e10cSrcweir         }
334cdf0e10cSrcweir         else if (_sFileURL.startsWith("file://"))
335cdf0e10cSrcweir         {
336cdf0e10cSrcweir             sSystemFile = _sFileURL.substring(5);
337cdf0e10cSrcweir         }
338cdf0e10cSrcweir         String fs = System.getProperty("file.separator");
339cdf0e10cSrcweir         if (! fs.equals("/"))
340cdf0e10cSrcweir         {
341cdf0e10cSrcweir             sSystemFile = sSystemFile.replace ('/', fs.toCharArray ()[0]);
342cdf0e10cSrcweir         }
343cdf0e10cSrcweir // FEATURE FOR UNC NEED!!!
344cdf0e10cSrcweir         return sSystemFile;
345cdf0e10cSrcweir     }
346cdf0e10cSrcweir 
347cdf0e10cSrcweir     private static boolean m_bDebugTextShown = false;
isDebugEnabled()348cdf0e10cSrcweir     public static boolean isDebugEnabled()
349cdf0e10cSrcweir         {
350cdf0e10cSrcweir             boolean bDebug = false;
351cdf0e10cSrcweir             String sTmpPath = util.utils.getUsersTempDir();
352cdf0e10cSrcweir             //util.utils.getUsersTempDir();
353cdf0e10cSrcweir             String fs = System.getProperty("file.separator");
354cdf0e10cSrcweir             String sName = sTmpPath + fs + "DOC_COMPARATOR_DEBUG";
355cdf0e10cSrcweir             File aFile = new File(sName);
356cdf0e10cSrcweir             if (aFile.exists())
357cdf0e10cSrcweir             {
358cdf0e10cSrcweir                 if (m_bDebugTextShown == false)
359cdf0e10cSrcweir                 {
360cdf0e10cSrcweir                     GlobalLogWriter.println("Found file: " + sName);
361cdf0e10cSrcweir                     GlobalLogWriter.println("Activate debug mode.");
362cdf0e10cSrcweir                     GlobalLogWriter.println("If debug mode is no longer necessary, remove the above file.");
363cdf0e10cSrcweir                     m_bDebugTextShown = true;
364cdf0e10cSrcweir                 }
365cdf0e10cSrcweir                 bDebug = true;
366cdf0e10cSrcweir             }
367cdf0e10cSrcweir             return bDebug;
368cdf0e10cSrcweir         }
369cdf0e10cSrcweir 
copyStream(InputStream _aIn, OutputStream _aOut)370cdf0e10cSrcweir     private static void copyStream(InputStream _aIn, OutputStream _aOut) throws java.io.IOException
371cdf0e10cSrcweir     {
372cdf0e10cSrcweir         byte[] aBuffer = new byte[0xFFFF];
373cdf0e10cSrcweir         for (int len; (len = _aIn.read(aBuffer)) != -1; )
374cdf0e10cSrcweir         {
375cdf0e10cSrcweir             _aOut.write(aBuffer, 0, len);
376cdf0e10cSrcweir         }
377cdf0e10cSrcweir     }
378cdf0e10cSrcweir 
copy(String _sSource, String _sDestination)379cdf0e10cSrcweir     public static void copy(String _sSource, String _sDestination)
380cdf0e10cSrcweir         {
381cdf0e10cSrcweir             FileInputStream aFIS = null;
382cdf0e10cSrcweir             FileOutputStream aFOS = null;
383cdf0e10cSrcweir 
384cdf0e10cSrcweir             try
385cdf0e10cSrcweir             {
386cdf0e10cSrcweir                 aFIS = new FileInputStream(_sSource);
387cdf0e10cSrcweir                 aFOS = new FileOutputStream(_sDestination);
388cdf0e10cSrcweir                 copyStream(aFIS, aFOS);
389cdf0e10cSrcweir             }
390cdf0e10cSrcweir             catch (java.io.IOException e)
391cdf0e10cSrcweir             {
392cdf0e10cSrcweir                 System.out.println("Error: caught Exception: " + e.getMessage());
393cdf0e10cSrcweir             }
394cdf0e10cSrcweir             finally
395cdf0e10cSrcweir             {
396cdf0e10cSrcweir                 if (aFIS != null)
397cdf0e10cSrcweir                 {
398cdf0e10cSrcweir                     try
399cdf0e10cSrcweir                     {
400cdf0e10cSrcweir                         aFIS.close();
401cdf0e10cSrcweir                     }
402cdf0e10cSrcweir                     catch (java.io.IOException e)
403cdf0e10cSrcweir                     {
404cdf0e10cSrcweir                         System.out.println("Error: caught Exception: " + e.getMessage());
405cdf0e10cSrcweir                     }
406cdf0e10cSrcweir                 }
407cdf0e10cSrcweir                 if (aFOS != null)
408cdf0e10cSrcweir                 {
409cdf0e10cSrcweir                     try
410cdf0e10cSrcweir                     {
411cdf0e10cSrcweir                         aFOS.close();
412cdf0e10cSrcweir                     }
413cdf0e10cSrcweir                     catch (java.io.IOException e)
414cdf0e10cSrcweir                     {
415cdf0e10cSrcweir                         System.out.println("Error: caught Exception: " + e.getMessage());
416cdf0e10cSrcweir                     }
417cdf0e10cSrcweir                 }
418cdf0e10cSrcweir             }
419cdf0e10cSrcweir 
420cdf0e10cSrcweir //            try
421cdf0e10cSrcweir //            {
422cdf0e10cSrcweir //                File inputFile = new File(_sSource);
423cdf0e10cSrcweir //                File outputFile = new File(_sDestination);
424cdf0e10cSrcweir //
425cdf0e10cSrcweir //                java.io.FileReader in = new java.io.FileReader(inputFile);
426cdf0e10cSrcweir //                java.io.FileWriter out = new java.io.FileWriter(outputFile);
427cdf0e10cSrcweir //                int c;
428cdf0e10cSrcweir //
429cdf0e10cSrcweir //                while ((c = in.read()) != -1)
430cdf0e10cSrcweir //                {
431cdf0e10cSrcweir //                    out.write(c);
432cdf0e10cSrcweir //                }
433cdf0e10cSrcweir //
434cdf0e10cSrcweir //                in.close();
435cdf0e10cSrcweir //                out.close();
436cdf0e10cSrcweir //            }
437cdf0e10cSrcweir //            catch (java.io.IOException e)
438cdf0e10cSrcweir //            {
439cdf0e10cSrcweir //                GlobalLogWriter.get().println("Exception caught. FileHelper.copy('" + _sSource + ", " + _sDestination + "')");
440cdf0e10cSrcweir //                GlobalLogWriter.get().println("Message: " + e.getMessage());
441cdf0e10cSrcweir //            }
442cdf0e10cSrcweir         }
443cdf0e10cSrcweir 
444cdf0e10cSrcweir 
445cdf0e10cSrcweir     /**
446cdf0e10cSrcweir      * Within the directory run through, it's possible to say which file extension types should not
447cdf0e10cSrcweir      * consider like '*.prn' because it's not a document.
448cdf0e10cSrcweir      *
449cdf0e10cSrcweir      * @return a FileFilter function
450cdf0e10cSrcweir      */
getFileFilter()451cdf0e10cSrcweir     public static FileFilter getFileFilter()
452cdf0e10cSrcweir         {
453cdf0e10cSrcweir             FileFilter aFileFilter = new FileFilter()
454cdf0e10cSrcweir                 {
455cdf0e10cSrcweir                     public boolean accept( File pathname )
456cdf0e10cSrcweir                         {
457cdf0e10cSrcweir                             // leave out files which started by '~$' these are Microsoft Office temp files
458cdf0e10cSrcweir                             if (pathname.getName().startsWith("~$"))
459cdf0e10cSrcweir                             {
460cdf0e10cSrcweir                                 return false;
461cdf0e10cSrcweir                             }
462cdf0e10cSrcweir                             // leave out files starts with '.~lock.' these are OpenOffice.org lock files
463cdf0e10cSrcweir                             if (pathname.getName().startsWith(".~lock."))
464cdf0e10cSrcweir                             {
465cdf0e10cSrcweir                                 return false;
466cdf0e10cSrcweir                             }
467cdf0e10cSrcweir                             // leave out files ends with '#' these could be temp files
468cdf0e10cSrcweir                             if (pathname.getName().endsWith("#"))
469cdf0e10cSrcweir                             {
470cdf0e10cSrcweir                                 return false;
471cdf0e10cSrcweir                             }
472cdf0e10cSrcweir                             if (pathname.getName().endsWith(".prn"))
473cdf0e10cSrcweir                             {
474cdf0e10cSrcweir                                 return false;
475cdf0e10cSrcweir                             }
476cdf0e10cSrcweir                             if (pathname.getName().endsWith(".ps"))
477cdf0e10cSrcweir                             {
478cdf0e10cSrcweir                                 return false;
479cdf0e10cSrcweir                             }
480cdf0e10cSrcweir                             // This type of document no one would like to load.
481cdf0e10cSrcweir                             if (pathname.getName().endsWith(".zip"))
482cdf0e10cSrcweir                             {
483cdf0e10cSrcweir                                 return false;
484cdf0e10cSrcweir                             }
485cdf0e10cSrcweir                             // just a hack
486cdf0e10cSrcweir                             if (pathname.getName().endsWith("_"))
487cdf0e10cSrcweir                             {
488cdf0e10cSrcweir                                 return false;
489cdf0e10cSrcweir                             }
490cdf0e10cSrcweir                             return true;
491cdf0e10cSrcweir                         }
492cdf0e10cSrcweir                 };
493cdf0e10cSrcweir             return aFileFilter;
494cdf0e10cSrcweir         }
495cdf0e10cSrcweir     /**
496cdf0e10cSrcweir      * Within the directory run through, it's possible to say which file extension types should not
497cdf0e10cSrcweir      * consider like '*.prn' because it's not a document.
498cdf0e10cSrcweir      *
499cdf0e10cSrcweir      * @return a FileFilter function
500cdf0e10cSrcweir      */
getFileFilterPSorPDF()501cdf0e10cSrcweir     public static FileFilter getFileFilterPSorPDF()
502cdf0e10cSrcweir         {
503cdf0e10cSrcweir             FileFilter aFileFilter = new FileFilter()
504cdf0e10cSrcweir                 {
505cdf0e10cSrcweir                     public boolean accept( File pathname )
506cdf0e10cSrcweir                         {
507cdf0e10cSrcweir                             if (pathname.getName().endsWith(".ps"))
508cdf0e10cSrcweir                             {
509cdf0e10cSrcweir                                 return true;
510cdf0e10cSrcweir                             }
511cdf0e10cSrcweir                             if (pathname.getName().endsWith(".pdf"))
512cdf0e10cSrcweir                             {
513cdf0e10cSrcweir                                 return true;
514cdf0e10cSrcweir                             }
515cdf0e10cSrcweir                             return false;
516cdf0e10cSrcweir                         }
517cdf0e10cSrcweir                 };
518cdf0e10cSrcweir             return aFileFilter;
519cdf0e10cSrcweir         }
520cdf0e10cSrcweir     /**
521cdf0e10cSrcweir      * Within the directory run through, it's possible to say which file extension types should not
522cdf0e10cSrcweir      * consider like '*.prn' because it's not a document.
523cdf0e10cSrcweir      *
524cdf0e10cSrcweir      * @return a FileFilter function
525cdf0e10cSrcweir      */
getFileFilterJPEG()526cdf0e10cSrcweir     public static FileFilter getFileFilterJPEG()
527cdf0e10cSrcweir         {
528cdf0e10cSrcweir             FileFilter aFileFilter = new FileFilter()
529cdf0e10cSrcweir                 {
530cdf0e10cSrcweir                     public boolean accept( File pathname )
531cdf0e10cSrcweir                         {
532cdf0e10cSrcweir                             if (pathname.getName().toLowerCase().endsWith(".jpg"))
533cdf0e10cSrcweir                             {
534cdf0e10cSrcweir                                 return true;
535cdf0e10cSrcweir                             }
536cdf0e10cSrcweir                             if (pathname.getName().toLowerCase().endsWith(".jpeg"))
537cdf0e10cSrcweir                             {
538cdf0e10cSrcweir                                 return true;
539cdf0e10cSrcweir                             }
540cdf0e10cSrcweir                             return false;
541cdf0e10cSrcweir                         }
542cdf0e10cSrcweir                 };
543cdf0e10cSrcweir             return aFileFilter;
544cdf0e10cSrcweir         }
545cdf0e10cSrcweir     /**
546cdf0e10cSrcweir      * Within the directory run through, it's possible to say which file extension types should not
547cdf0e10cSrcweir      * consider like '*.ini' because it's not a document.
548cdf0e10cSrcweir      *
549cdf0e10cSrcweir      * @return a FileFilter function
550cdf0e10cSrcweir      */
getFileFilterINI()551cdf0e10cSrcweir     public static FileFilter getFileFilterINI()
552cdf0e10cSrcweir         {
553cdf0e10cSrcweir             FileFilter aFileFilter = new FileFilter()
554cdf0e10cSrcweir                 {
555cdf0e10cSrcweir                     public boolean accept( File pathname )
556cdf0e10cSrcweir                         {
557cdf0e10cSrcweir                             String sPathname = pathname.getName().toLowerCase();
558cdf0e10cSrcweir                             if (sPathname.endsWith("index.ini"))
559cdf0e10cSrcweir                             {
560cdf0e10cSrcweir                                 // don't consider the index.ini file
561cdf0e10cSrcweir                                 return false;
562cdf0e10cSrcweir                             }
563cdf0e10cSrcweir                             if (sPathname.endsWith(".ini"))
564cdf0e10cSrcweir                             {
565cdf0e10cSrcweir                                 return true;
566cdf0e10cSrcweir                             }
567cdf0e10cSrcweir                             return false;
568cdf0e10cSrcweir                         }
569cdf0e10cSrcweir                 };
570cdf0e10cSrcweir             return aFileFilter;
571cdf0e10cSrcweir         }
572cdf0e10cSrcweir 
appendPath(String _sPath, String _sRelativePathToAdd)573cdf0e10cSrcweir         public static String appendPath(String _sPath, String _sRelativePathToAdd)
574cdf0e10cSrcweir         {
575cdf0e10cSrcweir             String sNewPath = _sPath;
576cdf0e10cSrcweir             String fs = System.getProperty("file.separator");
577cdf0e10cSrcweir             if (_sPath.startsWith("file:"))
578cdf0e10cSrcweir             {
579cdf0e10cSrcweir                 fs = "/";                                  // we use a file URL so only '/' is allowed.
580cdf0e10cSrcweir             }
581cdf0e10cSrcweir             if (! (sNewPath.endsWith("/") || sNewPath.endsWith("\\") ) )
582cdf0e10cSrcweir             {
583cdf0e10cSrcweir                 sNewPath += fs;
584cdf0e10cSrcweir             }
585cdf0e10cSrcweir             sNewPath += _sRelativePathToAdd;
586cdf0e10cSrcweir             return sNewPath;
587cdf0e10cSrcweir         }
588cdf0e10cSrcweir 
589cdf0e10cSrcweir     // -----------------------------------------------------------------------------
createInfoFile(String _sFile, ParameterHelper _aGTA)590cdf0e10cSrcweir     public static void createInfoFile(String _sFile, ParameterHelper _aGTA)
591cdf0e10cSrcweir         {
592cdf0e10cSrcweir             createInfoFile(_sFile, _aGTA, "");
593cdf0e10cSrcweir         }
594cdf0e10cSrcweir 
createInfoFile(String _sFile, ParameterHelper _aGTA, String _sSpecial)595cdf0e10cSrcweir     public static void createInfoFile(String _sFile, ParameterHelper _aGTA, String _sSpecial)
596cdf0e10cSrcweir         {
597cdf0e10cSrcweir             String sFilename;
598cdf0e10cSrcweir             if (_sFile.startsWith("file://"))
599cdf0e10cSrcweir             {
600cdf0e10cSrcweir                 sFilename = FileHelper.getSystemPathFromFileURL(_sFile);
601cdf0e10cSrcweir                 GlobalLogWriter.println("CreateInfoFile: '" + sFilename + "'" );
602cdf0e10cSrcweir             }
603cdf0e10cSrcweir             else
604cdf0e10cSrcweir             {
605cdf0e10cSrcweir                 sFilename = _sFile;
606cdf0e10cSrcweir             }
607cdf0e10cSrcweir             String sFileDir = FileHelper.getPath(sFilename);
608cdf0e10cSrcweir             String sBasename = FileHelper.getBasename(sFilename);
609cdf0e10cSrcweir             String sNameNoSuffix = FileHelper.getNameNoSuffix(sBasename);
610cdf0e10cSrcweir 
611cdf0e10cSrcweir             String sIniFile = FileHelper.appendPath(sFileDir, sBasename + ".ini");
612cdf0e10cSrcweir             IniFile aIniFile = new IniFile(sIniFile);
613cdf0e10cSrcweir 
614cdf0e10cSrcweir             // OLD INFO FILE
615cdf0e10cSrcweir 
616cdf0e10cSrcweir             // String fs = System.getProperty("file.separator");
617cdf0e10cSrcweir             String ls = System.getProperty("line.separator");
618cdf0e10cSrcweir             String sInfoFilename = FileHelper.appendPath(sFileDir, sNameNoSuffix + ".info");
619cdf0e10cSrcweir             File aInfoFile = new File(sInfoFilename);
620cdf0e10cSrcweir 
621cdf0e10cSrcweir             String sBuildID = "";
622cdf0e10cSrcweir 
623cdf0e10cSrcweir             try
624cdf0e10cSrcweir             {
625cdf0e10cSrcweir                 FileOutputStream out2 = new FileOutputStream(aInfoFile.toString());
626cdf0e10cSrcweir                 PrintStream out = new PrintStream(out2);
627cdf0e10cSrcweir 
628cdf0e10cSrcweir                 out.println("# automatically created file by graphical compare");
629cdf0e10cSrcweir                 if (_aGTA != null)
630cdf0e10cSrcweir                 {
631cdf0e10cSrcweir                     if (_sSpecial != null && _sSpecial.equals("msoffice"))
632cdf0e10cSrcweir                     {
633cdf0e10cSrcweir                         out.println("# buildid from wordloadfile");
634cdf0e10cSrcweir                         sBuildID = _aGTA.getPerformance().getMSOfficeVersion();
635cdf0e10cSrcweir                         out.println("buildid=" + sBuildID);
636cdf0e10cSrcweir                     }
637cdf0e10cSrcweir                     else
638cdf0e10cSrcweir                     {
639cdf0e10cSrcweir                         out.println("# buildid is read out of the bootstrap file");
640cdf0e10cSrcweir                         sBuildID = _aGTA.getBuildID();
641cdf0e10cSrcweir                         out.println("buildid=" + sBuildID);
642cdf0e10cSrcweir                     }
643cdf0e10cSrcweir                     aIniFile.insertValue("global", "buildid", sBuildID);
644cdf0e10cSrcweir 
645cdf0e10cSrcweir                     // if (_sSpecial != null && _sSpecial.length() > 0)
646cdf0e10cSrcweir                     // {
647cdf0e10cSrcweir                     //    out.write("special=" + _sSpecial + ls);
648cdf0e10cSrcweir                     // }
649cdf0e10cSrcweir                     out.println();
650cdf0e10cSrcweir                     out.println("# resolution given in DPI");
651cdf0e10cSrcweir                     out.println("resolution=" + _aGTA.getResolutionInDPI());
652cdf0e10cSrcweir                     aIniFile.insertValue("global", "resolution", _aGTA.getResolutionInDPI());
653cdf0e10cSrcweir                 }
654cdf0e10cSrcweir                 else
655cdf0e10cSrcweir                 {
656cdf0e10cSrcweir                     out.println("buildid=" + _sSpecial);
657cdf0e10cSrcweir                     aIniFile.insertValue("global", "buildid", _sSpecial);
658cdf0e10cSrcweir                 }
659cdf0e10cSrcweir 
660cdf0e10cSrcweir                 // long nTime = stopTimer();
661cdf0e10cSrcweir                 // if (nTime != 0)
662cdf0e10cSrcweir                 // {
663cdf0e10cSrcweir                 //     out.write("# time is given in milli seconds" + ls);
664cdf0e10cSrcweir                 //     out.write("time=" + nTime + ls);
665cdf0e10cSrcweir                 // }
666cdf0e10cSrcweir 
667cdf0e10cSrcweir                 out.println();
668cdf0e10cSrcweir                 out.println("# Values out of System.getProperty(...)");
669cdf0e10cSrcweir                 out.println("os.name=" + System.getProperty("os.name"));
670cdf0e10cSrcweir                 out.println("os.arch=" + System.getProperty("os.arch"));
671cdf0e10cSrcweir                 out.println("os.version=" + System.getProperty("os.version"));
672cdf0e10cSrcweir 
673cdf0e10cSrcweir                 aIniFile.insertValue("global", "os.name", System.getProperty("os.name"));
674cdf0e10cSrcweir                 aIniFile.insertValue("global", "os.arch", System.getProperty("os.arch"));
675cdf0e10cSrcweir                 aIniFile.insertValue("global", "os.version", System.getProperty("os.version"));
676cdf0e10cSrcweir 
677cdf0e10cSrcweir                 if (_aGTA != null)
678cdf0e10cSrcweir                 {
679cdf0e10cSrcweir                     out.println();
680cdf0e10cSrcweir                     out.println("# Performance output, values are given in milli sec.");
681cdf0e10cSrcweir                     _aGTA.getPerformance().print(out);
682cdf0e10cSrcweir                     _aGTA.getPerformance().print(aIniFile, "global");
683cdf0e10cSrcweir                 }
684cdf0e10cSrcweir 
685cdf0e10cSrcweir                 out.flush();
686cdf0e10cSrcweir                 out.close();
687cdf0e10cSrcweir                 out2.close();
688cdf0e10cSrcweir             }
689cdf0e10cSrcweir             catch (java.io.IOException e)
690cdf0e10cSrcweir             {
691cdf0e10cSrcweir                 GlobalLogWriter.println("can't create Info file.");
692cdf0e10cSrcweir                 e.printStackTrace();
693cdf0e10cSrcweir             }
694cdf0e10cSrcweir             aIniFile.close();
695cdf0e10cSrcweir 
696cdf0e10cSrcweir //            String sExtension = FileHelper.getSuffix(_aGTA.getInputFile());
697cdf0e10cSrcweir //            if (sExtension.startsWith("."))
698cdf0e10cSrcweir //            {
699cdf0e10cSrcweir //                sExtension = sExtension.substring(1);
700cdf0e10cSrcweir //            }
701cdf0e10cSrcweir //
702cdf0e10cSrcweir //            DB.writeToDB(_aGTA.getInputFile(),
703cdf0e10cSrcweir //                         sNameNoSuffix,
704cdf0e10cSrcweir //                         sExtension,
705cdf0e10cSrcweir //                         sBuildID,
706cdf0e10cSrcweir //                         _aGTA.getReferenceType(),
707cdf0e10cSrcweir //                         _aGTA.getResolutionInDPI()
708cdf0e10cSrcweir //                         );
709cdf0e10cSrcweir         }
710cdf0e10cSrcweir 
addBasenameToFile(String _sIndexFilename, String _sBasename, String _sCreator, String _sType, String _sSource)711cdf0e10cSrcweir         public static void addBasenameToFile(String _sIndexFilename, String _sBasename, String _sCreator, String _sType, String _sSource)
712cdf0e10cSrcweir         {
713cdf0e10cSrcweir             // String sOutputDir = FileHelper.getPath(_sOutputFilename);
714cdf0e10cSrcweir             String sPath;
715cdf0e10cSrcweir             if (_sIndexFilename.startsWith("file:"))
716cdf0e10cSrcweir             {
717cdf0e10cSrcweir                 sPath = FileHelper.getSystemPathFromFileURL(_sIndexFilename);
718cdf0e10cSrcweir             }
719cdf0e10cSrcweir             else
720cdf0e10cSrcweir             {
721cdf0e10cSrcweir                 sPath = _sIndexFilename;
722cdf0e10cSrcweir             }
723cdf0e10cSrcweir             String sIndexFilename = sPath; // FileHelper.appendPath(sPath, _sFilename);
724cdf0e10cSrcweir             IniFile aIniFile = new IniFile(sIndexFilename);
725cdf0e10cSrcweir             aIniFile.insertValue(_sBasename, "creator", _sCreator);
726cdf0e10cSrcweir             aIniFile.insertValue(_sBasename, "type", _sType);
727cdf0e10cSrcweir             aIniFile.insertValue(_sBasename, "source", _sSource);
728cdf0e10cSrcweir             aIniFile.close();
729cdf0e10cSrcweir //            File aFile = new File(sIndexFilename);
730cdf0e10cSrcweir //            try
731cdf0e10cSrcweir //            {
732cdf0e10cSrcweir //                RandomAccessFile aRandomAccess = new RandomAccessFile(aFile, "rw");
733cdf0e10cSrcweir //                // String sBasename = FileHelper.getBasename(_sOutputFilename);
734cdf0e10cSrcweir //                aRandomAccess.seek(aRandomAccess.length()); // jump to the end.
735cdf0e10cSrcweir //// TODO: seems to be wrong, there exist no writeLine() with 'return' ending?
736cdf0e10cSrcweir //                aRandomAccess.writeUTF(_sBasename);
737cdf0e10cSrcweir //                aRandomAccess.close();
738cdf0e10cSrcweir //            }
739cdf0e10cSrcweir //            catch (java.io.FileNotFoundException e)
740cdf0e10cSrcweir //            {
741cdf0e10cSrcweir //            }
742cdf0e10cSrcweir //            catch (java.io.IOException e)
743cdf0e10cSrcweir //            {
744cdf0e10cSrcweir //            }
745cdf0e10cSrcweir         }
746cdf0e10cSrcweir 
addBasenameToPostscript(String _sOutputFilename)747cdf0e10cSrcweir         public static void addBasenameToPostscript(String _sOutputFilename)
748cdf0e10cSrcweir         {
749cdf0e10cSrcweir             String sIndexFilename = FileHelper.appendPath(_sOutputFilename, "postscript.ini");
750cdf0e10cSrcweir             // String sPath = FileHelper.getPath(sIndexFilename);
751cdf0e10cSrcweir             String sBasename = FileHelper.getBasename(_sOutputFilename);
752cdf0e10cSrcweir             addBasenameToFile(sIndexFilename, sBasename, "", "", "");
753cdf0e10cSrcweir         }
addBasenameToIndex(String _sOutputFilename, String _sBasename, String _sCreator, String _sType, String _sSource)754cdf0e10cSrcweir         public static void addBasenameToIndex(String _sOutputFilename, String _sBasename, String _sCreator, String _sType, String _sSource)
755cdf0e10cSrcweir         {
756cdf0e10cSrcweir             String sIndexFilename = FileHelper.appendPath(_sOutputFilename, "index.ini");
757cdf0e10cSrcweir             // String sPath = FileHelper.getPath(sIndexFilename);
758cdf0e10cSrcweir             // String sBasename = FileHelper.getBasename(_sOutputFilename);
759cdf0e10cSrcweir             addBasenameToFile(sIndexFilename, _sBasename, _sCreator, _sType, _sSource);
760cdf0e10cSrcweir         }
761cdf0e10cSrcweir 
762cdf0e10cSrcweir }
763cdf0e10cSrcweir 
764