KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > refactoring > whereused > TldWhereUsedRefactoring


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.whereused;
21
22 import java.io.IOException JavaDoc;
23 import java.text.MessageFormat JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import javax.jmi.reflect.RefObject;
27 import org.netbeans.modules.j2ee.dd.api.web.Filter;
28 import org.netbeans.modules.j2ee.dd.api.web.Listener;
29 import org.netbeans.modules.j2ee.dd.api.web.Servlet;
30 import org.netbeans.modules.j2ee.dd.api.web.WebApp;
31 import org.netbeans.jmi.javamodel.JavaClass;
32 import org.netbeans.jmi.javamodel.Resource;
33 import org.netbeans.modules.j2ee.refactoring.Utility;
34 import org.netbeans.modules.javacore.api.JavaModel;
35 import org.netbeans.modules.refactoring.api.AbstractRefactoring;
36 import org.netbeans.modules.refactoring.api.Problem;
37 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
38 import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation;
39 import org.netbeans.modules.web.api.webmodule.WebModule;
40 import org.netbeans.modules.web.taglib.TLDDataObject;
41 import org.netbeans.modules.web.taglib.TLDLoader;
42 import org.netbeans.modules.web.taglib.model.FunctionType;
43 import org.netbeans.modules.web.taglib.model.TagType;
44 import org.netbeans.modules.web.taglib.model.Taglib;
45 import org.netbeans.modules.web.taglib.model.ValidatorType;
46 import org.openide.ErrorManager;
47 import org.openide.filesystems.FileObject;
48 import org.openide.loaders.DataObject;
49 import org.openide.loaders.DataObjectNotFoundException;
50 import org.openide.util.NbBundle;
51
52 /**
53  *
54  * @author Martin Grebac
55  */

