/** * Recursively explores the specified section and invokes the specified builder's callback methods. * @param builder object that will receive building events. * @param root section to explore. * @throws ConfigurationException if any error occurs. */ private synchronized void build(ConfigurationBuilder builder, ConfigurationSection root) throws ConfigurationException { Iterator<String> enumeration; // Enumeration on the section's variables, then subsections. String name; // Name of the current variable, then section. ConfigurationSection section; // Current section. // Explores the section's variables. enumeration = root.variableNames(); while(enumeration.hasNext()) builder.addVariable(name = enumeration.next(), root.getVariable(name)); // Explores the section's subsections. enumeration = root.sectionNames(); while(enumeration.hasNext()) { name = enumeration.next(); section = root.getSection(name); // We only go through subsections if contain either variables or subsections of their own. if(section.hasSections() || section.hasVariables()) { builder.startSection(name); build(builder, section); builder.endSection(name); } } }
/** * Utility method for {@link #testVariableNames()}. * @param section section to explore. * @param count number of expected variable names. */ private void assertVariableNames(ConfigurationSection section, int count) { // No expected variables, make sure that the section reflects that. if(count == 0) { assert !section.hasVariables(); assert section.isEmpty(); assert !section.variableNames().hasNext(); } // Makes sure that the section contains exactly var1, var2..., var<count>. // We have to go through a set here: the order in which we'll iterate over the variable names is unreliable. else { Iterator<String> names; Set<String> expectedNames; assert section.hasVariables(); assert !section.isEmpty(); // Populates a set will all the expected variable names. expectedNames = new HashSet<String>(count); for(int i = 0; i < count; i++) expectedNames.add("var" + i); // Makes sure that we can remove all of the section's variables, and that none remains afterward. names = section.variableNames(); while(names.hasNext()) assert expectedNames.remove(names.next()); assert expectedNames.isEmpty(); } }