1 11 package org.eclipse.jdt.internal.corext.fix; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.text.edits.ReplaceEdit; 18 import org.eclipse.text.edits.TextEdit; 19 import org.eclipse.text.edits.TextEditGroup; 20 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.IStatus; 23 24 import org.eclipse.ltk.core.refactoring.CategorizedTextEditGroup; 25 import org.eclipse.ltk.core.refactoring.GroupCategory; 26 import org.eclipse.ltk.core.refactoring.GroupCategorySet; 27 import org.eclipse.ltk.core.refactoring.TextChange; 28 29 import org.eclipse.jdt.core.IBuffer; 30 import org.eclipse.jdt.core.ICompilationUnit; 31 import org.eclipse.jdt.core.JavaModelException; 32 import org.eclipse.jdt.core.compiler.IProblem; 33 import org.eclipse.jdt.core.dom.CompilationUnit; 34 import org.eclipse.jdt.core.formatter.IndentManipulation; 35 36 import org.eclipse.jdt.internal.corext.refactoring.changes.CompilationUnitChange; 37 import org.eclipse.jdt.internal.corext.refactoring.changes.TextChangeCompatibility; 38 import org.eclipse.jdt.internal.corext.refactoring.nls.NLSUtil; 39 40 import org.eclipse.jdt.ui.text.java.IProblemLocation; 41 42 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; 43 import org.eclipse.jdt.internal.ui.text.correction.ProblemLocation; 44 45 52 public class StringFix implements IFix { 53 54 private final TextEditGroup[] fEditGroups; 55 private final String fName; 56 private final ICompilationUnit fCompilationUnit; 57 58 public static StringFix createFix(CompilationUnit compilationUnit, IProblemLocation problem, boolean removeNLSTag, boolean addNLSTag) throws CoreException { 59 TextEdit addEdit= null; 60 ICompilationUnit cu= (ICompilationUnit)compilationUnit.getJavaElement(); 61 if (addNLSTag) { 62 addEdit= NLSUtil.createNLSEdit(cu, problem.getOffset()); 63 } 64 ReplaceEdit removeEdit= null; 65 if (removeNLSTag) { 66 IBuffer buffer= cu.getBuffer(); 67 if (buffer != null) { 68 removeEdit= getReplace(problem.getOffset(), problem.getLength(), buffer, true); 69 } 70 } 71 72 if (addEdit != null && removeEdit != null) { 73 String label= FixMessages.StringFix_AddRemoveNonNls_description; 74 return new StringFix(label, compilationUnit, new TextEditGroup[] {new TextEditGroup(label, addEdit), new TextEditGroup(label, removeEdit)}); 75 } else if (addEdit != null) { 76 String label= FixMessages.StringFix_AddNonNls_description; 77 return new StringFix(label, compilationUnit, new TextEditGroup[] {new TextEditGroup(label, addEdit)}); 78 } else if (removeEdit != null) { 79 String label= FixMessages.StringFix_RemoveNonNls_description; 80 return new StringFix(label, compilationUnit, new TextEditGroup[] {new TextEditGroup(label, removeEdit)}); 81 } else { 82 return null; 83 } 84 } 85 86 public static IFix createCleanUp(CompilationUnit compilationUnit, boolean addNLSTag, boolean removeNLSTag) throws CoreException, JavaModelException { 87 if (!addNLSTag && !removeNLSTag) 88 return null; 89 90 IProblem[] problems= compilationUnit.getProblems(); 91 IProblemLocation[] locations= new IProblemLocation[problems.length]; 92 for (int i= 0; i < problems.length; i++) { 93 locations[i]= new ProblemLocation(problems[i]); 94 } 95 return createCleanUp(compilationUnit, addNLSTag, removeNLSTag, locations); 96 } 97 98 public static IFix createCleanUp(CompilationUnit compilationUnit, IProblemLocation[] problems, boolean addNLSTag, boolean removeNLSTag) throws CoreException, JavaModelException { 99 if (!addNLSTag && !removeNLSTag) 100 return null; 101 102 return createCleanUp(compilationUnit, addNLSTag, removeNLSTag, problems); 103 } 104 105 private static IFix createCleanUp(CompilationUnit compilationUnit, boolean addNLSTag, boolean removeNLSTag, IProblemLocation[] problems) throws CoreException, JavaModelException { 106 ICompilationUnit cu= (ICompilationUnit)compilationUnit.getJavaElement(); 107 List result= new ArrayList (); 108 109 List missingNLSProblems= new ArrayList (); 110 for (int i= 0; i < problems.length; i++) { 111 IProblemLocation problem= problems[i]; 112 if (addNLSTag && problem.getProblemId() == IProblem.NonExternalizedStringLiteral) { 113 missingNLSProblems.add(problem); 114 } 115 if (removeNLSTag && problem.getProblemId() == IProblem.UnnecessaryNLSTag) { 116 IBuffer buffer= cu.getBuffer(); 117 if (buffer != null) { 118 TextEdit edit= StringFix.getReplace(problem.getOffset(), problem.getLength(), buffer, false); 119 if (edit != null) { 120 String label= FixMessages.StringFix_RemoveNonNls_description; 121 result.add(new CategorizedTextEditGroup(label, edit, new GroupCategorySet(new GroupCategory(label, label, label)))); 122 } 123 } 124 } 125 } 126 if (!missingNLSProblems.isEmpty()) { 127 int[] positions= new int[missingNLSProblems.size()]; 128 int i=0; 129 for (Iterator iter= missingNLSProblems.iterator(); iter.hasNext();) { 130 IProblemLocation problem= (IProblemLocation)iter.next(); 131 positions[i]= problem.getOffset(); 132 i++; 133 } 134 TextEdit[] edits= NLSUtil.createNLSEdits(cu, positions); 135 if (edits != null) { 136 for (int j= 0; j < edits.length; j++) { 137 String label= FixMessages.StringFix_AddNonNls_description; 138 result.add(new CategorizedTextEditGroup(label, edits[j], new GroupCategorySet(new GroupCategory(label, label, label)))); 139 } 140 } 141 } 142 if (result.isEmpty()) 143 return null; 144 145 return new StringFix("", compilationUnit, (TextEditGroup[])result.toArray(new TextEditGroup[result.size()])); } 147 148 private static ReplaceEdit getReplace(int offset, int length, IBuffer buffer, boolean removeLeadingIndents) { 149 150 String replaceString= new String (); 151 boolean hasMoreInComment= false; 152 153 int next= offset + length; 155 while (next < buffer.getLength()) { 156 char ch= buffer.getChar(next); 157 if (IndentManipulation.isIndentChar(ch)) { 158 next++; } else if (IndentManipulation.isLineDelimiterChar(ch)) { 160 length= next - offset; 161 break; 162 } else if (ch == '/') { 163 next++; 164 if (next == buffer.getLength() || buffer.getChar(next) != '/') { 165 replaceString= "//"; } else { 167 length= next - offset - 1; 168 } 169 hasMoreInComment= true; 170 break; 171 } else { 172 replaceString= "//"; hasMoreInComment= true; 174 break; 175 } 176 } 177 if (!hasMoreInComment && removeLeadingIndents) { 178 while (offset > 0 && IndentManipulation.isIndentChar(buffer.getChar(offset - 1))) { 179 offset--; 180 length++; 181 } 182 } 183 if (length > 0) { 184 ReplaceEdit replaceEdit= new ReplaceEdit(offset, length, replaceString); 185 return replaceEdit; 186 } else { 187 return null; 188 } 189 } 190 191 private StringFix(String name, CompilationUnit compilationUnit, TextEditGroup[] groups) { 192 fName= name; 193 fCompilationUnit= (ICompilationUnit)compilationUnit.getJavaElement(); 194 fEditGroups= groups; 195 } 196 197 200 public TextChange createChange() throws CoreException { 201 if (fEditGroups == null || fEditGroups.length == 0) 202 return null; 203 204 CompilationUnitChange result= new CompilationUnitChange(getDescription(), getCompilationUnit()); 205 for (int i= 0; i < fEditGroups.length; i++) { 206 TextEdit[] edits= fEditGroups[i].getTextEdits(); 207 String groupName= fEditGroups[i].getName(); 208 for (int j= 0; j < edits.length; j++) { 209 TextChangeCompatibility.addTextEdit(result, groupName, edits[j]); 210 } 211 } 212 return result; 213 } 214 215 218 public String getDescription() { 219 return fName; 220 } 221 222 225 public ICompilationUnit getCompilationUnit() { 226 return fCompilationUnit; 227 } 228 229 232 public IStatus getStatus() { 233 return StatusInfo.OK_STATUS; 234 } 235 } 236 | Popular Tags |