KickJava   Java API By Example, From Geeks To Geeks.

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


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.Properties JavaDoc;
14
15 import org.eclipse.core.runtime.Assert;
16
17
18 public class NLSSubstitution {
19     public static final int EXTERNALIZED= 0;
20     public static final int IGNORED= 1;
21     public static final int INTERNALIZED= 2;
22
23     public static final int DEFAULT= EXTERNALIZED;
24     public static final int STATE_COUNT= 3;
25
26     private int fState;
27     private String JavaDoc fKey;
28     /**
29      * @since 3.1
30      */

31     private String JavaDoc fCachedPrefixPlusKey;
32     private String JavaDoc fValue;
33
34     private int fInitialState;
35     private String JavaDoc fInitialKey;
36     private String JavaDoc fInitialValue;
37     
38     private NLSElement fNLSElement;
39     private AccessorClassReference fAccessorClassReference;
40     
41     private String JavaDoc fNewAccessorClassName;
42
43     private String JavaDoc fPrefix= ""; //$NON-NLS-1$
44

45
46     public NLSSubstitution(int state, String JavaDoc value, NLSElement element) {
47         fNLSElement= element;
48         fValue= value;
49         fState= state;
50         fInitialState= state;
51         fInitialValue= value;
52         fCachedPrefixPlusKey= null;
53         Assert.isTrue(state == EXTERNALIZED || state == IGNORED || state == INTERNALIZED);
54     }
55
56     /**
57      * value == null indicates a corrupt substitution.
58      */

59     public NLSSubstitution(int state, String JavaDoc key, String JavaDoc value, NLSElement element, AccessorClassReference accessorClassReference) {
60         this(state,value,element);
61         if (state != EXTERNALIZED) {
62             throw new IllegalArgumentException JavaDoc("Set to INTERNALIZE/IGNORED State with different Constructor"); //$NON-NLS-1$
63
}
64         fKey= key;
65         fInitialKey= key;
66         fAccessorClassReference= accessorClassReference;
67         fNewAccessorClassName= null;
68     }
69
70     //util
71
public static int countItems(NLSSubstitution[] elems, int task) {
72         Assert.isTrue(task == NLSSubstitution.EXTERNALIZED || task == NLSSubstitution.IGNORED || task == NLSSubstitution.INTERNALIZED);
73         int result= 0;
74         for (int i= 0; i < elems.length; i++) {
75             if (elems[i].fState == task) {
76                 result++;
77             }
78         }
79         return result;
80     }
81
82     public NLSElement getNLSElement() {
83         return fNLSElement;
84     }
85     
86     public String JavaDoc getKeyWithoutPrefix() {
87         return fKey;
88     }
89
90     /**
91      * Returns key dependent on state.
92      * @return prefix + key when
93      */

94     public String JavaDoc getKey() {
95         if ((fState == EXTERNALIZED) && hasStateChanged()) {
96             if (fCachedPrefixPlusKey == null) {
97                 int length= 0;
98                 if (fPrefix != null)
99                     length= length + fPrefix.length();
100                 if (fKey != null)
101                     length= length + fKey.length();
102                 StringBuffer JavaDoc sb= new StringBuffer JavaDoc(length);
103                 sb.append(fPrefix);
104                 sb.append(fKey);
105                 fCachedPrefixPlusKey= sb.toString();
106             }
107             return fCachedPrefixPlusKey;
108         }
109         return fKey;
110     }
111
112     public void setKey(String JavaDoc key) {
113         if (fState != EXTERNALIZED) {
114             throw new IllegalStateException JavaDoc("Must be in Externalized State !"); //$NON-NLS-1$
115
}
116         fCachedPrefixPlusKey= null;
117         fKey= key;
118     }
119
120     public void setValue(String JavaDoc value) {
121         fValue= value;
122     }
123     
124     public void setInitialValue(String JavaDoc value) {
125         fInitialValue= value;
126     }
127
128     /**
129      * Value can be null.
130      */

131     public String JavaDoc getValue() {
132         return fValue;
133     }
134     
135     public String JavaDoc getValueNonEmpty() {
136         if (fValue == null) {
137             return ""; //$NON-NLS-1$
138
}
139         return fValue;
140     }
141
142     public int getState() {
143         return fState;
144     }
145
146     public void setState(int state) {
147         fCachedPrefixPlusKey= null;
148         fState= state;
149     }
150     
151     public void setUpdatedAccessor(String JavaDoc accessorClassName) {
152         fNewAccessorClassName= accessorClassName;
153     }
154     
155     public String JavaDoc getUpdatedAccessor() {
156         return fNewAccessorClassName;
157     }
158
159     public boolean hasStateChanged() {
160         return fState != fInitialState;
161     }
162     
163     public boolean isKeyRename() {
164         return (fInitialKey != null && !fInitialKey.equals(fKey));
165     }
166     
167     public boolean isValueRename() {
168         return (fInitialValue != null && !fInitialValue.equals(fValue));
169     }
170     
171     public boolean isAccessorRename() {
172         return (fAccessorClassReference != null) && (fNewAccessorClassName != null) && !fNewAccessorClassName.equals(fAccessorClassReference.getName());
173     }
174     
175     
176     public boolean hasPropertyFileChange() {
177         if (fInitialState != EXTERNALIZED && fState != EXTERNALIZED) {
178             return false;
179         }
180         if (fInitialState != fState) {
181             return true;
182         }
183         if (fState == EXTERNALIZED) {
184             if (fInitialValue == null) {
185                 return true; // recreate entry in property file
186
} else if (!fInitialValue.equals(fValue)) {
187                 return true; // change of value
188
}
189             if (!fInitialKey.equals(fKey)) {
190                 return true; // change of key
191
}
192         }
193         return false;
194     }
195     
196     public boolean hasAccessorClassChange() {
197         if (fInitialState != EXTERNALIZED && fState != EXTERNALIZED) {
198             return false;
199         }
200         if (fInitialState != fState) {
201             return true;
202         }
203         if (fState == EXTERNALIZED) {
204             if (fInitialValue == null) {
205                 return true; // recreate entry in property file
206
} else if (!fInitialValue.equals(fValue)) {
207                 return false; // change of value
208
}
209             if (!fInitialKey.equals(fKey)) {
210                 return true; // change of key
211
}
212         }
213         return false;
214     }
215     
216     public boolean hasSourceChange() {
217         if (hasStateChanged()) {
218             return true;
219         }
220         if (fState == EXTERNALIZED) {
221             if (!fInitialKey.equals(fKey)) {
222                 return true; // change of key
223
}
224             if (isAccessorRename()) {
225                 return true;
226             }
227         } else {
228             if (!fInitialValue.equals(fValue)) {
229                 return true; // change of value
230
}
231         }
232         return false;
233     }
234     
235     public int getInitialState() {
236         return fInitialState;
237     }
238
239     public String JavaDoc getInitialKey() {
240         return fInitialKey;
241     }
242
243     public String JavaDoc getInitialValue() {
244         return fInitialValue;
245     }
246
247     public AccessorClassReference getAccessorClassReference() {
248         return fAccessorClassReference;
249     }
250
251     /**
252      * Sets the prefix.
253      */

254     public void setPrefix(String JavaDoc prefix) {
255         fPrefix= prefix;
256         fCachedPrefixPlusKey= null;
257     }
258
259     public boolean isConflicting(NLSSubstitution[] substitutions) {
260         if (fState == EXTERNALIZED) {
261             String JavaDoc currKey= getKey();
262             String JavaDoc currValue= getValueNonEmpty();
263             for (int i= 0; i < substitutions.length; i++) {
264                 NLSSubstitution substitution= substitutions[i];
265                 if (substitution != this && substitution.getState() == EXTERNALIZED) {
266                     // same key but different value
267
if (currKey.equals(substitution.getKey()) && !currValue.equals(substitution.getValueNonEmpty())) {
268                         return true;
269                     }
270                 }
271             }
272         }
273         return false;
274     }
275
276     private String JavaDoc internalGetKeyWithoutPrefix() {
277         if (fState == EXTERNALIZED && fPrefix != null && fKey != null && fKey.indexOf(fPrefix) == 0)
278             return fKey.substring(fPrefix.length());
279         return fKey;
280     }
281
282     public void generateKey(NLSSubstitution[] substitutions) {
283         if (fState != EXTERNALIZED || ((fState == EXTERNALIZED) && hasStateChanged())) {
284             int min= Integer.MAX_VALUE;
285             int max= Integer.MIN_VALUE;
286             
287             for (int i= 0; i < substitutions.length; i++) {
288                 NLSSubstitution substitution= substitutions[i];
289                 if (substitution == this || substitution.fState != EXTERNALIZED)
290                     continue;
291                 try {
292                     int value= Integer.parseInt(substitution.internalGetKeyWithoutPrefix());
293                     min= Math.min(min, value);
294                     max= Math.max(max, value);
295                 } catch (NumberFormatException JavaDoc ex) {
296                     
297                 }
298             }
299             
300             fCachedPrefixPlusKey= null;
301             if (min == Integer.MAX_VALUE)
302                 fKey= createKey(0);
303             else if (min > 0)
304                 fKey= createKey(min-1);
305             else
306                 fKey= createKey(max + 1);
307         }
308     }
309
310     public static void updateSubtitutions(NLSSubstitution[] substitutions, Properties JavaDoc props, String JavaDoc accessorClassName) {
311         for (int i= 0; i < substitutions.length; i++) {
312             NLSSubstitution substitution= substitutions[i];
313             if ((substitution.getState() == NLSSubstitution.EXTERNALIZED) && !substitution.hasStateChanged()) {
314                 substitution.setInitialValue(props.getProperty(substitution.getKey()));
315                 substitution.setUpdatedAccessor(accessorClassName);
316             }
317         }
318     }
319
320     public void revert() {
321         fState= fInitialState;
322         fKey= fInitialKey;
323         fCachedPrefixPlusKey= null;
324         fValue= fInitialValue;
325     }
326
327     private String JavaDoc createKey(int counter) {
328         return String.valueOf(counter);
329     }
330
331 }
332
Popular Tags