001: /*
002: * $Header: /cvsroot/jvalidate/jvalidate-framework/jvalidate/src/main/java/nl/knowlogy/validation/validators/MaxLengthValidator.java,v 1.7 2007/06/20 16:24:57 roberthofstra Exp $
003: * $Revision: 1.7 $
004: * $Date: 2007/06/20 16:24:57 $
005: *
006: *
007: * Created on Oct 6, 2004
008: *
009: * All right reserved(c) 2004, Knowlogy
010: *
011: * Copyright 2004 - 2005 Knowlogy, the Netherlands. All rights reserved.
012: * All Knowlogy brand and product names are trademarks or registered trademarks
013: * of Knowlogy in the Netherlands and other countries.
014: *
015: * No part of this publication may be reproduced, transmitted, stored in a retrieval system,
016: * or translated into any human or computer language, in any form, or by any means, electronic,
017: * mechanical, magnetic, optical, chemical, manual, or otherwise,
018: * without the prior written permission of the copyright owner, Knowlogy.
019: *
020: */
021: package nl.knowlogy.validation.validators;
022:
023: import nl.knowlogy.validation.ErrorCodes;
024: import nl.knowlogy.validation.MessageType;
025: import nl.knowlogy.validation.Messages;
026: import nl.knowlogy.validation.PropertyMetadataSuplier;
027: import nl.knowlogy.validation.metadata.PropertyMetadata;
028:
029: /**
030: *
031: * @author Robert
032: */
033: public class MaxLengthValidator extends BasePropertyValidator implements
034: PropertyMetadataSuplier {
035:
036: private Long maxLength;
037:
038: /**
039: *
040: */
041: public MaxLengthValidator(String propertyName, final Long maxLength) {
042: super (propertyName);
043: setMaxLength(maxLength);
044: }
045:
046: public MaxLengthValidator(String propertyName, Long maxLength,
047: String errorCode) {
048: this (propertyName, maxLength);
049: setErrorCode(errorCode);
050: }
051:
052: /*
053: * (non-Javadoc)
054: *
055: * @see nl.knowlogy.validation.validators.BasePropertyValidator#getDefaultErrorCode()
056: */
057: public String getDefaultErrorCode() {
058: return ErrorCodes.INVALID_MAX_LENGTH;
059: }
060:
061: /*
062: * (non-Javadoc)
063: *
064: * @see nl.knowlogy.validation.metadata.PropertyValidation#doValidatePropertyValue(java.lang.Object,
065: * nl.knowlogy.validation.errors.Errors)
066: */
067: public void doValidatePropertyValue(Object toValidate,
068: Object propertyValue, Messages errors) {
069: if (propertyValue != null) {
070: String stringValue = propertyValue.toString();
071: if ((maxLength != null)
072: && (stringValue.length() > maxLength.intValue())) {
073: Object[] errorArg = new Object[2];
074: errorArg[0] = new Long(maxLength.longValue());
075: errorArg[1] = new Integer(stringValue.length());
076: errors.addPropertyMessage(MessageType.ERROR,
077: toValidate, getPropertyName(), getErrorCode(),
078: errorArg, null);
079: }
080: } else {
081: if (logger.isDebugEnabled()) {
082: logger
083: .debug("Skipping validation propertyvalue == null for "
084: + this );
085: }
086: }
087:
088: }
089:
090: /**
091: * @param maxLength
092: * The maxLength to set.
093: */
094: public void setMaxLength(Long maxLength) {
095: this .maxLength = maxLength;
096: }
097:
098: public void supplyMetaData(PropertyMetadata propertyMetadata) {
099: propertyMetadata.setMaxLength(maxLength);
100:
101: }
102:
103: public String toString() {
104: return "MaxLengthValidator, maxLength=[" + maxLength + "]"
105: + super.toString();
106: }
107:
108: }
|