001: /*
002: * $Header: /cvsroot/jvalidate/jvalidate-framework/jvalidate/src/main/java/nl/knowlogy/validation/validators/CustomPropertyValidator.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 nl.knowlogy.validation.CustomValidator;
024: import nl.knowlogy.validation.ErrorCodes;
025: import nl.knowlogy.validation.MessageType;
026: import nl.knowlogy.validation.Messages;
027:
028: /**
029: *
030: *
031: *
032: * @author <a href="mailto:robert.hofstra@knowlogy.nl">Robert Hofstra, Knowlogy</a>
033: */
034: public class CustomPropertyValidator extends BasePropertyValidator {
035:
036: private String customValidatorName;
037:
038: private CustomValidator customValidator;
039:
040: private synchronized CustomValidator getCustomValidator() {
041: if (customValidator == null) {
042: try {
043: Class clazz = Class.forName(customValidatorName);
044: customValidator = (CustomValidator) clazz.newInstance();
045: } catch (Exception ex) {
046: throw new RuntimeException(ex);
047: }
048: }
049: return customValidator;
050: }
051:
052: /**
053: * @return Returns the customValidatorName.
054: */
055: public String getCustomValidatorName() {
056: return customValidatorName;
057: }
058:
059: /**
060: * @param customValidatorName
061: * The customValidatorName to set.
062: */
063: public void setCustomValidatorName(String customValidatorName) {
064: this .customValidatorName = customValidatorName;
065: }
066:
067: public void setCustomValidator(CustomValidator customValidator) {
068: this .customValidator = customValidator;
069: }
070:
071: public CustomPropertyValidator(String propertyName) {
072: super (propertyName);
073: }
074:
075: public CustomPropertyValidator(String propertyName,
076: String customValidatorName) {
077: this (propertyName);
078: setCustomValidatorName(customValidatorName);
079: }
080:
081: public CustomPropertyValidator(String propertyName,
082: String customValidatorName, String errorCode) {
083: this (propertyName, customValidatorName);
084: setErrorCode(errorCode);
085: }
086:
087: /*
088: * (non-Javadoc)
089: *
090: * @see nl.knowlogy.validation.validators.BasePropertyValidator#getDefaultErrorCode()
091: */
092: public String getDefaultErrorCode() {
093: return ErrorCodes.NOT_ALLOWED_VALUE;
094: }
095:
096: /*
097: * (non-Javadoc)
098: *
099: * @see nl.knowlogy.validation.PropertyValidation#doValidatePropertyValue(java.lang.Object,
100: * nl.knowlogy.validation.errors.Errors)
101: */
102: public void doValidatePropertyValue(Object toValidate,
103: Object propertyValue, Messages errors) {
104:
105: if ((getCustomValidator() != null)
106: && (!getCustomValidator().isValid(propertyValue))) {
107: errors.addPropertyMessage(MessageType.ERROR, toValidate,
108: getPropertyName(), getErrorCode());
109:
110: }
111: }
112:
113: public String toString() {
114: return "CustomPropertyValidator, customValidatorName=["
115: + customValidatorName + "]" + super.toString();
116: }
117:
118: }
|