1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 package convwatch; 25 26 import java.io.File; 27 import java.io.FileFilter; 28 import java.util.StringTokenizer; 29 import helper.OSHelper; 30 31 import javax.swing.JOptionPane; 32 33 public class FileHelper 34 { FileHelper()35 public FileHelper() 36 { 37 // fs = System.getProperty("file.separator"); 38 39 40 String sOSName = System.getProperty("os.name"); 41 String sOSArch = System.getProperty("os.arch"); 42 String sOSVersion = System.getProperty("os.version"); 43 44 GlobalLogWriter.get().println(sOSName); 45 GlobalLogWriter.get().println(sOSArch); 46 GlobalLogWriter.get().println(sOSVersion); 47 48 } 49 MessageBox(String _sStr)50 public static void MessageBox(String _sStr) 51 { 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 ); 55 } 56 exists(String _sFile)57 public static boolean exists(String _sFile) 58 { 59 if (_sFile == null) return false; 60 61 File aFile = new File(_sFile); 62 if (aFile.exists()) 63 { 64 return true; 65 } 66 // This is just nice for DEBUG behaviour 67 // due to the fact this is absolutly context dependency no one should use it. 68 // else 69 // { 70 // System.out.println("FileHelper:exists() tell this path doesn't exists. Check it. path is:" ); 71 // System.out.println( _sFile ); 72 // System.out.println( aFile.getAbsolutePath() ); 73 // MessageBox("Der JavaProzess wartet auf eine interaktion ihrerseits."); 74 // 75 // File aFile2 = new File(_sFile); 76 // if (aFile2.exists()) 77 // { 78 // System.out.println("Thanks, file exists." ); 79 // return true; 80 // } 81 // } 82 return false; 83 } 84 isDir(String _sDir)85 public static boolean isDir(String _sDir) 86 { 87 if (_sDir == null) return false; 88 try 89 { 90 File aFile = new File(_sDir); 91 if (aFile.exists() && aFile.isDirectory()) 92 { 93 return true; 94 } 95 } 96 catch (NullPointerException e) 97 { 98 GlobalLogWriter.get().println("Exception caught. FileHelper.isDir('" + _sDir + "')"); 99 e.printStackTrace(); 100 } 101 return false; 102 } 103 getBasename(String _sFilename)104 public static String getBasename(String _sFilename) 105 { 106 if (_sFilename == null) return ""; 107 String fs = System.getProperty("file.separator"); 108 109 int nIdx = _sFilename.lastIndexOf(fs); 110 if (nIdx > 0) 111 { 112 return _sFilename.substring(nIdx + 1); 113 } 114 return _sFilename; 115 } 116 getNameNoSuffix(String _sFilename)117 public static String getNameNoSuffix(String _sFilename) 118 { 119 if (_sFilename == null) return ""; 120 int nIdx = _sFilename.lastIndexOf("."); 121 if (nIdx > 0) 122 { 123 return _sFilename.substring(0, nIdx); 124 } 125 return _sFilename; 126 } 127 getSuffix(String _sFilename)128 public static String getSuffix(String _sFilename) 129 { 130 if (_sFilename == null) return ""; 131 int nIdx = _sFilename.lastIndexOf("."); 132 if (nIdx > 0) 133 { 134 return _sFilename.substring(nIdx ); 135 } 136 return ""; 137 } 138 getPath(String _sFilename)139 public static String getPath(String _sFilename) 140 { 141 if (_sFilename == null) return ""; 142 String fs = System.getProperty("file.separator"); 143 144 int nIdx = _sFilename.lastIndexOf(fs); 145 if (nIdx > 0) 146 { 147 return _sFilename.substring(0, nIdx); 148 } 149 return ""; 150 } 151 152 /* 153 static ArrayList files = new ArrayList(); 154 public static Object[] traverse( String afileDirectory ) 155 { 156 157 File fileDirectory = new File(afileDirectory); 158 // Testing, if the file is a directory, and if so, it throws an exception 159 if ( !fileDirectory.isDirectory() ) 160 { 161 throw new IllegalArgumentException( "not a directory: " + fileDirectory.getName() ); 162 } 163 164 // Getting all files and directories in the current directory 165 File[] entries = fileDirectory.listFiles(); 166 167 // Iterating for each file and directory 168 for ( int i = 0; i < entries.length; ++i ) 169 { 170 // adding file to List 171 try 172 { 173 // Composing the URL by replacing all backslashs 174 String stringUrl = "file:///" 175 + entries[ i ].getAbsolutePath().replace( '\\', '/' ); 176 files.add(stringUrl); 177 } 178 catch( Exception exception ) 179 { 180 exception.printStackTrace(); 181 } 182 } 183 return files.toArray(); 184 } 185 */ 186 187 // makeDirectories("", "/tmp/a/b"); 188 // creates all directories /tmp/a/b 189 // makeDirectories(String first, String path)190 public static void makeDirectories(String first, String path) 191 { 192 makeDirectories(first, path, "0777"); 193 } 194 makeDirectories(String first, String path, String _sMode)195 public static void makeDirectories(String first, String path, String _sMode) 196 { 197 String fs = System.getProperty("file.separator"); 198 if (path.startsWith(fs + fs)) // starts with UNC Path 199 { 200 int n = path.indexOf(fs, 2); 201 n = path.indexOf(fs, n + 1); 202 first = path.substring(0, n); 203 path = path.substring(n + 1); 204 } 205 206 String already_done = null; 207 StringTokenizer path_tokenizer = new StringTokenizer(path,fs,false); 208 already_done = first; 209 while (path_tokenizer.hasMoreTokens()) 210 { 211 String part = path_tokenizer.nextToken(); 212 File new_dir = new File(already_done + File.separatorChar + part); 213 already_done = new_dir.toString(); 214 // System.out.println(already_done); 215 //create the directory 216 new_dir.mkdirs(); 217 if (OSHelper.isUnix() && 218 _sMode.length() > 0) 219 { 220 try 221 { 222 chmod(new_dir, _sMode); 223 } 224 catch (java.io.IOException e) 225 { 226 GlobalLogWriter.get().println("Exception caught. FileHelper.makeDirectories('" + new_dir.getAbsolutePath() + "')"); 227 } 228 } 229 } 230 // return; 231 } 232 chmod(File file, String mode)233 public static void chmod(File file, String mode) throws java.io.IOException 234 { 235 Runtime.getRuntime().exec 236 (new String[] 237 {"chmod", mode, file.getAbsolutePath()}); 238 } 239 removeFirstDirectorysAndBasenameFrom(String _sName, String _sRemovePath)240 public static String removeFirstDirectorysAndBasenameFrom(String _sName, String _sRemovePath) 241 { 242 // pre: _sName: /a/b/c/d/e/f.g _sRemovePath /a/b/c 243 // result: d/e 244 String fs = System.getProperty("file.separator"); 245 246 String sBasename = FileHelper.getBasename(_sName); 247 String sSubDirs = ""; 248 if (_sName.startsWith(_sRemovePath)) 249 { 250 // if _sName starts with _sRemovePath 251 int nRemovePathIndex = _sRemovePath.length(); 252 if (! _sRemovePath.endsWith(fs)) 253 { 254 // add 1 if we not ends with file separator 255 nRemovePathIndex ++; 256 } 257 int nBasenameIndex = _sName.length() - sBasename.length() - 1; 258 if (nRemovePathIndex < nBasenameIndex) 259 { 260 sSubDirs = _sName.substring(nRemovePathIndex, nBasenameIndex); 261 } 262 } 263 else 264 { 265 // special case, the _sRemovePath is not part of _sName 266 sSubDirs = FileHelper.getPath(_sName); 267 if (sSubDirs.startsWith(fs)) 268 { 269 // remove leading file separator 270 sSubDirs = sSubDirs.substring(1); 271 } 272 } 273 274 return sSubDirs; 275 } 276 test_removeFirstDirectorysAndBasenameFrom()277 public static void test_removeFirstDirectorysAndBasenameFrom() 278 { 279 String a = removeFirstDirectorysAndBasenameFrom("/a/b/c/d/e/f.g", "/a/b/c"); 280 // assure("", a.equals("d/e")); 281 String b = removeFirstDirectorysAndBasenameFrom("/a/b/c/d/e/f.g", "/a/b/c/"); 282 // assure("", b.equals("d/e")); 283 String c = removeFirstDirectorysAndBasenameFrom("/a/b/c/d/e/f.g", "/b/c"); 284 // assure("", c.equals("a/b/c/d/e")); 285 } 286 287 getSystemPathFromFileURL( String _sFileURL )288 public static String getSystemPathFromFileURL( String _sFileURL ) 289 { 290 String sSystemFile = null; 291 292 if(_sFileURL.startsWith("file:///")) 293 { 294 if (OSHelper.isWindows()) 295 { 296 sSystemFile = _sFileURL.substring(8); 297 } 298 else 299 { 300 sSystemFile = _sFileURL.substring(7); 301 } 302 } 303 else if (_sFileURL.startsWith("file://")) 304 { 305 sSystemFile = _sFileURL.substring(5); 306 } 307 String fs = System.getProperty("file.separator"); 308 if (! fs.equals("/")) 309 { 310 sSystemFile = sSystemFile.replace ('/', fs.toCharArray ()[0]); 311 } 312 // FEATURE FOR UNC NEED!!! 313 return sSystemFile; 314 } 315 316 private static boolean m_bDebugTextShown = false; isDebugEnabled()317 public static boolean isDebugEnabled() 318 { 319 boolean bDebug = false; 320 String sTmpPath = util.utils.getUsersTempDir(); 321 //util.utils.getUsersTempDir(); 322 String fs = System.getProperty("file.separator"); 323 String sName = sTmpPath + fs + "DOC_COMPARATOR_DEBUG"; 324 File aFile = new File(sName); 325 if (aFile.exists()) 326 { 327 if (m_bDebugTextShown == false) 328 { 329 GlobalLogWriter.get().println("Found file: " + sName); 330 GlobalLogWriter.get().println("Activate debug mode."); 331 GlobalLogWriter.get().println("If debug mode is no longer necessary, remove the above file."); 332 m_bDebugTextShown = true; 333 } 334 bDebug = true; 335 } 336 return bDebug; 337 } 338 copy(String _sSource, String _sDestination)339 public static void copy(String _sSource, String _sDestination) 340 { 341 try 342 { 343 File inputFile = new File(_sSource); 344 File outputFile = new File(_sDestination); 345 346 java.io.FileReader in = new java.io.FileReader(inputFile); 347 java.io.FileWriter out = new java.io.FileWriter(outputFile); 348 int c; 349 350 while ((c = in.read()) != -1) 351 out.write(c); 352 353 in.close(); 354 out.close(); 355 } 356 catch (java.io.IOException e) 357 { 358 GlobalLogWriter.get().println("Exception caught. FileHelper.copy('" + _sSource + ", " + _sDestination + "')"); 359 GlobalLogWriter.get().println("Message: " + e.getMessage()); 360 } 361 } 362 363 /** 364 * Within the directory run through, it's possible to say which file extension types should not 365 * consider like '*.prn' because it's not a document. 366 * 367 * @return a FileFilter function 368 */ getFileFilter()369 public static FileFilter getFileFilter() 370 { 371 FileFilter aFileFilter = new FileFilter() 372 { 373 public boolean accept( File pathname ) 374 { 375 // leave out files which started by '~$' these are Microsoft Office temp files 376 if (pathname.getName().startsWith("~$")) 377 { 378 return false; 379 } 380 381 if (pathname.getName().endsWith(".prn")) 382 { 383 return false; 384 } 385 // This type of document no one would like to load. 386 if (pathname.getName().endsWith(".zip")) 387 { 388 return false; 389 } 390 // just a hack 391 if (pathname.getName().endsWith("_")) 392 { 393 return false; 394 } 395 return true; 396 } 397 }; 398 return aFileFilter; 399 } 400 } 401 402