List of usage examples for org.apache.commons.validator Field getArgs
public Arg[] getArgs(String key)
From source file:net.jawr.web.resource.bundle.generator.validator.CommonsValidatorGenerator.java
@SuppressWarnings("unchecked") private StringBuffer createDynamicJavascript(ValidatorResources resources, Form form, String messageNS, boolean stopOnErrors) { StringBuffer results = new StringBuffer(); List<ValidatorAction> actions = this.createActionList(resources, form); final String methods = this.createMethods(actions, stopOnErrors); String jsFormName = form.getName(); results.append(this.getJavascriptBegin(jsFormName, methods)); for (Iterator<ValidatorAction> i = actions.iterator(); i.hasNext();) { ValidatorAction va = i.next();//from w w w . j a v a 2s . c o m int jscriptVar = 0; String functionName = null; if ((va.getJsFunctionName() != null) && (va.getJsFunctionName().length() > 0)) { functionName = va.getJsFunctionName(); } else { functionName = va.getName(); } results.append(" function " + jsFormName + "_" + functionName + " () { \n"); for (Iterator<Field> x = form.getFields().iterator(); x.hasNext();) { Field field = x.next(); // Skip indexed fields for now until there is a good way to // handle error messages (and the length of the list (could // retrieve from scope?)) if (field.isIndexed() || !field.isDependency(va.getName())) { continue; } String message = null; Msg msg = field.getMessage(va.getName()); if ((msg != null) && !msg.isResource()) { message = JavascriptStringUtil.quote(msg.toString()); } else { if (msg == null) { message = va.getMsg(); } else { message = msg.getKey(); } Arg[] args = field.getArgs(va.getName()); message = messageNS + "." + message + "("; for (int a = 0; a < args.length; a++) { if (args[a] != null) { if (args[a].isResource()) { message += messageNS + "." + args[a].getKey() + "()"; } else { message += "\"" + args[a].getKey() + "\""; } message += ","; } } message += "null)"; } message = (message != null) ? message : ""; // prefix variable with 'a' to make it a legal identifier results.append( " this.a" + jscriptVar++ + " = [\"" + field.getKey() + "\", " + (message) + ", "); results.append("function(varName){"); Map<String, Var> vars = field.getVars(); // Loop through the field's variables. Iterator<Entry<String, Var>> varsIterator = vars.entrySet().iterator(); while (varsIterator.hasNext()) { Entry<String, Var> varEntry = varsIterator.next(); String varName = varEntry.getKey(); Var var = varEntry.getValue(); String varValue = var.getValue(); // Non-resource variable if (var.isResource()) { varValue = messageNS + "." + varValue + "()"; } else varValue = "'" + varValue + "'"; String jsType = var.getJsType(); // skip requiredif variables field, fieldIndexed, fieldTest, // fieldValue if (varName.startsWith("field")) { continue; } String varValueEscaped = JavascriptStringUtil.escape(varValue); if (Var.JSTYPE_INT.equalsIgnoreCase(jsType)) { results.append("this." + varName + "=+" + varValueEscaped + "; "); } else if (Var.JSTYPE_REGEXP.equalsIgnoreCase(jsType)) { results.append("this." + varName + "=eval('/'+" + varValueEscaped + "+'/'); "); } else if (Var.JSTYPE_STRING.equalsIgnoreCase(jsType)) { results.append("this." + varName + "=" + varValueEscaped + "; "); // So everyone using the latest format doesn't need to // change their xml files immediately. } else if ("mask".equalsIgnoreCase(varName)) { results.append("this." + varName + "=eval('/'+" + varValueEscaped + "+'/'); "); } else { results.append("this." + varName + "=" + varValueEscaped + "; "); } } results.append(" return this[varName];}];\n"); } results.append(" } \n\n"); } return results; }
From source file:org.apache.struts.validator.Resources.java
/** * Gets the <code>Locale</code> sensitive value based on the key passed * in./*from w w w . jav a 2 s . c o m*/ * * @param application the servlet context * @param request the servlet request * @param defaultMessages The default Message resources * @param locale The locale * @param va The Validator Action * @param field The Validator Field */ public static String getMessage(ServletContext application, HttpServletRequest request, MessageResources defaultMessages, Locale locale, ValidatorAction va, Field field) { Msg msg = field.getMessage(va.getName()); if ((msg != null) && !msg.isResource()) { return msg.getKey(); } String msgKey = null; String msgBundle = null; MessageResources messages = defaultMessages; if (msg == null) { msgKey = va.getMsg(); } else { msgKey = msg.getKey(); msgBundle = msg.getBundle(); if (msg.getBundle() != null) { messages = getMessageResources(application, request, msg.getBundle()); } } if ((msgKey == null) || (msgKey.length() == 0)) { return "??? " + va.getName() + "." + field.getProperty() + " ???"; } // Get the arguments Arg[] args = field.getArgs(va.getName()); String[] argValues = getArgValues(application, request, messages, locale, args); // Return the message return messages.getMessage(locale, msgKey, argValues); }
From source file:org.apache.struts.validator.Resources.java
/** * Gets the <code>ActionMessage</code> based on the * <code>ValidatorAction</code> message and the <code>Field</code>'s arg * objects./*from w w w .java2s . co m*/ * * @param validator the Validator * @param request the servlet request * @param va Validator action * @param field the validator Field */ public static ActionMessage getActionMessage(Validator validator, HttpServletRequest request, ValidatorAction va, Field field) { Msg msg = field.getMessage(va.getName()); if ((msg != null) && !msg.isResource()) { return new ActionMessage(msg.getKey(), false); } String msgKey = null; String msgBundle = null; if (msg == null) { msgKey = va.getMsg(); } else { msgKey = msg.getKey(); msgBundle = msg.getBundle(); } if ((msgKey == null) || (msgKey.length() == 0)) { return new ActionMessage("??? " + va.getName() + "." + field.getProperty() + " ???", false); } ServletContext application = (ServletContext) validator.getParameterValue(SERVLET_CONTEXT_PARAM); MessageResources messages = getMessageResources(application, request, msgBundle); Locale locale = RequestUtils.getUserLocale(request, null); Arg[] args = field.getArgs(va.getName()); String[] argValues = getArgValues(application, request, messages, locale, args); ActionMessage actionMessage = null; if (msgBundle == null) { actionMessage = new ActionMessage(msgKey, argValues); } else { String message = messages.getMessage(locale, msgKey, argValues); actionMessage = new ActionMessage(message, false); } return actionMessage; }
From source file:org.openmobster.core.common.validation.ObjectValidator.java
/** * Validates the value of the specified Field on the target Object * //from w w w .ja v a 2 s. co m * @param object * @param fieldName * @return a Set of Validation Error Keys * @throws ValidationException */ public Set<String> validate(Object object, String fieldName) throws ValidationException { try { Set<String> errorKeys = new HashSet<String>(); String objectId = object.getClass().getName(); //Setup the Validator Validator validator = new Validator(this.validatorResources, objectId); validator.setParameter(Validator.BEAN_PARAM, object); validator.setFieldName(fieldName); ValidatorResults results = validator.validate(); Form form = this.validatorResources.getForm(Locale.getDefault(), objectId); Iterator propertyNames = results.getPropertyNames().iterator(); while (propertyNames.hasNext()) { String property = (String) propertyNames.next(); ValidatorResult result = results.getValidatorResult(property); Map actionMap = result.getActionMap(); Iterator keys = actionMap.keySet().iterator(); while (keys.hasNext()) { String actionName = (String) keys.next(); if (!result.isValid(actionName)) { Field field = form.getField(property); Arg[] args = field.getArgs(actionName); if (args != null) { for (int i = 0; i < args.length; i++) { errorKeys.add(args[i].getKey()); } } } } } return errorKeys; } catch (Exception e) { log.error(this, e); throw new ValidationException(e); } }
From source file:org.openmobster.core.common.validation.ObjectValidator.java
/** * Fully validates all the Fields of the specified Object * //from w w w . java 2 s . c om * @param object * @return a Map of Fields to their corresponding Validation Error Keys * * @throws ValidationException */ public Map<String, String[]> validate(Object object) throws ValidationException { try { Map<String, String[]> errorKeys = new HashMap<String, String[]>(); String objectId = object.getClass().getName(); //Setup the Validator Validator validator = new Validator(this.validatorResources, objectId); validator.setParameter(Validator.BEAN_PARAM, object); ValidatorResults results = validator.validate(); Form form = this.validatorResources.getForm(Locale.getDefault(), objectId); Iterator propertyNames = results.getPropertyNames().iterator(); while (propertyNames.hasNext()) { Set<String> cour = new HashSet<String>(); String property = (String) propertyNames.next(); ValidatorResult result = results.getValidatorResult(property); Map actionMap = result.getActionMap(); Iterator keys = actionMap.keySet().iterator(); boolean errorFound = false; while (keys.hasNext()) { String actionName = (String) keys.next(); if (!result.isValid(actionName)) { Field field = form.getField(property); Arg[] args = field.getArgs(actionName); if (args != null) { for (int i = 0; i < args.length; i++) { cour.add(args[i].getKey()); errorFound = true; } } } } if (errorFound) { errorKeys.put(property, cour.toArray(new String[cour.size()])); } } return errorKeys; } catch (Exception e) { log.error(this, e); throw new ValidationException(e); } }
From source file:org.seasar.struts.lessconfig.validator.config.impl.ArgConfigRegisterImpl.java
public void register(Field field, Map parameter) { String key = (String) parameter.get("key"); String name = (String) parameter.get("name"); if (StringUtil.isEmpty(name)) { name = null;/*w w w .ja v a2 s . c o m*/ } String bundle = (String) parameter.get("bundle"); if (StringUtil.isEmpty(bundle)) { bundle = this.defaultBundle; } String resourceStr = (String) parameter.get("resource"); boolean resource = this.defaultResource; if (!StringUtil.isEmpty(resourceStr)) { resource = BooleanConversionUtil.toPrimitiveBoolean(resourceStr); } String positionStr = (String) parameter.get("position"); int position = -1; if (!StringUtil.isEmpty(positionStr)) { position = Integer.parseInt(positionStr); } if (position < 0) { // default args length. position = field.getArgs("").length; } Arg arg = new Arg(); arg.setKey(key); arg.setName(name); arg.setBundle(bundle); arg.setResource(resource); arg.setPosition(position); field.addArg(arg); }