1 /* 2 * Copyright 2004-2009 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.springmodules.validation.valang.javascript.taglib; 18 19 import java.util.Map; 20 import java.util.Collection; 21 import java.util.ArrayList; 22 23 import org.springframework.util.Assert; 24 import org.springframework.validation.Validator; 25 import org.springframework.web.servlet.mvc.BaseCommandController; 26 import org.springmodules.validation.valang.ValangValidator; 27 28 /** 29 * Static helper methods that place a <code>ValangValidator</code> into a model so 30 * that is is accessible to the JSP custom tag {@link ValangValidateTag}. 31 * 32 * @author Oliver Hutchison 33 * @author Uri Boness 34 */ 35 public abstract class ValangJavaScriptTagUtils { 36 37 /** 38 * Inserts the valang validator from the provided controller into 39 * the model using the controller's name as the validation rule's key. 40 * 41 * @param controller the controller that will provide the command name 42 * and validator 43 * @param model the model into which the validation rules will be placed 44 * @throws IllegalArgumentException if the controller does not specify a 45 * command name 46 * @throws IllegalArgumentException if the controller's validator is not 47 * an instance of {@link org.springmodules.validation.valang.ValangValidator} 48 */ 49 public static void addValangRulesToModel(BaseCommandController controller, Map model) { 50 Assert.hasText(controller.getCommandName(), "controller must define a command name"); 51 Validator validator = controller.getValidator(); 52 Assert.isInstanceOf(ValangValidator.class, validator, "controller's validator of class '" 53 + (validator != null ? validator.getClass().getName() : "[null]") 54 + "' must be an instance of 'ValangValidator'"); 55 addValangRulesToModel(controller.getCommandName(), (ValangValidator) validator, model); 56 } 57 58 /** 59 * Inserts the provided validator into the model using the provided command 60 * name as the validation rule's key. If there some rules that are already associated with the given command, the 61 * new rules will be added to them. 62 * 63 * @param commandName the command name 64 * @param validator the valang validator 65 * @param model the model into which the validation rules will be placed 66 */ 67 public static void addValangRulesToModel(String commandName, ValangValidator validator, Map model) { 68 Assert.notNull(commandName, "commandName is required."); 69 Assert.notNull(validator, "validator is required."); 70 String key = ValangValidateTag.VALANG_RULES_KEY_PREFIX + commandName; 71 Collection rules = (Collection)model.get(key); 72 if (rules == null) { 73 rules = new ArrayList(); 74 } 75 rules.addAll(validator.getRules()); 76 model.put(key, rules); 77 } 78 }