📄 gnomeappassociationwriter.java
字号:
mBufferWriter = new BufferedWriter(new FileWriter(dotMimeFilePath, true)); mBufferWriter.write(mimeType + "\n"); String fileExtensionString = null; if (fileExtList == null) { fileExtensionString = ""; } else { fileExtensionString = fileExtListToString(fileExtList); } mBufferWriter.write("\t" + "ext: " + fileExtensionString + "\n"); mBufferWriter.write("\n"); } catch (IOException e) { throw new IOException("Write mime info to " + dotMimeFilePath + " failed."); } finally { // No matter what happens, always close streams already opened. if (mBufferWriter != null) { try { mBufferWriter.close(); } catch (IOException e) { } } } } /** * Writes association fields into specified .keys file, including mime type, and icon file, and action list. * * @throws IOException if the given association info fails to be write to the given * mime file. */ private void writeDotKeysFile(Association assoc, String dotKeysFilePath) throws IOException { // Create file first. createFile(dotKeysFilePath); String mimeType = assoc.getMimeType(); String description = assoc.getDescription(); String iconFileName = assoc.getIconFileName(); BufferedWriter kBufferWriter = null; try { // Appends new mime info into .keys file. kBufferWriter = new BufferedWriter(new FileWriter(dotKeysFilePath, true)); kBufferWriter.write(mimeType + "\n"); if (description != null) { kBufferWriter.write("\t" + GnomeAssociationUtil.GNOME_VFS_MIME_KEY_DESCRIPTION + "=" + description + "\n"); } if (iconFileName != null) { kBufferWriter.write("\t" + GnomeAssociationUtil.GNOME_VFS_MIME_KEY_ICON_FILENAME + "=" + iconFileName + "\n"); } // Parse the given action list to get the application id and command. parseOpenAction(assoc); if (defaultAppID != null) { kBufferWriter.write("\t" + "default_action_type=application" + "\n"); kBufferWriter.write("\t" + "default_application_id=" + defaultAppID + "\n"); kBufferWriter.write("\t" + "short_list_application_user_additions=" + defaultAppID + "\n"); } kBufferWriter.write("\n"); } catch (IOException e) { throw new IOException("Write mime info to " + dotKeysFilePath + " failed."); } finally { // No matter what happens, always close streams already opened. if (kBufferWriter != null) { try { kBufferWriter.close(); } catch (IOException e) { } } } } /** * Writes association fields into specified .applications file, * * @throws IOException if the given association info fails to be write to the given * mime file. */ private void writeDotApplicationsFile(Association assoc, String dotApplicationsFilePath) throws IOException { // Create file first. createFile(dotApplicationsFilePath); BufferedWriter mBufferWriter = null; try { // Parse the given action list to get the application id and command. parseOpenAction(assoc); if (defaultAppID != null && defaultAppCommand != null) { // Appends new mime info into .applications file. mBufferWriter = new BufferedWriter(new FileWriter(dotApplicationsFilePath, true)); mBufferWriter.write(defaultAppID + "\n"); mBufferWriter.write("\t" + "command=" + defaultAppCommand + "\n"); mBufferWriter.write("\t" + "name=" + defaultAppID + "\n"); mBufferWriter.write("\t" + "can_open_multiple_files=false" + "\n"); mBufferWriter.write("\t" + "requires_terminal=false" + "\n"); String mimeType = assoc.getMimeType(); mBufferWriter.write("\t" + "mime_types=" + mimeType + "\n"); mBufferWriter.write("\n"); } } catch (IOException e) { throw new IOException("Write mime info to " + dotApplicationsFilePath + " failed."); } finally { // No matter what happens, always close streams already opened. if (mBufferWriter != null) { try { mBufferWriter.close(); } catch (IOException e) { } } } } /** * Checks whether the specified .mime file contains the specified mime type. */ private boolean dotMimeFileContainsMimeType(File dotMimeFile, String mimeType) { boolean isMimeTypeExist = false; try { BufferedReader mBufferReader = new BufferedReader(new FileReader(dotMimeFile)); String oneLine; while ((oneLine = mBufferReader.readLine()) != null) { if (mimeType.equals(oneLine)) { isMimeTypeExist = true; break; } } mBufferReader.close(); return isMimeTypeExist; } catch (IOException e) { return false; } } /** * Checks whether the specified association object is valid for registration. * <P> * Both the name and mimeType fields must be specified to perform this operation. * If either of the fields is null, an IllegalArgumentException is thrown. * * @param assoc a given Association object. * @throws IllegalArgumentException if the given association is not valid for registration. */ public void checkAssociationValidForRegistration(Association assoc) throws IllegalArgumentException { if (assoc.getName() == null || assoc.getMimeType() == null) { throw new IllegalArgumentException("The given association is invalid. It should " + "specify both the name and mimeType fields to perform this operation."); } } /** * Checks whether the specified association object is valid for unregistration. * <P> * The name field must be specified to perform this operation. Or else, * an IllegalArgumentException is thrown. * * @param assoc a given Association object. * @throws IllegalArgumentException if the given association is not valid for unregistration. */ public void checkAssociationValidForUnregistration(Association assoc) throws IllegalArgumentException { if (assoc.getName() == null) { throw new IllegalArgumentException("The given association is invalid. It should " + "specify the name field to perform this operation."); } } /** * Checks whether or not the given assocation already existed in the MIME type database. * <P> * If the mime files identified by the name field (name.mime and name.keys) already exists * in the specified MIME database, return true. * * @param assoc a given Association * @param level a given MIME database level. * @return true if the given Association already exists in the specified MIME database. */ public boolean isAssociationExist(Association assoc, int level) { File dotMimeFile = null; if (level == SYSTEM_LEVEL) { // Check the mime files in system default MIME database. dotMimeFile = new File(getSystemDotMimeFilePath(assoc)); } else { // Check the mime files in user MIME database. dotMimeFile = new File(getUserDotMimeFilePath(assoc)); } if (dotMimeFile.exists()) { // The .mime file exist, check the mime type in the .mime file. if (assoc.getMimeType() == null) { return true; } else { return dotMimeFileContainsMimeType(dotMimeFile, assoc.getMimeType()); } } else { return false; } } /** * Registers the given association info in the specified level. * <P> * Generate the mime files identified by the name field(name.mime and name.keys) * in the system or user MIME database. Then write the association info into the * generated mime files. * * @param assoc the given association. * @param level the given registration level. * @throws RegisterFailedException if the registration failed. */ public void registerAssociation(Association assoc, int level) throws RegisterFailedException { String dotMimeFilePath = null; String dotKeysFilePath = null; String dotApplicationsFilePath = null; try { if (level == SYSTEM_LEVEL) { checkSystemMIMEDatabase(); dotMimeFilePath = getSystemDotMimeFilePath(assoc); dotKeysFilePath = getSystemDotKeysFilePath(assoc); dotApplicationsFilePath = getSystemDotApplicationsFilePath(assoc); } else { checkUserMIMEDatabase(); dotMimeFilePath = getUserDotMimeFilePath(assoc); dotKeysFilePath = getUserDotKeysFilePath(assoc); dotApplicationsFilePath = getUserDotApplicationsFilePath(assoc); } // Create and write .mime file. writeDotMimeFile(assoc, dotMimeFilePath); // Create and write .keys file. writeDotKeysFile(assoc, dotKeysFilePath); // Create and write .applications file. writeDotApplicationsFile(assoc, dotApplicationsFilePath); } catch (IOException e) { // If there are errors, try to delete all the created mime files. if (dotMimeFilePath != null ) { (new File(dotMimeFilePath)).delete(); } if (dotKeysFilePath != null) { (new File(dotKeysFilePath)).delete(); } if (dotApplicationsFilePath != null) { (new File(dotApplicationsFilePath)).delete(); } throw new RegisterFailedException(e.getMessage()); } } /** * Unregisters the given association in the specified level. * <P> * Removes the mime files identified by the name field(name.mime and name.keys) from * the system or user MIME type. * * @param assoc the given association. * @param level the given unregistration level. * @throws RegisterFailedException if the unregistration failed. */ public void unregisterAssociation(Association assoc, int level) throws RegisterFailedException { String dotMimeFilePath = null; String dotKeysFilePath = null; String dotApplicationsFilePath = null; try { if (level == SYSTEM_LEVEL) { checkSystemMIMEDatabase(); dotMimeFilePath = getSystemDotMimeFilePath(assoc); dotKeysFilePath = getSystemDotKeysFilePath(assoc); dotApplicationsFilePath = getSystemDotApplicationsFilePath(assoc); } else { checkUserMIMEDatabase(); dotMimeFilePath = getUserDotMimeFilePath(assoc); dotKeysFilePath = getUserDotKeysFilePath(assoc); dotApplicationsFilePath = getUserDotApplicationsFilePath(assoc); } // Delete the mime files. (new File(dotMimeFilePath)).delete(); (new File(dotKeysFilePath)).delete(); (new File(dotApplicationsFilePath)).delete(); } catch (IOException e) { throw new RegisterFailedException(e.getMessage()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -