1 11 package org.eclipse.jdt.internal.corext.refactoring.nls; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 15 16 import org.eclipse.text.edits.InsertEdit; 17 import org.eclipse.text.edits.TextEdit; 18 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IPath; 21 import org.eclipse.core.runtime.NullProgressMonitor; 22 import org.eclipse.core.runtime.Platform; 23 import org.eclipse.core.runtime.content.IContentType; 24 25 import org.eclipse.core.resources.IFile; 26 import org.eclipse.core.resources.IProject; 27 import org.eclipse.core.resources.ResourcesPlugin; 28 29 import org.eclipse.jface.text.Document; 30 31 import org.eclipse.ltk.core.refactoring.Change; 32 import org.eclipse.ltk.core.refactoring.DocumentChange; 33 import org.eclipse.ltk.core.refactoring.TextChange; 34 import org.eclipse.ltk.core.refactoring.TextFileChange; 35 36 import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility; 37 import org.eclipse.jdt.internal.corext.refactoring.Checks; 38 import org.eclipse.jdt.internal.corext.refactoring.changes.TextChangeCompatibility; 39 import org.eclipse.jdt.internal.corext.refactoring.nls.changes.CreateTextFileChange; 40 import org.eclipse.jdt.internal.corext.util.Messages; 41 42 43 public class NLSPropertyFileModifier { 44 45 public static Change create(NLSSubstitution[] nlsSubstitutions, IPath propertyFilePath) throws CoreException { 46 47 String name= Messages.format(NLSMessages.NLSPropertyFileModifier_change_name, propertyFilePath.toString()); 48 TextChange textChange= null; 49 if (!Checks.resourceExists(propertyFilePath)) { 50 IProject project= getFileHandle(propertyFilePath).getProject(); 51 String lineDelimiter= StubUtility.getLineDelimiterPreference(project); 52 Document document= new Document(); 53 document.setInitialLineDelimiter(lineDelimiter); 54 textChange= new DocumentChange(name, document); 55 addChanges(textChange, nlsSubstitutions); 56 textChange.perform(new NullProgressMonitor()); 57 58 String encoding= null; 59 IContentType javaPropertiesContentType= Platform.getContentTypeManager().getContentType("org.eclipse.jdt.core.javaProperties"); IContentType[] contentTypes= Platform.getContentTypeManager().findContentTypesFor(propertyFilePath.lastSegment()); 61 if (contentTypes.length == 0 || contentTypes.length > 1 || !contentTypes[0].equals(javaPropertiesContentType)) { 62 if (javaPropertiesContentType != null) 63 encoding= javaPropertiesContentType.getDefaultCharset(); 64 if (encoding == null) 65 encoding= "ISO-8859-1"; } 67 68 return new CreateTextFileChange(propertyFilePath, textChange.getCurrentContent(new NullProgressMonitor()), encoding, "properties"); } 70 71 textChange= new TextFileChange(name, getPropertyFile(propertyFilePath)); 72 textChange.setTextType("properties"); 74 addChanges(textChange, nlsSubstitutions); 75 76 return textChange; 77 } 78 79 private static IFile getPropertyFile(IPath propertyFilePath) { 80 return (IFile) ResourcesPlugin.getWorkspace().getRoot().findMember(propertyFilePath); 81 } 82 83 private static IFile getFileHandle(IPath propertyFilePath) { 84 if (propertyFilePath == null) 85 return null; 86 return ResourcesPlugin.getWorkspace().getRoot().getFile(propertyFilePath); 87 } 88 89 private static void addChanges(TextChange textChange, NLSSubstitution[] substitutions) throws CoreException { 90 PropertyFileDocumentModel model= new PropertyFileDocumentModel(textChange.getCurrentDocument(new NullProgressMonitor())); 91 92 Map newKeyToSubstMap= getNewKeyToSubstitutionMap(substitutions); 93 Map oldKeyToSubstMap= getOldKeyToSubstitutionMap(substitutions); 94 95 addInsertEdits(textChange, substitutions, newKeyToSubstMap, oldKeyToSubstMap, model); 96 addRemoveEdits(textChange, substitutions, newKeyToSubstMap, oldKeyToSubstMap, model); 97 addReplaceEdits(textChange, substitutions, newKeyToSubstMap, oldKeyToSubstMap, model); 98 } 99 100 103 static HashMap getNewKeyToSubstitutionMap(NLSSubstitution[] substitutions) { 104 HashMap keyToSubstMap= new HashMap (substitutions.length); 105 for (int i= 0; i < substitutions.length; i++) { 107 NLSSubstitution curr= substitutions[i]; 108 if (curr.getState() == NLSSubstitution.EXTERNALIZED) { 109 NLSSubstitution val= (NLSSubstitution) keyToSubstMap.get(curr.getKey()); 110 if (val == null || (val.hasPropertyFileChange() && !curr.hasPropertyFileChange())) { 111 keyToSubstMap.put(curr.getKey(), curr); } 113 } 114 } 115 return keyToSubstMap; 116 } 117 118 121 static HashMap getOldKeyToSubstitutionMap(NLSSubstitution[] substitutions) { 122 HashMap keyToSubstMap= new HashMap (substitutions.length); 123 for (int i= 0; i < substitutions.length; i++) { 125 NLSSubstitution curr= substitutions[i]; 126 if (curr.getInitialState() == NLSSubstitution.EXTERNALIZED) { 127 String key= curr.getInitialKey(); 128 if (key != null) { 129 NLSSubstitution fav= (NLSSubstitution) keyToSubstMap.get(key); 130 if (fav == null || (fav.hasStateChanged() && !curr.hasStateChanged())) { 131 keyToSubstMap.put(key, curr); } 133 } 134 } 135 } 136 return keyToSubstMap; 137 } 138 139 static boolean doReplace(NLSSubstitution substitution, Map newKeyToSubstMap, Map oldKeyToSubstMap) { 140 if (substitution.getState() != NLSSubstitution.EXTERNALIZED || substitution.hasStateChanged() || substitution.getInitialValue() == null) { 141 return false; } 143 if (oldKeyToSubstMap.get(substitution.getInitialKey()) != substitution) { 144 return false; } 146 if (substitution.isKeyRename() || substitution.isValueRename()) { 147 if (newKeyToSubstMap.get(substitution.getKey()) == substitution) { return true; 149 } 150 } 151 return false; 152 } 153 154 private static void addReplaceEdits(TextChange textChange, NLSSubstitution[] substitutions, Map newKeyToSubstMap, Map oldKeyToSubstMap, PropertyFileDocumentModel model) { 155 for (int i= 0; i < substitutions.length; i++) { 156 NLSSubstitution substitution= substitutions[i]; 157 if (doReplace(substitution, newKeyToSubstMap, oldKeyToSubstMap)) { 158 KeyValuePair initialPair= new KeyValuePair(substitution.getInitialKey(), substitution.getInitialValue()); 159 KeyValuePair newPair= new KeyValuePair(substitution.getKey(), substitution.getValueNonEmpty()); 160 TextEdit edit= model.replace(initialPair, newPair); 161 if (edit != null) { 162 TextChangeCompatibility.addTextEdit(textChange, Messages.format(NLSMessages.NLSPropertyFileModifier_replace_entry, substitution.getKey()), edit); 163 } 164 } 165 } 166 } 167 168 static boolean doInsert(NLSSubstitution substitution, Map newKeyToSubstMap, Map oldKeyToSubstMap) { 169 if (substitution.getState() != NLSSubstitution.EXTERNALIZED) { 170 return false; } 172 if (!substitution.hasStateChanged() && substitution.getInitialValue() != null) { 173 if (!substitution.isKeyRename() || oldKeyToSubstMap.get(substitution.getInitialKey()) == substitution) { 174 return false; } 176 } 177 if (newKeyToSubstMap.get(substitution.getKey()) == substitution) { return true; 179 } 180 return false; 181 } 182 183 private static void addInsertEdits(TextChange textChange, NLSSubstitution[] substitutions, Map newKeyToSubstMap, Map oldKeyToSubstMap, PropertyFileDocumentModel model) { 184 for (int i= 0; i < substitutions.length; i++) { 185 NLSSubstitution substitution= substitutions[i]; 186 if (doInsert(substitution, newKeyToSubstMap, oldKeyToSubstMap)) { 187 String value= substitution.getValueNonEmpty(); 188 KeyValuePair curr= new KeyValuePair(substitution.getKey(), value); 189 190 InsertEdit insert= model.insert(curr); 191 String message= Messages.format(NLSMessages.NLSPropertyFileModifier_add_entry, curr.getKey()); 192 TextChangeCompatibility.addTextEdit(textChange, message, insert); 193 } 194 } 195 } 196 197 static boolean doRemove(NLSSubstitution substitution, Map newKeyToSubstMap, Map oldKeyToSubstMap) { 198 if (substitution.getInitialState() != NLSSubstitution.EXTERNALIZED || substitution.getInitialKey() == null) { 199 return false; } 201 if (oldKeyToSubstMap.get(substitution.getInitialKey()) != substitution) { 202 return false; } 204 if (substitution.hasStateChanged()) { 205 return true; } else { 207 if (substitution.hasPropertyFileChange() && newKeyToSubstMap.get(substitution.getKey()) != substitution) { 208 return true; } 210 } 211 return false; 212 } 213 214 private static void addRemoveEdits(TextChange textChange, NLSSubstitution[] substitutions, Map newKeyToSubstMap, Map oldKeyToSubstMap, PropertyFileDocumentModel model) { 215 for (int i= 0; i < substitutions.length; i++) { 216 NLSSubstitution substitution= substitutions[i]; 217 if (doRemove(substitution, newKeyToSubstMap, oldKeyToSubstMap)) { 218 TextEdit edit= model.remove(substitution.getInitialKey()); 219 if (edit != null) { 220 TextChangeCompatibility.addTextEdit(textChange, Messages.format(NLSMessages.NLSPropertyFileModifier_remove_entry, substitution.getInitialKey()), edit); 221 } 222 } 223 } 224 } 225 226 } 227 | Popular Tags |