Author: epbernard
Date: 2006-12-20 17:08:59 -0500 (Wed, 20 Dec 2006)
New Revision: 11005
Added:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/AbstractLuhnValidator.java
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/CreditCardNumber.java
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/CreditCardNumberValidator.java
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/EAN.java
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/EANValidator.java
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/validator/test/validator/CreditCard.java
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/validator/test/validator/LuhnTest.java
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages.properties
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_da.properties
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_de.properties
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_es.properties
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_fr.properties
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_it.properties
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_nl.properties
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_pt_BR.properties
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_sv.properties
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_zh_CN.properties
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_zh_TW.properties
Log:
ANN-508 @CreditCardNumber
ANN-510 @EAN
Added:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/AbstractLuhnValidator.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/AbstractLuhnValidator.java 2006-12-19
22:22:21 UTC (rev 11004)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/AbstractLuhnValidator.java 2006-12-20
22:08:59 UTC (rev 11005)
@@ -0,0 +1,40 @@
+//$Id: $
+package org.hibernate.validator;
+
+import java.util.List;
+import java.util.ArrayList;
+
+/**
+ * Implement the Luhn algorithm (with Luhn key on the last digit)
+ * @author Emmanuel Bernard
+ */
+public abstract class AbstractLuhnValidator {
+ abstract int multiplicator();
+
+ public boolean isValid(Object value) {
+ if (value == null) return true;
+ if ( ! ( value instanceof String) ) return false;
+ String creditCard = (String) value;
+ char[] chars = creditCard.toCharArray();
+
+ List<Integer> ints = new ArrayList<Integer>();
+ for (char c : chars) {
+ if ( Character.isDigit( c ) ) ints.add( c - '0' );
+ }
+ int length = ints.size();
+ int sum = 0;
+ boolean even = false;
+ for ( int index = length - 1 ; index >= 0 ; index-- ) {
+ int digit = ints.get(index);
+ if (even) {
+ digit *= multiplicator();
+ }
+ if (digit > 9) {
+ digit = digit / 10 + digit % 10;
+ }
+ sum+= digit;
+ even = !even;
+ }
+ return sum % 10 == 0;
+ }
+}
Added:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/CreditCardNumber.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/CreditCardNumber.java 2006-12-19
22:22:21 UTC (rev 11004)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/CreditCardNumber.java 2006-12-20
22:08:59 UTC (rev 11005)
@@ -0,0 +1,23 @@
+//$Id: $
+package org.hibernate.validator;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Target;
+import java.lang.annotation.Retention;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.RetentionPolicy;
+
+/**
+ * The annotated element has to represent a valid
+ * credit card number. This is the Luhn algorithm implementation
+ * which aims to check for user mistake, not credit card validity!
+ *
+ * @author Emmanuel Bernard
+ */
+@Documented
+@ValidatorClass( CreditCardNumberValidator.class)
+(a)Target({ElementType.METHOD, ElementType.FIELD})
+@Retention( RetentionPolicy.RUNTIME )
+public @interface CreditCardNumber {
+ String message() default "{validator.creditCard}";
+}
Added:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/CreditCardNumberValidator.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/CreditCardNumberValidator.java 2006-12-19
22:22:21 UTC (rev 11004)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/CreditCardNumberValidator.java 2006-12-20
22:08:59 UTC (rev 11005)
@@ -0,0 +1,19 @@
+//$Id: $
+package org.hibernate.validator;
+
+import java.io.Serializable;
+
+/**
+ * Check a credit card number through the Luhn algorithm
+ *
+ * @author Emmanuel Bernard
+ */
+public class CreditCardNumberValidator extends AbstractLuhnValidator implements
Validator<CreditCardNumber>, Serializable {
+
+ public void initialize(CreditCardNumber parameters) {
+ }
+
+ int multiplicator() {
+ return 2;
+ }
+}
Added:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/EAN.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/EAN.java 2006-12-19
22:22:21 UTC (rev 11004)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/EAN.java 2006-12-20
22:08:59 UTC (rev 11005)
@@ -0,0 +1,23 @@
+//$Id: $
+package org.hibernate.validator;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Target;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+/**
+ * The annotated element has to represent an EAN-13 or UPC-A
+ *
+ * which aims to check for user mistake, not actual number validity!
+ *
+ * @author Emmanuel Bernard
+ */
+@Documented
+@ValidatorClass( EANValidator.class)
+(a)Target({ElementType.METHOD, ElementType.FIELD})
+@Retention( RetentionPolicy.RUNTIME )
+public @interface EAN {
+ String message() default "{validator.ean}";
+}
Added:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/EANValidator.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/EANValidator.java 2006-12-19
22:22:21 UTC (rev 11004)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/EANValidator.java 2006-12-20
22:08:59 UTC (rev 11005)
@@ -0,0 +1,41 @@
+//$Id: $
+package org.hibernate.validator;
+
+import java.util.List;
+import java.util.ArrayList;
+
+/**
+ * Validate EAN13 and UPC-A
+ *
+ * @author Emmanuel Bernard
+ */
+public class EANValidator implements Validator<EAN> {
+
+ public void initialize(EAN parameters) {
+ }
+
+
+ public boolean isValid(Object value) {
+ if (value == null) return true;
+ if ( ! ( value instanceof String) ) return false;
+ String creditCard = (String) value;
+ char[] chars = creditCard.toCharArray();
+
+ List<Integer> ints = new ArrayList<Integer>();
+ for (char c : chars) {
+ if ( Character.isDigit( c ) ) ints.add( c - '0' );
+ }
+ int length = ints.size();
+ int sum = 0;
+ boolean even = false;
+ for ( int index = length - 1 ; index >= 0 ; index-- ) {
+ int digit = ints.get(index);
+ if (even) {
+ digit *= 3;
+ }
+ sum+= digit;
+ even = !even;
+ }
+ return sum % 10 == 0;
+ }
+}
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages.properties
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages.properties 2006-12-19
22:22:21 UTC (rev 11004)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages.properties 2006-12-20
22:08:59 UTC (rev 11005)
@@ -11,4 +11,6 @@
validator.size=size must be between {min} and {max}
validator.email=not a well-formed email address
validator.notEmpty=may not be null or empty
-validator.digits=numeric value out of bounds (<{integerDigits}
digits>.<{fractionalDigits} digits> expected)
\ No newline at end of file
+validator.digits=numeric value out of bounds (<{integerDigits}
digits>.<{fractionalDigits} digits> expected)
+validator.creditCard=invalid credit card number
+validator.ean=invalid EAN
\ No newline at end of file
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_da.properties
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_da.properties 2006-12-19
22:22:21 UTC (rev 11004)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_da.properties 2006-12-20
22:08:59 UTC (rev 11005)
@@ -11,4 +11,6 @@
validator.size=antallet skal v�re mellem {min} og {max}
validator.email=not a well-formed email address
validator.notEmpty=m� ikke v�re null eller tom
-validator.digits=numeric value out of bounds (<{integerDigits}
digits>.<{fractionalDigits} digits> expected)
\ No newline at end of file
+validator.digits=numeric value out of bounds (<{integerDigits}
digits>.<{fractionalDigits} digits> expected)
+validator.ean=invalid EAN
+validator.creditCard=invalid credit card number
\ No newline at end of file
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_de.properties
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_de.properties 2006-12-19
22:22:21 UTC (rev 11004)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_de.properties 2006-12-20
22:08:59 UTC (rev 11005)
@@ -11,4 +11,6 @@
validator.size=muss zwischen {min} und {max} gross sein
validator.email=not a well-formed email address
validator.notEmpty=kann nicht null oder leer sein
-validator.digits=numeric value out of bounds (<{integerDigits}
digits>.<{fractionalDigits} digits> expected)
\ No newline at end of file
+validator.digits=numeric value out of bounds (<{integerDigits}
digits>.<{fractionalDigits} digits> expected)
+validator.ean=invalid EAN
+validator.creditCard=invalid credit card number
\ No newline at end of file
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_es.properties
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_es.properties 2006-12-19
22:22:21 UTC (rev 11004)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_es.properties 2006-12-20
22:08:59 UTC (rev 11005)
@@ -11,4 +11,6 @@
validator.size=el tama�o debe ser entre {min} y {max}
validator.email=no es una direcci�n de correo electr�nico bien formada
validator.notEmpty=no debe ser nulo o vac�o
-validator.digits=numeric value out of bounds (<{integerDigits}
digits>.<{fractionalDigits} digits> expected)
\ No newline at end of file
+validator.digits=numeric value out of bounds (<{integerDigits}
digits>.<{fractionalDigits} digits> expected)
+validator.ean=invalid EAN
+validator.creditCard=invalid credit card number
\ No newline at end of file
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_fr.properties
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_fr.properties 2006-12-19
22:22:21 UTC (rev 11004)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_fr.properties 2006-12-20
22:08:59 UTC (rev 11005)
@@ -11,4 +11,6 @@
validator.size=le nombre d'�l�ments doit �tre entre {min} et {max}
validator.email=Address email mal form�e
validator.notEmpty=ne peut pas �tre nul ou vide
-validator.digits=valeur num�rique hors limite (<{integerDigits}
chiffres>.<{fractionalDigits} chiffres> attendus)
\ No newline at end of file
+validator.digits=valeur num�rique hors limite (<{integerDigits}
chiffres>.<{fractionalDigits} chiffres> attendus)
+validator.ean=invalid EAN
+validator.creditCard=invalid credit card number
\ No newline at end of file
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_it.properties
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_it.properties 2006-12-19
22:22:21 UTC (rev 11004)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_it.properties 2006-12-20
22:08:59 UTC (rev 11005)
@@ -12,3 +12,5 @@
validator.email=not a well-formed email address
validator.notEmpty=non pu� essere nullo o vuoto
validator.digits=numeric value out of bounds (<{integerDigits}
digits>.<{fractionalDigits} digits> expected)
+validator.ean=invalid EAN
+validator.creditCard=invalid credit card number
\ No newline at end of file
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_nl.properties
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_nl.properties 2006-12-19
22:22:21 UTC (rev 11004)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_nl.properties 2006-12-20
22:08:59 UTC (rev 11005)
@@ -11,4 +11,6 @@
validator.size=moet tussen {min} en {max} groot zijn
validator.email=moet een geldig e-mail adres zijn
validator.notEmpty=mag niet null of leeg zijn
-validator.digits=numeric value out of bounds (<{integerDigits}
digits>.<{fractionalDigits} digits> expected)
\ No newline at end of file
+validator.digits=numeric value out of bounds (<{integerDigits}
digits>.<{fractionalDigits} digits> expected)
+validator.ean=invalid EAN
+validator.creditCard=invalid credit card number
\ No newline at end of file
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_pt_BR.properties
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_pt_BR.properties 2006-12-19
22:22:21 UTC (rev 11004)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_pt_BR.properties 2006-12-20
22:08:59 UTC (rev 11005)
@@ -11,4 +11,6 @@
validator.size=tamanho deve estar entre {min} e {max}
validator.email=n�o � um email v�lido
validator.notEmpty=n�o pode ser nulo ou vazio
-validator.digits=numeric value out of bounds (<{integerDigits}
digits>.<{fractionalDigits} digits> expected)
\ No newline at end of file
+validator.digits=numeric value out of bounds (<{integerDigits}
digits>.<{fractionalDigits} digits> expected)
+validator.ean=invalid EAN
+validator.creditCard=invalid credit card number
\ No newline at end of file
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_sv.properties
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_sv.properties 2006-12-19
22:22:21 UTC (rev 11004)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_sv.properties 2006-12-20
22:08:59 UTC (rev 11005)
@@ -11,4 +11,6 @@
validator.size=storlek m�ste vara mellan {min} och {max}
validator.email=ogiltig e-mail adress
validator.notEmpty=f�r inte vara tomt
-validator.digits=numeric value out of bounds (<{integerDigits}
digits>.<{fractionalDigits} digits> expected)
\ No newline at end of file
+validator.digits=numeric value out of bounds (<{integerDigits}
digits>.<{fractionalDigits} digits> expected)
+validator.ean=invalid EAN
+validator.creditCard=invalid credit card number
\ No newline at end of file
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_zh_CN.properties
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_zh_CN.properties 2006-12-19
22:22:21 UTC (rev 11004)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_zh_CN.properties 2006-12-20
22:08:59 UTC (rev 11005)
@@ -11,4 +11,6 @@
validator.size=\u5927\u5c0f\u5fc5\u987b\u4ecb\u4e8e {min} \u4e0e {max} \u4e4b\u95f4
validator.email=not a well-formed email address
validator.notEmpty=may not be null or empty
-validator.digits=numeric value out of bounds (<{integerDigits}
digits>.<{fractionalDigits} digits> expected)
\ No newline at end of file
+validator.digits=numeric value out of bounds (<{integerDigits}
digits>.<{fractionalDigits} digits> expected)
+validator.ean=invalid EAN
+validator.creditCard=invalid credit card number
\ No newline at end of file
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_zh_TW.properties
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_zh_TW.properties 2006-12-19
22:22:21 UTC (rev 11004)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/validator/resources/DefaultValidatorMessages_zh_TW.properties 2006-12-20
22:08:59 UTC (rev 11005)
@@ -11,4 +11,6 @@
validator.size=\u5927\u5c0f\u5fc5\u9808\u4ecb\u65bc{min} \u8207 {max} \u4e4b\u9593
validator.email=not a well-formed email address
validator.digits=numeric value out of bounds (<{integerDigits}
digits>.<{fractionalDigits} digits> expected)
-validator.notEmpty=may not be null or empty
\ No newline at end of file
+validator.notEmpty=may not be null or empty
+validator.ean=invalid EAN
+validator.creditCard=invalid credit card number
\ No newline at end of file
Added:
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/validator/test/validator/CreditCard.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/validator/test/validator/CreditCard.java 2006-12-19
22:22:21 UTC (rev 11004)
+++
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/validator/test/validator/CreditCard.java 2006-12-20
22:08:59 UTC (rev 11005)
@@ -0,0 +1,14 @@
+//$Id: $
+package org.hibernate.validator.test.validator;
+
+import org.hibernate.validator.EAN;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class CreditCard {
+ @org.hibernate.validator.CreditCardNumber
+ public String number;
+ @EAN
+ public String ean;
+}
Added:
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/validator/test/validator/LuhnTest.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/validator/test/validator/LuhnTest.java 2006-12-19
22:22:21 UTC (rev 11004)
+++
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/validator/test/validator/LuhnTest.java 2006-12-20
22:08:59 UTC (rev 11005)
@@ -0,0 +1,28 @@
+//$Id: $
+package org.hibernate.validator.test.validator;
+
+import junit.framework.TestCase;
+import org.hibernate.validator.ClassValidator;
+import org.hibernate.validator.InvalidValue;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class LuhnTest extends TestCase {
+ public void testCreditCard() {
+ CreditCard card = new CreditCard();
+ card.number = "1234567890123456";
+ ClassValidator<CreditCard> classValidator = new ClassValidator<CreditCard>(
CreditCard.class );
+ InvalidValue[] invalidValues = classValidator.getInvalidValues( card );
+ assertEquals( 1, invalidValues.length );
+ card.number = "541234567890125"; //right CC (luhn compliant)
+ invalidValues = classValidator.getInvalidValues( card );
+ assertEquals( 0, invalidValues.length );
+ card.ean = "9782266156066"; //right EAN
+ invalidValues = classValidator.getInvalidValues( card );
+ assertEquals( 0, invalidValues.length );
+ card.ean = "9782266156067"; //wrong EAN
+ invalidValues = classValidator.getInvalidValues( card );
+ assertEquals( 1, invalidValues.length );
+ }
+}