-
-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support conditional blocks in class based resource bundles. Fixes #212
- Loading branch information
Showing
11 changed files
with
409 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
subprojects/griffon-core/src/main/java/griffon/util/ResourceBundleReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
* Copyright 2017 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package griffon.util; | ||
|
||
import griffon.core.env.Environment; | ||
import griffon.core.env.Metadata; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
import javax.inject.Inject; | ||
import java.util.Collections; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.ResourceBundle; | ||
|
||
import static griffon.util.GriffonNameUtils.isBlank; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
/** | ||
* @author Andres Almiray | ||
* @since 2.10.0 | ||
*/ | ||
public class ResourceBundleReader { | ||
private static final String ENVIRONMENTS_METHOD = "environments"; | ||
|
||
private final Map<String, String> conditionValues = new LinkedHashMap<>(); | ||
|
||
public static class Provider implements javax.inject.Provider<ResourceBundleReader> { | ||
@Inject private Metadata metadata; | ||
@Inject private Environment environment; | ||
|
||
@Override | ||
public ResourceBundleReader get() { | ||
ResourceBundleReader propertiesReader = new ResourceBundleReader(); | ||
propertiesReader.registerConditionalBlock("environments", environment.getName()); | ||
propertiesReader.registerConditionalBlock("projects", metadata.getApplicationName()); | ||
propertiesReader.registerConditionalBlock("platforms", GriffonApplicationUtils.getPlatform()); | ||
return propertiesReader; | ||
} | ||
} | ||
|
||
public void registerConditionalBlock(@Nullable String blockName, @Nullable String blockValue) { | ||
if (!isBlank(blockName)) { | ||
if (isBlank(blockValue)) { | ||
conditionValues.remove(blockName); | ||
} else { | ||
conditionValues.put(blockName, blockValue); | ||
} | ||
} | ||
} | ||
|
||
@Nonnull | ||
public Map<String, String> getConditionalBlockValues() { | ||
return Collections.unmodifiableMap(conditionValues); | ||
} | ||
|
||
@Nonnull | ||
public String getEnvironment() { | ||
return conditionValues.get(ENVIRONMENTS_METHOD); | ||
} | ||
|
||
public void setEnvironment(String environment) { | ||
conditionValues.put(ENVIRONMENTS_METHOD, environment); | ||
} | ||
|
||
@Nonnull | ||
public ResourceBundle read(final @Nonnull ResourceBundle bundle) { | ||
requireNonNull(bundle, "Argument 'bundle' must not be null"); | ||
return new AbstractMapResourceBundle() { | ||
@Override | ||
protected void initialize(@Nonnull Map<String, Object> entries) { | ||
entries.putAll(processBundle(bundle)); | ||
} | ||
}; | ||
} | ||
|
||
@Nonnull | ||
protected Map<String, Object> processBundle(@Nonnull ResourceBundle input) { | ||
Map<String, Object> output = new LinkedHashMap<>(); | ||
|
||
for (String key : input.keySet()) { | ||
ConditionalBlockMatch match = resolveConditionalBlockMatch(key); | ||
if (match != null) { | ||
if (match.key != null) { | ||
output.put(match.key, input.getObject(key)); | ||
} | ||
} else { | ||
output.put(key, input.getObject(key)); | ||
} | ||
} | ||
|
||
return output; | ||
} | ||
|
||
@Nullable | ||
private ConditionalBlockMatch resolveConditionalBlockMatch(@Nonnull String key) { | ||
for (Map.Entry<String, String> e : conditionValues.entrySet()) { | ||
String blockName = e.getKey(); | ||
if (!key.startsWith(blockName + ".")) { | ||
continue; | ||
} | ||
|
||
String prefix = blockName + "." + e.getValue() + "."; | ||
if (key.startsWith(prefix)) { | ||
String subkey = key.substring(prefix.length()); | ||
ConditionalBlockMatch match = resolveConditionalBlockMatch(subkey); | ||
if (match == null) { | ||
match = new ConditionalBlockMatch(subkey); | ||
} | ||
return match; | ||
} else { | ||
return new ConditionalBlockMatch(null); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private static class ConditionalBlockMatch { | ||
public final String key; | ||
|
||
private ConditionalBlockMatch(String key) { | ||
this.key = key; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.