001: /*
002: * $Header: /cvsroot/jvalidate/jvalidate-framework/jvalidate/src/main/java/nl/knowlogy/validation/validators/PatternValidator.java,v 1.6 2007/10/31 17:02:19 roberthofstra Exp $
003: * $Revision: 1.6 $
004: * $Date: 2007/10/31 17:02:19 $
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 gnu.regexp.RE;
024: import gnu.regexp.REException;
025: import gnu.regexp.REMatch;
026: import nl.knowlogy.validation.ErrorCodes;
027: import nl.knowlogy.validation.MessageType;
028: import nl.knowlogy.validation.Messages;
029: import nl.knowlogy.validation.PropertyMetadataSuplier;
030: import nl.knowlogy.validation.metadata.PropertyMetadata;
031:
032: import org.apache.commons.lang.StringUtils;
033:
034: /**
035: *
036: * @author Robert
037: */
038: public class PatternValidator extends BasePropertyValidator implements
039: PropertyMetadataSuplier {
040:
041: private String pattern;
042:
043: /**
044: *
045: */
046: public PatternValidator(String propertyName, String pattern) {
047: super (propertyName);
048: setPattern(pattern);
049: }
050:
051: public PatternValidator(String propertyName, String pattern,
052: String errorCode) {
053: this (propertyName, pattern);
054: setErrorCode(errorCode);
055: }
056:
057: /*
058: * (non-Javadoc)
059: *
060: * @see nl.knowlogy.validation.validators.BasePropertyValidator#getDefaultErrorCode()
061: */
062: public String getDefaultErrorCode() {
063: return ErrorCodes.INVALID_FORMAT;
064: }
065:
066: /*
067: * (non-Javadoc)
068: *
069: * @see nl.knowlogy.validation.metadata.PropertyValidation#doValidatePropertyValue(java.lang.Object,
070: * nl.knowlogy.validation.errors.Errors)
071: */
072: public void doValidatePropertyValue(Object toValidate,
073: Object propertyValue, Messages errors) {
074: if (propertyValue != null) {
075: String stringValue = propertyValue.toString();
076: if (StringUtils.isNotBlank(stringValue)) {
077: if (pattern != null) {
078: try {
079: RE re = new RE(pattern);
080: REMatch match = re.getMatch(stringValue);
081:
082: if (match == null) {
083: errors.addPropertyMessage(
084: MessageType.ERROR, toValidate,
085: getPropertyName(), getErrorCode());
086: }
087: } catch (REException re) {
088: throw new IllegalStateException(re.getMessage());
089: }
090: }
091: }
092: }
093:
094: }
095:
096: /**
097: * @return Returns the regularExpression.
098: */
099: public String getPattern() {
100: return pattern;
101: }
102:
103: /**
104: * @param regularExpression
105: * The regularExpression to set.
106: */
107: public void setPattern(String regularExpression) {
108: this .pattern = regularExpression;
109: }
110:
111: public void supplyMetaData(PropertyMetadata propertyMetadata) {
112: propertyMetadata.setPattern(pattern);
113:
114: }
115:
116: public String toString() {
117: return "PatternValidator, pattern=[" + pattern + "]"
118: + super.toString();
119: }
120:
121: }
|