Skip to main content
summaryrefslogtreecommitdiffstats
blob: e505f4dd300b4f25ce3a3c61ad478cb7cd420162 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*******************************************************************************
 * Copyright (c) 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsp.ui.internal.java.refactoring;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.search.SearchDocument;
import org.eclipse.jdt.core.search.SearchMatch;
import org.eclipse.jdt.core.search.SearchRequestor;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jst.jsp.core.internal.java.search.JSPSearchSupport;
import org.eclipse.jst.jsp.core.internal.java.search.JavaSearchDocumentDelegate;
import org.eclipse.jst.jsp.ui.internal.JSPUIMessages;
import org.eclipse.jst.jsp.ui.internal.Logger;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.DocumentChange;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.osgi.util.NLS;
import org.eclipse.text.edits.MalformedTreeException;
import org.eclipse.text.edits.MultiTextEdit;
import org.eclipse.text.edits.ReplaceEdit;
import org.eclipse.text.edits.TextEdit;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.WorkspaceModifyOperation;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.wst.sse.core.internal.document.DocumentReader;
import org.eclipse.wst.sse.core.internal.encoding.CodedStreamCreator;

/**
 * Creates document change(s) for an IJavaElement rename.
 * Changes are created for every type "match" in the workspace
 * @author pavery
 */
public class BasicRefactorSearchRequestor extends SearchRequestor {
	
	/**
	 * Workspace operation to perform save on model for updated documents.
	 * Should only be done on models not open in an editor.
	 */
	private class SaveJspFileOp extends WorkspaceModifyOperation {
		
		private IDocument fJSPDoc = null;
		private IFile fJSPFile = null;
		
		public SaveJspFileOp(IFile jspFile, IDocument jspDoc) {
			this.fJSPDoc = jspDoc;
			this.fJSPFile = jspFile;
		}
		
		protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException {
			
			// https://w3.opensource.ibm.com/bugzilla/show_bug.cgi?id=3765
			// save file w/ no intermediate model creation
			
			CodedStreamCreator codedStreamCreator = new CodedStreamCreator();
			Reader reader = new DocumentReader(this.fJSPDoc);
			codedStreamCreator.set(this.fJSPFile, reader);
			
			ByteArrayOutputStream codedByteStream = null;
			InputStream codedStream = null;
			try {
				codedByteStream = codedStreamCreator.getCodedByteArrayOutputStream();
				codedStream = new ByteArrayInputStream(codedByteStream.toByteArray());
				if (this.fJSPFile.exists())
					this.fJSPFile.setContents(codedStream, true, true, null);
				else
					this.fJSPFile.create(codedStream, false, null);
				
			} catch (CoreException e) {
				Logger.logException(e);
			} catch (IOException e) {
				Logger.logException(e);
			}
			finally {
				try {
					if(codedByteStream != null)
						codedByteStream.close();
					if(codedStream != null)
						codedStream.close();
				}
				catch (IOException e){
					// unlikely
				}
			}
		}
	}
	// end inner class SaveJspFileOp
	
	/**
	 * Change class that wraps a text edit on the jsp document
	 */
	private class RenameChange extends DocumentChange {

		private TextEdit fEdit = null;
		private IFile fJSPFile = null;
		private IDocument fJSPDoc = null;
		private String fDescription = JSPUIMessages.BasicRefactorSearchRequestor_0;
		
		public RenameChange(IFile jspFile, IDocument jspDoc, TextEdit edit, String description) {
			super(JSPUIMessages.BasicRefactorSearchRequestor_6, jspDoc);
			this.fEdit = edit;
			this.fJSPFile = jspFile;
			this.fJSPDoc = jspDoc;
			this.fDescription = description;
		}
		
		public RefactoringStatus isValid(IProgressMonitor pm)throws CoreException {
			return new RefactoringStatus();
		}
		
