/** * Deletes the specified section. * <p> * Note that this method is very inefficient and should only be called when strictly necessary. * </p> * @param section section to remove. * @return <code>true</code> if the specified section was removed, <code>false</code> if it didn't exist. */ public boolean removeSection(ConfigurationSection section) { String name; Iterator<String> sectionNames; sectionNames = sectionNames(); // Goes through each key / value pair and checks whether we've found the sectioon // we were looking for. while(sectionNames.hasNext()) { name = sectionNames.next(); // If we have, remove it and break. if(getSection(name).equals(section)) { removeSection(name); return true; } } return false; }
/** * 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 #testSectionNames()}. * @param section section to explore. * @param count number of expected section names. */ private void assertSectionNames(ConfigurationSection section, int count) { // No expected variables, make sure that the section reflects that. if(count == 0) { assert !section.hasSections(); assert section.isEmpty(); assert !section.sectionNames().hasNext(); } // Makes sure that the section contains exactly sect1, sect2..., sect<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.hasSections(); 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("sect" + i); // Makes sure that we can remove all of the section's sub-sections, and that none remains afterward. names = section.sectionNames(); while(names.hasNext()) assert expectedNames.remove(names.next()); assert expectedNames.isEmpty(); } }