bump product version to 4.1.6.2
[LibreOffice.git] / qadevOOo / runner / graphical / FileHelper.java
blobf551cffc1da850c8f4b57fafea18907ad229e69d
1 /*
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 .
19 package graphical;
21 import java.io.File;
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
35 public 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)
59 if (_sFile == null)
61 return false;
64 File aFile = new File(_sFile);
65 if (aFile.exists())
67 return true;
69 // This is just nice for DEBUG behaviour
70 // due to the fact this is absolutly context dependency no one should use it.
71 // else
72 // {
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())
80 // {
81 // System.out.println("Thanks, file exists." );
82 // return true;
83 // }
84 // }
85 return false;
88 public static boolean isDir(String _sDir)
90 if (_sDir == null)
92 return false;
94 try
96 File aFile = new File(_sDir);
97 if (aFile.exists() && aFile.isDirectory())
99 return true;
102 catch (NullPointerException e)
104 GlobalLogWriter.println("Exception caught. FileHelper.isDir('" + _sDir + "')");
105 e.printStackTrace();
107 return false;
110 public static String getBasename(String _sFilename)
112 if (_sFilename == null)
114 return "";
116 // String fs = System.getProperty("file.separator");
118 int nIdx = _sFilename.lastIndexOf("\\");
119 if (nIdx == -1)
121 nIdx = _sFilename.lastIndexOf("/");
123 if (nIdx > 0)
125 return _sFilename.substring(nIdx + 1);
127 return _sFilename;
130 public static String getNameNoSuffix(String _sFilename)
132 if (_sFilename == null)
134 return "";
136 int nIdx = _sFilename.lastIndexOf(".");
137 if (nIdx > 0)
139 return _sFilename.substring(0, nIdx);
141 return _sFilename;
144 public static String getSuffix(String _sFilename)
146 if (_sFilename == null)
148 return "";
150 int nIdx = _sFilename.lastIndexOf(".");
151 if (nIdx > 0)
153 return _sFilename.substring(nIdx );
155 return "";
158 public static String getPath(String _sFilename)
160 if (_sFilename == null)
162 return "";
164 // String fs = System.getProperty("file.separator");
166 int nIdx = _sFilename.lastIndexOf("\\");
167 if (nIdx == -1)
169 nIdx = _sFilename.lastIndexOf("/");
171 if (nIdx > 0)
173 return _sFilename.substring(0, nIdx);
175 return "";
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
242 new_dir.mkdirs();
243 if (OSHelper.isUnix() &&
244 _sMode.length() > 0)
248 chmod(new_dir, _sMode);
250 catch (java.io.IOException e)
252 GlobalLogWriter.println("Exception caught. FileHelper.makeDirectories('" + new_dir.getAbsolutePath() + "')");
256 // return;
259 public static void chmod(File file, String mode) throws java.io.IOException
261 Runtime.getRuntime().exec
262 (new String[]
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
269 // result: d/e
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
281 nRemovePathIndex ++;
283 int nBasenameIndex = _sName.length() - sBasename.length() - 1;
284 if (nRemovePathIndex < nBasenameIndex)
286 sSubDirs = _sName.substring(nRemovePathIndex, nBasenameIndex);
289 else
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);
300 return sSubDirs;
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);
321 else
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!!!
336 return sSystemFile;
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);
348 if (aFile.exists())
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;
357 bDebug = true;
359 return bDebug;
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());
386 finally
388 if (aFIS != null)
392 aFIS.close();
394 catch (java.io.IOException e)
396 System.out.println("Error: caught Exception: " + e.getMessage());
399 if (aFOS != null)
403 aFOS.close();
405 catch (java.io.IOException e)
407 System.out.println("Error: caught Exception: " + e.getMessage());
412 // try
413 // {
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);
419 // int c;
421 // while ((c = in.read()) != -1)
422 // {
423 // out.write(c);
424 // }
426 // in.close();
427 // out.close();
428 // }
429 // catch (java.io.IOException e)
430 // {
431 // GlobalLogWriter.get().println("Exception caught. FileHelper.copy('" + _sSource + ", " + _sDestination + "')");
432 // GlobalLogWriter.get().println("Message: " + e.getMessage());
433 // }
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("~$"))
452 return false;
454 // leave out files starts with '.~lock.' these are OpenOffice.org lock files
455 if (pathname.getName().startsWith(".~lock."))
457 return false;
459 // leave out files ends with '#' these could be temp files
460 if (pathname.getName().endsWith("#"))
462 return false;
464 if (pathname.getName().endsWith(".prn"))
466 return false;
468 if (pathname.getName().endsWith(".ps"))
470 return false;
472 // This type of document no one would like to load.
473 if (pathname.getName().endsWith(".zip"))
475 return false;
477 // just a hack
478 if (pathname.getName().endsWith("_"))
480 return false;
482 return true;
485 return aFileFilter;
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"))
501 return true;
503 if (pathname.getName().endsWith(".pdf"))
505 return true;
507 return false;
510 return aFileFilter;
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"))
526 return true;
528 if (pathname.getName().toLowerCase().endsWith(".jpeg"))
530 return true;
532 return false;
535 return aFileFilter;
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
553 return false;
555 if (sPathname.endsWith(".ini"))
557 return true;
559 return false;
562 return aFileFilter;
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("\\") ) )
575 sNewPath += fs;
577 sNewPath += _sRelativePathToAdd;
578 return sNewPath;
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)
589 String sFilename;
590 if (_sFile.startsWith("file://"))
592 sFilename = FileHelper.getSystemPathFromFileURL(_sFile);
593 GlobalLogWriter.println("CreateInfoFile: '" + sFilename + "'" );
595 else
597 sFilename = _sFile;
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);
606 // OLD INFO FILE
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");
620 if (_aGTA != null)
622 if (_sSpecial != null && _sSpecial.equals("msoffice"))
624 out.println("# buildid from wordloadfile");
625 sBuildID = _aGTA.getPerformance().getMSOfficeVersion();
626 out.println("buildid=" + sBuildID);
628 else
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)
637 // {
638 // out.write("special=" + _sSpecial + ls);
639 // }
640 out.println();
641 out.println("# resolution given in DPI");
642 out.println("resolution=" + _aGTA.getResolutionInDPI());
643 aIniFile.insertValue("global", "resolution", _aGTA.getResolutionInDPI());
645 else
647 out.println("buildid=" + _sSpecial);
648 aIniFile.insertValue("global", "buildid", _sSpecial);
651 // long nTime = stopTimer();
652 // if (nTime != 0)
653 // {
654 // out.write("# time is given in milli seconds" + ls);
655 // out.write("time=" + nTime + ls);
656 // }
658 out.println();
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"));
668 if (_aGTA != null)
670 out.println();
671 out.println("# Performance output, values are given in milli sec.");
672 _aGTA.getPerformance().print(out);
673 _aGTA.getPerformance().print(aIniFile, "global");
676 out.flush();
677 out.close();
678 out2.close();
680 catch (java.io.IOException e)
682 GlobalLogWriter.println("can't create Info file.");
683 e.printStackTrace();
685 aIniFile.close();
687 // String sExtension = FileHelper.getSuffix(_aGTA.getInputFile());
688 // if (sExtension.startsWith("."))
689 // {
690 // sExtension = sExtension.substring(1);
691 // }
693 // DB.writeToDB(_aGTA.getInputFile(),
694 // sNameNoSuffix,
695 // sExtension,
696 // sBuildID,
697 // _aGTA.getReferenceType(),
698 // _aGTA.getResolutionInDPI()
699 // );
702 public static void addBasenameToFile(String _sIndexFilename, String _sBasename, String _sCreator, String _sType, String _sSource)
704 // String sOutputDir = FileHelper.getPath(_sOutputFilename);
705 String sPath;
706 if (_sIndexFilename.startsWith("file:"))
708 sPath = FileHelper.getSystemPathFromFileURL(_sIndexFilename);
710 else
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);
719 aIniFile.close();
720 // File aFile = new File(sIndexFilename);
721 // try
722 // {
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();
729 // }
730 // catch (java.io.FileNotFoundException e)
731 // {
732 // }
733 // catch (java.io.IOException e)
734 // {
735 // }
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);