01: /*
02: * $Header: /cvsroot/jvalidate/jvalidate-framework/jvalidate/src/main/java/nl/knowlogy/validation/validators/MinLengthValidator.java,v 1.8 2007/06/20 16:24:57 roberthofstra Exp $
03: * $Revision: 1.8 $
04: * $Date: 2007/06/20 16:24:57 $
05: *
06: *
07: * Created on Oct 6, 2004
08: *
09: * All right reserved(c) 2004, Knowlogy
10: *
11: * Copyright 2004 - 2005 Knowlogy, the Netherlands. All rights reserved.
12: * All Knowlogy brand and product names are trademarks or registered trademarks
13: * of Knowlogy in the Netherlands and other countries.
14: *
15: * No part of this publication may be reproduced, transmitted, stored in a retrieval system,
16: * or translated into any human or computer language, in any form, or by any means, electronic,
17: * mechanical, magnetic, optical, chemical, manual, or otherwise,
18: * without the prior written permission of the copyright owner, Knowlogy.
19: *
20: */
21: package nl.knowlogy.validation.validators;
22:
23: import nl.knowlogy.validation.ErrorCodes;
24: import nl.knowlogy.validation.MessageType;
25: import nl.knowlogy.validation.Messages;
26: import nl.knowlogy.validation.PropertyMetadataSuplier;
27: import nl.knowlogy.validation.metadata.PropertyMetadata;
28:
29: /**
30: *
31: * @author Robert
32: */
33: public class MinLengthValidator extends BasePropertyValidator implements
34: PropertyMetadataSuplier {
35:
36: private Long minLength;
37:
38: /**
39: *
40: */
41:
42: public MinLengthValidator(String propertyName, Long minLength) {
43: super (propertyName);
44: this .minLength = minLength;
45: }
46:
47: public MinLengthValidator(String propertyName, Long minLength,
48: String errorCode) {
49: this (propertyName, minLength);
50: setErrorCode(errorCode);
51: }
52:
53: /* (non-Javadoc)
54: * @see nl.knowlogy.validation.validators.BasePropertyValidator#getDefaultErrorCode()
55: */
56: public String getDefaultErrorCode() {
57: return ErrorCodes.INVALID_MIN_LENGTH;
58: }
59:
60: /* (non-Javadoc)
61: * @see nl.knowlogy.validation.metadata.PropertyValidation#doValidatePropertyValue(java.lang.Object, nl.knowlogy.validation.errors.Errors)
62: */
63: public void doValidatePropertyValue(Object toValidate,
64: Object propertyValue, Messages errors) {
65:
66: if (propertyValue != null) {
67: String stringValue = propertyValue.toString();
68: if ((minLength != null)
69: && (stringValue.length() < minLength.intValue())) {
70: Object[] errorArg = new Object[2];
71: errorArg[0] = new Long(minLength.longValue());
72: errorArg[1] = new Long(stringValue.length());
73:
74: errors.addPropertyMessage(MessageType.ERROR,
75: toValidate, getPropertyName(), getErrorCode(),
76: errorArg, null);
77: }
78: }
79:
80: }
81:
82: public void supplyMetaData(PropertyMetadata propertyMetadata) {
83:
84: propertyMetadata.setMinLength(minLength);
85: }
86:
87: public String toString() {
88: return "MinLengthValidator, minLength=[" + minLength + "]"
89: + super.toString();
90: }
91:
92: }
|