1 20 21 package org.jdesktop.jdic.filetypes.internal; 22 23 import java.net.URL ; 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import org.jdesktop.jdic.filetypes.Action; 28 import org.jdesktop.jdic.filetypes.RegisterFailedException; 29 30 31 34 public class WinRegistryUtil { 35 36 66 67 private static final int MAX_CLSID_NUMBER = 1000; 69 70 private final static int ERROR_SUCCESS = WinRegistryWrapper.ERROR_SUCCESS; 72 private final static int ERROR_ITEM_EXISTED = WinRegistryWrapper.ERROR_ITEM_EXIST; 73 private final static int ERROR_ITEM_NOTEXISTED = WinRegistryWrapper.ERROR_ITEM_NOTEXIST; 74 private final static int MAX_KEY_LENGTH = WinRegistryWrapper.MAX_KEY_LENGTH; 75 private final static String VN_DEFAULT = ""; 77 private final static String SYS_USER_KN_PREFIX = "SOFTWARE\\Classes"; 79 80 private final static String VN_CONTENT = "Content type"; 82 83 private final static String KN_DEFAULTICON = "DefaultIcon"; private final static String VN_DEFAULTGENERATOR = "Generated By"; private final static String VALUE_DEFAULTGENERATOR = "Generated By Java-Association"; private final static String KN_SHELL = "shell"; private final static String KN_COMMAND = "command"; private final static String KN_CURVER = "CurVer"; 91 private final static String KN_MIME = "MIME\\Database\\Content Type"; private final static String VN_EXTENSION = "Extension"; 95 private final static String USER_FILE_EXT_KEY_PREFIX = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts"; 97 private final static String USER_FILE_EXT_VALUENAME = "Application"; 98 private final static String USER_FILE_EXT_APP_PREFIX = "Applications"; 99 100 101 private final static int USER_LEVEL = AppConstants.USER_LEVEL; 103 private final static int SYSTEM_LEVEL = AppConstants.SYSTEM_LEVEL; 104 private final static int ROOT_LEVEL = AppConstants.DEFAULT_LEVEL; 105 106 private final static String osName = System.getProperty("os.name"); 108 private final static String WIN2KOS = "Windows 2000"; 109 110 private WinRegistryUtil() {} 111 112 118 private static int getHKeyByLevel(int regLevel) { 119 int hKey; 120 121 switch (regLevel) { 122 case USER_LEVEL: 123 hKey = WinRegistryWrapper.HKEY_CURRENT_USER; 124 break; 125 126 case SYSTEM_LEVEL: 127 hKey = WinRegistryWrapper.HKEY_LOCAL_MACHINE; 128 break; 129 130 case ROOT_LEVEL: 131 hKey = WinRegistryWrapper.HKEY_CLASSES_ROOT; 132 break; 133 134 default: 135 hKey = WinRegistryWrapper.HKEY_CLASSES_ROOT; 136 break; 137 } 138 return hKey; 139 } 140 141 150 private static String genClassID(String fileExt, int regLevel) { 151 boolean isClsIDExist = true; 152 String appendix, temClsID, temClsIDKey, temFileExt; 153 154 temFileExt = fileExt.trim(); 155 if (temFileExt.charAt(0) == '.') { 156 temFileExt = temFileExt.substring(1); 157 } 158 int i = 1; 159 160 while ((isClsIDExist) && (i < MAX_CLSID_NUMBER)) { 161 appendix = Integer.toString(i); 162 temClsID = "class" + temFileExt + appendix; 164 if (temClsID != null) { 165 temClsIDKey = getClsIDKey(temClsID, regLevel); 167 if (temClsIDKey != null) { 168 isClsIDExist = isSubKeyExist(temClsIDKey, regLevel); 170 if (!isClsIDExist) { 171 return temClsID; 173 } 174 } 175 } 176 i++; 177 } 178 179 return null; 180 } 181 182 189 private static boolean isSubKeyExist(String subKey, int regLevel) { 190 int hKey = getHKeyByLevel(regLevel); 191 192 if (WinRegistryWrapper.WinRegSubKeyExist(hKey, subKey) 193 == ERROR_ITEM_EXISTED) { 194 return true; 195 } else { 196 return false; 197 } 198 } 199 200 208 private static boolean isValueExist(String subKey, String valueName, int regLevel) { 209 if (isSubKeyExist(subKey, regLevel)) { 210 int hKey = getHKeyByLevel(regLevel); 211 212 if (WinRegistryWrapper.WinRegValueExist(hKey, subKey, valueName) 213 == ERROR_ITEM_EXISTED) { 214 return true; 215 } 216 } 217 218 return false; 219 } 220 221 228 private static String getMimeTypeKey(String mimeType, int regLevel) { 229 String mimeSubKey = KN_MIME + "\\" + mimeType; 231 232 if (regLevel != ROOT_LEVEL) { 233 mimeSubKey = SYS_USER_KN_PREFIX + "\\" + mimeSubKey; 235 } 236 return mimeSubKey; 237 } 238 239 246 private static String getFileExtKey(String fileExt, int regLevel) { 247 String fileExtKey = fileExt; 248 249 if (regLevel != ROOT_LEVEL) { 250 fileExtKey = SYS_USER_KN_PREFIX + "\\" + fileExtKey; 252 } 253 return fileExtKey; 254 } 255 256 263 private static String getClsIDKey(String clsID, int regLevel) { 264 String clsIDKey = clsID; 265 266 if (regLevel != ROOT_LEVEL) { 267 clsIDKey = SYS_USER_KN_PREFIX + "\\" + clsIDKey; 269 } 270 return clsIDKey; 271 } 272 273 280 private static String getIconKey(String fileExt, int regLevel) { 281 String clsID = getClassIDByFileExt(fileExt, regLevel); 283 284 if (clsID != null) { 285 String iconKey = clsID + "\\" + KN_DEFAULTICON; 287 if (regLevel != ROOT_LEVEL) { 288 iconKey = SYS_USER_KN_PREFIX + "\\" + iconKey; 290 } 291 return iconKey; 292 } 293 return null; 294 } 295 296 303 private static void regCreateKeyEx(String subKey, int regLevel) 304 throws RegisterFailedException { 305 int hKey = getHKeyByLevel(regLevel); 307 if (WinRegistryWrapper.WinRegCreateKeyEx(hKey, subKey) 309 != ERROR_SUCCESS) { 310 throw new RegisterFailedException("Key " + subKey 311 + " creation error!"); 312 } 313 } 314 315 322 private static void regDeleteKey(String subKey, int regLevel) 323 throws RegisterFailedException { 324 int hKey = getHKeyByLevel(regLevel); 326 327 if (WinRegistryWrapper.WinRegDeleteKey(hKey, subKey) 328 != ERROR_SUCCESS) { 329 throw new RegisterFailedException("Key " + subKey 330 + " delete error."); 331 } 332 } 333 334 342 private static String regQueryValueEx(String subKey, String valueName, int regLevel) { 343 if (isValueExist(subKey, valueName, regLevel)) { 345 int hKey = getHKeyByLevel(regLevel); 346 347 return WinRegistryWrapper.WinRegQueryValueEx(hKey, subKey, 348 valueName); 349 } 350 return null; 351 } 352 353 362 private static void regSetValueEx(String subKey, String valueName, String value, 363 int regLevel) throws RegisterFailedException { 364 if (!isSubKeyExist(subKey, regLevel)) { 366 regCreateKeyEx(subKey, regLevel); 367 } 368 int hKey = getHKeyByLevel(regLevel); 369 370 if (WinRegistryWrapper.WinRegSetValueEx(hKey, subKey, valueName, 371 value) != ERROR_SUCCESS) { 372 throw new RegisterFailedException("Value:" + " valueName" 373 + " setting error"); 374 } 375 } 376 377 385 private static String [] regGetSubKeys(String subKey, int regLevel) { 386 int hKey = getHKeyByLevel(regLevel); 387 388 return WinRegistryWrapper.WinRegGetSubKeys(hKey, subKey, 389 MAX_KEY_LENGTH); 390 } 391 392 400 private static String getDefaultValue(String subKey, int regLevel) { 401 int hKey = getHKeyByLevel(regLevel); 402 403 return WinRegistryWrapper.WinRegQueryValueEx(hKey, subKey, 404 VN_DEFAULT); 405 } 406 407 415 private static void setDefaultValue(String subKey, String value, int regLevel) 416 throws RegisterFailedException { 417 if (!isSubKeyExist(subKey, regLevel)) { 419 regCreateKeyEx(subKey, regLevel); 420 } 421 int hKey = getHKeyByLevel(regLevel); 422 423 if (WinRegistryWrapper.WinRegSetValueEx(hKey, subKey, VN_DEFAULT, 424 value) != ERROR_SUCCESS) { 425 throw new RegisterFailedException("Set default value" 426 + " for key " + subKey + " error."); 427 } 428 WinRegistryWrapper.WinRegFlushKey(hKey, subKey); 429 } 430 431 440 private static void addActionByClsID(Action action, String clsID, int regLevel) 441 throws RegisterFailedException { 442 String verb = action.getVerb(); 443 String desc = action.getDescription(); 444 String cmd = action.getCommand(); 445 446 String clsIDKey = getClsIDKey(clsID, regLevel); 447 String shellKey = clsIDKey + "\\" + KN_SHELL; 448 String verbKey = shellKey + "\\" + verb; 449 String cmdKey = verbKey + "\\" + KN_COMMAND; 450 451 if (cmdKey != null) { 452 regCreateKeyEx(cmdKey, regLevel); 453 if (cmd != null) { 454 setDefaultValue(cmdKey, cmd, regLevel); 455 if ((desc != null) && (verbKey != null)) { 456 setDefaultValue(verbKey, desc, regLevel); 457 } 458 } 459 } 460 461 } 462 463 471 public static String getMimeTypeByFileExt(String fileExt, int regLevel) { 472 String fileExtKey = getFileExtKey(fileExt, regLevel); 473 if (fileExtKey != null) { 474 return regQueryValueEx(fileExtKey, VN_CONTENT, regLevel); 475 } else { 476 return null; 477 } 478 } 479 480 487 public static String getMimeTypeByFileExt(String fileExt) { 488 return (getMimeTypeByFileExt(fileExt, ROOT_LEVEL)); 489 } 490 491 499 public static void setMimeTypeByFileExt(String mimeType, String fileExt, 500 int regLevel) throws RegisterFailedException { 501 String fileExtKey = getFileExtKey(fileExt, regLevel); 502 if (fileExtKey != null) { 503 regSetValueEx(fileExtKey, VN_CONTENT, mimeType, regLevel); 505 } 506 } 507 508 516 public static String getFileExtByMimeType(String mimeType, int regLevel) { 517 String mimeSubKey = getMimeTypeKey(mimeType, regLevel); 518 if (mimeSubKey != null) { 519 return regQueryValueEx(mimeSubKey, VN_EXTENSION, regLevel); 520 } else { 521 return null; 522 } 523 } 524 525 532 public static String getFileExtByMimeType(String mimeType) { 533 return (getFileExtByMimeType(mimeType, ROOT_LEVEL)); 534 } 535 536 544 public static void setFileExtByMimeType(String fileExt, String mimeType, int regLevel) 545 throws RegisterFailedException { 546 String mimeSubKey = getMimeTypeKey(mimeType, regLevel); 547 if (mimeSubKey != null) { 548 regSetValueEx(mimeSubKey, VN_EXTENSION, fileExt, regLevel); 549 } 550 } 551 552 559 public static String getIconFileNameByFileExt(String fileExt, int regLevel) { 560 String iconKey = getIconKey(fileExt, regLevel); 562 if (iconKey == null) { 563 return null; 564 } 565 String unDealedFileName = getDefaultValue(iconKey, regLevel); 566 if (unDealedFileName == null) { 567 return null; 568 } 569 return ExpandEnvironmentStrings(unDealedFileName); 570 } 571 572 580 public static String getIconFileNameByFileExt(String fileExt) { 581 return (getIconFileNameByFileExt(fileExt, ROOT_LEVEL)); 582 } 583 584 592 public static void setIconFileNameByFileExt(String iconFileName, String fileExt, 593 int regLevel) throws RegisterFailedException { 594 String iconKey = getIconKey(fileExt, regLevel); 596 if (iconKey == null) { 597 String temClassID = genClassID(fileExt, regLevel); 599 if (temClassID != null) { 600 setClassIDByFileExt(fileExt, temClassID, regLevel); 601 iconKey = getIconKey(fileExt, regLevel); 602 } 603 } 604 if (iconKey != null) { 605 setDefaultValue(iconKey, iconFileName, regLevel); 607 } 608 } 609 610 617 public static String getDescriptionByFileExt(String fileExt, int regLevel) { 618 String classID = getClassIDByFileExt(fileExt, regLevel); 620 if (classID != null) { 621 String clsIDKey = getClsIDKey(classID, regLevel); 622 if (clsIDKey != null) { 623 return getDefaultValue(clsIDKey, regLevel); 625 } 626 } 627 return null; 628 } 629 630 637 public static String getDescriptionByFileExt(String fileExt) { 638 return (getDescriptionByFileExt(fileExt, ROOT_LEVEL)); 639 } 640 641 649 public static void setDescriptionByFileExt(String description, String fileExt, 650 int regLevel) throws RegisterFailedException { 651 String classID = getClassIDByFileExt(fileExt, regLevel); 652 if (classID == null) { 653 classID = genClassID(fileExt, regLevel); 655 if (classID != null) { 656 setClassIDByFileExt(fileExt, classID, regLevel); 657 } 658 } 659 if (classID != null) { 660 String clsIDKey = getClsIDKey(classID, regLevel); 661 if (clsIDKey != null) { 662 setDefaultValue(clsIDKey, description, regLevel); 664 } 665 } 666 } 667 668 675 public static void markGeneratorByFileExt(String fileExt, int regLevel) 676 throws RegisterFailedException { 677 String clsID = getClassIDByFileExt(fileExt, regLevel); 679 String clsIDKey = getClsIDKey(clsID, regLevel); 680 if (clsIDKey != null) { 681 regSetValueEx(clsIDKey, VN_DEFAULTGENERATOR, VALUE_DEFAULTGENERATOR, regLevel); 682 } 683 } 684 685 692 public static List getActionListByFileExt(String fileExt, int regLevel) { 693 List actionList = null; 694 695 String clsID = getClassIDByFileExt(fileExt, regLevel); 697 698 if (clsID!= null) { 699 String clsIDKey = getClsIDKey(clsID, regLevel); 700 String shellKey = clsIDKey + "\\" + KN_SHELL; 701 String verbs[] = null; 702 if (shellKey != null) { 703 verbs = regGetSubKeys(shellKey, regLevel); 704 } 705 706 if (verbs != null) { 707 int verbsNum = verbs.length; 708 if (verbsNum > 0) { 710 actionList = new ArrayList (); 711 for (int i = 0; i < verbsNum; i++) { 712 String verbKey = shellKey + "\\" + verbs[i]; 713 String cmdKey = verbKey + "\\" + KN_COMMAND; 714 if (cmdKey != null) { 715 Action oneAction; 716 String temCmd = getDefaultValue(cmdKey, regLevel); 717 if (temCmd == null) { 719 temCmd = ""; 720 } else { 721 temCmd = ExpandEnvironmentStrings(temCmd); 722 } 723 oneAction = new Action(verbs[i], temCmd, getDefaultValue(verbKey, regLevel)); 724 actionList.add(oneAction); 725 } 726 } 727 } 728 } 729 } 730 731 return actionList; 732 } 733 734 741 public static List getActionListByFileExt(String fileExt) { 742 List rootActionList = getActionListByFileExt(fileExt, ROOT_LEVEL); 743 List userDefinedList = getUserAddedActionListByFileExt(fileExt); 744 if (userDefinedList != null) { 745 return userDefinedList; 746 } else { 747 return rootActionList; 748 } 749 } 750 751 770 private static List getUserAddedActionListByFileExt(String fileExt) { 771 String fileExtKey = USER_FILE_EXT_KEY_PREFIX + "\\" + fileExt; 772 String valueName = USER_FILE_EXT_VALUENAME; 773 int hKey = WinRegistryWrapper.HKEY_CURRENT_USER; 774 String appName = WinRegistryWrapper.WinRegQueryValueEx(hKey, fileExtKey, valueName); 776 String verbs[] = null; 778 String appShellKey = USER_FILE_EXT_APP_PREFIX + "\\" + appName + "\\" + KN_SHELL; 779 hKey = WinRegistryWrapper.HKEY_CLASSES_ROOT; 780 verbs = WinRegistryWrapper.WinRegGetSubKeys(hKey, appShellKey, 255); 781 782 List actionList = null; 784 if (verbs != null) { 785 int verbsNum = verbs.length; 786 if (verbsNum > 0) { 787 actionList = new ArrayList (); 788 for (int i = 0; i < verbsNum; i++) { 789 String verbKey = appShellKey + "\\" + verbs[i]; 790 String cmdKey = verbKey + "\\" + KN_COMMAND; 791 if (cmdKey != null) { 792 Action oneAction; 793 String temCmd = getDefaultValue(cmdKey, ROOT_LEVEL); 794 if (temCmd == null) { 796 temCmd = ""; 797 }else { 798 temCmd = ExpandEnvironmentStrings(temCmd); 799 } 800 oneAction = new Action(verbs[i], temCmd, getDefaultValue(verbKey, ROOT_LEVEL)); 801 actionList.add(oneAction); 802 } 803 } 804 } 805 } 806 807 return actionList; 808 } 809 810 818 public static void setActionListByFileExt(List actionList, String fileExt, 819 int regLevel) throws RegisterFailedException { 820 String clsID = getClassIDByFileExt(fileExt, regLevel); 822 if (clsID == null) { 823 clsID = genClassID(fileExt, regLevel); 825 if (clsID != null) { 826 setClassIDByFileExt(fileExt, clsID, regLevel); 828 } 829 } 830 831 Action oneAction; 832 if (clsID != null) { 833 if (actionList != null) { 834 Iterator actionIter = actionList.iterator(); 835 while (actionIter.hasNext()) { 837 oneAction = (Action) actionIter.next(); 838 if ((oneAction != null) && (clsID != null)) { 839 addActionByClsID(oneAction, clsID, regLevel); 840 } 841 } 842 } 843 } 844 } 845 846 853 public static String getMimeTypeByURL(URL url) { 854 return WinRegistryWrapper.WinFindMimeFromData(url); 855 } 856 857 864 public static String ExpandEnvironmentStrings(String cmdString) { 865 return WinRegistryWrapper.WinExpandEnvironmentStrings(cmdString); 866 } 867 868 875 public static boolean isMimeTypeExist(String mimeType, int regLevel) { 876 String mimeTypeKey = getMimeTypeKey(mimeType, regLevel); 878 if (mimeTypeKey != null) { 879 return isSubKeyExist(mimeTypeKey, regLevel); 881 } else { 882 return false; 883 } 884 } 885 886 892 public static boolean isMimeTypeExist(String mimeType) { 893 return (isMimeTypeExist(mimeType, ROOT_LEVEL)); 894 } 895 896 903 public static boolean isFileExtExist(String fileExt, int regLevel) { 904 String fileExtKey = getFileExtKey(fileExt, regLevel); 906 if (fileExtKey != null) { 907 return (isSubKeyExist(fileExtKey, regLevel) || isWin2kUserDefinedFileExtExist(fileExt)); 911 } else { 912 return false; 913 } 914 } 915 916 923 public static boolean isWin2kUserDefinedFileExtExist(String fileExt) { 924 boolean win2kFileDefinedByUser = false; 925 if (osName.equalsIgnoreCase(WIN2KOS)) { 926 String fileExtKey = USER_FILE_EXT_KEY_PREFIX + "\\" + fileExt; 927 win2kFileDefinedByUser = isSubKeyExist(fileExtKey, USER_LEVEL); 928 } 929 return win2kFileDefinedByUser; 930 } 931 932 938 public static boolean isFileExtExist(String fileExt) { 939 return (isFileExtExist(fileExt, ROOT_LEVEL)); 940 } 941 942 949 public static void addFileExt(String fileExt, int regLevel) 950 throws RegisterFailedException { 951 String fileExtKey = getFileExtKey(fileExt, regLevel); 953 if (fileExtKey != null) { 954 regCreateKeyEx(fileExtKey, regLevel); 956 } 957 } 958 959 966 public static void removeFileExt(String fileExt, int regLevel) 967 throws RegisterFailedException { 968 if (isFileExtExist(fileExt, regLevel)) { 969 String fileExtKey = getFileExtKey(fileExt, regLevel); 971 String clsID = getClassIDByFileExt(fileExt, regLevel); 973 if (clsID != null) { 974 String clsIDKey = getClsIDKey(clsID, regLevel); 975 if (clsIDKey != null) { 976 String value_generator = regQueryValueEx(clsIDKey, VN_DEFAULTGENERATOR, regLevel); 977 if (value_generator.compareTo(VALUE_DEFAULTGENERATOR) == 0) { 978 regDeleteKey(clsIDKey, regLevel); 980 } 981 } 982 } 983 if (fileExtKey != null) { 984 if (isSubKeyExist(fileExtKey, regLevel)) { 986 regDeleteKey(fileExtKey, regLevel); 987 } 988 } 989 if (isWin2kUserDefinedFileExtExist(fileExt)) { 992 fileExtKey = USER_FILE_EXT_KEY_PREFIX + "\\" + fileExt; 993 regDeleteKey(fileExtKey, USER_LEVEL); 994 } 995 } 996 } 997 998 1005 public static void addMimeType(String mimeType, int regLevel) 1006 throws RegisterFailedException { 1007 String temMimeKey = getMimeTypeKey(mimeType, regLevel); 1009 if (temMimeKey != null) { 1010 regCreateKeyEx(temMimeKey, regLevel); 1012 } 1013 } 1014 1015 1022 public static void removeMimeType(String mimeType, int regLevel) 1023 throws RegisterFailedException { 1024 if (isMimeTypeExist(mimeType, regLevel)) { 1025 String mimeKey = getMimeTypeKey(mimeType, regLevel); 1027 if (mimeKey != null) { 1028 regDeleteKey(mimeKey, regLevel); 1030 } 1031 } 1032 } 1033 1034 1042 public static void setClassIDByFileExt(String fileExt, String classID, int regLevel) 1043 throws RegisterFailedException { 1044 String fileExtKey = getFileExtKey(fileExt, regLevel); 1045 String clsIDKey = getClsIDKey(classID, regLevel); 1046 if (fileExtKey != null) { 1047 1050 if (!isSubKeyExist(fileExtKey, regLevel)) { 1051 addFileExt(fileExt, regLevel); 1052 } 1053 1056 if (!isSubKeyExist(clsIDKey, regLevel)) { 1057 if (clsIDKey != null) { 1058 regCreateKeyEx(clsIDKey, regLevel); 1059 } 1060 } 1061 setDefaultValue(fileExtKey, classID, regLevel); 1062 } 1063 } 1064 1065 1073 public static String getClassIDByFileExt(String fileExt, int regLevel) { 1074 String fileExtKey = getFileExtKey(fileExt, regLevel); 1076 if (fileExtKey != null) { 1077 if (isSubKeyExist(fileExtKey, regLevel)) { 1078 return getCurVerClassID(getDefaultValue(fileExtKey,regLevel),regLevel); 1080 } 1081 } 1082 return null; 1083 } 1084 1085 1091 private static String getCurVerClassID(String defaultVerClassID, 1092 int regLevel) { 1093 String curVerClassIDKey = defaultVerClassID + "\\" + KN_CURVER; 1094 1095 if (regLevel != ROOT_LEVEL) { 1096 curVerClassIDKey = SYS_USER_KN_PREFIX + "\\" + curVerClassIDKey; 1098 } 1099 1100 if (isSubKeyExist(curVerClassIDKey, regLevel)) { 1101 return getDefaultValue(curVerClassIDKey, regLevel); 1103 } else { 1104 return defaultVerClassID; 1106 } 1107 } 1108 1109 1117 public static void setMutualRef(String fileExt, String mimeType, int regLevel) 1118 throws RegisterFailedException { 1119 String mimeKey = getMimeTypeKey(mimeType, regLevel); 1120 String fileExtKey = getFileExtKey(fileExt, regLevel); 1121 1122 if ((mimeKey != null) && (fileExtKey != null)) { 1123 if ((isSubKeyExist(fileExtKey, regLevel)) 1124 && (isSubKeyExist(mimeKey, regLevel))) { 1125 setMimeTypeByFileExt(mimeType, fileExt, regLevel); 1126 setFileExtByMimeType(fileExt, mimeType, regLevel); 1127 } 1128 } 1129 } 1130 1131} 1132 | Popular Tags |