/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see String
, so the evaluation
* will be against the value's String
equivalent.
*
* The failure condition can be changed to occur either on a match or on a non-match by using
* the {@link #setRequiresMatch(boolean) requiresMatch} property. The default is true, i.e.
* failures will occur if the object value does not match the given expression.
*
* @see java.lang.String#matches(java.lang.String)
* @see java.util.regex.Pattern
*
* @author Derek Hulley
*/
public class RegexConstraint extends AbstractConstraint
{
public static final String CONSTRAINT_REGEX_NO_MATCH = "d_dictionary.constraint.regex.no_match";
public static final String CONSTRAINT_REGEX_MATCH = "d_dictionary.constraint.regex.match";
public static final String CONSTRAINT_REGEX_MSG_PREFIX = "d_dictionary.constraint.regex.error.";
private String expression;
private Pattern patternMatcher;
private boolean requiresMatch = true;
/**
* {@inheritDoc}
*/
public String getType()
{
return "REGEX";
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder(80);
sb.append("RegexConstraint")
.append("[ expression=").append(expression)
.append(", requiresMatch=").append(requiresMatch)
.append("]");
return sb.toString();
}
/**
* @return Returns the regular expression similar to the {@link String#matches(java.lang.String)}
*/
public String getExpression()
{
return expression;
}
/**
* Set the regular expression used to evaluate String values
* @param regular expression similar to the {@link String#matches(java.lang.String)} argument
*/
public void setExpression(String expression)
{
this.expression = expression;
}
/**
* @return Returns true if the value must match the regular expression
* or false if the value must not match the regular expression
*/
public boolean getRequiresMatch()
{
return requiresMatch;
}
/**
* Set whether the regular expression must be matched or not
*
* @param requiresMatch Set to true if the value must match the regular expression
* or false if the value must not match the regular expression
*/
public void setRequiresMatch(boolean requiresMatch)
{
this.requiresMatch = requiresMatch;
}
@Override
public Map