Source Code Cross Referenced for ValidWhen.java in  » Web-Framework » struts-1.3.8 » org » apache » struts » validator » validwhen » 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.validwhen 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: ValidWhen.java 471754 2006-11-06 14:55:09Z husted $
003:         *
004:         * Licensed to the Apache Software Foundation (ASF) under one
005:         * or more contributor license agreements.  See the NOTICE file
006:         * distributed with this work for additional information
007:         * regarding copyright ownership.  The ASF licenses this file
008:         * to you under the Apache License, Version 2.0 (the
009:         * "License"); you may not use this file except in compliance
010:         * with the License.  You may obtain a copy of the License at
011:         *
012:         *  http://www.apache.org/licenses/LICENSE-2.0
013:         *
014:         * Unless required by applicable law or agreed to in writing,
015:         * software distributed under the License is distributed on an
016:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017:         * KIND, either express or implied.  See the License for the
018:         * specific language governing permissions and limitations
019:         * under the License.
020:         */
021:        package org.apache.struts.validator.validwhen;
022:
023:        import org.apache.commons.logging.Log;
024:        import org.apache.commons.logging.LogFactory;
025:        import org.apache.commons.validator.Field;
026:        import org.apache.commons.validator.Validator;
027:        import org.apache.commons.validator.ValidatorAction;
028:        import org.apache.commons.validator.util.ValidatorUtils;
029:        import org.apache.struts.action.ActionMessage;
030:        import org.apache.struts.action.ActionMessages;
031:        import org.apache.struts.util.MessageResources;
032:        import org.apache.struts.validator.Resources;
033:
034:        import javax.servlet.http.HttpServletRequest;
035:
036:        import java.io.StringReader;
037:
038:        /**
039:         * This class contains the validwhen validation that is used in the
040:         * validator-rules.xml file.
041:         *
042:         * @since Struts 1.2
043:         */
044:        public class ValidWhen {
045:            /**
046:             * Commons Logging instance.
047:             */
048:            private static final Log log = LogFactory.getLog(ValidWhen.class);
049:
050:            /**
051:             * The message resources for this package.
052:             */
053:            private static MessageResources sysmsgs = MessageResources
054:                    .getMessageResources("org.apache.struts.validator.LocalStrings");
055:
056:            /**
057:             * Returns true if <code>obj</code> is null or a String.
058:             */
059:            private static boolean isString(Object obj) {
060:                return (obj == null) ? true : String.class.isInstance(obj);
061:            }
062:
063:            /**
064:             * Checks if the field matches the boolean expression specified in
065:             * <code>test</code> parameter.
066:             *
067:             * @param bean    The bean validation is being performed on.
068:             * @param va      The <code>ValidatorAction</code> that is currently being
069:             *                performed.
070:             * @param field   The <code>Field</code> object associated with the
071:             *                current field being validated.
072:             * @param errors  The <code>ActionMessages</code> object to add errors to
073:             *                if any validation errors occur.
074:             * @param request Current request object.
075:             * @return <code>true</code> if meets stated requirements,
076:             *         <code>false</code> otherwise.
077:             */
078:            public static boolean validateValidWhen(Object bean,
079:                    ValidatorAction va, Field field, ActionMessages errors,
080:                    Validator validator, HttpServletRequest request) {
081:                Object form = validator.getParameterValue(Validator.BEAN_PARAM);
082:                String value = null;
083:                boolean valid = false;
084:                int index = -1;
085:
086:                if (field.isIndexed()) {
087:                    String key = field.getKey();
088:
089:                    final int leftBracket = key.indexOf("[");
090:                    final int rightBracket = key.indexOf("]");
091:
092:                    if ((leftBracket > -1) && (rightBracket > -1)) {
093:                        index = Integer.parseInt(key.substring(leftBracket + 1,
094:                                rightBracket));
095:                    }
096:                }
097:
098:                if (isString(bean)) {
099:                    value = (String) bean;
100:                } else {
101:                    value = ValidatorUtils.getValueAsString(bean, field
102:                            .getProperty());
103:                }
104:
105:                String test = null;
106:
107:                try {
108:                    test = Resources.getVarValue("test", field, validator,
109:                            request, true);
110:                } catch (IllegalArgumentException ex) {
111:                    String logErrorMsg = sysmsgs.getMessage(
112:                            "validation.failed", "validwhen", field
113:                                    .getProperty(), ex.toString());
114:
115:                    log.error(logErrorMsg);
116:
117:                    String userErrorMsg = sysmsgs.getMessage("system.error");
118:
119:                    errors.add(field.getKey(), new ActionMessage(userErrorMsg,
120:                            false));
121:
122:                    return false;
123:                }
124:
125:                // Create the Lexer
126:                ValidWhenLexer lexer = null;
127:
128:                try {
129:                    lexer = new ValidWhenLexer(new StringReader(test));
130:                } catch (Exception ex) {
131:                    String logErrorMsg = "ValidWhenLexer Error for field ' "
132:                            + field.getKey() + "' - " + ex;
133:
134:                    log.error(logErrorMsg);
135:
136:                    String userErrorMsg = sysmsgs.getMessage("system.error");
137:
138:                    errors.add(field.getKey(), new ActionMessage(userErrorMsg,
139:                            false));
140:
141:                    return false;
142:                }
143:
144:                // Create the Parser
145:                ValidWhenParser parser = null;
146:
147:                try {
148:                    parser = new ValidWhenParser(lexer);
149:                } catch (Exception ex) {
150:                    String logErrorMsg = "ValidWhenParser Error for field ' "
151:                            + field.getKey() + "' - " + ex;
152:
153:                    log.error(logErrorMsg);
154:
155:                    String userErrorMsg = sysmsgs.getMessage("system.error");
156:
157:                    errors.add(field.getKey(), new ActionMessage(userErrorMsg,
158:                            false));
159:
160:                    return false;
161:                }
162:
163:                parser.setForm(form);
164:                parser.setIndex(index);
165:                parser.setValue(value);
166:
167:                try {
168:                    parser.expression();
169:                    valid = parser.getResult();
170:                } catch (Exception ex) {
171:                    String logErrorMsg = "ValidWhen Error for field ' "
172:                            + field.getKey() + "' - " + ex;
173:
174:                    log.error(logErrorMsg);
175:
176:                    String userErrorMsg = sysmsgs.getMessage("system.error");
177:
178:                    errors.add(field.getKey(), new ActionMessage(userErrorMsg,
179:                            false));
180:
181:                    return false;
182:                }
183:
184:                if (!valid) {
185:                    errors.add(field.getKey(), Resources.getActionMessage(
186:                            validator, request, va, field));
187:
188:                    return false;
189:                }
190:
191:                return true;
192:            }
193:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.