1 // Copyright (c) 2005 by Leif Frenzel. All rights reserved.
2 // See http://leiffrenzel.de
3 // modified for phpeclipse.de project by axelcl
4 package net.sourceforge.phpdt.ltk.core;
6 import java.io.ByteArrayOutputStream;
7 import java.io.InputStream;
8 import java.util.ArrayList;
10 import net.sourceforge.phpdt.core.ISourceRange;
11 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
12 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
13 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
14 import net.sourceforge.phpdt.internal.compiler.parser.SyntaxError;
15 import net.sourceforge.phpdt.internal.core.SourceMethod;
16 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.IProject;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.ltk.core.refactoring.CompositeChange;
25 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
26 import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
27 import org.eclipse.ltk.core.refactoring.participants.IConditionChecker;
28 import org.eclipse.ltk.core.refactoring.participants.ValidateEditChecker;
32 * delegate object that contains the logic used by the processor.
36 public class RenameLocalVariableDelegate extends RenameIdentifierDelegate {
38 public RenameLocalVariableDelegate(final RenameIdentifierInfo info) {
42 RefactoringStatus checkInitialConditions() {
43 RefactoringStatus result = new RefactoringStatus();
44 IFile sourceFile = info.getSourceFile();
45 if (sourceFile == null || !sourceFile.exists()) {
46 result.addFatalError(CoreTexts.renamePropertyDelegate_noSourceFile);
47 } else if (info.getSourceFile().isReadOnly()) {
48 result.addFatalError(CoreTexts.renamePropertyDelegate_roFile);
49 } else if (isEmpty(info.getOldName())) {
50 // || !isPropertyKey( info.getSourceFile(), info.getOldName() ) ) {
51 result.addFatalError(CoreTexts.renamePropertyDelegate_noPHPKey);
56 RefactoringStatus checkFinalConditions(final IProgressMonitor pm, final CheckConditionsContext ctxt) {
57 RefactoringStatus result = new RefactoringStatus();
58 pm.beginTask(CoreTexts.renamePropertyDelegate_checking, 100);
59 // do something long-running here: traverse the entire project (or even
60 // workspace) to look for all *.p files with the same bundle
62 IFile file = info.getSourceFile();
63 IProject project = file.getProject();
65 SourceMethod method = info.getMethod();
66 ISourceRange range = method.getSourceRange();
67 if (project.isNatureEnabled(PHPeclipsePlugin.PHP_NATURE_ID)) {
68 determineMethodOffsets(file, range.getOffset(), range.getLength(), result);
70 } catch (CoreException e) {
71 String msg = "Project: " + project.getLocation().toOSString() + " CoreException " + e.getMessage();
73 } catch (Exception e) {
74 String msg = "Project: " + project.getLocation().toOSString() + " Exception " + e.getMessage();
81 IFile[] files = new IFile[phpFiles.size()];
82 phpFiles.keySet().toArray(files);
83 IConditionChecker checker = ctxt.getChecker(ValidateEditChecker.class);
84 ValidateEditChecker editChecker = (ValidateEditChecker) checker;
85 editChecker.addFiles(files);
91 protected void createChange(final IProgressMonitor pm, final CompositeChange rootChange) {
93 pm.beginTask(CoreTexts.renamePropertyDelegate_collectingChanges, 100);
94 // all files in the same bundle
95 rootChange.addAll(createChangesForContainer(pm));
101 private void determineMethodOffsets(final IFile file, int offset, int length, final RefactoringStatus status) {
102 ArrayList matches = new ArrayList();
104 String content = readFileContent(file, status);
107 // Find a PHPdoc directly before the method
109 Scanner firstScanner = new Scanner(true, false);
110 firstScanner.setSource(content.toCharArray());
111 int fToken = ITerminalSymbols.TokenNameEOF;
113 int phpdocStart = -1;
115 fToken = firstScanner.getNextToken();
116 while (fToken != ITerminalSymbols.TokenNameEOF && start < offset) {
117 if (fToken == ITerminalSymbols.TokenNameCOMMENT_PHPDOC) {
118 phpdocStart = firstScanner.getCurrentTokenStartPosition();
122 fToken = firstScanner.getNextToken();
123 start = firstScanner.getCurrentTokenStartPosition();
126 } catch (InvalidInputException e) {
127 String msg = "File: " + file.getLocation().toOSString() + " InvalidInputException " + e.getMessage();
128 status.addError(msg);
129 } catch (SyntaxError e) {
130 String msg = "File: " + file.getLocation().toOSString() + " SyntaxError " + e.getMessage();
131 status.addError(msg);
135 // Find matches for the word in the PHPdoc+method declaration
137 if (phpdocStart >= 0 && phpdocStart < offset) {
138 length += offset - phpdocStart;
139 offset = phpdocStart;
141 String methodString = content.substring(offset, offset + length);
142 Scanner secondScanner = new Scanner(true, false);
143 secondScanner.setSource(methodString.toCharArray());
144 secondScanner.setPHPMode(true);
145 String wordStr = info.getOldName();
146 boolean renameDQString = info.isRenameDQString();
147 boolean renamePHPdoc = info.isRenamePHPdoc();
148 boolean renameOtherComments = info.isRenameOtherComments();
149 char[] word = wordStr.toCharArray();
151 fToken = ITerminalSymbols.TokenNameEOF;
152 // double quoted string
154 // double quoted string offset
158 fToken = secondScanner.getNextToken();
159 while (fToken != ITerminalSymbols.TokenNameEOF) {
160 if (fToken == ITerminalSymbols.TokenNameVariable) {
161 if (secondScanner.equalsCurrentTokenSource(word)) {
162 // the current variable token is equal to the given word
163 matches.add(Integer.valueOf(secondScanner.getCurrentTokenStartPosition() + offset));
165 } else if (fToken == ITerminalSymbols.TokenNameStringDoubleQuote && renameDQString) {
166 // determine the word in double quoted strings:
167 tokenString = new String(secondScanner.getCurrentTokenSource());
168 tokenOffset = secondScanner.getCurrentTokenStartPosition();
170 while ((index = tokenString.indexOf(wordStr, index + 1)) >= 0) {
171 matches.add(Integer.valueOf(offset + tokenOffset + index));
173 } else if (fToken == ITerminalSymbols.TokenNameCOMMENT_PHPDOC && renamePHPdoc) {
174 tokenString = new String(secondScanner.getCurrentTokenSource());
175 tokenOffset = secondScanner.getCurrentTokenStartPosition();
177 while ((index = tokenString.indexOf(wordStr, index + 1)) >= 0) {
178 matches.add(Integer.valueOf(offset + tokenOffset + index));
180 } else if ( (fToken == ITerminalSymbols.TokenNameCOMMENT_BLOCK || fToken == ITerminalSymbols.TokenNameCOMMENT_LINE) && renameOtherComments) {
181 tokenString = new String(secondScanner.getCurrentTokenSource());
182 tokenOffset = secondScanner.getCurrentTokenStartPosition();
184 while ((index = tokenString.indexOf(wordStr, index + 1)) >= 0) {
185 matches.add(Integer.valueOf(offset + tokenOffset + index));
188 fToken = secondScanner.getNextToken();
191 } catch (InvalidInputException e) {
192 String msg = "File: " + file.getLocation().toOSString() + " InvalidInputException " + e.getMessage();
193 status.addError(msg);
194 } catch (SyntaxError e) {
195 String msg = "File: " + file.getLocation().toOSString() + " SyntaxError " + e.getMessage();
196 status.addError(msg);
199 } catch (Exception e) {
200 String msg = "File: " + file.getLocation().toOSString() + " Exception " + e.getMessage();
201 status.addError(msg);
203 if (matches.size() > 0) {
204 phpFiles.put(file, matches);
208 private String readFileContent(final IFile file, final RefactoringStatus refStatus) {
209 String result = null;
211 InputStream is = file.getContents();
212 byte[] buf = new byte[1024];
213 ByteArrayOutputStream bos = new ByteArrayOutputStream();
214 int len = is.read(buf);
216 bos.write(buf, 0, len);
220 result = new String(bos.toByteArray());
221 } catch (Exception ex) {
222 String msg = ex.toString();
223 refStatus.addFatalError(msg);
224 String pluginId = PHPeclipsePlugin.getPluginId();
225 IStatus status = new Status(IStatus.ERROR, pluginId, 0, msg, ex);
226 PHPeclipsePlugin.getDefault().getLog().log(status);