[Jvalid-checkins] CVS: jvalid/src/net/sourceforge/jvalid Validator.java,1.3,1.4
Status: Alpha
Brought to you by:
mikewill_1998
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; ! } ! } |