1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: FileHelper.java,v $
10 * $Revision: 1.8.8.1 $
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 ************************************************************************/
34 import java
.io
.FileFilter
;
35 import java
.util
.StringTokenizer
;
36 import helper
.OSHelper
;
38 import javax
.swing
.JOptionPane
;
40 public class FileHelper
44 // fs = System.getProperty("file.separator");
47 String sOSName
= System
.getProperty("os.name");
48 String sOSArch
= System
.getProperty("os.arch");
49 String sOSVersion
= System
.getProperty("os.version");
51 GlobalLogWriter
.get().println(sOSName
);
52 GlobalLogWriter
.get().println(sOSArch
);
53 GlobalLogWriter
.get().println(sOSVersion
);
57 public static void MessageBox(String _sStr
)
59 String sVersion
= System
.getProperty("java.version");
60 String sOSName
= System
.getProperty("os.name");
61 JOptionPane
.showMessageDialog( null, _sStr
, sVersion
+ " " + sOSName
+ " Hello World Debugger", JOptionPane
.INFORMATION_MESSAGE
);
64 public static boolean exists(String _sFile
)
66 if (_sFile
== null) return false;
68 File aFile
= new File(_sFile
);
73 // This is just nice for DEBUG behaviour
74 // due to the fact this is absolutly context dependency no one should use it.
77 // System.out.println("FileHelper:exists() tell this path doesn't exists. Check it. path is:" );
78 // System.out.println( _sFile );
79 // System.out.println( aFile.getAbsolutePath() );
80 // MessageBox("Der JavaProzess wartet auf eine interaktion ihrerseits.");
82 // File aFile2 = new File(_sFile);
83 // if (aFile2.exists())
85 // System.out.println("Thanks, file exists." );
92 public static boolean isDir(String _sDir
)
94 if (_sDir
== null) return false;
97 File aFile
= new File(_sDir
);
98 if (aFile
.exists() && aFile
.isDirectory())
103 catch (NullPointerException e
)
105 GlobalLogWriter
.get().println("Exception caught. FileHelper.isDir('" + _sDir
+ "')");
111 public static String
getBasename(String _sFilename
)
113 if (_sFilename
== null) return "";
114 String fs
= System
.getProperty("file.separator");
116 int nIdx
= _sFilename
.lastIndexOf(fs
);
119 return _sFilename
.substring(nIdx
+ 1);
124 public static String
getNameNoSuffix(String _sFilename
)
126 if (_sFilename
== null) return "";
127 int nIdx
= _sFilename
.lastIndexOf(".");
130 return _sFilename
.substring(0, nIdx
);
135 public static String
getSuffix(String _sFilename
)
137 if (_sFilename
== null) return "";
138 int nIdx
= _sFilename
.lastIndexOf(".");
141 return _sFilename
.substring(nIdx
);
146 public static String
getPath(String _sFilename
)
148 if (_sFilename
== null) return "";
149 String fs
= System
.getProperty("file.separator");
151 int nIdx
= _sFilename
.lastIndexOf(fs
);
154 return _sFilename
.substring(0, nIdx
);
160 static ArrayList files = new ArrayList();
161 public static Object[] traverse( String afileDirectory )
164 File fileDirectory = new File(afileDirectory);
165 // Testing, if the file is a directory, and if so, it throws an exception
166 if ( !fileDirectory.isDirectory() )
168 throw new IllegalArgumentException( "not a directory: " + fileDirectory.getName() );
171 // Getting all files and directories in the current directory
172 File[] entries = fileDirectory.listFiles();
174 // Iterating for each file and directory
175 for ( int i = 0; i < entries.length; ++i )
177 // adding file to List
180 // Composing the URL by replacing all backslashs
181 String stringUrl = "file:///"
182 + entries[ i ].getAbsolutePath().replace( '\\', '/' );
183 files.add(stringUrl);
185 catch( Exception exception )
187 exception.printStackTrace();
190 return files.toArray();
194 // makeDirectories("", "/tmp/a/b");
195 // creates all directories /tmp/a/b
197 public static void makeDirectories(String first
, String path
)
199 makeDirectories(first
, path
, "0777");
202 public static void makeDirectories(String first
, String path
, String _sMode
)
204 String fs
= System
.getProperty("file.separator");
205 if (path
.startsWith(fs
+ fs
)) // starts with UNC Path
207 int n
= path
.indexOf(fs
, 2);
208 n
= path
.indexOf(fs
, n
+ 1);
209 first
= path
.substring(0, n
);
210 path
= path
.substring(n
+ 1);
213 String already_done
= null;
214 StringTokenizer path_tokenizer
= new StringTokenizer(path
,fs
,false);
215 already_done
= first
;
216 while (path_tokenizer
.hasMoreTokens())
218 String part
= path_tokenizer
.nextToken();
219 File new_dir
= new File(already_done
+ File
.separatorChar
+ part
);
220 already_done
= new_dir
.toString();
221 // System.out.println(already_done);
222 //create the directory
224 if (OSHelper
.isUnix() &&
229 chmod(new_dir
, _sMode
);
231 catch (java
.io
.IOException e
)
233 GlobalLogWriter
.get().println("Exception caught. FileHelper.makeDirectories('" + new_dir
.getAbsolutePath() + "')");
240 public static void chmod(File file
, String mode
) throws java
.io
.IOException
242 Runtime
.getRuntime().exec
244 {"chmod", mode
, file
.getAbsolutePath()});
247 public static String
removeFirstDirectorysAndBasenameFrom(String _sName
, String _sRemovePath
)
249 // pre: _sName: /a/b/c/d/e/f.g _sRemovePath /a/b/c
251 String fs
= System
.getProperty("file.separator");
253 String sBasename
= FileHelper
.getBasename(_sName
);
254 String sSubDirs
= "";
255 if (_sName
.startsWith(_sRemovePath
))
257 // if _sName starts with _sRemovePath
258 int nRemovePathIndex
= _sRemovePath
.length();
259 if (! _sRemovePath
.endsWith(fs
))
261 // add 1 if we not ends with file separator
264 int nBasenameIndex
= _sName
.length() - sBasename
.length() - 1;
265 if (nRemovePathIndex
< nBasenameIndex
)
267 sSubDirs
= _sName
.substring(nRemovePathIndex
, nBasenameIndex
);
272 // special case, the _sRemovePath is not part of _sName
273 sSubDirs
= FileHelper
.getPath(_sName
);
274 if (sSubDirs
.startsWith(fs
))
276 // remove leading file separator
277 sSubDirs
= sSubDirs
.substring(1);
284 public static void test_removeFirstDirectorysAndBasenameFrom()
286 String a
= removeFirstDirectorysAndBasenameFrom("/a/b/c/d/e/f.g", "/a/b/c");
287 // assure("", a.equals("d/e"));
288 String b
= removeFirstDirectorysAndBasenameFrom("/a/b/c/d/e/f.g", "/a/b/c/");
289 // assure("", b.equals("d/e"));
290 String c
= removeFirstDirectorysAndBasenameFrom("/a/b/c/d/e/f.g", "/b/c");
291 // assure("", c.equals("a/b/c/d/e"));
295 public static String
getSystemPathFromFileURL( String _sFileURL
)
297 String sSystemFile
= null;
299 if(_sFileURL
.startsWith("file:///"))
301 if (OSHelper
.isWindows())
303 sSystemFile
= _sFileURL
.substring(8);
307 sSystemFile
= _sFileURL
.substring(7);
310 else if (_sFileURL
.startsWith("file://"))
312 sSystemFile
= _sFileURL
.substring(5);
314 String fs
= System
.getProperty("file.separator");
315 if (! fs
.equals("/"))
317 sSystemFile
= sSystemFile
.replace ('/', fs
.toCharArray ()[0]);
319 // FEATURE FOR UNC NEED!!!
323 private static boolean m_bDebugTextShown
= false;
324 public static boolean isDebugEnabled()
326 boolean bDebug
= false;
327 String sTmpPath
= util
.utils
.getUsersTempDir();
328 //util.utils.getUsersTempDir();
329 String fs
= System
.getProperty("file.separator");
330 String sName
= sTmpPath
+ fs
+ "DOC_COMPARATOR_DEBUG";
331 File aFile
= new File(sName
);
334 if (m_bDebugTextShown
== false)
336 GlobalLogWriter
.get().println("Found file: " + sName
);
337 GlobalLogWriter
.get().println("Activate debug mode.");
338 GlobalLogWriter
.get().println("If debug mode is no longer necessary, remove the above file.");
339 m_bDebugTextShown
= true;
346 public static void copy(String _sSource
, String _sDestination
)
350 File inputFile
= new File(_sSource
);
351 File outputFile
= new File(_sDestination
);
353 java
.io
.FileReader in
= new java
.io
.FileReader(inputFile
);
354 java
.io
.FileWriter out
= new java
.io
.FileWriter(outputFile
);
357 while ((c
= in
.read()) != -1)
363 catch (java
.io
.IOException e
)
365 GlobalLogWriter
.get().println("Exception caught. FileHelper.copy('" + _sSource
+ ", " + _sDestination
+ "')");
366 GlobalLogWriter
.get().println("Message: " + e
.getMessage());
371 * Within the directory run through, it's possible to say which file extension types should not
372 * consider like '*.prn' because it's not a document.
374 * @return a FileFilter function
376 public static FileFilter
getFileFilter()
378 FileFilter aFileFilter
= new FileFilter()
380 public boolean accept( File pathname
)
382 // leave out files which started by '~$' these are Microsoft Office temp files
383 if (pathname
.getName().startsWith("~$"))
388 if (pathname
.getName().endsWith(".prn"))
392 // This type of document no one would like to load.
393 if (pathname
.getName().endsWith(".zip"))
398 if (pathname
.getName().endsWith("_"))