1 11 package org.eclipse.jdt.internal.corext.refactoring.nls; 12 13 import java.util.Properties ; 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 fKey; 28 31 private String fCachedPrefixPlusKey; 32 private String fValue; 33 34 private int fInitialState; 35 private String fInitialKey; 36 private String fInitialValue; 37 38 private NLSElement fNLSElement; 39 private AccessorClassReference fAccessorClassReference; 40 41 private String fNewAccessorClassName; 42 43 private String fPrefix= ""; 45 46 public NLSSubstitution(int state, String 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 59 public NLSSubstitution(int state, String key, String value, NLSElement element, AccessorClassReference accessorClassReference) { 60 this(state,value,element); 61 if (state != EXTERNALIZED) { 62 throw new IllegalArgumentException ("Set to INTERNALIZE/IGNORED State with different Constructor"); } 64 fKey= key; 65 fInitialKey= key; 66 fAccessorClassReference= accessorClassReference; 67 fNewAccessorClassName= null; 68 } 69 70 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 getKeyWithoutPrefix() { 87 return fKey; 88 } 89 90 94 public String 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 sb= new StringBuffer (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 key) { 113 if (fState != EXTERNALIZED) { 114 throw new IllegalStateException ("Must be in Externalized State !"); } 116 fCachedPrefixPlusKey= null; 117 fKey= key; 118 } 119 120 public void setValue(String value) { 121 fValue= value; 122 } 123 124 public void setInitialValue(String value) { 125 fInitialValue= value; 126 } 127 128 131 public String getValue() { 132 return fValue; 133 } 134 135 public String getValueNonEmpty() { 136 if (fValue == null) { 137 return ""; } 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 accessorClassName) { 152 fNewAccessorClassName= accessorClassName; 153 } 154 155 public String 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; } else if (!fInitialValue.equals(fValue)) { 187 return true; } 189 if (!fInitialKey.equals(fKey)) { 190 return true; } 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; } else if (!fInitialValue.equals(fValue)) { 207 return false; } 209 if (!fInitialKey.equals(fKey)) { 210 return true; } 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; } 224 if (isAccessorRename()) { 225 return true; 226 } 227 } else { 228 if (!fInitialValue.equals(fValue)) { 229 return true; } 231 } 232 return false; 233 } 234 235 public int getInitialState() { 236 return fInitialState; 237 } 238 239 public String getInitialKey() { 240 return fInitialKey; 241 } 242 243 public String getInitialValue() { 244 return fInitialValue; 245 } 246 247 public AccessorClassReference getAccessorClassReference() { 248 return fAccessorClassReference; 249 } 250 251 254 public void setPrefix(String prefix) { 255 fPrefix= prefix; 256 fCachedPrefixPlusKey= null; 257 } 258 259 public boolean isConflicting(NLSSubstitution[] substitutions) { 260 if (fState == EXTERNALIZED) { 261 String currKey= getKey(); 262 String currValue= getValueNonEmpty(); 263 for (int i= 0; i < substitutions.length; i++) { 264 NLSSubstitution substitution= substitutions[i]; 265 if (substitution != this && substitution.getState() == EXTERNALIZED) { 266 if (currKey.equals(substitution.getKey()) && !currValue.equals(substitution.getValueNonEmpty())) { 268 return true; 269 } 270 } 271 } 272 } 273 return false; 274 } 275 276 private String 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 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 props, String 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 createKey(int counter) { 328 return String.valueOf(counter); 329 } 330 331 } 332 | Popular Tags |