📄 associationservice.java
字号:
/*
* 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;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import org.jdesktop.jdic.filetypes.internal.AppAssociationWriter;
import org.jdesktop.jdic.filetypes.internal.AppAssociationReader;
import org.jdesktop.jdic.filetypes.internal.AppUtility;
import org.jdesktop.jdic.init.JdicInitException;
import org.jdesktop.jdic.init.JdicManager;
/**
* The <code>AssociationService</code> class provides several methods to access
* the file type associations. It includes methods to retrieve a particular
* file type association, to register a new file type association into the system,
* and to unregister a registered file type association from the system.
* Each file type association is represented by an <code>Association</code> object.
* <p>
* The file type information storage and access mechanism is platform-dependent:
* <ul>
* <li> For Microsoft Windows platforms, the file type information is stored
* in the registry, which is a tree structure.
* <li> For Gnome/UNIX platforms, the file type information is stored in the
* MIME type database, which consists of some plain text files
* (.keys, .mime, .applications). Each MIME text file could contain one
* or multiple file types. A file type could be registered in the system
* by providing new MIME files, or being added to existing MIME files.
* </ul>
*
* @see Association
* @see Action
*/
public class AssociationService {
// A platform-dependent instance of AppAssociationReader.
private AppAssociationReader appAssocReader;
// A platform-dependent instance of AppAssociationWriter.
private AppAssociationWriter appAssocWriter;
static java.lang.reflect.Method mdAppAssociationReaderFactory_newInstance = null;
static java.lang.reflect.Method mdAppAssociationWriterFactory_newInstance = null;
/**
* Constructor of an <code>AssociationService</code> object.
*/
public AssociationService() {
//appAssocReader = AppAssociationReaderFactory.newInstance();
//appAssocWriter = AppAssociationWriterFactory.newInstance();
try{
if(null==mdAppAssociationReaderFactory_newInstance){
mdAppAssociationReaderFactory_newInstance = ClassLoader
.getSystemClassLoader()
.loadClass("org.jdesktop.jdic.filetypes.internal.AppAssociationReaderFactory_"
+ JdicManager.getPlatformSuffix() )
.getMethod("newInstance", null);
mdAppAssociationWriterFactory_newInstance = ClassLoader
.getSystemClassLoader()
.loadClass("org.jdesktop.jdic.filetypes.internal.AppAssociationWriterFactory_"
+ JdicManager.getPlatformSuffix() )
.getMethod("newInstance", null);
}
appAssocReader = (AppAssociationReader)mdAppAssociationReaderFactory_newInstance.invoke(null, null);
appAssocWriter = (AppAssociationWriter)mdAppAssociationWriterFactory_newInstance.invoke(null, null);
}catch(Exception e){
e.printStackTrace();
}
}
/**
* Returns the association representing the file type of the
* given MIME type.
*
* @param mimeType a given MIME type name.
* @return the appropriate <code>Association</code> object; <code>null</code>
* if the given MIME type is not found in the system.
*/
public Association getMimeTypeAssociation(String mimeType) {
if (mimeType == null) {
throw new IllegalArgumentException("The specified mime type is null");
}
// Check whether the given mime type exists/is registered in the system.
if (!appAssocReader.isMimeTypeExist(mimeType)) {
return null;
}
// Get the association associated with the mime type.
Association assoc = new Association();
List fileExtList = appAssocReader.getFileExtListByMimeType(mimeType);
String iconFileName = appAssocReader.getIconFileNameByMimeType(mimeType);
String description = appAssocReader.getDescriptionByMimeType(mimeType);
List actionList = appAssocReader.getActionListByMimeType(mimeType);
assoc.setMimeType(mimeType);
if (fileExtList != null) {
Iterator iter = fileExtList.iterator();
if (iter != null) {
while (iter.hasNext()) {
assoc.addFileExtension((String) iter.next());
}
}
}
if (iconFileName != null) {
assoc.setIconFileName(iconFileName);
}
if (description != null) {
assoc.setDescription(description);
}
if (actionList != null) {
Iterator iter = actionList.iterator();
if (iter != null) {
while (iter.hasNext()) {
assoc.addAction((Action) iter.next());
}
}
}
return assoc;
}
/**
* Returns the association representing the file type of the given
* file extension.
* <p>
* The file extension list in the returned <code>Association</code> object
* contains only this given file extension.
*
* @param fileExt a given file extension name.
* @return the appropriate <code>Association</code> object; <code>null</code>
* if the given file extension is not found in the system.
*/
public Association getFileExtensionAssociation(String fileExt) {
if (fileExt == null) {
throw new IllegalArgumentException("The specified file extension is null");
}
// Add the leading '.' character to the given file extension if not exists.
fileExt = AppUtility.addDotToFileExtension(fileExt);
// Check whether the given file extension exists/is registered in the system.
if (!appAssocReader.isFileExtExist(fileExt)) {
return null;
}
// Get the association associated with the file extension.
Association assoc = new Association();
String mimeType = appAssocReader.getMimeTypeByFileExt(fileExt);
String iconFileName = appAssocReader.getIconFileNameByFileExt(fileExt);
String description = appAssocReader.getDescriptionByFileExt(fileExt);
List actionList = appAssocReader.getActionListByFileExt(fileExt);
// Do not retrieve other file extensions.
assoc.addFileExtension(fileExt);
if (iconFileName != null) {
assoc.setIconFileName(iconFileName);
}
if (mimeType != null) {
assoc.setMimeType(mimeType);
}
if (description != null) {
assoc.setDescription(description);
}
if (actionList != null) {
Iterator iter = actionList.iterator();
if (iter != null) {
while (iter.hasNext()) {
assoc.addAction((Action) iter.next());
}
}
}
return assoc;
}
/**
* Returns the association representing the file type of the file the given
* URL points to.
*
* @param url a given URL.
* @return the appropriate <code>Association</code> object; <code>null</code>
* if the file type of the file the given URL points to is not
* found in the system.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -