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