jvalid-checkins Mailing List for Java Validation Library
Status: Alpha
Brought to you by:
mikewill_1998
You can subscribe to this list here.
| 2001 |
Jan
|
Feb
|
Mar
(11) |
Apr
|
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
|---|
|
From: Mike W. <mik...@us...> - 2001-03-29 10:26:38
|
Update of /cvsroot/jvalid/jvalid
In directory usw-pr-cvs1:/tmp/cvs-serv1412
Modified Files:
build.xml
Log Message:
Included LICENSE-JDOM and CHANGES.
Also modified to build the jvaild jar file without the version number.
Index: build.xml
===================================================================
RCS file: /cvsroot/jvalid/jvalid/build.xml,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -r1.4 -r1.5
*** build.xml 2001/03/22 03:31:30 1.4
--- build.xml 2001/03/29 10:26:33 1.5
***************
*** 7,11 ****
<property name="projname" value="jvalid" />
<property name="packages" value="net.sourceforge.jvalid" />
! <property name="version" value="0.1" />
<property name="year" value="2001" />
--- 7,11 ----
<property name="projname" value="jvalid" />
<property name="packages" value="net.sourceforge.jvalid" />
! <property name="version" value="0.2" />
<property name="year" value="2001" />
***************
*** 59,66 ****
--- 59,74 ----
<fileset dir="${basedir}" includes="LICENSE*"/>
</copy>
+
+ <copy todir="${dist}">
+ <fileset dir="${basedir}" includes="LICENSE-JDOM"/>
+ </copy>
<copy todir="${dist}">
<fileset dir="${basedir}" includes="README"/>
</copy>
+
+ <copy todir="${dist}">
+ <fileset dir="${basedir}" includes="CHANGES"/>
+ </copy>
<copy todir="${dist}">
***************
*** 76,80 ****
</copy>
! <jar jarfile="${dist}/lib/${projname}-${version}.jar"
basedir="${build}"
includes="net/**"/>
--- 84,88 ----
</copy>
! <jar jarfile="${dist}/lib/${projname}.jar"
basedir="${build}"
includes="net/**"/>
|
|
From: Mike W. <mik...@us...> - 2001-03-29 10:25:22
|
Update of /cvsroot/jvalid/jvalid
In directory usw-pr-cvs1:/tmp/cvs-serv1266
Modified Files:
CHANGES
Log Message:
Reorganized to have the latest versions notes first.
Index: CHANGES
===================================================================
RCS file: /cvsroot/jvalid/jvalid/CHANGES,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** CHANGES 2001/03/29 03:08:23 1.1
--- CHANGES 2001/03/29 10:25:18 1.2
***************
*** 5,19 ****
- Version 0.1
- ===========================
-
- Initial release.
-
-
Version 0.2
===========================
* Some code optimization in the Validator class.
! Found that I was creating a lot unecessary temporary objects in when
checking data types. They now use the static methods of their respective
wrapper class.
--- 5,13 ----
Version 0.2
===========================
* Some code optimization in the Validator class.
! Found that I was creating a lot unecessary temporary objects when
checking data types. They now use the static methods of their respective
wrapper class.
***************
*** 38,41 ****
* The jvalid jar file no longer includes the version number. (i.e. jvalid.jar not jvalid-?.?.jar )
! * Created this file :)
\ No newline at end of file
--- 32,41 ----
* The jvalid jar file no longer includes the version number. (i.e. jvalid.jar not jvalid-?.?.jar )
+
+ * Created this file :)
+
+
+ Version 0.1
+ ===========================
! Initial release.
\ No newline at end of file
|
|
From: Mike W. <mik...@us...> - 2001-03-29 03:08:27
|
Update of /cvsroot/jvalid/jvalid In directory usw-pr-cvs1:/tmp/cvs-serv14038 Added Files: CHANGES Log Message: Version history. --- NEW FILE --- Changes =========================== Below you will find the history of changes to JValid. Version 0.1 =========================== Initial release. Version 0.2 =========================== * Some code optimization in the Validator class. Found that I was creating a lot unecessary temporary objects in when checking data types. They now use the static methods of their respective wrapper class. * New method added to the Validator class, getObject( "<Contraint_Name>", <Data_To_Check> ). This method will return an instance of the appropriate wrapper class to represent the data validated. It will throw a DataValidationException if any validation errors occur. You can retrieve a List of the errors by calling getErrorList(). This method is meant to provide a convient way to perform validation and retrieve the data in a usable format. * New class DataValidationException which is thrown by the getObject() method in Validator on validation errors. * Rewrote the ValidatorTest example so that hopefully the output is more useful. Also added examples of how to use the new getObject() method in Validator. * Tested JValid with JDOM Beta 6 and Xerces 1.3. It worked! :) * Updated the readme to reflect the above. * The jvalid jar file no longer includes the version number. (i.e. jvalid.jar not jvalid-?.?.jar ) * Created this file :) |
|
From: Mike W. <mik...@us...> - 2001-03-29 03:06:09
|
Update of /cvsroot/jvalid/jvalid In directory usw-pr-cvs1:/tmp/cvs-serv13836 Modified Files: README Log Message: Updated to reflect compatibilty with JDOM beta 6 and Xerces 1.3. Index: README =================================================================== RCS file: /cvsroot/jvalid/jvalid/README,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -r1.3 -r1.4 *** README 2001/03/11 14:39:12 1.3 --- README 2001/03/29 03:06:06 1.4 *************** *** 30,34 **** JDom is required for building and running the JValid you can get it at http://www.jdom.org. ! This version of JValid was built and tested with JDom beta 5. It may or may not work with other versions. I plan on trying to keep up with the latest mile stone releases. --- 30,34 ---- JDom is required for building and running the JValid you can get it at http://www.jdom.org. ! This version of JValid was built and tested with JDom beta 6 and Xerces 1.3. It may or may not work with other versions. I plan on trying to keep up with the latest mile stone releases. |
|
From: Mike W. <mik...@us...> - 2001-03-29 02:43:06
|
Update of /cvsroot/jvalid/jvalid/src/net/sourceforge/jvalid In directory usw-pr-cvs1:/tmp/cvs-serv12047 Modified Files: DataValidationException.java Log Message: Removed stray cvs log comment. Index: DataValidationException.java =================================================================== RCS file: /cvsroot/jvalid/jvalid/src/net/sourceforge/jvalid/DataValidationException.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -r1.2 -r1.3 *** DataValidationException.java 2001/03/29 02:35:24 1.2 --- DataValidationException.java 2001/03/29 02:43:02 1.3 *************** *** 45,48 **** --- 45,51 ---- * $Header$ * $Log$ + * Revision 1.3 2001/03/29 02:43:02 mikewill_1998 + * Removed stray cvs log comment. + * * Revision 1.2 2001/03/29 02:35:24 mikewill_1998 * Added 2 new methods: *************** *** 53,61 **** * New exception that is thrown by the getObject() method in the Validator * class when validation errors occur. - * - * Revision 1.1 2001/03/15 00:39:47 mikewill_1998 - * This exception is thrown if an unkown data type is passed to the toDouble() method in DataConverter. - * Missed adding it the first time. Bad developer!!! ;) - * * */ --- 56,59 ---- |
|
From: Mike W. <mik...@us...> - 2001-03-29 02:37:30
|
Update of /cvsroot/jvalid/jvalid/src/net/sourceforge/jvalid/examples In directory usw-pr-cvs1:/tmp/cvs-serv11610 Modified Files: ValidatorTest.java Log Message: Basically rewrote the example so that hopefully the output makes more sense. Also added code to demonstrate the new getObject() method available in the Validator class. Index: ValidatorTest.java =================================================================== RCS file: /cvsroot/jvalid/jvalid/src/net/sourceforge/jvalid/examples/ValidatorTest.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -r1.3 -r1.4 *** ValidatorTest.java 2001/03/02 03:47:02 1.3 --- ValidatorTest.java 2001/03/29 02:37:26 1.4 *************** *** 1,138 **** ! /*------------------------------------------------------------------------------------------ ! * First let me give credit where credit is due! ! * ! * This package is based on the orginal work of Brett McLaughlin in the Javaworld article ! * 'Validation with Java and XML schema, Part 1' which can be found at ! * http://www.javaworld.com/javaworld/jw-09-2000/jw-0908-validation.html ! * ! * ! * Major modifications are noted in the class JavaDoc comment or in the CVS change log. ! *------------------------------------------------------------------------------------------ ! * ! * Legal Stuff ! * ! * JValid is Open Source. ! * ! * Copyright (c) 2000 Mike Williams. All rights reserved. ! * ! * Redistribution and use in source and binary forms, with or without ! * modification, are permitted under the terms of the ! * GNU LESSER GENERAL PUBLIC LICENSE version 2.1 or later. ! * ! * You should have received a copy the license in this distribution. ! * ! * This product includes software developed by the JDOM Project ! * (http://www.jdom.org). Please see LICENSE-JDOM for details. ! * ! * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED ! * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES ! * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE ! * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, ! * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ! * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE ! * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS ! * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER ! * IN CONTRACT, STRICT LIABILITY, OR TORT ! * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE ! * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH ! * DAMAGE. ! * ! *------------------------------------------------------------------- ! * C H A N G E L O G ! *------------------------------------------------------------------- ! * $Header$ * $Log$ * Revision 1.3 2001/03/02 03:47:02 mikewill_1998 * Added date validation examples. ! * ! * Revision 1.2 2001/02/25 17:50:04 mikewill_1998 ! * Updated JavaDoc comments. ! * Added acknowledgements for JDOM and Apache. ! * ! * Revision 1.1.1.1 2001/02/22 11:30:38 mikewill_1998 ! * Initial Import ! * ! */ ! ! package net.sourceforge.jvalid.examples; ! ! import java.io.File; ! import java.io.IOException; ! import java.net.URL; ! import java.util.LinkedList; ! import java.util.List; ! import java.util.Iterator; ! ! ! import net.sourceforge.jvalid.Validator; ! ! ! /** ! * <p>Test class for the Java Validation Library.</p> ! * ! * @author Mike Williams ! */ ! public class ValidatorTest ! { ! ! /** ! * <p>Public interface. Used to run the example.</p> ! * ! * @param args Location of the buyShoes.xsd example configuration file. ! */ ! public static void main(String[] args) ! { ! try ! { ! if (args.length != 1) ! { ! System.out.println("Usage: java ValidatorTest [schema URL]"); ! return; ! } ! ! // Create a valiator ! File schemaFile = new File(args[0]); ! Validator validator = Validator.getInstance(schemaFile.toURL()); ! ! // Create some data ! String badDate1 = "02/29/2001"; ! String badDate2 = "02/01/2001"; ! String badDate3 = "52/01/2001"; ! String dateNeeded = "02/28/2001"; ! ! String shoeSize = "13"; ! String width = "F"; ! String brand = "V-Form"; ! String numEyelets = "@2"; ! ! StringBuffer errorBuffer = new StringBuffer(); ! LinkedList errorList = new LinkedList(); ! ! // Validate the data. ! errorList.addAll( validator.checkValidity("dateNeeded", badDate1) ); ! errorList.addAll( validator.checkValidity("dateNeeded", badDate2) ); ! errorList.addAll( validator.checkValidity("dateNeeded", badDate3) ); ! errorList.addAll( validator.checkValidity("dateNeeded", dateNeeded) ); ! errorList.addAll( validator.checkValidity("shoeSize", shoeSize) ); ! errorList.addAll( validator.checkValidity("width", width) ); ! errorList.addAll( validator.checkValidity("brand", brand) ); ! errorList.addAll( validator.checkValidity("numEyelets", numEyelets) ); ! ! Iterator i = errorList.iterator(); ! ! while ( i.hasNext() ) ! { ! errorBuffer.append( i.next() ); ! errorBuffer.append( "\n" ); ! } ! ! String errorMessage = errorBuffer.toString(); ! System.out.println("Error Message: [" + errorMessage + "]"); ! ! } ! catch (Exception e) ! { ! e.printStackTrace(); ! } ! } } --- 1,290 ---- ! /*------------------------------------------------------------------------------------------ ! * First let me give credit where credit is due! ! * ! * This package is based on the orginal work of Brett McLaughlin in the Javaworld article ! * 'Validation with Java and XML schema, Part 1' which can be found at ! * http://www.javaworld.com/javaworld/jw-09-2000/jw-0908-validation.html ! * ! * ! * Major modifications are noted in the class JavaDoc comment or in the CVS change log. ! *------------------------------------------------------------------------------------------ ! * ! * Legal Stuff ! * ! * JValid is Open Source. ! * ! * Copyright (c) 2000 Mike Williams. All rights reserved. ! * ! * Redistribution and use in source and binary forms, with or without ! * modification, are permitted under the terms of the ! * GNU LESSER GENERAL PUBLIC LICENSE version 2.1 or later. ! * ! * You should have received a copy the license in this distribution. ! * ! * This product includes software developed by the JDOM Project ! * (http://www.jdom.org). Please see LICENSE-JDOM for details. ! * ! * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED ! * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES ! * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE ! * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, ! * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ! * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE ! * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS ! * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER ! * IN CONTRACT, STRICT LIABILITY, OR TORT ! * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE ! * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH ! * DAMAGE. ! * ! *------------------------------------------------------------------- ! * C H A N G E L O G ! *------------------------------------------------------------------- ! * $Header$ * $Log$ + * Revision 1.4 2001/03/29 02:37:26 mikewill_1998 + * Basically rewrote the example so that hopefully the output makes more sense. + * Also added code to demonstrate the new getObject() method available in the Validator class. + * * Revision 1.3 2001/03/02 03:47:02 mikewill_1998 * Added date validation examples. ! * ! * Revision 1.2 2001/02/25 17:50:04 mikewill_1998 ! * Updated JavaDoc comments. ! * Added acknowledgements for JDOM and Apache. ! * ! * Revision 1.1.1.1 2001/02/22 11:30:38 mikewill_1998 ! * Initial Import ! * ! */ ! ! package net.sourceforge.jvalid.examples; ! ! import java.io.File; ! import java.io.IOException; ! import java.net.URL; ! import java.util.LinkedList; ! import java.util.List; ! import java.util.Iterator; ! import java.lang.StringBuffer; ! import java.util.Date; ! ! ! import net.sourceforge.jvalid.Validator; ! import net.sourceforge.jvalid.DataValidationException; ! ! ! /** ! * <p>Test class for the Java Validation Library.</p> ! * ! * @author Mike Williams ! */ ! public class ValidatorTest ! { ! ! /** ! * <p>Public interface. Used to run the example.</p> ! * ! * @param args Location of the buyShoes.xsd example configuration file. ! */ ! public static void main(String[] args) ! { ! if (args.length != 1) ! { ! System.out.println("Usage: java ValidatorTest [schema URL]"); ! return; ! } ! ! File schemaFile = null; ! Validator validator = null; ! ! try ! { ! // Create a valiator ! schemaFile = new File(args[0]); ! validator = Validator.getInstance(schemaFile.toURL()); ! } ! catch (IOException ioe) ! { ! System.out.println( "Unable to find contraints file " + args[0] + "." ); ! return; ! } ! ! ! StringBuffer errorBuffer = new StringBuffer( "Testing checkValidity()...\n" ); ! errorBuffer.append( "----------------------------------------\n" ); ! errorBuffer.append( "This test calls the checkValidity() method of the Validator class.\n\n" ); ! ! LinkedList errorList = new LinkedList(); ! ! ! // Create some data ! String badDate1 = "02/29/2001"; ! String badDate2 = "02/01/2001"; ! String badDate3 = "52/01/2001"; ! String dateNeeded = "02/28/2001"; ! ! String shoeSize = "13"; ! String width = "F"; ! String brand = "V-Form"; ! String numEyelets = "@2"; ! ! errorBuffer.append( "Test Data: \n" ) ! .append( "Date test 1 (bad) : " ).append( badDate1 ).append( "\n" ) ! .append( "Date test 2 (bad) : " ).append( badDate2 ).append( "\n" ) ! .append( "Date test 3 (bad) : " ).append( badDate3 ).append( "\n" ) ! .append( "Date test 4 (good) : " ).append( dateNeeded ).append( "\n" ) ! .append( "Shoe size (good) : " ).append( shoeSize ).append( "\n" ) ! .append( "Width (bad) : " ).append( width ).append( "\n" ) ! .append( "Brand (good) : " ).append( brand ).append( "\n" ) ! .append( "Number of Eyelets (bad) : " ).append( numEyelets ).append( "\n" ); ! ! ! // Validate the data. ! errorList.addAll( validator.checkValidity("dateNeeded", badDate1) ); ! errorList.addAll( validator.checkValidity("dateNeeded", badDate2) ); ! errorList.addAll( validator.checkValidity("dateNeeded", badDate3) ); ! errorList.addAll( validator.checkValidity("dateNeeded", dateNeeded) ); ! errorList.addAll( validator.checkValidity("shoeSize", shoeSize) ); ! errorList.addAll( validator.checkValidity("width", width) ); ! errorList.addAll( validator.checkValidity("brand", brand) ); ! errorList.addAll( validator.checkValidity("numEyelets", numEyelets) ); ! ! errorBuffer.append( "\nErrors: " ); ! ! Iterator i = errorList.iterator(); ! ! while ( i.hasNext() ) ! { ! errorBuffer.append( "\n" ) ! .append( i.next() ); ! } ! ! errorBuffer.append( "\n\nEnd checkValidity() test.\n" ) ! .append( "----------------------------------------\n\n" ); ! ! System.out.println( errorBuffer.toString() ); ! ! ! // Get ready for the next test. ! errorList.clear(); ! errorBuffer.delete( 0, errorBuffer.length() ); ! ! errorBuffer.append( "Testing getObject()...\n" ) ! .append( "----------------------------------------\n\n" ); ! ! errorBuffer.append( "Test Data: \n" ) ! .append( "Date test 1 (bad) : " ).append( badDate1 ).append( "\n" ) ! .append( " Expecting Date\n" ) ! .append( "Date test 2 (bad) : " ).append( badDate2 ).append( "\n" ) ! .append( " Expecting Date\n" ) ! .append( "Date test 3 (bad) : " ).append( badDate3 ).append( "\n" ) ! .append( " Expecting Date\n" ) ! .append( "Date test 4 (good) : " ).append( dateNeeded ).append( "\n" ) ! .append( " Expecting Date\n" ) ! .append( "Shoe size (good) : " ).append( shoeSize ).append( "\n" ) ! .append( " Expecting Integer\n" ) ! .append( "Width (bad) : " ).append( width ).append( "\n" ) ! .append( " Expecting String\n" ) ! .append( "Brand (good) : " ).append( brand ).append( "\n" ) ! .append( " Expecting String\n" ) ! .append( "Number of Eyelets (bad) : " ).append( numEyelets ).append( "\n" ) ! .append( " Expecting Integer\n" ) ! .append( "\n\nResults:" ); ! ! try ! { ! Date tempDate = (Date) validator.getObject( "dateNeeded", badDate1 ); ! errorBuffer.append( "\nTest Date 1: Got a date!" ); ! } ! catch ( DataValidationException dve ) ! { ! errorList.addAll( dve.getErrorList() ); ! } ! ! try ! { ! Date tempDate = (Date) validator.getObject( "dateNeeded", badDate2 ); ! errorBuffer.append( "\nTest Date 2: Got a date!" ); ! } ! catch ( DataValidationException dve ) ! { ! errorList.addAll( dve.getErrorList() ); ! } ! ! try ! { ! Date tempDate = (Date) validator.getObject( "dateNeeded", badDate3 ); ! errorBuffer.append( "\nTest Date 3: Got a date!" ); ! } ! catch ( DataValidationException dve ) ! { ! errorList.addAll( dve.getErrorList() ); ! } ! ! try ! { ! Date tempDate = (Date) validator.getObject( "dateNeeded", dateNeeded ); ! errorBuffer.append( "\nTest Date 4: Got a date!" ); ! } ! catch ( DataValidationException dve ) ! { ! errorList.addAll( dve.getErrorList() ); ! } ! ! try ! { ! Integer tempInt = (Integer) validator.getObject( "shoeSize", shoeSize ); ! errorBuffer.append( "\nShoe size: Got a Integer!" ); ! } ! catch ( DataValidationException dve ) ! { ! errorList.addAll( dve.getErrorList() ); ! } ! ! try ! { ! String tempInt = (String) validator.getObject( "width", width ); ! errorBuffer.append( "\nWidth: Got a String!" ); ! } ! catch ( DataValidationException dve ) ! { ! errorList.addAll( dve.getErrorList() ); ! } ! ! try ! { ! String tempString = (String) validator.getObject( "brand", brand ); ! errorBuffer.append( "\nBrand: Got a String!" ); ! } ! catch ( DataValidationException dve ) ! { ! errorList.addAll( dve.getErrorList() ); ! } ! ! try ! { ! Integer tempInt = (Integer) validator.getObject( "numEyelets", numEyelets ); ! errorBuffer.append( "\nNumber of Eyelets: Got a String!" ); ! } ! catch ( DataValidationException dve ) ! { ! errorList.addAll( dve.getErrorList() ); ! } ! ! errorBuffer.append( "\n\nErrors: " ); ! ! i = errorList.iterator(); ! ! while ( i.hasNext() ) ! { ! errorBuffer.append( "\n" ) ! .append( i.next() ); ! } ! ! errorBuffer.append( "\n\nEnd getObject() test.\n" ) ! .append( "----------------------------------------\n" ); ! ! System.out.println( errorBuffer.toString() ); ! } } |
|
From: Mike W. <mik...@us...> - 2001-03-29 02:35:28
|
Update of /cvsroot/jvalid/jvalid/src/net/sourceforge/jvalid
In directory usw-pr-cvs1:/tmp/cvs-serv11405
Modified Files:
DataValidationException.java
Log Message:
Added 2 new methods:
getErrorList() returns a List containing any validation errors.
getErrors() returns an Iterator for navigating through the list.
Index: DataValidationException.java
===================================================================
RCS file: /cvsroot/jvalid/jvalid/src/net/sourceforge/jvalid/DataValidationException.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** DataValidationException.java 2001/03/27 10:48:22 1.1
--- DataValidationException.java 2001/03/29 02:35:24 1.2
***************
*** 45,48 ****
--- 45,53 ----
* $Header$
* $Log$
+ * Revision 1.2 2001/03/29 02:35:24 mikewill_1998
+ * Added 2 new methods:
+ * getErrorList() returns a List containing any validation errors.
+ * getErrors() returns an Iterator for navigating through the list.
+ *
* Revision 1.1 2001/03/27 10:48:22 mikewill_1998
* New exception that is thrown by the getObject() method in the Validator
***************
*** 142,145 ****
--- 147,170 ----
return sb.toString();
+ }
+
+ /**
+ * Returns the error list.
+ *
+ * @return <code>List</code> containing any validation errors.
+ */
+ public List getErrorList()
+ {
+ return mErrorList;
+ }
+
+ /**
+ * Returns an iterator for moving through the List of validation errors.
+ *
+ * @return <code>Iterator</code> for moving through the <code>List</code> of validation errors.
+ */
+ public Iterator getErrors()
+ {
+ return mErrorList.iterator();
}
}
|
|
From: Mike W. <mik...@us...> - 2001-03-27 10:48:25
|
Update of /cvsroot/jvalid/jvalid/src/net/sourceforge/jvalid In directory usw-pr-cvs1:/tmp/cvs-serv16627 Added Files: DataValidationException.java Log Message: New exception that is thrown by the getObject() method in the Validator class when validation errors occur. --- NEW FILE --- /*------------------------------------------------------------------------------------------ * First let me give credit where credit is due! * * This package is based on the orginal work of Brett McLaughlin in the Javaworld article * 'Validation with Java and XML schema, Part 1' which can be found at * http://www.javaworld.com/javaworld/jw-09-2000/jw-0908-validation.html * * * Major modifications are noted in the JavaDoc comment or in the CVS change log. *------------------------------------------------------------------------------------------ * * Legal Stuff * * JValid is Open Source. * * Copyright (c) 2000 Mike Williams. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted under the terms of the * GNU LESSER GENERAL PUBLIC LICENSE version 2.1 or later. * * You should have received a copy the license in this distribution. * * This product includes software developed by the JDOM Project * (http://www.jdom.org). Please see LICENSE-JDOM for details. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER * IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. * *------------------------------------------------------------------- * C H A N G E L O G *------------------------------------------------------------------- * Created on February 25, 2001, 11:13 PM * * $Header: /cvsroot/jvalid/jvalid/src/net/sourceforge/jvalid/DataValidationException.java,v 1.1 2001/03/27 10:48:22 mikewill_1998 Exp $ * $Log: DataValidationException.java,v $ * Revision 1.1 2001/03/27 10:48:22 mikewill_1998 * New exception that is thrown by the getObject() method in the Validator * class when validation errors occur. * * Revision 1.1 2001/03/15 00:39:47 mikewill_1998 * This exception is thrown if an unkown data type is passed to the toDouble() method in DataConverter. * Missed adding it the first time. Bad developer!!! ;) * * */ package net.sourceforge.jvalid; import java.lang.Exception; import java.lang.StringBuffer; import java.util.List; import java.util.LinkedList; import java.util.Iterator; /** * <p> * An Exception subclass. Note that methods throwing an Exception have to * declare this by using the "throws DataValidationException" keyword. * </p> * * This exception provides access to a List containing any validation * errors encountered. * * @author Mike Williams * @version 1.0 */ public class DataValidationException extends Exception { /** * List containing all errors associated with this exception. */ private List mErrorList; /** * Default constructor. */ private DataValidationException() { super(); } /** * Construct an exception having a Message String<br> * The message is printed to System.out when printStackTrace() is called * * @param <code>String</code> message providing details about the Exception. */ public DataValidationException( String message ) { this(); mErrorList = new LinkedList(); mErrorList.add( message ); } /** * Construct an exception having a List of errors. * You can retrieve this list by calling getErrors(). * The getMessage() method will build a String from this * list. * * @param <code>List</code> errorList providing all errors associated with * the Exception. */ public DataValidationException( List errorList ) { this.mErrorList = errorList; } /** * Overridden to return the contents of the ErrorList. * * @return <code>String</code> contents of the ErrorList. */ public String getMessage() { Iterator i = mErrorList.iterator(); StringBuffer sb = new StringBuffer(); while ( i.hasNext() ) { sb.append( (String) i.next() + "\n" ); } if ( sb.length() >= 2 ) { sb.setLength( sb.length() - 2 ); } return sb.toString(); } } |
|
From: Mike W. <mik...@us...> - 2001-03-27 10:46:53
|
Update of /cvsroot/jvalid/jvalid/src/net/sourceforge/jvalid
In directory usw-pr-cvs1:/tmp/cvs-serv16403
Modified Files:
Validator.java
Log Message:
Cleaned up the code a little.
Added new method getObject() that will return the value of a variable in its
native Java format if it passes validation. A DataValidationException is thrown
if validation fails.
Index: Validator.java
===================================================================
RCS file: /cvsroot/jvalid/jvalid/src/net/sourceforge/jvalid/Validator.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -r1.4 -r1.5
*** Validator.java 2001/03/22 11:24:15 1.4
--- Validator.java 2001/03/27 10:46:48 1.5
***************
*** 43,46 ****
--- 43,52 ----
* $Header$
* $Log$
+ * Revision 1.5 2001/03/27 10:46:48 mikewill_1998
+ * Cleaned up the code a little.
+ * Added new method getObject() that will return the value of a variable in its
+ * native Java format if it passes validation. A DataValidationException is thrown
+ * if validation fails.
+ *
* Revision 1.4 2001/03/22 11:24:15 mikewill_1998
* Updated JavaDoc comments on checkValidation().
***************
*** 72,76 ****
import java.util.Iterator;
- import net.sourceforge.jvalid.UnsupportedDataTypeException;
/**
--- 78,81 ----
***************
*** 285,372 ****
/**
* <p>
! * This will test the supplied data to see if it can be converted to the
! * Java data type given in <code>dataType</code>.
* </p>
*
! * @param data <code>String</code> to test data type of.
! * @param dataType <code>String</code> name of Java class to convert to.
! * @return <code>boolean</code> - whether conversion can occur.
*/
! private boolean correctDataType(String data, String dataType)
! {
! if ((dataType.equals("String")) || (dataType.equals("java.lang.String")))
{
! return true;
}
! if ((dataType.equals("int")) || (dataType.equals("java.lang.Integer")))
{
try
{
! Integer test = new Integer(data);
! return true;
}
catch (NumberFormatException e)
{
! return false;
}
}
! if ((dataType.equals("long")) || (dataType.equals("java.lang.Long")))
{
try
{
! Long test = new Long(data);
! return true;
}
catch (NumberFormatException e)
{
! return false;
}
}
! if ((dataType.equals("float")) || (dataType.equals("java.lang.Float")))
{
try
{
! Float test = new Float(data);
! return true;
}
catch (NumberFormatException e)
{
! return false;
}
}
! if ((dataType.equals("double")) || (dataType.equals("java.lang.Double")))
{
try
{
! Double test = new Double(data);
! return true;
}
catch (NumberFormatException e)
{
! return false;
}
}
! if (dataType.equals("java.lang.Boolean"))
{
! if ((data.equalsIgnoreCase("true")) ||
! (data.equalsIgnoreCase("false")) ||
! (data.equalsIgnoreCase("yes")) ||
! (data.equalsIgnoreCase("no")))
{
! return true;
}
else
{
! return false;
}
}
!
! // Check for date.
! if ( dataType.equals ( "date" ) || dataType.equals( "java.lang.Date" ) )
{
try
--- 290,421 ----
/**
* <p>
! * This method will return an object representing the data in it's primative or
! * native format. The primative values will be represented by their corresponding
! * wrapper classes (i.e boolean = Boolean, int = Integer, etc...).
! * Dates will of course be represented by a Date object.
* </p>
+ * <strong>Note:</strong> If the named contraint doesn't exist a DataValidationException will be
+ * thrown. This is in order to avoid ClassCastExceptions since the checkValidity method treats
+ * this condition as valid.
*
! * @param constraintName the identifier in the constraints to validate this data against.
! * @param data <code>String</code> data to validate.
! * @return <code>Object</code> representing the value in it's primative or native format.
! * @throws <code>DataValidationException</code> if the value has any validation errors.
*/
! public Object getObject( String constraintName, String value )
! throws DataValidationException
! {
!
! // Make sure the value is valid.
! List errorList = checkValidity( constraintName, value );
!
! if ( !errorList.isEmpty() )
{
! throw new DataValidationException( errorList );
}
+
+ Object o = constraints.get( constraintName );
+
+ // Constraint not found.
+ if ( o == null )
+ {
+ throw new DataValidationException( "Constraint " + constraintName + " not found." );
+ }
+
+ String dataType = ( ( Constraint ) o ).getDataType();
+
+ return toObject( value, dataType );
+ }
!
! /**
! * <p>
! * This method will return an object representing the data in it's primitive format.
! * null is returned if the data can't be converted to the specified type.
! * </p>
! *
! * @param data <code>String</code> to convert.
! * @param dataType <code>String</code> name of the Java type to convert to.
! * @return <code>Object</code> representing the converted data.
! */
! private Object toObject( String data, String dataType )
! {
! if ( dataType.equals("String") || dataType.equals("java.lang.String") )
{
+ return data;
+ }
+
+ if ( dataType.equals("int") || dataType.equals("java.lang.Integer") )
+ {
try
{
! Integer object = Integer.valueOf( data );
! return object;
}
catch (NumberFormatException e)
{
! return null;
}
}
! if ( dataType.equals("long") || dataType.equals("java.lang.Long") )
{
try
{
! Long object = Long.valueOf( data );
! return object;
}
catch (NumberFormatException e)
{
! return null;
}
}
! if ( dataType.equals("float") || dataType.equals("java.lang.Float") )
{
try
{
! Float object = Float.valueOf( data );
! return object;
}
catch (NumberFormatException e)
{
! return null;
}
}
! if ( dataType.equals("double") || dataType.equals("java.lang.Double") )
{
try
{
! Double object = Double.valueOf( data );
! return object;
}
catch (NumberFormatException e)
{
! return null;
}
}
! if ( dataType.equals("java.lang.Boolean") || dataType.equals( "boolean" ) )
{
! if ( data.equalsIgnoreCase("true") || data.equalsIgnoreCase("yes") )
{
! Boolean object = Boolean.valueOf( "true" );
! return object;
}
+ else if ( data.equalsIgnoreCase("false") || data.equalsIgnoreCase("no") )
+ {
+ Boolean object = Boolean.valueOf( "false" );
+ return object;
+ }
else
{
! return null;
}
}
!
! if ( dataType.equals( "java.util.Date" ) || dataType.equals( "date" ) )
{
try
***************
*** 374,386 ****
DateFormat df = DateFormat.getDateInstance( DateFormat.SHORT );
df.setLenient( false );
! Date date = df.parse( data );
}
catch ( ParseException pe )
{
! return false;
}
}
!
! return true;
}
--- 423,455 ----
DateFormat df = DateFormat.getDateInstance( DateFormat.SHORT );
df.setLenient( false );
!
! // Convert the string to date.
! Date object = df.parse( data );
! return object;
}
catch ( ParseException pe )
{
! return null;
!
}
+
}
! return null;
! }
!
! /**
! * <p>
! * This will test the supplied data to see if it can be converted to the
! * Java data type given in <code>dataType</code>.
! * </p>
! *
! * @param data <code>String</code> to test data type of.
! * @param dataType <code>String</code> name of Java class to convert to.
! * @return <code>boolean</code> - whether conversion can occur.
! */
! private boolean correctDataType(String data, String dataType)
! {
! Object object = toObject( data, dataType );
! return ( object != null );
}
|
|
From: Mike W. <mik...@us...> - 2001-03-22 11:24:19
|
Update of /cvsroot/jvalid/jvalid/src/net/sourceforge/jvalid In directory usw-pr-cvs1:/tmp/cvs-serv24200 Modified Files: Validator.java Log Message: Updated JavaDoc comments on checkValidation(). Index: Validator.java =================================================================== RCS file: /cvsroot/jvalid/jvalid/src/net/sourceforge/jvalid/Validator.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -r1.3 -r1.4 *** Validator.java 2001/03/02 03:46:15 1.3 --- Validator.java 2001/03/22 11:24:15 1.4 *************** *** 1,406 **** ! /*------------------------------------------------------------------------------------------ ! * First let me give credit where credit is due! ! * ! * This package is based on the orginal work of Brett McLaughlin in the Javaworld article ! * 'Validation with Java and XML schema, Part 1' which can be found at ! * http://www.javaworld.com/javaworld/jw-09-2000/jw-0908-validation.html ! * ! * ! * Major modifications are noted in the class JavaDoc comment or in the CVS change log. ! *------------------------------------------------------------------------------------------ ! * ! * Legal Stuff ! * ! * JValid is Open Source. ! * ! * Copyright (c) 2000 Mike Williams. All rights reserved. ! * ! * Redistribution and use in source and binary forms, with or without ! * modification, are permitted under the terms of the ! * GNU LESSER GENERAL PUBLIC LICENSE version 2.1 or later. ! * ! * You should have received a copy the license in this distribution. ! * ! * This product includes software developed by the JDOM Project ! * (http://www.jdom.org). Please see LICENSE-JDOM for details. ! * ! * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED ! * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES ! * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE ! * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, ! * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ! * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE ! * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS ! * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER ! * IN CONTRACT, STRICT LIABILITY, OR TORT ! * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE ! * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH ! * DAMAGE. ! * ! *------------------------------------------------------------------- ! * C H A N G E L O G ! *------------------------------------------------------------------- ! * $Header$ * $Log$ * Revision 1.3 2001/03/02 03:46:15 mikewill_1998 * Added support for date validation. ! * ! * Revision 1.2 2001/02/25 17:42:18 mikewill_1998 ! * Updated JavaDoc comments. ! * Added acknowledgements for JDOM and Apache. ! * ! * Revision 1.1.1.1 2001/02/22 11:30:33 mikewill_1998 ! * Initial Import ! * ! */ ! ! package net.sourceforge.jvalid; ! ! import java.io.IOException; ! import java.net.URL; ! import java.util.HashMap; ! import java.util.Iterator; ! import java.util.List; ! import java.util.Map; ! import java.util.Date; ! import java.util.LinkedList; ! import java.text.DateFormat; ! import java.text.ParseException; ! import java.util.Iterator; ! ! import net.sourceforge.jvalid.UnsupportedDataTypeException; ! ! /** ! * <p> ! * The <code>Validator</code> class allows an application component or client to ! * provide data, and determine if the data is valid for the requested type. ! * </p> ! * ! * @author Mike Williams ! */ ! public class Validator ! { ! ! /** The instances of this class for use (singleton design pattern) */ ! private static Map instances = null; ! ! /** The URL of the XML Schema for this <code>Validator</code> */ ! private URL schemaURL; ! ! /** The constraints for this XML Schema */ ! private Map constraints; ! ! /** ! * <p> ! * This constructor is private so that the class cannot be instantiated ! * directly, but instead only through <code>{@link #getInstance()}</code>. ! * </p> ! * ! * @param schemaURL <code>URL</code> to parse the schema at. ! * @throws <code>IOException</code> - when errors in parsing occur. ! */ ! private Validator(URL schemaURL) throws IOException ! { ! this.schemaURL = schemaURL; ! ! // parse the XML Schema and create the constraints ! SchemaParser parser = new SchemaParser(schemaURL); ! constraints = parser.getConstraints(); ! } ! ! /** ! * <p> ! * This will return the instance for the specific XML Schema URL. If a schema ! * exists, it is returned (as parsing will already be done); otherwise, ! * a new instance is created, and then returned. ! * </p> ! * ! * @param schemaURL <code>URL</code> of schema to validate against. ! * @return <code>Validator</code> - the instance, ready to use. ! * @throws <code>IOException</code> - when errors in parsing occur. ! */ ! public static synchronized Validator getInstance(URL schemaURL) ! throws IOException ! { ! ! if (instances != null) ! { ! if (instances.containsKey(schemaURL.toString())) ! { ! return (Validator)instances.get(schemaURL.toString()); ! } ! else ! { ! Validator validator = new Validator(schemaURL); ! instances.put(schemaURL.toString(), validator); ! return validator; ! } ! } ! else ! { ! instances = new HashMap(); ! Validator validator = new Validator(schemaURL); ! instances.put(schemaURL.toString(), validator); ! return validator; ! } ! } ! ! /** ! * <p> ! * This will validate a data value (in <code>String</code> format) against a ! * specific constraint, and return an error message if there is a problem. ! * </p> ! * <p> ! * <i>Note: </i> The signature of this method has changed. The orginal ! * code returned a string. ! * </p> ! * @param constraintName the identifier in the constraints to validate this data against. ! * @param data <code>String</code> data to validate. ! * @return <code>List</code> - Error message, or an empty String if no problems occurred. ! */ ! public List checkValidity(String constraintName, String data) ! { ! StringBuffer errorMessage = new StringBuffer(); ! LinkedList errorList = new LinkedList(); ! ! // Validate against the correct constraint ! Object o = constraints.get(constraintName); ! ! // If no constraint, then everything is valid ! if (o == null) ! { ! return errorList; ! } ! ! Constraint constraint = (Constraint)o; ! ! // Validate data type ! if (!correctDataType(data, constraint.getDataType())) ! { ! errorMessage.append("The value supplied, ") ! .append(data) ! .append(" cannot be converted to the required type, ") ! .append(constraint.getDataType()); ! errorList.add( errorMessage.toString() ); ! } ! ! errorMessage.delete( 0, errorMessage.length() ); ! ! // Validate against allowed values ! if (constraint.hasAllowedValues()) ! { ! List allowedValues = constraint.getAllowedValues(); ! if (!allowedValues.contains(data)) ! { ! errorMessage.append("The value supplied, ") ! .append(data) ! .append(" is not in the allowed set of data types ("); ! ! for (Iterator i = allowedValues.iterator(); i.hasNext(); ) ! { ! errorMessage.append("'") ! .append((String)i.next()) ! .append("', "); ! } ! ! errorMessage.setLength(errorMessage.length() - 2); ! errorMessage.append(")"); ! errorList.add( errorMessage.toString() ); ! } ! } ! ! errorMessage.delete( 0, errorMessage.length() ); ! ! // Validate against range specifications ! try ! { ! double doubleValue = DataConverter.toDouble( data, constraint.getDataType() ); ! ! if (constraint.hasMinExclusive()) ! { ! if (doubleValue <= constraint.getMinExclusive()) ! { ! errorMessage.append("The value supplied, ") ! .append(data) ! .append(" is less than or equal to the allowed minimum value, ") ! .append(constraint.getMinExclusiveString()); ! errorList.add( errorMessage.toString() ); ! } ! } ! ! errorMessage.delete( 0, errorMessage.length() ); ! ! if (constraint.hasMinInclusive()) ! { ! if (doubleValue < constraint.getMinInclusive()) ! { ! errorMessage.append("The value supplied, ") ! .append(data) ! .append(" is less than the allowed minimum value, ") ! .append(constraint.getMinInclusiveString()); ! errorList.add( errorMessage.toString() ); ! } ! } ! ! errorMessage.delete( 0, errorMessage.length() ); ! ! if (constraint.hasMaxExclusive()) ! { ! if (doubleValue >= constraint.getMaxExclusive()) ! { ! errorMessage.append("The value supplied, ") ! .append(data) ! .append(" is greater than or equal to the allowed maximum value, ") ! .append(constraint.getMaxExclusiveString()); ! errorList.add( errorMessage.toString() ); ! } ! } ! ! errorMessage.delete( 0, errorMessage.length() ); ! ! if (constraint.hasMaxInclusive()) ! { ! if (doubleValue > constraint.getMaxInclusive()) ! { ! errorMessage.append("The value supplied, ") ! .append(data) ! .append(" is greater than the allowed maximum value, ") ! .append(constraint.getMaxInclusiveString()); ! errorList.add( errorMessage.toString() ); ! } ! } ! } ! catch (UnsupportedDataTypeException e) ! { ! ! } ! ! // Return any errors found ! return errorList; ! } ! ! /** ! * <p> ! * This will test the supplied data to see if it can be converted to the ! * Java data type given in <code>dataType</code>. ! * </p> ! * ! * @param data <code>String</code> to test data type of. ! * @param dataType <code>String</code> name of Java class to convert to. ! * @return <code>boolean</code> - whether conversion can occur. ! */ ! private boolean correctDataType(String data, String dataType) ! { ! if ((dataType.equals("String")) || (dataType.equals("java.lang.String"))) ! { ! return true; ! } ! ! if ((dataType.equals("int")) || (dataType.equals("java.lang.Integer"))) ! { ! try ! { ! Integer test = new Integer(data); ! return true; ! } ! catch (NumberFormatException e) ! { ! return false; ! } ! } ! ! if ((dataType.equals("long")) || (dataType.equals("java.lang.Long"))) ! { ! try ! { ! Long test = new Long(data); ! return true; ! } ! catch (NumberFormatException e) ! { ! return false; ! } ! } ! ! if ((dataType.equals("float")) || (dataType.equals("java.lang.Float"))) ! { ! try ! { ! Float test = new Float(data); ! return true; ! } ! catch (NumberFormatException e) ! { ! return false; ! } ! } ! ! if ((dataType.equals("double")) || (dataType.equals("java.lang.Double"))) ! { ! try ! { ! Double test = new Double(data); ! return true; ! } ! catch (NumberFormatException e) ! { ! return false; ! } ! } ! ! if (dataType.equals("java.lang.Boolean")) ! { ! if ((data.equalsIgnoreCase("true")) || ! (data.equalsIgnoreCase("false")) || ! (data.equalsIgnoreCase("yes")) || ! (data.equalsIgnoreCase("no"))) ! { ! return true; ! } ! else ! { ! return false; ! } ! } ! ! // Check for date. ! if ( dataType.equals ( "date" ) || dataType.equals( "java.lang.Date" ) ) ! { ! try ! { ! DateFormat df = DateFormat.getDateInstance( DateFormat.SHORT ); ! df.setLenient( false ); ! Date date = df.parse( data ); ! } ! catch ( ParseException pe ) ! { ! return false; ! } ! } ! ! return true; ! } ! ! /** ! * Basically clears the instances hashmap.<br> ! * The idea is that if you want to force schemas to be reloaded you can ! * call this method. This will force the next call to getInstance() to ! * reload the schema. ! */ ! public static synchronized void unload() ! { ! if ( instances != null && ! instances.size() > 0 ) ! { ! Iterator i = instances.values().iterator(); ! while ( i.hasNext() ) ! { ! i.remove(); ! } ! } ! ! instances.clear(); ! instances = null; ! } ! } --- 1,409 ---- ! /*------------------------------------------------------------------------------------------ ! * First let me give credit where credit is due! ! * ! * This package is based on the orginal work of Brett McLaughlin in the Javaworld article ! * 'Validation with Java and XML schema, Part 1' which can be found at ! * http://www.javaworld.com/javaworld/jw-09-2000/jw-0908-validation.html ! * ! * ! * Major modifications are noted in the class JavaDoc comment or in the CVS change log. ! *------------------------------------------------------------------------------------------ ! * ! * Legal Stuff ! * ! * JValid is Open Source. ! * ! * Copyright (c) 2000 Mike Williams. All rights reserved. ! * ! * Redistribution and use in source and binary forms, with or without ! * modification, are permitted under the terms of the ! * GNU LESSER GENERAL PUBLIC LICENSE version 2.1 or later. ! * ! * You should have received a copy the license in this distribution. ! * ! * This product includes software developed by the JDOM Project ! * (http://www.jdom.org). Please see LICENSE-JDOM for details. ! * ! * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED ! * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES ! * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE ! * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, ! * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ! * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE ! * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS ! * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER ! * IN CONTRACT, STRICT LIABILITY, OR TORT ! * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE ! * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH ! * DAMAGE. ! * ! *------------------------------------------------------------------- ! * C H A N G E L O G ! *------------------------------------------------------------------- ! * $Header$ * $Log$ + * Revision 1.4 2001/03/22 11:24:15 mikewill_1998 + * Updated JavaDoc comments on checkValidation(). + * * Revision 1.3 2001/03/02 03:46:15 mikewill_1998 * Added support for date validation. ! * ! * Revision 1.2 2001/02/25 17:42:18 mikewill_1998 ! * Updated JavaDoc comments. ! * Added acknowledgements for JDOM and Apache. ! * ! * Revision 1.1.1.1 2001/02/22 11:30:33 mikewill_1998 ! * Initial Import ! * ! */ ! ! package net.sourceforge.jvalid; ! ! import java.io.IOException; ! import java.net.URL; ! import java.util.HashMap; ! import java.util.Iterator; ! import java.util.List; ! import java.util.Map; ! import java.util.Date; ! import java.util.LinkedList; ! import java.text.DateFormat; ! import java.text.ParseException; ! import java.util.Iterator; ! ! import net.sourceforge.jvalid.UnsupportedDataTypeException; ! ! /** ! * <p> ! * The <code>Validator</code> class allows an application component or client to ! * provide data, and determine if the data is valid for the requested type. ! * </p> ! * ! * @author Mike Williams ! */ ! public class Validator ! { ! ! /** The instances of this class for use (singleton design pattern) */ ! private static Map instances = null; ! ! /** The URL of the XML Schema for this <code>Validator</code> */ ! private URL schemaURL; ! ! /** The constraints for this XML Schema */ ! private Map constraints; ! ! /** ! * <p> ! * This constructor is private so that the class cannot be instantiated ! * directly, but instead only through <code>{@link #getInstance()}</code>. ! * </p> ! * ! * @param schemaURL <code>URL</code> to parse the schema at. ! * @throws <code>IOException</code> - when errors in parsing occur. ! */ ! private Validator(URL schemaURL) throws IOException ! { ! this.schemaURL = schemaURL; ! ! // parse the XML Schema and create the constraints ! SchemaParser parser = new SchemaParser(schemaURL); ! constraints = parser.getConstraints(); ! } ! ! /** ! * <p> ! * This will return the instance for the specific XML Schema URL. If a schema ! * exists, it is returned (as parsing will already be done); otherwise, ! * a new instance is created, and then returned. ! * </p> ! * ! * @param schemaURL <code>URL</code> of schema to validate against. ! * @return <code>Validator</code> - the instance, ready to use. ! * @throws <code>IOException</code> - when errors in parsing occur. ! */ ! public static synchronized Validator getInstance(URL schemaURL) ! throws IOException ! { ! ! if (instances != null) ! { ! if (instances.containsKey(schemaURL.toString())) ! { ! return (Validator)instances.get(schemaURL.toString()); ! } ! else ! { ! Validator validator = new Validator(schemaURL); ! instances.put(schemaURL.toString(), validator); ! return validator; ! } ! } ! else ! { ! instances = new HashMap(); ! Validator validator = new Validator(schemaURL); ! instances.put(schemaURL.toString(), validator); ! return validator; ! } ! } ! ! /** ! * <p> ! * This will validate a data value (in <code>String</code> format) against a ! * specific constraint, and return a List containing any errors if there is a problem. ! * </p> ! * <p> ! * <i>Note: </i> The signature of this method has changed. The orginal ! * code returned a string. ! * </p> ! * @param constraintName the identifier in the constraints to validate this data against. ! * @param data <code>String</code> data to validate. ! * @return <code>List</code> - A List of error messages, or an empty List if no problems occurred. ! */ ! public List checkValidity(String constraintName, String data) ! { ! StringBuffer errorMessage = new StringBuffer(); ! LinkedList errorList = new LinkedList(); ! ! // Validate against the correct constraint ! Object o = constraints.get(constraintName); ! ! // If no constraint, then everything is valid ! if (o == null) ! { ! return errorList; ! } ! ! Constraint constraint = (Constraint)o; ! ! // Validate data type ! if (!correctDataType(data, constraint.getDataType())) ! { ! errorMessage.append("The value supplied, ") ! .append(data) ! .append(" cannot be converted to the required type, ") ! .append(constraint.getDataType()); ! errorList.add( errorMessage.toString() ); ! } ! ! errorMessage.delete( 0, errorMessage.length() ); ! ! // Validate against allowed values ! if (constraint.hasAllowedValues()) ! { ! List allowedValues = constraint.getAllowedValues(); ! if (!allowedValues.contains(data)) ! { ! errorMessage.append("The value supplied, ") ! .append(data) ! .append(" is not in the allowed set of data types ("); ! ! for (Iterator i = allowedValues.iterator(); i.hasNext(); ) ! { ! errorMessage.append("'") ! .append((String)i.next()) ! .append("', "); ! } ! ! errorMessage.setLength(errorMessage.length() - 2); ! errorMessage.append(")"); ! errorList.add( errorMessage.toString() ); ! } ! } ! ! errorMessage.delete( 0, errorMessage.length() ); ! ! // Validate against range specifications ! try ! { ! double doubleValue = DataConverter.toDouble( data, constraint.getDataType() ); ! ! if (constraint.hasMinExclusive()) ! { ! if (doubleValue <= constraint.getMinExclusive()) ! { ! errorMessage.append("The value supplied, ") ! .append(data) ! .append(" is less than or equal to the allowed minimum value, ") ! .append(constraint.getMinExclusiveString()); ! errorList.add( errorMessage.toString() ); ! } ! } ! ! errorMessage.delete( 0, errorMessage.length() ); ! ! if (constraint.hasMinInclusive()) ! { ! if (doubleValue < constraint.getMinInclusive()) ! { ! errorMessage.append("The value supplied, ") ! .append(data) ! .append(" is less than the allowed minimum value, ") ! .append(constraint.getMinInclusiveString()); ! errorList.add( errorMessage.toString() ); ! } ! } ! ! errorMessage.delete( 0, errorMessage.length() ); ! ! if (constraint.hasMaxExclusive()) ! { ! if (doubleValue >= constraint.getMaxExclusive()) ! { ! errorMessage.append("The value supplied, ") ! .append(data) ! .append(" is greater than or equal to the allowed maximum value, ") ! .append(constraint.getMaxExclusiveString()); ! errorList.add( errorMessage.toString() ); ! } ! } ! ! errorMessage.delete( 0, errorMessage.length() ); ! ! if (constraint.hasMaxInclusive()) ! { ! if (doubleValue > constraint.getMaxInclusive()) ! { ! errorMessage.append("The value supplied, ") ! .append(data) ! .append(" is greater than the allowed maximum value, ") ! .append(constraint.getMaxInclusiveString()); ! errorList.add( errorMessage.toString() ); ! } ! } ! } ! catch (UnsupportedDataTypeException e) ! { ! ! } ! ! // Return any errors found ! return errorList; ! } ! ! /** ! * <p> ! * This will test the supplied data to see if it can be converted to the ! * Java data type given in <code>dataType</code>. ! * </p> ! * ! * @param data <code>String</code> to test data type of. ! * @param dataType <code>String</code> name of Java class to convert to. ! * @return <code>boolean</code> - whether conversion can occur. ! */ ! private boolean correctDataType(String data, String dataType) ! { ! if ((dataType.equals("String")) || (dataType.equals("java.lang.String"))) ! { ! return true; ! } ! ! if ((dataType.equals("int")) || (dataType.equals("java.lang.Integer"))) ! { ! try ! { ! Integer test = new Integer(data); ! return true; ! } ! catch (NumberFormatException e) ! { ! return false; ! } ! } ! ! if ((dataType.equals("long")) || (dataType.equals("java.lang.Long"))) ! { ! try ! { ! Long test = new Long(data); ! return true; ! } ! catch (NumberFormatException e) ! { ! return false; ! } ! } ! ! if ((dataType.equals("float")) || (dataType.equals("java.lang.Float"))) ! { ! try ! { ! Float test = new Float(data); ! return true; ! } ! catch (NumberFormatException e) ! { ! return false; ! } ! } ! ! if ((dataType.equals("double")) || (dataType.equals("java.lang.Double"))) ! { ! try ! { ! Double test = new Double(data); ! return true; ! } ! catch (NumberFormatException e) ! { ! return false; ! } ! } ! ! if (dataType.equals("java.lang.Boolean")) ! { ! if ((data.equalsIgnoreCase("true")) || ! (data.equalsIgnoreCase("false")) || ! (data.equalsIgnoreCase("yes")) || ! (data.equalsIgnoreCase("no"))) ! { ! return true; ! } ! else ! { ! return false; ! } ! } ! ! // Check for date. ! if ( dataType.equals ( "date" ) || dataType.equals( "java.lang.Date" ) ) ! { ! try ! { ! DateFormat df = DateFormat.getDateInstance( DateFormat.SHORT ); ! df.setLenient( false ); ! Date date = df.parse( data ); ! } ! catch ( ParseException pe ) ! { ! return false; ! } ! } ! ! return true; ! } ! ! /** ! * Basically clears the instances hashmap.<br> ! * The idea is that if you want to force schemas to be reloaded you can ! * call this method. This will force the next call to getInstance() to ! * reload the schema. ! */ ! public static synchronized void unload() ! { ! if ( instances != null && ! instances.size() > 0 ) ! { ! Iterator i = instances.values().iterator(); ! while ( i.hasNext() ) ! { ! i.remove(); ! } ! } ! ! instances.clear(); ! instances = null; ! } ! } |
|
From: Mike W. <mik...@us...> - 2001-03-22 03:31:33
|
Update of /cvsroot/jvalid/jvalid In directory usw-pr-cvs1:/tmp/cvs-serv2574 Modified Files: build.xml Log Message: Lost the cvs comments. Thought it was a bit over kill for a config file :) Index: build.xml =================================================================== RCS file: /cvsroot/jvalid/jvalid/build.xml,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -r1.3 -r1.4 *** build.xml 2001/03/17 04:32:30 1.3 --- build.xml 2001/03/22 03:31:30 1.4 *************** *** 1,14 **** - <!-- $Header$ - $Log$ - Revision 1.3 2001/03/17 04:32:30 mikewill_1998 - Testing syncmail :) - - Revision 1.2 2001/03/02 03:40:07 mikewill_1998 - Cleaned it up a bit and added zip file creation. - - Revision 1.1 2001/03/01 03:02:31 mikewill_1998 - Ant build configuration file. - - --> <project name="jvalid" default="dist" basedir="."> --- 1,2 ---- |