56 public final class TldWhereUsedRefactoring {
57     
58     private static final ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.modules.j2ee.refactoring.whereused"); // NOI18N
59

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

64     public Problem prepare(AbstractRefactoring refactoring, RefObject refObject, RefactoringElementsBag refactoringElements) {
65             
66         Problem problem = null;
67         if (refObject instanceof JavaClass) {
68             
69             JavaClass jClass = (JavaClass)refObject;
70             err.log("jClass: " + jClass);
71             
72             String JavaDoc name = jClass.getName();
73             err.log("name: " + name);
74             Resource res = jClass.getResource();
75             err.log("res: " + res);
76             
77             FileObject fo = JavaModel.getFileObject(res);
78             err.log("fo: " + fo);
79
80             WebModule wm = WebModule.getWebModule(fo);
81             if (wm != null) { // the class is in a web module
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 TaglibTagClassWhereUsedRefactoringElement(taglib, name, tld);
111                                                 refactoringElements.add(refactoring, elem);
112                                             }
113                                             String JavaDoc teiClass = tagT.getTeiClass();
114                                             if ((teiClass != null) && (teiClass.equals(name))) {
115                                                 RefactoringElementImplementation elem = new TaglibTeiClassWhereUsedRefactoringElement(taglib, name, 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 TaglibFunctionClassWhereUsedRefactoringElement(taglib, name, 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 TaglibValidatorClassWhereUsedRefactoringElement(taglib, name, 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 TaglibListenerClassWhereUsedRefactoringElement(taglib, name, 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(TldWhereUsedRefactoring.class, "TXT_TaglibWhereUsedInvalidProblem"), args);
159                                     Problem newProblem = new Problem(false, msg);
160                                     problem = Utility.addProblemsToEnd(problem, newProblem);
161                                 }
162                             }
163                         }
164                     }
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 TaglibTagClassWhereUsedRefactoringElement extends AbstractWhereUsedRefactoringElement {
183
184         protected Taglib taglib;
185
186         /** Creates a new instance of TaglibTagClassWhereUsedRefactoringElement */
187         public TaglibTagClassWhereUsedRefactoringElement(Taglib taglib, String JavaDoc name, FileObject parentFile) {
188             this.taglib = taglib;
189             this.name = name;
190             this.parentFile = parentFile;
191         }
192
193         /** Returns text describing the refactoring formatted for display (using HTML tags).
194          * @return Formatted text.
195          */

196         public String JavaDoc getDisplayText() {
197             Object JavaDoc[] args = new Object JavaDoc [] {name};
198             return MessageFormat.format(NbBundle.getMessage(TldWhereUsedRefactoring.class, "TXT_TaglibTagClassWhereUsed"), args);
199         }
200     }
201
202     public final class TaglibTeiClassWhereUsedRefactoringElement extends AbstractWhereUsedRefactoringElement {
203
204         protected Taglib taglib;
205
206         /** Creates a new instance of TaglibTeiClassWhereUsedRefactoringElement */
207         public TaglibTeiClassWhereUsedRefactoringElement(Taglib taglib, String JavaDoc name, FileObject parentFile) {
208             this.taglib = taglib;
209             this.name = name;
210             this.parentFile = parentFile;
211         }
212
213         /** Returns text describing the refactoring formatted for display (using HTML tags).
214          * @return Formatted text.
215          */

216         public String JavaDoc getDisplayText() {
217             Object JavaDoc[] args = new Object JavaDoc [] {name};
218             return MessageFormat.format(NbBundle.getMessage(TldWhereUsedRefactoring.class, "TXT_TaglibTeiClassWhereUsed"), args);
219         }
220     }
221
222     public final class TaglibValidatorClassWhereUsedRefactoringElement extends AbstractWhereUsedRefactoringElement {
223
224         protected Taglib taglib;
225
226         /** Creates a new instance of TaglibValidatorClassWhereUsedRefactoringElement */
227         public TaglibValidatorClassWhereUsedRefactoringElement(Taglib taglib, String JavaDoc name, FileObject parentFile) {
228             this.taglib = taglib;
229             this.name = name;
230             this.parentFile = parentFile;
231         }
232
233         /** Returns text describing the refactoring formatted for display (using HTML tags).
234          * @return Formatted text.
235          */

236         public String JavaDoc getDisplayText() {
237             Object JavaDoc[] args = new Object JavaDoc [] {name};
238             return MessageFormat.format(NbBundle.getMessage(TldWhereUsedRefactoring.class, "TXT_TaglibValidatorClassWhereUsed"), args);
239         }
240     }
241     
242     public final class TaglibListenerClassWhereUsedRefactoringElement extends AbstractWhereUsedRefactoringElement {
243
244         protected Taglib taglib;
245
246         /** Creates a new instance of TaglibListenerClassWhereUsedRefactoringElement */
247         public TaglibListenerClassWhereUsedRefactoringElement(Taglib taglib, String JavaDoc name, FileObject parentFile) {
248             this.taglib = taglib;
249             this.name = name;
250             this.parentFile = parentFile;
251         }
252
253         /** Returns text describing the refactoring formatted for display (using HTML tags).
254          * @return Formatted text.
255          */

256         public String JavaDoc getDisplayText() {
257             Object JavaDoc[] args = new Object JavaDoc [] {name};
258             return MessageFormat.format(NbBundle.getMessage(TldWhereUsedRefactoring.class, "TXT_TaglibListenerClassWhereUsed"), args);
259         }
260     }
261
262     public final class TaglibFunctionClassWhereUsedRefactoringElement extends AbstractWhereUsedRefactoringElement {
263
264         protected Taglib taglib;
265
266         /** Creates a new instance of TaglibFunctionClassWhereUsedRefactoringElement */
267         public TaglibFunctionClassWhereUsedRefactoringElement(Taglib taglib, String JavaDoc name, FileObject parentFile) {
268             this.taglib = taglib;
269             this.name = name;
270             this.parentFile = parentFile;
271         }
272
273         /** Returns text describing the refactoring formatted for display (using HTML tags).
274          * @return Formatted text.
275          */

276         public String JavaDoc getDisplayText() {
277             Object JavaDoc[] args = new Object JavaDoc [] {name};
278             return MessageFormat.format(NbBundle.getMessage(TldWhereUsedRefactoring.class, "TXT_TaglibFunctionClassWhereUsed"), args);
279         }
280     }
281         
282 }
283
Popular Tags