Dave Ward 1264fca386 Merged V3.1 to HEAD
13484: ETHREEOH-1547: Do not set requiresNew flag and propagate exceptions in BaseDialogBean
   13383: ETHREEOH-1220: Update LDAP-authentication-context to include allowGetEnabled entry to support Share
   13381: ETHREEOH-1181: NTLM authentication periodically fails over CIFS - "Read-Write transaction started within read-only transaction"
   13376: ETHREEOH-279: Friendly error message when cm:filename regular expression constraint is violated
   13364: ETHREEOH-814: Correct character encoding issues in LDAP synchronization
   13353: ETHREEOH-1444: Ability to run Alfresco from unexploded .war file with embedded license
   13328: ETHREEOH-1400: Prevent TLD warnings on Weblogic startup
   13183: Follow up to 13177: Fixes for Weblogic compatibility
   13177: Fixes for Weblogic compatibility
   13109: Build/test fix (to avoid unintentional import via application-context.xml)
   13100: Checkpoint for new DM index check (enterprise-only)


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@13525 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2009-03-10 14:27:09 +00:00

160 lines
5.8 KiB
Java

/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program 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 General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.repo.dictionary.constraint;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.alfresco.i18n.I18NUtil;
import org.alfresco.service.cmr.dictionary.ConstraintException;
import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
/**
* Constraint implementation that performs regular expression comparisons.
* Where possible, the {@link org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter type converter}
* will be used to first convert the value to a <code>String</code>, so the evaluation
* will be against the value's <code>String</code> equivalent.
* <p>
* 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 <tt>true</tt>, 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;
/*
* @see org.alfresco.service.cmr.dictionary.Constraint#getType()
*/
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 <tt>true</tt> if the value must match the regular expression
* or <tt>false</tt> 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 <tt>true</tt> if the value must match the regular expression
* or <tt>false</tt> if the value must not match the regular expression
*/
public void setRequiresMatch(boolean requiresMatch)
{
this.requiresMatch = requiresMatch;
}
public void initialize()
{
checkPropertyNotNull("expression", expression);
this.patternMatcher = Pattern.compile(expression);
}
protected void evaluateSingleValue(Object value)
{
// convert the value to a String
String valueStr = DefaultTypeConverter.INSTANCE.convert(String.class, value);
Matcher matcher = patternMatcher.matcher(valueStr);
boolean matches = matcher.matches();
if (matches != requiresMatch)
{
// Look for a message corresponding to this constraint name
String messageId = CONSTRAINT_REGEX_MSG_PREFIX + getShortName();
if (I18NUtil.getMessage(messageId, value) != null)
{
throw new ConstraintException(messageId, value);
}
// Otherwise, fall back to a generic (but unfriendly) message
else if (requiresMatch)
{
throw new ConstraintException(RegexConstraint.CONSTRAINT_REGEX_NO_MATCH, value, expression);
}
else
{
throw new ConstraintException(RegexConstraint.CONSTRAINT_REGEX_MATCH, value, expression);
}
}
}
public Map<String, Object> getParameters()
{
Map<String, Object> params = new HashMap<String, Object>(2);
params.put("expression", this.expression);
params.put("requiresMatch", this.requiresMatch);
return params;
}
}