/** * Writes the configuration data to the specified builder. * @param builder object that will receive configuration building messages. * @throws ConfigurationException if any error occurs while going through the configuration tree. */ public void write(ConfigurationBuilder builder) throws ConfigurationException { builder.startConfiguration(); build(builder, root); builder.endConfiguration(); }
/** * This method is public as an implementation side effect and should never be called directly. */ @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { depth++; if(depth == 1) return; if(itemName != null) { try {builder.startSection(itemName);} catch(Exception e) {throw new SAXParseException(e.getMessage(), locator, e);} } buffer.setLength(0); itemName = qName; isVariable = true; }
/** * 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); } } }
/** * This method is public as an implementation side effect and should never be called directly. */ @Override public void endElement(String uri, String localName, String qName) throws SAXException { depth--; if(depth == 0) return; // If the current element doesn't have subsections, considers it to be a variable. if(isVariable) { String value; value = buffer.toString().trim(); // Ignores empty values, otherwise notifies the builder of a new variable. if(!value.isEmpty()) { try {builder.addVariable(qName, value);} catch(Exception e) {throw new SAXParseException(e.getMessage(), locator, e);} } } // The current element is a container, closes it. else { try {builder.endSection(qName);} catch(Exception e) {throw new SAXParseException(e.getMessage(), locator, e);} } isVariable = false; itemName = null; }
/** * This method is public as an implementation side effect and should never be called directly. */ @Override public void startDocument() throws SAXException { try {builder.startConfiguration();} catch(Exception e) {throw new SAXParseException(e.getMessage(), locator, e);} }
/** * This method is public as an implementation side effect and should never be called directly. */ @Override public void endDocument() throws SAXException { try {builder.endConfiguration();} catch(Exception e) {throw new SAXParseException(e.getMessage(), locator, e);} }