1 24 25 package org.gjt.sp.jedit; 26 27 import javax.swing.text.Segment ; 29 import javax.swing.JMenuItem ; 30 import java.io.*; 31 import java.net.MalformedURLException ; 32 import java.net.URL ; 33 import java.nio.charset.Charset ; 34 import java.text.DecimalFormat ; 35 import java.util.*; 36 import java.util.regex.Matcher ; 37 import java.util.regex.Pattern ; 38 import java.util.zip.GZIPInputStream ; 39 40 import org.xml.sax.InputSource ; 41 import org.xml.sax.helpers.DefaultHandler ; 42 43 import org.gjt.sp.jedit.io.*; 44 import org.gjt.sp.util.Log; 45 import org.gjt.sp.util.ProgressObserver; 46 import org.gjt.sp.util.StandardUtilities; 47 import org.gjt.sp.util.IOUtilities; 48 import org.gjt.sp.util.XMLUtilities; 49 import org.gjt.sp.jedit.menu.EnhancedMenuItem; 50 import org.gjt.sp.jedit.bufferio.BufferIORequest; 51 import org.gjt.sp.jedit.buffer.JEditBuffer; 52 54 89 public class MiscUtilities 90 { 91 95 public static final String UTF_8_Y = "UTF-8Y"; 96 97 99 107 public static String canonPath(String path) 108 { 109 if(path.length() == 0) 110 return path; 111 112 if(path.startsWith("file://")) 113 path = path.substring("file://".length()); 114 else if(path.startsWith("file:")) 115 path = path.substring("file:".length()); 116 else if(isURL(path)) 117 return path; 118 119 if(File.separatorChar == '\\') 120 { 121 path = path.replace('/','\\'); 123 int trim = path.length(); 125 while(path.charAt(trim - 1) == ' ') 126 trim--; 127 128 if (path.charAt(trim - 1) == '\\') 129 while (trim > 1 && path.charAt(trim - 2) == '\\') 130 { 131 trim--; 132 } 133 path = path.substring(0,trim); 134 } 135 else if(OperatingSystem.isMacOS()) 136 { 137 path = path.replace(':','/'); 139 } 140 141 if(path.startsWith('~' + File.separator)) 142 { 143 path = path.substring(2); 144 String home = System.getProperty("user.home"); 145 146 if(home.endsWith(File.separator)) 147 return home + path; 148 else 149 return home + File.separator + path; 150 } 151 else if(path.equals("~")) 152 return System.getProperty("user.home"); 153 else 154 return path; 155 } 157 static final String varPatternString = "(\\$([a-zA-Z0-9_]+))"; 159 static final String varPatternString2 = "(\\$\\{([^}]+)\\})"; 160 static final Pattern varPattern = Pattern.compile(varPatternString); 161 static final Pattern varPattern2 = Pattern.compile(varPatternString2); 162 163 173 public static String expandVariables(String arg) 174 { 175 Pattern p = varPattern; 176 Matcher m = p.matcher(arg); 177 if (!m.find()) 178 { 179 p = varPattern2; 180 m = p.matcher(arg); 181 if (!m.find()) return arg; 183 } 184 String varName = m.group(2); 185 String expansion = System.getenv(varName); 186 if (expansion == null) 187 { varName = varName.toUpperCase(); 189 String uparg = arg.toUpperCase(); 190 m = p.matcher(uparg); 191 expansion = System.getenv(varName); 192 } 193 if (expansion != null) 194 { 195 expansion = expansion.replace("\\", "\\\\"); 196 return m.replaceFirst(expansion); 197 } 198 return arg; 199 } 201 208 public static String resolveSymlinks(String path) 209 { 210 if(isURL(path)) 211 return path; 212 213 if(OperatingSystem.isOS2()) 215 return path; 216 if(OperatingSystem.isDOSDerived()) 219 { 220 if(path.length() == 2 || path.length() == 3) 221 { 222 if(path.charAt(1) == ':') 223 return path; 224 } 225 } 226 try 227 { 228 return new File(path).getCanonicalPath(); 229 } 230 catch(IOException io) 231 { 232 return path; 233 } 234 } 236 241 public static boolean isAbsolutePath(String path) 242 { 243 if(isURL(path)) 244 return true; 245 else if(path.startsWith("~/") || path.startsWith("~" + File.separator) || path.equals("~")) 246 return true; 247 else if(OperatingSystem.isDOSDerived()) 248 { 249 if(path.length() == 2 && path.charAt(1) == ':') 250 return true; 251 if(path.length() > 2 && path.charAt(1) == ':' 252 && (path.charAt(2) == '\\' 253 || path.charAt(2) == '/')) 254 return true; 255 if(path.startsWith("\\\\") 256 || path.startsWith("//")) 257 return true; 258 } 259 else if(OperatingSystem.isUnix() 261 || OperatingSystem.isVMS()) 262 { 263 if(path.length() > 0 && path.charAt(0) == '/') 265 return true; 266 } 267 268 return false; 269 } 271 278 public static String constructPath(String parent, String path) 279 { 280 if(isAbsolutePath(path)) 281 return canonPath(path); 282 283 if(OperatingSystem.isDOSDerived()) 286 { 287 if(path.length() == 2 && path.charAt(1) == ':') 288 return path; 289 else if(path.length() > 2 && path.charAt(1) == ':' 290 && path.charAt(2) != '\\') 291 { 292 path = path.substring(0,2) + '\\' 293 + path.substring(2); 294 return canonPath(path); 295 } 296 } 297 298 String dd = ".." + File.separator; 299 String d = '.' + File.separator; 300 301 if(parent == null) 302 parent = System.getProperty("user.dir"); 303 304 for(;;) 305 { 306 if(path.equals(".")) 307 return parent; 308 else if(path.equals("..")) 309 return getParentOfPath(parent); 310 else if(path.startsWith(dd) || path.startsWith("../")) 311 { 312 parent = getParentOfPath(parent); 313 path = path.substring(3); 314 } 315 else if(path.startsWith(d) || path.startsWith("./")) 316 path = path.substring(2); 317 else 318 break; 319 } 320 321 if(OperatingSystem.isDOSDerived() 322 && !isURL(parent) 323 && path.charAt(0) == '\\') 324 parent = parent.substring(0,2); 325 326 VFS vfs = VFSManager.getVFSForPath(parent); 327 328 return canonPath(vfs.constructPath(parent,path)); 329 } 331 339 public static String constructPath(String parent, 340 String path1, String path2) 341 { 342 return constructPath(constructPath(parent,path1),path2); 343 } 345 354 public static String concatPath(String parent, String path) 355 { 356 parent = canonPath(parent); 357 path = canonPath(path); 358 359 if (path.startsWith(File.separator)) 361 path = path.substring(1); 362 else if ((path.length() >= 3) && (path.charAt(1) == ':')) 363 path = path.replace(':', File.separatorChar); 364 365 if (parent == null) 366 parent = System.getProperty("user.dir"); 367 368 if (parent.endsWith(File.separator)) 369 return parent + path; 370 else 371 return parent + File.separator + path; 372 } 374 381 public static int getFirstSeparatorIndex(String path) 382 { 383 int start = getPathStart(path); 384 int index = path.indexOf('/',start); 385 if(index == -1) 386 index = path.indexOf(File.separatorChar,start); 387 return index; 388 } 390 397 public static int getLastSeparatorIndex(String path) 398 { 399 int start = getPathStart(path); 400 if(start != 0) 401 path = path.substring(start); 402 int index = Math.max(path.lastIndexOf('/'), 403 path.lastIndexOf(File.separatorChar)); 404 if(index == -1) 405 return index; 406 else 407 return index + start; 408 } 410 416 public static String getFileExtension(String path) 417 { 418 int fsIndex = getLastSeparatorIndex(path); 419 int index = path.indexOf('.',fsIndex); 420 if(index == -1) 421 return ""; 422 else 423 return path.substring(index); 424 } 426 432 public static String getFileName(String path) 433 { 434 return VFSManager.getVFSForPath(path).getFileName(path); 435 } 437 444 public static String getFileNameNoExtension(String path) 445 { 446 String name = getFileName(path); 447 int index = name.indexOf('.'); 448 if(index == -1) 449 return name; 450 else 451 return name.substring(0,index); 452 } 454 458 @Deprecated 459 public static String getFileParent(String path) 460 { 461 return getParentOfPath(path); 462 } 464 470 public static String getParentOfPath(String path) 471 { 472 return VFSManager.getVFSForPath(path).getParentOfPath(path); 473 } 475 479 @Deprecated 480 public static String getFileProtocol(String url) 481 { 482 return getProtocolOfURL(url); 483 } 485 491 public static String getProtocolOfURL(String url) 492 { 493 return url.substring(0,url.indexOf(':')); 494 } 496 502 public static boolean isURL(String str) 503 { 504 int fsIndex = getLastSeparatorIndex(str); 505 if(fsIndex == 0) return false; 507 else if(fsIndex == 2) return false; 509 510 int cIndex = str.indexOf(':'); 511 if(cIndex <= 1) return false; 513 514 String protocol = str.substring(0,cIndex); 515 VFS vfs = VFSManager.getVFSForProtocol(protocol); 516 if(vfs != null && !(vfs instanceof UrlVFS)) 517 return true; 518 519 try 520 { 521 new URL (str); 522 return true; 523 } 524 catch(MalformedURLException mf) 525 { 526 return false; 527 } 528 } 530 542 public static void saveBackup(File file, int backups, 543 String backupPrefix, String backupSuffix, 544 String backupDirectory) 545 { 546 saveBackup(file,backups,backupPrefix,backupSuffix,backupDirectory,0); 547 } 549 564 public static void saveBackup(File file, int backups, 565 String backupPrefix, String backupSuffix, 566 String backupDirectory, int backupTimeDistance) 567 { 568 if(backupPrefix == null) 569 backupPrefix = ""; 570 if(backupSuffix == null) 571 backupSuffix = ""; 572 573 String name = file.getName(); 574 575 if(backups == 1) 577 { 578 File backupFile = new File(backupDirectory, 579 backupPrefix + name + backupSuffix); 580 long modTime = backupFile.lastModified(); 581 584 if(System.currentTimeMillis() - modTime 585 >= backupTimeDistance) 586 { 587 backupFile.delete(); 588 if (!file.renameTo(backupFile)) 589 IOUtilities.moveFile(file, backupFile); 590 } 591 } 592 else 594 { 595 596 new File(backupDirectory, 597 backupPrefix + name + backupSuffix 598 + backups + backupSuffix).delete(); 599 600 File firstBackup = new File(backupDirectory, 601 backupPrefix + name + backupSuffix 602 + "1" + backupSuffix); 603 long modTime = firstBackup.lastModified(); 604 607 if(System.currentTimeMillis() - modTime 608 >= backupTimeDistance) 609 { 610 for(int i = backups - 1; i > 0; i--) 611 { 612 File backup = new File(backupDirectory, 613 backupPrefix + name 614 + backupSuffix + i 615 + backupSuffix); 616 617 backup.renameTo( 618 new File(backupDirectory, 619 backupPrefix + name 620 + backupSuffix + (i+1) 621 + backupSuffix)); 622 } 623 624 File backupFile = new File(backupDirectory, 625 backupPrefix + name + backupSuffix 626 + "1" + backupSuffix); 627 if (!file.renameTo(backupFile)) 628 IOUtilities.moveFile(file, backupFile); 629 } 630 } 631 } 633 649 @Deprecated 650 public static boolean moveFile(File source, File dest) 651 { 652 return IOUtilities.moveFile(source, dest); 653 } 655 669 @Deprecated 670 public static boolean copyStream(int bufferSize, ProgressObserver progress, 671 InputStream in, OutputStream out, boolean canStop) 672 throws IOException 673 { 674 return IOUtilities.copyStream(bufferSize, progress, in, out, canStop); 675 } 677 690 @Deprecated 691 public static boolean copyStream(ProgressObserver progress, InputStream in, OutputStream out, boolean canStop) 692 throws IOException 693 { 694 return IOUtilities.copyStream(4096,progress, in, out, canStop); 695 } 697 712 public static boolean isBinary(Reader reader) 713 throws IOException 714 { 715 int nbChars = jEdit.getIntegerProperty("vfs.binaryCheck.length",100); 716 int authorized = jEdit.getIntegerProperty("vfs.binaryCheck.count",1); 717 for (long i = 0L;i < nbChars;i++) 718 { 719 int c = reader.read(); 720 if (c == -1) 721 return false; 722 if (c == 0) 723 { 724 authorized--; 725 if (authorized == 0) 726 return true; 727 } 728 } 729 return false; 730 } 732 739 public static boolean isBackup( String filename ) { 740 if (filename.startsWith("#")) return true; 741 if (filename.endsWith("~")) return true; 742 if (filename.endsWith(".bak")) return true; 743 return false; 744 } 746 747 758 public static Reader autodetect(InputStream in, Buffer buffer) throws IOException 759 { 760 in = new BufferedInputStream(in); 761 762 String encoding; 763 if (buffer == null) 764 encoding = System.getProperty("file.encoding"); 765 else 766 encoding = buffer.getStringProperty(JEditBuffer.ENCODING); 767 768 if(!in.markSupported()) 769 Log.log(Log.WARNING,MiscUtilities.class,"Mark not supported: " + in); 770 else if(buffer == null || buffer.getBooleanProperty(Buffer.ENCODING_AUTODETECT)) 771 { 772 in.mark(BufferIORequest.XML_PI_LENGTH); 773 int b1 = in.read(); 774 int b2 = in.read(); 775 int b3 = in.read(); 776 777 if(b1 == BufferIORequest.GZIP_MAGIC_1 && b2 == BufferIORequest.GZIP_MAGIC_2) 778 { 779 in.reset(); 780 in = new GZIPInputStream (in); 781 if (buffer != null) 782 buffer.setBooleanProperty(Buffer.GZIPPED,true); 783 return autodetect(in, buffer); 785 } 786 else if (b1 == BufferIORequest.UNICODE_MAGIC_1 787 && b2 == BufferIORequest.UNICODE_MAGIC_2) 788 { 789 in.reset(); 790 in.read(); 791 in.read(); 792 encoding = "UTF-16BE"; 793 if (buffer != null) 794 buffer.setProperty(JEditBuffer.ENCODING,encoding); 795 } 796 else if (b1 == BufferIORequest.UNICODE_MAGIC_2 797 && b2 == BufferIORequest.UNICODE_MAGIC_1) 798 { 799 in.reset(); 800 in.read(); 801 in.read(); 802 encoding = "UTF-16LE"; 803 if (buffer != null) 804 buffer.setProperty(JEditBuffer.ENCODING,encoding); 805 } 806 else if(b1 == BufferIORequest.UTF8_MAGIC_1 && b2 == BufferIORequest.UTF8_MAGIC_2 807 && b3 == BufferIORequest.UTF8_MAGIC_3) 808 { 809 if (buffer != null) 812 buffer.setProperty(JEditBuffer.ENCODING, MiscUtilities.UTF_8_Y); 813 814 encoding = "UTF-8"; 815 } 816 else 817 { 818 in.reset(); 819 820 byte[] _xmlPI = new byte[BufferIORequest.XML_PI_LENGTH]; 821 int offset = 0; 822 int count; 823 while((count = in.read(_xmlPI,offset, 824 BufferIORequest.XML_PI_LENGTH - offset)) != -1) 825 { 826 offset += count; 827 if(offset == BufferIORequest.XML_PI_LENGTH) 828 break; 829 } 830 831 String xmlEncoding = getXMLEncoding(new String ( 832 _xmlPI,0,offset,"ASCII")); 833 if(xmlEncoding != null) 834 { 835 encoding = xmlEncoding; 836 if (buffer != null) 837 buffer.setProperty(JEditBuffer.ENCODING,encoding); 838 } 839 840 if(encoding.equals(MiscUtilities.UTF_8_Y)) 841 encoding = "UTF-8"; 842 843 in.reset(); 844 } 845 } 846 847 return new InputStreamReader(in,encoding); 848 } 850 854 private static String getXMLEncoding(String xmlPI) 855 { 856 if(!xmlPI.startsWith("<?xml")) 857 return null; 858 859 int index = xmlPI.indexOf("encoding="); 860 if(index == -1 || index + 9 == xmlPI.length()) 861 return null; 862 863 char ch = xmlPI.charAt(index + 9); 864 int endIndex = xmlPI.indexOf(ch,index + 10); 865 if(endIndex == -1) 866 return null; 867 868 String encoding = xmlPI.substring(index + 10,endIndex); 869 870 if(Charset.isSupported(encoding)) 871 return encoding; 872 else 873 { 874 Log.log(Log.WARNING,MiscUtilities.class,"XML PI specifies " 875 + "unsupported encoding: " + encoding); 876 return null; 877 } 878 } 880 888 @Deprecated 889 public static void closeQuietly(InputStream in) 890 { 891 IOUtilities.closeQuietly(in); 892 } 894 902 @Deprecated 903 public static void closeQuietly(OutputStream out) 904 { 905 IOUtilities.closeQuietly(out); 906 } 908 914 public static String fileToClass(String name) 915 { 916 char[] clsName = name.toCharArray(); 917 for(int i = clsName.length - 6; i >= 0; i--) 918 if(clsName[i] == '/') 919 clsName[i] = '.'; 920 return new String (clsName,0,clsName.length - 6); 921 } 923 929 public static String classToFile(String name) 930 { 931 return name.replace('.','/').concat(".class"); 932 } 934 942 public static boolean pathsEqual(String p1, String p2) 943 { 944 VFS v1 = VFSManager.getVFSForPath(p1); 945 VFS v2 = VFSManager.getVFSForPath(p2); 946 947 if(v1 != v2) 948 return false; 949 950 if(p1.endsWith("/") || p1.endsWith(File.separator)) 951 p1 = p1.substring(0,p1.length() - 1); 952 953 if(p2.endsWith("/") || p2.endsWith(File.separator)) 954 p2 = p2.substring(0,p2.length() - 1); 955 956 if((v1.getCapabilities() & VFS.CASE_INSENSITIVE_CAP) != 0) 957 return p1.equalsIgnoreCase(p2); 958 else 959 return p1.equals(p2); 960 } 962 964 966 973 @Deprecated 974 public static int getLeadingWhiteSpace(String str) 975 { 976 return StandardUtilities.getLeadingWhiteSpace(str); 977 } 979 987 @Deprecated 988 public static int getTrailingWhiteSpace(String str) 989 { 990 return StandardUtilities.getTrailingWhiteSpace(str); 991 } 993 1001 @Deprecated 1002 public static int getLeadingWhiteSpaceWidth(String str, int tabSize) 1003 { 1004 return StandardUtilities.getLeadingWhiteSpaceWidth(str, tabSize); 1005 } 1007 1017 @Deprecated 1018 public static int getVirtualWidth(Segment seg, int tabSize) 1019 { 1020 return StandardUtilities.getVirtualWidth(seg, tabSize); 1021 } 1023 1040 @Deprecated 1041 public static int getOffsetOfVirtualColumn(Segment seg, int tabSize, 1042 int column, int[] totalVirtualWidth) 1043 { 1044 return StandardUtilities.getOffsetOfVirtualColumn(seg, tabSize, column, totalVirtualWidth); 1045 } 1047 1062 @Deprecated 1063 public static String createWhiteSpace(int len, int tabSize) 1064 { 1065 return StandardUtilities.createWhiteSpace(len,tabSize,0); 1066 } 1068 1085 @Deprecated 1086 public static String createWhiteSpace(int len, int tabSize, int start) 1087 { 1088 return StandardUtilities.createWhiteSpace(len, tabSize, start); 1089 } 1091 1099 @Deprecated 1100 public static String globToRE(String glob) 1101 { 1102 return StandardUtilities.globToRE(glob); 1103 } 1105 1112 public static String escapesToChars(String str) 1113 { 1114 StringBuffer buf = new StringBuffer (); 1115 for(int i = 0; i < str.length(); i++) 1116 { 1117 char c = str.charAt(i); 1118 switch(c) 1119 { 1120 case '\\': 1121 if(i == str.length() - 1) 1122 { 1123 buf.append('\\'); 1124 break; 1125 } 1126 c = str.charAt(++i); 1127 switch(c) 1128 { 1129 case 'n': 1130 buf.append('\n'); 1131 break; 1132 case 't': 1133 buf.append('\t'); 1134 break; 1135 default: 1136 buf.append(c); 1137 break; 1138 } 1139 break; 1140 default: 1141 buf.append(c); 1142 } 1143 } 1144 return buf.toString(); 1145 } 1147 1154 public static String charsToEscapes(String str) 1155 { 1156 return charsToEscapes(str,"\n\t\\\"'"); 1157 } 1159 1166 public static String charsToEscapes(String str, String toEscape) 1167 { 1168 StringBuffer buf = new StringBuffer (); 1169 for(int i = 0; i < str.length(); i++) 1170 { 1171 char c = str.charAt(i); 1172 if(toEscape.indexOf(c) != -1) 1173 { 1174 if(c == '\n') 1175 buf.append("\\n"); 1176 else if(c == '\t') 1177 buf.append("\\t"); 1178 else 1179 { 1180 buf.append('\\'); 1181 buf.append(c); 1182 } 1183 } 1184 else 1185 buf.append(c); 1186 } 1187 return buf.toString(); 1188 } 1190 1194 @Deprecated 1195 public static int compareVersions(String v1, String v2) 1196 { 1197 return StandardUtilities.compareStrings(v1,v2,false); 1198 } 1200 1216 @Deprecated 1217 public static int compareStrings(String str1, String str2, boolean ignoreCase) 1218 { 1219 return StandardUtilities.compareStrings(str1, str2, ignoreCase); 1220 } 1222 1226 @Deprecated 1227 public static boolean stringsEqual(String s1, String s2) 1228 { 1229 return StandardUtilities.objectsEqual(s1,s2); 1230 } 1232 1239 @Deprecated 1240 public static boolean objectsEqual(Object o1, Object o2) 1241 { 1242 return StandardUtilities.objectsEqual(o1, o2); 1243 } 1245 1253 @Deprecated 1254 public static String charsToEntities(String str) 1255 { 1256 return XMLUtilities.charsToEntities(str,false); 1257 } 1259 public static final DecimalFormat KB_FORMAT = new DecimalFormat ("#.# KB"); 1261 public static final DecimalFormat MB_FORMAT = new DecimalFormat ("#.# MB"); 1262 1263 1269 public static String formatFileSize(long length) 1270 { 1271 if(length < 1024) 1272 return length + " bytes"; 1273 else if(length < 1024 << 10) 1274 return KB_FORMAT.format((double)length / 1024); 1275 else 1276 return MB_FORMAT.format((double)length / 1024 / 1024); 1277 } 1279 1286 public static String getLongestPrefix(List str, boolean ignoreCase) 1287 { 1288 if(str.size() == 0) 1289 return ""; 1290 1291 int prefixLength = 0; 1292 1293loop: for(;;) 1294 { 1295 String s = str.get(0).toString(); 1296 if(prefixLength >= s.length()) 1297 break loop; 1298 char ch = s.charAt(prefixLength); 1299 for(int i = 1; i < str.size(); i++) 1300 { 1301 s = str.get(i).toString(); 1302 if(prefixLength >= s.length()) 1303 break loop; 1304 if(!compareChars(s.charAt(prefixLength),ch,ignoreCase)) 1305 break loop; 1306 } 1307 prefixLength++; 1308 } 1309 1310 return str.get(0).toString().substring(0,prefixLength); 1311 } 1313 1320 public static String getLongestPrefix(String [] str, boolean ignoreCase) 1321 { 1322 return getLongestPrefix((Object [])str,ignoreCase); 1323 } 1325 1332 public static String getLongestPrefix(Object [] str, boolean ignoreCase) 1333 { 1334 if(str.length == 0) 1335 return ""; 1336 1337 int prefixLength = 0; 1338 1339 String first = str[0].toString(); 1340 1341loop: for(;;) 1342 { 1343 if(prefixLength >= first.length()) 1344 break loop; 1345 char ch = first.charAt(prefixLength); 1346 for(int i = 1; i < str.length; i++) 1347 { 1348 String s = str[i].toString(); 1349 if(prefixLength >= s.length()) 1350 break loop; 1351 if(!compareChars(s.charAt(prefixLength),ch,ignoreCase)) 1352 break loop; 1353 } 1354 prefixLength++; 1355 } 1356 1357 return first.substring(0,prefixLength); 1358 } 1360 1362 1364 1373 @Deprecated 1374 public static void quicksort(Object [] obj, Comparator compare) 1375 { 1376 Arrays.sort(obj,compare); 1377 } 1379 1387 @Deprecated 1388 public static void quicksort(Vector vector, Comparator compare) 1389 { 1390 Collections.sort(vector,compare); 1391 } 1393 1401 @Deprecated 1402 public static void quicksort(List list, Comparator compare) 1403 { 1404 Collections.sort(list,compare); 1405 } 1407 1415 @Deprecated 1416 public static void quicksort(Object [] obj, Compare compare) 1417 { 1418 Arrays.sort(obj,compare); 1419 } 1421 1428 @Deprecated 1429 public static void quicksort(Vector vector, Compare compare) 1430 { 1431 Collections.sort(vector,compare); 1432 } 1434 1442 @Deprecated 1443 public interface Compare extends Comparator 1444 { 1445 int compare(Object obj1, Object obj2); 1446 } 1448 1453 @Deprecated 1454 public static class StringCompare implements Compare 1455 { 1456 public int compare(Object obj1, Object obj2) 1457 { 1458 return StandardUtilities.compareStrings(obj1.toString(), 1459 obj2.toString(),false); 1460 } 1461 } 1463 1467 public static class StringICaseCompare implements Comparator<Object > 1468 { 1469 public int compare(Object obj1, Object obj2) 1470 { 1471 return StandardUtilities.compareStrings(obj1.toString(), obj2.toString(), true); 1472 } 1473 } 1475 1479 public static class MenuItemCompare implements Compare 1480 { 1481 public int compare(Object obj1, Object obj2) 1482 { 1483 boolean obj1E, obj2E; 1484 obj1E = obj1 instanceof EnhancedMenuItem; 1485 obj2E = obj2 instanceof EnhancedMenuItem; 1486 if(obj1E && !obj2E) 1487 return 1; 1488 else if(obj2E && !obj1E) 1489 return -1; 1490 else 1491 return StandardUtilities.compareStrings(((JMenuItem )obj1).getText(), 1492 ((JMenuItem )obj2).getText(),true); 1493 } 1494 } 1496 1498 1504 public static String buildToVersion(String build) 1505 { 1506 if(build.length() != 11) 1507 return "<unknown version: " + build + ">"; 1508 int major = Integer.parseInt(build.substring(0,2)); 1510 int minor = Integer.parseInt(build.substring(3,5)); 1512 int beta = Integer.parseInt(build.substring(6,8)); 1514 int bugfix = Integer.parseInt(build.substring(9,11)); 1516 1517 return major + "." + minor 1518 + (beta != 99 ? "pre" + beta : 1519 (bugfix != 0 ? "." + bugfix : "final")); 1520 } 1522 1546 public static boolean isToolsJarAvailable() 1547 { 1548 Log.log(Log.DEBUG, MiscUtilities.class,"Searching for tools.jar..."); 1549 1550 Vector paths = new Vector(); 1551 1552 paths.addElement("System classpath: " 1554 + System.getProperty("java.class.path")); 1555 1556 try 1557 { 1558 try 1561 { 1562 Class.forName("sun.tools.javac.Main"); 1563 } 1564 catch(ClassNotFoundException e1) 1565 { 1566 Class.forName("com.sun.tools.javac.Main"); 1567 } 1568 Log.log(Log.DEBUG, MiscUtilities.class, 1569 "- is in classpath. Fine."); 1570 return true; 1571 } 1572 catch(ClassNotFoundException e) 1573 { 1574 } 1578 String settingsDir = jEdit.getSettingsDirectory(); 1580 if(settingsDir != null) 1581 { 1582 String toolsPath = constructPath(settingsDir, "jars", 1583 "tools.jar"); 1584 paths.addElement(toolsPath); 1585 if(new File(toolsPath).exists()) 1586 { 1587 Log.log(Log.DEBUG, MiscUtilities.class, 1588 "- is in the user's jars folder. Fine."); 1589 return true; 1591 } 1592 } 1594 String jEditDir = jEdit.getJEditHome(); 1596 if(jEditDir != null) 1597 { 1598 String toolsPath = constructPath(jEditDir, "jars", "tools.jar"); 1599 paths.addElement(toolsPath); 1600 if(new File(toolsPath).exists()) 1601 { 1602 Log.log(Log.DEBUG, MiscUtilities.class, 1603 "- is in jEdit's system jars folder. Fine."); 1604 return true; 1606 } 1607 } 1609 String toolsPath = System.getProperty("java.home"); 1611 if(toolsPath.toLowerCase().endsWith(File.separator + "jre")) 1612 toolsPath = toolsPath.substring(0, toolsPath.length() - 4); 1613 toolsPath = constructPath(toolsPath, "lib", "tools.jar"); 1614 paths.addElement(toolsPath); 1615 1616 if(!(new File(toolsPath).exists())) 1617 { 1618 Log.log(Log.WARNING, MiscUtilities.class, 1619 "Could not find tools.jar.\n" 1620 + "I checked the following locations:\n" 1621 + paths.toString()); 1622 return false; 1623 } 1625 PluginJAR jar = jEdit.getPluginJAR(toolsPath); 1627 if(jar == null) 1628 { 1629 Log.log(Log.DEBUG, MiscUtilities.class, 1630 "- adding " + toolsPath + " to jEdit plugins."); 1631 jEdit.addPluginJAR(toolsPath); 1632 } 1633 else 1634 Log.log(Log.DEBUG, MiscUtilities.class, 1635 "- has been loaded before."); 1636 1638 return true; 1639 } 1641 1647 public static int parsePermissions(String s) 1648 { 1649 int permissions = 0; 1650 1651 if(s.length() == 9) 1652 { 1653 if(s.charAt(0) == 'r') 1654 permissions += 0400; 1655 if(s.charAt(1) == 'w') 1656 permissions += 0200; 1657 if(s.charAt(2) == 'x') 1658 permissions += 0100; 1659 else if(s.charAt(2) == 's') 1660 permissions += 04100; 1661 else if(s.charAt(2) == 'S') 1662 permissions += 04000; 1663 if(s.charAt(3) == 'r') 1664 permissions += 040; 1665 if(s.charAt(4) == 'w') 1666 permissions += 020; 1667 if(s.charAt(5) == 'x') 1668 permissions += 010; 1669 else if(s.charAt(5) == 's') 1670 permissions += 02010; 1671 else if(s.charAt(5) == 'S') 1672 permissions += 02000; 1673 if(s.charAt(6) == 'r') 1674 permissions += 04; 1675 if(s.charAt(7) == 'w') 1676 permissions += 02; 1677 if(s.charAt(8) == 'x') 1678 permissions += 01; 1679 else if(s.charAt(8) == 't') 1680 permissions += 01001; 1681 else if(s.charAt(8) == 'T') 1682 permissions += 01000; 1683 } 1684 1685 return permissions; 1686 } 1688 1694 @Deprecated 1695 public static String [] getEncodings() 1696 { 1697 return getEncodings(false); 1698 } 1700 1706 public static String [] getEncodings(boolean getSelected) 1707 { 1708 List returnValue = new ArrayList(); 1709 1710 Map map = Charset.availableCharsets(); 1711 Iterator iter = map.keySet().iterator(); 1712 1713 if ((getSelected && !jEdit.getBooleanProperty("encoding.opt-out."+UTF_8_Y,false)) || 1714 !getSelected) 1715 { 1716 returnValue.add(UTF_8_Y); 1717 } 1718 1719 while(iter.hasNext()) 1720 { 1721 String encoding = (String )iter.next(); 1722 if ((getSelected && !jEdit.getBooleanProperty("encoding.opt-out."+encoding,false)) || 1723 !getSelected) 1724 { 1725 returnValue.add(encoding); 1726 } 1727 } 1728 1729 return (String [])returnValue.toArray( 1730 new String [returnValue.size()]); 1731 } 1733 1738 public static String throwableToString(Throwable t) 1739 { 1740 StringWriter s = new StringWriter(); 1741 t.printStackTrace(new PrintWriter(s)); 1742 return s.toString(); 1743 } 1745 1753 @Deprecated 1754 public static boolean parseXML(InputStream in, DefaultHandler handler) 1755 throws IOException 1756 { 1757 return XMLUtilities.parseXML(in, handler); 1758 } 1760 1767 @Deprecated 1768 public static InputSource findEntity(String systemId, String test, Class where) 1769 { 1770 return XMLUtilities.findEntity(systemId, test, where); 1771 } 1773 private MiscUtilities() {} 1775 1776 1778 private static boolean compareChars(char ch1, char ch2, boolean ignoreCase) 1779 { 1780 if(ignoreCase) 1781 return Character.toUpperCase(ch1) == Character.toUpperCase(ch2); 1782 else 1783 return ch1 == ch2; 1784 } 1786 private static int getPathStart(String path) 1788 { 1789 int start = 0; 1790 if(path.startsWith("/")) 1791 return 1; 1792 else if(OperatingSystem.isDOSDerived() 1793 && path.length() >= 3 1794 && path.charAt(1) == ':' 1795 && (path.charAt(2) == '/' 1796 || path.charAt(2) == '\\')) 1797 return 3; 1798 else 1799 return 0; 1800 } 1802 } 1804 | Popular Tags |