KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > refactoring > rename > TldRenameRefactoring


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.refactoring.rename;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.text.MessageFormat JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Enumeration JavaDoc;
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"); // NOI18N
59

60     public TldRenameRefactoring() { }
61
62     /** Find usages in web deployment descriptor: web.xml
63      */

64     public Problem prepare(AbstractRefactoring refactoring, RefObject refObject, String JavaDoc newName, RefactoringElementsBag refactoringElements) {
65             
66         Problem problem = null;
67         if (refObject instanceof JavaClass) {
68             
69             JavaClass jClass = (JavaClass)refObject;
70             
71             String JavaDoc 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) { // the class is in a web module, therefore tld makes sense
82
FileObject webInf = wm.getWebInf();
83                 Enumeration JavaDoc 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                                 // ignore
96
}
97                             if ((tldData != null) && (tldData instanceof TLDDataObject)) {
98                                 Taglib taglib = null;
99                                 try {
100                                     taglib = ((TLDDataObject)tldData).getTaglib();
101                                 } catch (IOException JavaDoc ioe) {}
102                                 if (taglib != null) {
103                                     // tag element
104
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 JavaDoc 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 JavaDoc 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 JavaDoc 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                                     // validator element
134
ValidatorType validatorType = taglib.getValidator();
135                                     if (validatorType != null) {
136                                         String JavaDoc 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                                     // listener element
144
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 JavaDoc 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 JavaDoc[] args = new Object JavaDoc [] {tld.getNameExt()};
158                                     String JavaDoc 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                     } // while
165
}
166             }
167         } // javaclass
168
return problem;
169     }
170
171     private boolean isTld(FileObject fo) {
172         boolean isTld = false;
173         if (fo != null) {
174             String JavaDoc 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         /** Creates a new instance of TaglibTagClassRenameRefactoringElement */
188         public TaglibTagClassRenameRefactoringElement(Taglib tld, String JavaDoc oldName, String JavaDoc 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         /** Returns text describing the refactoring formatted for display (using HTML tags).
197          * @return Formatted text.
198          */

199         public String JavaDoc getDisplayText() {
200             Object JavaDoc[] args = new Object JavaDoc [] {parentFile.getNameExt(), oldName, newName};
201             return MessageFormat.format(NbBundle.getMessage(TldRenameRefactoring.class, "TXT_TaglibTagClassRename"), args);
202         }
203
204         /** Performs the change represented by this refactoring element.
205          */

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 JavaDoc ioe) {
218                 //TODO
219
}
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 JavaDoc ioe) {
230                 //TODO
231
}
232         }
233     }
234     
235     public final class TaglibTeiClassRenameRefactoringElement extends AbstractRenameRefactoringElement implements ExternalChange {
236
237         protected Taglib tld;
238         private TagType tagType;
239
240         /** Creates a new instance of TaglibTagClassRenameRefactoringElement */
241         public TaglibTeiClassRenameRefactoringElement(Taglib tld, String JavaDoc oldName, String JavaDoc 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         /** Returns text describing the refactoring formatted for display (using HTML tags).
250          * @return Formatted text.
251          */

252         public String JavaDoc getDisplayText() {
253             Object JavaDoc[] args = new Object JavaDoc [] {parentFile.getNameExt(), oldName, newName};
254             return MessageFormat.format(NbBundle.getMessage(TldRenameRefactoring.class, "TXT_TaglibTeiClassRename"), args);
255         }
256
257         /** Performs the change represented by this refactoring element.
258          */

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 JavaDoc ioe) {
271                 //TODO
272
}
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 JavaDoc ioe) {
283                 //TODO
284
}
285         }
286     }
287     
288     public final class TaglibFunctionClassRenameRefactoringElement extends AbstractRenameRefactoringElement implements ExternalChange {
289
290         protected Taglib tld;
291         private FunctionType functionType;
292
293         /** Creates a new instance of TaglibFunctionClassRenameRefactoringElement */
294         public TaglibFunctionClassRenameRefactoringElement(Taglib tld, String JavaDoc oldName, String JavaDoc 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         /** Returns text describing the refactoring formatted for display (using HTML tags).
303          * @return Formatted text.
304          */

305         public String JavaDoc getDisplayText() {
306             Object JavaDoc[] args = new Object JavaDoc [] {parentFile.getNameExt(), oldName, newName};
307             return MessageFormat.format(NbBundle.getMessage(TldRenameRefactoring.class, "TXT_TaglibFunctionClassRename"), args);
308         }
309
310         /** Performs the change represented by this refactoring element.
311          */

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 JavaDoc ioe) {
324                 //TODO
325
}
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 JavaDoc ioe) {
336                 //TODO
337
}
338         }
339     }
340     
341     public final class TaglibValidatorClassRenameRefactoringElement extends AbstractRenameRefactoringElement implements ExternalChange {
342
343         protected Taglib tld;
344         private ValidatorType validatorType;
345
346         /** Creates a new instance of TaglibFunctionClassRenameRefactoringElement */
347         public TaglibValidatorClassRenameRefactoringElement(Taglib tld, String JavaDoc oldName, String JavaDoc 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         /** Returns text describing the refactoring formatted for display (using HTML tags).
356          * @return Formatted text.
357          */

358         public String JavaDoc getDisplayText() {
359             Object JavaDoc[] args = new Object JavaDoc [] {parentFile.getNameExt(), oldName, newName};
360             return MessageFormat.format(NbBundle.getMessage(TldRenameRefactoring.class, "TXT_TaglibValidatorClassRename"), args);
361         }
362
363         /** Performs the change represented by this refactoring element.
364          */

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 JavaDoc ioe) {
377                 //TODO
378
}
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 JavaDoc ioe) {
389                 //TODO
390
}
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         /** Creates a new instance of TaglibFunctionClassRenameRefactoringElement */
400         public TaglibListenerClassRenameRefactoringElement(Taglib tld, String JavaDoc oldName, String JavaDoc 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         /** Returns text describing the refactoring formatted for display (using HTML tags).
410          * @return Formatted text.
411          */

412         public String JavaDoc getDisplayText() {
413             Object JavaDoc[] args = new Object JavaDoc [] {parentFile.getNameExt(), oldName, newName};
414             return MessageFormat.format(NbBundle.getMessage(TldRenameRefactoring.class, "TXT_TaglibListenerClassRename"), args);
415         }
416
417         /** Performs the change represented by this refactoring element.
418          */

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 JavaDoc ioe) {
431                 //TODO
432
}
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 JavaDoc ioe) {
443                 //TODO
444
}
445         }
446     }
447 }
448
Popular Tags