/** * 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); }
/** * Navigates the specified explorer to the parent section of the specified variable. * @param root where to start exploring from. * @param name name of the variable to seek. * @param create whether or not the path to the variable should be created if it doesn't exist. * @return the name of the variable trimmed of section information, <code>null</code> if not found. */ private String moveToParent(ConfigurationExplorer root, String name, boolean create) { StringTokenizer parser; // Used to parse the variable's path. // Goes through each element of the path. parser = new StringTokenizer(name, "."); while(parser.hasMoreTokens()) { // If we've reached the variable's name, return it. name = parser.nextToken(); if(!parser.hasMoreTokens()) return name; // If we've reached a dead-end, return null. if(!root.moveTo(name, create)) return null; } return name; }
/** * Moves to the specified depth. * @param explorer explorer to use when moving to the specified depth. * @param depth depth in the configuration tree at which to move. * @return section that was found at the specified depth. */ protected ConfigurationSection moveTo(ConfigurationExplorer explorer, int depth) { ConfigurationSection section; section = conf.getRoot(); for(int i = 0; i < depth; i++) { assert explorer.moveTo(SECTION_NAME + i, false); section = explorer.getSection(); } return section; }
/** * Returns a configuration explorer on the test section. * @return a configuration explorer on the test section. */ protected ConfigurationExplorer getExplorer() { return new ConfigurationExplorer(conf.getRoot()); }
/** * 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)); } } }
/** * 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); } }
/** * 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; }
/** * 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. */ @Override public boolean moveTo(String name, boolean create) { if(super.moveTo(name, create)) { sections.push(getSection()); return true; } return false; } }
/** * Sets the value of the specified variable. * <p> * This method will return <code>false</code> if it didn't modify <code>name</code>'s value. Note that this doesn't * mean the call failed, but that <code>name</code>'s value was already equal to <code>value</code>. * </p> * <p> * If the value of the specified variable is actually modified, an {@link ConfigurationEvent event} will be passed * to all LISTENERS. * </p> * @param name fully qualified name of the variable to set. * @param value new value for the variable. * @return <code>true</code> if this call resulted in a modification of the variable's value, * <code>false</code> otherwise. * @see #getVariable(String) * @see #getVariable(String,String) */ public synchronized boolean setVariable(String name, String value) { ConfigurationExplorer explorer; // Used to navigate to the variable's parent section. String buffer; // Buffer for the variable's name trimmed of section information. // Moves to the parent section. buffer = moveToParent(explorer = new ConfigurationExplorer(root), name, true); // If the variable's value was actually modified, triggers an event. if(explorer.getSection().setVariable(buffer, value)) { triggerEvent(new ConfigurationEvent(this, name, value)); return true; } return false; }