bump product version to 4.1.6.2
[LibreOffice.git] / qadevOOo / runner / convwatch / FileHelper.java
blob202f3d7712a7c38ba8d45fe0b40b3954fdb00012
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 convwatch;
21 import java.io.File;
22 import java.io.FileFilter;
23 import java.util.StringTokenizer;
24 import helper.OSHelper;
26 import javax.swing.JOptionPane;
28 public class FileHelper
30 public FileHelper()
32 // fs = System.getProperty("file.separator");
35 String sOSName = System.getProperty("os.name");
36 String sOSArch = System.getProperty("os.arch");
37 String sOSVersion = System.getProperty("os.version");
39 GlobalLogWriter.get().println(sOSName);
40 GlobalLogWriter.get().println(sOSArch);
41 GlobalLogWriter.get().println(sOSVersion);
45 public static void MessageBox(String _sStr)
47 String sVersion = System.getProperty("java.version");
48 String sOSName = System.getProperty("os.name");
49 JOptionPane.showMessageDialog( null, _sStr, sVersion + " " + sOSName + " Hello World Debugger", JOptionPane.INFORMATION_MESSAGE );
52 public static boolean exists(String _sFile)
54 if (_sFile == null) return false;
56 File aFile = new File(_sFile);
57 if (aFile.exists())
59 return true;
61 // This is just nice for DEBUG behaviour
62 // due to the fact this is absolutly context dependency no one should use it.
63 // else
64 // {
65 // System.out.println("FileHelper:exists() tell this path doesn't exists. Check it. path is:" );
66 // System.out.println( _sFile );
67 // System.out.println( aFile.getAbsolutePath() );
68 // MessageBox("Der JavaProzess wartet auf eine interaktion ihrerseits.");
70 // File aFile2 = new File(_sFile);
71 // if (aFile2.exists())
72 // {
73 // System.out.println("Thanks, file exists." );
74 // return true;
75 // }
76 // }
77 return false;
80 public static boolean isDir(String _sDir)
82 if (_sDir == null) return false;
83 try
85 File aFile = new File(_sDir);
86 if (aFile.exists() && aFile.isDirectory())
88 return true;
91 catch (NullPointerException e)
93 GlobalLogWriter.get().println("Exception caught. FileHelper.isDir('" + _sDir + "')");
94 e.printStackTrace();
96 return false;
99 public static String getBasename(String _sFilename)
101 if (_sFilename == null) return "";
102 String fs = System.getProperty("file.separator");
104 int nIdx = _sFilename.lastIndexOf(fs);
105 if (nIdx > 0)
107 return _sFilename.substring(nIdx + 1);
109 return _sFilename;
112 public static String getNameNoSuffix(String _sFilename)
114 if (_sFilename == null) return "";
115 int nIdx = _sFilename.lastIndexOf(".");
116 if (nIdx > 0)
118 return _sFilename.substring(0, nIdx);
120 return _sFilename;
123 public static String getSuffix(String _sFilename)
125 if (_sFilename == null) return "";
126 int nIdx = _sFilename.lastIndexOf(".");
127 if (nIdx > 0)
129 return _sFilename.substring(nIdx );
131 return "";
134 public static String getPath(String _sFilename)
136 if (_sFilename == null) return "";
137 String fs = System.getProperty("file.separator");
139 int nIdx = _sFilename.lastIndexOf(fs);
140 if (nIdx > 0)
142 return _sFilename.substring(0, nIdx);
144 return "";
148 static ArrayList files = new ArrayList();
149 public static Object[] traverse( String afileDirectory )
152 File fileDirectory = new File(afileDirectory);
153 // Testing, if the file is a directory, and if so, it throws an exception
154 if ( !fileDirectory.isDirectory() )
156 throw new IllegalArgumentException( "not a directory: " + fileDirectory.getName() );
159 // Getting all files and directories in the current directory
160 File[] entries = fileDirectory.listFiles();
162 // Iterating for each file and directory
163 for ( int i = 0; i < entries.length; ++i )
165 // adding file to List
168 // Composing the URL by replacing all backslashs
169 String stringUrl = "file:///"
170 + entries[ i ].getAbsolutePath().replace( '\\', '/' );
171 files.add(stringUrl);
173 catch( Exception exception )
175 exception.printStackTrace();
178 return files.toArray();
182 // makeDirectories("", "/tmp/a/b");
183 // creates all directories /tmp/a/b
185 public static void makeDirectories(String first, String path)
187 makeDirectories(first, path, "0777");
190 public static void makeDirectories(String first, String path, String _sMode)
192 String fs = System.getProperty("file.separator");
193 if (path.startsWith(fs + fs)) // starts with UNC Path
195 int n = path.indexOf(fs, 2);
196 n = path.indexOf(fs, n + 1);
197 first = path.substring(0, n);
198 path = path.substring(n + 1);
201 String already_done = null;
202 StringTokenizer path_tokenizer = new StringTokenizer(path,fs,false);
203 already_done = first;
204 while (path_tokenizer.hasMoreTokens())
206 String part = path_tokenizer.nextToken();
207 File new_dir = new File(already_done + File.separatorChar + part);
208 already_done = new_dir.toString();
209 // System.out.println(already_done);
210 //create the directory
211 new_dir.mkdirs();
212 if (OSHelper.isUnix() &&
213 _sMode.length() > 0)
217 chmod(new_dir, _sMode);
219 catch (java.io.IOException e)
221 GlobalLogWriter.get().println("Exception caught. FileHelper.makeDirectories('" + new_dir.getAbsolutePath() + "')");
225 // return;
228 public static void chmod(File file, String mode) throws java.io.IOException
230 Runtime.getRuntime().exec
231 (new String[]
232 {"chmod", mode, file.getAbsolutePath()});
235 public static String removeFirstDirectorysAndBasenameFrom(String _sName, String _sRemovePath)
237 // pre: _sName: /a/b/c/d/e/f.g _sRemovePath /a/b/c
238 // result: d/e
239 String fs = System.getProperty("file.separator");
241 String sBasename = FileHelper.getBasename(_sName);
242 String sSubDirs = "";
243 if (_sName.startsWith(_sRemovePath))
245 // if _sName starts with _sRemovePath
246 int nRemovePathIndex = _sRemovePath.length();
247 if (! _sRemovePath.endsWith(fs))
249 // add 1 if we not ends with file separator
250 nRemovePathIndex ++;
252 int nBasenameIndex = _sName.length() - sBasename.length() - 1;
253 if (nRemovePathIndex < nBasenameIndex)
255 sSubDirs = _sName.substring(nRemovePathIndex, nBasenameIndex);
258 else
260 // special case, the _sRemovePath is not part of _sName
261 sSubDirs = FileHelper.getPath(_sName);
262 if (sSubDirs.startsWith(fs))
264 // remove leading file separator
265 sSubDirs = sSubDirs.substring(1);
269 return sSubDirs;
272 public static void test_removeFirstDirectorysAndBasenameFrom()
274 removeFirstDirectorysAndBasenameFrom("/a/b/c/d/e/f.g", "/a/b/c");
275 removeFirstDirectorysAndBasenameFrom("/a/b/c/d/e/f.g", "/a/b/c/");
276 removeFirstDirectorysAndBasenameFrom("/a/b/c/d/e/f.g", "/b/c");
280 public static String getSystemPathFromFileURL( String _sFileURL )
282 String sSystemFile = null;
284 if(_sFileURL.startsWith("file:///"))
286 if (OSHelper.isWindows())
288 sSystemFile = _sFileURL.substring(8);
290 else
292 sSystemFile = _sFileURL.substring(7);
295 else if (_sFileURL.startsWith("file://"))
297 sSystemFile = _sFileURL.substring(5);
299 String fs = System.getProperty("file.separator");
300 if (! fs.equals("/"))
302 sSystemFile = sSystemFile.replace ('/', fs.toCharArray ()[0]);
304 // FEATURE FOR UNC NEED!!!
305 return sSystemFile;
308 private static boolean m_bDebugTextShown = false;
309 public static boolean isDebugEnabled()
311 boolean bDebug = false;
312 String sTmpPath = util.utils.getUsersTempDir();
313 //util.utils.getUsersTempDir();
314 String fs = System.getProperty("file.separator");
315 String sName = sTmpPath + fs + "DOC_COMPARATOR_DEBUG";
316 File aFile = new File(sName);
317 if (aFile.exists())
319 if (m_bDebugTextShown == false)
321 GlobalLogWriter.get().println("Found file: " + sName);
322 GlobalLogWriter.get().println("Activate debug mode.");
323 GlobalLogWriter.get().println("If debug mode is no longer necessary, remove the above file.");
324 m_bDebugTextShown = true;
326 bDebug = true;
328 return bDebug;
331 public static void copy(String _sSource, String _sDestination)
335 File inputFile = new File(_sSource);
336 File outputFile = new File(_sDestination);
338 java.io.FileReader in = new java.io.FileReader(inputFile);
339 java.io.FileWriter out = new java.io.FileWriter(outputFile);
340 int c;
342 while ((c = in.read()) != -1)
343 out.write(c);
345 in.close();
346 out.close();
348 catch (java.io.IOException e)
350 GlobalLogWriter.get().println("Exception caught. FileHelper.copy('" + _sSource + ", " + _sDestination + "')");
351 GlobalLogWriter.get().println("Message: " + e.getMessage());
356 * Within the directory run through, it's possible to say which file extension types should not
357 * consider like '*.prn' because it's not a document.
359 * @return a FileFilter function
361 public static FileFilter getFileFilter()
363 FileFilter aFileFilter = new FileFilter()
365 public boolean accept( File pathname )
367 // leave out files which started by '~$' these are Microsoft Office temp files
368 if (pathname.getName().startsWith("~$"))
370 return false;
373 if (pathname.getName().endsWith(".prn"))
375 return false;
377 // This type of document no one would like to load.
378 if (pathname.getName().endsWith(".zip"))
380 return false;
382 // just a hack
383 if (pathname.getName().endsWith("_"))
385 return false;
387 return true;
390 return aFileFilter;