From b2dae80da05319b4decbb94b72f427696bb8385a Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Mon, 11 Mar 2013 22:52:35 -0700 Subject: [PATCH] Move Attribute awareness to new init() event of TagHandler. --- .../guacamole/net/basic/xml/DocumentHandler.java | 5 ++++- .../guacamole/net/basic/xml/TagHandler.java | 13 +++++++++++-- .../xml/user_mapping/AuthorizeTagHandler.java | 16 +++++----------- .../xml/user_mapping/ConnectionTagHandler.java | 14 ++++---------- .../basic/xml/user_mapping/ParamTagHandler.java | 15 +++++++-------- .../xml/user_mapping/ProtocolTagHandler.java | 7 ++++++- .../xml/user_mapping/UserMappingTagHandler.java | 10 +++++++--- 7 files changed, 44 insertions(+), 36 deletions(-) diff --git a/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/xml/DocumentHandler.java b/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/xml/DocumentHandler.java index b46da4281..8c123dfd5 100644 --- a/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/xml/DocumentHandler.java +++ b/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/xml/DocumentHandler.java @@ -104,13 +104,16 @@ public class DocumentHandler extends DefaultHandler { // Otherwise, get handler from parent else { 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 (handler == null) throw new SAXException("Unexpected element: '" + localName + "'"); + // Initialize handler + handler.init(attributes); + // Append new element state to stack stack.addLast(new DocumentHandlerState(handler)); diff --git a/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/xml/TagHandler.java b/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/xml/TagHandler.java index 6383f4af8..90af9dc09 100644 --- a/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/xml/TagHandler.java +++ b/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/xml/TagHandler.java @@ -33,16 +33,25 @@ public interface TagHandler { * Called when a child element of the current element is parsed. * * @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 * related to the child element. * @throws SAXException If the child element being parsed was not expected, * or some other error prevents a proper TagHandler * from being constructed for the child element. */ - public TagHandler childElement(String localName, Attributes attributes) + public TagHandler childElement(String localName) 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, * and the entire text content of this element (if any) is available. diff --git a/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/xml/user_mapping/AuthorizeTagHandler.java b/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/xml/user_mapping/AuthorizeTagHandler.java index c8a7e4338..b9b580dbe 100644 --- a/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/xml/user_mapping/AuthorizeTagHandler.java +++ b/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/xml/user_mapping/AuthorizeTagHandler.java @@ -44,14 +44,8 @@ public class AuthorizeTagHandler implements TagHandler { */ private GuacamoleConfiguration default_config = null; - /** - * Creates a new handler for an "authorize" tag having the given - * attributes. - * - * @param attributes The attributes of the "authorize" tag. - * @throws SAXException If the attributes given are not valid. - */ - public AuthorizeTagHandler(Attributes attributes) throws SAXException { + @Override + public void init(Attributes attributes) throws SAXException { // Init username and password authorization.setUsername(attributes.getValue("username")); @@ -79,13 +73,13 @@ public class AuthorizeTagHandler implements TagHandler { } @Override - public TagHandler childElement(String localName, Attributes attributes) throws SAXException { + public TagHandler childElement(String localName) throws SAXException { // "connection" tag if (localName.equals("connection")) { // Get tag handler for connection tag - ConnectionTagHandler tagHandler = new ConnectionTagHandler(attributes); + ConnectionTagHandler tagHandler = new ConnectionTagHandler(); // Store configuration stub GuacamoleConfiguration config_stub = tagHandler.asGuacamoleConfiguration(); @@ -103,7 +97,7 @@ public class AuthorizeTagHandler implements TagHandler { authorization.addConfiguration("DEFAULT", default_config); } - return new ParamTagHandler(default_config, attributes); + return new ParamTagHandler(default_config); } // "protocol" tag diff --git a/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/xml/user_mapping/ConnectionTagHandler.java b/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/xml/user_mapping/ConnectionTagHandler.java index 69bd62b71..62c51ff4f 100644 --- a/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/xml/user_mapping/ConnectionTagHandler.java +++ b/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/xml/user_mapping/ConnectionTagHandler.java @@ -40,22 +40,16 @@ public class ConnectionTagHandler implements TagHandler { */ private String name; - /** - * Creates a new handler for an "connection" tag having the given - * attributes. - * - * @param attributes The attributes of the "connection" tag. - * @throws SAXException If the attributes given are not valid. - */ - public ConnectionTagHandler(Attributes attributes) throws SAXException { + @Override + public void init(Attributes attributes) throws SAXException { name = attributes.getValue("name"); } @Override - public TagHandler childElement(String localName, Attributes attributes) throws SAXException { + public TagHandler childElement(String localName) throws SAXException { if (localName.equals("param")) - return new ParamTagHandler(config, attributes); + return new ParamTagHandler(config); if (localName.equals("protocol")) return new ProtocolTagHandler(config); diff --git a/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/xml/user_mapping/ParamTagHandler.java b/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/xml/user_mapping/ParamTagHandler.java index d66206905..d55c72bb6 100644 --- a/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/xml/user_mapping/ParamTagHandler.java +++ b/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/xml/user_mapping/ParamTagHandler.java @@ -47,19 +47,18 @@ public class ParamTagHandler implements TagHandler { * * @param config The GuacamoleConfiguration to update with the data parsed * 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, - Attributes attributes) throws SAXException { - + public ParamTagHandler(GuacamoleConfiguration config) { this.config = config; - this.name = attributes.getValue("name"); - } @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."); } diff --git a/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/xml/user_mapping/ProtocolTagHandler.java b/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/xml/user_mapping/ProtocolTagHandler.java index 02168036d..bbffc5df8 100644 --- a/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/xml/user_mapping/ProtocolTagHandler.java +++ b/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/xml/user_mapping/ProtocolTagHandler.java @@ -49,7 +49,12 @@ public class ProtocolTagHandler implements TagHandler { } @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."); } diff --git a/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/xml/user_mapping/UserMappingTagHandler.java b/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/xml/user_mapping/UserMappingTagHandler.java index e1a71a8e1..842871fe8 100644 --- a/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/xml/user_mapping/UserMappingTagHandler.java +++ b/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/xml/user_mapping/UserMappingTagHandler.java @@ -37,14 +37,18 @@ public class UserMappingTagHandler implements TagHandler { private UserMapping user_mapping = new UserMapping(); @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 if (localName.equals("authorize")) { // Get tag handler for authorize tag - AuthorizeTagHandler tagHandler = - new AuthorizeTagHandler(attributes); + AuthorizeTagHandler tagHandler = new AuthorizeTagHandler(); // Store authorization stub in map of authorizations Authorization auth_stub = tagHandler.asAuthorization();