KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdic > filetypes > AssociationService


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;
22
23 import java.net.URL JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import org.jdesktop.jdic.filetypes.internal.AppAssociationWriter;
27 import org.jdesktop.jdic.filetypes.internal.AppAssociationWriterFactory;
28 import org.jdesktop.jdic.filetypes.internal.AppAssociationReader;
29 import org.jdesktop.jdic.filetypes.internal.AppAssociationReaderFactory;
30 import org.jdesktop.jdic.filetypes.internal.AppUtility;
31 import org.jdesktop.jdic.init.JdicInitException;
32 import org.jdesktop.jdic.init.JdicManager;
33
34
35 /**
36  * The <code>AssociationService</code> class provides several methods to access
37  * the file type associations. It includes methods to retrieve a particular
38  * file type association, to register a new file type association into the system,
39  * and to unregister a registered file type association from the system.
40  * Each file type association is represented by an <code>Association</code> object.
41  * <p>
42  * The file type information storage and access mechanism is platform-dependent:
43  * <ul>
44  * <li> For Microsoft Windows platforms, the file type information is stored
45  * in the registry, which is a tree structure.
46  * <li> For Gnome/UNIX platforms, the file type information is stored in the
47  * MIME type database, which consists of some plain text files
48  * (.keys, .mime, .applications). Each MIME text file could contain one
49  * or multiple file types. A file type could be registered in the system
50  * by providing new MIME files, or being added to existing MIME files.
51  * </ul>
52  *
53  * @see Association
54  * @see Action
55  */

