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