1 20 21 package org.jdesktop.jdic.filetypes; 22 23 import java.net.URL ; 24 import java.util.Iterator ; 25 import java.util.List ; 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 56 public class AssociationService { 57 private AppAssociationReader appAssocReader; 59 private AppAssociationWriter appAssocWriter; 61 62 static { 66 try { 67 JdicManager jm = JdicManager.getManager(); 68 jm.initShareNative(); 69 } catch (JdicInitException e){ 70 e.printStackTrace(); 71 } 72 } 73 74 77 public AssociationService() { 78 appAssocReader = AppAssociationReaderFactory.newInstance(); 79 appAssocWriter = AppAssociationWriterFactory.newInstance(); 80 } 81 82 90 public Association getMimeTypeAssociation(String mimeType) { 91 if (mimeType == null) { 92 throw new IllegalArgumentException ("The specified mime type is null"); 93 } 94 95 if (!appAssocReader.isMimeTypeExist(mimeType)) { 97 return null; 98 } 99 100 Association assoc = new Association(); 102 List fileExtList = appAssocReader.getFileExtListByMimeType(mimeType); 103 String iconFileName = appAssocReader.getIconFileNameByMimeType(mimeType); 104 String description = appAssocReader.getDescriptionByMimeType(mimeType); 105 List actionList = appAssocReader.getActionListByMimeType(mimeType); 106 107 assoc.setMimeType(mimeType); 108 109 if (fileExtList != null) { 110 Iterator iter = fileExtList.iterator(); 111 112 if (iter != null) { 113 while (iter.hasNext()) { 114 assoc.addFileExtension((String ) 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 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 151 public Association getFileExtensionAssociation(String fileExt) { 152 if (fileExt == null) { 153 throw new IllegalArgumentException ("The specified file extension is null"); 154 } 155 156 fileExt = AppUtility.addDotToFileExtension(fileExt); 158 159 if (!appAssocReader.isFileExtExist(fileExt)) { 161 return null; 162 } 163 164 Association assoc = new Association(); 166 String mimeType = appAssocReader.getMimeTypeByFileExt(fileExt); 167 String iconFileName = appAssocReader.getIconFileNameByFileExt(fileExt); 168 String description = appAssocReader.getDescriptionByFileExt(fileExt); 169 List actionList = appAssocReader.getActionListByFileExt(fileExt); 170 171 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 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 208 public Association getAssociationByContent(URL url) { 209 if (url == null) { 210 throw new IllegalArgumentException ("The specified URL is null"); 211 } 212 213 Association assoc = null; 214 String mimeType = appAssocReader.getMimeTypeByURL(url); 215 216 if (mimeType != null) { 217 assoc = getMimeTypeAssociation(mimeType); 219 } 220 221 if (assoc == null) { 222 String fileExt = AppUtility.getFileExtensionByURL(url); 224 225 if (fileExt != null) { 226 assoc = getFileExtensionAssociation(fileExt); 227 } 228 } 229 230 return assoc; 231 } 232 233 259 public void registerUserAssociation(Association assoc) 260 throws AssociationAlreadyRegisteredException, RegisterFailedException { 261 if (assoc == null) { 262 throw new IllegalArgumentException ("The specified association is null"); 263 } 264 265 try { 267 appAssocWriter.checkAssociationValidForRegistration(assoc); 268 } catch (IllegalArgumentException e) { 269 throw e; 270 } 271 272 if (appAssocWriter.isAssociationExist(assoc, AppAssociationWriter.USER_LEVEL)) { 274 throw new AssociationAlreadyRegisteredException("Assocation already exists!"); 275 } 276 277 appAssocWriter.registerAssociation(assoc, AppAssociationWriter.USER_LEVEL); 279 } 280 281 307 public void unregisterUserAssociation(Association assoc) 308 throws AssociationNotRegisteredException, RegisterFailedException { 309 if (assoc == null) { 310 throw new IllegalArgumentException ("The specified association is null"); 311 } 312 313 try { 315 appAssocWriter.checkAssociationValidForUnregistration(assoc); 316 } catch (IllegalArgumentException e) { 317 throw e; 318 } 319 320 if (!appAssocWriter.isAssociationExist(assoc, AppAssociationWriter.USER_LEVEL)) { 322 throw new AssociationNotRegisteredException("Assocation not exists!"); 323 } 324 325 appAssocWriter.unregisterAssociation(assoc, AppAssociationWriter.USER_LEVEL); 327 } 328 329 352 public void registerSystemAssociation(Association assoc) 353 throws AssociationAlreadyRegisteredException, RegisterFailedException { 354 if (assoc == null) { 355 throw new IllegalArgumentException ("The specified association is null"); 356 } 357 358 try { 360 appAssocWriter.checkAssociationValidForRegistration(assoc); 361 } catch (IllegalArgumentException e) { 362 throw e; 363 } 364 365 if (appAssocWriter.isAssociationExist(assoc, AppAssociationWriter.SYSTEM_LEVEL)) { 367 throw new AssociationAlreadyRegisteredException("Assocation already exists!"); 368 } 369 370 appAssocWriter.registerAssociation(assoc, AppAssociationWriter.SYSTEM_LEVEL); 372 } 373 374 397 public void unregisterSystemAssociation(Association assoc) 398 throws AssociationNotRegisteredException, RegisterFailedException { 399 if (assoc == null) { 400 throw new IllegalArgumentException ("The specified association is null"); 401 } 402 403 try { 405 appAssocWriter.checkAssociationValidForUnregistration(assoc); 406 } catch (IllegalArgumentException e) { 407 throw e; 408 } 409 410 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 |