From 379912311cf89d96a6a9b4c47bfc89f15437727b Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Thu, 22 Aug 2013 16:48:19 -0700 Subject: [PATCH] Clean up parser. Add sanity checks against XML. Add documentation. --- .../noauth/NoAuthConfigContentHandler.java | 65 +++++++++++++++++-- 1 file changed, 61 insertions(+), 4 deletions(-) diff --git a/extensions/guacamole-auth-noauth/src/main/java/net/sourceforge/guacamole/net/auth/noauth/NoAuthConfigContentHandler.java b/extensions/guacamole-auth-noauth/src/main/java/net/sourceforge/guacamole/net/auth/noauth/NoAuthConfigContentHandler.java index 546877c64..9ffadfe09 100644 --- a/extensions/guacamole-auth-noauth/src/main/java/net/sourceforge/guacamole/net/auth/noauth/NoAuthConfigContentHandler.java +++ b/extensions/guacamole-auth-noauth/src/main/java/net/sourceforge/guacamole/net/auth/noauth/NoAuthConfigContentHandler.java @@ -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 configs = new HashMap(); + + /** + * 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 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); + } }