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