		public IDocument getPreviewDocument(IProgressMonitor pm) throws CoreException {
			IDocument copyDoc = new Document(fJSPDoc.get());
			try {
				fEdit.apply(copyDoc);
			}
			catch (MalformedTreeException e) {
				// ignore
			}
			catch (BadLocationException e) {
				// ignore
			}
			return copyDoc;
		}
		
		public Change perform(IProgressMonitor pm) throws CoreException {
			RenameChange undoChange = null;
			try {
				
				if(!isOpenInEditor(this.fJSPDoc)) {
					// apply edit to JSP doc AND save model
					undoChange = new RenameChange(this.fJSPFile, this.fJSPDoc, this.fEdit.apply(fJSPDoc), this.fDescription);
					saveFile(this.fJSPFile, this.fJSPDoc);
				}
				else {
					// just apply edit to JSP document
					undoChange = new RenameChange(this.fJSPFile, this.fJSPDoc, this.fEdit.apply(fJSPDoc), this.fDescription);
				}
				
			} catch (MalformedTreeException e) {
				Logger.logException(e);
			} catch (BadLocationException e) {
				Logger.logException(e);
			}
			return undoChange;
		}
		
		/**
		 * Performed in an operation since it modifies resources in the workspace
		 * @param jspDoc
		 * @throws CoreException
		 */
		private void saveFile(IFile jspFile, IDocument jspDoc) {
			
			SaveJspFileOp op  = new SaveJspFileOp(jspFile, jspDoc);
			
			try {
				op.run(JSPSearchSupport.getInstance().getProgressMonitor());
			} catch (InvocationTargetException e) {
				Logger.logException(e);
			} catch (InterruptedException e) {
				Logger.logException(e);
			}
		}

		/**
		 * Checks if a document is open in an editor
		 * @param jspDoc
		 * @return
		 */
		private boolean isOpenInEditor(IDocument jspDoc) {
			IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
			IWorkbenchWindow w = null;
			for (int i = 0; i < windows.length; i++) {

				w = windows[i];
				IWorkbenchPage page = w.getActivePage();
				if (page != null) {

					IEditorReference[] references = page.getEditorReferences();
					IEditorPart editor = null;
					Object o = null;
					IDocument doc = null;
					for (int j = 0; j < references.length; j++) {

						editor = references[j].getEditor(true);
						// https://w3.opensource.ibm.com/bugzilla/show_bug.cgi?id=3764
						// use adapter to get ITextEditor (for things like
						// page designer)
						o = editor.getAdapter(ITextEditor.class);
						if (o != null && o instanceof ITextEditor) {

							doc = ((ITextEditor) o).getDocumentProvider().getDocument(editor.getEditorInput());
							if (doc != null && doc.equals(jspDoc)) {
								return true;
							}
						}
					}
				}
			}
			return false;
		}

		public String getName() {
			return this.fDescription;
		}
		
		public Object getModifiedElement() {
			return getElement();
		}
	}
	// end inner class RenameChange
	
	
	/** The type being renamed (the old type)*/
	IJavaElement fElement = null;
	/** The new name of the type being renamed*/
	private String fNewName = ""; //$NON-NLS-1$
	/** maps a JSPSearchDocument path -> MultiTextEdit for the java file*/
	private HashMap fSearchDocPath2JavaEditMap = null;
	
	public BasicRefactorSearchRequestor(IJavaElement element, String newName) {
		this.fNewName = newName;
		this.fElement = element;
		this.fSearchDocPath2JavaEditMap = new HashMap();
	}
	
	public IJavaElement getElement() {
		return this.fElement;
	}

	/**
	 * @return the new name for the Type
	 */
	public String getNewName() {
		return this.fNewName;
	}
	
