1 19 20 package org.netbeans.modules.j2ee.refactoring.rename; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.text.MessageFormat ; 25 import java.util.Collection ; 26 import java.util.Enumeration ; 27 import javax.jmi.reflect.RefObject; 28 import org.netbeans.modules.j2ee.dd.api.web.Filter; 29 import org.netbeans.modules.j2ee.dd.api.web.Listener; 30 import org.netbeans.modules.j2ee.dd.api.web.Servlet; 31 import org.netbeans.modules.j2ee.dd.api.web.WebApp; 32 import org.netbeans.jmi.javamodel.JavaClass; 33 import org.netbeans.jmi.javamodel.Resource; 34 import org.netbeans.modules.j2ee.refactoring.Utility; 35 import org.netbeans.modules.javacore.api.JavaModel; 36 import org.netbeans.modules.javacore.internalapi.ExternalChange; 37 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 38 import org.netbeans.modules.refactoring.api.AbstractRefactoring; 39 import org.netbeans.modules.refactoring.api.Problem; 40 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag; 41 import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation; 42 import org.netbeans.modules.web.api.webmodule.WebModule; 43 import org.netbeans.modules.web.taglib.TLDDataObject; 44 import org.netbeans.modules.web.taglib.TLDLoader; 45 import org.netbeans.modules.web.taglib.model.FunctionType; 46 import org.netbeans.modules.web.taglib.model.TagType; 47 import org.netbeans.modules.web.taglib.model.Taglib; 48 import org.netbeans.modules.web.taglib.model.ValidatorType; 49 import org.openide.ErrorManager; 50 import org.openide.filesystems.FileObject; 51 import org.openide.filesystems.FileUtil; 52 import org.openide.loaders.DataObject; 53 import org.openide.loaders.DataObjectNotFoundException; 54 import org.openide.util.NbBundle; 55 56 public final class TldRenameRefactoring { 57 58 private static final ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.modules.j2ee.refactoring.rename"); 60 public TldRenameRefactoring() { } 61 62 64 public Problem prepare(AbstractRefactoring refactoring, RefObject refObject, String newName, RefactoringElementsBag refactoringElements) { 65 66 Problem problem = null; 67 if (refObject instanceof JavaClass) { 68 69 JavaClass jClass = (JavaClass)refObject; 70 71 String name = jClass.getName(); 72 err.log("name: " + name); 73 74 newName = name.substring(0, name.lastIndexOf('.') + 1) + newName; 75 err.log("newName: " + newName); 76 77 Resource res = jClass.getResource(); 78 FileObject fo = JavaModel.getFileObject(res); 79 WebModule wm = WebModule.getWebModule(fo); 80 81 if (wm != null) { FileObject webInf = wm.getWebInf(); 83 Enumeration e = null; 84 if (webInf != null) { 85 e = webInf.getChildren(true); 86 } 87 if (e != null) { 88 while (e.hasMoreElements()) { 89 FileObject tld = (FileObject)e.nextElement(); 90 if (isTld(tld)) { 91 DataObject tldData = null; 92 try { 93 tldData = DataObject.find(tld); 94 } catch (DataObjectNotFoundException dne) { 95 } 97 if ((tldData != null) && (tldData instanceof TLDDataObject)) { 98 Taglib taglib = null; 99 try { 100 taglib = ((TLDDataObject)tldData).getTaglib(); 101 } catch (IOException ioe) {} 102 if (taglib != null) { 103 TagType[] tagTypes = taglib.getTag(); 105 if ((tagTypes != null) && (tagTypes.length > 0)) { 106 for (int tt = 0; tt < tagTypes.length; tt++) { 107 TagType tagT = tagTypes[tt]; 108 String tagClass = tagT.getTagClass(); 109 if ((tagClass != null) && (tagClass.equals(name))) { 110 RefactoringElementImplementation elem = new TaglibTagClassRenameRefactoringElement(taglib, name, newName, tagT, tld); 111 refactoringElements.add(refactoring, elem); 112 } 113 String teiClass = tagT.getTeiClass(); 114 if ((teiClass != null) && (teiClass.equals(name))) { 115 RefactoringElementImplementation elem = new TaglibTeiClassRenameRefactoringElement(taglib, name, newName, tagT, tld); 116 refactoringElements.add(refactoring, elem); 117 } 118 } 119 } 120 121 FunctionType[] functionTypes = taglib.getFunction(); 122 if ((functionTypes != null) && (functionTypes.length > 0)) { 123 for (int tt = 0; tt < functionTypes.length; tt++) { 124 FunctionType functionT = functionTypes[tt]; 125 String functionClass = functionT.getFunctionClass(); 126 if ((functionClass != null) && (functionClass.equals(name))) { 127 RefactoringElementImplementation elem = new TaglibFunctionClassRenameRefactoringElement(taglib, name, newName, functionT, tld); 128 refactoringElements.add(refactoring, elem); 129 } 130 } 131 } 132 133 ValidatorType validatorType = taglib.getValidator(); 135 if (validatorType != null) { 136 String validatorClass = validatorType.getValidatorClass(); 137 if ((validatorClass != null) && (validatorClass.equals(name))) { 138 RefactoringElementImplementation elem = new TaglibValidatorClassRenameRefactoringElement(taglib, name, newName, validatorType, tld); 139 refactoringElements.add(refactoring, elem); 140 } 141 } 142 143 org.netbeans.modules.web.taglib.model.ListenerType[] listenerTypes = taglib.getListener(); 145 if ((listenerTypes != null) && (listenerTypes.length > 0)) { 146 for (int tt = 0; tt < listenerTypes.length; tt++) { 147 org.netbeans.modules.web.taglib.model.ListenerType listenerT = listenerTypes[tt]; 148 String listenerClass = listenerT.getListenerClass(); 149 if ((listenerClass != null) && (listenerClass.equals(name))) { 150 RefactoringElementImplementation elem = new TaglibListenerClassRenameRefactoringElement(taglib, name, newName, listenerT, tld); 151 refactoringElements.add(refactoring, elem); 152 } 153 } 154 } 155 156 } else { 157 Object [] args = new Object [] {tld.getNameExt()}; 158 String msg = MessageFormat.format(NbBundle.getMessage(TldRenameRefactoring.class, "TXT_TaglibRenameInvalidProblem"), args); 159 Problem newProblem = new Problem(false, msg); 160 problem = Utility.addProblemsToEnd(problem, newProblem); 161 } 162 } 163 } 164 } } 166 } 167 } return problem; 169 } 170 171 private boolean isTld(FileObject fo) { 172 boolean isTld = false; 173 if (fo != null) { 174 String ext = fo.getExt(); 175 if (TLDLoader.tldExt.equalsIgnoreCase(ext)) { 176 isTld = true; 177 } 178 } 179 return isTld; 180 } 181 182 public final class TaglibTagClassRenameRefactoringElement extends AbstractRenameRefactoringElement implements ExternalChange { 183 184 protected Taglib tld; 185 private TagType tagType; 186 187 188 public TaglibTagClassRenameRefactoringElement(Taglib tld, String oldName, String newName, TagType tagType, FileObject parentFile) { 189 this.tld = tld; 190 this.oldName = oldName; 191 this.newName = newName; 192 this.tagType = tagType; 193 this.parentFile = parentFile; 194 } 195 196 199 public String getDisplayText() { 200 Object [] args = new Object [] {parentFile.getNameExt(), oldName, newName}; 201 return MessageFormat.format(NbBundle.getMessage(TldRenameRefactoring.class, "TXT_TaglibTagClassRename"), args); 202 } 203 204 206 public void performChange() { 207 JavaMetamodel.getManager().registerExtChange(this); 208 } 209 210 public void performExternalChange() { 211 tagType.setTagClass(newName); 212 try { 213 TLDDataObject tdo =(TLDDataObject)DataObject.find(parentFile); 214 if (tdo != null) { 215 tdo.write(tld); 216 } 217 } catch (IOException ioe) { 218 } 220 } 221 222 public void undoExternalChange() { 223 tagType.setTagClass(oldName); 224 try { 225 TLDDataObject tdo =(TLDDataObject)DataObject.find(parentFile); 226 if (tdo != null) { 227 tdo.write(tld); 228 } 229 } catch (IOException ioe) { 230 } 232 } 233 } 234 235 public final class TaglibTeiClassRenameRefactoringElement extends AbstractRenameRefactoringElement implements ExternalChange { 236 237 protected Taglib tld; 238 private TagType tagType; 239 240 241 public TaglibTeiClassRenameRefactoringElement(Taglib tld, String oldName, String newName, TagType tagType, FileObject parentFile) { 242 this.tld = tld; 243 this.oldName = oldName; 244 this.newName = newName; 245 this.tagType = tagType; 246 this.parentFile = parentFile; 247 } 248 249 252 public String getDisplayText() { 253 Object [] args = new Object [] {parentFile.getNameExt(), oldName, newName}; 254 return MessageFormat.format(NbBundle.getMessage(TldRenameRefactoring.class, "TXT_TaglibTeiClassRename"), args); 255 } 256 257 259 public void performChange() { 260 JavaMetamodel.getManager().registerExtChange(this); 261 } 262 263 public void performExternalChange() { 264 tagType.setTeiClass(newName); 265 try { 266 TLDDataObject tdo =(TLDDataObject)DataObject.find(parentFile); 267 if (tdo != null) { 268 tdo.write(tld); 269 } 270 } catch (IOException ioe) { 271 } 273 } 274 275 public void undoExternalChange() { 276 tagType.setTeiClass(oldName); 277 try { 278 TLDDataObject tdo =(TLDDataObject)DataObject.find(parentFile); 279 if (tdo != null) { 280 tdo.write(tld); 281 } 282 } catch (IOException ioe) { 283 } 285 } 286 } 287 288 public final class TaglibFunctionClassRenameRefactoringElement extends AbstractRenameRefactoringElement implements ExternalChange { 289 290 protected Taglib tld; 291 private FunctionType functionType; 292 293 294 public TaglibFunctionClassRenameRefactoringElement(Taglib tld, String oldName, String newName, FunctionType functionType, FileObject parentFile) { 295 this.tld = tld; 296 this.oldName = oldName; 297 this.newName = newName; 298 this.functionType = functionType; 299 this.parentFile = parentFile; 300 } 301 302 305 public String getDisplayText() { 306 Object [] args = new Object [] {parentFile.getNameExt(), oldName, newName}; 307 return MessageFormat.format(NbBundle.getMessage(TldRenameRefactoring.class, "TXT_TaglibFunctionClassRename"), args); 308 } 309 310 312 public void performChange() { 313 JavaMetamodel.getManager().registerExtChange(this); 314 } 315 316 public void performExternalChange() { 317 functionType.setFunctionClass(newName); 318 try { 319 TLDDataObject tdo =(TLDDataObject)DataObject.find(parentFile); 320 if (tdo != null) { 321 tdo.write(tld); 322 } 323 } catch (IOException ioe) { 324 } 326 } 327 328 public void undoExternalChange() { 329 functionType.setFunctionClass(oldName); 330 try { 331 TLDDataObject tdo =(TLDDataObject)DataObject.find(parentFile); 332 if (tdo != null) { 333 tdo.write(tld); 334 } 335 } catch (IOException ioe) { 336 } 338 } 339 } 340 341 public final class TaglibValidatorClassRenameRefactoringElement extends AbstractRenameRefactoringElement implements ExternalChange { 342 343 protected Taglib tld; 344 private ValidatorType validatorType; 345 346 347 public TaglibValidatorClassRenameRefactoringElement(Taglib tld, String oldName, String newName, ValidatorType validatorType, FileObject parentFile) { 348 this.tld = tld; 349 this.oldName = oldName; 350 this.newName = newName; 351 this.validatorType = validatorType; 352 this.parentFile = parentFile; 353 } 354 355 358 public String getDisplayText() { 359 Object [] args = new Object [] {parentFile.getNameExt(), oldName, newName}; 360 return MessageFormat.format(NbBundle.getMessage(TldRenameRefactoring.class, "TXT_TaglibValidatorClassRename"), args); 361 } 362 363 365 public void performChange() { 366 JavaMetamodel.getManager().registerExtChange(this); 367 } 368 369 public void performExternalChange() { 370 validatorType.setValidatorClass(newName); 371 try { 372 TLDDataObject tdo =(TLDDataObject)DataObject.find(parentFile); 373 if (tdo != null) { 374 tdo.write(tld); 375 } 376 } catch (IOException ioe) { 377 } 379 } 380 381 public void undoExternalChange() { 382 validatorType.setValidatorClass(oldName); 383 try { 384 TLDDataObject tdo =(TLDDataObject)DataObject.find(parentFile); 385 if (tdo != null) { 386 tdo.write(tld); 387 } 388 } catch (IOException ioe) { 389 } 391 } 392 } 393 394 public final class TaglibListenerClassRenameRefactoringElement extends AbstractRenameRefactoringElement implements ExternalChange { 395 396 protected Taglib tld; 397 private org.netbeans.modules.web.taglib.model.ListenerType listenerType; 398 399 400 public TaglibListenerClassRenameRefactoringElement(Taglib tld, String oldName, String newName, 401 org.netbeans.modules.web.taglib.model.ListenerType listenerType, FileObject parentFile) { 402 this.tld = tld; 403 this.oldName = oldName; 404 this.newName = newName; 405 this.listenerType = listenerType; 406 this.parentFile = parentFile; 407 } 408 409 412 public String getDisplayText() { 413 Object [] args = new Object [] {parentFile.getNameExt(), oldName, newName}; 414 return MessageFormat.format(NbBundle.getMessage(TldRenameRefactoring.class, "TXT_TaglibListenerClassRename"), args); 415 } 416 417 419 public void performChange() { 420 JavaMetamodel.getManager().registerExtChange(this); 421 } 422 423 public void performExternalChange() { 424 listenerType.setListenerClass(newName); 425 try { 426 TLDDataObject tdo =(TLDDataObject)DataObject.find(parentFile); 427 if (tdo != null) { 428 tdo.write(tld); 429 } 430 } catch (IOException ioe) { 431 } 433 } 434 435 public void undoExternalChange() { 436 listenerType.setListenerClass(oldName); 437 try { 438 TLDDataObject tdo =(TLDDataObject)DataObject.find(parentFile); 439 if (tdo != null) { 440 tdo.write(tld); 441 } 442 } catch (IOException ioe) { 443 } 445 } 446 } 447 } 448 | Popular Tags |