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