mirror of
https://github.com/gyurix1968/guacamole-client.git
synced 2025-09-06 13:17:41 +00:00
Clean up parser. Add sanity checks against XML. Add documentation.
This commit is contained in:
@@ -46,39 +46,96 @@ import org.xml.sax.SAXException;
|
|||||||
import org.xml.sax.helpers.DefaultHandler;
|
import org.xml.sax.helpers.DefaultHandler;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* XML parser for the configuration file used by the NoAuth auth provider.
|
||||||
*
|
*
|
||||||
* @author Laurent Meunier
|
* @author Laurent Meunier
|
||||||
*/
|
*/
|
||||||
public class NoAuthConfigContentHandler extends DefaultHandler {
|
public class NoAuthConfigContentHandler extends DefaultHandler {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Map of all configurations, indexed by name.
|
||||||
|
*/
|
||||||
private Map<String, GuacamoleConfiguration> configs = new HashMap<String, GuacamoleConfiguration>();
|
private Map<String, GuacamoleConfiguration> configs = new HashMap<String, GuacamoleConfiguration>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name of the current configuration, if any.
|
||||||
|
*/
|
||||||
private String current = null;
|
private String current = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The current configuration being parsed, if any.
|
||||||
|
*/
|
||||||
private GuacamoleConfiguration currentConfig = null;
|
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() {
|
public Map<String, GuacamoleConfiguration> getConfigs() {
|
||||||
return Collections.unmodifiableMap(configs);
|
return Collections.unmodifiableMap(configs);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void endElement(String uri, String localName, String qName) throws SAXException {
|
public void endElement(String uri, String localName, String qName) throws SAXException {
|
||||||
|
|
||||||
|
// If end of config element, add to map
|
||||||
if (localName.equals("config")) {
|
if (localName.equals("config")) {
|
||||||
|
|
||||||
|
// Add to map
|
||||||
configs.put(current, currentConfig);
|
configs.put(current, currentConfig);
|
||||||
|
|
||||||
|
// Reset state for next configuration
|
||||||
|
currentConfig = null;
|
||||||
|
current = null;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
|
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
|
||||||
|
|
||||||
|
// Begin configuration parsing if config element
|
||||||
if (localName.equals("config")) {
|
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 = new GuacamoleConfiguration();
|
||||||
currentConfig.setProtocol(attributes.getValue("protocol"));
|
currentConfig.setProtocol(protocol);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add parameters to existing configuration
|
||||||
else if (localName.equals("param")) {
|
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"));
|
currentConfig.setParameter(attributes.getValue("name"), attributes.getValue("value"));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fail on unexpected elements
|
||||||
|
else
|
||||||
|
throw new SAXException("Unexpected element: " + localName);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user