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 graphical; 25 26 import java.io.File; 27 import java.io.FileFilter; 28 import java.io.FileInputStream; 29 import java.io.FileOutputStream; 30 import java.io.InputStream; 31 import java.io.OutputStream; 32 import java.util.StringTokenizer; 33 import helper.OSHelper; 34 35 import java.io.PrintStream; 36 import javax.swing.JOptionPane; 37 38 public class FileHelper 39 { FileHelper()40 public FileHelper() 41 { 42 // fs = System.getProperty("file.separator"); 43 44 45 String sOSName = System.getProperty("os.name"); 46 String sOSArch = System.getProperty("os.arch"); 47 String sOSVersion = System.getProperty("os.version"); 48 49 GlobalLogWriter.println(sOSName); 50 GlobalLogWriter.println(sOSArch); 51 GlobalLogWriter.println(sOSVersion); 52 53 } 54 MessageBox(String _sStr)55 public static void MessageBox(String _sStr) 56 { 57 String sVersion = System.getProperty("java.version"); 58 String sOSName = System.getProperty("os.name"); 59 JOptionPane.showMessageDialog( null, _sStr, sVersion + " " + sOSName + " Hello World Debugger", JOptionPane.INFORMATION_MESSAGE ); 60 } 61 exists(String _sFile)62 public static boolean exists(String _sFile) 63 { 64 if (_sFile == null) 65 { 66 return false; 67 } 68 69 File aFile = new File(_sFile); 70 if (aFile.exists()) 71 { 72 return true; 73 } 74 // This is just nice for DEBUG behaviour 75 // due to the fact this is absolutly context dependency no one should use it. 76 // else 77 // { 78 // System.out.println("FileHelper:exists() tell this path doesn't exists. Check it. path is:" ); 79 // System.out.println( _sFile ); 80 // System.out.println( aFile.getAbsolutePath() ); 81 // MessageBox("Der JavaProzess wartet auf eine interaktion ihrerseits."); 82 // 83 // File aFile2 = new File(_sFile); 84 // if (aFile2.exists()) 85 // { 86 // System.out.println("Thanks, file exists." ); 87 // return true; 88 // } 89 // } 90 return false; 91 } 92 isDir(String _sDir)93 public static boolean isDir(String _sDir) 94 { 95 if (_sDir == null) 96 { 97 return false; 98 } 99 try 100 { 101 File aFile = new File(_sDir); 102 if (aFile.exists() && aFile.isDirectory()) 103 { 104 return true; 105 } 106 } 107 catch (NullPointerException e) 108 { 109 GlobalLogWriter.println("Exception caught. FileHelper.isDir('" + _sDir + "')"); 110 e.printStackTrace(); 111 } 112 return false; 113 } 114 getBasename(String _sFilename)115 public static String getBasename(String _sFilename) 116 { 117 if (_sFilename == null) 118 { 119 return ""; 120 } 121 // String fs = System.getProperty("file.separator"); 122 123 int nIdx = _sFilename.lastIndexOf("\\"); 124 if (nIdx == -1) 125 { 126 nIdx = _sFilename.lastIndexOf("/"); 127 } 128 if (nIdx > 0) 129 { 130 return _sFilename.substring(nIdx + 1); 131 } 132 return _sFilename; 133 } 134 getNameNoSuffix(String _sFilename)135 public static String getNameNoSuffix(String _sFilename) 136 { 137 if (_sFilename == null) 138 { 139 return ""; 140 } 141 int nIdx = _sFilename.lastIndexOf("."); 142 if (nIdx > 0) 143 { 144 return _sFilename.substring(0, nIdx); 145 } 146 return _sFilename; 147 } 148 getSuffix(String _sFilename)149 public static String getSuffix(String _sFilename) 150 { 151 if (_sFilename == null) 152 { 153 return ""; 154 } 155 int nIdx = _sFilename.lastIndexOf("."); 156 if (nIdx > 0) 157 { 158 return _sFilename.substring(nIdx ); 159 } 160 return ""; 161 } 162 getPath(String _sFilename)163 public static String getPath(String _sFilename) 164 { 165 if (_sFilename == null) 166 { 167 return ""; 168 } 169 // String fs = System.getProperty("file.separator"); 170 171 int nIdx = _sFilename.lastIndexOf("\\"); 172 if (nIdx == -1) 173 { 174 nIdx = _sFilename.lastIndexOf("/"); 175 } 176 if (nIdx > 0) 177 { 178 return _sFilename.substring(0, nIdx); 179 } 180 return ""; 181 } 182 183 /* 184 static ArrayList files = new ArrayList(); 185 public static Object[] traverse( String afileDirectory ) 186 { 187 188 File fileDirectory = new File(afileDirectory); 189 // Testing, if the file is a directory, and if so, it throws an exception 190 if ( !fileDirectory.isDirectory() ) 191 { 192 throw new IllegalArgumentException( "not a directory: " + fileDirectory.getName() ); 193 } 194 195 // Getting all files and directories in the current directory 196 File[] entries = fileDirectory.listFiles(); 197 198 // Iterating for each file and directory 199 for ( int i = 0; i < entries.length; ++i ) 200 { 201 // adding file to List 202 try 203 { 204 // Composing the URL by replacing all backslashs 205 String stringUrl = "file:///" 206 + entries[ i ].getAbsolutePath().replace( '\\', '/' ); 207 files.add(stringUrl); 208 } 209 catch( Exception exception ) 210 { 211 exception.printStackTrace(); 212 } 213 } 214 return files.toArray(); 215 } 216 */ 217 218 // makeDirectories("", "/tmp/a/b"); 219 // creates all directories /tmp/a/b 220 // makeDirectories(String first, String path)221 public static void makeDirectories(String first, String path) 222 { 223 makeDirectories(first, path, "0777"); 224 } 225 makeDirectories(String first, String path, String _sMode)226 public static void makeDirectories(String first, String path, String _sMode) 227 { 228 String fs = System.getProperty("file.separator"); 229 if (path.startsWith(fs + fs)) // starts with UNC Path 230 { 231 int n = path.indexOf(fs, 2); 232 n = path.indexOf(fs, n + 1); 233 first = path.substring(0, n); 234 path = path.substring(n + 1); 235 } 236 237 String already_done = null; 238 StringTokenizer path_tokenizer = new StringTokenizer(path,fs,false); 239 already_done = first; 240 while (path_tokenizer.hasMoreTokens()) 241 { 242 String part = path_tokenizer.nextToken(); 243 File new_dir = new File(already_done + File.separatorChar + part); 244 already_done = new_dir.toString(); 245 // System.out.println(already_done); 246 //create the directory 247 new_dir.mkdirs(); 248 if (OSHelper.isUnix() && 249 _sMode.length() > 0) 250 { 251 try 252 { 253 chmod(new_dir, _sMode); 254 } 255 catch (java.io.IOException e) 256 { 257 GlobalLogWriter.println("Exception caught. FileHelper.makeDirectories('" + new_dir.getAbsolutePath() + "')"); 258 } 259 } 260 } 261 // return; 262 } 263 chmod(File file, String mode)264 public static void chmod(File file, String mode) throws java.io.IOException 265 { 266 Runtime.getRuntime().exec 267 (new String[] 268 {"chmod", mode, file.getAbsolutePath()}); 269 } 270 removeFirstDirectorysAndBasenameFrom(String _sName, String _sRemovePath)271 public static String removeFirstDirectorysAndBasenameFrom(String _sName, String _sRemovePath) 272 { 273 // pre: _sName: /a/b/c/d/e/f.g _sRemovePath /a/b/c 274 // result: d/e 275 String fs = System.getProperty("file.separator"); 276 277 String sBasename = FileHelper.getBasename(_sName); 278 String sSubDirs = ""; 279 if (_sName.startsWith(_sRemovePath)) 280 { 281 // if _sName starts with _sRemovePath 282 int nRemovePathIndex = _sRemovePath.length(); 283 if (! _sRemovePath.endsWith(fs)) 284 { 285 // add 1 if we not ends with file separator 286 nRemovePathIndex ++; 287 } 288 int nBasenameIndex = _sName.length() - sBasename.length() - 1; 289 if (nRemovePathIndex < nBasenameIndex) 290 { 291 sSubDirs = _sName.substring(nRemovePathIndex, nBasenameIndex); 292 } 293 } 294 else 295 { 296 // special case, the _sRemovePath is not part of _sName 297 sSubDirs = FileHelper.getPath(_sName); 298 if (sSubDirs.startsWith(fs)) 299 { 300 // remove leading file separator 301 sSubDirs = sSubDirs.substring(1); 302 } 303 } 304 305 return sSubDirs; 306 } 307 test_removeFirstDirectorysAndBasenameFrom()308 public static void test_removeFirstDirectorysAndBasenameFrom() 309 { 310 String a = removeFirstDirectorysAndBasenameFrom("/a/b/c/d/e/f.g", "/a/b/c"); 311 // assure("", a.equals("d/e")); 312 String b = removeFirstDirectorysAndBasenameFrom("/a/b/c/d/e/f.g", "/a/b/c/"); 313 // assure("", b.equals("d/e")); 314 String c = removeFirstDirectorysAndBasenameFrom("/a/b/c/d/e/f.g", "/b/c"); 315 // assure("", c.equals("a/b/c/d/e")); 316 } 317 318 getSystemPathFromFileURL( String _sFileURL )319 public static String getSystemPathFromFileURL( String _sFileURL ) 320 { 321 String sSystemFile = null; 322 323 if(_sFileURL.startsWith("file:///")) 324 { 325 if (OSHelper.isWindows()) 326 { 327 sSystemFile = _sFileURL.substring(8); 328 } 329 else 330 { 331 sSystemFile = _sFileURL.substring(7); 332 } 333 } 334 else if (_sFileURL.startsWith("file://")) 335 { 336 sSystemFile = _sFileURL.substring(5); 337 } 338 String fs = System.getProperty("file.separator"); 339 if (! fs.equals("/")) 340 { 341 sSystemFile = sSystemFile.replace ('/', fs.toCharArray ()[0]); 342 } 343 // FEATURE FOR UNC NEED!!! 344 return sSystemFile; 345 } 346 347 private static boolean m_bDebugTextShown = false; isDebugEnabled()348 public static boolean isDebugEnabled() 349 { 350 boolean bDebug = false; 351 String sTmpPath = util.utils.getUsersTempDir(); 352 //util.utils.getUsersTempDir(); 353 String fs = System.getProperty("file.separator"); 354 String sName = sTmpPath + fs + "DOC_COMPARATOR_DEBUG"; 355 File aFile = new File(sName); 356 if (aFile.exists()) 357 { 358 if (m_bDebugTextShown == false) 359 { 360 GlobalLogWriter.println("Found file: " + sName); 361 GlobalLogWriter.println("Activate debug mode."); 362 GlobalLogWriter.println("If debug mode is no longer necessary, remove the above file."); 363 m_bDebugTextShown = true; 364 } 365 bDebug = true; 366 } 367 return bDebug; 368 } 369 copyStream(InputStream _aIn, OutputStream _aOut)370 private static void copyStream(InputStream _aIn, OutputStream _aOut) throws java.io.IOException 371 { 372 byte[] aBuffer = new byte[0xFFFF]; 373 for (int len; (len = _aIn.read(aBuffer)) != -1; ) 374 { 375 _aOut.write(aBuffer, 0, len); 376 } 377 } 378 copy(String _sSource, String _sDestination)379 public static void copy(String _sSource, String _sDestination) 380 { 381 FileInputStream aFIS = null; 382 FileOutputStream aFOS = null; 383 384 try 385 { 386 aFIS = new FileInputStream(_sSource); 387 aFOS = new FileOutputStream(_sDestination); 388 copyStream(aFIS, aFOS); 389 } 390 catch (java.io.IOException e) 391 { 392 System.out.println("Error: caught Exception: " + e.getMessage()); 393 } 394 finally 395 { 396 if (aFIS != null) 397 { 398 try 399 { 400 aFIS.close(); 401 } 402 catch (java.io.IOException e) 403 { 404 System.out.println("Error: caught Exception: " + e.getMessage()); 405 } 406 } 407 if (aFOS != null) 408 { 409 try 410 { 411 aFOS.close(); 412 } 413 catch (java.io.IOException e) 414 { 415 System.out.println("Error: caught Exception: " + e.getMessage()); 416 } 417 } 418 } 419 420 // try 421 // { 422 // File inputFile = new File(_sSource); 423 // File outputFile = new File(_sDestination); 424 // 425 // java.io.FileReader in = new java.io.FileReader(inputFile); 426 // java.io.FileWriter out = new java.io.FileWriter(outputFile); 427 // int c; 428 // 429 // while ((c = in.read()) != -1) 430 // { 431 // out.write(c); 432 // } 433 // 434 // in.close(); 435 // out.close(); 436 // } 437 // catch (java.io.IOException e) 438 // { 439 // GlobalLogWriter.get().println("Exception caught. FileHelper.copy('" + _sSource + ", " + _sDestination + "')"); 440 // GlobalLogWriter.get().println("Message: " + e.getMessage()); 441 // } 442 } 443 444 445 /** 446 * Within the directory run through, it's possible to say which file extension types should not 447 * consider like '*.prn' because it's not a document. 448 * 449 * @return a FileFilter function 450 */ getFileFilter()451 public static FileFilter getFileFilter() 452 { 453 FileFilter aFileFilter = new FileFilter() 454 { 455 public boolean accept( File pathname ) 456 { 457 // leave out files which started by '~$' these are Microsoft Office temp files 458 if (pathname.getName().startsWith("~$")) 459 { 460 return false; 461 } 462 // leave out files starts with '.~lock.' these are OpenOffice.org lock files 463 if (pathname.getName().startsWith(".~lock.")) 464 { 465 return false; 466 } 467 // leave out files ends with '#' these could be temp files 468 if (pathname.getName().endsWith("#")) 469 { 470 return false; 471 } 472 if (pathname.getName().endsWith(".prn")) 473 { 474 return false; 475 } 476 if (pathname.getName().endsWith(".ps")) 477 { 478 return false; 479 } 480 // This type of document no one would like to load. 481 if (pathname.getName().endsWith(".zip")) 482 { 483 return false; 484 } 485 // just a hack 486 if (pathname.getName().endsWith("_")) 487 { 488 return false; 489 } 490 return true; 491 } 492 }; 493 return aFileFilter; 494 } 495 /** 496 * Within the directory run through, it's possible to say which file extension types should not 497 * consider like '*.prn' because it's not a document. 498 * 499 * @return a FileFilter function 500 */ getFileFilterPSorPDF()501 public static FileFilter getFileFilterPSorPDF() 502 { 503 FileFilter aFileFilter = new FileFilter() 504 { 505 public boolean accept( File pathname ) 506 { 507 if (pathname.getName().endsWith(".ps")) 508 { 509 return true; 510 } 511 if (pathname.getName().endsWith(".pdf")) 512 { 513 return true; 514 } 515 return false; 516 } 517 }; 518 return aFileFilter; 519 } 520 /** 521 * Within the directory run through, it's possible to say which file extension types should not 522 * consider like '*.prn' because it's not a document. 523 * 524 * @return a FileFilter function 525 */ getFileFilterJPEG()526 public static FileFilter getFileFilterJPEG() 527 { 528 FileFilter aFileFilter = new FileFilter() 529 { 530 public boolean accept( File pathname ) 531 { 532 if (pathname.getName().toLowerCase().endsWith(".jpg")) 533 { 534 return true; 535 } 536 if (pathname.getName().toLowerCase().endsWith(".jpeg")) 537 { 538 return true; 539 } 540 return false; 541 } 542 }; 543 return aFileFilter; 544 } 545 /** 546 * Within the directory run through, it's possible to say which file extension types should not 547 * consider like '*.ini' because it's not a document. 548 * 549 * @return a FileFilter function 550 */ getFileFilterINI()551 public static FileFilter getFileFilterINI() 552 { 553 FileFilter aFileFilter = new FileFilter() 554 { 555 public boolean accept( File pathname ) 556 { 557 String sPathname = pathname.getName().toLowerCase(); 558 if (sPathname.endsWith("index.ini")) 559 { 560 // don't consider the index.ini file 561 return false; 562 } 563 if (sPathname.endsWith(".ini")) 564 { 565 return true; 566 } 567 return false; 568 } 569 }; 570 return aFileFilter; 571 } 572 appendPath(String _sPath, String _sRelativePathToAdd)573 public static String appendPath(String _sPath, String _sRelativePathToAdd) 574 { 575 String sNewPath = _sPath; 576 String fs = System.getProperty("file.separator"); 577 if (_sPath.startsWith("file:")) 578 { 579 fs = "/"; // we use a file URL so only '/' is allowed. 580 } 581 if (! (sNewPath.endsWith("/") || sNewPath.endsWith("\\") ) ) 582 { 583 sNewPath += fs; 584 } 585 sNewPath += _sRelativePathToAdd; 586 return sNewPath; 587 } 588 589 // ----------------------------------------------------------------------------- createInfoFile(String _sFile, ParameterHelper _aGTA)590 public static void createInfoFile(String _sFile, ParameterHelper _aGTA) 591 { 592 createInfoFile(_sFile, _aGTA, ""); 593 } 594 createInfoFile(String _sFile, ParameterHelper _aGTA, String _sSpecial)595 public static void createInfoFile(String _sFile, ParameterHelper _aGTA, String _sSpecial) 596 { 597 String sFilename; 598 if (_sFile.startsWith("file://")) 599 { 600 sFilename = FileHelper.getSystemPathFromFileURL(_sFile); 601 GlobalLogWriter.println("CreateInfoFile: '" + sFilename + "'" ); 602 } 603 else 604 { 605 sFilename = _sFile; 606 } 607 String sFileDir = FileHelper.getPath(sFilename); 608 String sBasename = FileHelper.getBasename(sFilename); 609 String sNameNoSuffix = FileHelper.getNameNoSuffix(sBasename); 610 611 String sIniFile = FileHelper.appendPath(sFileDir, sBasename + ".ini"); 612 IniFile aIniFile = new IniFile(sIniFile); 613 614 // OLD INFO FILE 615 616 // String fs = System.getProperty("file.separator"); 617 String ls = System.getProperty("line.separator"); 618 String sInfoFilename = FileHelper.appendPath(sFileDir, sNameNoSuffix + ".info"); 619 File aInfoFile = new File(sInfoFilename); 620 621 String sBuildID = ""; 622 623 try 624 { 625 FileOutputStream out2 = new FileOutputStream(aInfoFile.toString()); 626 PrintStream out = new PrintStream(out2); 627 628 out.println("# automatically created file by graphical compare"); 629 if (_aGTA != null) 630 { 631 if (_sSpecial != null && _sSpecial.equals("msoffice")) 632 { 633 out.println("# buildid from wordloadfile"); 634 sBuildID = _aGTA.getPerformance().getMSOfficeVersion(); 635 out.println("buildid=" + sBuildID); 636 } 637 else 638 { 639 out.println("# buildid is read out of the bootstrap file"); 640 sBuildID = _aGTA.getBuildID(); 641 out.println("buildid=" + sBuildID); 642 } 643 aIniFile.insertValue("global", "buildid", sBuildID); 644 645 // if (_sSpecial != null && _sSpecial.length() > 0) 646 // { 647 // out.write("special=" + _sSpecial + ls); 648 // } 649 out.println(); 650 out.println("# resolution given in DPI"); 651 out.println("resolution=" + _aGTA.getResolutionInDPI()); 652 aIniFile.insertValue("global", "resolution", _aGTA.getResolutionInDPI()); 653 } 654 else 655 { 656 out.println("buildid=" + _sSpecial); 657 aIniFile.insertValue("global", "buildid", _sSpecial); 658 } 659 660 // long nTime = stopTimer(); 661 // if (nTime != 0) 662 // { 663 // out.write("# time is given in milli seconds" + ls); 664 // out.write("time=" + nTime + ls); 665 // } 666 667 out.println(); 668 out.println("# Values out of System.getProperty(...)"); 669 out.println("os.name=" + System.getProperty("os.name")); 670 out.println("os.arch=" + System.getProperty("os.arch")); 671 out.println("os.version=" + System.getProperty("os.version")); 672 673 aIniFile.insertValue("global", "os.name", System.getProperty("os.name")); 674 aIniFile.insertValue("global", "os.arch", System.getProperty("os.arch")); 675 aIniFile.insertValue("global", "os.version", System.getProperty("os.version")); 676 677 if (_aGTA != null) 678 { 679 out.println(); 680 out.println("# Performance output, values are given in milli sec."); 681 _aGTA.getPerformance().print(out); 682 _aGTA.getPerformance().print(aIniFile, "global"); 683 } 684 685 out.flush(); 686 out.close(); 687 out2.close(); 688 } 689 catch (java.io.IOException e) 690 { 691 GlobalLogWriter.println("can't create Info file."); 692 e.printStackTrace(); 693 } 694 aIniFile.close(); 695 696 // String sExtension = FileHelper.getSuffix(_aGTA.getInputFile()); 697 // if (sExtension.startsWith(".")) 698 // { 699 // sExtension = sExtension.substring(1); 700 // } 701 // 702 // DB.writeToDB(_aGTA.getInputFile(), 703 // sNameNoSuffix, 704 // sExtension, 705 // sBuildID, 706 // _aGTA.getReferenceType(), 707 // _aGTA.getResolutionInDPI() 708 // ); 709 } 710 addBasenameToFile(String _sIndexFilename, String _sBasename, String _sCreator, String _sType, String _sSource)711 public static void addBasenameToFile(String _sIndexFilename, String _sBasename, String _sCreator, String _sType, String _sSource) 712 { 713 // String sOutputDir = FileHelper.getPath(_sOutputFilename); 714 String sPath; 715 if (_sIndexFilename.startsWith("file:")) 716 { 717 sPath = FileHelper.getSystemPathFromFileURL(_sIndexFilename); 718 } 719 else 720 { 721 sPath = _sIndexFilename; 722 } 723 String sIndexFilename = sPath; // FileHelper.appendPath(sPath, _sFilename); 724 IniFile aIniFile = new IniFile(sIndexFilename); 725 aIniFile.insertValue(_sBasename, "creator", _sCreator); 726 aIniFile.insertValue(_sBasename, "type", _sType); 727 aIniFile.insertValue(_sBasename, "source", _sSource); 728 aIniFile.close(); 729 // File aFile = new File(sIndexFilename); 730 // try 731 // { 732 // RandomAccessFile aRandomAccess = new RandomAccessFile(aFile, "rw"); 733 // // String sBasename = FileHelper.getBasename(_sOutputFilename); 734 // aRandomAccess.seek(aRandomAccess.length()); // jump to the end. 735 //// TODO: seems to be wrong, there exist no writeLine() with 'return' ending? 736 // aRandomAccess.writeUTF(_sBasename); 737 // aRandomAccess.close(); 738 // } 739 // catch (java.io.FileNotFoundException e) 740 // { 741 // } 742 // catch (java.io.IOException e) 743 // { 744 // } 745 } 746 addBasenameToPostscript(String _sOutputFilename)747 public static void addBasenameToPostscript(String _sOutputFilename) 748 { 749 String sIndexFilename = FileHelper.appendPath(_sOutputFilename, "postscript.ini"); 750 // String sPath = FileHelper.getPath(sIndexFilename); 751 String sBasename = FileHelper.getBasename(_sOutputFilename); 752 addBasenameToFile(sIndexFilename, sBasename, "", "", ""); 753 } addBasenameToIndex(String _sOutputFilename, String _sBasename, String _sCreator, String _sType, String _sSource)754 public static void addBasenameToIndex(String _sOutputFilename, String _sBasename, String _sCreator, String _sType, String _sSource) 755 { 756 String sIndexFilename = FileHelper.appendPath(_sOutputFilename, "index.ini"); 757 // String sPath = FileHelper.getPath(sIndexFilename); 758 // String sBasename = FileHelper.getBasename(_sOutputFilename); 759 addBasenameToFile(sIndexFilename, _sBasename, _sCreator, _sType, _sSource); 760 } 761 762 } 763 764