Move Attribute awareness to new init() event of TagHandler.

This commit is contained in:
Michael Jumper
2013-03-11 22:52:35 -07:00
parent 905b041192
commit b2dae80da0
7 changed files with 44 additions and 36 deletions

View File

@@ -104,13 +104,16 @@ public class DocumentHandler extends DefaultHandler {
// Otherwise, get handler from parent // Otherwise, get handler from parent
else { else {
TagHandler parent_handler = current.getTagHandler(); TagHandler parent_handler = current.getTagHandler();
handler = parent_handler.childElement(localName, attributes); handler = parent_handler.childElement(localName);
} }
// If no handler returned, the element was not expected // If no handler returned, the element was not expected
if (handler == null) if (handler == null)
throw new SAXException("Unexpected element: '" + localName + "'"); throw new SAXException("Unexpected element: '" + localName + "'");
// Initialize handler
handler.init(attributes);
// Append new element state to stack // Append new element state to stack
stack.addLast(new DocumentHandlerState(handler)); stack.addLast(new DocumentHandlerState(handler));

View File

@@ -33,16 +33,25 @@ public interface TagHandler {
* Called when a child element of the current element is parsed. * Called when a child element of the current element is parsed.
* *
* @param localName The local name of the child element seen. * @param localName The local name of the child element seen.
* @param attributes The attributes of the child element seen.
* @return The TagHandler which should handle all element-level events * @return The TagHandler which should handle all element-level events
* related to the child element. * related to the child element.
* @throws SAXException If the child element being parsed was not expected, * @throws SAXException If the child element being parsed was not expected,
* or some other error prevents a proper TagHandler * or some other error prevents a proper TagHandler
* from being constructed for the child element. * from being constructed for the child element.
*/ */
public TagHandler childElement(String localName, Attributes attributes) public TagHandler childElement(String localName)
throws SAXException; throws SAXException;
/**
* Called when the element corresponding to this TagHandler is first seen,
* just after an instance is created.
*
* @param attributes The attributes of the element seen.
* @throws SAXException If an error prevents a the TagHandler from being
* from being initialized.
*/
public void init(Attributes attributes) throws SAXException;
/** /**
* Called when this element, and all child elements, have been fully parsed, * Called when this element, and all child elements, have been fully parsed,
* and the entire text content of this element (if any) is available. * and the entire text content of this element (if any) is available.

View File

@@ -44,14 +44,8 @@ public class AuthorizeTagHandler implements TagHandler {
*/ */
private GuacamoleConfiguration default_config = null; private GuacamoleConfiguration default_config = null;
/** @Override
* Creates a new handler for an "authorize" tag having the given public void init(Attributes attributes) throws SAXException {
* attributes.
*
* @param attributes The attributes of the "authorize" tag.
* @throws SAXException If the attributes given are not valid.
*/
public AuthorizeTagHandler(Attributes attributes) throws SAXException {
// Init username and password // Init username and password
authorization.setUsername(attributes.getValue("username")); authorization.setUsername(attributes.getValue("username"));
@@ -79,13 +73,13 @@ public class AuthorizeTagHandler implements TagHandler {
} }
@Override @Override
public TagHandler childElement(String localName, Attributes attributes) throws SAXException { public TagHandler childElement(String localName) throws SAXException {
// "connection" tag // "connection" tag
if (localName.equals("connection")) { if (localName.equals("connection")) {
// Get tag handler for connection tag // Get tag handler for connection tag
ConnectionTagHandler tagHandler = new ConnectionTagHandler(attributes); ConnectionTagHandler tagHandler = new ConnectionTagHandler();
// Store configuration stub // Store configuration stub
GuacamoleConfiguration config_stub = tagHandler.asGuacamoleConfiguration(); GuacamoleConfiguration config_stub = tagHandler.asGuacamoleConfiguration();
@@ -103,7 +97,7 @@ public class AuthorizeTagHandler implements TagHandler {
authorization.addConfiguration("DEFAULT", default_config); authorization.addConfiguration("DEFAULT", default_config);
} }
return new ParamTagHandler(default_config, attributes); return new ParamTagHandler(default_config);
} }
// "protocol" tag // "protocol" tag

View File

@@ -40,22 +40,16 @@ public class ConnectionTagHandler implements TagHandler {
*/ */
private String name; private String name;
/** @Override
* Creates a new handler for an "connection" tag having the given public void init(Attributes attributes) throws SAXException {
* attributes.
*
* @param attributes The attributes of the "connection" tag.
* @throws SAXException If the attributes given are not valid.
*/
public ConnectionTagHandler(Attributes attributes) throws SAXException {
name = attributes.getValue("name"); name = attributes.getValue("name");
} }
@Override @Override
public TagHandler childElement(String localName, Attributes attributes) throws SAXException { public TagHandler childElement(String localName) throws SAXException {
if (localName.equals("param")) if (localName.equals("param"))
return new ParamTagHandler(config, attributes); return new ParamTagHandler(config);
if (localName.equals("protocol")) if (localName.equals("protocol"))
return new ProtocolTagHandler(config); return new ProtocolTagHandler(config);

View File

@@ -47,19 +47,18 @@ public class ParamTagHandler implements TagHandler {
* *
* @param config The GuacamoleConfiguration to update with the data parsed * @param config The GuacamoleConfiguration to update with the data parsed
* from the "protocol" tag. * from the "protocol" tag.
* @param attributes The attributes of the "param" tag.
* @throws SAXException If the attributes given are not valid.
*/ */
public ParamTagHandler(GuacamoleConfiguration config, public ParamTagHandler(GuacamoleConfiguration config) {
Attributes attributes) throws SAXException {
this.config = config; this.config = config;
this.name = attributes.getValue("name");
} }
@Override @Override
public TagHandler childElement(String localName, Attributes attributes) throws SAXException { public void init(Attributes attributes) throws SAXException {
this.name = attributes.getValue("name");
}
@Override
public TagHandler childElement(String localName) throws SAXException {
throw new SAXException("The 'param' tag can contain no elements."); throw new SAXException("The 'param' tag can contain no elements.");
} }

View File

@@ -49,7 +49,12 @@ public class ProtocolTagHandler implements TagHandler {
} }
@Override @Override
public TagHandler childElement(String localName, Attributes attributes) throws SAXException { public void init(Attributes attributes) throws SAXException {
// Do nothing
}
@Override
public TagHandler childElement(String localName) throws SAXException {
throw new SAXException("The 'protocol' tag can contain no elements."); throw new SAXException("The 'protocol' tag can contain no elements.");
} }

View File

@@ -37,14 +37,18 @@ public class UserMappingTagHandler implements TagHandler {
private UserMapping user_mapping = new UserMapping(); private UserMapping user_mapping = new UserMapping();
@Override @Override
public TagHandler childElement(String localName, Attributes attributes) throws SAXException { public void init(Attributes attributes) throws SAXException {
// Do nothing
}
@Override
public TagHandler childElement(String localName) throws SAXException {
// Start parsing of authorize tags, add to list of all authorizations // Start parsing of authorize tags, add to list of all authorizations
if (localName.equals("authorize")) { if (localName.equals("authorize")) {
// Get tag handler for authorize tag // Get tag handler for authorize tag
AuthorizeTagHandler tagHandler = AuthorizeTagHandler tagHandler = new AuthorizeTagHandler();
new AuthorizeTagHandler(attributes);
// Store authorization stub in map of authorizations // Store authorization stub in map of authorizations
Authorization auth_stub = tagHandler.asAuthorization(); Authorization auth_stub = tagHandler.asAuthorization();