56 public class AssociationService {
57     // A platform-dependent instance of AppAssociationReader.
58
private AppAssociationReader appAssocReader;
59     // A platform-dependent instance of AppAssociationWriter.
60
private AppAssociationWriter appAssocWriter;
61   
62     // Add the initialization code from package org.jdesktop.jdic.init.
63
// To set the environment variables or initialize the set up for
64
// native libraries and executable files.
65
static {
66         try {
67             JdicManager jm = JdicManager.getManager();
68             jm.initShareNative();
69         } catch (JdicInitException e){
70             e.printStackTrace();
71         }
72     }
73
74     /**
75      * Constructor of an <code>AssociationService</code> object.
76      */

77     public AssociationService() {
78         appAssocReader = AppAssociationReaderFactory.newInstance();
79         appAssocWriter = AppAssociationWriterFactory.newInstance();
80     }
81   
82     /**
83      * Returns the association representing the file type of the
84      * given MIME type.
85      *
86      * @param mimeType a given MIME type name.
87      * @return the appropriate <code>Association</code> object; <code>null</code>
88      * if the given MIME type is not found in the system.
89      */

90     public Association getMimeTypeAssociation(String JavaDoc mimeType) {
91         if (mimeType == null) {
92             throw new IllegalArgumentException JavaDoc("The specified mime type is null");
93         }
94
95         // Check whether the given mime type exists/is registered in the system.
96
if (!appAssocReader.isMimeTypeExist(mimeType)) {
97             return null;
98         }
99         
100         // Get the association associated with the mime type.
101
Association assoc = new Association();
102         List JavaDoc fileExtList = appAssocReader.getFileExtListByMimeType(mimeType);
103         String JavaDoc iconFileName = appAssocReader.getIconFileNameByMimeType(mimeType);
104         String JavaDoc description = appAssocReader.getDescriptionByMimeType(mimeType);
105         List JavaDoc actionList = appAssocReader.getActionListByMimeType(mimeType);
106       
107         assoc.setMimeType(mimeType);
108
109         if (fileExtList != null) {
110             Iterator JavaDoc iter = fileExtList.iterator();
111
112             if (iter != null) {
113                 while (iter.hasNext()) {
114                     assoc.addFileExtension((String JavaDoc) iter.next());
115                 }
116             }
117         }
118         
119         if (iconFileName != null) {
120             assoc.setIconFileName(iconFileName);
121         }
122         
123         if (description != null) {
124             assoc.setDescription(description);
125         }
126       
127         if (actionList != null) {
128             Iterator JavaDoc iter = actionList.iterator();
129
130             if (iter != null) {
131                 while (iter.hasNext()) {
132                     assoc.addAction((Action) iter.next());
133                 }
134             }
135         }
136         
137         return assoc;
138     }
139   
140     /**
141      * Returns the association representing the file type of the given
142      * file extension.
143      * <p>
144      * The file extension list in the returned <code>Association</code> object
145      * contains only this given file extension.
146      *
147      * @param fileExt a given file extension name.
148      * @return the appropriate <code>Association</code> object; <code>null</code>
149      * if the given file extension is not found in the system.
150      */

151     public Association getFileExtensionAssociation(String JavaDoc fileExt) {
152         if (fileExt == null) {
153             throw new IllegalArgumentException JavaDoc("The specified file extension is null");
154         }
155
156         // Add the leading '.' character to the given file extension if not exists.
157
fileExt = AppUtility.addDotToFileExtension(fileExt);
158
159         // Check whether the given file extension exists/is registered in the system.
160
if (!appAssocReader.isFileExtExist(fileExt)) {
161             return null;
162         }
163         
164         // Get the association associated with the file extension.
165
Association assoc = new Association();
166         String JavaDoc mimeType = appAssocReader.getMimeTypeByFileExt(fileExt);
167         String JavaDoc iconFileName = appAssocReader.getIconFileNameByFileExt(fileExt);
168         String JavaDoc description = appAssocReader.getDescriptionByFileExt(fileExt);
169         List JavaDoc actionList = appAssocReader.getActionListByFileExt(fileExt);
170       
171         // Do not retrieve other file extensions.
172
assoc.addFileExtension(fileExt);
173         
174         if (iconFileName != null) {
175             assoc.setIconFileName(iconFileName);
176         }
177         
178         if (mimeType != null) {
179             assoc.setMimeType(mimeType);
180         }
181         
182         if (description != null) {
183             assoc.setDescription(description);
184         }
185       
186         if (actionList != null) {
187             Iterator JavaDoc iter = actionList.iterator();
188
189             if (iter != null) {
190                 while (iter.hasNext()) {
191                     assoc.addAction((Action) iter.next());
192                 }
193             }
194         }
195         
196         return assoc;
197     }
198   
199     /**
200      * Returns the association representing the file type of the file the given
201      * URL points to.
202      *
203      * @param url a given URL.
204      * @return the appropriate <code>Association</code> object; <code>null</code>
205      * if the file type of the file the given URL points to is not
206      * found in the system.
207      */

208     public Association getAssociationByContent(URL JavaDoc url) {
209         if (url == null) {
210             throw new IllegalArgumentException JavaDoc("The specified URL is null");
211         }
212         
213         Association assoc = null;
214         String JavaDoc mimeType = appAssocReader.getMimeTypeByURL(url);
215
216         if (mimeType != null) {
217             // Get association by mime type.
218
assoc = getMimeTypeAssociation(mimeType);
219         }
220         
221         if (assoc == null) {
222             // Get association by file extension.
223
String JavaDoc fileExt = AppUtility.getFileExtensionByURL(url);
224
225             if (fileExt != null) {
226                 assoc = getFileExtensionAssociation(fileExt);
227             }
228         }
229             
230         return assoc;
231     }
232   
233     /**
234      * Registers the given association in the user specific level.
235      * <p>
236      * <ul>
237      * <li> For Microsoft Windows platforms: the file extension list and MIME
238      * type can't both be null. If any of the description, icon file name, action
239      * list fields is not null, the file extension list couldn't be empty.
240      * <p>
241      * For Windows NT, Windows Me/98/95: the registration is always system
242      * wide, since all users share the same association information.
243      * <p>
244      * For Windows 2000 and Windows XP: the registration is only applied to
245      * this specific user.
246      *
247      * <li> For Gnome/Unix platforms: both the name and MIME type fields need to
248      * be specified to perform this operation.
249      * </ul>
250      *
251      * @param assoc a given <code>Association</code> object.
252      * @throws IllegalArgumentException if the given association is not valid for
253      * this operation.
254      * @throws AssociationAlreadyRegisteredException if the given association
255      * already exists in the system.
256      * @throws RegisterFailedException if the given association fails to be
257      * registered in the system.
258      */

259     public void registerUserAssociation(Association assoc)
260             throws AssociationAlreadyRegisteredException, RegisterFailedException {
261         if (assoc == null) {
262             throw new IllegalArgumentException JavaDoc("The specified association is null");
263         }
264
265         // Check whether the specified association is valid for registration.
266
try {
267             appAssocWriter.checkAssociationValidForRegistration(assoc);
268         } catch (IllegalArgumentException JavaDoc e) {
269             throw e;
270         }
271         
272         // Check whether the specified association already exists.
273
if (appAssocWriter.isAssociationExist(assoc, AppAssociationWriter.USER_LEVEL)) {
274             throw new AssociationAlreadyRegisteredException("Assocation already exists!");
275         }
276
277         // Perform registration.
278
appAssocWriter.registerAssociation(assoc, AppAssociationWriter.USER_LEVEL);
279     }
280
281     /**
282      * Unregisters the given association in the user specific level.
283      * <p>
284      * <ul>
285      * <li> For Microsoft Windows platforms: either the MIME type or the file extension
286      * list field needs to be specified to perform this operation.
287      * <p>
288      * For Windows NT, Windows Me/98/95: the unregistration is always system wide,
289      * since all users share the same association information.
290      * <p>
291      * For Windows 2000 and Windows XP: the unregistration is only applied to
292      * this specific user.
293      *
294      * <li> For Gnome/Unix platforms: only the name field needs to be specified to
295      * perform this operation.
296      * </ul>
297      * <P>
298      *
299      * @param assoc a given Association object.
300      * @throws IllegalArgumentException if the given association is not valid for
301      * this operation.
302      * @throws AssociationNotRegisteredException if the given association doesn't
303      * exist in the system.
304      * @throws RegisterFailedException if the given association fails to be
305      * unregistered in the system.
306      */

307     public void unregisterUserAssociation(Association assoc)
308             throws AssociationNotRegisteredException, RegisterFailedException {
309         if (assoc == null) {
310             throw new IllegalArgumentException JavaDoc("The specified association is null");
311         }
312
313         // Check whether the specified association is valid for unregistration.
314
try {
315             appAssocWriter.checkAssociationValidForUnregistration(assoc);
316         } catch (IllegalArgumentException JavaDoc e) {
317             throw e;
318         }
319         
320         // Check whether the specified association not exists.
321
if (!appAssocWriter.isAssociationExist(assoc, AppAssociationWriter.USER_LEVEL)) {
322             throw new AssociationNotRegisteredException("Assocation not exists!");
323         }
324
325         // Perform unregistration.
326
appAssocWriter.unregisterAssociation(assoc, AppAssociationWriter.USER_LEVEL);
327     }
328
329     /**
330      * Registers the given association in the system level.
331      * <p>
332      * <ul>
333      * <li> For Microsoft Windows platforms: the file extension list and MIME
334      * type can't all be null. If any of the description, icon file name, action
335      * list fields is not null, the file extension list couldn't be empty.
336      * <p>
337      * For Windows XP: the user needs the administrator permission to
338      * access the system association information in the registry.
339      *
340      * <li> For Gnome/Unix platforms: both the name and MIME type fields need to
341      * be specified to perform this operation.
342      * </ul>
343      *
344      * @param assoc a given <code>Association</code> object.
345      * @throws IllegalArgumentException if the given association is not valid for
346      * this operation.
347      * @throws AssociationAlreadyRegisteredException if the given association
348      * already exists in the system.
349      * @throws RegisterFailedException if the given association fails to be
350      * registered in the system.
351      */

352     public void registerSystemAssociation(Association assoc)
353             throws AssociationAlreadyRegisteredException, RegisterFailedException {
354         if (assoc == null) {
355             throw new IllegalArgumentException JavaDoc("The specified association is null");
356         }
357
358         // Check whether the specified association is valid for registration.
359
try {
360             appAssocWriter.checkAssociationValidForRegistration(assoc);
361         } catch (IllegalArgumentException JavaDoc e) {
362             throw e;
363         }
364         
365         // Check whether the specified association already exists.
366
if (appAssocWriter.isAssociationExist(assoc, AppAssociationWriter.SYSTEM_LEVEL)) {
367             throw new AssociationAlreadyRegisteredException("Assocation already exists!");
368         }
369
370         // Perform registration.
371
appAssocWriter.registerAssociation(assoc, AppAssociationWriter.SYSTEM_LEVEL);
372     }
373
374     /**
375      * Unregisters the given association in the system level.
376      * <p>
377      * <ul>
378      * <li> For Microsoft Windows platforms: either the MIME type or the file extension
379      * list field needs to be specified to perform this operation.
380      * <p>
381      * For Windows XP: the user needs the administrator permission to access the
382      * system association information in the registry.
383      *
384      * <li> For Gnome/Unix platforms: only the name field needs to be specified to
385      * perform this operation.
386      * </ul>
387      * <P>
388      *
389      * @param assoc a given Association object.
390      * @throws IllegalArgumentException if the given association is not valid for
391      * this operation.
392      * @throws AssociationNotRegisteredException if the given association doesn't
393      * exist in the system.
394      * @throws RegisterFailedException if the given association fails to be
395      * unregistered in the system.
396      */

397     public void unregisterSystemAssociation(Association assoc)
398         throws AssociationNotRegisteredException, RegisterFailedException {
399         if (assoc == null) {
400             throw new IllegalArgumentException JavaDoc("The specified association is null");
401         }
402
403         // Check whether the specified association is valid for unregistration.
404
try {
405             appAssocWriter.checkAssociationValidForUnregistration(assoc);
406         } catch (IllegalArgumentException JavaDoc e) {
407             throw e;
408         }
409         
410         // Check whether the specified association not exists.
411
if (!appAssocWriter.isAssociationExist(assoc, AppAssociationWriter.SYSTEM_LEVEL)) {
412             throw new AssociationNotRegisteredException("Assocation not existed!");
413         }
414
415         appAssocWriter.unregisterAssociation(assoc, AppAssociationWriter.SYSTEM_LEVEL);
416     }
417 }
418
Popular Tags