Source Code Cross Referenced for StrutsValidation.java in  » J2EE » jag » com » finalist » jaggenerator » validation » 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 » J2EE » jag » com.finalist.jaggenerator.validation 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*   Copyright (C) 2003 Finalist IT Group
002:         *
003:         *   This file is part of JAG - the Java J2EE Application Generator
004:         *
005:         *   JAG is free software; you can redistribute it and/or modify
006:         *   it under the terms of the GNU General Public License as published by
007:         *   the Free Software Foundation; either version 2 of the License, or
008:         *   (at your option) any later version.
009:         *   JAG is distributed in the hope that it will be useful,
010:         *   but WITHOUT ANY WARRANTY; without even the implied warranty of
011:         *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012:         *   GNU General Public License for more details.
013:         *   You should have received a copy of the GNU General Public License
014:         *   along with JAG; if not, write to the Free Software
015:         *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
016:         */
017:
018:        package com.finalist.jaggenerator.validation;
019:
020:        import java.util.ArrayList;
021:        import java.util.Iterator;
022:        import java.util.List;
023:        import java.util.StringTokenizer;
024:
025:        /**
026:         * Represents Struts 1.1 validations for a particular database column, ultimately destined to be
027:         * included in the 'validation.xml' declarative validations configuration file.
028:         * <p>
029:         * This class represents a Struts validation as two parts: a 'depends' list and an XML fragment:
030:         * the 'depends list' is a comma-seperated list of validation methods (e.g. 'integer', 'date', 'mask'),
031:         * and the XML fragment contains any parameters needed by those validation methods.
032:         *
033:         * @author Michael O'Connor - Finalist IT Group
034:         */
035:        public class StrutsValidation {
036:
037:            private String dependsList = "";
038:            private String xml = "";
039:
040:            /**
041:             * Creates a StrutsValidation without generating the validations.
042:             */
043:            public StrutsValidation() {
044:            }
045:
046:            /**
047:             *
048:             * @param sqlType the column type (SQL data type, e.g. NUMBER(12)).
049:             * @param jdbcType the JDBC data type.
050:             * @param required whether or not the field is mandatory.
051:             */
052:            public StrutsValidation(String sqlType, String jdbcType,
053:                    boolean required) {
054:                ArrayList validationsList = new ArrayList(3);
055:                StringBuffer validations = new StringBuffer();
056:                List parameters = getParams(sqlType);
057:
058:                if (required) {
059:                    validationsList.add("required");
060:                }
061:                if (sqlType.startsWith("NUMBER") || sqlType.equals("MEDIUMINT")) {
062:                    if ("INTEGER".equals(jdbcType)) {
063:                        validationsList.add("integer");
064:                    } else {
065:                        validationsList.add("float");
066:
067:                        if (parameters.size() == 2) {
068:                            validationsList.add("decimal");
069:                            generateDecimalMaskXML(Integer
070:                                    .parseInt((String) parameters.get(0)),
071:                                    Integer
072:                                            .parseInt((String) parameters
073:                                                    .get(1)));
074:                        } else if (!parameters.isEmpty()) {
075:                            validationsList.add("maxlength");
076:                            generateMaxlengthXML(Integer
077:                                    .parseInt((String) parameters.get(0)));
078:                        }
079:                    }
080:                }
081:                if (sqlType.startsWith("INT")) {
082:                    validationsList.add("integer");
083:                    if (!parameters.isEmpty()) {
084:                        validationsList.add("maxlength");
085:                        generateMaxlengthXML(Integer
086:                                .parseInt((String) parameters.get(0)));
087:                    }
088:                }
089:                if (sqlType.indexOf("CHAR") != -1 && !parameters.isEmpty()) {
090:                    validationsList.add("maxlength");
091:                    generateMaxlengthXML(Integer.parseInt((String) parameters
092:                            .get(0)));
093:                }
094:                if (sqlType.indexOf("DATE") != -1) {
095:                    validationsList.add("date");
096:                    generateDateXML();
097:                }
098:                if (sqlType.indexOf("BIGINT") != -1) {
099:                    validationsList.add("long");
100:                    if (!parameters.isEmpty()) {
101:                        validationsList.add("maxlength");
102:                        generateMaxlengthXML(Integer
103:                                .parseInt((String) parameters.get(0)));
104:                    }
105:                }
106:                if (sqlType.indexOf("SMALLINT") != -1) {
107:                    validationsList.add("short");
108:                    if (!parameters.isEmpty()) {
109:                        validationsList.add("maxlength");
110:                        generateMaxlengthXML(Integer
111:                                .parseInt((String) parameters.get(0)));
112:                    }
113:                }
114:                if (sqlType.indexOf("TINYINT") != -1) {
115:                    validationsList.add("byte");
116:                    if (!parameters.isEmpty()) {
117:                        validationsList.add("maxlength");
118:                        generateMaxlengthXML(Integer
119:                                .parseInt((String) parameters.get(0)));
120:                    }
121:                }
122:                if (sqlType.indexOf("FLOAT") != -1) {
123:                    validationsList.add("float");
124:                }
125:
126:                Iterator v = validationsList.iterator();
127:                while (v.hasNext()) {
128:                    String item = (String) v.next();
129:                    validations.append(item);
130:                    if (v.hasNext()) {
131:                        validations.append(", ");
132:                    }
133:                }
134:                dependsList = validations.toString();
135:
136:            }
137:
138:            /** @see {@link #getDependsList()}. */
139:            public void setDependsList(String dependsList) {
140:                this .dependsList = dependsList;
141:            }
142:
143:            /** @see {@link #getXml()}. */
144:            public void setXml(String xml) {
145:                this .xml = xml;
146:            }
147:
148:            /**
149:             * Gets the comma-seperated list of Struts validators to be applied to this field.
150:             * @return the dependsList.
151:             */
152:            public String getDependsList() {
153:                return dependsList;
154:            }
155:
156:            /**
157:             * Gets the corresponding XML fragment that complements the dependsList.
158:             * @return the xml fragment.
159:             */
160:            public String getXml() {
161:                return xml;
162:            }
163:
164:            private void generateDateXML() {
165:                StringBuffer temp = new StringBuffer(xml);
166:                temp.append("<var>\n");
167:                temp.append("   <var-name>datePattern</var-name>\n");
168:                temp.append("   <var-value>${date_format}</var-value>\n");
169:                temp.append("</var>");
170:                setXml(temp.toString());
171:            }
172:
173:            private void generateTimestampXML() {
174:                StringBuffer temp = new StringBuffer(xml);
175:                temp.append("<var>\n");
176:                temp.append("   <var-name>datePattern</var-name>\n");
177:                temp.append("   <var-value>${timestamp_format}</var-value>\n");
178:                temp.append("</var>");
179:                setXml(temp.toString());
180:            }
181:
182:            private void generateMaxlengthXML(int maxLength) {
183:                StringBuffer temp = new StringBuffer(xml);
184:                temp
185:                        .append("<arg1 key=\"${var:maxlength}\" name=\"maxlength\" resource=\"false\"/>\n");
186:                temp.append("<var>\n");
187:                temp.append("   <var-name>maxlength</var-name>\n");
188:                temp.append("   <var-value>").append(maxLength).append(
189:                        "</var-value>\n");
190:                temp.append("</var>");
191:                setXml(temp.toString());
192:            }
193:
194:            private void generateDecimalMaskXML(int length, int precision) {
195:                StringBuffer temp = new StringBuffer(xml);
196:                temp
197:                        .append("<arg1 key=\"${var:decimalPrecision}\" name=\"decimal\" resource=\"false\"/>");
198:                temp
199:                        .append("<arg2 key=\"${var:decimalLength}\" name=\"decimal\" resource=\"false\"/>");
200:                temp.append("<var>\n");
201:                temp.append("   <var-name>decimalLength</var-name>\n");
202:                temp.append("   <var-value>").append(length).append(
203:                        "</var-value>\n");
204:                temp.append("</var>");
205:                temp.append("<var>\n");
206:                temp.append("   <var-name>decimalPrecision</var-name>\n");
207:                temp.append("   <var-value>").append(precision).append(
208:                        "</var-value>\n");
209:                temp.append("</var>");
210:                setXml(temp.toString());
211:            }
212:
213:            public static List getParams(String sqlType) {
214:                String trimmed = sqlType.trim();
215:                ArrayList params = new ArrayList();
216:                int openBracketPos = trimmed.indexOf('(');
217:                int closeBracketPos = trimmed.indexOf(')');
218:                if (openBracketPos != -1
219:                        && closeBracketPos == (trimmed.length() - 1)) {
220:                    StringTokenizer tokie = new StringTokenizer(trimmed
221:                            .substring(openBracketPos + 1, closeBracketPos),
222:                            ",");
223:                    while (tokie.hasMoreTokens()) {
224:                        params.add(tokie.nextToken().trim());
225:                    }
226:                }
227:
228:                return params;
229:            }
230:
231:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.