2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 import java
.io
.FileFilter
;
23 import java
.io
.FileInputStream
;
24 import java
.io
.FileOutputStream
;
25 import java
.io
.InputStream
;
26 import java
.io
.OutputStream
;
27 import java
.util
.StringTokenizer
;
28 import helper
.OSHelper
;
30 import java
.io
.PrintStream
;
31 import javax
.swing
.JOptionPane
;
33 public class FileHelper
37 // fs = System.getProperty("file.separator");
40 String sOSName
= System
.getProperty("os.name");
41 String sOSArch
= System
.getProperty("os.arch");
42 String sOSVersion
= System
.getProperty("os.version");
44 GlobalLogWriter
.println(sOSName
);
45 GlobalLogWriter
.println(sOSArch
);
46 GlobalLogWriter
.println(sOSVersion
);
50 public static void MessageBox(String _sStr
)
52 String sVersion
= System
.getProperty("java.version");
53 String sOSName
= System
.getProperty("os.name");
54 JOptionPane
.showMessageDialog( null, _sStr
, sVersion
+ " " + sOSName
+ " Hello World Debugger", JOptionPane
.INFORMATION_MESSAGE
);
57 public static boolean exists(String _sFile
)
64 File aFile
= new File(_sFile
);
69 // This is just nice for DEBUG behaviour
70 // due to the fact this is absolutly context dependency no one should use it.
73 // System.out.println("FileHelper:exists() tell this path doesn't exists. Check it. path is:" );
74 // System.out.println( _sFile );
75 // System.out.println( aFile.getAbsolutePath() );
76 // MessageBox("Der JavaProzess wartet auf eine interaktion ihrerseits.");
78 // File aFile2 = new File(_sFile);
79 // if (aFile2.exists())
81 // System.out.println("Thanks, file exists." );
88 public static boolean isDir(String _sDir
)
96 File aFile
= new File(_sDir
);
97 if (aFile
.exists() && aFile
.isDirectory())
102 catch (NullPointerException e
)
104 GlobalLogWriter
.println("Exception caught. FileHelper.isDir('" + _sDir
+ "')");
110 public static String
getBasename(String _sFilename
)
112 if (_sFilename
== null)
116 // String fs = System.getProperty("file.separator");
118 int nIdx
= _sFilename
.lastIndexOf("\\");
121 nIdx
= _sFilename
.lastIndexOf("/");
125 return _sFilename
.substring(nIdx
+ 1);
130 public static String
getNameNoSuffix(String _sFilename
)
132 if (_sFilename
== null)
136 int nIdx
= _sFilename
.lastIndexOf(".");
139 return _sFilename
.substring(0, nIdx
);
144 public static String
getSuffix(String _sFilename
)
146 if (_sFilename
== null)
150 int nIdx
= _sFilename
.lastIndexOf(".");
153 return _sFilename
.substring(nIdx
);
158 public static String
getPath(String _sFilename
)
160 if (_sFilename
== null)
164 // String fs = System.getProperty("file.separator");
166 int nIdx
= _sFilename
.lastIndexOf("\\");
169 nIdx
= _sFilename
.lastIndexOf("/");
173 return _sFilename
.substring(0, nIdx
);
179 static ArrayList files = new ArrayList();
180 public static Object[] traverse( String afileDirectory )
183 File fileDirectory = new File(afileDirectory);
184 // Testing, if the file is a directory, and if so, it throws an exception
185 if ( !fileDirectory.isDirectory() )
187 throw new IllegalArgumentException( "not a directory: " + fileDirectory.getName() );
190 // Getting all files and directories in the current directory
191 File[] entries = fileDirectory.listFiles();
193 // Iterating for each file and directory
194 for ( int i = 0; i < entries.length; ++i )
196 // adding file to List
199 // Composing the URL by replacing all backslashs
200 String stringUrl = "file:///"
201 + entries[ i ].getAbsolutePath().replace( '\\', '/' );
202 files.add(stringUrl);
204 catch( Exception exception )
206 exception.printStackTrace();
209 return files.toArray();
213 // makeDirectories("", "/tmp/a/b");
214 // creates all directories /tmp/a/b
216 public static void makeDirectories(String first
, String path
)
218 makeDirectories(first
, path
, "0777");
221 public static void makeDirectories(String first
, String path
, String _sMode
)
223 String fs
= System
.getProperty("file.separator");
224 if (path
.startsWith(fs
+ fs
)) // starts with UNC Path
226 int n
= path
.indexOf(fs
, 2);
227 n
= path
.indexOf(fs
, n
+ 1);
228 first
= path
.substring(0, n
);
229 path
= path
.substring(n
+ 1);
232 String already_done
= null;
233 StringTokenizer path_tokenizer
= new StringTokenizer(path
,fs
,false);
234 already_done
= first
;
235 while (path_tokenizer
.hasMoreTokens())
237 String part
= path_tokenizer
.nextToken();
238 File new_dir
= new File(already_done
+ File
.separatorChar
+ part
);
239 already_done
= new_dir
.toString();
240 // System.out.println(already_done);
241 //create the directory
243 if (OSHelper
.isUnix() &&
248 chmod(new_dir
, _sMode
);
250 catch (java
.io
.IOException e
)
252 GlobalLogWriter
.println("Exception caught. FileHelper.makeDirectories('" + new_dir
.getAbsolutePath() + "')");
259 public static void chmod(File file
, String mode
) throws java
.io
.IOException
261 Runtime
.getRuntime().exec
263 {"chmod", mode
, file
.getAbsolutePath()});
266 public static String
removeFirstDirectorysAndBasenameFrom(String _sName
, String _sRemovePath
)
268 // pre: _sName: /a/b/c/d/e/f.g _sRemovePath /a/b/c
270 String fs
= System
.getProperty("file.separator");
272 String sBasename
= FileHelper
.getBasename(_sName
);
273 String sSubDirs
= "";
274 if (_sName
.startsWith(_sRemovePath
))
276 // if _sName starts with _sRemovePath
277 int nRemovePathIndex
= _sRemovePath
.length();
278 if (! _sRemovePath
.endsWith(fs
))
280 // add 1 if we not ends with file separator
283 int nBasenameIndex
= _sName
.length() - sBasename
.length() - 1;
284 if (nRemovePathIndex
< nBasenameIndex
)
286 sSubDirs
= _sName
.substring(nRemovePathIndex
, nBasenameIndex
);
291 // special case, the _sRemovePath is not part of _sName
292 sSubDirs
= FileHelper
.getPath(_sName
);
293 if (sSubDirs
.startsWith(fs
))
295 // remove leading file separator
296 sSubDirs
= sSubDirs
.substring(1);
303 public static void test_removeFirstDirectorysAndBasenameFrom()
305 removeFirstDirectorysAndBasenameFrom("/a/b/c/d/e/f.g", "/a/b/c");
306 removeFirstDirectorysAndBasenameFrom("/a/b/c/d/e/f.g", "/a/b/c/");
307 removeFirstDirectorysAndBasenameFrom("/a/b/c/d/e/f.g", "/b/c");
311 public static String
getSystemPathFromFileURL( String _sFileURL
)
313 String sSystemFile
= null;
315 if(_sFileURL
.startsWith("file:///"))
317 if (OSHelper
.isWindows())
319 sSystemFile
= _sFileURL
.substring(8);
323 sSystemFile
= _sFileURL
.substring(7);
326 else if (_sFileURL
.startsWith("file://"))
328 sSystemFile
= _sFileURL
.substring(5);
330 String fs
= System
.getProperty("file.separator");
331 if (! fs
.equals("/"))
333 sSystemFile
= sSystemFile
.replace ('/', fs
.toCharArray ()[0]);
335 // FEATURE FOR UNC NEED!!!
339 private static boolean m_bDebugTextShown
= false;
340 public static boolean isDebugEnabled()
342 boolean bDebug
= false;
343 String sTmpPath
= util
.utils
.getUsersTempDir();
344 //util.utils.getUsersTempDir();
345 String fs
= System
.getProperty("file.separator");
346 String sName
= sTmpPath
+ fs
+ "DOC_COMPARATOR_DEBUG";
347 File aFile
= new File(sName
);
350 if (m_bDebugTextShown
== false)
352 GlobalLogWriter
.println("Found file: " + sName
);
353 GlobalLogWriter
.println("Activate debug mode.");
354 GlobalLogWriter
.println("If debug mode is no longer necessary, remove the above file.");
355 m_bDebugTextShown
= true;
362 private static void copyStream(InputStream _aIn
, OutputStream _aOut
) throws java
.io
.IOException
364 byte[] aBuffer
= new byte[0xFFFF];
365 for (int len
; (len
= _aIn
.read(aBuffer
)) != -1; )
367 _aOut
.write(aBuffer
, 0, len
);
371 public static void copy(String _sSource
, String _sDestination
)
373 FileInputStream aFIS
= null;
374 FileOutputStream aFOS
= null;
378 aFIS
= new FileInputStream(_sSource
);
379 aFOS
= new FileOutputStream(_sDestination
);
380 copyStream(aFIS
, aFOS
);
382 catch (java
.io
.IOException e
)
384 System
.out
.println("Error: caught Exception: " + e
.getMessage());
394 catch (java
.io
.IOException e
)
396 System
.out
.println("Error: caught Exception: " + e
.getMessage());
405 catch (java
.io
.IOException e
)
407 System
.out
.println("Error: caught Exception: " + e
.getMessage());
414 // File inputFile = new File(_sSource);
415 // File outputFile = new File(_sDestination);
417 // java.io.FileReader in = new java.io.FileReader(inputFile);
418 // java.io.FileWriter out = new java.io.FileWriter(outputFile);
421 // while ((c = in.read()) != -1)
429 // catch (java.io.IOException e)
431 // GlobalLogWriter.get().println("Exception caught. FileHelper.copy('" + _sSource + ", " + _sDestination + "')");
432 // GlobalLogWriter.get().println("Message: " + e.getMessage());
438 * Within the directory run through, it's possible to say which file extension types should not
439 * consider like '*.prn' because it's not a document.
441 * @return a FileFilter function
443 public static FileFilter
getFileFilter()
445 FileFilter aFileFilter
= new FileFilter()
447 public boolean accept( File pathname
)
449 // leave out files which started by '~$' these are Microsoft Office temp files
450 if (pathname
.getName().startsWith("~$"))
454 // leave out files starts with '.~lock.' these are OpenOffice.org lock files
455 if (pathname
.getName().startsWith(".~lock."))
459 // leave out files ends with '#' these could be temp files
460 if (pathname
.getName().endsWith("#"))
464 if (pathname
.getName().endsWith(".prn"))
468 if (pathname
.getName().endsWith(".ps"))
472 // This type of document no one would like to load.
473 if (pathname
.getName().endsWith(".zip"))
478 if (pathname
.getName().endsWith("_"))
488 * Within the directory run through, it's possible to say which file extension types should not
489 * consider like '*.prn' because it's not a document.
491 * @return a FileFilter function
493 public static FileFilter
getFileFilterPSorPDF()
495 FileFilter aFileFilter
= new FileFilter()
497 public boolean accept( File pathname
)
499 if (pathname
.getName().endsWith(".ps"))
503 if (pathname
.getName().endsWith(".pdf"))
513 * Within the directory run through, it's possible to say which file extension types should not
514 * consider like '*.prn' because it's not a document.
516 * @return a FileFilter function
518 public static FileFilter
getFileFilterJPEG()
520 FileFilter aFileFilter
= new FileFilter()
522 public boolean accept( File pathname
)
524 if (pathname
.getName().toLowerCase().endsWith(".jpg"))
528 if (pathname
.getName().toLowerCase().endsWith(".jpeg"))
538 * Within the directory run through, it's possible to say which file extension types should not
539 * consider like '*.ini' because it's not a document.
541 * @return a FileFilter function
543 public static FileFilter
getFileFilterINI()
545 FileFilter aFileFilter
= new FileFilter()
547 public boolean accept( File pathname
)
549 String sPathname
= pathname
.getName().toLowerCase();
550 if (sPathname
.endsWith("index.ini"))
552 // don't consider the index.ini file
555 if (sPathname
.endsWith(".ini"))
565 public static String
appendPath(String _sPath
, String _sRelativePathToAdd
)
567 String sNewPath
= _sPath
;
568 String fs
= System
.getProperty("file.separator");
569 if (_sPath
.startsWith("file:"))
571 fs
= "/"; // we use a file URL so only '/' is allowed.
573 if (! (sNewPath
.endsWith("/") || sNewPath
.endsWith("\\") ) )
577 sNewPath
+= _sRelativePathToAdd
;
581 // -----------------------------------------------------------------------------
582 public static void createInfoFile(String _sFile
, ParameterHelper _aGTA
)
584 createInfoFile(_sFile
, _aGTA
, "");
587 public static void createInfoFile(String _sFile
, ParameterHelper _aGTA
, String _sSpecial
)
590 if (_sFile
.startsWith("file://"))
592 sFilename
= FileHelper
.getSystemPathFromFileURL(_sFile
);
593 GlobalLogWriter
.println("CreateInfoFile: '" + sFilename
+ "'" );
599 String sFileDir
= FileHelper
.getPath(sFilename
);
600 String sBasename
= FileHelper
.getBasename(sFilename
);
601 String sNameNoSuffix
= FileHelper
.getNameNoSuffix(sBasename
);
603 String sIniFile
= FileHelper
.appendPath(sFileDir
, sBasename
+ ".ini");
604 IniFile aIniFile
= new IniFile(sIniFile
);
608 System
.getProperty("line.separator");
609 String sInfoFilename
= FileHelper
.appendPath(sFileDir
, sNameNoSuffix
+ ".info");
610 File aInfoFile
= new File(sInfoFilename
);
612 String sBuildID
= "";
616 FileOutputStream out2
= new FileOutputStream(aInfoFile
.toString());
617 PrintStream out
= new PrintStream(out2
);
619 out
.println("# automatically created file by graphical compare");
622 if (_sSpecial
!= null && _sSpecial
.equals("msoffice"))
624 out
.println("# buildid from wordloadfile");
625 sBuildID
= _aGTA
.getPerformance().getMSOfficeVersion();
626 out
.println("buildid=" + sBuildID
);
630 out
.println("# buildid is read out of the bootstrap file");
631 sBuildID
= _aGTA
.getBuildID();
632 out
.println("buildid=" + sBuildID
);
634 aIniFile
.insertValue("global", "buildid", sBuildID
);
636 // if (_sSpecial != null && _sSpecial.length() > 0)
638 // out.write("special=" + _sSpecial + ls);
641 out
.println("# resolution given in DPI");
642 out
.println("resolution=" + _aGTA
.getResolutionInDPI());
643 aIniFile
.insertValue("global", "resolution", _aGTA
.getResolutionInDPI());
647 out
.println("buildid=" + _sSpecial
);
648 aIniFile
.insertValue("global", "buildid", _sSpecial
);
651 // long nTime = stopTimer();
654 // out.write("# time is given in milli seconds" + ls);
655 // out.write("time=" + nTime + ls);
659 out
.println("# Values out of System.getProperty(...)");
660 out
.println("os.name=" + System
.getProperty("os.name"));
661 out
.println("os.arch=" + System
.getProperty("os.arch"));
662 out
.println("os.version=" + System
.getProperty("os.version"));
664 aIniFile
.insertValue("global", "os.name", System
.getProperty("os.name"));
665 aIniFile
.insertValue("global", "os.arch", System
.getProperty("os.arch"));
666 aIniFile
.insertValue("global", "os.version", System
.getProperty("os.version"));
671 out
.println("# Performance output, values are given in milli sec.");
672 _aGTA
.getPerformance().print(out
);
673 _aGTA
.getPerformance().print(aIniFile
, "global");
680 catch (java
.io
.IOException e
)
682 GlobalLogWriter
.println("can't create Info file.");
687 // String sExtension = FileHelper.getSuffix(_aGTA.getInputFile());
688 // if (sExtension.startsWith("."))
690 // sExtension = sExtension.substring(1);
693 // DB.writeToDB(_aGTA.getInputFile(),
697 // _aGTA.getReferenceType(),
698 // _aGTA.getResolutionInDPI()
702 public static void addBasenameToFile(String _sIndexFilename
, String _sBasename
, String _sCreator
, String _sType
, String _sSource
)
704 // String sOutputDir = FileHelper.getPath(_sOutputFilename);
706 if (_sIndexFilename
.startsWith("file:"))
708 sPath
= FileHelper
.getSystemPathFromFileURL(_sIndexFilename
);
712 sPath
= _sIndexFilename
;
714 String sIndexFilename
= sPath
; // FileHelper.appendPath(sPath, _sFilename);
715 IniFile aIniFile
= new IniFile(sIndexFilename
);
716 aIniFile
.insertValue(_sBasename
, "creator", _sCreator
);
717 aIniFile
.insertValue(_sBasename
, "type", _sType
);
718 aIniFile
.insertValue(_sBasename
, "source", _sSource
);
720 // File aFile = new File(sIndexFilename);
723 // RandomAccessFile aRandomAccess = new RandomAccessFile(aFile, "rw");
724 // // String sBasename = FileHelper.getBasename(_sOutputFilename);
725 // aRandomAccess.seek(aRandomAccess.length()); // jump to the end.
726 //// TODO: seems to be wrong, there exist no writeLine() with 'return' ending?
727 // aRandomAccess.writeUTF(_sBasename);
728 // aRandomAccess.close();
730 // catch (java.io.FileNotFoundException e)
733 // catch (java.io.IOException e)
738 public static void addBasenameToPostscript(String _sOutputFilename
)
740 String sIndexFilename
= FileHelper
.appendPath(_sOutputFilename
, "postscript.ini");
741 // String sPath = FileHelper.getPath(sIndexFilename);
742 String sBasename
= FileHelper
.getBasename(_sOutputFilename
);
743 addBasenameToFile(sIndexFilename
, sBasename
, "", "", "");
745 public static void addBasenameToIndex(String _sOutputFilename
, String _sBasename
, String _sCreator
, String _sType
, String _sSource
)
747 String sIndexFilename
= FileHelper
.appendPath(_sOutputFilename
, "index.ini");
748 // String sPath = FileHelper.getPath(sIndexFilename);
749 // String sBasename = FileHelper.getBasename(_sOutputFilename);
750 addBasenameToFile(sIndexFilename
, _sBasename
, _sCreator
, _sType
, _sSource
);