Process platform-specific suffixes immediately

In preferences files, platform-specific versions can be indicated by a
.linux, .windows or .macos suffix on the key name. Previously, these
keys were loaded as normal and then afterwards, all keys were scanned
after loading them and any platform-specific versions replaced the
regular ones.

However, this means that these platform-specific versions get an
unexpected form of priority. Normally, when a single key is set twice,
the latter overrides the first. However, the platform-specific values
could override the regular versions, even when the regular version
occurs later in the file.

This problem was particularly confusing when using the new
platform.local.txt: a regular preference in platform.local.txt did not
override a platform-specific preference in platform.txt.

This commit changes behaviour to process these suffixes directly as they
are read from the preference files. If a suffix for the current platform
is found, the line is processed as if the suffix was not present. If a
suffix for another platform is found, the line is ignored altogether.

This can slightly change the way preferences files are parsed, but as
long as platform-specific preferences are defined after the
corresponding regular preferences, the behaviour should be the same.
This commit is contained in:
Matthijs Kooijman 2014-04-14 15:12:32 +02:00
parent f0738fdf9f
commit d2505a08f6
1 changed files with 20 additions and 20 deletions

View File

@ -77,6 +77,19 @@ public class PreferencesMap extends LinkedHashMap<String, String> {
load(new FileInputStream(file));
}
protected String processPlatformSuffix(String key, String suffix, boolean isCurrentPlatform) {
if (key == null)
return null;
// Key does not end with the given suffix? Process as normal
if (!key.endsWith(suffix))
return key;
// Not the current platform? Ignore this key
if (!isCurrentPlatform)
return null;
// Strip the suffix from the key
return key.substring(0, key.length() - suffix.length());
}
/**
* Parse a property list stream and put key/value pairs into the Map
*
@ -91,28 +104,15 @@ public class PreferencesMap extends LinkedHashMap<String, String> {
int equals = line.indexOf('=');
if (equals != -1) {
String key = line.substring(0, equals);
String value = line.substring(equals + 1);
put(key.trim(), value.trim());
}
}
String key = line.substring(0, equals).trim();
String value = line.substring(equals + 1).trim();
// This is needed to avoid ConcurrentAccessExceptions
Set<String> keys = new LinkedHashSet<String>(keySet());
key = processPlatformSuffix(key, ".linux", Base.isLinux());
key = processPlatformSuffix(key, ".windows", Base.isWindows());
key = processPlatformSuffix(key, ".macos", Base.isMacOS());
// Override keys that have OS specific versions
for (String key : keys) {
boolean replace = false;
if (Base.isLinux() && key.endsWith(".linux"))
replace = true;
if (Base.isWindows() && key.endsWith(".windows"))
replace = true;
if (Base.isMacOS() && key.endsWith(".macos"))
replace = true;
if (replace) {
int dot = key.lastIndexOf('.');
String overridenKey = key.substring(0, dot);
put(overridenKey, get(key));
if (key != null)
put(key, value);
}
}
}