KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdic > filetypes > internal > WinRegistryUtil


1 /*
2  * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is
3  * subject to license terms.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the Lesser GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  */

20
21 package org.jdesktop.jdic.filetypes.internal;
22
23 import java.net.URL JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import org.jdesktop.jdic.filetypes.Action;
28 import org.jdesktop.jdic.filetypes.RegisterFailedException;
29
30
31 /**
32  * Utility class for accessing the system registry and association info for Windows.
33  */

34 public class WinRegistryUtil {
35     
36     /* Draft to illustrate the Registry items relationship
37      HKEY_CLASSES_ROOT
38      | Key Nmae Valuename Value
39      |_.html //file extension "" htmlfile //clsID
40      | Content Type text/html //MIME type (optional)
41      |
42      ... ...
43      |_htmlfile //clsID "" HTML Document //Description
44      | |
45      | |_DefaultIcon "" "c:\program files\..." //Default Icon file name
46      | |
47      | |_shell //shell command list "" opennew //Default shell command (optional)
48      | |
49      | |_Edit //shell command "" Edit(&E) //Command Description
50      | | |
51      | | |_Command // content "" "c:\Program files\..." //real command
52      | |
53      | |_opennew "" Open (&O)
54      | |
55      | |_Command "" "c:\Program files\..."
56      ... ...
57      |_MIME //MIME Info
58      | |
59      | |_Database
60      | |
61      | |_Content Type
62      | |
63      | |_text/html //MIME Extension .htm //corresponding file extension
64      |
65      */

66
67     // Constant value to generate a classID name
68
private static final int MAX_CLSID_NUMBER = 1000;
69   
70     // general constants
71
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 JavaDoc VN_DEFAULT = ""; // default value name
76

77     // Constants for USERKEY & SYSKEY
78
private final static String JavaDoc SYS_USER_KN_PREFIX = "SOFTWARE\\Classes";
79         
80     // Constants for file extension
81
private final static String JavaDoc VN_CONTENT = "Content type";
82     
83     // Constants under clsID item
84
private final static String JavaDoc KN_DEFAULTICON = "DefaultIcon"; // key name to denote the default icon file name
85
private final static String JavaDoc VN_DEFAULTGENERATOR = "Generated By"; //key name to specify the generator
86
private final static String JavaDoc VALUE_DEFAULTGENERATOR = "Generated By Java-Association"; //Value to specify the generator
87
private final static String JavaDoc KN_SHELL = "shell"; // key name for the shell command list
88
private final static String JavaDoc KN_COMMAND = "command"; // key name to denote the actual command string
89
private final static String JavaDoc KN_CURVER = "CurVer"; // key name for CurVer
90

91     // Constants reletive to the MIME information
92
private final static String JavaDoc KN_MIME = "MIME\\Database\\Content Type"; // key name to locate mime type
93
private final static String JavaDoc VN_EXTENSION = "Extension"; // value name to indicate mime type corresponding file extension
94

95     // Constants for the Windows 2000 user added file extension
96
private final static String JavaDoc USER_FILE_EXT_KEY_PREFIX = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts";
97     private final static String JavaDoc USER_FILE_EXT_VALUENAME = "Application";
98     private final static String JavaDoc USER_FILE_EXT_APP_PREFIX = "Applications";
99     
100   
101     // Constants for the level designation
102
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     // Constants for the OS Name
107
private final static String JavaDoc osName = System.getProperty("os.name");
108     private final static String JavaDoc WIN2KOS = "Windows 2000";
109
110     private WinRegistryUtil() {}
111     
112     /**
113      * Gets the corresponding Windows Registry folder name according to the regLevel.
114      *
115      * @param regLevel given regLeve
116      * @return corresponding windows registry folder constants
117      */

118     private static int getHKeyByLevel(int regLevel) {
119         int hKey;
120
121         switch (regLevel) {
122         case USER_LEVEL: /* USER_LEVEL -->HKEY_CURRENT_USER */
123             hKey = WinRegistryWrapper.HKEY_CURRENT_USER;
124             break;
125
126         case SYSTEM_LEVEL: /* SYSTEM_LEVEL-->HKEY_LOCAL_MACHINE */
127             hKey = WinRegistryWrapper.HKEY_LOCAL_MACHINE;
128             break;
129
130         case ROOT_LEVEL: /* ROOT_LEVEL-->HKEY_CLASSES_ROOT */
131             hKey = WinRegistryWrapper.HKEY_CLASSES_ROOT;
132             break;
133             
134         default: /* Default will be HKEY_CLASSES_ROOT */
135             hKey = WinRegistryWrapper.HKEY_CLASSES_ROOT;
136             break;
137         }
138         return hKey;
139     }
140   
141     /**
142      * Constructs a new class ID name based on the given fileExt name
143      * by adding a sequence number at the rear of the given fileExt.
144      *
145      * @param fileExt given file extension (not null)
146      * @param regLevel given regLevel
147      * @return the constructed class ID name string, or null if construction
148      * failed
149      */

150     private static String JavaDoc genClassID(String JavaDoc fileExt, int regLevel) {
151         boolean isClsIDExist = true;
152         String JavaDoc 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             // construct the temporary ClsID
163
temClsID = "class" + temFileExt + appendix;
164             if (temClsID != null) {
165                 // Get the corresponding windows registry key
166
temClsIDKey = getClsIDKey(temClsID, regLevel);
167                 if (temClsIDKey != null) {
168                     // Check if this key exists
169
isClsIDExist = isSubKeyExist(temClsIDKey, regLevel);
170                     if (!isClsIDExist) {
171                         // Not exists, return the key
172
return temClsID;
173                     }
174                 }
175             }
176             i++;
177         }
178         
179         return null;
180     }
181   
182     /**
183      * Wrapper method for WinRegistryWrapper.WinRegSubKeyExist.
184      *
185      * @param subKey name of the key (not null)
186      * @param regLevel given regLevel
187      * @return true if the subKey exists
188      */

