Source Code Cross Referenced for ValidationManager.java in  » Web-Framework » jWebApp » jwebapp » controller » 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 » jWebApp » jwebapp.controller 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Copyright (C) 2006, 2007 David Bulmore, Software Sensation Inc.  
003:         * All Rights Reserved.
004:         *
005:         * This file is part of jWebApp.
006:         *
007:         * jWebApp is free software; you can redistribute it and/or modify it under 
008:         * the terms of the GNU General Public License (Version 2) as published by 
009:         * the Free Software Foundation.
010:         *
011:         * jWebApp is distributed in the hope that it will be useful, but WITHOUT 
012:         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
013:         * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
014:         * for more details.
015:         *
016:         * You should have received a copy of the GNU General Public License 
017:         * along with jWebApp; if not, write to the Free Software Foundation, 
018:         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
019:         */package jwebapp.controller;
020:
021:        import jwebtk.validator.Validator;
022:        import java.util.Enumeration;
023:        import java.util.logging.Level;
024:        import java.util.logging.Logger;
025:
026:        /**
027:         * Provides basic validation.
028:         */
029:        public class ValidationManager {
030:            private static Logger logger = Logger
031:                    .getLogger(ValidationManager.class.getName());
032:
033:            /**
034:             * Validates the parameters based in validation definitions in the request data.
035:             * @param serverInterface server interface
036:             * @return true if the request passes validation, false otherwise.
037:             */
038:            public static boolean validateParameters(
039:                    ServerInterface serverInterface) {
040:                boolean isValid = true;
041:                DataManager.RequestData requestData = serverInterface
042:                        .getRequestData();
043:
044:                if (requestData.isDefined()) {
045:                    if (requestData.getValidationVector().size() != 0) {
046:                        for (int i = 0; i < requestData.getValidationVector()
047:                                .size(); i++) {
048:                            String val = (String) requestData
049:                                    .getValidationVector().elementAt(i);
050:                            String parameterId = val.substring(0, val
051:                                    .indexOf('|'));
052:                            String validationId = val.substring(val
053:                                    .indexOf('|') + 1);
054:                            ValidationData validationData = serverInterface
055:                                    .getDataManager().getValidation(
056:                                            validationId);
057:
058:                            if (logger.isLoggable(Level.FINER))
059:                                logger.finer("validating parameter "
060:                                        + parameterId + " with validation "
061:                                        + validationId);
062:
063:                            boolean valid = validateParameterValue(
064:                                    serverInterface, validationData,
065:                                    serverInterface.getParameter(parameterId));
066:
067:                            if (logger.isLoggable(Level.FINER))
068:                                logger.finer("parameter "
069:                                        + parameterId
070:                                        + (valid ? " is valid"
071:                                                : " is not valid"));
072:
073:                            if (!valid)
074:                                isValid = false;
075:                        }
076:                    }
077:                } else {
078:                    Enumeration e = serverInterface.getDataManager()
079:                            .getValidationVector().elements();
080:
081:                    while (e.hasMoreElements()) {
082:                        ValidationData validationData = (ValidationData) e
083:                                .nextElement();
084:                        String parameterId = validationData.getValidationId(), parameter = serverInterface
085:                                .getParameter(parameterId);
086:
087:                        if (parameter != null) {
088:                            if (logger.isLoggable(Level.FINER))
089:                                logger.finer("validating parameter "
090:                                        + parameterId);
091:
092:                            boolean valid = validateParameterValue(
093:                                    serverInterface, validationData, parameter);
094:
095:                            if (logger.isLoggable(Level.FINER))
096:                                logger.finer("parameter "
097:                                        + parameterId
098:                                        + (valid ? " is valid"
099:                                                : " is not valid"));
100:
101:                            if (!valid)
102:                                isValid = false;
103:                        }
104:                    }
105:                }
106:
107:                return isValid;
108:            }
109:
110:            private static boolean validateParameterValue(
111:                    ServerInterface serverInterface,
112:                    ValidationData validationData, String parameter) {
113:                boolean passed = true;
114:                String message = "";
115:
116:                try {
117:                    if (validationData.getType().equalsIgnoreCase(
118:                            "isNotEmptyOrNull"))
119:                        passed = getValidator().isNotEmptyOrNull(parameter);
120:                    else if (validationData.getType().equalsIgnoreCase(
121:                            "isEmptyOrNull"))
122:                        passed = getValidator().isEmptyOrNull(parameter);
123:                    else if (validationData.getType()
124:                            .equalsIgnoreCase("isByte"))
125:                        passed = getValidator().isByte(parameter);
126:                    else if (validationData.getType().equalsIgnoreCase(
127:                            "isCreditCard"))
128:                        passed = getValidator().isCreditCard(parameter);
129:                    else if (validationData.getType()
130:                            .equalsIgnoreCase("isDate"))
131:                        passed = getValidator().isDate(parameter,
132:                                validationData.getArguments(), true);
133:                    else if (validationData.getType().equalsIgnoreCase(
134:                            "isDouble"))
135:                        passed = getValidator().isDouble(parameter);
136:                    else if (validationData.getType().equalsIgnoreCase(
137:                            "isEmail"))
138:                        passed = getValidator().isEmail(parameter);
139:                    else if (validationData.getType().equalsIgnoreCase(
140:                            "isEqualTo"))
141:                        passed = getValidator().isEqualTo(parameter,
142:                                validationData.getArguments(), false);
143:                    else if (validationData.getType().equalsIgnoreCase(
144:                            "isEqualToIgnoreCase"))
145:                        passed = getValidator().isEqualTo(parameter,
146:                                validationData.getArguments(), true);
147:                    else if (validationData.getType().equalsIgnoreCase(
148:                            "isFloat"))
149:                        passed = getValidator().isFloat(parameter);
150:                    else if (validationData.getType().equalsIgnoreCase(
151:                            "isInRange")) {
152:                        if (validationData.getArguments().length() > 0
153:                                && validationData.getArguments().indexOf(',') != -1) {
154:                            String val1 = validationData.getArguments()
155:                                    .substring(
156:                                            0,
157:                                            validationData.getArguments()
158:                                                    .indexOf(',')), val2 = validationData
159:                                    .getArguments().substring(
160:                                            validationData.getArguments()
161:                                                    .indexOf(',') + 1);
162:
163:                            if (val1.indexOf('.') > -1
164:                                    || val2.indexOf('.') > -1)
165:                                passed = getValidator().isInRange(
166:                                        new Double(parameter).doubleValue(),
167:                                        new Double(val1).doubleValue(),
168:                                        new Double(val2).doubleValue());
169:                            else
170:                                passed = getValidator().isInRange(
171:                                        new Integer(parameter).intValue(),
172:                                        new Integer(val1).intValue(),
173:                                        new Integer(val2).intValue());
174:                        }
175:                    } else if (validationData.getType().equalsIgnoreCase(
176:                            "isInt"))
177:                        passed = getValidator().isInt(parameter);
178:                    else if (validationData.getType()
179:                            .equalsIgnoreCase("isIsbn"))
180:                        passed = getValidator().isIsbn(parameter);
181:                    else if (validationData.getType()
182:                            .equalsIgnoreCase("isLong"))
183:                        passed = getValidator().isLong(parameter);
184:                    else if (validationData.getType().equalsIgnoreCase(
185:                            "isShort"))
186:                        passed = getValidator().isShort(parameter);
187:                    else if (validationData.getType().equalsIgnoreCase(
188:                            "isTrueFalse"))
189:                        passed = parameter.equalsIgnoreCase("true")
190:                                || parameter.equalsIgnoreCase("false");
191:                    else if (validationData.getType().equalsIgnoreCase(
192:                            "isYesNo"))
193:                        passed = parameter.equalsIgnoreCase("yes")
194:                                || parameter.equalsIgnoreCase("no");
195:                    else if (validationData.getType().equalsIgnoreCase(
196:                            "isLessOrEqualToMax")) {
197:                        if (parameter.indexOf('.') > -1)
198:                            passed = getValidator().maxValue(
199:                                    new Double(parameter).doubleValue(),
200:                                    new Double(validationData.getArguments())
201:                                            .doubleValue());
202:                        else
203:                            passed = getValidator().maxValue(
204:                                    new Integer(parameter).intValue(),
205:                                    new Integer(validationData.getArguments())
206:                                            .intValue());
207:                    } else if (validationData.getType().equalsIgnoreCase(
208:                            "isGreaterOrEqualToMin")) {
209:                        if (parameter.indexOf('.') > -1)
210:                            passed = getValidator().minValue(
211:                                    new Double(parameter).doubleValue(),
212:                                    new Double(validationData.getArguments())
213:                                            .doubleValue());
214:                        else
215:                            passed = getValidator().minValue(
216:                                    new Integer(parameter).intValue(),
217:                                    new Integer(validationData.getArguments())
218:                                            .intValue());
219:                    } else if (validationData.getType().equalsIgnoreCase(
220:                            "isLessOrEqualToMaxLength"))
221:                        passed = getValidator().maxLength(
222:                                parameter,
223:                                new Integer(validationData.getArguments())
224:                                        .intValue());
225:                    else if (validationData.getType().equalsIgnoreCase(
226:                            "isGreaterOrEqualToMinLength"))
227:                        passed = getValidator().minLength(
228:                                parameter,
229:                                new Integer(validationData.getArguments())
230:                                        .intValue());
231:                    else if (validationData.getType().equalsIgnoreCase(
232:                            "isSubstring"))
233:                        passed = getValidator().isSubstring(parameter,
234:                                validationData.getArguments(), false);
235:                    else if (validationData.getType().equalsIgnoreCase(
236:                            "isSubstringIgnoreCase"))
237:                        passed = getValidator().isSubstring(parameter,
238:                                validationData.getArguments(), true);
239:                    else if (validationData.getType().equalsIgnoreCase("isUrl"))
240:                        passed = getValidator().isUrl(parameter);
241:                    else if (validationData.getType().equalsIgnoreCase(
242:                            "matchesRegExp"))
243:                        passed = getValidator().matchRegexp(parameter,
244:                                validationData.getArguments());
245:                    else if (validationData.getType().equalsIgnoreCase(
246:                            "endsWith"))
247:                        passed = getValidator().endsWith(parameter,
248:                                validationData.getArguments(), false);
249:                    else if (validationData.getType().equalsIgnoreCase(
250:                            "endsWithIgnoreCase"))
251:                        passed = getValidator().endsWith(parameter,
252:                                validationData.getArguments(), true);
253:                    else if (validationData.getType().equalsIgnoreCase(
254:                            "startsWith"))
255:                        passed = getValidator().startsWith(parameter,
256:                                validationData.getArguments(), false);
257:                    else if (validationData.getType().equalsIgnoreCase(
258:                            "startsWithIgnoreCase"))
259:                        passed = getValidator().startsWith(parameter,
260:                                validationData.getArguments(), true);
261:                    else if (!validationData.getType().equalsIgnoreCase(
262:                            "isNotEmptyOrNull")) {
263:                        serverInterface
264:                                .getServletContext()
265:                                .log(
266:                                        "Unknown validation type or invalid argument syntax");
267:                        message = "Unknown validation type or invalid argument syntax<br>\n";
268:
269:                        passed = false;
270:                    }
271:                } catch (NumberFormatException e) {
272:                    serverInterface.getServletContext().log(
273:                            "invalid number format");
274:                    message = "invalid number format<br>\n";
275:
276:                    passed = false;
277:                } catch (Exception e) {
278:                    serverInterface.getServletContext().log(
279:                            "invalid argument syntax");
280:                    message = "invalid argument syntax<br>\n";
281:
282:                    passed = false;
283:                }
284:
285:                if (!passed)
286:                    serverInterface.setErrorMessage(
287:                            ServerInterface.VALIDATION_ERROR, serverInterface
288:                                    .getErrorMessage()
289:                                    + validationData.getErrorMessage()
290:                                    + "<br>\n" + message);
291:
292:                return passed;
293:            }
294:
295:            /**
296:             * Returns an instance of this validator.
297:             * @return an instance of ValidationHandler
298:             */
299:            public static Validator getValidator() {
300:                return new Validator();
301:            }
302:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.