	/**
	 * @see org.eclipse.jdt.core.search.SearchRequestor#acceptSearchMatch(org.eclipse.jdt.core.search.SearchMatch)
	 */
	public void acceptSearchMatch(SearchMatch javaMatch) throws CoreException {
		
		String matchDocumentPath = javaMatch.getResource().getFullPath().toString();
		SearchDocument searchDoc = JSPSearchSupport.getInstance().getSearchDocument(matchDocumentPath);
	
		if (searchDoc != null && searchDoc instanceof JavaSearchDocumentDelegate) {
	
			String renameText = getRenameText((JavaSearchDocumentDelegate)searchDoc, javaMatch);
			
			// add it for the correct document
			addJavaEdit(searchDoc.getPath(), new ReplaceEdit(javaMatch.getOffset(), javaMatch.getLength(), renameText));
		}
	}
	
	/**
	 * @param searchDoc
	 * @return
	 */
	protected String getRenameText(JavaSearchDocumentDelegate searchDoc, SearchMatch javaMatch) {
		return getNewName();
	}

	/**
	 * Adds to the multi edit for a give java document.
	 * @param javaDocument
	 * @param javaEdit
	 */
	private void addJavaEdit(String searchDocPath, ReplaceEdit javaEdit) {
		
		Object o = this.fSearchDocPath2JavaEditMap.get(searchDocPath);
		if(o != null) {

			MultiTextEdit multi = (MultiTextEdit)o;
			multi.addChild(javaEdit);
		}
		else {
			// use a multi edit so doc position offsets get updated automatically
			// when adding multiple child edits
			MultiTextEdit multi = new MultiTextEdit();
			multi.addChild(javaEdit);
			this.fSearchDocPath2JavaEditMap.put(searchDocPath, multi);
		}
	}
	
	/**
	 * 
	 * @return all JSP changes for the search matches for the given Type
	 */
	public Change[] getChanges() {
		
		JSPSearchSupport support = JSPSearchSupport.getInstance();
		List changes = new ArrayList();
		Iterator keys = fSearchDocPath2JavaEditMap.keySet().iterator();
		String searchDocPath = null;
		SearchDocument delegate = null;
		
		while(keys.hasNext()) {
			// create on the fly
			searchDocPath = (String)keys.next();
			MultiTextEdit javaEdit = (MultiTextEdit)fSearchDocPath2JavaEditMap.get(searchDocPath);
			delegate = support.getSearchDocument(searchDocPath);
			
			if(delegate != null && delegate instanceof JavaSearchDocumentDelegate) {
				JavaSearchDocumentDelegate javaDelegate = (JavaSearchDocumentDelegate)delegate;
				changes.add(createChange(javaDelegate, javaDelegate.getJspTranslation().getJspEdit(javaEdit)));
			}
		}
		return (Change[])changes.toArray(new Change[changes.size()]);
	}
	
	private Change createChange(JavaSearchDocumentDelegate searchDoc, TextEdit edit) {
		
		IDocument doc = searchDoc.getJspTranslation().getJspDocument();
		String file = searchDoc.getFile().getName();
		String description = getDescription();
		try {
			// document lines are 0 based
			String lineNumber = Integer.toString(doc.getLineOfOffset(edit.getOffset()) + 1);
			description += " " + NLS.bind(JSPUIMessages.BasicRefactorSearchRequestor_1, new String[]{file, lineNumber}); //$NON-NLS-1$
		} 
		catch (BadLocationException e) {
			Logger.logException(e);
		}
		return new RenameChange(searchDoc.getFile(), doc, edit, description);
	}
	
	// https://w3.opensource.ibm.com/bugzilla/show_bug.cgi?id=3205
	// only relevant for IType refactorings
	protected boolean isFullyQualified(String matchText) {
		if(getElement() instanceof IType) {
			String pkg = ((IType)getElement()).getPackageFragment().getElementName();
			return matchText.startsWith(pkg);
		}
		return false;
	}

	/**
	 * Subclasses should override to better describe the change.
	 * @return
	 */
	protected String getDescription() {
		return ""; //$NON-NLS-1$
	}
}

Back to the top