⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 winregistryutil.java

📁 JDesktop Integration Components (JDIC)
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is * subject to license terms. *  * This program is free software; you can redistribute it and/or modify * it under the terms of the Lesser GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. *  * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * General Public License for more details. *  * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA. */ package org.jdesktop.jdic.filetypes.internal;import java.net.URL;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import org.jdesktop.jdic.filetypes.Action;import org.jdesktop.jdic.filetypes.RegisterFailedException;/** * Utility class for accessing the system registry and association info for Windows. */public class WinRegistryUtil {        /* Draft to illustrate the Registry items relationship     HKEY_CLASSES_ROOT     |           Key Nmae              Valuename         Value     |_.html //file extension             ""            htmlfile    //clsID     |                                  Content Type    text/html   //MIME type (optional)     |                                                 ... ...     |_htmlfile //clsID                   ""              HTML Document //Description     |   |     |   |_DefaultIcon                    ""              "c:\program files\..." //Default Icon file name     |   |     |   |_shell //shell command list     ""              opennew       //Default shell command (optional)     |       |     |       |_Edit //shell command       ""              Edit(&E)      //Command Description     |       |   |     |       |   |_Command // content     ""              "c:\Program files\..." //real command     |       |     |       |_opennew                    ""              Open (&O)     |           |     |           |_Command                ""              "c:\Program files\..."     ... ...     |_MIME //MIME Info     |   |     |   |_Database     |       |     |       |_Content Type     |           |     |           |_text/html  //MIME     Extension       .htm        //corresponding file extension                |      */    // Constant value to generate a classID name     private static final int MAX_CLSID_NUMBER = 1000;      // general constants    private final static int ERROR_SUCCESS         = WinRegistryWrapper.ERROR_SUCCESS;    private final static int ERROR_ITEM_EXISTED    = WinRegistryWrapper.ERROR_ITEM_EXIST;    private final static int ERROR_ITEM_NOTEXISTED = WinRegistryWrapper.ERROR_ITEM_NOTEXIST;    private final static int MAX_KEY_LENGTH        = WinRegistryWrapper.MAX_KEY_LENGTH;    private final static String VN_DEFAULT         = "";             // default value name      // Constants for USERKEY & SYSKEY    private final static String SYS_USER_KN_PREFIX = "SOFTWARE\\Classes";            // Constants for file extension    private final static String VN_CONTENT         = "Content type";        // Constants under clsID item    private final static String KN_DEFAULTICON         = "DefaultIcon"; // key name to denote the default icon file name    private final static String VN_DEFAULTGENERATOR    = "Generated By"; //key name to specify the generator    private final static String VALUE_DEFAULTGENERATOR = "Generated By Java-Association"; //Value to specify the generator    private final static String KN_SHELL               = "shell";             // key name for the shell command list    private final static String KN_COMMAND             = "command";         // key name to denote the actual command string    private final static String KN_CURVER              = "CurVer";          // key name for CurVer      // Constants reletive to the MIME information        private final static String KN_MIME      = "MIME\\Database\\Content Type";            // key name to locate mime type    private final static String VN_EXTENSION = "Extension";  // value name to indicate mime type corresponding file extension        // Constants for the Windows 2000 user added file extension    private final static String USER_FILE_EXT_KEY_PREFIX = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts";    private final static String USER_FILE_EXT_VALUENAME  = "Application";    private final static String USER_FILE_EXT_APP_PREFIX = "Applications";          // Constants for the level designation    private final static int USER_LEVEL = AppConstants.USER_LEVEL;    private final static int SYSTEM_LEVEL = AppConstants.SYSTEM_LEVEL;    private final static int ROOT_LEVEL = AppConstants.DEFAULT_LEVEL;        // Constants for the OS Name    private final static String osName = System.getProperty("os.name");    private final static String WIN2KOS = "Windows 2000";    private WinRegistryUtil() {}        /**     * Gets the corresponding Windows Registry folder name according to the regLevel.     *     * @param regLevel given regLeve     * @return corresponding windows registry folder constants     */      private static int getHKeyByLevel(int regLevel) {        int hKey;        switch (regLevel) {        case USER_LEVEL:    /* USER_LEVEL -->HKEY_CURRENT_USER */            hKey = WinRegistryWrapper.HKEY_CURRENT_USER;            break;        case SYSTEM_LEVEL:  /* SYSTEM_LEVEL-->HKEY_LOCAL_MACHINE */            hKey = WinRegistryWrapper.HKEY_LOCAL_MACHINE;            break;        case ROOT_LEVEL:    /* ROOT_LEVEL-->HKEY_CLASSES_ROOT */            hKey = WinRegistryWrapper.HKEY_CLASSES_ROOT;            break;                    default:            /* Default will be HKEY_CLASSES_ROOT */            hKey = WinRegistryWrapper.HKEY_CLASSES_ROOT;            break;        }         return hKey;    }      /**     *  Constructs a new class ID name based on the given fileExt name     *  by adding a sequence number at the rear of the given fileExt.     *     *  @param fileExt given file extension (not null)     *  @param regLevel given regLevel     *  @return the constructed class ID name string, or null if construction     *  failed     */    private static String genClassID(String fileExt, int regLevel) {        boolean isClsIDExist = true;        String appendix, temClsID, temClsIDKey, temFileExt;        temFileExt = fileExt.trim();        if (temFileExt.charAt(0) == '.') {            temFileExt = temFileExt.substring(1);        }        int i = 1;        while ((isClsIDExist) && (i < MAX_CLSID_NUMBER)) {            appendix = Integer.toString(i);            // construct the temporary ClsID            temClsID = "class" + temFileExt + appendix;            if (temClsID != null) {                // Get the corresponding windows registry key                 temClsIDKey = getClsIDKey(temClsID, regLevel);                if (temClsIDKey != null) {                    // Check if this key exists                    isClsIDExist = isSubKeyExist(temClsIDKey, regLevel);                    if (!isClsIDExist) {                        // Not exists, return the key                        return temClsID;                     }                }            }            i++;        }                return null;    }        /**     * Wrapper method for WinRegistryWrapper.WinRegSubKeyExist.     *     * @param subKey name of the key (not null)     * @param regLevel given regLevel     * @return true if the subKey exists     */    private static boolean isSubKeyExist(String subKey, int regLevel) {        int hKey = getHKeyByLevel(regLevel);        if (WinRegistryWrapper.WinRegSubKeyExist(hKey, subKey)                == ERROR_ITEM_EXISTED) {            return true;        } else {            return false;        }    }      /**     * Returns true if the specified subkey and value exist.     *     * @param subKey given key name (not null).     * @param valueName given value name (not null).     * @param regLevel given regLevel.     * @return true if the given key contains the specified valuename.     */     private static boolean isValueExist(String subKey, String valueName, int regLevel) {        if (isSubKeyExist(subKey, regLevel)) {            int hKey = getHKeyByLevel(regLevel);            if (WinRegistryWrapper.WinRegValueExist(hKey, subKey, valueName)                    == ERROR_ITEM_EXISTED) {                return true;            }        }                return false;    }    /**     * Returns the corresponding mime key in the Windows registry table.     *     * @param mimeType specified mime type (not null)     * @regLeve given regLevel     * @return corresponding mime type key     */      private static String getMimeTypeKey(String mimeType, int regLevel) {        // MIME\\Database\\Content Type\\***        String mimeSubKey = KN_MIME + "\\" + mimeType;                  if (regLevel != ROOT_LEVEL) {            // software\\classes\\MIME\\Database\\Content Type\\***            mimeSubKey = SYS_USER_KN_PREFIX + "\\" + mimeSubKey;         }        return mimeSubKey;    }      /**     * Returns the corresponding file extension key in the Windows registry table.     *     * @param fileExt given file extension (not null)     * @param regLevel given reglevel     * @return corresponding file extension key     */      private static String getFileExtKey(String fileExt, int regLevel) {        String fileExtKey = fileExt;           if (regLevel != ROOT_LEVEL) {            // software\\classes\\***            fileExtKey = SYS_USER_KN_PREFIX + "\\" + fileExtKey;         }        return fileExtKey;    }      /**     * Returns the corresponding class ID key in the Windows registry table.     *     * @param clsID given class ID (not null)     * @param regLevel given regLevel     * @return corresponding class ID key     */      private static String getClsIDKey(String clsID, int regLevel) {        String clsIDKey = clsID;           if (regLevel != ROOT_LEVEL) {            // software\\classes\\***            clsIDKey = SYS_USER_KN_PREFIX + "\\" + clsIDKey;         }        return clsIDKey;    }    /**     * Returns the corresponding icon key name of the specified file extension.     *     * @param fileExt given file extension (not null)     * @param regLevel given regLevel     * @return corresponding icon key     */      private static String getIconKey(String fileExt, int regLevel) {        // Retrieve the relevant class ID        String clsID = getClassIDByFileExt(fileExt, regLevel);        if (clsID != null) {            //* clsID\\DefaultIcon            String iconKey = clsID + "\\" + KN_DEFAULTICON;            if (regLevel != ROOT_LEVEL) {                // software\\classes\\clsID\\DefaultIcon                iconKey = SYS_USER_KN_PREFIX + "\\" + iconKey;             }            return iconKey;        }        return null;     }      /**     * Wrapper method for WinRegistryWrapper.WinRegCreateKeyEx.     *     * @param subKey name of the key (not null)     * @param regLevel given regLevel     * @throws RegisterFailedException if the operation fails.     */    private static void regCreateKeyEx(String subKey, int regLevel)         throws RegisterFailedException {        // Retrieve the relevant key class        int hKey = getHKeyByLevel(regLevel);        // Create the subKey under hKey        if (WinRegistryWrapper.WinRegCreateKeyEx(hKey, subKey)                != ERROR_SUCCESS) {            throw new RegisterFailedException("Key " + subKey                    + " creation error!");         }    }      /**     * Wrapper method for WinRegistryWrapper.WinRegDeleteKey.     *     * @param subKey name of the key (not null)     * @param regLevel given registry level     * @throws RegisterFailedException if the operation fails.     */    private static void regDeleteKey(String subKey, int regLevel)         throws RegisterFailedException {        // Retrieves the relevant windows registry key handle        int hKey = getHKeyByLevel(regLevel);                if (WinRegistryWrapper.WinRegDeleteKey(hKey, subKey)                != ERROR_SUCCESS) {            throw new RegisterFailedException("Key " + subKey                    + " delete error.");         }    }      /**     * Wrapper method for WinRegistryWrapper.WinRegQueryValueEx.     *     * @param subKey name of the key (not null)     * @param valueName name of the value (not null)     * @param regLevel given regLevel     * @return content of the specified <subKey, valueName>     */    private static String regQueryValueEx(String subKey, String valueName, int regLevel) {        // if the value for the given subkey exist, retieve        if (isValueExist(subKey, valueName, regLevel)) {            int hKey = getHKeyByLevel(regLevel);            return WinRegistryWrapper.WinRegQueryValueEx(hKey, subKey,                    valueName);        }        return null;    }      /**     * Wrapper method for WinRegistryWrapper.WinRegSetValueEx.     *     * @param subKey name of the key (not null)     * @param valueName name of the value (not null)     * @param value content of the value (not null)     * @param regLevel given regLevel     * @throws RegisterFailedException if the operation fails.     */    private static void regSetValueEx(String subKey, String valueName, String value,         int regLevel) throws RegisterFailedException {        // if the given subkey does not exists, create it first        if (!isSubKeyExist(subKey, regLevel)) {            regCreateKeyEx(subKey, regLevel);        }        int hKey = getHKeyByLevel(regLevel);        if (WinRegistryWrapper.WinRegSetValueEx(hKey, subKey, valueName,                value) != ERROR_SUCCESS) {            throw new RegisterFailedException("Value:" + " valueName"                    + " setting error");        }    }      /**     * Wrapper method for WinRegistryWrapper.WinRegGetSubKeys.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -