/** * Makes sure that the specified variable has the specified value. * @param section section in which the variable to test is located. * @param var name of the variable to test. * @param value expected variable value. */ private void assertVariable(ConfigurationSection section, String var, String value) { if(value == null || value.isEmpty()) assert section.getVariable(var) == null: "Expected null but found " + section.getVariable(var); else { assert section.getVariable(var).equals(value): "Expected " + value + " but found " + section.getVariable(var); } }
/** * Sets the specified variable to the specified value. * <p> * If <code>value</code> is either <code>null</code> or an empty string, * the call will be equivalent to {@link #removeVariable(String)}. * </p> * @param name name of the variable to set. * @param value value for the variable. * @return <code>true</code> if the variable's value was changed as a result of this call, <code>false</code> * otherwise. */ public boolean setVariable(String name, String value) { // If the specified value is empty, deletes the variable. if(value == null || value.trim().equals("")) { // If the variable wasn't set, we haven't changed its value. if(getVariable(name) == null) return false; // Otherwise, deletes it and returns true. removeVariable(name); return true; } // Compares the variable's new and old values. String buffer; buffer = variables.put(name, value); return buffer == null || !buffer.equals(value); }
/** * Returns the value of the specified variable. * @param name fully qualified name of the variable whose value should be retrieved. * @return the variable's value if set, <code>null</code> otherwise. * @see #setVariable(String,String) * @see #getVariable(String,String) */ public synchronized String getVariable(String name) { ConfigurationExplorer explorer; // Used to navigate to the variable's parent section. // If the variable's 'path' doesn't exist, return null. if((name = moveToParent(explorer = new ConfigurationExplorer(root), name, false)) == null) return null; return explorer.getSection().getVariable(name); }
/** * Test configuration navigation to existing sections. */ @Test public void testSectionFound() { ConfigurationExplorer explorer; for(int i = 0; i < DEPTH; i++) { moveTo(explorer = getExplorer(), i); assert (VARIABLE_VALUE + i).equals(explorer.getSection().getVariable(VARIABLE_NAME + i)); } } }
/** * Backtracks through the section history and makes sure it contains the correct values. */ private void backtrack(BufferedConfigurationExplorer explorer, int depth) { for(int i = depth; i > 0; i--) { assert explorer.hasSections(); assert (VARIABLE_VALUE + i).equals(explorer.popSection().getVariable(VARIABLE_NAME + i)); } assert !explorer.hasSections(); }
/** * 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); } } }
/** * Retrieves the value of the specified variable. * <p> * If the variable isn't set, this method will set it to <code>defaultValue</code> before * returning it. If this happens, a configuration {@link ConfigurationEvent event} will * be sent to all registered LISTENERS. * </p> * @param name name of the variable to retrieve. * @param defaultValue value to use if <code>name</code> is not set. * @return the specified variable's value. * @see #setVariable(String,String) * @see #getVariable(String) */ public synchronized String getVariable(String name, String defaultValue) { ConfigurationExplorer explorer; // Used to navigate to the variable's parent section. String value; // Buffer for the variable's value. String buffer; // Buffer for the variable's name trimmed of section information. // Navigates to the parent section. We do not have to check for null values here, // as the section will be created if it doesn't exist. buffer = moveToParent(explorer = new ConfigurationExplorer(root), name, true); // If the variable isn't set, set it to defaultValue and triggers an event. if((value = explorer.getSection().getVariable(buffer)) == null) { explorer.getSection().setVariable(buffer, defaultValue); triggerEvent(new ConfigurationEvent(this, name, defaultValue)); return defaultValue; } return value; }