KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > nls > NLSPropertyFileModifier


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.refactoring.nls;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
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 JavaDoc 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 JavaDoc 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 JavaDoc encoding= null;
59             IContentType javaPropertiesContentType= Platform.getContentTypeManager().getContentType("org.eclipse.jdt.core.javaProperties"); //$NON-NLS-1$
60
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"; //$NON-NLS-1$
66
}
67             
68             return new CreateTextFileChange(propertyFilePath, textChange.getCurrentContent(new NullProgressMonitor()), encoding, "properties"); //$NON-NLS-1$
69
}
70
71         textChange= new TextFileChange(name, getPropertyFile(propertyFilePath));
72         textChange.setTextType("properties"); //$NON-NLS-1$
73

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 JavaDoc newKeyToSubstMap= getNewKeyToSubstitutionMap(substitutions);
93         Map JavaDoc 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     /**
101      * Maps the new keys to a substitutions. If a substitution is not in the map then it is a duplicate.
102      */

103     static HashMap JavaDoc getNewKeyToSubstitutionMap(NLSSubstitution[] substitutions) {
104         HashMap JavaDoc keyToSubstMap= new HashMap JavaDoc(substitutions.length);
105         // find all duplicates
106
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); // store if first or if stored in new and we are existing
112
}
113             }
114         }
115         return keyToSubstMap;
116     }
117     
118     /**
119      * Maps the old keys to a substitutions. If a substitution is not in the map then it is a duplicate.
120      */

121     static HashMap JavaDoc getOldKeyToSubstitutionMap(NLSSubstitution[] substitutions) {
122         HashMap JavaDoc keyToSubstMap= new HashMap JavaDoc(substitutions.length);
123         // find all duplicates
124
for (int i= 0; i < substitutions.length; i++) {
125             NLSSubstitution curr= substitutions[i];
126             if (curr.getInitialState() == NLSSubstitution.EXTERNALIZED) {
127                 String JavaDoc 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); // store if first or if stored will not be externalized anymore
132
}
133                 }
134             }
135         }
136         return keyToSubstMap;
137     }
138
139     static boolean doReplace(NLSSubstitution substitution, Map JavaDoc newKeyToSubstMap, Map JavaDoc oldKeyToSubstMap) {
140         if (substitution.getState() != NLSSubstitution.EXTERNALIZED || substitution.hasStateChanged() || substitution.getInitialValue() == null) {
141             return false; // was not in property file before
142
}
143         if (oldKeyToSubstMap.get(substitution.getInitialKey()) != substitution) {
144             return false; // not the owner of this key
145
}
146         if (substitution.isKeyRename() || substitution.isValueRename()) {
147             if (newKeyToSubstMap.get(substitution.getKey()) == substitution) { // only rename if we're not a duplicate. duplicates will be removed
148
return true;
149             }
150         }
151         return false;
152     }
153     
154     private static void addReplaceEdits(TextChange textChange, NLSSubstitution[] substitutions, Map JavaDoc newKeyToSubstMap, Map JavaDoc 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 JavaDoc newKeyToSubstMap, Map JavaDoc oldKeyToSubstMap) {
169         if (substitution.getState() != NLSSubstitution.EXTERNALIZED) {
170             return false; // does not go into the property file
171
}
172         if (!substitution.hasStateChanged() && substitution.getInitialValue() != null) {
173             if (!substitution.isKeyRename() || oldKeyToSubstMap.get(substitution.getInitialKey()) == substitution) {
174                 return false; // no key rename and was not a duplicate
175
}
176         }
177         if (newKeyToSubstMap.get(substitution.getKey()) == substitution) { // only insert if we're not a duplicate
178
return true;
179         }
180         return false;
181     }
182
183     private static void addInsertEdits(TextChange textChange, NLSSubstitution[] substitutions, Map JavaDoc newKeyToSubstMap, Map JavaDoc 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 JavaDoc value= substitution.getValueNonEmpty();
188                 KeyValuePair curr= new KeyValuePair(substitution.getKey(), value);
189                 
190                 InsertEdit insert= model.insert(curr);
191                 String JavaDoc 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 JavaDoc newKeyToSubstMap, Map JavaDoc oldKeyToSubstMap) {
198         if (substitution.getInitialState() != NLSSubstitution.EXTERNALIZED || substitution.getInitialKey() == null) {
199             return false; // was not in property file before
200
}
201         if (oldKeyToSubstMap.get(substitution.getInitialKey()) != substitution) {
202             return false; // not the owner of this key
203
}
204         if (substitution.hasStateChanged()) {
205             return true; // was externalized, but not anymore
206
} else {
207             if (substitution.hasPropertyFileChange() && newKeyToSubstMap.get(substitution.getKey()) != substitution) {
208                 return true; // has been changed to an already existing
209
}
210         }
211         return false;
212     }
213     
214     private static void addRemoveEdits(TextChange textChange, NLSSubstitution[] substitutions, Map JavaDoc newKeyToSubstMap, Map JavaDoc 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