Added
Link Here
|
1 |
package org.eclipse.jst.servlet.ui.internale.ltkbasedrefactoring; |
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.List; |
5 |
import java.util.Map; |
6 |
|
7 |
import org.eclipse.core.resources.IFile; |
8 |
import org.eclipse.core.resources.IProject; |
9 |
import org.eclipse.core.resources.IResource; |
10 |
import org.eclipse.core.runtime.CoreException; |
11 |
import org.eclipse.core.runtime.NullProgressMonitor; |
12 |
import org.eclipse.core.runtime.Path; |
13 |
import org.eclipse.emf.ecore.util.FeatureMap; |
14 |
import org.eclipse.emf.ecore.util.FeatureMap.Entry; |
15 |
import org.eclipse.jem.util.emf.workbench.ProjectUtilities; |
16 |
import org.eclipse.jface.text.Document; |
17 |
import org.eclipse.jface.text.IDocument; |
18 |
import org.eclipse.jst.j2ee.internal.web.operations.WebPropertiesUtil; |
19 |
import org.eclipse.jst.j2ee.model.IModelProvider; |
20 |
import org.eclipse.jst.javaee.core.Listener; |
21 |
import org.eclipse.jst.javaee.core.UrlPatternType; |
22 |
import org.eclipse.jst.javaee.ltk.core.delete.J2eeDeleteProcessorImpl; |
23 |
import org.eclipse.jst.javaee.web.Filter; |
24 |
import org.eclipse.jst.javaee.web.FilterMapping; |
25 |
import org.eclipse.jst.javaee.web.Servlet; |
26 |
import org.eclipse.jst.javaee.web.ServletMapping; |
27 |
import org.eclipse.jst.javaee.web.WebApp; |
28 |
import org.eclipse.jst.jee.ui.internal.navigator.web.WebAppProvider; |
29 |
import org.eclipse.ltk.core.refactoring.TextFileChange; |
30 |
import org.eclipse.text.edits.DeleteEdit; |
31 |
|
32 |
public class WebArtifactDeleteProcessor extends J2eeDeleteProcessorImpl { |
33 |
|
34 |
private String fObjectFullyQualifiedName; |
35 |
private String fObjectName; |
36 |
private List<DeleteEdit> deletes = new ArrayList<DeleteEdit>(); |
37 |
private List<Object> fObjects = new ArrayList<Object>(); |
38 |
private String fStartTag; |
39 |
private String fEndTag; |
40 |
|
41 |
public WebArtifactDeleteProcessor(List<Object> objects, |
42 |
Map<Object, List<IResource>> map) { |
43 |
super(objects, map); |
44 |
fObjects = objects; |
45 |
} |
46 |
|
47 |
|
48 |
public Object specifyXMLFile(IProject project, |
49 |
IModelProvider modelProvider) { |
50 |
WebApp webApp = (WebApp) modelProvider.getModelObject(); |
51 |
WebAppProvider prov = new WebAppProvider(webApp, project); |
52 |
|
53 |
if(getXmlFile() == null){ |
54 |
setXmlFile(prov.getDDFile()); |
55 |
} else { |
56 |
if(!getXmlFile().equals(prov.getDDFile())){ |
57 |
setXmlFile(prov.getDDFile()); |
58 |
} |
59 |
} |
60 |
|
61 |
if(!getFiles().contains(getXmlFile())){ |
62 |
getFiles().add(getXmlFile()); |
63 |
} |
64 |
return webApp; |
65 |
} |
66 |
|
67 |
|
68 |
public void modifyXML(IFile file, Object modelObject, TextFileChange result, |
69 |
Object fObject) throws CoreException { |
70 |
WebApp webApp = (WebApp) modelObject; |
71 |
|
72 |
String xmlContent = result.getCurrentContent(new NullProgressMonitor()); |
73 |
|
74 |
List<String> listString = new ArrayList<String>(); |
75 |
|
76 |
IDocument doc = new Document(); |
77 |
doc.set(xmlContent); |
78 |
|
79 |
if (fObject instanceof Listener) { |
80 |
listenerObjectChanges(file, fObject, xmlContent, listString, doc); |
81 |
} |
82 |
if (fObject instanceof Filter) { |
83 |
filterChanges(file, webApp, fObject, xmlContent, listString, doc); |
84 |
} |
85 |
if (fObject instanceof Servlet) { |
86 |
servletChanges(file, webApp, fObject, xmlContent, listString, doc); |
87 |
} |
88 |
if (fObject instanceof FilterMapping) { |
89 |
filterMappingChanges(file, fObject, xmlContent, listString, doc); |
90 |
} |
91 |
if (fObject instanceof ServletMapping) { |
92 |
servletMappingsChanges(file, fObject, xmlContent, listString, doc); |
93 |
} |
94 |
} |
95 |
|
96 |
|
97 |
public String getObjectName(Object object){ |
98 |
String name = null; |
99 |
if(object instanceof Servlet){ |
100 |
name = ((Servlet)object).getServletName(); |
101 |
} else if(object instanceof Filter){ |
102 |
name = ((Filter)object).getFilterName(); |
103 |
} else if(object instanceof Listener){ |
104 |
name = ((Listener)object).getListenerClass(); |
105 |
} else if(object instanceof FilterMapping){ |
106 |
name = getFilterMappingDisplay((FilterMapping)object); |
107 |
} else if(object instanceof ServletMapping){ |
108 |
name = getServletMappingDisplay((ServletMapping)object); |
109 |
} |
110 |
return name; |
111 |
} |
112 |
|
113 |
|
114 |
public String getObjectType(Object object){ |
115 |
String objectType = null; |
116 |
if(object instanceof Servlet){ |
117 |
objectType = "servlet"; //$NON-NLS-1$ |
118 |
} else if(object instanceof Filter){ |
119 |
objectType = "filter"; //$NON-NLS-1$ |
120 |
} else if(object instanceof Listener){ |
121 |
objectType = "listener"; //$NON-NLS-1$ |
122 |
} else if(object instanceof FilterMapping){ |
123 |
objectType = "filter-mapping"; //$NON-NLS-1$ |
124 |
} else if(object instanceof ServletMapping){ |
125 |
objectType = "servlet-mapping"; //$NON-NLS-1$ |
126 |
} |
127 |
return objectType; |
128 |
} |
129 |
|
130 |
|
131 |
public IResource getObjectResource(Object object){ |
132 |
IProject project = ProjectUtilities.getProject(object); |
133 |
String container = WebPropertiesUtil.getJavaSourceFolder(project).getName(); |
134 |
String fullyQualifiedName = getFullyQualifiedName(object); |
135 |
if(fullyQualifiedName!=null){ |
136 |
IResource resource = project.getFile(new Path( |
137 |
container + "/" + (fullyQualifiedName).replace(".", "/") + ".java")); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
138 |
return resource; |
139 |
} |
140 |
return null; |
141 |
} |
142 |
|
143 |
private String getFullyQualifiedName(Object object){ |
144 |
String name = null; |
145 |
if(object instanceof Servlet){ |
146 |
name = ((Servlet)object).getServletName(); |
147 |
} else if(object instanceof Filter){ |
148 |
name = ((Filter)object).getFilterName(); |
149 |
} else if(object instanceof Listener){ |
150 |
name = ((Listener)object).getListenerClass(); |
151 |
} |
152 |
return name; |
153 |
} |
154 |
|
155 |
private void listenerObjectChanges(IFile file, Object fObject, |
156 |
String xmlContent, List<String> listString, IDocument doc) { |
157 |
fObjectFullyQualifiedName = ((Listener) fObject).getListenerClass(); |
158 |
setfStartTag("<listener>"); //$NON-NLS-1$ |
159 |
setfEndTag("</listener>"); //$NON-NLS-1$ |
160 |
String identifyObject = getfStartTag().replace(">", "-class>") + fObjectFullyQualifiedName + getfEndTag().replace(">", "-class>"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
161 |
List<String> identifyObjects = new ArrayList<String>(); |
162 |
identifyObjects.add(identifyObject); |
163 |
findELementTag(file, xmlContent, listString, doc, identifyObjects); |
164 |
} |
165 |
|
166 |
private void filterChanges(IFile file, WebApp webApp, Object fObject, |
167 |
String xmlContent, List<String> listString, IDocument doc) { |
168 |
List<String> identifyObjects; |
169 |
filterObjectChanges(file, fObject, xmlContent, listString, doc); |
170 |
|
171 |
List<FilterMapping> filterMappings = WebRefactoringUtils.getFilterMappings(webApp, |
172 |
fObjectName); |
173 |
for (int j = 0; j < filterMappings.size(); j++) { |
174 |
identifyObjects = new ArrayList<String>(); |
175 |
FilterMapping filterMapping = filterMappings.get(j); |
176 |
if (fObjects.contains(filterMapping)) |
177 |
continue; |
178 |
urlPatternForFilterChanges(file, xmlContent, listString, doc, |
179 |
identifyObjects, filterMapping); |
180 |
servletForFilterChanges(file, xmlContent, listString, doc, |
181 |
identifyObjects, filterMapping); |
182 |
} |
183 |
} |
184 |
|
185 |
private void filterObjectChanges(IFile file, Object fObject, |
186 |
String xmlContent, List<String> listString, IDocument doc) { |
187 |
fObjectFullyQualifiedName = ((Filter) fObject).getFilterClass(); |
188 |
fObjectName = ((Filter) fObject).getFilterName(); |
189 |
setfStartTag("<filter>"); //$NON-NLS-1$ |
190 |
setfEndTag("</filter>"); //$NON-NLS-1$ |
191 |
String identifyObject = getfStartTag().replace(">", "-name>") + fObjectName + getfEndTag().replace(">", "-name>"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
192 |
List<String> identifyObjects = new ArrayList<String>(); |
193 |
identifyObjects.add(identifyObject); |
194 |
findELementTag(file, xmlContent, listString, doc, identifyObjects); |
195 |
} |
196 |
|
197 |
private void servletForFilterChanges(IFile file, String xmlContent, |
198 |
List<String> listString, IDocument doc, |
199 |
List<String> identifyObjects, FilterMapping filterMapping) { |
200 |
String identifyObject; |
201 |
if (filterMapping.getServletNames().size() > 0) { |
202 |
setfStartTag("<filter-mapping>"); //$NON-NLS-1$ |
203 |
setfEndTag("</filter-mapping>"); //$NON-NLS-1$ |
204 |
for (int k = 0; k < filterMapping.getServletNames().size(); k++) { |
205 |
fObjectName = filterMapping.getServletNames() |
206 |
.get(k); |
207 |
identifyObject = "<servlet-name>" + fObjectName + "</servlet-name>"; //$NON-NLS-1$ //$NON-NLS-2$ |
208 |
identifyObjects.add(identifyObject); |
209 |
} |
210 |
findELementTag(file, xmlContent, listString, doc, |
211 |
identifyObjects); |
212 |
} |
213 |
} |
214 |
|
215 |
private void urlPatternForFilterChanges(IFile file, String xmlContent, |
216 |
List<String> listString, IDocument doc, |
217 |
List<String> identifyObjects, FilterMapping filterMapping) { |
218 |
String identifyObject; |
219 |
if (filterMapping.getUrlPatterns().size() > 0) { |
220 |
setfStartTag("<filter-mapping>"); //$NON-NLS-1$ |
221 |
setfEndTag("</filter-mapping>"); //$NON-NLS-1$ |
222 |
for (int k = 0; k < filterMapping.getUrlPatterns().size(); k++) { |
223 |
fObjectName = (filterMapping |
224 |
.getUrlPatterns().get(k)).getValue(); |
225 |
identifyObject = "<url-pattern>" + fObjectName + "</url-pattern>"; //$NON-NLS-1$ //$NON-NLS-2$ |
226 |
identifyObjects.add(identifyObject); |
227 |
} |
228 |
findELementTag(file, xmlContent, listString, doc, |
229 |
identifyObjects); |
230 |
} |
231 |
} |
232 |
|
233 |
private void servletMappingsChanges(IFile file, Object fObject, |
234 |
String xmlContent, List<String> listString, IDocument doc) { |
235 |
ServletMapping servletMapping = (ServletMapping) fObject; |
236 |
List<String> identifyObjects = new ArrayList<String>(); |
237 |
if (servletMapping.getUrlPatterns().size() > 0) { |
238 |
urlPatternForServletChanges(file, xmlContent, listString, doc, |
239 |
servletMapping, identifyObjects); |
240 |
} |
241 |
} |
242 |
|
243 |
private void urlPatternForServletChanges(IFile file, String xmlContent, |
244 |
List<String> listString, IDocument doc, |
245 |
ServletMapping servletMapping, List<String> identifyObjects) { |
246 |
setfStartTag("<servlet-mapping>"); //$NON-NLS-1$ |
247 |
setfEndTag("</servlet-mapping>"); //$NON-NLS-1$ |
248 |
for (int k = 0; k < servletMapping.getUrlPatterns().size(); k++) { |
249 |
fObjectName = (servletMapping |
250 |
.getUrlPatterns().get(k)).getValue(); |
251 |
String identifyObject = "<url-pattern>" + fObjectName + "</url-pattern>"; //$NON-NLS-1$ //$NON-NLS-2$ |
252 |
identifyObjects.add(identifyObject); |
253 |
} |
254 |
findELementTag(file, xmlContent, listString, doc, |
255 |
identifyObjects); |
256 |
} |
257 |
|
258 |
private void filterMappingChanges(IFile file, Object fObject, |
259 |
String xmlContent, List<String> listString, IDocument doc) { |
260 |
FilterMapping filterMapping = (FilterMapping) fObject; |
261 |
List<String> identifyObjects = new ArrayList<String>(); |
262 |
urlPatternForFilterChanges(file, xmlContent, listString, doc, identifyObjects, filterMapping); |
263 |
servletForFilterChanges(file, xmlContent, listString, doc, identifyObjects, filterMapping); |
264 |
} |
265 |
|
266 |
private void servletChanges(IFile file, WebApp webApp, Object fObject, |
267 |
String xmlContent, List<String> listString, IDocument doc) { |
268 |
List<String> identifyObjects; |
269 |
servletObjectChanges(file, fObject, xmlContent, listString, doc); |
270 |
|
271 |
List<ServletMapping> servletMappings = WebRefactoringUtils.getServletMappings(webApp, |
272 |
fObjectName); |
273 |
for (int j = 0; j < servletMappings.size(); j++) { |
274 |
identifyObjects = new ArrayList<String>(); |
275 |
ServletMapping servletMapping = servletMappings.get(j); |
276 |
if (fObjects.contains(servletMapping)) |
277 |
continue; |
278 |
urlPatternForServletChanges(file, xmlContent, listString, doc, servletMapping, identifyObjects); |
279 |
} |
280 |
} |
281 |
|
282 |
private void servletObjectChanges(IFile file, Object fObject, |
283 |
String xmlContent, List<String> listString, IDocument doc) { |
284 |
fObjectFullyQualifiedName = ((Servlet) fObject).getServletClass(); |
285 |
fObjectName = ((Servlet) fObject).getServletName(); |
286 |
setfStartTag("<servlet>"); //$NON-NLS-1$ |
287 |
setfEndTag("</servlet>"); //$NON-NLS-1$ |
288 |
String identifyObject = getfStartTag().replace(">", "-name>") + fObjectName + getfEndTag().replace(">", "-name>"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
289 |
List<String> identifyObjects = new ArrayList<String>(); |
290 |
identifyObjects.add(identifyObject); |
291 |
findELementTag(file, xmlContent, listString, doc, identifyObjects); |
292 |
} |
293 |
|
294 |
private void findELementTag(IFile file, String xmlContent, |
295 |
List<String> listString, IDocument doc, List<String> identifyObject) { |
296 |
if (getfStartTag() != null && getfEndTag() != null |
297 |
&& xmlContent.contains(getfStartTag()) |
298 |
&& xmlContent.contains(getfEndTag())) { |
299 |
createXMLChangeToBeShown(file, xmlContent, listString, doc, |
300 |
identifyObject, getfStartTag(), getfEndTag()); |
301 |
} |
302 |
} |
303 |
|
304 |
|
305 |
private void createXMLChangeToBeShown(IFile file, String xmlContent, |
306 |
List<String> listStrings, IDocument doc, |
307 |
List<String> identifyObject, String tag, String enclosingTag) { |
308 |
List<String> strings = markTextToBeDeletedFromXML(doc, tag, |
309 |
enclosingTag); |
310 |
findStartAndEndPoint(xmlContent, listStrings, strings, doc, tag, |
311 |
enclosingTag, identifyObject); |
312 |
DeleteEdit edit = new DeleteEdit(getOffset(), getLength()); |
313 |
|
314 |
if (!getEditMap().containsKey(file)) { |
315 |
deletes = new ArrayList<DeleteEdit>(); |
316 |
deletes.add(edit); |
317 |
getEditMap().put(file, deletes); |
318 |
} else { |
319 |
if (!getEditMap().get(file).contains(edit)) { |
320 |
getEditMap().get(file).add(edit); |
321 |
} |
322 |
} |
323 |
} |
324 |
|
325 |
private void findStartAndEndPoint(String xmlContent, |
326 |
List<String> textStrings, List<String> strings, IDocument doc, |
327 |
String startTag, String endTag, List<String> identifyObject) { |
328 |
String s = ""; //$NON-NLS-1$ |
329 |
|
330 |
for (int p = 0; p < strings.size(); p++) { |
331 |
s = s + strings.get(p); |
332 |
} |
333 |
textStrings.add(s); |
334 |
|
335 |
for (int i = 0; i < strings.size(); i++) { |
336 |
String st = strings.get(i); |
337 |
st = st.trim(); |
338 |
strings.remove(i); |
339 |
strings.add(i, st); |
340 |
} |
341 |
|
342 |
if (strings.containsAll(identifyObject)) { |
343 |
setOffset(xmlContent.indexOf(s)); |
344 |
setLength(s.length()); |
345 |
} else { |
346 |
String textDoc = xmlContent; |
347 |
for (int j = 0; j < textStrings.size(); j++) { |
348 |
if (textDoc.contains(textStrings.get(j))) { |
349 |
textDoc = textDoc.replace(textStrings.get(j), ""); //$NON-NLS-1$ |
350 |
} |
351 |
} |
352 |
|
353 |
IDocument doc1 = new Document(textDoc); |
354 |
List<String> strings1 = markTextToBeDeletedFromXML(doc1, startTag, |
355 |
endTag); |
356 |
findStartAndEndPoint(xmlContent, textStrings, strings1, doc, |
357 |
startTag, endTag, identifyObject); |
358 |
} |
359 |
} |
360 |
|
361 |
private List<String> markTextToBeDeletedFromXML(IDocument doc, |
362 |
String startTag, String endTag) { |
363 |
int linesSum = doc.getNumberOfLines(); |
364 |
List<String> strings = new ArrayList<String>(); |
365 |
findElementInXML: for (int l = 0; l < linesSum; l++) { |
366 |
try { |
367 |
int start = doc.getLineOffset(l); |
368 |
|
369 |
int lengthOfString = doc.getLineLength(l); |
370 |
|
371 |
if (start != 0 && lengthOfString != 0) { |
372 |
String stringOfWebArtifact = doc.get(start, lengthOfString); |
373 |
if (stringOfWebArtifact.contains(startTag)) { |
374 |
strings.add(stringOfWebArtifact); |
375 |
for (int m = l + 1; m < linesSum; m++) { |
376 |
int nextStart = doc.getLineOffset(m); |
377 |
int nextStringLength = doc.getLineLength(m); |
378 |
String nextStringOfWebArtifact = doc.get(nextStart, |
379 |
nextStringLength); |
380 |
strings.add(nextStringOfWebArtifact); |
381 |
if (nextStringOfWebArtifact.contains(endTag)) { |
382 |
break findElementInXML; |
383 |
} |
384 |
} |
385 |
} |
386 |
} |
387 |
|
388 |
} catch (Exception e) { |
389 |
e.printStackTrace(); |
390 |
} |
391 |
} |
392 |
return strings; |
393 |
} |
394 |
|
395 |
private String getFilterMappingDisplay(FilterMapping element) { |
396 |
UrlPatternType urlPatterns = null; |
397 |
String value = null; |
398 |
if (element.getUrlPatterns().size() > 0){ |
399 |
urlPatterns = element.getUrlPatterns().get(0); |
400 |
value = urlPatterns.getValue(); |
401 |
}else{ |
402 |
FeatureMap group = element.getGroup(); |
403 |
if (group.size() >0) { |
404 |
Entry entry = group.get(0); |
405 |
value = entry.getValue().toString(); |
406 |
} |
407 |
} |
408 |
return value + " -> " + element.getFilterName(); //$NON-NLS-1$ |
409 |
} |
410 |
|
411 |
private String getServletMappingDisplay(ServletMapping element) { |
412 |
UrlPatternType urlPatterns = element.getUrlPatterns().get(0); |
413 |
return urlPatterns.getValue() + " -> " + element.getServletName(); //$NON-NLS-1$; |
414 |
} |
415 |
|
416 |
public void setfStartTag(String startTag) { |
417 |
this.fStartTag = startTag; |
418 |
} |
419 |
|
420 |
public String getfStartTag() { |
421 |
return fStartTag; |
422 |
} |
423 |
|
424 |
public void setfEndTag(String endTag) { |
425 |
this.fEndTag = endTag; |
426 |
} |
427 |
|
428 |
public String getfEndTag() { |
429 |
return fEndTag; |
430 |
} |
431 |
|
432 |
} |
433 |
|
434 |
|