Object name validator component
* Moved the component from the -user package to the -naming package * Added a separate interface to the component
This commit is contained in:
parent
35d8891fe3
commit
b4903d78e4
14 changed files with 483 additions and 104 deletions
legacyworlds-server-beans-naming/src/main
java/com/deepclone/lw/beans/naming
resources/configuration/meta
|
@ -0,0 +1,136 @@
|
|||
package com.deepclone.lw.beans.naming;
|
||||
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import com.deepclone.lw.cmd.ObjectNameError;
|
||||
import com.deepclone.lw.interfaces.naming.ObjectNameValidator;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Object name validation component
|
||||
*
|
||||
* <p>
|
||||
* This component implements the simple name "pre-validation" component, used before an empire or
|
||||
* map name is sent to the database.
|
||||
*
|
||||
* <p>
|
||||
* By default, it will accept names that are between 2 and 20 characters. That may be modified in
|
||||
* the component's initialisation.
|
||||
*
|
||||
* @author <a href="mailto:tseeker@legacyworlds.com">E. Benoît</a>
|
||||
*/
|
||||
class ObjectNameValidatorBean
|
||||
implements ObjectNameValidator
|
||||
{
|
||||
/** Default minimal length */
|
||||
public static final int MIN_LENGTH = 2;
|
||||
|
||||
/** Default maximal length */
|
||||
public static final int MAX_LENGTH = 20;
|
||||
|
||||
/**
|
||||
* Invalid patterns
|
||||
*
|
||||
* <p>
|
||||
* This constant lists patterns which must be rejected systematically.
|
||||
*/
|
||||
private static final Pattern fail[] = {
|
||||
Pattern.compile( "\\s\\s+" ) , Pattern.compile( "[^A-Za-z0-9 _\\'\\!\\:\\,\\-\\.\\*@\\[\\]\\{\\}]" )
|
||||
};
|
||||
|
||||
/**
|
||||
* Required patterns
|
||||
*
|
||||
* <p>
|
||||
* This constant lists patters which must be present for a name to be considered valid.
|
||||
*/
|
||||
private static final Pattern needed[] = {
|
||||
Pattern.compile( "[A-Za-z]" )
|
||||
};
|
||||
|
||||
/** Minimal length of a name */
|
||||
private int minLength = MIN_LENGTH;
|
||||
|
||||
/** Maximal length of a name */
|
||||
private int maxLength = MAX_LENGTH;
|
||||
|
||||
|
||||
/**
|
||||
* Set the minimal length of a name
|
||||
*
|
||||
* @param length
|
||||
* the new minimal length
|
||||
*/
|
||||
public void setMinLength( Integer length )
|
||||
{
|
||||
this.minLength = length;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the maximal length of a name
|
||||
*
|
||||
* @param length
|
||||
* the new maximal length
|
||||
*/
|
||||
public void setMaxLength( Integer length )
|
||||
{
|
||||
this.maxLength = length;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see com.deepclone.lw.beans.user.ObjectNameValidator#validate(java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public ObjectNameError validate( String name )
|
||||
{
|
||||
return this.validate( name , this.minLength , this.maxLength );
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see com.deepclone.lw.beans.user.ObjectNameValidator#customValidate(java.lang.String, int,
|
||||
* int)
|
||||
*/
|
||||
@Override
|
||||
public ObjectNameError validate( String name , int minLength , int maxLength )
|
||||
{
|
||||
if ( "".equals( name.trim( ) ) ) {
|
||||
return ObjectNameError.EMPTY;
|
||||
}
|
||||
|
||||
// No leading or trailing spaces
|
||||
if ( !name.equals( name.trim( ) ) ) {
|
||||
return ObjectNameError.INVALID;
|
||||
}
|
||||
|
||||
// Check length
|
||||
int length = name.length( );
|
||||
if ( length < minLength || length > maxLength ) {
|
||||
return ObjectNameError.INVALID;
|
||||
}
|
||||
|
||||
// Check bad patterns
|
||||
for ( Pattern p : ObjectNameValidatorBean.fail ) {
|
||||
if ( p.matcher( name ).find( ) ) {
|
||||
return ObjectNameError.INVALID;
|
||||
}
|
||||
}
|
||||
|
||||
// Check good patterns
|
||||
for ( Pattern p : ObjectNameValidatorBean.needed ) {
|
||||
if ( !p.matcher( name ).find( ) ) {
|
||||
return ObjectNameError.INVALID;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@
|
|||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
|
||||
|
||||
<bean id="objectNameValidator" class="com.deepclone.lw.beans.naming.ObjectNameValidatorBean" />
|
||||
<bean id="namesManager" class="com.deepclone.lw.beans.naming.NamesManagerBean" />
|
||||
<bean id="namingDAO" class="com.deepclone.lw.beans.naming.NamingDAOBean" />
|
||||
|
||||
|
|
Reference in a new issue