1 20 21 package org.jdesktop.jdic.filetypes.internal; 22 23 import java.util.Iterator ; 24 import java.util.List ; 25 import org.jdesktop.jdic.filetypes.Association; 26 import org.jdesktop.jdic.filetypes.RegisterFailedException; 27 28 29 32 public class WinAppAssociationWriter implements AppAssociationWriter { 33 34 37 protected class BackupAssociation { 38 private String curMimeType; 40 private String curFileExt; 42 private boolean curMimeTypeExisted; 43 private boolean curFileExtExisted; 44 private String backupMimeType; 45 private String backupClassID; 46 private String backupFileExt; 47 48 51 private BackupAssociation() {} 52 53 59 protected BackupAssociation(Association assoc, int regLevel) { 60 curMimeType = assoc.getMimeType(); 61 62 Iterator iter = null; 63 List temFileExtList = assoc.getFileExtList(); 64 if (temFileExtList != null) { 65 iter = temFileExtList.iterator(); 66 } 67 if (iter != null) { 68 if (iter.hasNext()) { 69 curFileExt = (String ) iter.next(); 70 } 71 } 72 73 if (curMimeType != null) { 74 curMimeTypeExisted = WinRegistryUtil.isMimeTypeExist(curMimeType, regLevel); 75 } else { 76 curMimeTypeExisted = false; 77 } 78 79 if (curFileExt != null) { 80 curFileExtExisted = WinRegistryUtil.isFileExtExist(curFileExt, regLevel); 81 } else { 82 curFileExtExisted = false; 83 } 84 85 if (curMimeTypeExisted) { 86 backupFileExt = WinRegistryUtil.getFileExtByMimeType(curMimeType, regLevel); 87 } 88 89 if (curFileExtExisted) { 90 backupClassID = WinRegistryUtil.getClassIDByFileExt(curFileExt, regLevel); 91 backupMimeType = WinRegistryUtil.getMimeTypeByFileExt(curFileExt, regLevel); 92 } 93 } 94 95 100 protected String getCurMimeType() { 101 return curMimeType; 102 } 103 104 109 protected String getCurFileExt() { 110 return curFileExt; 111 } 112 113 118 protected boolean getCurMimeTypeExisted() { 119 return curMimeTypeExisted; 120 } 121 122 127 protected boolean getCurFileExtExisted() { 128 return curFileExtExisted; 129 } 130 131 136 protected String getBackupMimeType() { 137 return backupMimeType; 138 } 139 140 145 protected String getBackupClassID() { 146 return backupClassID; 147 } 148 149 154 protected String getBackupFileExt() { 155 return backupFileExt; 156 } 157 } 158 159 165 private void restoreAssociationRegistration(BackupAssociation backupAssoc, int regLevel) { 166 try { 167 String curMimeType = backupAssoc.getCurMimeType(); 168 String curFileExt = backupAssoc.getCurFileExt(); 169 170 171 if (!backupAssoc.getCurMimeTypeExisted()) { 172 if (curMimeType != null) { 173 WinRegistryUtil.removeMimeType(curMimeType, regLevel); 174 } 175 } else { 176 String backupFileExt = backupAssoc.getBackupFileExt(); 177 if (backupFileExt != null) { 178 WinRegistryUtil.setFileExtByMimeType(backupFileExt, 179 curMimeType, regLevel); 180 } 181 } 182 183 if (!backupAssoc.getCurFileExtExisted()) { 184 if (curFileExt != null) { 185 WinRegistryUtil.removeFileExt(curFileExt, regLevel); 186 } 187 } else { 188 String backupMimeType = backupAssoc.getBackupMimeType(); 189 if (backupMimeType != null) { 190 WinRegistryUtil.setMimeTypeByFileExt(backupMimeType, 191 curFileExt, regLevel); 192 } 193 String backupClassID = backupAssoc.getBackupClassID(); 194 if (backupClassID != null) { 195 WinRegistryUtil.setClassIDByFileExt(curFileExt, 196 backupClassID, regLevel); 197 } 198 } 199 } catch (RegisterFailedException e) { 200 } 201 } 202 203 209 private void restoreAssociationUnregistration(BackupAssociation backupAssoc, int regLevel) { 210 try { 211 String curMimeType = backupAssoc.getCurMimeType(); 212 String curFileExt = backupAssoc.getCurFileExt(); 213 214 if (backupAssoc.getCurMimeTypeExisted()) { 215 WinRegistryUtil.addMimeType(curMimeType, regLevel); 216 String backupFileExt = backupAssoc.getBackupFileExt(); 217 if (backupFileExt != null) { 218 WinRegistryUtil.setFileExtByMimeType(backupFileExt, 219 curMimeType, regLevel); 220 } 221 } 222 223 if (backupAssoc.getCurFileExtExisted()) { 224 WinRegistryUtil.addFileExt(curFileExt, regLevel); 225 String backupMimeType = backupAssoc.getBackupMimeType(); 226 String backupClassID = backupAssoc.getBackupClassID(); 227 228 if (backupMimeType != null) { 229 WinRegistryUtil.setMimeTypeByFileExt(backupMimeType, 230 curFileExt, regLevel); 231 } 232 if (backupClassID != null) { 233 WinRegistryUtil.setClassIDByFileExt(curFileExt, 234 backupClassID, regLevel); 235 } 236 } 237 } catch (RegisterFailedException e) { 238 } 239 } 240 241 252 public void checkAssociationValidForRegistration(Association assoc) 253 throws IllegalArgumentException { 254 boolean isActionListEmpty = true; 255 boolean isFileExtensionEmpty = true; 256 boolean isValid = false; 257 258 if (assoc.getActionList() != null) { 260 isActionListEmpty = assoc.getActionList().isEmpty(); 261 } 262 if (assoc.getFileExtList() != null) { 264 isFileExtensionEmpty = assoc.getFileExtList().isEmpty(); 265 } 266 267 if (isFileExtensionEmpty && (assoc.getMimeType() == null)) { 268 isValid = false; 269 } else if ((assoc.getDescription() != null) || (assoc.getIconFileName() != null) || 270 (!isActionListEmpty)) { 271 isValid = !isFileExtensionEmpty; 272 } else { 273 isValid = true; 274 } 275 276 if (!isValid) { 277 throw new IllegalArgumentException ("The given association is invalid. It should " + 278 "specify both the mimeType and fileExtensionList fields to perform this operation."); 279 } 280 } 281 282 289 public void checkAssociationValidForUnregistration(Association assoc) 290 throws IllegalArgumentException { 291 boolean isFileExtListEmpty = true; 292 if (assoc.getFileExtList() != null) { 293 isFileExtListEmpty = assoc.getFileExtList().isEmpty(); 294 } 295 if ((assoc.getMimeType() == null) && isFileExtListEmpty) { 296 throw new IllegalArgumentException ("The given association is invalid. It should " + 297 "specify both the mimeType and fileExtensionList fields to perform this " + 298 "operation."); 299 } 300 } 301 302 324 public boolean isAssociationExist(Association assoc, int regLevel) { 325 String temFileExt = null; 326 String temMimeType = assoc.getMimeType(); 327 Iterator temFileExtIter; 328 if (assoc.getFileExtList() != null) { 329 temFileExtIter = assoc.getFileExtList().iterator(); 330 } else { 331 temFileExtIter = null; 332 } 333 334 if (temFileExtIter != null) { 335 if (temFileExtIter.hasNext()) { 336 temFileExt = (String ) temFileExtIter.next(); 337 } 338 } 339 340 if (WinRegistryUtil.isWin2kUserDefinedFileExtExist(temFileExt)) { 343 return true; 344 } 345 346 if ((temMimeType == null) && (temFileExt == null)) { 347 return false; 348 } else if ((temMimeType == null) && (temFileExt != null)) { 349 return WinRegistryUtil.isFileExtExist(temFileExt, regLevel); 350 } else if ((temMimeType != null) && (temFileExt == null)) { 351 return WinRegistryUtil.isMimeTypeExist(temMimeType, regLevel); 352 } else { 353 String regMimeType = WinRegistryUtil.getMimeTypeByFileExt(temFileExt, regLevel); 354 String regFileExt = WinRegistryUtil.getFileExtByMimeType(temMimeType, regLevel); 355 356 return ((WinRegistryUtil.isMimeTypeExist(temMimeType, regLevel)) 357 && (WinRegistryUtil.isFileExtExist(temFileExt, regLevel)) 358 && (temFileExt == null 359 ? regFileExt == null 360 : temFileExt.equals(regFileExt)) 361 && (temMimeType == null 362 ? regMimeType == null 363 : temMimeType.equals(regMimeType))); 364 } 365 } 366 367 390 public void registerAssociation(Association assoc, int regLevel) 391 throws RegisterFailedException { 392 boolean isOldWindows = false; 395 String osName = System.getProperty("os.name").toLowerCase(); 396 if ((osName.indexOf("98") != -1) || 397 (osName.indexOf("me") != -1) || 398 (osName.indexOf("nt") != -1)) { 399 isOldWindows = true; 400 } 401 if (isOldWindows) { 402 regLevel = AppConstants.SYSTEM_LEVEL; 403 } 404 BackupAssociation backupAssoc = new BackupAssociation(assoc, regLevel); 405 String curMimeType = backupAssoc.getCurMimeType(); 406 String curFileExt = backupAssoc.getCurFileExt(); 407 String curDescription = assoc.getDescription(); 408 String curIconFileName = assoc.getIconFileName(); 409 List curActionList = assoc.getActionList(); 410 boolean curMimeTypeExisted = backupAssoc.getCurMimeTypeExisted(); 411 boolean curFileExtExisted = backupAssoc.getCurFileExtExisted(); 412 413 414 try { 415 if ((curMimeType == null) && (curFileExt != null)) { 416 WinRegistryUtil.addFileExt(curFileExt, regLevel); 417 if (curDescription != null) { 418 WinRegistryUtil.setDescriptionByFileExt(curDescription, 419 curFileExt, regLevel); 420 } 421 if (curIconFileName != null) { 422 WinRegistryUtil.setIconFileNameByFileExt(curIconFileName, 423 curFileExt, regLevel); 424 } 425 if (curActionList != null) { 426 WinRegistryUtil.setActionListByFileExt(curActionList, 427 curFileExt, regLevel); 428 } 429 WinRegistryUtil.markGeneratorByFileExt(curFileExt, regLevel); 431 } else if ((curMimeType != null) && (curFileExt == null)) { 432 WinRegistryUtil.addMimeType(curMimeType, regLevel); 433 } else if ((curMimeType != null) && (curFileExt != null)) { 434 if (!curMimeTypeExisted) { 435 WinRegistryUtil.addMimeType(curMimeType, regLevel); 436 } 437 if (!curFileExtExisted) { 438 WinRegistryUtil.addFileExt(curFileExt, regLevel); 439 } 440 if (curDescription != null) { 441 WinRegistryUtil.setDescriptionByFileExt(curDescription, 442 curFileExt, regLevel); 443 } 444 if (curIconFileName != null) { 445 WinRegistryUtil.setIconFileNameByFileExt(curIconFileName, 446 curFileExt, regLevel); 447 } 448 if (curActionList != null) { 449 WinRegistryUtil.setActionListByFileExt(curActionList, 450 curFileExt, regLevel); 451 } 452 WinRegistryUtil.markGeneratorByFileExt(curFileExt, regLevel); 454 WinRegistryUtil.setMutualRef(curFileExt, curMimeType, regLevel); 455 } 456 } catch (RegisterFailedException e) { 457 restoreAssociationRegistration(backupAssoc, regLevel); 458 459 throw e; 460 } 461 } 462 463 470 public void unregisterAssociation(Association assoc, int regLevel) 471 throws RegisterFailedException { 472 boolean isOldWindows = false; 476 String osName = System.getProperty("os.name").toLowerCase(); 477 if ((osName.indexOf("98") != -1) || 478 (osName.indexOf("me") != -1) || 479 (osName.indexOf("nt") != -1)) { 480 isOldWindows = true; 481 } 482 if (isOldWindows) { 483 regLevel = AppConstants.SYSTEM_LEVEL; 484 } 485 486 BackupAssociation backupAssoc = new BackupAssociation(assoc, regLevel); 487 String curMimeType = backupAssoc.getCurMimeType(); 488 String curFileExt = backupAssoc.getCurFileExt(); 489 boolean curMimeTypeExisted = backupAssoc.getCurMimeTypeExisted(); 490 boolean curFileExtExisted = backupAssoc.getCurFileExtExisted(); 491 492 try { 493 if (curMimeTypeExisted) { 494 WinRegistryUtil.removeMimeType(curMimeType, regLevel); 495 } 496 if (curFileExtExisted) { 497 WinRegistryUtil.removeFileExt(curFileExt, regLevel); 498 } 499 } catch (RegisterFailedException e) { 500 restoreAssociationUnregistration(backupAssoc, regLevel); 501 502 throw e; 503 } 504 } 505 } 506 | Popular Tags |