189     private static boolean isSubKeyExist(String JavaDoc 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     /**
201      * Returns true if the specified subkey and value exist.
202      *
203      * @param subKey given key name (not null).
204      * @param valueName given value name (not null).
205      * @param regLevel given regLevel.
206      * @return true if the given key contains the specified valuename.
207      */

208     private static boolean isValueExist(String JavaDoc subKey, String JavaDoc 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     /**
222      * Returns the corresponding mime key in the Windows registry table.
223      *
224      * @param mimeType specified mime type (not null)
225      * @regLeve given regLevel
226      * @return corresponding mime type key
227      */

228     private static String JavaDoc getMimeTypeKey(String JavaDoc mimeType, int regLevel) {
229         // MIME\\Database\\Content Type\\***
230
String JavaDoc mimeSubKey = KN_MIME + "\\" + mimeType;
231        
232         if (regLevel != ROOT_LEVEL) {
233             // software\\classes\\MIME\\Database\\Content Type\\***
234
mimeSubKey = SYS_USER_KN_PREFIX + "\\" + mimeSubKey;
235         }
236         return mimeSubKey;
237     }
238   
239     /**
240      * Returns the corresponding file extension key in the Windows registry table.
241      *
242      * @param fileExt given file extension (not null)
243      * @param regLevel given reglevel
244      * @return corresponding file extension key
245      */

246     private static String JavaDoc getFileExtKey(String JavaDoc fileExt, int regLevel) {
247         String JavaDoc fileExtKey = fileExt;
248
249         if (regLevel != ROOT_LEVEL) {
250             // software\\classes\\***
251
fileExtKey = SYS_USER_KN_PREFIX + "\\" + fileExtKey;
252         }
253         return fileExtKey;
254     }
255   
256     /**
257      * Returns the corresponding class ID key in the Windows registry table.
258      *
259      * @param clsID given class ID (not null)
260      * @param regLevel given regLevel
261      * @return corresponding class ID key
262      */

263     private static String JavaDoc getClsIDKey(String JavaDoc clsID, int regLevel) {
264         String JavaDoc clsIDKey = clsID;
265
266         if (regLevel != ROOT_LEVEL) {
267             // software\\classes\\***
268
clsIDKey = SYS_USER_KN_PREFIX + "\\" + clsIDKey;
269         }
270         return clsIDKey;
271     }
272
273     /**
274      * Returns the corresponding icon key name of the specified file extension.
275      *
276      * @param fileExt given file extension (not null)
277      * @param regLevel given regLevel
278      * @return corresponding icon key
279      */

280     private static String JavaDoc getIconKey(String JavaDoc fileExt, int regLevel) {
281         // Retrieve the relevant class ID
282
String JavaDoc clsID = getClassIDByFileExt(fileExt, regLevel);
283
284         if (clsID != null) {
285             //* clsID\\DefaultIcon
286
String JavaDoc iconKey = clsID + "\\" + KN_DEFAULTICON;
287             if (regLevel != ROOT_LEVEL) {
288                 // software\\classes\\clsID\\DefaultIcon
289
iconKey = SYS_USER_KN_PREFIX + "\\" + iconKey;
290             }
291             return iconKey;
292         }
293         return null;
294     }
295   
296     /**
297      * Wrapper method for WinRegistryWrapper.WinRegCreateKeyEx.
298      *
299      * @param subKey name of the key (not null)
300      * @param regLevel given regLevel
301      * @throws RegisterFailedException if the operation fails.
302      */

303     private static void regCreateKeyEx(String JavaDoc subKey, int regLevel)
304         throws RegisterFailedException {
305         // Retrieve the relevant key class
306
int hKey = getHKeyByLevel(regLevel);
307         // Create the subKey under hKey
308
if (WinRegistryWrapper.WinRegCreateKeyEx(hKey, subKey)
309                 != ERROR_SUCCESS) {
310             throw new RegisterFailedException("Key " + subKey
311                     + " creation error!");
312         }
313     }
314   
315     /**
316      * Wrapper method for WinRegistryWrapper.WinRegDeleteKey.
317      *
318      * @param subKey name of the key (not null)
319      * @param regLevel given registry level
320      * @throws RegisterFailedException if the operation fails.
321      */

322     private static void regDeleteKey(String JavaDoc subKey, int regLevel)
323         throws RegisterFailedException {
324         // Retrieves the relevant windows registry key handle
325
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     /**
335      * Wrapper method for WinRegistryWrapper.WinRegQueryValueEx.
336      *
337      * @param subKey name of the key (not null)
338      * @param valueName name of the value (not null)
339      * @param regLevel given regLevel
340      * @return content of the specified <subKey, valueName>
341      */

342     private static String JavaDoc regQueryValueEx(String JavaDoc subKey, String JavaDoc valueName, int regLevel) {
343         // if the value for the given subkey exist, retieve
344
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     /**
354      * Wrapper method for WinRegistryWrapper.WinRegSetValueEx.
355      *
356      * @param subKey name of the key (not null)
357      * @param valueName name of the value (not null)
358      * @param value content of the value (not null)
359      * @param regLevel given regLevel
360      * @throws RegisterFailedException if the operation fails.
361      */

362     private static void regSetValueEx(String JavaDoc subKey, String JavaDoc valueName, String JavaDoc value,
363         int regLevel) throws RegisterFailedException {
364         // if the given subkey does not exists, create it first
365
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     /**
378      * Wrapper method for WinRegistryWrapper.WinRegGetSubKeys.
379      *
380      * @param subKey name of the key (not null)
381      * @param regLevel given regLevel
382      * @return string array containing the sub keys
383      * @exception RegisterFailedException
384      */

385     private static String JavaDoc[] regGetSubKeys(String JavaDoc subKey, int regLevel) {
386         int hKey = getHKeyByLevel(regLevel);
387
388         return WinRegistryWrapper.WinRegGetSubKeys(hKey, subKey,
389                 MAX_KEY_LENGTH);
390     }
391   
392     /**
393      * Gets the default value for a specified key.
394      *
395      * @param subKey Name of the key (not null)
396      * @param regLevel given regLevel
397      * @return content of the default value
398      * @exception RegisterFailedException
399      */

400     private static String JavaDoc getDefaultValue(String JavaDoc subKey, int regLevel) {
401         int hKey = getHKeyByLevel(regLevel);
402
403         return WinRegistryWrapper.WinRegQueryValueEx(hKey, subKey,
404                 VN_DEFAULT);
405     }
406   
407     /**
408      * Sets the default value for a specified key.
409      *
410      * @param subKey Name of the key (not null)
411      * @param value Value to be set (not null)
412      * @param regLevel given regLevel
413      * @throws RegisterFailedException if the operation fails.
414      */

415     private static void setDefaultValue(String JavaDoc subKey, String JavaDoc value, int regLevel)
416         throws RegisterFailedException {
417         // if the specified subKey does not exist, create it first
418
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     /**
432      * Writes an action into the registry table
433      * (From the specified registry folder).
434      *
435      * @param action given action to be added (not null)
436      * @param clsID given class ID (not null)
437      * @param regLevel given regLevel
438      * @throws RegisterFailedException if the operation fails
439      */

440     private static void addActionByClsID(Action action, String JavaDoc clsID, int regLevel)
441         throws RegisterFailedException {
442         String JavaDoc verb = action.getVerb();
443         String JavaDoc desc = action.getDescription();
444         String JavaDoc cmd = action.getCommand();
445     
446         String JavaDoc clsIDKey = getClsIDKey(clsID, regLevel);
447         String JavaDoc shellKey = clsIDKey + "\\" + KN_SHELL;
448         String JavaDoc verbKey = shellKey + "\\" + verb;
449         String JavaDoc 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     /**
464      * Returns the mime type associated with the given file extension
465      * (From the given registry folder).
466      *
467      * @param fileExt given file extension (not null)
468      * @param regLevel given regLevel
469      * @return corresponding mime type, or null if not exists
470      */

471     public static String JavaDoc getMimeTypeByFileExt(String JavaDoc fileExt, int regLevel) {
472         String JavaDoc fileExtKey = getFileExtKey(fileExt, regLevel);
473         if (fileExtKey != null) {
474             return regQueryValueEx(fileExtKey, VN_CONTENT, regLevel);
475         } else {
476             return null;
477         }
478     }
479   
480     /**
481      * Returns the mime type information associated with the given file extension
482      * (From HKEY_ROOT registry level).
483      *
484      * @param fileExt given file extension (not null)
485      * @return corresponding mime type, or null if not exists
486      */

487     public static String JavaDoc getMimeTypeByFileExt(String JavaDoc fileExt) {
488         return (getMimeTypeByFileExt(fileExt, ROOT_LEVEL));
489     }
490
491     /**
492      * Sets the mime type associated with the given file extension.
493      *
494      * @param mimeType given mime type (not null)
495      * @param fileExt given file extension (not null)
496      * @param regLevel given reglevel
497      * @throws RegisterFailedException if the given operation fails.
498      */

499     public static void setMimeTypeByFileExt(String JavaDoc mimeType, String JavaDoc fileExt,
500         int regLevel) throws RegisterFailedException {
501         String JavaDoc fileExtKey = getFileExtKey(fileExt, regLevel);
502         if (fileExtKey != null) {
503             // set the content value
504
regSetValueEx(fileExtKey, VN_CONTENT, mimeType, regLevel);
505         }
506     }
507   
508     /**
509      * Returns the file extensione associated with the given mime type
510      * (From the given registry folder).
511      *
512      * @param mimeType given mime type (not null)
513      * @param regLevel given reglevel
514      * @return corresponding file extension, or null if none
515      */

516     public static String JavaDoc getFileExtByMimeType(String JavaDoc mimeType, int regLevel) {
517         String JavaDoc mimeSubKey = getMimeTypeKey(mimeType, regLevel);
518         if (mimeSubKey != null) {
519             return regQueryValueEx(mimeSubKey, VN_EXTENSION, regLevel);
520         } else {
521             return null;
522         }
523     }
524   
525     /**
526      * Returns the file extension associated with the given mime type
527      * (From HKEY_ROOT registry folder).
528      *
529      * @param mimeType given mime type (not null)
530      * @return corresponding file extension, or null if none
531      */

532     public static String JavaDoc getFileExtByMimeType(String JavaDoc mimeType) {
533         return (getFileExtByMimeType(mimeType, ROOT_LEVEL));
534     }
535
536     /**
537      * Sets the file extensione associated with the given mime type.
538      *
539      * @param fileExt given file extension (not null)
540      * @param mimeType given mime type (not null)
541      * @param regLevel given regLevel
542      * @throws RegisterFailedException if the operation fails.
543      */

544     public static void setFileExtByMimeType(String JavaDoc fileExt, String JavaDoc mimeType, int regLevel)
545         throws RegisterFailedException {
546         String JavaDoc mimeSubKey = getMimeTypeKey(mimeType, regLevel);
547         if (mimeSubKey != null) {
548             regSetValueEx(mimeSubKey, VN_EXTENSION, fileExt, regLevel);
549         }
550     }
551   
552     /**
553      * Returns the icon file name associated with the given file extension.
554      *
555      * @param fileExt given file extension (not null)
556      * @param regLevel given regLevel
557      * @return corresponding icon file, or null if none
558      */

559     public static String JavaDoc getIconFileNameByFileExt(String JavaDoc fileExt, int regLevel) {
560         // Retrieve the icon key
561
String JavaDoc iconKey = getIconKey(fileExt, regLevel);
562         if (iconKey == null) {
563             return null;
564         }
565         String JavaDoc unDealedFileName = getDefaultValue(iconKey, regLevel);
566         if (unDealedFileName == null) {
567             return null;
568         }
569         return ExpandEnvironmentStrings(unDealedFileName);
570     }
571     
572     /**
573      * Retrievs the icon file of the specified file extension (From HKEY_ROOT
574      * folder).
575      *
576      * @param fileExt
577      * given file extension (not null)
578      * @return corresponding icon file, or null if none
579      */

580     public static String JavaDoc getIconFileNameByFileExt(String JavaDoc fileExt) {
581         return (getIconFileNameByFileExt(fileExt, ROOT_LEVEL));
582     }
583
584     /**
585      * Sets the icon file name associated with the given file extension.
586      *
587      * @param iconFileName given icon file name (not null)
588      * @param fileExt given file extension name (not null)
589      * @param regLevel given regLevel
590      * @throws RegisterFailedException if the operation fails.
591      */

592     public static void setIconFileNameByFileExt(String JavaDoc iconFileName, String JavaDoc fileExt,
593         int regLevel) throws RegisterFailedException {
594         // Get the icon key
595
String JavaDoc iconKey = getIconKey(fileExt, regLevel);
596         if (iconKey == null) {
597             // If the classID do not created yet, create it first
598
String JavaDoc 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             // Set the default value of the iconkye as the iconfile
606
setDefaultValue(iconKey, iconFileName, regLevel);
607         }
608     }
609
610     /**
611      * Returns the description associated with the given file extension.
612      *
613      * @param fileExt given file extension (not null)
614      * @param regLevel given regLevel
615      * @return corresponding description about the file extension, or null if none
616      */

617     public static String JavaDoc getDescriptionByFileExt(String JavaDoc fileExt, int regLevel) {
618         // Retrievs the class ID
619
String JavaDoc classID = getClassIDByFileExt(fileExt, regLevel);
620         if (classID != null) {
621             String JavaDoc clsIDKey = getClsIDKey(classID, regLevel);
622             if (clsIDKey != null) {
623                 // The default value of the class ID key is the description
624
return getDefaultValue(clsIDKey, regLevel);
625             }
626         }
627         return null;
628     }
629   
630     /**
631      * Returns the description of the given file extension
632      * (From HKEY_ROOT registry folder).
633      *
634      * @param fileExt given file extension (not null)
635      * @return corresponding description about the file extension, or null if none
636      */

637     public static String JavaDoc getDescriptionByFileExt(String JavaDoc fileExt) {
638         return (getDescriptionByFileExt(fileExt, ROOT_LEVEL));
639     }
640
641     /**
642      * Sets the Description associated with the given file extension.
643      *
644      * @param description given description (not null)
645      * @param fileExt given file extension name (not null)
646      * @param regLevel given registeration level
647      * @throws RegisterFailedException if the operation fails.
648      */

649     public static void setDescriptionByFileExt(String JavaDoc description, String JavaDoc fileExt,
650         int regLevel) throws RegisterFailedException {
651         String JavaDoc classID = getClassIDByFileExt(fileExt, regLevel);
652         if (classID == null) {
653             // If the classID does not exist, create it first
654
classID = genClassID(fileExt, regLevel);
655             if (classID != null) {
656                 setClassIDByFileExt(fileExt, classID, regLevel);
657             }
658         }
659         if (classID != null) {
660             String JavaDoc clsIDKey = getClsIDKey(classID, regLevel);
661             if (clsIDKey != null) {
662                 // Default value of the class ID key will be the description
663
setDefaultValue(clsIDKey, description, regLevel);
664             }
665         }
666     }
667
668     /**
669      * Marks the generator value field of the classID key of the given file extension.
670      *
671      * @param fileExt given file extension name (not null)
672      * @param regLevel given registeration level
673      * @throws RegisterFailedException if the operation fails.
674      */

675     public static void markGeneratorByFileExt(String JavaDoc fileExt, int regLevel)
676         throws RegisterFailedException {
677         //Get the corresponding class ID key
678
String JavaDoc clsID = getClassIDByFileExt(fileExt, regLevel);
679         String JavaDoc clsIDKey = getClsIDKey(clsID, regLevel);
680         if (clsIDKey != null) {
681             regSetValueEx(clsIDKey, VN_DEFAULTGENERATOR, VALUE_DEFAULTGENERATOR, regLevel);
682         }
683     }
684     
685     /**
686      * Returns the action list associated with the given file extension
687      * (From specified registiry folder).
688      *
689      * @param fileExt given file extension (not null)
690      * @return the action list
691      */

692     public static List JavaDoc getActionListByFileExt(String JavaDoc fileExt, int regLevel) {
693         List JavaDoc actionList = null;
694     
695         // Retrievs the relevant class ID
696
String JavaDoc clsID = getClassIDByFileExt(fileExt, regLevel);
697
698         if (clsID!= null) {
699             String JavaDoc clsIDKey = getClsIDKey(clsID, regLevel);
700             String JavaDoc shellKey = clsIDKey + "\\" + KN_SHELL;
701             String JavaDoc verbs[] = null;
702             if (shellKey != null) {
703                 verbs = regGetSubKeys(shellKey, regLevel);
704             }
705
706             if (verbs != null) {
707                 int verbsNum = verbs.length;
708                 // Construct relevant actions one by one
709
if (verbsNum > 0) {
710                     actionList = new ArrayList JavaDoc();
711                     for (int i = 0; i < verbsNum; i++) {
712                         String JavaDoc verbKey = shellKey + "\\" + verbs[i];
713                         String JavaDoc cmdKey = verbKey + "\\" + KN_COMMAND;
714                         if (cmdKey != null) {
715                             Action oneAction;
716                             String JavaDoc temCmd = getDefaultValue(cmdKey, regLevel);
717                             //In case cmd is a null string, we shall replace it with a empty string
718
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     /**
735      * Returns the action list associated with the specified file extension
736      * (From HKEY_ROOT).
737      *
738      * @param fileExt given file extension (not null)
739      * @return the action list
740      */

741     public static List JavaDoc getActionListByFileExt(String JavaDoc fileExt) {
742         List JavaDoc rootActionList = getActionListByFileExt(fileExt, ROOT_LEVEL);
743         List JavaDoc userDefinedList = getUserAddedActionListByFileExt(fileExt);
744         if (userDefinedList != null) {
745             return userDefinedList;
746         } else {
747             return rootActionList;
748         }
749     }
750     
751     /**
752      * Returns the action list associated with the user defined file extension
753      * <p>
754      * <B>Note:</B> Windows 2000 will save user added file extension under a
755      * special place.
756      * For example, user add a new file extension .aoo and specify notepad.exe
757      * as the default application for this file type.
758      * The file extension information will be stored at
759      * HKEY_CURRENT_USER\software\Microsoft\Windows\CurrentVersion\
760      * Explorer\FileExts\.aoo
761      * with data as
762      * Application REG_SZ notepad.exe
763      * the notepad.exe will be stored at
764      * HKEY_CLASSES_ROOT\Applications\notepad.exe
765      * <p>
766      *
767      * @param fileExt given file extension(no null)
768      * @return the action list
769      */

770     private static List JavaDoc getUserAddedActionListByFileExt(String JavaDoc fileExt) {
771         String JavaDoc fileExtKey = USER_FILE_EXT_KEY_PREFIX + "\\" + fileExt;
772         String JavaDoc valueName = USER_FILE_EXT_VALUENAME;
773         int hKey = WinRegistryWrapper.HKEY_CURRENT_USER;
774         //Get application name, e.g. notepad.exe
775
String JavaDoc appName = WinRegistryWrapper.WinRegQueryValueEx(hKey, fileExtKey, valueName);
776         //Get the corresponding application's shell keys
777
String JavaDoc verbs[] = null;
778         String JavaDoc appShellKey = USER_FILE_EXT_APP_PREFIX + "\\" + appName + "\\" + KN_SHELL;
779         hKey = WinRegistryWrapper.HKEY_CLASSES_ROOT;
780         verbs = WinRegistryWrapper.WinRegGetSubKeys(hKey, appShellKey, 255);
781         
782         //Construct relevant actions one by one
783
List JavaDoc actionList = null;
784         if (verbs != null) {
785             int verbsNum = verbs.length;
786             if (verbsNum > 0) {
787                 actionList = new ArrayList JavaDoc();
788                 for (int i = 0; i < verbsNum; i++) {
789                     String JavaDoc verbKey = appShellKey + "\\" + verbs[i];
790                     String JavaDoc cmdKey = verbKey + "\\" + KN_COMMAND;
791                     if (cmdKey != null) {
792                         Action oneAction;
793                         String JavaDoc temCmd = getDefaultValue(cmdKey, ROOT_LEVEL);
794                         //In case cmd is a null string, we shall replace it with a empty string
795
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     /**
811      * Sets the action list associated with the given file extension.
812      *
813      * @param actionList given action list (not null)
814      * @param fileExt given file extension (not null)
815      * @param regLevel given regLevel
816      * @throws RegisterFailedException if the operation fails.
817      */

818     public static void setActionListByFileExt(List JavaDoc actionList, String JavaDoc fileExt,
819         int regLevel) throws RegisterFailedException {
820         // Retrieves the corresponding class ID
821
String JavaDoc clsID = getClassIDByFileExt(fileExt, regLevel);
822         if (clsID == null) {
823             // If the classID does not exist, create it first
824
clsID = genClassID(fileExt, regLevel);
825             if (clsID != null) {
826                 // Bundle the file extension and class ID
827
setClassIDByFileExt(fileExt, clsID, regLevel);
828             }
829         }
830         
831         Action oneAction;
832         if (clsID != null) {
833             if (actionList != null) {
834                 Iterator JavaDoc actionIter = actionList.iterator();
835                 // Add action to under the class ID key one by one
836
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     /**
847      * Returns the mime type associated with the given URL
848      * by checking the content of the URL.
849      *
850      * @param url given URL (not null)
851      * @return corresponding mime type
852      */

853     public static String JavaDoc getMimeTypeByURL(URL JavaDoc url) {
854         return WinRegistryWrapper.WinFindMimeFromData(url);
855     }
856
857     /**
858      * Expands environment-variable strings and replaces them with their defined values
859      * E.g: "%SystemRoot%\\system32\\NOTEPAD.EXE %1" -> "C:\\system32\\NOTEPAD.EXE %1".
860      *
861      * @param cmdString given command string (not null)
862      * @return String
863      */

864     public static String JavaDoc ExpandEnvironmentStrings(String JavaDoc cmdString) {
865         return WinRegistryWrapper.WinExpandEnvironmentStrings(cmdString);
866     }
867   
868     /**
869      * Returns true if the specified mime type exists in the Windows registry table.
870      *
871      * @param mimeType given mime type (not null)
872      * @param regLevel given regLevel
873      * @return true if the mime type exists in the registry table
874      */

875     public static boolean isMimeTypeExist(String JavaDoc mimeType, int regLevel) {
876         // Retrieve the relevant mime type key
877
String JavaDoc mimeTypeKey = getMimeTypeKey(mimeType, regLevel);
878         if (mimeTypeKey != null) {
879             // Checks if the mime type key exists
880
return isSubKeyExist(mimeTypeKey, regLevel);
881         } else {
882             return false;
883         }
884     }
885   
886     /**
887      * Returns true if the mime type exists in the default win registry folder.
888      *
889      * @param mimeType given mimeType (not null)
890      * @return true if the mime type exists in the registry table
891      */

892     public static boolean isMimeTypeExist(String JavaDoc mimeType) {
893         return (isMimeTypeExist(mimeType, ROOT_LEVEL));
894     }
895   
896     /**
897      * Returns true if the specified file extension exists in the given registry folder.
898      *
899      * @param fileExt given file extension (not null)
900      * @param regLevel given regLevel
901      * @return true if the file extension exists in the registry table
902      */

903     public static boolean isFileExtExist(String JavaDoc fileExt, int regLevel) {
904         // Retrieve the relevant file extension key
905
String JavaDoc fileExtKey = getFileExtKey(fileExt, regLevel);
906         if (fileExtKey != null) {
907             // check if this key exists
908
//For windows 2000, we still need to check if the file ext in
909
//Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts
910
return (isSubKeyExist(fileExtKey, regLevel) || isWin2kUserDefinedFileExtExist(fileExt));
911         } else {
912             return false;
913         }
914     }
915     
916     /**
917      * Returns true if the specified file extension exists in the
918      * \\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts in Windows 2000.
919      *
920      * @param fileExt The given file extension
921      * @return true if the file extension exists.
922      */

923     public static boolean isWin2kUserDefinedFileExtExist(String JavaDoc fileExt) {
924         boolean win2kFileDefinedByUser = false;
925         if (osName.equalsIgnoreCase(WIN2KOS)) {
926             String JavaDoc fileExtKey = USER_FILE_EXT_KEY_PREFIX + "\\" + fileExt;
927             win2kFileDefinedByUser = isSubKeyExist(fileExtKey, USER_LEVEL);
928         }
929         return win2kFileDefinedByUser;
930     }
931     
932     /**
933      * Checks if the speicifed file extension exists in the default registry folder.
934      *
935      * @param fileExt given file extension (not null)
936      * @return true if the file extension exists in the registry table
937      */

938     public static boolean isFileExtExist(String JavaDoc fileExt) {
939         return (isFileExtExist(fileExt, ROOT_LEVEL));
940     }
941
942     /**
943      * Adds a new file extension entry in the Windows registry table under the specified folder.
944      *
945      * @param fileExt name of the new file extension (not null)
946      * @param regLevel given regLevel
947      * @throws RegisterFailedException if the operation fails.
948      */

949     public static void addFileExt(String JavaDoc fileExt, int regLevel)
950         throws RegisterFailedException {
951         // Retrieve the relevant file extension key
952
String JavaDoc fileExtKey = getFileExtKey(fileExt, regLevel);
953         if (fileExtKey != null) {
954             // Create this key in the windows registry table
955
regCreateKeyEx(fileExtKey, regLevel);
956         }
957     }
958   
959     /**
960      * Removes the specified file extension entry from the given folder in the Windows registry table.
961      *
962      * @param fileExt given file extension (not null)
963      * @param regLevel given regLevel
964      * @throws RegisterFailedException if the operation fails.
965      */

966     public static void removeFileExt(String JavaDoc fileExt, int regLevel)
967         throws RegisterFailedException {
968         if (isFileExtExist(fileExt, regLevel)) {
969             // Retrieve the relevant file extension key in Windows regtable
970
String JavaDoc fileExtKey = getFileExtKey(fileExt, regLevel);
971             //Check if we should remove the classID key
972
String JavaDoc clsID = getClassIDByFileExt(fileExt, regLevel);
973             if (clsID != null) {
974                 String JavaDoc clsIDKey = getClsIDKey(clsID, regLevel);
975                 if (clsIDKey != null) {
976                     String JavaDoc value_generator = regQueryValueEx(clsIDKey, VN_DEFAULTGENERATOR, regLevel);
977                     if (value_generator.compareTo(VALUE_DEFAULTGENERATOR) == 0) {
978                         //The classID is generated by us, we should remove the clssID
979
regDeleteKey(clsIDKey, regLevel);
980                     }
981                 }
982             }
983             if (fileExtKey != null) {
984                 // Delete the key from registry table
985
if (isSubKeyExist(fileExtKey, regLevel)) {
986                     regDeleteKey(fileExtKey, regLevel);
987                 }
988             }
989             //For windows 2000, we still need to check if the file ext in
990
//Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts
991
if (isWin2kUserDefinedFileExtExist(fileExt)) {
992                 fileExtKey = USER_FILE_EXT_KEY_PREFIX + "\\" + fileExt;
993                 regDeleteKey(fileExtKey, USER_LEVEL);
994             }
995         }
996     }
997
998     /**
999      * Adds a new MIME type entrty in the Windows registry table under the given folder.
1000     *
1001     * @param mimeType given mime type (not null)
1002     * @param regLevel given regLevel
1003     * @throws RegisterFailedException if the operation fails.
1004     */

1005    public static void addMimeType(String JavaDoc mimeType, int regLevel)
1006        throws RegisterFailedException {
1007        // Retrieve the relevant mime type key
1008
String JavaDoc temMimeKey = getMimeTypeKey(mimeType, regLevel);
1009        if (temMimeKey != null) {
1010            // Create this mime key
1011
regCreateKeyEx(temMimeKey, regLevel);
1012        }
1013    }
1014  
1015    /**
1016     * Removes the specified mime type entry in the Windows registy table from the given folder.
1017     *
1018     * @param mimeType given mime type (not null)
1019     * @param regLevel given regLevel
1020     * @throws RegisterFailedException if the operation fails.
1021     */

1022    public static void removeMimeType(String JavaDoc mimeType, int regLevel)
1023        throws RegisterFailedException {
1024        if (isMimeTypeExist(mimeType, regLevel)) {
1025            // Retrieve the relvant mime type key
1026
String JavaDoc mimeKey = getMimeTypeKey(mimeType, regLevel);
1027            if (mimeKey != null) {
1028                // Delete the key from the registry table
1029
regDeleteKey(mimeKey, regLevel);
1030            }
1031        }
1032    }
1033
1034    /**
1035     * Sets the class ID value of the specified file extension under the given folder.
1036     *
1037     * @param fileExt given file extension (not null)
1038     * @param classID given class ID value (not null)
1039     * @param regLevel given regLevel
1040     * @throws RegisterFailedException if the operation fails.
1041     */

1042    public static void setClassIDByFileExt(String JavaDoc fileExt, String JavaDoc classID, int regLevel)
1043        throws RegisterFailedException {
1044        String JavaDoc fileExtKey = getFileExtKey(fileExt, regLevel);
1045        String JavaDoc clsIDKey = getClsIDKey(classID, regLevel);
1046        if (fileExtKey != null) {
1047            /* If the file extension does not exist in the registry table
1048             * Add it first
1049             */

1050            if (!isSubKeyExist(fileExtKey, regLevel)) {
1051                addFileExt(fileExt, regLevel);
1052            }
1053            /* If the classID does not exist in the registry table
1054             * Add it first
1055             */

1056            if (!isSubKeyExist(clsIDKey, regLevel)) {
1057                if (clsIDKey != null) {
1058                    regCreateKeyEx(clsIDKey, regLevel);
1059                }
1060            }
1061            setDefaultValue(fileExtKey, classID, regLevel);
1062        }
1063    }
1064  
1065    /**
1066     * Gets the class ID value of the specified fileExt.
1067     *
1068     * @param fileExt given file extension (not null)
1069     * @param regLevel given regLevel
1070     * @return class ID of the this file extension
1071     * @exception RegisterFailedException
1072     */

1073    public static String JavaDoc getClassIDByFileExt(String JavaDoc fileExt, int regLevel) {
1074        //Retrieves the relevant file extension key
1075
String JavaDoc fileExtKey = getFileExtKey(fileExt, regLevel);
1076        if (fileExtKey != null) {
1077           if (isSubKeyExist(fileExtKey, regLevel)) {
1078               //in case of having CurVer key
1079
return getCurVerClassID(getDefaultValue(fileExtKey,regLevel),regLevel);
1080           }
1081        }
1082        return null;
1083    }
1084    
1085    /**
1086     * Gets the real classID in case of having CurVer key
1087     * @param defaultVerClassID default version classID
1088     * @param regLevel given regLevel
1089     * @return CurVerClassID or given classID
1090     */

1091    private static String JavaDoc getCurVerClassID(String JavaDoc defaultVerClassID,
1092                                           int regLevel) {
1093        String JavaDoc curVerClassIDKey = defaultVerClassID + "\\" + KN_CURVER;
1094
1095        if (regLevel != ROOT_LEVEL) {
1096            // software\\classes\\***
1097
curVerClassIDKey = SYS_USER_KN_PREFIX + "\\" + curVerClassIDKey;
1098        }
1099        
1100        if (isSubKeyExist(curVerClassIDKey, regLevel)) {
1101            //key CurVer exists
1102
return getDefaultValue(curVerClassIDKey, regLevel);
1103        } else {
1104            //key CurVer doesn't exist
1105
return defaultVerClassID;
1106        }
1107    }
1108  
1109    /**
1110     * Sets the mutual reference of the mimeType and fileExt.
1111     *
1112     * @param fileExt given file extension (not null)
1113     * @param mimeType given mime type (not null)
1114     * @param regLevel given regLevel
1115     * @throws RegisterFailedException if the operation fails.
1116     */

1117    public static void setMutualRef(String JavaDoc fileExt, String JavaDoc mimeType, int regLevel)
1118        throws RegisterFailedException {
1119        String JavaDoc mimeKey = getMimeTypeKey(mimeType, regLevel);
1120        String JavaDoc 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