protected Map<String,Object> getSubset(ReadConfiguration config, ConfigNamespace umbrella, String... umbrellaElements) { verifyElement(umbrella); String prefix = umbrella.isRoot() ? "" : ConfigElement.getPath(umbrella, umbrellaElements); Map<String,Object> result = Maps.newHashMap(); for (String key : config.getKeys(prefix)) { Preconditions.checkArgument(key.startsWith(prefix)); // A zero-length prefix is a root. A positive-length prefix // is not a root and we should tack on an additional character // to consume the dot between the prefix and the rest of the key. int startIndex = umbrella.isRoot() ? prefix.length() : prefix.length() + 1; String sub = key.substring(startIndex).trim(); if (!sub.isEmpty()) { result.put(sub,config.get(key,Object.class)); } } return result; }
private String getFullPath(ConfigOption<?> opt) { StringBuilder sb = new StringBuilder(64); sb.insert(0, opt.getName()); for (ConfigNamespace parent = opt.getNamespace(); null != parent && !parent.isRoot(); parent = parent.getNamespace()) { if (parent.isUmbrella()) sb.insert(0, "[X]."); sb.insert(0, "."); sb.insert(0, parent.getName()); } return sb.toString(); }
/** * Whether this namespace or any parent namespace is an umbrella namespace. * @return */ public boolean hasUmbrella() { return isUmbrella() || (!isRoot() && getNamespace().hasUmbrella()); }
private void printNamespace(ConfigNamespace n, String prefix) { String newPrefix = prefix + n.getName() + "."; if (n.isUmbrella()) newPrefix += "[X]."; if (n.isRoot()) newPrefix = ""; //Override for root namespace //Only print namespace if it contains (visible) options if (!Iterables.isEmpty(getSortedChildOptions(n))) { stream.println(getNamespaceSectionHeader(n, prefix)); stream.println(getTableHeader()); for (ConfigOption<?> o : getSortedChildOptions(n)) { stream.println(getTableLineForOption(o, newPrefix)); } stream.println(TABLE_FOOTER_LINES); } for (ConfigNamespace cn : getSortedChildNamespaces(n)) { printNamespace(cn, newPrefix); } }
public static String getPath(ConfigElement element, boolean includeRoot, String... umbrellaElements) { Preconditions.checkNotNull(element); if (umbrellaElements==null) umbrellaElements = new String[0]; StringBuilder path = new StringBuilder(element.getName()); int umbrellaPos = umbrellaElements.length-1; while (!element.isRoot() && !element.getNamespace().isRoot()) { ConfigNamespace parent = element.getNamespace(); if (parent.isUmbrella()) { Preconditions.checkArgument(umbrellaPos>=0,"Missing umbrella element path for element: %s",element); String umbrellaName = umbrellaElements[umbrellaPos]; Preconditions.checkArgument(!StringUtils.containsAny(umbrellaName,ILLEGAL_CHARS),"Invalid umbrella name provided: %s. Contains illegal chars",umbrellaName); path.insert(0, umbrellaName + SEPARATOR); umbrellaPos--; } path.insert(0, parent.getName() + SEPARATOR); element = parent; } if (includeRoot) { // Assumes that roots are not umbrellas // If roots could be umbrellas, we might have to change the interpretation of umbrellaElements path.insert(0, (element.isRoot() ? element.getName() : element.getNamespace().getName()) + SEPARATOR); } //Don't make this check so that we can still access more general config items Preconditions.checkArgument(umbrellaPos<0,"Found unused umbrella element: %s",umbrellaPos<0?null:umbrellaElements[umbrellaPos]); return path.toString(); }