Source Code Cross Referenced for FieldValidator.java in  » J2EE » Jaffa » org » jaffa » datatypes » 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 » Jaffa » org.jaffa.datatypes 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * ====================================================================
003:         * JAFFA - Java Application Framework For All
004:         *
005:         * Copyright (C) 2002 JAFFA Development Group
006:         *
007:         *     This library is free software; you can redistribute it and/or
008:         *     modify it under the terms of the GNU Lesser General Public
009:         *     License as published by the Free Software Foundation; either
010:         *     version 2.1 of the License, or (at your option) any later version.
011:         *
012:         *     This library is distributed in the hope that it will be useful,
013:         *     but WITHOUT ANY WARRANTY; without even the implied warranty of
014:         *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015:         *     Lesser General Public License for more details.
016:         *
017:         *     You should have received a copy of the GNU Lesser General Public
018:         *     License along with this library; if not, write to the Free Software
019:         *     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
020:         *
021:         * Redistribution and use of this software and associated documentation ("Software"),
022:         * with or without modification, are permitted provided that the following conditions are met:
023:         * 1.	Redistributions of source code must retain copyright statements and notices.
024:         *         Redistributions must also contain a copy of this document.
025:         * 2.	Redistributions in binary form must reproduce the above copyright notice,
026:         * 	this list of conditions and the following disclaimer in the documentation
027:         * 	and/or other materials provided with the distribution.
028:         * 3.	The name "JAFFA" must not be used to endorse or promote products derived from
029:         * 	this Software without prior written permission. For written permission,
030:         * 	please contact mail to: jaffagroup@yahoo.com.
031:         * 4.	Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
032:         * 	appear in their names without prior written permission.
033:         * 5.	Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
034:         *
035:         * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
036:         * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037:         * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038:         * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039:         * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040:         * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041:         * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042:         * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043:         * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044:         * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045:         * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046:         * SUCH DAMAGE.
047:         * ====================================================================
048:         */
049:
050:        package org.jaffa.datatypes;
051:
052:        import org.jaffa.datatypes.exceptions.*;
053:        import org.jaffa.metadata.*;
054:        import java.util.regex.*;
055:        import java.text.*;
056:        import org.apache.log4j.*;
057:        import java.io.*;
058:
059:        /** This class has convenience methods for validating data.
060:         */
061:        public class FieldValidator {
062:            private static final Logger log = Logger
063:                    .getLogger(FieldValidator.class);
064:
065:            /** This method validates the input field value based on the meta data.
066:             * This will not check the mandatory property.
067:             * It then returns the parsed object corresponding to the input field.
068:             * @param field the value to validate.
069:             * @param meta the meta object based on which the validation is to be performed.
070:             * @throws MandatoryFieldException if the field is null. This should never happen.
071:             * @throws TooMuchDataException if the length of the field exceeds the limit specified in the meta data.
072:             * @throws TooLittleDataException if the length of the field is below the value specified in the meta data.
073:             * @throws PatternMismatchException if the field does not conform to the pattern specified in the meta data.
074:             * @throws BelowMinimumException if the field has a value less than the minimum value specified in the meta data.
075:             * @throws ExceedsMaximumException if the field has a value greater than the maximum value specified in the meta data.
076:             * @throws FormatCurrencyException if the field has an invalid currency format. This can happen for Currency fields only.
077:             * @throws FormatDateOnlyException if the field has an invalid date format. This can happen for DateOnly fields only.
078:             * @throws FormatDateTimeException if the field has an invalid datetime format. This can happen for DateTime fields only.
079:             * @throws FormatDecimalException if the field has an invalid decimal format. This can happen for Decimal fields only.
080:             * @throws FormatIntegerException if the field has an invalid integer format. This can happen for Integer fields only.
081:             * @return the parsed object corresponding to the input field.
082:             */
083:            public static Object validateData(String field, FieldMetaData meta)
084:                    throws MandatoryFieldException, TooMuchDataException,
085:                    TooLittleDataException, PatternMismatchException,
086:                    BelowMinimumException, ExceedsMaximumException,
087:                    FormatCurrencyException, FormatDateOnlyException,
088:                    FormatDateTimeException, FormatDecimalException,
089:                    FormatIntegerException {
090:                return validateData(field, meta, false);
091:            }
092:
093:            /** This method validates the input field value based on the meta data.
094:             * It then returns the parsed object corresponding to the input field.
095:             * @param field the value to validate.
096:             * @param meta the meta object based on which the validation is to be performed.
097:             * @param checkMandatory determines if the mandatory check is to be performed.
098:             * @throws MandatoryFieldException if checkMandatory is true and the field is null.
099:             * @throws TooMuchDataException if the length of the field exceeds the limit specified in the meta data.
100:             * @throws TooLittleDataException if the length of the field is below the value specified in the meta data.
101:             * @throws PatternMismatchException if the field does not conform to the pattern specified in the meta data.
102:             * @throws BelowMinimumException if the field has a value less than the minimum value specified in the meta data.
103:             * @throws ExceedsMaximumException if the field has a value greater than the maximum value specified in the meta data.
104:             * @throws FormatCurrencyException if the field has an invalid currency format. This can happen for Currency fields only.
105:             * @throws FormatDateOnlyException if the field has an invalid date format. This can happen for DateOnly fields only.
106:             * @throws FormatDateTimeException if the field has an invalid datetime format. This can happen for DateTime fields only.
107:             * @throws FormatDecimalException if the field has an invalid decimal format. This can happen for Decimal fields only.
108:             * @throws FormatIntegerException if the field has an invalid integer format. This can happen for Integer fields only.
109:             * @return the parsed object corresponding to the input field.
110:             */
111:            public static Object validateData(String field, FieldMetaData meta,
112:                    boolean checkMandatory) throws MandatoryFieldException,
113:                    TooMuchDataException, TooLittleDataException,
114:                    PatternMismatchException, BelowMinimumException,
115:                    ExceedsMaximumException, FormatCurrencyException,
116:                    FormatDateOnlyException, FormatDateTimeException,
117:                    FormatDecimalException, FormatIntegerException {
118:                Object output = null;
119:                String dataType = meta.getDataType();
120:                if (dataType.equals(Defaults.STRING)) {
121:                    output = validateString(field, (StringFieldMetaData) meta,
122:                            checkMandatory);
123:
124:                } else if (dataType.equals(Defaults.BOOLEAN)) {
125:                    output = validateBoolean(field,
126:                            (BooleanFieldMetaData) meta, checkMandatory);
127:
128:                } else if (dataType.equals(Defaults.CURRENCY)) {
129:                    output = validateCurrency(field,
130:                            (CurrencyFieldMetaData) meta, checkMandatory);
131:
132:                } else if (dataType.equals(Defaults.DATEONLY)) {
133:                    output = validateDateOnly(field,
134:                            (DateOnlyFieldMetaData) meta, checkMandatory);
135:
136:                } else if (dataType.equals(Defaults.DATETIME)) {
137:                    output = validateDateTime(field,
138:                            (DateTimeFieldMetaData) meta, checkMandatory);
139:
140:                } else if (dataType.equals(Defaults.DECIMAL)) {
141:                    output = validateDecimal(field,
142:                            (DecimalFieldMetaData) meta, checkMandatory);
143:
144:                } else if (dataType.equals(Defaults.INTEGER)) {
145:                    output = validateInteger(field,
146:                            (IntegerFieldMetaData) meta, checkMandatory);
147:
148:                } else if (dataType.equals(Defaults.RAW)) {
149:                    output = validateRaw(field, (RawFieldMetaData) meta,
150:                            checkMandatory);
151:
152:                } else {
153:                    // this should never happen
154:                    String str = "Unknown DataType - " + dataType
155:                            + ". Unable to perform field validation";
156:                    log.error(str);
157:                    throw new UnknownDataTypeRuntimeException(str);
158:                }
159:                return output;
160:            }
161:
162:            /** This method validates the input field value based on the meta data.
163:             * It then returns the parsed object corresponding to the input field.
164:             * @param field the value to validate.
165:             * @param meta the meta object based on which the validation is to be performed.
166:             * @param checkMandatory determines if the mandatory check is to be performed.
167:             * @throws MandatoryFieldException if checkMandatory is true and the field is null.
168:             * @throws TooMuchDataException if the length of the field exceeds the limit specified in the meta data.
169:             * @throws TooLittleDataException if the length of the field is below the value specified in the meta data.
170:             * @throws PatternMismatchException if the field does not conform to the pattern specified in the meta data.
171:             * @return the parsed object corresponding to the input field.
172:             */
173:            public static String validate(String field,
174:                    StringFieldMetaData meta, boolean checkMandatory)
175:                    throws MandatoryFieldException, TooMuchDataException,
176:                    TooLittleDataException, PatternMismatchException {
177:                String output = field;
178:                if (field != null && field.length() > 0) {
179:                    // convert to required case
180:                    String caseType = meta.getCaseType();
181:                    if (caseType != null) {
182:                        if (caseType.equals(StringFieldMetaData.UPPER_CASE))
183:                            output = field.toUpperCase();
184:                        else if (caseType
185:                                .equals(StringFieldMetaData.LOWER_CASE))
186:                            output = field.toLowerCase();
187:                        else
188:                            output = field;
189:                    }
190:
191:                    // check length
192:                    Integer length = meta.getLength();
193:                    if (length != null && output.length() > length.intValue())
194:                        throw new TooMuchDataException(meta.getLabelToken(),
195:                                new Object[] { length });
196:
197:                    // check minimum length
198:                    Integer minLength = meta.getMinLength();
199:                    if (minLength != null
200:                            && output.length() < minLength.intValue())
201:                        throw new TooLittleDataException(meta.getLabelToken(),
202:                                new Object[] { minLength });
203:
204:                    // validate pattern
205:                    String pattern = meta.getPattern();
206:                    if (pattern != null) {
207:                        try {
208:                            // @todo: the pattern can probably be cached
209:                            if (!Pattern.matches(pattern, output))
210:                                throw new PatternMismatchException(meta
211:                                        .getLabelToken(),
212:                                        new Object[] { pattern });
213:                        } catch (PatternSyntaxException e) {
214:                            // This should never happen
215:                            String str = "Invalid syntax for Regular Expression "
216:                                    + pattern;
217:                            log.error(str, e);
218:                            throw new InvalidPatternRuntimeException(str, e);
219:                        }
220:                    }
221:                }
222:
223:                // check mandatory
224:                if (checkMandatory) {
225:                    Boolean bool = meta.isMandatory();
226:                    if (bool != null && bool.booleanValue()) {
227:                        if (output == null || output.length() == 0)
228:                            throw new MandatoryFieldException(meta
229:                                    .getLabelToken());
230:                    }
231:                }
232:
233:                return output;
234:            }
235:
236:            /** This method validates the input field value based on the meta data.
237:             * It then returns the parsed object corresponding to the input field.
238:             * @param field the value to validate.
239:             * @param meta the meta object based on which the validation is to be performed.
240:             * @param checkMandatory determines if the mandatory check is to be performed.
241:             * @throws MandatoryFieldException if checkMandatory is true and the field is null.
242:             * @return the parsed object corresponding to the input field.
243:             */
244:            public static Boolean validate(Boolean field,
245:                    BooleanFieldMetaData meta, boolean checkMandatory)
246:                    throws MandatoryFieldException {
247:                // check mandatory
248:                if (checkMandatory) {
249:                    Boolean bool = meta.isMandatory();
250:                    if (bool != null && bool.booleanValue()) {
251:                        if (field == null)
252:                            throw new MandatoryFieldException(meta
253:                                    .getLabelToken());
254:                    }
255:                }
256:                return field;
257:            }
258:
259:            /** This method validates the input field value based on the meta data.
260:             * It then returns the parsed object corresponding to the input field.
261:             * @param field the value to validate.
262:             * @param meta the meta object based on which the validation is to be performed.
263:             * @param checkMandatory determines if the mandatory check is to be performed.
264:             * @throws MandatoryFieldException if checkMandatory is true and the field is null.
265:             * @throws TooMuchDataException if the length of the field exceeds the limit specified in the meta data.
266:             * @throws BelowMinimumException if the field has a value less than the minimum value specified in the meta data.
267:             * @throws ExceedsMaximumException if the field has a value greater than the maximum value specified in the meta data.
268:             * @return the parsed object corresponding to the input field.
269:             */
270:            public static Currency validate(Currency field,
271:                    CurrencyFieldMetaData meta, boolean checkMandatory)
272:                    throws MandatoryFieldException, TooMuchDataException,
273:                    BelowMinimumException, ExceedsMaximumException {
274:                if (field != null) {
275:                    // check the intSize/fracSize
276:                    if (!checkLength(field.getValue(), meta.getIntSize(), meta
277:                            .getFracSize()))
278:                        throw new TooMuchDataException(meta.getLabelToken(),
279:                                new Object[] { meta.getIntSize() + ", "
280:                                        + meta.getFracSize() });
281:
282:                    // check the min-value
283:                    Currency min = meta.getMinValue();
284:                    if (min != null) {
285:                        if (min.compareTo(field) > 0)
286:                            throw new BelowMinimumException(meta
287:                                    .getLabelToken(), new Object[] { meta
288:                                    .getMinValue() });
289:                    }
290:
291:                    // check the max-value
292:                    Currency max = meta.getMaxValue();
293:                    if (max != null) {
294:                        if (max.compareTo(field) < 0)
295:                            throw new ExceedsMaximumException(meta
296:                                    .getLabelToken(), new Object[] { meta
297:                                    .getMaxValue() });
298:                    }
299:                }
300:
301:                // check mandatory
302:                if (checkMandatory) {
303:                    Boolean bool = meta.isMandatory();
304:                    if (bool != null && bool.booleanValue()) {
305:                        if (field == null)
306:                            throw new MandatoryFieldException(meta
307:                                    .getLabelToken());
308:                    }
309:                }
310:                return field;
311:            }
312:
313:            /** This method validates the input field value based on the meta data.
314:             * It then returns the parsed object corresponding to the input field.
315:             * @param field the value to validate.
316:             * @param meta the meta object based on which the validation is to be performed.
317:             * @param checkMandatory determines if the mandatory check is to be performed.
318:             * @throws MandatoryFieldException if checkMandatory is true and the field is null.
319:             * @throws BelowMinimumException if the field has a value less than the minimum value specified in the meta data.
320:             * @throws ExceedsMaximumException if the field has a value greater than the maximum value specified in the meta data.
321:             * @return the parsed object corresponding to the input field.
322:             */
323:            public static DateOnly validate(DateOnly field,
324:                    DateOnlyFieldMetaData meta, boolean checkMandatory)
325:                    throws MandatoryFieldException, BelowMinimumException,
326:                    ExceedsMaximumException {
327:                if (field != null) {
328:                    // check the min-value
329:                    DateOnly min = meta.getMinValue();
330:                    if (min != null) {
331:                        if (min.compareTo(field) > 0)
332:                            throw new BelowMinimumException(meta
333:                                    .getLabelToken(), new Object[] { meta
334:                                    .getMinValue() });
335:                    }
336:
337:                    // check the max-value
338:                    DateOnly max = meta.getMaxValue();
339:                    if (max != null) {
340:                        if (max.compareTo(field) < 0)
341:                            throw new ExceedsMaximumException(meta
342:                                    .getLabelToken(), new Object[] { meta
343:                                    .getMaxValue() });
344:                    }
345:                }
346:
347:                // check mandatory
348:                if (checkMandatory) {
349:                    Boolean bool = meta.isMandatory();
350:                    if (bool != null && bool.booleanValue()) {
351:                        if (field == null)
352:                            throw new MandatoryFieldException(meta
353:                                    .getLabelToken());
354:                    }
355:                }
356:                return field;
357:            }
358:
359:            /** This method validates the input field value based on the meta data.
360:             * It then returns the parsed object corresponding to the input field.
361:             * @param field the value to validate.
362:             * @param meta the meta object based on which the validation is to be performed.
363:             * @param checkMandatory determines if the mandatory check is to be performed.
364:             * @throws MandatoryFieldException if checkMandatory is true and the field is null.
365:             * @throws BelowMinimumException if the field has a value less than the minimum value specified in the meta data.
366:             * @throws ExceedsMaximumException if the field has a value greater than the maximum value specified in the meta data.
367:             * @return the parsed object corresponding to the input field.
368:             */
369:            public static DateTime validate(DateTime field,
370:                    DateTimeFieldMetaData meta, boolean checkMandatory)
371:                    throws MandatoryFieldException, BelowMinimumException,
372:                    ExceedsMaximumException {
373:                if (field != null) {
374:                    // check the min-value
375:                    DateTime min = meta.getMinValue();
376:                    if (min != null) {
377:                        if (min.compareTo(field) > 0)
378:                            throw new BelowMinimumException(meta
379:                                    .getLabelToken(), new Object[] { meta
380:                                    .getMinValue() });
381:                    }
382:
383:                    // check the max-value
384:                    DateTime max = meta.getMaxValue();
385:                    if (max != null) {
386:                        if (max.compareTo(field) < 0)
387:                            throw new ExceedsMaximumException(meta
388:                                    .getLabelToken(), new Object[] { meta
389:                                    .getMaxValue() });
390:                    }
391:                }
392:
393:                // check mandatory
394:                if (checkMandatory) {
395:                    Boolean bool = meta.isMandatory();
396:                    if (bool != null && bool.booleanValue()) {
397:                        if (field == null)
398:                            throw new MandatoryFieldException(meta
399:                                    .getLabelToken());
400:                    }
401:                }
402:                return field;
403:            }
404:
405:            /** This method validates the input field value based on the meta data.
406:             * It then returns the parsed object corresponding to the input field.
407:             * @param field the value to validate.
408:             * @param meta the meta object based on which the validation is to be performed.
409:             * @param checkMandatory determines if the mandatory check is to be performed.
410:             * @throws MandatoryFieldException if checkMandatory is true and the field is null.
411:             * @throws TooMuchDataException if the length of the field exceeds the limit specified in the meta data.
412:             * @throws BelowMinimumException if the field has a value less than the minimum value specified in the meta data.
413:             * @throws ExceedsMaximumException if the field has a value greater than the maximum value specified in the meta data.
414:             * @return the parsed object corresponding to the input field.
415:             */
416:            public static Double validate(Double field,
417:                    DecimalFieldMetaData meta, boolean checkMandatory)
418:                    throws MandatoryFieldException, TooMuchDataException,
419:                    BelowMinimumException, ExceedsMaximumException {
420:                if (field != null) {
421:                    // check the intSize/fracSize
422:                    if (!checkLength(field, meta.getIntSize(), meta
423:                            .getFracSize()))
424:                        throw new TooMuchDataException(meta.getLabelToken(),
425:                                new Object[] { meta.getIntSize() + ", "
426:                                        + meta.getFracSize() });
427:
428:                    // check the min-value
429:                    Double min = meta.getMinValue();
430:                    if (min != null) {
431:                        if (min.compareTo(field) > 0)
432:                            throw new BelowMinimumException(meta
433:                                    .getLabelToken(), new Object[] { meta
434:                                    .getMinValue() });
435:                    }
436:
437:                    // check the max-value
438:                    Double max = meta.getMaxValue();
439:                    if (max != null) {
440:                        if (max.compareTo(field) < 0)
441:                            throw new ExceedsMaximumException(meta
442:                                    .getLabelToken(), new Object[] { meta
443:                                    .getMaxValue() });
444:                    }
445:                }
446:
447:                // check mandatory
448:                if (checkMandatory) {
449:                    Boolean bool = meta.isMandatory();
450:                    if (bool != null && bool.booleanValue()) {
451:                        if (field == null)
452:                            throw new MandatoryFieldException(meta
453:                                    .getLabelToken());
454:                    }
455:                }
456:                return field;
457:            }
458:
459:            /** This method validates the input field value based on the meta data.
460:             * It then returns the parsed object corresponding to the input field.
461:             * @param field the value to validate.
462:             * @param meta the meta object based on which the validation is to be performed.
463:             * @param checkMandatory determines if the mandatory check is to be performed.
464:             * @throws MandatoryFieldException if checkMandatory is true and the field is null.
465:             * @throws TooMuchDataException if the length of the field exceeds the limit specified in the meta data.
466:             * @throws BelowMinimumException if the field has a value less than the minimum value specified in the meta data.
467:             * @throws ExceedsMaximumException if the field has a value greater than the maximum value specified in the meta data.
468:             * @return the parsed object corresponding to the input field.
469:             */
470:            public static Long validate(Long field, IntegerFieldMetaData meta,
471:                    boolean checkMandatory) throws MandatoryFieldException,
472:                    TooMuchDataException, BelowMinimumException,
473:                    ExceedsMaximumException {
474:                if (field != null) {
475:                    // check the intSize/fracSize
476:                    if (!checkLength(new Double(field.doubleValue()), meta
477:                            .getIntSize(), null))
478:                        throw new TooMuchDataException(meta.getLabelToken(),
479:                                new Object[] { meta.getIntSize() });
480:
481:                    // check the min-value
482:                    Long min = meta.getMinValue();
483:                    if (min != null) {
484:                        if (min.compareTo(field) > 0)
485:                            throw new BelowMinimumException(meta
486:                                    .getLabelToken(), new Object[] { meta
487:                                    .getMinValue() });
488:                    }
489:
490:                    // check the max-value
491:                    Long max = meta.getMaxValue();
492:                    if (max != null) {
493:                        if (max.compareTo(field) < 0)
494:                            throw new ExceedsMaximumException(meta
495:                                    .getLabelToken(), new Object[] { meta
496:                                    .getMaxValue() });
497:                    }
498:                }
499:
500:                // check mandatory
501:                if (checkMandatory) {
502:                    Boolean bool = meta.isMandatory();
503:                    if (bool != null && bool.booleanValue()) {
504:                        if (field == null)
505:                            throw new MandatoryFieldException(meta
506:                                    .getLabelToken());
507:                    }
508:                }
509:                return field;
510:            }
511:
512:            /** This method validates the input field value based on the meta data.
513:             * It then returns the parsed object corresponding to the input field.
514:             * @param field the value to validate.
515:             * @param meta the meta object based on which the validation is to be performed.
516:             * @param checkMandatory determines if the mandatory check is to be performed.
517:             * @throws MandatoryFieldException if checkMandatory is true and the field is null.
518:             * @return the parsed object corresponding to the input field.
519:             */
520:            public static byte[] validate(byte[] field, RawFieldMetaData meta,
521:                    boolean checkMandatory) throws MandatoryFieldException {
522:                // check mandatory
523:                if (checkMandatory) {
524:                    Boolean bool = meta.isMandatory();
525:                    if (bool != null && bool.booleanValue()) {
526:                        if (field == null)
527:                            throw new MandatoryFieldException(meta
528:                                    .getLabelToken());
529:                    }
530:                }
531:                return field;
532:            }
533:
534:            // *** PRIVATE METHODS ***
535:
536:            private static String validateString(String field,
537:                    StringFieldMetaData meta, boolean checkMandatory)
538:                    throws MandatoryFieldException, TooMuchDataException,
539:                    TooLittleDataException, PatternMismatchException {
540:                // no need for parsing
541:                return validate(field, meta, checkMandatory);
542:            }
543:
544:            private static Boolean validateBoolean(String field,
545:                    BooleanFieldMetaData meta, boolean checkMandatory)
546:                    throws MandatoryFieldException, PatternMismatchException {
547:                Boolean output = null;
548:                if (field != null && field.length() > 0) {
549:                    // validate pattern
550:                    String pattern = meta.getPattern();
551:                    if (pattern != null) {
552:                        Pattern p = null;
553:                        try {
554:                            // @todo: the pattern can probably be cached
555:                            p = Pattern.compile(pattern,
556:                                    Pattern.CASE_INSENSITIVE);
557:                        } catch (PatternSyntaxException e) {
558:                            // This should never happen
559:                            String str = "Invalid syntax for Regular Expression "
560:                                    + pattern;
561:                            log.error(str, e);
562:                            throw new InvalidPatternRuntimeException(str, e);
563:                        }
564:                        // *** NOTE ***
565:                        // It is assumed that the pattern will be of the type - (a|b|c)|(d|e|f)
566:                        // This means the value in the 1st bracket represents a TRUE
567:                        // & the value in the second bracket means a FALSE
568:                        Matcher m = p.matcher(field);
569:                        if (m.matches()) {
570:                            if (field.equals(m.group(1)))
571:                                output = new Boolean(true);
572:                            else
573:                                output = new Boolean(false);
574:                        } else {
575:                            throw new PatternMismatchException(meta
576:                                    .getLabelToken(), new Object[] { pattern });
577:                        }
578:                    } else
579:                        output = Parser.parseBoolean(field, meta.getLayout());
580:                }
581:                return validate(output, meta, checkMandatory);
582:            }
583:
584:            private static Currency validateCurrency(String field,
585:                    CurrencyFieldMetaData meta, boolean checkMandatory)
586:                    throws MandatoryFieldException, TooMuchDataException,
587:                    BelowMinimumException, ExceedsMaximumException,
588:                    FormatCurrencyException {
589:                Currency output = null;
590:                if (field != null && field.length() > 0) {
591:                    // parse the input
592:                    try {
593:                        output = Parser.parseCurrency(field, meta.getLayout());
594:                    } catch (FormatCurrencyException e) {
595:                        e.setField(meta.getLabelToken());
596:                        throw e;
597:                    }
598:                }
599:                return validate(output, meta, checkMandatory);
600:            }
601:
602:            private static DateOnly validateDateOnly(String field,
603:                    DateOnlyFieldMetaData meta, boolean checkMandatory)
604:                    throws MandatoryFieldException, BelowMinimumException,
605:                    ExceedsMaximumException, FormatDateOnlyException {
606:                DateOnly output = null;
607:                if (field != null && field.length() > 0) {
608:                    // parse the input
609:                    try {
610:                        output = Parser.parseDateOnly(field, meta.getLayout());
611:                    } catch (FormatDateOnlyException e) {
612:                        e.setField(meta.getLabelToken());
613:                        throw e;
614:                    }
615:                }
616:                return validate(output, meta, checkMandatory);
617:            }
618:
619:            private static DateTime validateDateTime(String field,
620:                    DateTimeFieldMetaData meta, boolean checkMandatory)
621:                    throws MandatoryFieldException, BelowMinimumException,
622:                    ExceedsMaximumException, FormatDateTimeException {
623:                DateTime output = null;
624:                if (field != null && field.length() > 0) {
625:                    // parse the input
626:                    try {
627:                        output = Parser.parseDateTime(field, meta.getLayout());
628:                    } catch (FormatDateTimeException e) {
629:                        e.setField(meta.getLabelToken());
630:                        throw e;
631:                    }
632:                }
633:                return validate(output, meta, checkMandatory);
634:            }
635:
636:            private static Double validateDecimal(String field,
637:                    DecimalFieldMetaData meta, boolean checkMandatory)
638:                    throws MandatoryFieldException, TooMuchDataException,
639:                    BelowMinimumException, ExceedsMaximumException,
640:                    FormatDecimalException {
641:                Double output = null;
642:                if (field != null && field.length() > 0) {
643:                    // parse the input
644:                    try {
645:                        output = Parser.parseDecimal(field, meta.getLayout());
646:                    } catch (FormatDecimalException e) {
647:                        e.setField(meta.getLabelToken());
648:                        throw e;
649:                    }
650:                }
651:                return validate(output, meta, checkMandatory);
652:            }
653:
654:            private static Long validateInteger(String field,
655:                    IntegerFieldMetaData meta, boolean checkMandatory)
656:                    throws MandatoryFieldException, TooMuchDataException,
657:                    BelowMinimumException, ExceedsMaximumException,
658:                    FormatIntegerException {
659:                Long output = null;
660:                if (field != null && field.length() > 0) {
661:                    // parse the input
662:                    try {
663:                        output = Parser.parseInteger(field, meta.getLayout());
664:                    } catch (FormatIntegerException e) {
665:                        e.setField(meta.getLabelToken());
666:                        throw e;
667:                    }
668:                }
669:                return validate(output, meta, checkMandatory);
670:            }
671:
672:            private static byte[] validateRaw(String field,
673:                    RawFieldMetaData meta, boolean checkMandatory)
674:                    throws MandatoryFieldException {
675:                byte[] output = null;
676:                if (field != null && field.length() > 0)
677:                    output = Parser.parseRaw(field);
678:                return validate(output, meta, checkMandatory);
679:            }
680:
681:            private static boolean checkLength(Double decimal, Integer intSize,
682:                    Integer fracSize) {
683:                if (decimal != null && (intSize != null || fracSize != null)) {
684:                    double value = Math.abs(decimal.doubleValue());
685:
686:                    if (intSize != null) {
687:                        // any of the 2 alternatives shown here, work !!!
688:                        double intLimit = Math.pow(10, intSize.intValue());
689:                        if ((long) value >= (long) intLimit)
690:                            return false;
691:                        //String intString = "" + (long) value;
692:                        //if ( !doCheck( intString, intSize.intValue() ) ) return false;
693:                    }
694:
695:                    if (fracSize != null) {
696:                        /** @todo */
697:                        // should find a much more efficient way of finding the no. of fractional digits
698:                        StringBuffer buf = new StringBuffer();
699:                        FieldPosition fp = new FieldPosition(
700:                                NumberFormat.FRACTION_FIELD);
701:                        NumberFormat df = NumberFormat.getNumberInstance();
702:                        df.setGroupingUsed(false);
703:                        df.setMaximumFractionDigits(20); // THIS SHOULD BE SUFFICIENT
704:                        df.format(value, buf, fp);
705:                        String fracString = buf.substring(fp.getBeginIndex(),
706:                                fp.getEndIndex());
707:                        if (!doCheck(fracString, fracSize.intValue()))
708:                            return false;
709:                    }
710:                }
711:                return true;
712:            }
713:
714:            private static boolean doCheck(String input, int length) {
715:                if (input == null || input.length() == 0)
716:                    return true;
717:                else {
718:                    return input.length() > length ? false : true;
719:                }
720:            }
721:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.