KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > fix > StringFix


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.fix;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
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 /**
46  * Fix which solves various issues with strings.
47  * Supported:
48  * Add missing $NON-NLS$ tag
49  * Remove unnecessary $NON-NLS$ tag
50  *
51  */

52 public class StringFix implements IFix {
53     
54     private final TextEditGroup[] fEditGroups;
55     private final String JavaDoc 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 JavaDoc 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 JavaDoc label= FixMessages.StringFix_AddNonNls_description;
77             return new StringFix(label, compilationUnit, new TextEditGroup[] {new TextEditGroup(label, addEdit)});
78         } else if (removeEdit != null) {
79             String JavaDoc 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 JavaDoc result= new ArrayList JavaDoc();
108         
109         List JavaDoc missingNLSProblems= new ArrayList JavaDoc();
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 JavaDoc 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 JavaDoc 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 JavaDoc 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()])); //$NON-NLS-1$
146
}
147     
148     private static ReplaceEdit getReplace(int offset, int length, IBuffer buffer, boolean removeLeadingIndents) {
149         
150         String JavaDoc replaceString= new String JavaDoc();
151         boolean hasMoreInComment= false;
152         
153         // look after the tag
154
int next= offset + length;
155         while (next < buffer.getLength()) {
156             char ch= buffer.getChar(next);
157             if (IndentManipulation.isIndentChar(ch)) {
158                 next++; // remove all whitespace
159
} 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= "//"; //$NON-NLS-1$
166
} else {
167                     length= next - offset - 1;
168                 }
169                 hasMoreInComment= true;
170                 break;
171             } else {
172                 replaceString= "//"; //$NON-NLS-1$
173
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 JavaDoc name, CompilationUnit compilationUnit, TextEditGroup[] groups) {
192         fName= name;
193         fCompilationUnit= (ICompilationUnit)compilationUnit.getJavaElement();
194         fEditGroups= groups;
195     }
196
197     /* (non-Javadoc)
198      * @see org.eclipse.jdt.internal.corext.fix.AbstractFix#createChange()
199      */

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 JavaDoc 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     /* (non-Javadoc)
216      * @see org.eclipse.jdt.internal.corext.fix.IFix#getDescription()
217      */

218     public String JavaDoc getDescription() {
219         return fName;
220     }
221
222     /* (non-Javadoc)
223      * @see org.eclipse.jdt.internal.corext.fix.IFix#getCompilationUnit()
224      */

225     public ICompilationUnit getCompilationUnit() {
226         return fCompilationUnit;
227     }
228
229     /* (non-Javadoc)
230      * @see org.eclipse.jdt.internal.corext.fix.IFix#getStatus()
231      */

232     public IStatus getStatus() {
233         return StatusInfo.OK_STATUS;
234     }
235 }
236
Popular Tags