001: /*
002: * $Header: /cvsroot/jvalidate/jvalidate-framework/jvalidate/src/main/java/nl/knowlogy/validation/validators/AllowedValuesValidator.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 java.util.Iterator;
024: import java.util.StringTokenizer;
025:
026: import nl.knowlogy.validation.ErrorCodes;
027: import nl.knowlogy.validation.MessageType;
028: import nl.knowlogy.validation.Messages;
029:
030: import org.apache.commons.collections.FastArrayList;
031:
032: /**
033: * Validates if a property contains specific values.
034: * Default the values are separated by ',' .
035: *
036: * @author Robert
037: */
038: public class AllowedValuesValidator extends BasePropertyValidator {
039:
040: private FastArrayList allowedValuesList;
041:
042: /**
043: *
044: */
045: public AllowedValuesValidator(String propertyName,
046: String allAllowedValues) {
047: super (propertyName);
048: setAllAllowedValues(allAllowedValues);
049: }
050:
051: public AllowedValuesValidator(String propertyName,
052: String allAllowedValues, String errorCode) {
053: this (propertyName, allAllowedValues);
054: setErrorCode(errorCode);
055: }
056:
057: public AllowedValuesValidator(String propertyName,
058: String[] allowedValues, String errorCode) {
059: super (propertyName);
060: setErrorCode(errorCode);
061: setAllAllowedValues(allowedValues);
062: }
063:
064: /**
065: * @see nl.knowlogy.validation.validators.BasePropertyValidator#getDefaultErrorCode()
066: */
067: public String getDefaultErrorCode() {
068:
069: return ErrorCodes.NOT_ALLOWED_VALUE;
070: }
071:
072: /**
073: * @see nl.knowlogy.validation.metadata.PropertyValidation#doValidatePropertyValue(java.lang.Object,
074: * nl.knowlogy.validation.Messages)
075: */
076: public void doValidatePropertyValue(Object toValidate,
077: Object propertyValue, Messages messages) {
078: String stringValue = (String) propertyValue;
079:
080: if ((stringValue != null) && (allowedValuesList != null)
081: && (!allowedValuesList.contains(stringValue))) {
082: messages.addPropertyMessage(MessageType.ERROR, toValidate,
083: getPropertyName(), getErrorCode());
084:
085: }
086:
087: }
088:
089: /**
090: * Sets the allowed values of the property. This is a comma seperated string
091: * containing the values.
092: *
093: * @param allowedValues
094: * The allowedValues to set.
095: */
096: public synchronized void setAllAllowedValues(String allAllowedValues) {
097: StringTokenizer stringTokenizer = new StringTokenizer(
098: allAllowedValues, ",");
099: this .allowedValuesList = new FastArrayList();
100: this .allowedValuesList.setFast(false);
101: while (stringTokenizer.hasMoreTokens()) {
102: this .allowedValuesList.add(stringTokenizer.nextToken());
103: }
104: this .allowedValuesList.setFast(true);
105: }
106:
107: public synchronized void setAllAllowedValues(String[] allowedValues) {
108: this .allowedValuesList = new FastArrayList();
109: this .allowedValuesList.setFast(false);
110: for (int i = 0; i < allowedValues.length; i++) {
111: this .allowedValuesList.add(allowedValues[i]);
112: }
113: this .allowedValuesList.setFast(true);
114: }
115:
116: private String getAllowedValuesStr() {
117: String allowedValueStr = "";
118: Iterator allowedValuesIt = allowedValuesList.iterator();
119: if (allowedValuesIt.hasNext()) {
120: allowedValueStr = (String) allowedValuesIt.next();
121: }
122: while (allowedValuesIt.hasNext()) {
123: allowedValueStr = allowedValueStr + ","
124: + (String) allowedValuesIt.next();
125: }
126: return allowedValueStr;
127: }
128:
129: public String toString() {
130: return "AllowedValuesValidator, allowedValues=["
131: + getAllowedValuesStr() + "]" + super.toString();
132: }
133: }
|