List of usage examples for org.apache.commons.validator Field getKey
public String getKey()
From source file:jp.terasoluna.fw.web.struts.form.FieldChecksEx.java
/** * wtB?[hpJi`FbN?B/* w ww . j a va 2 s .co m*/ * * <p> * G?[???AG?[????A wG?[?Xg?B G?[?bZ?[WtH?[}bg?A<code>validation-rules.xml</code> * L?q?B * </p> * * @param bean * ??IuWFNg * @param va * <code>Validator</code>p <code>ValidatorAction</code> * @param field * tB?[hIuWFNg * @param errors * ActionMessages ANVG?[ * @param validator * ValidatorCX^X * @param request * HTTPNGXg * @return l? <code>true</code> */ public static boolean validateHankakuKanaString(Object bean, ValidatorAction va, Field field, ActionMessages errors, Validator validator, HttpServletRequest request) { // beannull?AG?[?O?o?Atruep?B if (bean == null) { log.error("bean is null."); return true; } String value = null; if (isString(bean)) { value = (String) bean; } else { value = ValidatorUtils.getValueAsString(bean, field.getProperty()); } // ?lnull?Atruep if (GenericValidator.isBlankOrNull(value)) { return true; } char[] chars = value.toCharArray(); for (int i = 0; i < chars.length; i++) { if (!isHankakuKana(chars[i])) { errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); return false; } } return true; }
From source file:jp.terasoluna.fw.web.struts.form.FieldChecksEx.java
/** * wtB?[hSpJ^Ji`FbN?B// www . j a v a2s . c om * * <p> * G?[???AG?[????A wG?[?Xg?BG?[?bZ?[WtH?[}bg?A * <code>validation-rules.xml</code>L?q?B * </p> * * @param bean * ??IuWFNg * @param va * <code>Validator</code>p <code>ValidatorAction</code> * @param field * tB?[hIuWFNg * @param errors * ANVG?[ * @param validator * ValidatorCX^X * @param request * HTTPNGXg * @return l? <code>true</code> */ public static boolean validateZenkakuKanaString(Object bean, ValidatorAction va, Field field, ActionMessages errors, Validator validator, HttpServletRequest request) { // beannull?AG?[?O?o?Atruep?B if (bean == null) { log.error("bean is null."); return true; } String value = null; if (isString(bean)) { value = (String) bean; } else { value = ValidatorUtils.getValueAsString(bean, field.getProperty()); } // ?lnull?Atruep if (GenericValidator.isBlankOrNull(value)) { return true; } char[] chars = value.toCharArray(); for (int i = 0; i < chars.length; i++) { if (!isZenkakuKana(chars[i])) { errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); return false; } } return true; }
From source file:jp.terasoluna.fw.web.struts.form.FieldChecksEx.java
/** * wtB?[h~? `FbN?B//from www .j a v a2 s . co m * * <p> * G?[???AG?[????A wG?[?Xg?B G?[?bZ?[WtH?[}bg?A * <code>validation-rules.xml</code>L?q?B<br> * ?A~`FbN??B * </p> * <h5>validation.xmlL?q</h5> * <code><pre> * <form name="/sample"> * ?E?E?E * <field property="escape" * depends="prohibited"> * <arg0 key="sample.escape"/> * <var> * <var-name>chars</var-name> * <var-value>!"#$%&'()</var-value> * </var> * </field> * ?E?E?E * </form> * </pre></code> * <h5>validation.xml?v<var>vf</h5> * <table border="1"> * <tr> * <td><center><b><code>var-name</code></b></center></td> * <td><center><b><code>var-value</code></b></center></td> * <td><center><b>K?{?</b></center></td> * <td><center><b>Tv</b></center></td> * </tr> * <tr> * <td> chars </td> * <td>~LN^</td> * <td>false</td> * <td>?A~LN^?A <code>false</code>p?B?<code>true</code></td> * </tr> * </table> * * @param bean * ??IuWFNg * @param va * <code>Validator</code>p <code>ValidatorAction</code> * @param field * tB?[hIuWFNg * @param errors * ANVG?[ * @param validator * ValidatorCX^X * @param request * HTTPNGXg * @return l? <code>true</code> */ public static boolean validateProhibited(Object bean, ValidatorAction va, Field field, ActionMessages errors, Validator validator, HttpServletRequest request) { // beannull?AG?[?O?o?Atruep?B if (bean == null) { log.error("bean is null."); return true; } // ~ String prohibitedStr = field.getVarValue("chars"); if (prohibitedStr == null || "".equals(prohibitedStr)) { // ~null?Atruep return true; } String value = null; if (isString(bean)) { value = (String) bean; } else { value = ValidatorUtils.getValueAsString(bean, field.getProperty()); } // ?lnull?Atruep if (StringUtils.isEmpty(value)) { return true; } char[] chars = value.toCharArray(); // ~????A`FbN?s for (int i = 0; i < chars.length; i++) { if (prohibitedStr.indexOf(chars[i]) >= 0) { errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); return false; } } return true; }
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();/*w w w .java2 s. com*/ 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:jp.terasoluna.fw.web.struts.form.FieldChecksEx.java
/** * wtB?[h?l`FbN?B/* ww w. ja va 2s .c om*/ * * ?Ap?A<code>BigDecimal</code> ^?? * ??s\G?[pActionMessage???A<code>false</code> p?B * * ???w???A?mF?s?B <code>validation.xml</code> * <code>isAccordedInteger()</code> <code>"true"</code> w?? * ???`FbN?s?B `FbN???AG?[pActionMessage???Afalsep?B * * ????w???A?mF?s?B * validation.xmlisAccordedScale"true"?? ???`FbN?s?B * * <p> * G?[???AG?[????A wG?[?Xg?B G?[?bZ?[WtH?[}bg?A<code>validation-rules.xml</code> * L?q?B<br> * L?A??3?A??2?l??B * </p> * * <h5>validation.xmlL?q</h5> * <code><pre> * <form name="/sample"> * ?E?E?E * <field property="escape" * depends="number"> * <arg0 key="sample.escape"/> * <var> * <var-name>integerLength</var-name> * <var-value>3</var-value> * </var> * <var> * <var-name>scale</var-name> * <var-value>2</var-value> * </var> * <var> * <var-name>isAccordedInteger</var-name> * <var-value>true</var-value> * </var> * <var> * <var-name>isAccordedScale</var-name> * <var-value>true</var-value> * </var> * </field> * ?E?E?E * </form> * </pre></code> * <h5>validation.xml?v<var>vf</h5> * <table border="1"> * <tr> * <td><center><b><code>var-name</code></b></center></td> * <td><center><b><code>var-value</code></b></center></td> * <td><center><b>K?{?</b></center></td> * <td><center><b>Tv</b></center></td> * </tr> * <tr> * <td> <code>integerLength</code> </td> * <td> ??? </td> * <td> <code>false</code> </td> * <td>?????A<code>isAccordedInteger</code>w * ?Aw???s?B??A?l ??A??s?B</td> * </tr> * <tr> * <td> <code>scale</code> </td> * <td> ??? </td> * <td> <code>false</code> </td> * <td>??l???A<code>isAccordedScale</code>w * ?Aw???s?B??A?l ??A??s?B</td> * </tr> * <tr> * <td> <code>isAccordedInteger</code> </td> * <td> ???v`FbN </td> * <td> <code>false</code> </td> * <td> <code>true</code>w?A???v`FbN ?s?B??A<code>true</code>O? * ?`FbN?B</td> * </tr> * <tr> * <td> <code>isAccordedScale</code> </td> * <td> ???v`FbN </td> * <td> <code>false</code> </td> * <td> <code>true</code>w?A???v`FbN ?s?B??A<code>true</code>O? * ?`FbN?B</td> * </tr> * </table> * * @param bean * ??IuWFNg * @param va * StrutspValidatorAction * @param field * tB?[hIuWFNg * @param errors * ActionMessages ANVG?[ * @param validator * ValidatorCX^X * @param request * HTTPNGXg * @return l? <code>true</code> */ public static boolean validateNumber(Object bean, ValidatorAction va, Field field, ActionMessages errors, Validator validator, HttpServletRequest request) { // beannull?AG?[?O?o?Atruep?B if (bean == null) { log.error("bean is null."); return true; } // ??? String integerLength = field.getVarValue("integerLength"); // ??? String scaleStr = field.getVarValue("scale"); // ???v`FbN String isAccordedInteger = field.getVarValue("isAccordedInteger"); // ???v`FbN String isAccordedScale = field.getVarValue("isAccordedScale"); String value = null; if (isString(bean)) { value = (String) bean; } else { value = ValidatorUtils.getValueAsString(bean, field.getProperty()); } // ?lnull?Atruep if (GenericValidator.isBlankOrNull(value)) { return true; } char[] chars = value.toCharArray(); for (int i = 0; i < chars.length; i++) { if (!isHankaku(chars[i])) { errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); return false; } } // ???w???A??`FbN?s if (GenericValidator.isInt(integerLength)) { try { BigDecimal bd = new BigDecimal(value); // ???l?o BigInteger bi = bd.toBigInteger().abs(); // ??? int length = bi.toString().length(); // validation.xmlw? int checkLength = Integer.valueOf(integerLength).intValue(); // ?I?[o?Afalsep if (length > checkLength) { errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); return false; } // vw if (isAccordedInteger != null && "true".equals(isAccordedInteger)) { // ?sv?Afalsep if (length != checkLength) { errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); return false; } } } catch (NumberFormatException nfe) { // ?l^?Afalsep errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); return false; } } // ???w???A??`FbN?s if (GenericValidator.isInt(scaleStr)) { int scale = 0; int checkScale = 0; try { BigDecimal bd = new BigDecimal(value); scale = bd.scale(); // validation.xmlw? checkScale = Integer.valueOf(scaleStr).intValue(); } catch (NumberFormatException e) { // ?l^?Afalsep errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); return false; } // ?I?[o?Afalsep if (scale > checkScale) { errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); return false; } // vw if (isAccordedScale != null && "true".equals(isAccordedScale)) { // ?sv?Afalsep if (scale != checkScale) { errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); return false; } } } return true; }
From source file:jp.terasoluna.fw.web.struts.form.FieldChecksEx.java
/** * wtB?[hv`FbN?B/*www .j av a 2s.com*/ * * <p> * G?[???AG?[????A wG?[?Xg?B G?[?bZ?[WtH?[}bg?A * <code>validation-rules.xml</code>L?q?B<br> * ?A5<code>true</code>p ???B * </p> * <h5>validation.xmlL?q</h5> * <code><pre> * <form name="/sample"> * ?E?E?E * <field property="escape" * depends="stringLength"> * <arg0 key="sample.escape"/> * <var> * <var-name>length</var-name> * <var-value>5</var-value> * </var> * </field> * ?E?E?E * </form> * </pre></code> * <h5>validation.xml?v<var>vf</h5> * <table border="1"> * <tr> * <td><center><b><code>var-name</code></b></center></td> * <td><center><b><code>var-value</code></b></center></td> * <td><center><b>K?{?</b></center></td> * <td><center><b>Tv</b></center></td> * </tr> * <tr> * <td> stringLength </td> * <td>?</td> * <td>false</td> * <td>w? <code>true</code> ?<code>true</code>p?B</td> * </tr> * </table> * * @param bean * ??IuWFNg * @param va * ValidatorpValidatorAction * @param field * tB?[hIuWFNg * @param errors * ANVG?[ * @param validator * ValidatorCX^X * @param request * HTTPNGXg * @return l? <code>true</code> */ public static boolean validateStringLength(Object bean, ValidatorAction va, Field field, ActionMessages errors, Validator validator, HttpServletRequest request) { // beannull?AG?[?O?o?Atruep?B if (bean == null) { log.error("bean is null."); return true; } // String lengthStr = field.getVarValue("stringLength"); // w??A`FbN?s if (lengthStr == null || "".equals(lengthStr)) { return true; } String value = null; if (isString(bean)) { value = (String) bean; } else { value = ValidatorUtils.getValueAsString(bean, field.getProperty()); } // ?lnull?Atruep if (GenericValidator.isBlankOrNull(value)) { return true; } int checkLength = 0; try { checkLength = Integer.valueOf(lengthStr).intValue(); } catch (NumberFormatException e) { // wints\?? log.error("stringLength is not numeric(integer).", e); return true; } // length?Afalsep if (value.length() != checkLength) { errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); return false; } return true; }
From source file:jp.terasoluna.fw.web.struts.form.FieldChecksEx.java
/** * tw`FbN?B//w w w . j a v a 2s . c o m * * <p> * G?[???AG?[????A wG?[?Xg?B G?[?bZ?[WtH?[}bg?A * <code>validation-rules.xml</code>L?q?B<br> * ?At2000/01/012010/12/31 ???B * </p> * <h5>validation.xmlL?q</h5> * <code><pre> * <form name="/sample"> * ?E?E?E * <field property="date" * depends="dateRange"> * <arg key="label.date" position="0"/> * <var> * <var-name>startDate</var-name> * <var-value>2000/01/01</var-value> * </var> * <var> * <var-name>endDate</var-name> * <var-value>2010/12/31</var-value> * </var> * <var> * <var-name>datePattern</var-name> * <var-value>yyyy/MM/dd</var-value> * </var> * </field> * ?E?E?E * </form> * </pre></code> * <h5>validation.xml?v<var>vf</h5> * <table border="1"> * <tr> * <td><center><b><code>var-name</code></b></center></td> * <td><center><b><code>var-value</code></b></center></td> * <td><center><b>K?{?</b></center></td> * <td><center><b>Tv</b></center></td> * </tr> * <tr> * <td> startDate </td> * <td>Jnt</td> * <td>false</td> * <td>tJnlt?B * datePatterndatePatternStrictwttH?[}bgv?B</td> * </tr> * <tr> * <td> endDate </td> * <td>?It</td> * <td>false</td> * <td>t?Ilt?B * datePatterndatePatternStrictwttH?[}bgv?B</td> * </tr> * <tr> * <td> datePattern </td> * <td>tp^?[</td> * <td>false</td> * <td>tp^?[?BDate^tH?[}bg?[?]?B</td> * </tr> * <tr> * <td> datePatternStrict </td> * <td>tp^?[?BDate^tH?[}bg?[?]?B</td> * <td>false</td> * <td>tp^?[`FbN?s?B tp^?[yyyy/MM/dd???A2001/1/1G?[?B * datePatternw???AdatePatternwtH?[}bg D??B</td> * </tr> * </table> * * @param bean * ??IuWFNg * @param va * ValidatorpValidatorAction * @param field * tB?[hIuWFNg * @param errors * ANVG?[ * @param validator * ValidatorCX^X * @param request * HTTPNGXg * @return l? <code>true</code> */ public static boolean validateDateRange(Object bean, ValidatorAction va, Field field, ActionMessages errors, Validator validator, HttpServletRequest request) { // beannull?AG?[?O?o?Atruep?B if (bean == null) { log.error("bean is null."); return true; } String value = null; if (isString(bean)) { value = (String) bean; } else { value = ValidatorUtils.getValueAsString(bean, field.getProperty()); } // ?lnull?Atruep if (GenericValidator.isBlankOrNull(value)) { return true; } // t?`FbN String datePattern = field.getVarValue("datePattern"); String datePatternStrict = field.getVarValue("datePatternStrict"); Locale locale = RequestUtils.getUserLocale(request, null); Date result = null; try { if (datePattern != null && datePattern.length() > 0) { result = GenericTypeValidator.formatDate(value, datePattern, false); } else if (datePatternStrict != null && datePatternStrict.length() > 0) { result = GenericTypeValidator.formatDate(value, datePatternStrict, true); } else { result = GenericTypeValidator.formatDate(value, locale); } } catch (Exception e) { log.error(e.getMessage(), e); } if (result == null) { errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); return false; } // t`FbN // Jn String startDateStr = field.getVarValue("startDate"); // ?I String endDateStr = field.getVarValue("endDate"); if (startDateStr == null && endDateStr == null) { // tw???? return true; } // Jnt?~`FbN if (startDateStr != null && startDateStr.length() > 0) { Date startDate = null; if (datePattern != null && datePattern.length() > 0) { startDate = GenericTypeValidator.formatDate(startDateStr, datePattern, false); } else if (datePatternStrict != null && datePatternStrict.length() > 0) { startDate = GenericTypeValidator.formatDate(startDateStr, datePatternStrict, true); } else { startDate = GenericTypeValidator.formatDate(startDateStr, locale); } if (startDate == null) { errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); return false; } if (result.before(startDate)) { errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); return false; } } // ?ItO`FbN if (endDateStr != null && endDateStr.length() > 0) { Date endDate = null; if (datePattern != null && datePattern.length() > 0) { endDate = GenericTypeValidator.formatDate(endDateStr, datePattern, false); } else if (datePatternStrict != null && datePatternStrict.length() > 0) { endDate = GenericTypeValidator.formatDate(endDateStr, datePatternStrict, true); } else { endDate = GenericTypeValidator.formatDate(endDateStr, locale); } if (endDate == null) { errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); return false; } if (result.after(endDate)) { errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); return false; } } return true; }
From source file:jp.terasoluna.fw.web.struts.form.FieldChecksEx.java
/** * wtB?[hoCg w`FbN?B/* ww w .ja v a2 s .co m*/ * * <p> * G?[???AG?[????A wG?[?Xg?B G?[?bZ?[WtH?[}bg?A * <code>validation-rules.xml</code>L?q?B<br> * ?AoCg5??A10 <code>true</code> p???B * </p> * <h5>validation.xmlL?q</h5> * <code><pre> * <form name="/sample"> * ?E?E?E * <field property="escape" * depends="byteRange"> * <arg0 key="sample.escape"/> * <var> * <var-name>maxByte</var-name> * <var-value>10</var-value> * </var> * <var> * <var-name>minByte</var-name> * <var-value>5</var-value> * </var> * <var> * <var-name>encoding</var-name> * <var-value>Windows-31J</var-value> * </var> * </field> * ?E?E?E * </form> * </pre></code> * <h5>validation.xml?v<var>vf</h5> * <table border="1"> * <tr> * <td><center><b><code>var-name</code></b></center></td> * <td><center><b><code>var-value</code></b></center></td> * <td><center><b>K?{?</b></center></td> * <td><center><b>Tv</b></center></td> * </tr> * <tr> * <td> maxByte </td> * <td>?oCg?</td> * <td>false</td> * <td>oCg??oCg?B ???int^?l?B</td> * </tr> * <tr> * <td> minByte </td> * <td>?oCg?</td> * <td>false</td> * <td>oCg???oCg?B ???0?B</td> * </tr> * <tr> * <td> encoding </td> * <td>GR?[fBO</td> * <td>false</td> * <td>oCg?gpGR?[fBO?B ???VMftHgGR?[fBOgp?B</td> * </tr> * </table> * * @param bean * ??IuWFNg * @param va * ValidatorpValidatorAction * @param field * tB?[hIuWFNg * @param errors * ANVG?[ * @param validator * ValidatorCX^X * @param request * HTTPNGXg * @return l? <code>true</code> */ public static boolean validateByteRange(Object bean, ValidatorAction va, Field field, ActionMessages errors, Validator validator, HttpServletRequest request) { // beannull?AG?[?O?o?Atruep?B if (bean == null) { log.error("bean is null."); return true; } // l String value = null; if (isString(bean)) { value = (String) bean; } else { value = ValidatorUtils.getValueAsString(bean, field.getProperty()); } // ?lnull?Atruep if (GenericValidator.isBlankOrNull(value)) { return true; } // GR?[fBO String encoding = field.getVarValue("encoding"); // oCg int bytesLength = getByteLength(value, encoding); int min = 0; String minStr = field.getVarValue("minByte"); if (!GenericValidator.isBlankOrNull(minStr)) { try { min = Integer.parseInt(minStr); } catch (NumberFormatException e) { log.error("", e); } } int max = Integer.MAX_VALUE; String maxStr = field.getVarValue("maxByte"); if (!GenericValidator.isBlankOrNull(maxStr)) { try { max = Integer.parseInt(maxStr); } catch (NumberFormatException e) { log.error("", e); } } if (!GenericValidator.isInRange(bytesLength, min, max)) { errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); return false; } return true; }
From source file:jp.terasoluna.fw.web.struts.form.FieldChecksEx.java
/** * wtB?[hoCgv`FbN?B/*from w w w . j a va 2 s. co m*/ * * <p> * G?[???AG?[????A wG?[?Xg?B G?[?bZ?[WtH?[}bg?A * <code>validation-rules.xml</code>L?q?B<br> * ?AoCg5<code>true</code>p ???B * </p> * <h5>validation.xmlL?q</h5> * <code><pre> * <form name="/sample"> * ?E?E?E * <field property="escape" * depends="byteLength"> * <arg0 key="sample.escape"/> * <var> * <var-name>byteLength</var-name> * <var-value>5</var-value> * </var> * <var> * <var-name>encoding</var-name> * <var-value>Windows-31J</var-value> * </var> * </field> * ?E?E?E * </form> * </pre></code> * <h5>validation.xml?v<var>vf</h5> * <table border="1"> * <tr> * <td><center><b><code>var-name</code></b></center></td> * <td><center><b><code>var-value</code></b></center></td> * <td><center><b>K?{?</b></center></td> * <td><center><b>Tv</b></center></td> * </tr> * <tr> * <td> byteLength </td> * <td>oCg?</td> * <td>false</td> * <td>oCg?oCg?B</td> * </tr> * <tr> * <td> encoding </td> * <td>GR?[fBO</td> * <td>false</td> * <td>oCg?gpGR?[fBO?B ???VMftHgGR?[fBOgp?B</td> * </tr> * </table> * * @param bean * ??IuWFNg * @param va * ValidatorpValidatorAction * @param field * tB?[hIuWFNg * @param errors * ANVG?[ * @param validator * ValidatorCX^X * @param request * HTTPNGXg * @return l? <code>true</code> */ public static boolean validateByteLength(Object bean, ValidatorAction va, Field field, ActionMessages errors, Validator validator, HttpServletRequest request) { // beannull?AG?[?O?o?Atruep?B if (bean == null) { log.error("bean is null."); return true; } // l String value = null; if (isString(bean)) { value = (String) bean; } else { value = ValidatorUtils.getValueAsString(bean, field.getProperty()); } // ?lnull?Atruep if (GenericValidator.isBlankOrNull(value)) { return true; } // oCg String lengthStr = field.getVarValue("byteLength"); // oCgw??A`FbN?s if (lengthStr == null || "".equals(lengthStr)) { if (log.isInfoEnabled()) { log.info("length is not specified."); } return true; } int checkLength = 0; try { checkLength = Integer.valueOf(lengthStr).intValue(); } catch (NumberFormatException e) { // wints\?? log.error("byteLength is not numeric(integer).", e); return true; } // GR?[fBO String encoding = field.getVarValue("encoding"); int bytesLength = getByteLength(value, encoding); // oCg?length?Afalsep if (bytesLength != checkLength) { errors.add(field.getKey(), Resources.getActionMessage(validator, request, va, field)); return false; } return true; }
From source file:jp.terasoluna.fw.web.struts.form.FieldChecksEx.java
/** * z?ERNV^tB?[hlSvf`FbN?B// w w w . ja v a 2 s . c o m * <p> * z?ERNV^tH?[vf??A? CfbNX? <code>ActionMessage</code>?B * ?sTu?\bh?^?A???A?\bh vKv?B * Fieldproperty?JXPathIndexedBeanWrapperImpldl?]?A * lXgv?peB?\?B * </p> * * @param bean * ??IuWFNg * @param va * Strutsp<code>ValidatorAction</code> * @param field * tB?[hIuWFNg * @param errors * ANVG?[ * @param validator * ValidatorCX^X * @param request * HTTPNGXg * @return vfl? <code>true</code> */ public static boolean validateArraysIndex(@SuppressWarnings("unused") Object bean, ValidatorAction va, Field field, ActionMessages errors, Validator validator, HttpServletRequest request) { // ?? int methodArgCount = 6; // z?ERNV???ifalsep???Afalse?j boolean totalResult = true; try { // RXgN^?NXp^?[ Class[] paramClass = getParamClass(va); if (paramClass == null) { log.error("Can not get class pattern."); return true; } // paramClassvf?=0?AG?[?O?o?Atruep if (paramClass.length == 0) { log.error("Class pattern length is zero."); return true; } // evf??s?\bh Method method = getMethod(va, paramClass); if (method == null) { log.error("Can not get validateMethod."); return true; } // tH?[IuWFNg ActionForm form = getActionForm(request); if (form == null) { log.error("Can not get ActionForm."); return true; } // vfof?[V?[?A // O?svIuWFNg Object[] argParams = new Object[methodArgCount]; argParams[0] = form; argParams[1] = va; argParams[3] = errors; argParams[4] = validator; argParams[5] = request; IndexedBeanWrapper wrapper = new JXPathIndexedBeanWrapperImpl(form); Map<String, Object> propertyMap = wrapper.getIndexedPropertyValues(field.getKey()); int index = 0; for (String key : propertyMap.keySet()) { // CfbNXtv?peBtB?[h? Field indexedField = (Field) field.clone(); indexedField.setIndexedListProperty(null); indexedField = getArrayIndexField(indexedField, index); indexedField.setKey(key); indexedField.setProperty(key); argParams[2] = indexedField; // tB?[h // `FbN?\bh?o Object resultObj = method.invoke(FieldChecksEx.class, argParams); // ? if (!isValid(resultObj)) { totalResult = false; } index++; } } catch (IllegalArgumentException e) { log.error("", e); return true; } catch (IllegalAccessException e) { log.error("", e); return true; } catch (InvocationTargetException e) { log.error("", e); return true; } return totalResult; }