/** * A ConfigObject is a tree structure consisting of nested maps. This flattens the maps into * a single level structure like a properties file */ public Map flatten() { return flatten(null); }
/** * Overrides the default getProperty implementation to create nested ConfigObject instances on demand * for non-existent keys */ public Object getProperty(String name) { if ("configFile".equals(name)) return this.configFile; if (!containsKey(name)) { ConfigObject prop = new ConfigObject(this.configFile); put(name, prop); return prop; } return get(name); }
for (Object o1 : map.keySet()) { String key = (String) o1; Object v = map.get(key); if (!value.isEmpty()) { for (Object o : value.entrySet()) { Entry e = (Entry) o; String k = (String) e.getKey(); int configSize = value.size(); Object firstKey = value.keySet().iterator().next(); Object firstValue = value.values().iterator().next(); firstSize = ((ConfigObject) firstValue).size(); } else { firstSize = 1; key = KEYWORDS.contains(key) ? InvokerHelper.inspect(key) : key; String writePrefix = prefix + key + "." + firstKey + "."; writeConfig(writePrefix, (ConfigObject) firstValue, out, tab, true); } else if (!DefaultGroovyMethods.asBoolean(dotsInKeys) && firstValue instanceof ConfigObject) { writeNode(key, space, tab, value, out); } else { for (Object j : value.keySet()) { Object v2 = value.get(j); Object k2 = ((String) j).indexOf('.') > -1 ? InvokerHelper.inspect(j) : j; if (v2 instanceof ConfigObject) { key = KEYWORDS.contains(key) ? InvokerHelper.inspect(key) : key;
/** * Flattens this ConfigObject populating the results into the target Map * * @see ConfigObject#flatten() */ public Map flatten(Map target) { if (target == null) target = new ConfigObject(); populate("", target, this); return target; }
/** * Converts this ConfigObject into a the java.util.Properties format, flattening the tree structure beforehand * * @return A java.util.Properties instance */ public Properties toProperties() { Properties props = new Properties(); flatten(props); props = convertValuesToString(props); return props; }
private static void createFlatConfig(ConfigObject currentConfigObject, Map<String, Object> rootConfig, String prefix) { Set keySet = currentConfigObject.keySet(); for (Object key : keySet) { Object value = currentConfigObject.get(key); if(value instanceof ConfigObject) { ConfigObject sub = ((ConfigObject) value).clone(); String fullPath = prefix + key; if(!sub.isEmpty()) { Map flattened = sub.flatten(); sub.putAll(flattened); createFlatConfig(sub, rootConfig, fullPath + "."); if(!rootConfig.containsKey(fullPath)) { rootConfig.put(fullPath, sub); } } } else { rootConfig.put(prefix + key, value); } } }
ConfigObject cloned = existingConfig.clone(); propertySources.addFirst(new ConfigObjectPropertySource(DATASTORE_CONFIG + i, cloned)); propertySources.addFirst(new ConfigObjectPropertySource(DATASTORE_CONFIG_FLAT+ i, createFlatConfig(cloned))); ConfigObject flatConfigObject = new ConfigObject(); configObject.flatten(flatConfigObject); flatConfigObject.merge(configObject); propertySources.addFirst(new ConfigObjectPropertySource(DATASTORE_CONFIG + i, configObject)); propertySources.addFirst(new ConfigObjectPropertySource(DATASTORE_CONFIG_FLAT + i, createFlatConfig(flatConfigObject)));
public void properties(Closure<Properties> closure) { ConfigObject config = new ConfigObject(); closure.setResolveStrategy(Closure.DELEGATE_ONLY); closure.setDelegate(config); closure.call(); config.flatten(this.properties); }
static ConfigObject mergeConfigObjects(ConfigObject lowPriorityCO, ConfigObject highPriorityCO) { if(lowPriorityCO != null){ return highPriorityCO != null ? (ConfigObject) lowPriorityCO.merge(highPriorityCO) : lowPriorityCO; } return highPriorityCO; }
/** * * @param co * current config object which is queried * @param currentNamePart * the queried property name * @return the value for given property name * @throws ObjectNotFoundException */ private Object configObjectGet(ConfigObject co, String currentNamePart) { /* * get the property value */ Object result = co.getProperty(currentNamePart); if (result instanceof ConfigObject) { // try if property value is empty ConfigObject coResult = (ConfigObject) result; if (coResult.size() == 0) { throw new ObjectNotFoundException(); } } else { result = resolvePropObject(result); }// fi return result; }
/** * * @param co current config object which is queried * @param currentNamePart the queried property name * @return the value for given property name * @throws ObjectNotFoundException */ private Object configObjectGet(ConfigObject co, String currentNamePart) { /* * get the property value */ Object result = co.getProperty(currentNamePart); if (result instanceof ConfigObject) { // try if property value is empty ConfigObject coResult = (ConfigObject) result; if (coResult.isEmpty()) { throw new ObjectNotFoundException(); } } else { result = resolvePropObject(result); }// fi return result; }
/** * Checks if a config option is set. Example usage: * <pre class="groovyTestCase"> * def config = new ConfigSlurper().parse("foo { password='' }") * assert config.foo.isSet('password') * assert config.foo.isSet('username') == false * </pre> * * The check works <b>only</v> for options <b>one</b> block below the current block. * E.g. <code>config.isSet('foo.password')</code> will always return false. * * @param option The name of the option * @return <code>true</code> if the option is set <code>false</code> otherwise * @since 2.3.0 */ public Boolean isSet(String option) { if (delegateMap.containsKey(option)) { Object entry = delegateMap.get(option); if (!(entry instanceof ConfigObject) || !((ConfigObject) entry).isEmpty()) { return Boolean.TRUE; } } return Boolean.FALSE; }
@Root(name="webtv", strict = false) public class XmlModelMapper { public ConfigObject getConfigObject() { return configObject; } @Element(name="config") public ConfigObject configObject = new ConfigObject(); //rest of the elements ... }
/** * Converts this ConfigObject into a the java.util.Properties format, flattening the tree structure beforehand * * @return A java.util.Properties instance */ public Properties toProperties() { Properties props = new Properties(); flatten(props); props = convertValuesToString(props); return props; }
/** * merge two config objects. If both of config objects contian the same * property key, then the value of <code>highPriorityCO</code> is propagated * to the result. * @param lowPriorityCO * @param highPriorityCO * @return the merged version of two config objects. */ static ConfigObject mergeConfigObjects(ConfigObject lowPriorityCO, ConfigObject highPriorityCO) { return (ConfigObject) lowPriorityCO.merge(highPriorityCO); }
/** * Flattens this ConfigObject populating the results into the target Map * * @see ConfigObject#flatten() */ public Map flatten(Map target) { if (target == null) target = new ConfigObject(); populate("", target, this); return target; }
/** * * @param co * current config object which is queried * @param currentNamePart * the queried property name * @return the value for given property name * @throws ObjectNotFoundException */ private Object configObjectGet(ConfigObject co, String currentNamePart) { /* * get the property value */ Object result = co.getProperty(currentNamePart); if (result instanceof ConfigObject) { // try if property value is empty ConfigObject coResult = (ConfigObject) result; if (coResult.size() == 0) { throw new ObjectNotFoundException(); } } else { result = resolvePropObject(result); }// fi return result; }
@Nullable @Override @SuppressWarnings("unchecked") protected Object handleGetObject(@Nonnull String key) { Object value = getConfigValue(config, requireNonBlank(key, "Argument 'key' must not be blank"), null); if (null == value) return null; if (value instanceof ConfigObject) { return ((ConfigObject) value).isEmpty() ? null : value; } return value; }
for (Object o1 : map.keySet()) { String key = (String) o1; Object v = map.get(key); if (!value.isEmpty()) { for (Object o : value.entrySet()) { Entry e = (Entry) o; String k = (String) e.getKey(); int configSize = value.size(); Object firstKey = value.keySet().iterator().next(); Object firstValue = value.values().iterator().next(); firstSize = ((ConfigObject) firstValue).size(); } else { firstSize = 1; key = KEYWORDS.contains(key) ? InvokerHelper.inspect(key) : key; String writePrefix = prefix + key + "." + firstKey + "."; writeConfig(writePrefix, (ConfigObject) firstValue, out, tab, true); } else if (!DefaultGroovyMethods.asBoolean(dotsInKeys) && firstValue instanceof ConfigObject) { writeNode(key, space, tab, value, out); } else { for (Object j : value.keySet()) { Object v2 = value.get(j); Object k2 = ((String) j).indexOf('.') > -1 ? InvokerHelper.inspect(j) : j; if (v2 instanceof ConfigObject) { key = KEYWORDS.contains(key) ? InvokerHelper.inspect(key) : key;
/** * A ConfigObject is a tree structure consisting of nested maps. This flattens the maps into * a single level structure like a properties file */ public Map flatten() { return flatten(null); }