001: /*
002: * $Header: /cvsroot/jvalidate/jvalidate-framework/jvalidate/src/main/java/nl/knowlogy/validation/validators/IsValidValidator.java,v 1.8 2007/06/20 16:24:57 roberthofstra Exp $
003: * $Revision: 1.8 $
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.Collection;
024: import java.util.Iterator;
025:
026: import nl.knowlogy.validation.Messages;
027: import nl.knowlogy.validation.Validatable;
028: import nl.knowlogy.validation.ValidationEngine;
029:
030: /**
031: * <p>
032: * The <code>IsValidValidator</code> validates properties which are not
033: * returning default types (like Integer, Long, String etc) but user defined classes or
034: * arrays/collections of user defined classes.
035: * </p>
036: * <p>
037: * These instances are validated, if exists, with the use of the <code>Validatable</code>
038: * interface, otherwise with <code>ValidationEngine<code> instance.
039: * </p>
040: *
041: * @see ValidationEngine
042: * @see Validatable
043: *
044: * @author Robert
045: */
046: public class IsValidValidator extends BasePropertyValidator {
047:
048: /**
049: *
050: */
051: public IsValidValidator(String propertyName) {
052: super (propertyName);
053: }
054:
055: public IsValidValidator(String propertyName, String errorCode) {
056: this (propertyName);
057: setErrorCode(errorCode);
058: }
059:
060: /*
061: * (non-Javadoc)
062: *
063: * @see nl.knowlogy.validation.validators.BasePropertyValidator#getDefaultErrorCode()
064: */
065: public String getDefaultErrorCode() {
066: return null;
067: }
068:
069: /*
070: * (non-Javadoc)
071: *
072: * @see nl.knowlogy.validation.metadata.PropertyValidation#doValidatePropertyValue(java.lang.Object,
073: * nl.knowlogy.validation.errors.Errors)
074: */
075: public void doValidatePropertyValue(Object toValidate,
076: Object propertyValue, Messages messages) {
077: if ((propertyValue != null)
078: && (propertyValue instanceof Collection)) {
079: Collection collection = (Collection) propertyValue;
080: Iterator collectionIterator = collection.iterator();
081: while (collectionIterator.hasNext()) {
082: Object obj = collectionIterator.next();
083: ValidationEngine.validate(obj, messages);
084: }
085: } else if ((propertyValue != null)
086: && (propertyValue instanceof Object[])) {
087: Object[] objectArray = (Object[]) propertyValue;
088: for (int i = 0; i < objectArray.length; i++) {
089: Object object = objectArray[i];
090: ValidationEngine.validate(object, messages);
091: }
092: } else if (propertyValue != null) {
093: ValidationEngine.validate(propertyValue, messages);
094: }
095:
096: }
097:
098: public void doValidatePropertyValue(Object toValidate,
099: Object propertyValue, String group, Messages messages) {
100: if ((propertyValue != null)
101: && (propertyValue instanceof Collection)) {
102: Collection collection = (Collection) propertyValue;
103: Iterator collectionIterator = collection.iterator();
104: while (collectionIterator.hasNext()) {
105: Object object = collectionIterator.next();
106: ValidationEngine.validateGroup(object, group, messages);
107: }
108: } else if ((propertyValue != null)
109: && (propertyValue instanceof Object[])) {
110: Object[] objectArray = (Object[]) propertyValue;
111: for (int i = 0; i < objectArray.length; i++) {
112: Object object = objectArray[i];
113: ValidationEngine.validateGroup(object, group, messages);
114: }
115: } else if (propertyValue != null) {
116: ValidationEngine.validateGroup(propertyValue, group,
117: messages);
118: }
119: }
120:
121: public String toString() {
122: return "IsValidValidator, " + super.toString();
123: }
124:
125: }
|