Source Code Cross Referenced for FieldChecks.java in  » Web-Framework » struts-1.3.8 » org » apache » struts » validator » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » struts 1.3.8 » org.apache.struts.validator 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * $Id: FieldChecks.java 471754 2006-11-06 14:55:09Z husted $
0003:         *
0004:         * Licensed to the Apache Software Foundation (ASF) under one
0005:         * or more contributor license agreements.  See the NOTICE file
0006:         * distributed with this work for additional information
0007:         * regarding copyright ownership.  The ASF licenses this file
0008:         * to you under the Apache License, Version 2.0 (the
0009:         * "License"); you may not use this file except in compliance
0010:         * with the License.  You may obtain a copy of the License at
0011:         *
0012:         *  http://www.apache.org/licenses/LICENSE-2.0
0013:         *
0014:         * Unless required by applicable law or agreed to in writing,
0015:         * software distributed under the License is distributed on an
0016:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0017:         * KIND, either express or implied.  See the License for the
0018:         * specific language governing permissions and limitations
0019:         * under the License.
0020:         */
0021:        package org.apache.struts.validator;
0022:
0023:        import org.apache.commons.logging.Log;
0024:        import org.apache.commons.logging.LogFactory;
0025:        import org.apache.commons.validator.Field;
0026:        import org.apache.commons.validator.GenericTypeValidator;
0027:        import org.apache.commons.validator.GenericValidator;
0028:        import org.apache.commons.validator.UrlValidator;
0029:        import org.apache.commons.validator.Validator;
0030:        import org.apache.commons.validator.ValidatorAction;
0031:        import org.apache.commons.validator.util.ValidatorUtils;
0032:        import org.apache.struts.action.ActionMessage;
0033:        import org.apache.struts.action.ActionMessages;
0034:        import org.apache.struts.util.MessageResources;
0035:        import org.apache.struts.util.RequestUtils;
0036:
0037:        import javax.servlet.http.HttpServletRequest;
0038:
0039:        import java.io.Serializable;
0040:
0041:        import java.util.Locale;
0042:        import java.util.StringTokenizer;
0043:
0044:        /**
0045:         * <p> This class contains the default validations that are used in the
0046:         * validator-rules.xml file. </p> <p> In general passing in a null or blank
0047:         * will return a null Object or a false boolean. However, nulls and blanks do
0048:         * not result in an error being added to the errors. </p>
0049:         *
0050:         * @since Struts 1.1
0051:         */
0052:        public class FieldChecks implements  Serializable {
0053:            /**
0054:             * Commons Logging instance.
0055:             */
0056:            private static final Log log = LogFactory.getLog(FieldChecks.class);
0057:
0058:            /**
0059:             * The message resources for this package.
0060:             */
0061:            private static MessageResources sysmsgs = MessageResources
0062:                    .getMessageResources("org.apache.struts.validator.LocalStrings");
0063:            public static final String FIELD_TEST_NULL = "NULL";
0064:            public static final String FIELD_TEST_NOTNULL = "NOTNULL";
0065:            public static final String FIELD_TEST_EQUAL = "EQUAL";
0066:
0067:            /**
0068:             * Checks if the field isn't null and length of the field is greater than
0069:             * zero not including whitespace.
0070:             *
0071:             * @param bean      The bean validation is being performed on.
0072:             * @param va        The <code>ValidatorAction</code> that is currently
0073:             *                  being performed.
0074:             * @param field     The <code>Field</code> object associated with the
0075:             *                  current field being validated.
0076:             * @param errors    The <code>ActionMessages</code> object to add errors
0077:             *                  to if any validation errors occur.
0078:             * @param validator The <code>Validator</code> instance, used to access
0079:             *                  other field values.
0080:             * @param request   Current request object.
0081:             * @return true if meets stated requirements, false otherwise.
0082:             */
0083:            public static boolean validateRequired(Object bean,
0084:                    ValidatorAction va, Field field, ActionMessages errors,
0085:                    Validator validator, HttpServletRequest request) {
0086:                String value = null;
0087:
0088:                value = evaluateBean(bean, field);
0089:
0090:                if (GenericValidator.isBlankOrNull(value)) {
0091:                    errors.add(field.getKey(), Resources.getActionMessage(
0092:                            validator, request, va, field));
0093:
0094:                    return false;
0095:                } else {
0096:                    return true;
0097:                }
0098:            }
0099:
0100:            /**
0101:             * Checks if the field isn't null based on the values of other fields.
0102:             *
0103:             * @param bean      The bean validation is being performed on.
0104:             * @param va        The <code>ValidatorAction</code> that is currently
0105:             *                  being performed.
0106:             * @param field     The <code>Field</code> object associated with the
0107:             *                  current field being validated.
0108:             * @param errors    The <code>ActionMessages</code> object to add errors
0109:             *                  to if any validation errors occur.
0110:             * @param validator The <code>Validator</code> instance, used to access
0111:             *                  other field values.
0112:             * @param request   Current request object.
0113:             * @return true if meets stated requirements, false otherwise.
0114:             */
0115:            public static boolean validateRequiredIf(Object bean,
0116:                    ValidatorAction va, Field field, ActionMessages errors,
0117:                    Validator validator, HttpServletRequest request) {
0118:                Object form = validator
0119:                        .getParameterValue(org.apache.commons.validator.Validator.BEAN_PARAM);
0120:                String value = null;
0121:                boolean required = false;
0122:
0123:                value = evaluateBean(bean, field);
0124:
0125:                int i = 0;
0126:                String fieldJoin = "AND";
0127:
0128:                if (!GenericValidator.isBlankOrNull(field
0129:                        .getVarValue("fieldJoin"))) {
0130:                    fieldJoin = field.getVarValue("fieldJoin");
0131:                }
0132:
0133:                if (fieldJoin.equalsIgnoreCase("AND")) {
0134:                    required = true;
0135:                }
0136:
0137:                while (!GenericValidator.isBlankOrNull(field
0138:                        .getVarValue("field[" + i + "]"))) {
0139:                    String dependProp = field.getVarValue("field[" + i + "]");
0140:                    String dependTest = field.getVarValue("fieldTest[" + i
0141:                            + "]");
0142:                    String dependTestValue = field.getVarValue("fieldValue["
0143:                            + i + "]");
0144:                    String dependIndexed = field.getVarValue("fieldIndexed["
0145:                            + i + "]");
0146:
0147:                    if (dependIndexed == null) {
0148:                        dependIndexed = "false";
0149:                    }
0150:
0151:                    String dependVal = null;
0152:                    boolean this Required = false;
0153:
0154:                    if (field.isIndexed()
0155:                            && dependIndexed.equalsIgnoreCase("true")) {
0156:                        String key = field.getKey();
0157:
0158:                        if ((key.indexOf("[") > -1) && (key.indexOf("]") > -1)) {
0159:                            String ind = key.substring(0, key.indexOf(".") + 1);
0160:
0161:                            dependProp = ind + dependProp;
0162:                        }
0163:                    }
0164:
0165:                    dependVal = ValidatorUtils.getValueAsString(form,
0166:                            dependProp);
0167:
0168:                    if (dependTest.equals(FIELD_TEST_NULL)) {
0169:                        if ((dependVal != null) && (dependVal.length() > 0)) {
0170:                            this Required = false;
0171:                        } else {
0172:                            this Required = true;
0173:                        }
0174:                    }
0175:
0176:                    if (dependTest.equals(FIELD_TEST_NOTNULL)) {
0177:                        if ((dependVal != null) && (dependVal.length() > 0)) {
0178:                            this Required = true;
0179:                        } else {
0180:                            this Required = false;
0181:                        }
0182:                    }
0183:
0184:                    if (dependTest.equals(FIELD_TEST_EQUAL)) {
0185:                        this Required = dependTestValue
0186:                                .equalsIgnoreCase(dependVal);
0187:                    }
0188:
0189:                    if (fieldJoin.equalsIgnoreCase("AND")) {
0190:                        required = required && this Required;
0191:                    } else {
0192:                        required = required || this Required;
0193:                    }
0194:
0195:                    i++;
0196:                }
0197:
0198:                if (required) {
0199:                    if (GenericValidator.isBlankOrNull(value)) {
0200:                        errors.add(field.getKey(), Resources.getActionMessage(
0201:                                validator, request, va, field));
0202:
0203:                        return false;
0204:                    } else {
0205:                        return true;
0206:                    }
0207:                }
0208:
0209:                return true;
0210:            }
0211:
0212:            /**
0213:             * Checks if the field matches the regular expression in the field's mask
0214:             * attribute.
0215:             *
0216:             * @param bean      The bean validation is being performed on.
0217:             * @param va        The <code>ValidatorAction</code> that is currently
0218:             *                  being performed.
0219:             * @param field     The <code>Field</code> object associated with the
0220:             *                  current field being validated.
0221:             * @param errors    The <code>ActionMessages</code> object to add errors
0222:             *                  to if any validation errors occur.
0223:             * @param validator The <code>Validator</code> instance, used to access
0224:             *                  other field values.
0225:             * @param request   Current request object.
0226:             * @return true if field matches mask, false otherwise.
0227:             */
0228:            public static boolean validateMask(Object bean, ValidatorAction va,
0229:                    Field field, ActionMessages errors, Validator validator,
0230:                    HttpServletRequest request) {
0231:                String value = null;
0232:
0233:                value = evaluateBean(bean, field);
0234:
0235:                try {
0236:                    String mask = Resources.getVarValue("mask", field,
0237:                            validator, request, true);
0238:
0239:                    if (value != null && value.length() > 0
0240:                            && !GenericValidator.matchRegexp(value, mask)) {
0241:                        errors.add(field.getKey(), Resources.getActionMessage(
0242:                                validator, request, va, field));
0243:
0244:                        return false;
0245:                    } else {
0246:                        return true;
0247:                    }
0248:                } catch (Exception e) {
0249:                    processFailure(errors, field, "mask", e);
0250:
0251:                    return false;
0252:                }
0253:            }
0254:
0255:            /**
0256:             * Checks if the field can safely be converted to a byte primitive.
0257:             *
0258:             * @param bean      The bean validation is being performed on.
0259:             * @param va        The <code>ValidatorAction</code> that is currently
0260:             *                  being performed.
0261:             * @param field     The <code>Field</code> object associated with the
0262:             *                  current field being validated.
0263:             * @param errors    The <code>ActionMessages</code> object to add errors
0264:             *                  to if any validation errors occur.
0265:             * @param validator The <code>Validator</code> instance, used to access
0266:             *                  other field values.
0267:             * @param request   Current request object.
0268:             * @return true if valid, false otherwise.
0269:             */
0270:            public static Object validateByte(Object bean, ValidatorAction va,
0271:                    Field field, ActionMessages errors, Validator validator,
0272:                    HttpServletRequest request) {
0273:                Object result = null;
0274:                String value = null;
0275:
0276:                value = evaluateBean(bean, field);
0277:
0278:                if (GenericValidator.isBlankOrNull(value)) {
0279:                    return Boolean.TRUE;
0280:                }
0281:
0282:                result = GenericTypeValidator.formatByte(value);
0283:
0284:                if (result == null) {
0285:                    errors.add(field.getKey(), Resources.getActionMessage(
0286:                            validator, request, va, field));
0287:                }
0288:
0289:                return (result == null) ? Boolean.FALSE : result;
0290:            }
0291:
0292:            /**
0293:             * Checks if the field can safely be converted to a byte primitive.
0294:             *
0295:             * @param bean      The bean validation is being performed on.
0296:             * @param va        The <code>ValidatorAction</code> that is currently
0297:             *                  being performed.
0298:             * @param field     The <code>Field</code> object associated with the
0299:             *                  current field being validated.
0300:             * @param errors    The <code>ActionMessages</code> object to add errors
0301:             *                  to if any validation errors occur.
0302:             * @param validator The <code>Validator</code> instance, used to access
0303:             *                  other field values.
0304:             * @param request   Current request object.
0305:             * @return true if valid, false otherwise.
0306:             */
0307:            public static Object validateByteLocale(Object bean,
0308:                    ValidatorAction va, Field field, ActionMessages errors,
0309:                    Validator validator, HttpServletRequest request) {
0310:                Object result = null;
0311:                String value = null;
0312:
0313:                value = evaluateBean(bean, field);
0314:
0315:                if (GenericValidator.isBlankOrNull(value)) {
0316:                    return Boolean.TRUE;
0317:                }
0318:
0319:                Locale locale = RequestUtils.getUserLocale(request, null);
0320:
0321:                result = GenericTypeValidator.formatByte(value, locale);
0322:
0323:                if (result == null) {
0324:                    errors.add(field.getKey(), Resources.getActionMessage(
0325:                            validator, request, va, field));
0326:                }
0327:
0328:                return (result == null) ? Boolean.FALSE : result;
0329:            }
0330:
0331:            /**
0332:             * @param bean
0333:             * @param field
0334:             * @return
0335:             */
0336:            private static String evaluateBean(Object bean, Field field) {
0337:                String value;
0338:
0339:                if (isString(bean)) {
0340:                    value = (String) bean;
0341:                } else {
0342:                    value = ValidatorUtils.getValueAsString(bean, field
0343:                            .getProperty());
0344:                }
0345:
0346:                return value;
0347:            }
0348:
0349:            /**
0350:             * Checks if the field can safely be converted to a short primitive.
0351:             *
0352:             * @param bean      The bean validation is being performed on.
0353:             * @param va        The <code>ValidatorAction</code> that is currently
0354:             *                  being performed.
0355:             * @param field     The <code>Field</code> object associated with the
0356:             *                  current field being validated.
0357:             * @param errors    The <code>ActionMessages</code> object to add errors
0358:             *                  to if any validation errors occur.
0359:             * @param validator The <code>Validator</code> instance, used to access
0360:             *                  other field values.
0361:             * @param request   Current request object.
0362:             * @return true if valid, false otherwise.
0363:             */
0364:            public static Object validateShort(Object bean, ValidatorAction va,
0365:                    Field field, ActionMessages errors, Validator validator,
0366:                    HttpServletRequest request) {
0367:                Object result = null;
0368:                String value = null;
0369:
0370:                value = evaluateBean(bean, field);
0371:
0372:                if (GenericValidator.isBlankOrNull(value)) {
0373:                    return Boolean.TRUE;
0374:                }
0375:
0376:                result = GenericTypeValidator.formatShort(value);
0377:
0378:                if (result == null) {
0379:                    errors.add(field.getKey(), Resources.getActionMessage(
0380:                            validator, request, va, field));
0381:                }
0382:
0383:                return (result == null) ? Boolean.FALSE : result;
0384:            }
0385:
0386:            /**
0387:             * Checks if the field can safely be converted to a short primitive.
0388:             *
0389:             * @param bean      The bean validation is being performed on.
0390:             * @param va        The <code>ValidatorAction</code> that is currently
0391:             *                  being performed.
0392:             * @param field     The <code>Field</code> object associated with the
0393:             *                  current field being validated.
0394:             * @param errors    The <code>ActionMessages</code> object to add errors
0395:             *                  to if any validation errors occur.
0396:             * @param validator The <code>Validator</code> instance, used to access
0397:             *                  other field values.
0398:             * @param request   Current request object.
0399:             * @return true if valid, false otherwise.
0400:             */
0401:            public static Object validateShortLocale(Object bean,
0402:                    ValidatorAction va, Field field, ActionMessages errors,
0403:                    Validator validator, HttpServletRequest request) {
0404:                Object result = null;
0405:                String value = null;
0406:
0407:                value = evaluateBean(bean, field);
0408:
0409:                if (GenericValidator.isBlankOrNull(value)) {
0410:                    return Boolean.TRUE;
0411:                }
0412:
0413:                Locale locale = RequestUtils.getUserLocale(request, null);
0414:
0415:                result = GenericTypeValidator.formatShort(value, locale);
0416:
0417:                if (result == null) {
0418:                    errors.add(field.getKey(), Resources.getActionMessage(
0419:                            validator, request, va, field));
0420:                }
0421:
0422:                return (result == null) ? Boolean.FALSE : result;
0423:            }
0424:
0425:            /**
0426:             * Checks if the field can safely be converted to an int primitive.
0427:             *
0428:             * @param bean      The bean validation is being performed on.
0429:             * @param va        The <code>ValidatorAction</code> that is currently
0430:             *                  being performed.
0431:             * @param field     The <code>Field</code> object associated with the
0432:             *                  current field being validated.
0433:             * @param errors    The <code>ActionMessages</code> object to add errors
0434:             *                  to if any validation errors occur.
0435:             * @param validator The <code>Validator</code> instance, used to access
0436:             *                  other field values.
0437:             * @param request   Current request object.
0438:             * @return true if valid, false otherwise.
0439:             */
0440:            public static Object validateInteger(Object bean,
0441:                    ValidatorAction va, Field field, ActionMessages errors,
0442:                    Validator validator, HttpServletRequest request) {
0443:                Object result = null;
0444:                String value = null;
0445:
0446:                value = evaluateBean(bean, field);
0447:
0448:                if (GenericValidator.isBlankOrNull(value)) {
0449:                    return Boolean.TRUE;
0450:                }
0451:
0452:                result = GenericTypeValidator.formatInt(value);
0453:
0454:                if (result == null) {
0455:                    errors.add(field.getKey(), Resources.getActionMessage(
0456:                            validator, request, va, field));
0457:                }
0458:
0459:                return (result == null) ? Boolean.FALSE : result;
0460:            }
0461:
0462:            /**
0463:             * Checks if the field can safely be converted to an int primitive.
0464:             *
0465:             * @param bean      The bean validation is being performed on.
0466:             * @param va        The <code>ValidatorAction</code> that is currently
0467:             *                  being performed.
0468:             * @param field     The <code>Field</code> object associated with the
0469:             *                  current field being validated.
0470:             * @param errors    The <code>ActionMessages</code> object to add errors
0471:             *                  to if any validation errors occur.
0472:             * @param validator The <code>Validator</code> instance, used to access
0473:             *                  other field values.
0474:             * @param request   Current request object.
0475:             * @return true if valid, false otherwise.
0476:             */
0477:            public static Object validateIntegerLocale(Object bean,
0478:                    ValidatorAction va, Field field, ActionMessages errors,
0479:                    Validator validator, HttpServletRequest request) {
0480:                Object result = null;
0481:                String value = null;
0482:
0483:                value = evaluateBean(bean, field);
0484:
0485:                if (GenericValidator.isBlankOrNull(value)) {
0486:                    return Boolean.TRUE;
0487:                }
0488:
0489:                Locale locale = RequestUtils.getUserLocale(request, null);
0490:
0491:                result = GenericTypeValidator.formatInt(value, locale);
0492:
0493:                if (result == null) {
0494:                    errors.add(field.getKey(), Resources.getActionMessage(
0495:                            validator, request, va, field));
0496:                }
0497:
0498:                return (result == null) ? Boolean.FALSE : result;
0499:            }
0500:
0501:            /**
0502:             * Checks if the field can safely be converted to a long primitive.
0503:             *
0504:             * @param bean      The bean validation is being performed on.
0505:             * @param va        The <code>ValidatorAction</code> that is currently
0506:             *                  being performed.
0507:             * @param field     The <code>Field</code> object associated with the
0508:             *                  current field being validated.
0509:             * @param errors    The <code>ActionMessages</code> object to add errors
0510:             *                  to if any validation errors occur.
0511:             * @param validator The <code>Validator</code> instance, used to access
0512:             *                  other field values.
0513:             * @param request   Current request object.
0514:             * @return true if valid, false otherwise.
0515:             */
0516:            public static Object validateLong(Object bean, ValidatorAction va,
0517:                    Field field, ActionMessages errors, Validator validator,
0518:                    HttpServletRequest request) {
0519:                Object result = null;
0520:                String value = null;
0521:
0522:                value = evaluateBean(bean, field);
0523:
0524:                if (GenericValidator.isBlankOrNull(value)) {
0525:                    return Boolean.TRUE;
0526:                }
0527:
0528:                result = GenericTypeValidator.formatLong(value);
0529:
0530:                if (result == null) {
0531:                    errors.add(field.getKey(), Resources.getActionMessage(
0532:                            validator, request, va, field));
0533:                }
0534:
0535:                return (result == null) ? Boolean.FALSE : result;
0536:            }
0537:
0538:            /**
0539:             * Checks if the field can safely be converted to a long primitive.
0540:             *
0541:             * @param bean      The bean validation is being performed on.
0542:             * @param va        The <code>ValidatorAction</code> that is currently
0543:             *                  being performed.
0544:             * @param field     The <code>Field</code> object associated with the
0545:             *                  current field being validated.
0546:             * @param errors    The <code>ActionMessages</code> object to add errors
0547:             *                  to if any validation errors occur.
0548:             * @param validator The <code>Validator</code> instance, used to access
0549:             *                  other field values.
0550:             * @param request   Current request object.
0551:             * @return true if valid, false otherwise.
0552:             */
0553:            public static Object validateLongLocale(Object bean,
0554:                    ValidatorAction va, Field field, ActionMessages errors,
0555:                    Validator validator, HttpServletRequest request) {
0556:                Object result = null;
0557:                String value = null;
0558:
0559:                value = evaluateBean(bean, field);
0560:
0561:                if (GenericValidator.isBlankOrNull(value)) {
0562:                    return Boolean.TRUE;
0563:                }
0564:
0565:                Locale locale = RequestUtils.getUserLocale(request, null);
0566:
0567:                result = GenericTypeValidator.formatLong(value, locale);
0568:
0569:                if (result == null) {
0570:                    errors.add(field.getKey(), Resources.getActionMessage(
0571:                            validator, request, va, field));
0572:                }
0573:
0574:                return (result == null) ? Boolean.FALSE : result;
0575:            }
0576:
0577:            /**
0578:             * Checks if the field can safely be converted to a float primitive.
0579:             *
0580:             * @param bean      The bean validation is being performed on.
0581:             * @param va        The <code>ValidatorAction</code> that is currently
0582:             *                  being performed.
0583:             * @param field     The <code>Field</code> object associated with the
0584:             *                  current field being validated.
0585:             * @param errors    The <code>ActionMessages</code> object to add errors
0586:             *                  to if any validation errors occur.
0587:             * @param validator The <code>Validator</code> instance, used to access
0588:             *                  other field values.
0589:             * @param request   Current request object.
0590:             * @return true if valid, false otherwise.
0591:             */
0592:            public static Object validateFloat(Object bean, ValidatorAction va,
0593:                    Field field, ActionMessages errors, Validator validator,
0594:                    HttpServletRequest request) {
0595:                Object result = null;
0596:                String value = null;
0597:
0598:                value = evaluateBean(bean, field);
0599:
0600:                if (GenericValidator.isBlankOrNull(value)) {
0601:                    return Boolean.TRUE;
0602:                }
0603:
0604:                result = GenericTypeValidator.formatFloat(value);
0605:
0606:                if (result == null) {
0607:                    errors.add(field.getKey(), Resources.getActionMessage(
0608:                            validator, request, va, field));
0609:                }
0610:
0611:                return (result == null) ? Boolean.FALSE : result;
0612:            }
0613:
0614:            /**
0615:             * Checks if the field can safely be converted to a float primitive.
0616:             *
0617:             * @param bean      The bean validation is being performed on.
0618:             * @param va        The <code>ValidatorAction</code> that is currently
0619:             *                  being performed.
0620:             * @param field     The <code>Field</code> object associated with the
0621:             *                  current field being validated.
0622:             * @param errors    The <code>ActionMessages</code> object to add errors
0623:             *                  to if any validation errors occur.
0624:             * @param validator The <code>Validator</code> instance, used to access
0625:             *                  other field values.
0626:             * @param request   Current request object.
0627:             * @return true if valid, false otherwise.
0628:             */
0629:            public static Object validateFloatLocale(Object bean,
0630:                    ValidatorAction va, Field field, ActionMessages errors,
0631:                    Validator validator, HttpServletRequest request) {
0632:                Object result = null;
0633:                String value = null;
0634:
0635:                value = evaluateBean(bean, field);
0636:
0637:                if (GenericValidator.isBlankOrNull(value)) {
0638:                    return Boolean.TRUE;
0639:                }
0640:
0641:                Locale locale = RequestUtils.getUserLocale(request, null);
0642:
0643:                result = GenericTypeValidator.formatFloat(value, locale);
0644:
0645:                if (result == null) {
0646:                    errors.add(field.getKey(), Resources.getActionMessage(
0647:                            validator, request, va, field));
0648:                }
0649:
0650:                return (result == null) ? Boolean.FALSE : result;
0651:            }
0652:
0653:            /**
0654:             * Checks if the field can safely be converted to a double primitive.
0655:             *
0656:             * @param bean      The bean validation is being performed on.
0657:             * @param va        The <code>ValidatorAction</code> that is currently
0658:             *                  being performed.
0659:             * @param field     The <code>Field</code> object associated with the
0660:             *                  current field being validated.
0661:             * @param errors    The <code>ActionMessages</code> object to add errors
0662:             *                  to if any validation errors occur.
0663:             * @param validator The <code>Validator</code> instance, used to access
0664:             *                  other field values.
0665:             * @param request   Current request object.
0666:             * @return true if valid, false otherwise.
0667:             */
0668:            public static Object validateDouble(Object bean,
0669:                    ValidatorAction va, Field field, ActionMessages errors,
0670:                    Validator validator, HttpServletRequest request) {
0671:                Object result = null;
0672:                String value = null;
0673:
0674:                value = evaluateBean(bean, field);
0675:
0676:                if (GenericValidator.isBlankOrNull(value)) {
0677:                    return Boolean.TRUE;
0678:                }
0679:
0680:                result = GenericTypeValidator.formatDouble(value);
0681:
0682:                if (result == null) {
0683:                    errors.add(field.getKey(), Resources.getActionMessage(
0684:                            validator, request, va, field));
0685:                }
0686:
0687:                return (result == null) ? Boolean.FALSE : result;
0688:            }
0689:
0690:            /**
0691:             * Checks if the field can safely be converted to a double primitive.
0692:             *
0693:             * @param bean      The bean validation is being performed on.
0694:             * @param va        The <code>ValidatorAction</code> that is currently
0695:             *                  being performed.
0696:             * @param field     The <code>Field</code> object associated with the
0697:             *                  current field being validated.
0698:             * @param errors    The <code>ActionMessages</code> object to add errors
0699:             *                  to if any validation errors occur.
0700:             * @param validator The <code>Validator</code> instance, used to access
0701:             *                  other field values.
0702:             * @param request   Current request object.
0703:             * @return true if valid, false otherwise.
0704:             */
0705:            public static Object validateDoubleLocale(Object bean,
0706:                    ValidatorAction va, Field field, ActionMessages errors,
0707:                    Validator validator, HttpServletRequest request) {
0708:                Object result = null;
0709:                String value = null;
0710:
0711:                value = evaluateBean(bean, field);
0712:
0713:                if (GenericValidator.isBlankOrNull(value)) {
0714:                    return Boolean.TRUE;
0715:                }
0716:
0717:                Locale locale = RequestUtils.getUserLocale(request, null);
0718:
0719:                result = GenericTypeValidator.formatDouble(value, locale);
0720:
0721:                if (result == null) {
0722:                    errors.add(field.getKey(), Resources.getActionMessage(
0723:                            validator, request, va, field));
0724:                }
0725:
0726:                return (result == null) ? Boolean.FALSE : result;
0727:            }
0728:
0729:            /**
0730:             * Checks if the field is a valid date. If the field has a datePattern
0731:             * variable, that will be used to format <code>java.text.SimpleDateFormat</code>.
0732:             * If the field has a datePatternStrict variable, that will be used to
0733:             * format <code>java.text.SimpleDateFormat</code> and the length will be
0734:             * checked so '2/12/1999' will not pass validation with the format
0735:             * 'MM/dd/yyyy' because the month isn't two digits. If no datePattern
0736:             * variable is specified, then the field gets the DateFormat.SHORT format
0737:             * for the locale. The setLenient method is set to <code>false</code> for
0738:             * all variations.
0739:             *
0740:             * @param bean      The bean validation is being performed on.
0741:             * @param va        The <code>ValidatorAction</code> that is currently
0742:             *                  being performed.
0743:             * @param field     The <code>Field</code> object associated with the
0744:             *                  current field being validated.
0745:             * @param errors    The <code>ActionMessages</code> object to add errors
0746:             *                  to if any validation errors occur.
0747:             * @param validator The <code>Validator</code> instance, used to access
0748:             *                  other field values.
0749:             * @param request   Current request object.
0750:             * @return true if valid, false otherwise.
0751:             */
0752:            public static Object validateDate(Object bean, ValidatorAction va,
0753:                    Field field, ActionMessages errors, Validator validator,
0754:                    HttpServletRequest request) {
0755:                Object result = null;
0756:                String value = null;
0757:
0758:                value = evaluateBean(bean, field);
0759:
0760:                boolean isStrict = false;
0761:                String datePattern = Resources.getVarValue("datePattern",
0762:                        field, validator, request, false);
0763:
0764:                if (GenericValidator.isBlankOrNull(datePattern)) {
0765:                    datePattern = Resources.getVarValue("datePatternStrict",
0766:                            field, validator, request, false);
0767:
0768:                    if (!GenericValidator.isBlankOrNull(datePattern)) {
0769:                        isStrict = true;
0770:                    }
0771:                }
0772:
0773:                Locale locale = RequestUtils.getUserLocale(request, null);
0774:
0775:                if (GenericValidator.isBlankOrNull(value)) {
0776:                    return Boolean.TRUE;
0777:                }
0778:
0779:                try {
0780:                    if (GenericValidator.isBlankOrNull(datePattern)) {
0781:                        result = GenericTypeValidator.formatDate(value, locale);
0782:                    } else {
0783:                        result = GenericTypeValidator.formatDate(value,
0784:                                datePattern, isStrict);
0785:                    }
0786:                } catch (Exception e) {
0787:                    log.error(e.getMessage(), e);
0788:                }
0789:
0790:                if (result == null) {
0791:                    errors.add(field.getKey(), Resources.getActionMessage(
0792:                            validator, request, va, field));
0793:                }
0794:
0795:                return (result == null) ? Boolean.FALSE : result;
0796:            }
0797:
0798:            /**
0799:             * Checks if a fields value is within a range (min &amp; max specified in
0800:             * the vars attribute).
0801:             *
0802:             * @param bean      The bean validation is being performed on.
0803:             * @param va        The <code>ValidatorAction</code> that is currently
0804:             *                  being performed.
0805:             * @param field     The <code>Field</code> object associated with the
0806:             *                  current field being validated.
0807:             * @param errors    The <code>ActionMessages</code> object to add errors
0808:             *                  to if any validation errors occur.
0809:             * @param validator The <code>Validator</code> instance, used to access
0810:             *                  other field values.
0811:             * @param request   Current request object.
0812:             * @return True if in range, false otherwise.
0813:             */
0814:            public static boolean validateLongRange(Object bean,
0815:                    ValidatorAction va, Field field, ActionMessages errors,
0816:                    Validator validator, HttpServletRequest request) {
0817:                String value = null;
0818:
0819:                value = evaluateBean(bean, field);
0820:
0821:                if (!GenericValidator.isBlankOrNull(value)) {
0822:                    try {
0823:                        String minVar = Resources.getVarValue("min", field,
0824:                                validator, request, true);
0825:                        String maxVar = Resources.getVarValue("max", field,
0826:                                validator, request, true);
0827:                        long longValue = Long.parseLong(value);
0828:                        long min = Long.parseLong(minVar);
0829:                        long max = Long.parseLong(maxVar);
0830:
0831:                        if (min > max) {
0832:                            throw new IllegalArgumentException(
0833:                                    sysmsgs.getMessage("invalid.range", minVar,
0834:                                            maxVar));
0835:                        }
0836:
0837:                        if (!GenericValidator.isInRange(longValue, min, max)) {
0838:                            errors.add(field.getKey(), Resources
0839:                                    .getActionMessage(validator, request, va,
0840:                                            field));
0841:
0842:                            return false;
0843:                        }
0844:                    } catch (Exception e) {
0845:                        processFailure(errors, field, "longRange", e);
0846:
0847:                        return false;
0848:                    }
0849:                }
0850:
0851:                return true;
0852:            }
0853:
0854:            /**
0855:             * Checks if a fields value is within a range (min &amp; max specified in
0856:             * the vars attribute).
0857:             *
0858:             * @param bean      The bean validation is being performed on.
0859:             * @param va        The <code>ValidatorAction</code> that is currently
0860:             *                  being performed.
0861:             * @param field     The <code>Field</code> object associated with the
0862:             *                  current field being validated.
0863:             * @param errors    The <code>ActionMessages</code> object to add errors
0864:             *                  to if any validation errors occur.
0865:             * @param validator The <code>Validator</code> instance, used to access
0866:             *                  other field values.
0867:             * @param request   Current request object.
0868:             * @return True if in range, false otherwise.
0869:             */
0870:            public static boolean validateIntRange(Object bean,
0871:                    ValidatorAction va, Field field, ActionMessages errors,
0872:                    Validator validator, HttpServletRequest request) {
0873:                String value = null;
0874:
0875:                value = evaluateBean(bean, field);
0876:
0877:                if (!GenericValidator.isBlankOrNull(value)) {
0878:                    try {
0879:                        String minVar = Resources.getVarValue("min", field,
0880:                                validator, request, true);
0881:                        String maxVar = Resources.getVarValue("max", field,
0882:                                validator, request, true);
0883:                        int min = Integer.parseInt(minVar);
0884:                        int max = Integer.parseInt(maxVar);
0885:                        int intValue = Integer.parseInt(value);
0886:
0887:                        if (min > max) {
0888:                            throw new IllegalArgumentException(
0889:                                    sysmsgs.getMessage("invalid.range", minVar,
0890:                                            maxVar));
0891:                        }
0892:
0893:                        if (!GenericValidator.isInRange(intValue, min, max)) {
0894:                            errors.add(field.getKey(), Resources
0895:                                    .getActionMessage(validator, request, va,
0896:                                            field));
0897:
0898:                            return false;
0899:                        }
0900:                    } catch (Exception e) {
0901:                        processFailure(errors, field, "intRange", e);
0902:
0903:                        return false;
0904:                    }
0905:                }
0906:
0907:                return true;
0908:            }
0909:
0910:            /**
0911:             * Checks if a fields value is within a range (min &amp; max specified in
0912:             * the vars attribute).
0913:             *
0914:             * @param bean      The bean validation is being performed on.
0915:             * @param va        The <code>ValidatorAction</code> that is currently
0916:             *                  being performed.
0917:             * @param field     The <code>Field</code> object associated with the
0918:             *                  current field being validated.
0919:             * @param errors    The <code>ActionMessages</code> object to add errors
0920:             *                  to if any validation errors occur.
0921:             * @param validator The <code>Validator</code> instance, used to access
0922:             *                  other field values.
0923:             * @param request   Current request object.
0924:             * @return True if in range, false otherwise.
0925:             */
0926:            public static boolean validateDoubleRange(Object bean,
0927:                    ValidatorAction va, Field field, ActionMessages errors,
0928:                    Validator validator, HttpServletRequest request) {
0929:                String value = null;
0930:
0931:                value = evaluateBean(bean, field);
0932:
0933:                if (!GenericValidator.isBlankOrNull(value)) {
0934:                    try {
0935:                        String minVar = Resources.getVarValue("min", field,
0936:                                validator, request, true);
0937:                        String maxVar = Resources.getVarValue("max", field,
0938:                                validator, request, true);
0939:                        double doubleValue = Double.parseDouble(value);
0940:                        double min = Double.parseDouble(minVar);
0941:                        double max = Double.parseDouble(maxVar);
0942:
0943:                        if (min > max) {
0944:                            throw new IllegalArgumentException(
0945:                                    sysmsgs.getMessage("invalid.range", minVar,
0946:                                            maxVar));
0947:                        }
0948:
0949:                        if (!GenericValidator.isInRange(doubleValue, min, max)) {
0950:                            errors.add(field.getKey(), Resources
0951:                                    .getActionMessage(validator, request, va,
0952:                                            field));
0953:
0954:                            return false;
0955:                        }
0956:                    } catch (Exception e) {
0957:                        processFailure(errors, field, "doubleRange", e);
0958:
0959:                        return false;
0960:                    }
0961:                }
0962:
0963:                return true;
0964:            }
0965:
0966:            /**
0967:             * Checks if a fields value is within a range (min &amp; max specified in
0968:             * the vars attribute).
0969:             *
0970:             * @param bean      The bean validation is being performed on.
0971:             * @param va        The <code>ValidatorAction</code> that is currently
0972:             *                  being performed.
0973:             * @param field     The <code>Field</code> object associated with the
0974:             *                  current field being validated.
0975:             * @param errors    The <code>ActionMessages</code> object to add errors
0976:             *                  to if any validation errors occur.
0977:             * @param validator The <code>Validator</code> instance, used to access
0978:             *                  other field values.
0979:             * @param request   Current request object.
0980:             * @return True if in range, false otherwise.
0981:             */
0982:            public static boolean validateFloatRange(Object bean,
0983:                    ValidatorAction va, Field field, ActionMessages errors,
0984:                    Validator validator, HttpServletRequest request) {
0985:                String value = null;
0986:
0987:                value = evaluateBean(bean, field);
0988:
0989:                if (!GenericValidator.isBlankOrNull(value)) {
0990:                    try {
0991:                        String minVar = Resources.getVarValue("min", field,
0992:                                validator, request, true);
0993:                        String maxVar = Resources.getVarValue("max", field,
0994:                                validator, request, true);
0995:                        float floatValue = Float.parseFloat(value);
0996:                        float min = Float.parseFloat(minVar);
0997:                        float max = Float.parseFloat(maxVar);
0998:
0999:                        if (min > max) {
1000:                            throw new IllegalArgumentException(
1001:                                    sysmsgs.getMessage("invalid.range", minVar,
1002:                                            maxVar));
1003:                        }
1004:
1005:                        if (!GenericValidator.isInRange(floatValue, min, max)) {
1006:                            errors.add(field.getKey(), Resources
1007:                                    .getActionMessage(validator, request, va,
1008:                                            field));
1009:
1010:                            return false;
1011:                        }
1012:                    } catch (Exception e) {
1013:                        processFailure(errors, field, "floatRange", e);
1014:
1015:                        return false;
1016:                    }
1017:                }
1018:
1019:                return true;
1020:            }
1021:
1022:            /**
1023:             * Checks if the field is a valid credit card number.
1024:             *
1025:             * @param bean      The bean validation is being performed on.
1026:             * @param va        The <code>ValidatorAction</code> that is currently
1027:             *                  being performed.
1028:             * @param field     The <code>Field</code> object associated with the
1029:             *                  current field being validated.
1030:             * @param errors    The <code>ActionMessages</code> object to add errors
1031:             *                  to if any validation errors occur.
1032:             * @param validator The <code>Validator</code> instance, used to access
1033:             *                  other field values.
1034:             * @param request   Current request object.
1035:             * @return true if valid, false otherwise.
1036:             */
1037:            public static Object validateCreditCard(Object bean,
1038:                    ValidatorAction va, Field field, ActionMessages errors,
1039:                    Validator validator, HttpServletRequest request) {
1040:                Object result = null;
1041:                String value = null;
1042:
1043:                value = evaluateBean(bean, field);
1044:
1045:                if (GenericValidator.isBlankOrNull(value)) {
1046:                    return Boolean.TRUE;
1047:                }
1048:
1049:                result = GenericTypeValidator.formatCreditCard(value);
1050:
1051:                if (result == null) {
1052:                    errors.add(field.getKey(), Resources.getActionMessage(
1053:                            validator, request, va, field));
1054:                }
1055:
1056:                return (result == null) ? Boolean.FALSE : result;
1057:            }
1058:
1059:            /**
1060:             * Checks if a field has a valid e-mail address.
1061:             *
1062:             * @param bean      The bean validation is being performed on.
1063:             * @param va        The <code>ValidatorAction</code> that is currently
1064:             *                  being performed.
1065:             * @param field     The <code>Field</code> object associated with the
1066:             *                  current field being validated.
1067:             * @param errors    The <code>ActionMessages</code> object to add errors
1068:             *                  to if any validation errors occur.
1069:             * @param validator The <code>Validator</code> instance, used to access
1070:             *                  other field values.
1071:             * @param request   Current request object.
1072:             * @return True if valid, false otherwise.
1073:             */
1074:            public static boolean validateEmail(Object bean,
1075:                    ValidatorAction va, Field field, ActionMessages errors,
1076:                    Validator validator, HttpServletRequest request) {
1077:                String value = null;
1078:
1079:                value = evaluateBean(bean, field);
1080:
1081:                if (!GenericValidator.isBlankOrNull(value)
1082:                        && !GenericValidator.isEmail(value)) {
1083:                    errors.add(field.getKey(), Resources.getActionMessage(
1084:                            validator, request, va, field));
1085:
1086:                    return false;
1087:                } else {
1088:                    return true;
1089:                }
1090:            }
1091:
1092:            /**
1093:             * Checks if the field's length is less than or equal to the maximum
1094:             * value. A <code>Null</code> will be considered an error.
1095:             *
1096:             * @param bean      The bean validation is being performed on.
1097:             * @param va        The <code>ValidatorAction</code> that is currently
1098:             *                  being performed.
1099:             * @param field     The <code>Field</code> object associated with the
1100:             *                  current field being validated.
1101:             * @param errors    The <code>ActionMessages</code> object to add errors
1102:             *                  to if any validation errors occur.
1103:             * @param validator The <code>Validator</code> instance, used to access
1104:             *                  other field values.
1105:             * @param request   Current request object.
1106:             * @return True if stated conditions met.
1107:             */
1108:            public static boolean validateMaxLength(Object bean,
1109:                    ValidatorAction va, Field field, ActionMessages errors,
1110:                    Validator validator, HttpServletRequest request) {
1111:                String value = null;
1112:
1113:                value = evaluateBean(bean, field);
1114:
1115:                if (value != null) {
1116:                    try {
1117:                        String maxVar = Resources.getVarValue("maxlength",
1118:                                field, validator, request, true);
1119:                        int max = Integer.parseInt(maxVar);
1120:
1121:                        boolean isValid = false;
1122:                        String endLth = Resources.getVarValue("lineEndLength",
1123:                                field, validator, request, false);
1124:                        if (GenericValidator.isBlankOrNull(endLth)) {
1125:                            isValid = GenericValidator.maxLength(value, max);
1126:                        } else {
1127:                            isValid = GenericValidator.maxLength(value, max,
1128:                                    Integer.parseInt(endLth));
1129:                        }
1130:
1131:                        if (!isValid) {
1132:                            errors.add(field.getKey(), Resources
1133:                                    .getActionMessage(validator, request, va,
1134:                                            field));
1135:
1136:                            return false;
1137:                        }
1138:                    } catch (Exception e) {
1139:                        processFailure(errors, field, "maxlength", e);
1140:
1141:                        return false;
1142:                    }
1143:                }
1144:
1145:                return true;
1146:            }
1147:
1148:            /**
1149:             * Checks if the field's length is greater than or equal to the minimum
1150:             * value. A <code>Null</code> will be considered an error.
1151:             *
1152:             * @param bean      The bean validation is being performed on.
1153:             * @param va        The <code>ValidatorAction</code> that is currently
1154:             *                  being performed.
1155:             * @param field     The <code>Field</code> object associated with the
1156:             *                  current field being validated.
1157:             * @param errors    The <code>ActionMessages</code> object to add errors
1158:             *                  to if any validation errors occur.
1159:             * @param validator The <code>Validator</code> instance, used to access
1160:             *                  other field values.
1161:             * @param request   Current request object.
1162:             * @return True if stated conditions met.
1163:             */
1164:            public static boolean validateMinLength(Object bean,
1165:                    ValidatorAction va, Field field, ActionMessages errors,
1166:                    Validator validator, HttpServletRequest request) {
1167:                String value = null;
1168:
1169:                value = evaluateBean(bean, field);
1170:
1171:                if (!GenericValidator.isBlankOrNull(value)) {
1172:                    try {
1173:                        String minVar = Resources.getVarValue("minlength",
1174:                                field, validator, request, true);
1175:                        int min = Integer.parseInt(minVar);
1176:
1177:                        boolean isValid = false;
1178:                        String endLth = Resources.getVarValue("lineEndLength",
1179:                                field, validator, request, false);
1180:                        if (GenericValidator.isBlankOrNull(endLth)) {
1181:                            isValid = GenericValidator.minLength(value, min);
1182:                        } else {
1183:                            isValid = GenericValidator.minLength(value, min,
1184:                                    Integer.parseInt(endLth));
1185:                        }
1186:
1187:                        if (!isValid) {
1188:                            errors.add(field.getKey(), Resources
1189:                                    .getActionMessage(validator, request, va,
1190:                                            field));
1191:
1192:                            return false;
1193:                        }
1194:                    } catch (Exception e) {
1195:                        processFailure(errors, field, "minlength", e);
1196:
1197:                        return false;
1198:                    }
1199:                }
1200:
1201:                return true;
1202:            }
1203:
1204:            /**
1205:             * Checks if a field has a valid url. Four optional variables can be
1206:             * specified to configure url validation.
1207:             *
1208:             * <ul>
1209:             *
1210:             * <li>Variable <code>allow2slashes</code> can be set to <code>true</code>
1211:             * or <code>false</code> to control whether two slashes are allowed -
1212:             * default is <code>false</code> (i.e. two slashes are NOT allowed).</li>
1213:             *
1214:             * <li>Variable <code>nofragments</code> can be set to <code>true</code>
1215:             * or <code>false</code> to control whether fragments are allowed -
1216:             * default is <code>false</code> (i.e. fragments ARE allowed).</li>
1217:             *
1218:             * <li>Variable <code>allowallschemes</code> can be set to
1219:             * <code>true</code> or <code>false</code> to control if all schemes are
1220:             * allowed - default is <code>false</code> (i.e. all schemes are NOT
1221:             * allowed).</li>
1222:             *
1223:             * <li>Variable <code>schemes</code> can be set to a comma delimited list
1224:             * of valid schemes. This value is ignored if <code>allowallschemes</code>
1225:             * is set to <code>true</code>. Default schemes allowed are "http",
1226:             * "https" and "ftp" if this variable is not specified.</li>
1227:             *
1228:             * </ul>
1229:             *
1230:             * @param bean      The bean validation is being performed on.
1231:             * @param va        The <code>ValidatorAction</code> that is currently
1232:             *                  being performed.
1233:             * @param field     The <code>Field</code> object associated with the
1234:             *                  current field being validated.
1235:             * @param errors    The <code>ActionMessages</code> object to add errors
1236:             *                  to if any validation errors occur.
1237:             * @param validator The <code>Validator</code> instance, used to access
1238:             *                  other field values.
1239:             * @param request   Current request object.
1240:             * @return True if valid, false otherwise.
1241:             */
1242:            public static boolean validateUrl(Object bean, ValidatorAction va,
1243:                    Field field, ActionMessages errors, Validator validator,
1244:                    HttpServletRequest request) {
1245:                String value = null;
1246:
1247:                value = evaluateBean(bean, field);
1248:
1249:                if (GenericValidator.isBlankOrNull(value)) {
1250:                    return true;
1251:                }
1252:
1253:                // Get the options and schemes Vars
1254:                String allowallschemesVar = Resources.getVarValue(
1255:                        "allowallschemes", field, validator, request, false);
1256:                boolean allowallschemes = "true"
1257:                        .equalsIgnoreCase(allowallschemesVar);
1258:                int options = allowallschemes ? UrlValidator.ALLOW_ALL_SCHEMES
1259:                        : 0;
1260:
1261:                String allow2slashesVar = Resources.getVarValue(
1262:                        "allow2slashes", field, validator, request, false);
1263:
1264:                if ("true".equalsIgnoreCase(allow2slashesVar)) {
1265:                    options += UrlValidator.ALLOW_2_SLASHES;
1266:                }
1267:
1268:                String nofragmentsVar = Resources.getVarValue("nofragments",
1269:                        field, validator, request, false);
1270:
1271:                if ("true".equalsIgnoreCase(nofragmentsVar)) {
1272:                    options += UrlValidator.NO_FRAGMENTS;
1273:                }
1274:
1275:                String schemesVar = allowallschemes ? null : Resources
1276:                        .getVarValue("schemes", field, validator, request,
1277:                                false);
1278:
1279:                // No options or schemes - use GenericValidator as default
1280:                if ((options == 0) && (schemesVar == null)) {
1281:                    if (GenericValidator.isUrl(value)) {
1282:                        return true;
1283:                    } else {
1284:                        errors.add(field.getKey(), Resources.getActionMessage(
1285:                                validator, request, va, field));
1286:
1287:                        return false;
1288:                    }
1289:                }
1290:
1291:                // Parse comma delimited list of schemes into a String[]
1292:                String[] schemes = null;
1293:
1294:                if (schemesVar != null) {
1295:                    StringTokenizer st = new StringTokenizer(schemesVar, ",");
1296:
1297:                    schemes = new String[st.countTokens()];
1298:
1299:                    int i = 0;
1300:
1301:                    while (st.hasMoreTokens()) {
1302:                        schemes[i++] = st.nextToken().trim();
1303:                    }
1304:                }
1305:
1306:                // Create UrlValidator and validate with options/schemes
1307:                UrlValidator urlValidator = new UrlValidator(schemes, options);
1308:
1309:                if (urlValidator.isValid(value)) {
1310:                    return true;
1311:                } else {
1312:                    errors.add(field.getKey(), Resources.getActionMessage(
1313:                            validator, request, va, field));
1314:
1315:                    return false;
1316:                }
1317:            }
1318:
1319:            /**
1320:             * Process a validation failure.
1321:             */
1322:            private static void processFailure(ActionMessages errors,
1323:                    Field field, String validator, Throwable t) {
1324:                // Log the error
1325:                String logErrorMsg = sysmsgs.getMessage("validation.failed",
1326:                        validator, field.getProperty(), t.toString());
1327:
1328:                log.error(logErrorMsg);
1329:
1330:                // Add general "system error" message to show to the user
1331:                String userErrorMsg = sysmsgs.getMessage("system.error");
1332:
1333:                errors.add(field.getKey(), new ActionMessage(userErrorMsg,
1334:                        false));
1335:            }
1336:
1337:            /**
1338:             * Return <code>true</code> if the specified object is a String or a
1339:             * <code>null</code> value.
1340:             *
1341:             * @param o Object to be tested
1342:             * @return The string value
1343:             */
1344:            protected static boolean isString(Object o) {
1345:                return (o == null) ? true : String.class.isInstance(o);
1346:            }
1347:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.