Automated import from //branches/cupcake/...@142585,142585

This commit is contained in:
Raphael Moll
2009-03-25 15:03:39 -07:00
committed by The Android Open Source Project
parent 1e163c86ed
commit 660c0c97ee

View File

@@ -57,6 +57,7 @@ import org.eclipse.ltk.core.refactoring.CompositeChange;
import org.eclipse.ltk.core.refactoring.Refactoring; import org.eclipse.ltk.core.refactoring.Refactoring;
import org.eclipse.ltk.core.refactoring.RefactoringChangeDescriptor; import org.eclipse.ltk.core.refactoring.RefactoringChangeDescriptor;
import org.eclipse.ltk.core.refactoring.RefactoringStatus; import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.TextEditChangeGroup;
import org.eclipse.ltk.core.refactoring.TextFileChange; import org.eclipse.ltk.core.refactoring.TextFileChange;
import org.eclipse.text.edits.InsertEdit; import org.eclipse.text.edits.InsertEdit;
import org.eclipse.text.edits.MultiTextEdit; import org.eclipse.text.edits.MultiTextEdit;
@@ -416,25 +417,20 @@ class ExtractStringRefactoring extends Refactoring {
if (!isResIdDuplicate(mTargetXmlFileWsPath, mXmlStringId)) { if (!isResIdDuplicate(mTargetXmlFileWsPath, mXmlStringId)) {
// We actually change it only if the ID doesn't exist yet // We actually change it only if the ID doesn't exist yet
TextFileChange xmlChange = new TextFileChange(getName(), (IFile) targetXml); Change change = createXmlChange((IFile) targetXml, mXmlStringId, mTokenString,
xmlChange.setTextType("xml"); //$NON-NLS-1$ status, SubMonitor.convert(monitor, 1));
TextEdit edit = createXmlEdit((IFile) targetXml, mXmlStringId, mTokenString); if (change != null) {
if (edit == null) { mChanges.add(change);
status.addFatalError(String.format("Failed to modify file %1$s",
mTargetXmlFileWsPath));
} }
xmlChange.setEdit(edit);
mChanges.add(xmlChange);
} }
monitor.worked(1);
if (status.hasError()) { if (status.hasError()) {
return status; return status;
} }
// Prepare the change to the Java compilation unit // Prepare the change to the Java compilation unit
List<Change> changes = computeJavaChanges(mUnit, mXmlStringId, mTokenString, status, List<Change> changes = computeJavaChanges(mUnit, mXmlStringId, mTokenString,
SubMonitor.convert(monitor, 1)); status, SubMonitor.convert(monitor, 1));
if (changes != null) { if (changes != null) {
mChanges.addAll(changes); mChanges.addAll(changes);
} }
@@ -448,37 +444,47 @@ class ExtractStringRefactoring extends Refactoring {
} }
/** /**
* Internal helper that actually prepares the {@link TextEdit} that adds the given * Internal helper that actually prepares the {@link Change} that adds the given
* ID to the given XML File. * ID to the given XML File.
* <p/> * <p/>
* This does not actually modify the file. * This does not actually modify the file.
* *
* @param xmlFile The file resource to modify. * @param targetXml The file resource to modify.
* @param replacementStringId The new ID to insert. * @param xmlStringId The new ID to insert.
* @param oldString The old string, which will be the value in the XML string. * @param tokenString The old string, which will be the value in the XML string.
* @return A new {@link TextEdit} that describes how to change the file. * @return A new {@link TextEdit} that describes how to change the file.
*/ */
private TextEdit createXmlEdit(IFile xmlFile, String replacementStringId, String oldString) { private Change createXmlChange(IFile targetXml,
String xmlStringId,
String tokenString,
RefactoringStatus status,
SubMonitor subMonitor) {
if (!xmlFile.exists()) { TextFileChange xmlChange = new TextFileChange(getName(), targetXml);
xmlChange.setTextType("xml"); //$NON-NLS-1$
TextEdit edit = null;
TextEditGroup editGroup = null;
if (!targetXml.exists()) {
// The XML file does not exist. Simply create it. // The XML file does not exist. Simply create it.
StringBuilder content = new StringBuilder(); StringBuilder content = new StringBuilder();
content.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); //$NON-NLS-1$ content.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); //$NON-NLS-1$
content.append("<resources>\n"); //$NON-NLS-1$ content.append("<resources>\n"); //$NON-NLS-1$
content.append(" <string name=\""). //$NON-NLS-1$ content.append(" <string name=\""). //$NON-NLS-1$
append(replacementStringId). append(xmlStringId).
append("\">"). //$NON-NLS-1$ append("\">"). //$NON-NLS-1$
append(oldString). append(tokenString).
append("</string>\n"); //$NON-NLS-1$ append("</string>\n"); //$NON-NLS-1$
content.append("<resources>\n"); //$NON-NLS-1$ content.append("<resources>\n"); //$NON-NLS-1$
return new InsertEdit(0, content.toString()); edit = new InsertEdit(0, content.toString());
} editGroup = new TextEditGroup("Create ID in new XML file", edit);
} else {
// The file exist. Attempt to parse it as a valid XML document. // The file exist. Attempt to parse it as a valid XML document.
try { try {
int[] indices = new int[2]; int[] indices = new int[2];
if (findXmlOpeningTagPos(xmlFile.getContents(), "resources", indices)) { //$NON-NLS-1$ if (findXmlOpeningTagPos(targetXml.getContents(), "resources", indices)) { //$NON-NLS-1$
// Indices[1] indicates whether we found > or />. It can only be 1 or 2. // Indices[1] indicates whether we found > or />. It can only be 1 or 2.
// Indices[0] is the position of the first character of either > or />. // Indices[0] is the position of the first character of either > or />.
// //
@@ -492,28 +498,40 @@ class ExtractStringRefactoring extends Refactoring {
StringBuilder content = new StringBuilder(); StringBuilder content = new StringBuilder();
content.append(">\n"); //$NON-NLS-1$ content.append(">\n"); //$NON-NLS-1$
content.append(" <string name=\""). //$NON-NLS-1$ content.append(" <string name=\""). //$NON-NLS-1$
append(replacementStringId). append(xmlStringId).
append("\">"). //$NON-NLS-1$ append("\">"). //$NON-NLS-1$
append(oldString). append(tokenString).
append("</string>"); //$NON-NLS-1$ append("</string>"); //$NON-NLS-1$
if (len == 2) { if (len == 2) {
content.append("\n</resources>"); //$NON-NLS-1$ content.append("\n</resources>"); //$NON-NLS-1$
} }
return new ReplaceEdit(offset, len, content.toString()); edit = new ReplaceEdit(offset, len, content.toString());
editGroup = new TextEditGroup("Insert ID in XML file", edit);
} }
} catch (CoreException e) { } catch (CoreException e) {
// Failed to read file. Ignore. Will return null below. // Failed to read file. Ignore. Will return null below.
} }
}
if (edit == null) {
status.addFatalError(String.format("Failed to modify file %1$s",
mTargetXmlFileWsPath));
return null; return null;
} }
xmlChange.setEdit(edit);
// The TextEditChangeGroup let the user toggle this change on and off later.
xmlChange.addTextEditChangeGroup(new TextEditChangeGroup(xmlChange, editGroup));
subMonitor.worked(1);
return xmlChange;
}
/** /**
* Parse an XML input stream, looking for an opening tag. * Parse an XML input stream, looking for an opening tag.
* <p/> * <p/>
* If found, returns the character offet in the buffer of the closing bracket of that * If found, returns the character offest in the buffer of the closing bracket of that
* tag, e.g. the position of > in "<resources>". The first character is at offset 0. * tag, e.g. the position of > in "<resources>". The first character is at offset 0.
* <p/> * <p/>
* The implementation here relies on a simple character-based parser. No DOM nor SAX * The implementation here relies on a simple character-based parser. No DOM nor SAX
@@ -622,6 +640,9 @@ class ExtractStringRefactoring extends Refactoring {
return false; return false;
} }
/**
* Computes the changes to be made to Java file(s) and returns a list of {@link Change}.
*/
private List<Change> computeJavaChanges(ICompilationUnit unit, private List<Change> computeJavaChanges(ICompilationUnit unit,
String xmlStringId, String xmlStringId,
String tokenString, String tokenString,
@@ -700,14 +721,16 @@ class ExtractStringRefactoring extends Refactoring {
// ImportRewrite will allow us to add the new type to the imports and will resolve // ImportRewrite will allow us to add the new type to the imports and will resolve
// what the Java source must reference, e.g. the FQCN or just the simple name. // what the Java source must reference, e.g. the FQCN or just the simple name.
ImportRewrite ir = ImportRewrite.create((CompilationUnit) node, true); ImportRewrite importRewrite = ImportRewrite.create((CompilationUnit) node, true);
String Rqualifier = packageName + ".R"; //$NON-NLS-1$ String Rqualifier = packageName + ".R"; //$NON-NLS-1$
Rqualifier = ir.addImport(Rqualifier); Rqualifier = importRewrite.addImport(Rqualifier);
// Rewrite the AST itself via an ASTVisitor // Rewrite the AST itself via an ASTVisitor
AST ast = node.getAST(); AST ast = node.getAST();
ASTRewrite ar = ASTRewrite.create(ast); ASTRewrite astRewrite = ASTRewrite.create(ast);
ReplaceStringsVisitor visitor = new ReplaceStringsVisitor(ast, ar, ArrayList<TextEditGroup> astEditGroups = new ArrayList<TextEditGroup>();
ReplaceStringsVisitor visitor = new ReplaceStringsVisitor(
ast, astRewrite, astEditGroups,
tokenString, Rqualifier, xmlStringId); tokenString, Rqualifier, xmlStringId);
node.accept(visitor); node.accept(visitor);
@@ -716,13 +739,13 @@ class ExtractStringRefactoring extends Refactoring {
MultiTextEdit edit = new MultiTextEdit(); MultiTextEdit edit = new MultiTextEdit();
// Create the edit to change the imports, only if anything changed // Create the edit to change the imports, only if anything changed
TextEdit subEdit = ir.rewriteImports(subMonitor.newChild(1)); TextEdit subEdit = importRewrite.rewriteImports(subMonitor.newChild(1));
if (subEdit.hasChildren()) { if (subEdit.hasChildren()) {
edit.addChild(subEdit); edit.addChild(subEdit);
} }
// Create the edit to change the Java source, only if anything changed // Create the edit to change the Java source, only if anything changed
subEdit = ar.rewriteAST(); subEdit = astRewrite.rewriteAST();
if (subEdit.hasChildren()) { if (subEdit.hasChildren()) {
edit.addChild(subEdit); edit.addChild(subEdit);
} }
@@ -730,6 +753,13 @@ class ExtractStringRefactoring extends Refactoring {
// Only create a change set if any edit was collected // Only create a change set if any edit was collected
if (edit.hasChildren()) { if (edit.hasChildren()) {
change.setEdit(edit); change.setEdit(edit);
// Create TextEditChangeGroups which let the user turn changes on or off
// individually. This must be done after the change.setEdit() call above.
for (TextEditGroup editGroup : astEditGroups) {
change.addTextEditChangeGroup(new TextEditChangeGroup(change, editGroup));
}
changes.add(change); changes.add(change);
} }
@@ -741,6 +771,8 @@ class ExtractStringRefactoring extends Refactoring {
return changes; return changes;
} }
subMonitor.worked(1);
} catch (CoreException e) { } catch (CoreException e) {
// ImportRewrite.rewriteImports failed. // ImportRewrite.rewriteImports failed.
status.addFatalError(e.getMessage()); status.addFatalError(e.getMessage());
@@ -755,14 +787,17 @@ class ExtractStringRefactoring extends Refactoring {
private final String mOldString; private final String mOldString;
private final String mRQualifier; private final String mRQualifier;
private final String mXmlId; private final String mXmlId;
private final ArrayList<TextEditGroup> mEditGroups;
public ReplaceStringsVisitor(AST ast, public ReplaceStringsVisitor(AST ast,
ASTRewrite astRewrite, ASTRewrite astRewrite,
ArrayList<TextEditGroup> editGroups,
String oldString, String oldString,
String rQualifier, String rQualifier,
String xmlId) { String xmlId) {
mAst = ast; mAst = ast;
mRewriter = astRewrite; mRewriter = astRewrite;
mEditGroups = editGroups;
mOldString = oldString; mOldString = oldString;
mRQualifier = rQualifier; mRQualifier = rQualifier;
mXmlId = xmlId; mXmlId = xmlId;
@@ -776,7 +811,8 @@ class ExtractStringRefactoring extends Refactoring {
SimpleName idName = mAst.newSimpleName(mXmlId); SimpleName idName = mAst.newSimpleName(mXmlId);
QualifiedName newNode = mAst.newQualifiedName(qualifierName, idName); QualifiedName newNode = mAst.newQualifiedName(qualifierName, idName);
TextEditGroup editGroup = new TextEditGroup(getName()); TextEditGroup editGroup = new TextEditGroup("Replace string by ID");
mEditGroups.add(editGroup);
mRewriter.replace(node, newNode, editGroup); mRewriter.replace(node, newNode, editGroup);
} }
return super.visit(node); return super.visit(node);