/** * Creates a subsection wit the specified name in the section. * <p> * If a subsection with the specified name already exists, it will be returned. * </p> * @param name name of the new section. * @return the subsection with the specified name. */ public ConfigurationSection addSection(String name) { ConfigurationSection section; // The section already exists, returns it. if((section = getSection(name)) != null) return section; // Creates the new section. sections.put(name, section = new ConfigurationSection()); return section; }
/** * Move to the specified section. * @param name name of the current section's subsection in which to move. * @param create if <code>true</code> and <code>name</code> doesn't exist, it will be created. * @return <code>true</code> if we could move to <code>name</code>, <code>false</code> otherwise. */ public boolean moveTo(String name, boolean create) { ConfigurationSection buffer; // Buffer for the subsection. // Checks whether the requested subsection exists. if((buffer = section.getSection(name)) == null) { // If it doesn't exist, either return false or create it depending on // parameters. if(create) { section = section.addSection(name); return true; } return false; } section = buffer; return true; } }
/** * Ends the current section. * @param name name of the section that's being closed. * @throws ConfigurationException if we're not closing a legal section. */ public void endSection(String name) throws ConfigurationException { ConfigurationSection buffer; // Makes sure there is a section to close. try { buffer = sections.pop(); sectionNames.pop(); } catch(EmptyStackException e) {throw new ConfigurationStructureException("Section " + name + " was already closed.");} // Makes sure we're closing the right section. if(buffer.getSection(name) != currentSection) throw new ConfigurationStructureException("Section " + name + " is not the currently opened section."); currentSection = buffer; }
/** * 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); } } }
/** * Tests the {@link ConfigurationExplorer#moveTo(String,boolean)} method. */ private void assertSectionNotFound(boolean create) { ConfigurationExplorer explorer; ConfigurationSection section; // Checks what happens when sections are not found and create is set to false. for(int i = 0; i < DEPTH; i++) { section = moveTo(explorer = getExplorer(), i); // Makes sure the 'fake' section doesn't exist. assert !explorer.moveTo(FAKE_SECTION + i, false); if(create) { // Tries to create the section and makes sure the explorer // did move to it. assert explorer.moveTo(FAKE_SECTION + i, true); assert section.getSection(FAKE_SECTION + i).equals(explorer.getSection()); } else // Makes sure the explorer didn't change section. assert explorer.getSection().equals(section); } }
/** * Tests various section related method. */ @Test public void testSections() { ConfigurationSection section; ConfigurationSection buffer; section = new ConfigurationSection(); // Makes sure we can add a section. buffer = section.addSection("sect"); assert buffer != null; assert section.hasSections(); // Makes sure that adding the same section twice yields the same instance. assert section.addSection("sect") == buffer; assert section.hasSections(); // Makes sure we can remove a section by name. assert section.removeSection("sect") == buffer; assert section.getSection("sect") == null; assert !section.hasSections(); // Makes sure we can remove a section by value. buffer = section.addSection("sect"); assert section.removeSection(buffer); assert buffer.getSection("sect") == null; assert !section.hasSections(); assert !section.removeSection(buffer); }