diff --git a/source/java/org/alfresco/web/config/ConstraintHandlersConfigElement.java b/source/java/org/alfresco/web/config/ConstraintHandlersConfigElement.java new file mode 100644 index 0000000000..7467789edc --- /dev/null +++ b/source/java/org/alfresco/web/config/ConstraintHandlersConfigElement.java @@ -0,0 +1,186 @@ +/* + * Copyright (C) 2005-2007 Alfresco Software Limited. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + * As a special exception to the terms and conditions of version 2.0 of + * the GPL, you may redistribute this Program in connection with Free/Libre + * and Open Source Software ("FLOSS") applications as described in Alfresco's + * FLOSS exception. You should have recieved a copy of the text describing + * the FLOSS exception, and it is also available here: + * http://www.alfresco.com/legal/licensing" + */ +package org.alfresco.web.config; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.alfresco.config.ConfigElement; +import org.alfresco.config.ConfigException; +import org.alfresco.config.element.ConfigElementAdapter; + +public class ConstraintHandlersConfigElement extends ConfigElementAdapter +{ + public static final String CONFIG_ELEMENT_ID = "constraint-handlers"; + private List types = new ArrayList(); + private Map handlers = new HashMap(); + private Map messages = new HashMap(); + private Map messageIDs = new HashMap(); + + private static final long serialVersionUID = 1L; + + /** + * This constructor creates an instance with the default name. + */ + public ConstraintHandlersConfigElement() + { + super(CONFIG_ELEMENT_ID); + } + + /** + * This constructor creates an instance with the specified name. + * + * @param name the name for the ConfigElement. + */ + public ConstraintHandlersConfigElement(String name) + { + super(name); + } + + /** + * @see org.alfresco.config.ConfigElement#getChildren() + */ + public List getChildren() + { + throw new ConfigException( + "Reading the constraint-handlers config via the generic interfaces is not supported"); + } + + /** + * @see org.alfresco.config.ConfigElement#combine(org.alfresco.config.ConfigElement) + */ + public ConfigElement combine(ConfigElement configElement) + { + // There is an assumption here that it is only like-with-like combinations + // that are allowed. i.e. Only an instance of a ConstraintHandlersConfigElement + // can be combined with this. + ConstraintHandlersConfigElement otherCHCElement = (ConstraintHandlersConfigElement) configElement; + + ConstraintHandlersConfigElement result = new ConstraintHandlersConfigElement(); + + for (String nextType : types) + { + String nextValidationHandler = getValidationHandlerFor(nextType); + String nextMessage = getMessageFor(nextType); + String nextMessageId = getMessageIdFor(nextType); + result.addDataMapping(nextType, nextValidationHandler, nextMessage, + nextMessageId); + } + + for (String nextType : otherCHCElement.types) + { + String nextValidationHandler = otherCHCElement + .getValidationHandlerFor(nextType); + String nextMessage = otherCHCElement.getMessageFor(nextType); + String nextMessageId = otherCHCElement.getMessageIdFor(nextType); + result.addDataMapping(nextType, nextValidationHandler, nextMessage, + nextMessageId); + } + + return result; + } + + /* package */void addDataMapping(String type, String validationHandler, + String message, String messageID) + { + types.add(type); + handlers.put(type, validationHandler); + messages.put(type, message); + messageIDs.put(type, messageID); + } + + /** + * @see java.lang.Object#hashCode() + */ + public int hashCode() + { + return types.hashCode() + 7 * handlers.hashCode() + 13 + * messages.hashCode() + 17 * messageIDs.hashCode(); + } + + /** + * @see java.lang.Object#equals(java.lang.Object) + */ + public boolean equals(Object otherObj) + { + if (otherObj == null || !otherObj.getClass().equals(this.getClass())) + { + return false; + } + ConstraintHandlersConfigElement otherCHCE = (ConstraintHandlersConfigElement) otherObj; + return this.types.equals(otherCHCE.types) + && this.handlers.equals(otherCHCE.handlers) + && this.messages.equals(otherCHCE.messages) + && this.messageIDs.equals(otherCHCE.messageIDs); + } + + /** + * This method returns the registered constraint types. + * @return an unmodifiable List of the constraint types. + */ + public List getConstraintTypes() + { + return Collections.unmodifiableList(this.types); + } + + /** + * This method returns a String identifier for the validation-handler + * associated with the specified constraint type. + * + * @param type the constraint type. + * @return a String identifier for the validation-handler. + */ + public String getValidationHandlerFor(String type) + { + return handlers.get(type); + } + + /** + * This method returns a message String associated with the specified constraint + * type. + * + * @param type the constraint type. + * @return the message String for the validation-handler. + */ + public String getMessageFor(String type) + { + return messages.get(type); + } + + /** + * This method returns a message-id String associated with the specified constraint + * type. + * + * @param type the constraint type. + * @return the message-id String for the validation-handler. + */ + public String getMessageIdFor(String type) + { + return messageIDs.get(type); + } +} diff --git a/source/java/org/alfresco/web/config/ConstraintHandlersElementReader.java b/source/java/org/alfresco/web/config/ConstraintHandlersElementReader.java new file mode 100644 index 0000000000..8edcf0e1db --- /dev/null +++ b/source/java/org/alfresco/web/config/ConstraintHandlersElementReader.java @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2005-2008 Alfresco Software Limited. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + * As a special exception to the terms and conditions of version 2.0 of + * the GPL, you may redistribute this Program in connection with Free/Libre + * and Open Source Software ("FLOSS") applications as described in Alfresco's + * FLOSS exception. You should have received a copy of the text describing + * the FLOSS exception, and it is also available here: + * http://www.alfresco.com/legal/licensing" + */ +package org.alfresco.web.config; + +import java.util.Iterator; + +import org.alfresco.config.ConfigElement; +import org.alfresco.config.ConfigException; +import org.alfresco.config.xml.elementreader.ConfigElementReader; +import org.dom4j.Element; + +/** + * This class is a custom element reader to parse the config file for + * <constraint-handlers> elements. + * + * @author Neil McErlean. + */ +public class ConstraintHandlersElementReader implements ConfigElementReader +{ + public static final String ELEMENT_CONSTRAINT_HANDLERS = "constraint-handlers"; + public static final String ATTR_TYPE = "type"; + public static final String ATTR_VALIDATOR_HANDLER = "validation-handler"; + public static final String ATTR_MESSAGE = "message"; + public static final String ATTR_MESSAGE_ID = "message-id"; + + /** + * @see org.alfresco.config.xml.elementreader.ConfigElementReader#parse(org.dom4j.Element) + */ + @SuppressWarnings("unchecked") + public ConfigElement parse(Element element) + { + ConstraintHandlersConfigElement result = null; + if (element == null) + { + return null; + } + + String name = element.getName(); + if (!name.equals(ELEMENT_CONSTRAINT_HANDLERS)) + { + throw new ConfigException(this.getClass().getName() + " can only parse " + + ELEMENT_CONSTRAINT_HANDLERS + " elements, the element passed was '" + name + "'"); + } + + result = new ConstraintHandlersConfigElement(); + + Iterator xmlNodes = element.elementIterator(); + while (xmlNodes.hasNext()) + { + Element nextNode = xmlNodes.next(); + String type = nextNode.attributeValue(ATTR_TYPE); + String validationHandler = nextNode.attributeValue(ATTR_VALIDATOR_HANDLER); + String message = nextNode.attributeValue(ATTR_MESSAGE); + String messageId = nextNode.attributeValue(ATTR_MESSAGE_ID); + + result.addDataMapping(type, validationHandler, message, messageId); + } + + return result; + } +}