Clean up parser. Add sanity checks against XML. Add documentation.

This commit is contained in:
Michael Jumper
2013-08-22 16:48:19 -07:00
parent 5af0052430
commit 379912311c

View File

@@ -46,39 +46,96 @@ import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
*
* XML parser for the configuration file used by the NoAuth auth provider.
*
* @author Laurent Meunier
*/
public class NoAuthConfigContentHandler extends DefaultHandler {
/**
* Map of all configurations, indexed by name.
*/
private Map<String, GuacamoleConfiguration> configs = new HashMap<String, GuacamoleConfiguration>();
/**
* The name of the current configuration, if any.
*/
private String current = null;
/**
* The current configuration being parsed, if any.
*/
private GuacamoleConfiguration currentConfig = null;
/**
* Returns the a map of all available configurations as parsed from the
* XML file. This map is unmodifiable.
*
* @return A map of all available configurations.
*/
public Map<String, GuacamoleConfiguration> getConfigs() {
return Collections.unmodifiableMap(configs);
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
// If end of config element, add to map
if (localName.equals("config")) {
// Add to map
configs.put(current, currentConfig);
// Reset state for next configuration
currentConfig = null;
current = null;
}
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
// Begin configuration parsing if config element
if (localName.equals("config")) {
current = attributes.getValue("name");
// Ensure this config is on the top level
if (current != null)
throw new SAXException("Configurations cannot be nested.");
// Read name
String name = attributes.getValue("name");
if (name == null)
throw new SAXException("Each configuration must have a name.");
// Read protocol
String protocol = attributes.getValue("protocol");
if (protocol == null)
throw new SAXException("Each configuration must have a protocol.");
// Create config stub
current = name;
currentConfig = new GuacamoleConfiguration();
currentConfig.setProtocol(attributes.getValue("protocol"));
currentConfig.setProtocol(protocol);
}
// Add parameters to existing configuration
else if (localName.equals("param")) {
// Ensure a corresponding config exists
if (currentConfig == null)
throw new SAXException("Parameter without corresponding configuration.");
currentConfig.setParameter(attributes.getValue("name"), attributes.getValue("value"));
}
// Fail on unexpected elements
else
throw new SAXException("Unexpected element: " + localName);
}
}