merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / runner / graphical / FileHelper.java
blobf806a05804f6b7b97ea8bcb23fc08b82c5a47ff5
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: FileHelper.java,v $
10 * $Revision: 1.1.2.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package graphical;
33 import java.io.File;
34 import java.io.FileFilter;
35 import java.io.FileInputStream;
36 import java.io.FileOutputStream;
37 import java.io.InputStream;
38 import java.io.OutputStream;
39 import java.util.StringTokenizer;
40 import helper.OSHelper;
42 import java.io.PrintStream;
43 import javax.swing.JOptionPane;
45 public class FileHelper
47 public FileHelper()
49 // fs = System.getProperty("file.separator");
52 String sOSName = System.getProperty("os.name");
53 String sOSArch = System.getProperty("os.arch");
54 String sOSVersion = System.getProperty("os.version");
56 GlobalLogWriter.get().println(sOSName);
57 GlobalLogWriter.get().println(sOSArch);
58 GlobalLogWriter.get().println(sOSVersion);
62 public static void MessageBox(String _sStr)
64 String sVersion = System.getProperty("java.version");
65 String sOSName = System.getProperty("os.name");
66 JOptionPane.showMessageDialog( null, _sStr, sVersion + " " + sOSName + " Hello World Debugger", JOptionPane.INFORMATION_MESSAGE );
69 public static boolean exists(String _sFile)
71 if (_sFile == null)
73 return false;
76 File aFile = new File(_sFile);
77 if (aFile.exists())
79 return true;
81 // This is just nice for DEBUG behaviour
82 // due to the fact this is absolutly context dependency no one should use it.
83 // else
84 // {
85 // System.out.println("FileHelper:exists() tell this path doesn't exists. Check it. path is:" );
86 // System.out.println( _sFile );
87 // System.out.println( aFile.getAbsolutePath() );
88 // MessageBox("Der JavaProzess wartet auf eine interaktion ihrerseits.");
89 //
90 // File aFile2 = new File(_sFile);
91 // if (aFile2.exists())
92 // {
93 // System.out.println("Thanks, file exists." );
94 // return true;
95 // }
96 // }
97 return false;
100 public static boolean isDir(String _sDir)
102 if (_sDir == null)
104 return false;
108 File aFile = new File(_sDir);
109 if (aFile.exists() && aFile.isDirectory())
111 return true;
114 catch (NullPointerException e)
116 GlobalLogWriter.get().println("Exception caught. FileHelper.isDir('" + _sDir + "')");
117 e.printStackTrace();
119 return false;
122 public static String getBasename(String _sFilename)
124 if (_sFilename == null)
126 return "";
128 // String fs = System.getProperty("file.separator");
130 int nIdx = _sFilename.lastIndexOf("\\");
131 if (nIdx == -1)
133 nIdx = _sFilename.lastIndexOf("/");
135 if (nIdx > 0)
137 return _sFilename.substring(nIdx + 1);
139 return _sFilename;
142 public static String getNameNoSuffix(String _sFilename)
144 if (_sFilename == null)
146 return "";
148 int nIdx = _sFilename.lastIndexOf(".");
149 if (nIdx > 0)
151 return _sFilename.substring(0, nIdx);
153 return _sFilename;
156 public static String getSuffix(String _sFilename)
158 if (_sFilename == null)
160 return "";
162 int nIdx = _sFilename.lastIndexOf(".");
163 if (nIdx > 0)
165 return _sFilename.substring(nIdx );
167 return "";
170 public static String getPath(String _sFilename)
172 if (_sFilename == null)
174 return "";
176 // String fs = System.getProperty("file.separator");
178 int nIdx = _sFilename.lastIndexOf("\\");
179 if (nIdx == -1)
181 nIdx = _sFilename.lastIndexOf("/");
183 if (nIdx > 0)
185 return _sFilename.substring(0, nIdx);
187 return "";
191 static ArrayList files = new ArrayList();
192 public static Object[] traverse( String afileDirectory )
195 File fileDirectory = new File(afileDirectory);
196 // Testing, if the file is a directory, and if so, it throws an exception
197 if ( !fileDirectory.isDirectory() )
199 throw new IllegalArgumentException( "not a directory: " + fileDirectory.getName() );
202 // Getting all files and directories in the current directory
203 File[] entries = fileDirectory.listFiles();
205 // Iterating for each file and directory
206 for ( int i = 0; i < entries.length; ++i )
208 // adding file to List
211 // Composing the URL by replacing all backslashs
212 String stringUrl = "file:///"
213 + entries[ i ].getAbsolutePath().replace( '\\', '/' );
214 files.add(stringUrl);
216 catch( Exception exception )
218 exception.printStackTrace();
221 return files.toArray();
225 // makeDirectories("", "/tmp/a/b");
226 // creates all directories /tmp/a/b
228 public static void makeDirectories(String first, String path)
230 makeDirectories(first, path, "0777");
233 public static void makeDirectories(String first, String path, String _sMode)
235 String fs = System.getProperty("file.separator");
236 if (path.startsWith(fs + fs)) // starts with UNC Path
238 int n = path.indexOf(fs, 2);
239 n = path.indexOf(fs, n + 1);
240 first = path.substring(0, n);
241 path = path.substring(n + 1);
244 String already_done = null;
245 StringTokenizer path_tokenizer = new StringTokenizer(path,fs,false);
246 already_done = first;
247 while (path_tokenizer.hasMoreTokens())
249 String part = path_tokenizer.nextToken();
250 File new_dir = new File(already_done + File.separatorChar + part);
251 already_done = new_dir.toString();
252 // System.out.println(already_done);
253 //create the directory
254 new_dir.mkdirs();
255 if (OSHelper.isUnix() &&
256 _sMode.length() > 0)
260 chmod(new_dir, _sMode);
262 catch (java.io.IOException e)
264 GlobalLogWriter.get().println("Exception caught. FileHelper.makeDirectories('" + new_dir.getAbsolutePath() + "')");
268 // return;
271 public static void chmod(File file, String mode) throws java.io.IOException
273 Runtime.getRuntime().exec
274 (new String[]
275 {"chmod", mode, file.getAbsolutePath()});
278 public static String removeFirstDirectorysAndBasenameFrom(String _sName, String _sRemovePath)
280 // pre: _sName: /a/b/c/d/e/f.g _sRemovePath /a/b/c
281 // result: d/e
282 String fs = System.getProperty("file.separator");
284 String sBasename = FileHelper.getBasename(_sName);
285 String sSubDirs = "";
286 if (_sName.startsWith(_sRemovePath))
288 // if _sName starts with _sRemovePath
289 int nRemovePathIndex = _sRemovePath.length();
290 if (! _sRemovePath.endsWith(fs))
292 // add 1 if we not ends with file separator
293 nRemovePathIndex ++;
295 int nBasenameIndex = _sName.length() - sBasename.length() - 1;
296 if (nRemovePathIndex < nBasenameIndex)
298 sSubDirs = _sName.substring(nRemovePathIndex, nBasenameIndex);
301 else
303 // special case, the _sRemovePath is not part of _sName
304 sSubDirs = FileHelper.getPath(_sName);
305 if (sSubDirs.startsWith(fs))
307 // remove leading file separator
308 sSubDirs = sSubDirs.substring(1);
312 return sSubDirs;
315 public static void test_removeFirstDirectorysAndBasenameFrom()
317 String a = removeFirstDirectorysAndBasenameFrom("/a/b/c/d/e/f.g", "/a/b/c");
318 // assure("", a.equals("d/e"));
319 String b = removeFirstDirectorysAndBasenameFrom("/a/b/c/d/e/f.g", "/a/b/c/");
320 // assure("", b.equals("d/e"));
321 String c = removeFirstDirectorysAndBasenameFrom("/a/b/c/d/e/f.g", "/b/c");
322 // assure("", c.equals("a/b/c/d/e"));
326 public static String getSystemPathFromFileURL( String _sFileURL )
328 String sSystemFile = null;
330 if(_sFileURL.startsWith("file:///"))
332 if (OSHelper.isWindows())
334 sSystemFile = _sFileURL.substring(8);
336 else
338 sSystemFile = _sFileURL.substring(7);
341 else if (_sFileURL.startsWith("file://"))
343 sSystemFile = _sFileURL.substring(5);
345 String fs = System.getProperty("file.separator");
346 if (! fs.equals("/"))
348 sSystemFile = sSystemFile.replace ('/', fs.toCharArray ()[0]);
350 // FEATURE FOR UNC NEED!!!
351 return sSystemFile;
354 private static boolean m_bDebugTextShown = false;
355 public static boolean isDebugEnabled()
357 boolean bDebug = false;
358 String sTmpPath = util.utils.getUsersTempDir();
359 //util.utils.getUsersTempDir();
360 String fs = System.getProperty("file.separator");
361 String sName = sTmpPath + fs + "DOC_COMPARATOR_DEBUG";
362 File aFile = new File(sName);
363 if (aFile.exists())
365 if (m_bDebugTextShown == false)
367 GlobalLogWriter.get().println("Found file: " + sName);
368 GlobalLogWriter.get().println("Activate debug mode.");
369 GlobalLogWriter.get().println("If debug mode is no longer necessary, remove the above file.");
370 m_bDebugTextShown = true;
372 bDebug = true;
374 return bDebug;
377 private static void copyStream(InputStream _aIn, OutputStream _aOut) throws java.io.IOException
379 byte[] aBuffer = new byte[0xFFFF];
380 for (int len; (len = _aIn.read(aBuffer)) != -1; )
382 _aOut.write(aBuffer, 0, len);
386 public static void copy(String _sSource, String _sDestination)
388 FileInputStream aFIS = null;
389 FileOutputStream aFOS = null;
393 aFIS = new FileInputStream(_sSource);
394 aFOS = new FileOutputStream(_sDestination);
395 copyStream(aFIS, aFOS);
397 catch (java.io.IOException e)
399 System.out.println("Error: caught Exception: " + e.getMessage());
401 finally
403 if (aFIS != null)
407 aFIS.close();
409 catch (java.io.IOException e)
411 System.out.println("Error: caught Exception: " + e.getMessage());
414 if (aFOS != null)
418 aFOS.close();
420 catch (java.io.IOException e)
422 System.out.println("Error: caught Exception: " + e.getMessage());
427 // try
428 // {
429 // File inputFile = new File(_sSource);
430 // File outputFile = new File(_sDestination);
432 // java.io.FileReader in = new java.io.FileReader(inputFile);
433 // java.io.FileWriter out = new java.io.FileWriter(outputFile);
434 // int c;
436 // while ((c = in.read()) != -1)
437 // {
438 // out.write(c);
439 // }
441 // in.close();
442 // out.close();
443 // }
444 // catch (java.io.IOException e)
445 // {
446 // GlobalLogWriter.get().println("Exception caught. FileHelper.copy('" + _sSource + ", " + _sDestination + "')");
447 // GlobalLogWriter.get().println("Message: " + e.getMessage());
448 // }
453 * Within the directory run through, it's possible to say which file extension types should not
454 * consider like '*.prn' because it's not a document.
456 * @return a FileFilter function
458 public static FileFilter getFileFilter()
460 FileFilter aFileFilter = new FileFilter()
462 public boolean accept( File pathname )
464 // leave out files which started by '~$' these are Microsoft Office temp files
465 if (pathname.getName().startsWith("~$"))
467 return false;
469 if (pathname.getName().endsWith(".prn"))
471 return false;
473 if (pathname.getName().endsWith(".ps"))
475 return false;
477 // This type of document no one would like to load.
478 if (pathname.getName().endsWith(".zip"))
480 return false;
482 // just a hack
483 if (pathname.getName().endsWith("_"))
485 return false;
487 return true;
490 return aFileFilter;
493 * Within the directory run through, it's possible to say which file extension types should not
494 * consider like '*.prn' because it's not a document.
496 * @return a FileFilter function
498 public static FileFilter getFileFilterPSorPDF()
500 FileFilter aFileFilter = new FileFilter()
502 public boolean accept( File pathname )
504 if (pathname.getName().endsWith(".ps"))
506 return true;
508 if (pathname.getName().endsWith(".pdf"))
510 return true;
512 return false;
515 return aFileFilter;
518 * Within the directory run through, it's possible to say which file extension types should not
519 * consider like '*.prn' because it's not a document.
521 * @return a FileFilter function
523 public static FileFilter getFileFilterJPEG()
525 FileFilter aFileFilter = new FileFilter()
527 public boolean accept( File pathname )
529 if (pathname.getName().toLowerCase().endsWith(".jpg"))
531 return true;
533 if (pathname.getName().toLowerCase().endsWith(".jpeg"))
535 return true;
537 return false;
540 return aFileFilter;
543 * Within the directory run through, it's possible to say which file extension types should not
544 * consider like '*.ini' because it's not a document.
546 * @return a FileFilter function
548 public static FileFilter getFileFilterINI()
550 FileFilter aFileFilter = new FileFilter()
552 public boolean accept( File pathname )
554 String sPathname = pathname.getName().toLowerCase();
555 if (sPathname.endsWith("index.ini"))
557 // don't consider the index.ini file
558 return false;
560 if (sPathname.endsWith(".ini"))
562 return true;
564 return false;
567 return aFileFilter;
570 public static String appendPath(String _sPath, String _sRelativePathToAdd)
572 String sNewPath = _sPath;
573 String fs = System.getProperty("file.separator");
574 if (_sPath.startsWith("file:"))
576 fs = "/"; // we use a file URL so only '/' is allowed.
578 if (! (sNewPath.endsWith("/") || sNewPath.endsWith("\\") ) )
580 sNewPath += fs;
582 sNewPath += _sRelativePathToAdd;
583 return sNewPath;
586 // -----------------------------------------------------------------------------
587 public static void createInfoFile(String _sFile, ParameterHelper _aGTA)
589 createInfoFile(_sFile, _aGTA, "");
592 public static void createInfoFile(String _sFile, ParameterHelper _aGTA, String _sSpecial)
594 String sFilename;
595 if (_sFile.startsWith("file://"))
597 sFilename = FileHelper.getSystemPathFromFileURL(_sFile);
598 GlobalLogWriter.get().println("CreateInfoFile: '" + sFilename + "'" );
600 else
602 sFilename = _sFile;
604 String sFileDir = FileHelper.getPath(sFilename);
605 String sBasename = FileHelper.getBasename(sFilename);
606 String sNameNoSuffix = FileHelper.getNameNoSuffix(sBasename);
608 String sIniFile = FileHelper.appendPath(sFileDir, sBasename + ".ini");
609 IniFile aIniFile = new IniFile(sIniFile);
611 // OLD INFO FILE
613 // String fs = System.getProperty("file.separator");
614 String ls = System.getProperty("line.separator");
615 String sInfoFilename = FileHelper.appendPath(sFileDir, sNameNoSuffix + ".info");
616 File aInfoFile = new File(sInfoFilename);
618 String sBuildID = "";
622 FileOutputStream out2 = new FileOutputStream(aInfoFile.toString());
623 PrintStream out = new PrintStream(out2);
625 out.println("# automatically created file by graphical compare");
626 if (_aGTA != null)
628 if (_sSpecial != null && _sSpecial.equals("msoffice"))
630 out.println("# buildid from wordloadfile");
631 sBuildID = _aGTA.getPerformance().getMSOfficeVersion();
632 out.println("buildid=" + sBuildID);
634 else
636 out.println("# buildid is read out of the bootstrap file");
637 sBuildID = _aGTA.getBuildID();
638 out.println("buildid=" + sBuildID);
640 aIniFile.insertValue("global", "buildid", sBuildID);
642 // if (_sSpecial != null && _sSpecial.length() > 0)
643 // {
644 // out.write("special=" + _sSpecial + ls);
645 // }
646 out.println();
647 out.println("# resolution given in DPI");
648 out.println("resolution=" + _aGTA.getResolutionInDPI());
649 aIniFile.insertValue("global", "resolution", _aGTA.getResolutionInDPI());
651 else
653 out.println("buildid=" + _sSpecial);
654 aIniFile.insertValue("global", "buildid", _sSpecial);
657 // long nTime = stopTimer();
658 // if (nTime != 0)
659 // {
660 // out.write("# time is given in milli seconds" + ls);
661 // out.write("time=" + nTime + ls);
662 // }
664 out.println();
665 out.println("# Values out of System.getProperty(...)");
666 out.println("os.name=" + System.getProperty("os.name"));
667 out.println("os.arch=" + System.getProperty("os.arch"));
668 out.println("os.version=" + System.getProperty("os.version"));
670 aIniFile.insertValue("global", "os.name", System.getProperty("os.name"));
671 aIniFile.insertValue("global", "os.arch", System.getProperty("os.arch"));
672 aIniFile.insertValue("global", "os.version", System.getProperty("os.version"));
674 if (_aGTA != null)
676 out.println();
677 out.println("# Performance output, values are given in milli sec.");
678 _aGTA.getPerformance().print(out);
679 _aGTA.getPerformance().print(aIniFile, "global");
682 out.flush();
683 out.close();
684 out2.close();
686 catch (java.io.IOException e)
688 GlobalLogWriter.get().println("can't create Info file.");
689 e.printStackTrace();
691 aIniFile.close();
693 // String sExtension = FileHelper.getSuffix(_aGTA.getInputFile());
694 // if (sExtension.startsWith("."))
695 // {
696 // sExtension = sExtension.substring(1);
697 // }
699 // DB.writeToDB(_aGTA.getInputFile(),
700 // sNameNoSuffix,
701 // sExtension,
702 // sBuildID,
703 // _aGTA.getReferenceType(),
704 // _aGTA.getResolutionInDPI()
705 // );
708 public static void addBasenameToFile(String _sIndexFilename, String _sBasename, String _sCreator, String _sType, String _sSource)
710 // String sOutputDir = FileHelper.getPath(_sOutputFilename);
711 String sPath;
712 if (_sIndexFilename.startsWith("file:"))
714 sPath = FileHelper.getSystemPathFromFileURL(_sIndexFilename);
716 else
718 sPath = _sIndexFilename;
720 String sIndexFilename = sPath; // FileHelper.appendPath(sPath, _sFilename);
721 IniFile aIniFile = new IniFile(sIndexFilename);
722 aIniFile.insertValue(_sBasename, "creator", _sCreator);
723 aIniFile.insertValue(_sBasename, "type", _sType);
724 aIniFile.insertValue(_sBasename, "source", _sSource);
725 aIniFile.close();
726 // File aFile = new File(sIndexFilename);
727 // try
728 // {
729 // RandomAccessFile aRandomAccess = new RandomAccessFile(aFile, "rw");
730 // // String sBasename = FileHelper.getBasename(_sOutputFilename);
731 // aRandomAccess.seek(aRandomAccess.length()); // jump to the end.
732 //// TODO: seems to be wrong, there exist no writeLine() with 'return' ending?
733 // aRandomAccess.writeUTF(_sBasename);
734 // aRandomAccess.close();
735 // }
736 // catch (java.io.FileNotFoundException e)
737 // {
738 // }
739 // catch (java.io.IOException e)
740 // {
741 // }
744 public static void addBasenameToPostscript(String _sOutputFilename)
746 String sIndexFilename = FileHelper.appendPath(_sOutputFilename, "postscript.ini");
747 // String sPath = FileHelper.getPath(sIndexFilename);
748 String sBasename = FileHelper.getBasename(_sOutputFilename);
749 addBasenameToFile(sIndexFilename, sBasename, "", "", "");
751 public static void addBasenameToIndex(String _sOutputFilename, String _sBasename, String _sCreator, String _sType, String _sSource)
753 String sIndexFilename = FileHelper.appendPath(_sOutputFilename, "index.ini");
754 // String sPath = FileHelper.getPath(sIndexFilename);
755 // String sBasename = FileHelper.getBasename(_sOutputFilename);
756 addBasenameToFile(sIndexFilename, _sBasename, _sCreator, _sType, _sSource);