Commits

prmr  committed 4e1d48e

refs #119 Fixed

  • Participants
  • Parent commits af8a3ee

Comments (0)

Files changed (5)

File ca.mcgill.cs.swevo.qualyzer/src/ca/mcgill/cs/swevo/qualyzer/model/validation/BasicNameValidator.java

  * - A name in alphanumerical+ format.
  * - A name is shorter than 256 characters long.
  */
+/**
+ * @author martin
+ *
+ */
 public class BasicNameValidator extends AbstractValidator
 {
 	private static final String BLANK = " "; //$NON-NLS-1$
 			lReturn = false;
 			fMessage = fLabel + BLANK + Messages.getString("model.validation.BasicNameValidator.tooLong"); //$NON-NLS-1$
 		}
-		else if(!ValidationUtils.verifyID(fName))
+		else if(!validateName(fName))
 		{
 			lReturn = false;
 			fMessage = fLabel + BLANK + Messages.getString("model.validation.BasicNameValidator.invalid"); //$NON-NLS-1$
 		return lReturn;
 	}
 	
+	
+	/**
+	 * Step method of the template method design pattern. Defines the 
+	 * exact format for names. Override to change the format.
+	 * @param pName The name to validate
+	 * @return True if the name is valid
+	 */
+	protected boolean validateName(String pName)
+	{
+		return ValidationUtils.verifyID(fName);
+	}
+	
 	/**
 	 * @return True if fName is already in use.
 	 */

File ca.mcgill.cs.swevo.qualyzer/src/ca/mcgill/cs/swevo/qualyzer/model/validation/CodeValidator.java

 /**
  * Validates the business rules when a new code is created.
  */
-public class CodeValidator extends BasicNameValidator
+public class CodeValidator extends RelaxedNameValidator
 {
 	/**
 	 * Constructs a new CodeValidator.

File ca.mcgill.cs.swevo.qualyzer/src/ca/mcgill/cs/swevo/qualyzer/model/validation/ParticipantValidator.java

 /**
  * Validates the business rules when a new investigator is created.
  */
-public class ParticipantValidator extends BasicNameValidator
+public class ParticipantValidator extends RelaxedNameValidator
 {
 	/**
 	 * Constructs a new ParticipantValidator.

File ca.mcgill.cs.swevo.qualyzer/src/ca/mcgill/cs/swevo/qualyzer/model/validation/RelaxedNameValidator.java

+/*******************************************************************************
+ * Copyright (c) 2010 McGill University
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Martin Robillard
+ *******************************************************************************/
+package ca.mcgill.cs.swevo.qualyzer.model.validation;
+
+import ca.mcgill.cs.swevo.qualyzer.model.Project;
+
+/**
+ * Implements name validation behavior that is more relaxed than
+ * The basic name validator. Specifically, verifies that:
+ * - A name is not empty
+ * - A name is not already in use (except if it's oldName) (but the function to verify this is 
+ * a do nothing function that in most cases should be overriden)
+ * - A name only contains letter characters, digits, spaces, or underscores
+ * - A name is shorter than 256 characters long.
+ */
+public class RelaxedNameValidator extends BasicNameValidator
+{
+	/**
+	 * Constructs a new RelaxedNameValidator.
+	 * @param pLabel The label that appears before the error message (e.g., "Investigator ID").
+	 * @param pName The name chosen for the new code.
+	 * @param pOldName The current name of the code (null if this is a new code).
+	 * @param pProject The Project in which the code is to be created.
+	 */
+	protected RelaxedNameValidator(String pLabel, String pName, String pOldName, Project pProject)
+	{
+		super(pLabel, pName, pOldName, pProject);
+	}
+	
+	/**
+	 * Constructs a new RelaxedNameValidator with a null old name.
+	 * @param pLabel The label that appears before the error message (e.g., "Investigator ID").
+	 * @param pName The name chosen for the new code.
+	 * @param pProject The Project in which the code is to be created.
+	 */
+	public RelaxedNameValidator(String pLabel, String pName, Project pProject)
+	{
+		this(pLabel, pName, null, pProject);
+	}
+
+	/**
+	 * Step method of the template method design pattern. Defines the 
+	 * exact format for names. Override to change the format.
+	 * @param pName The name to validate
+	 * @return True if the name is valid
+	 */
+	protected boolean validateName(String pName)
+	{
+		return ValidationUtils.verifyIDRelaxed(fName);
+	}
+}

File ca.mcgill.cs.swevo.qualyzer/src/ca/mcgill/cs/swevo/qualyzer/model/validation/ValidationUtils.java

 		
 		return id.length() > 0;
 	}
+	
+	/*
+	 * Verifies that an id is valid. Package-private on purpose. To perform validation, a 
+	 * validator object should be used.
+	 * An id is valid if and only if it contains only letters (accents ok) and '_' or '-'.
+	 * @param id 
+	 * @return 
+	 */
+	static boolean verifyIDRelaxed(String id)
+	{
+		for(int i = 0; i < id.length(); i++)
+		{
+			char c = id.charAt(i);
+			if(Character.isLetterOrDigit(c)) //isAlpha
+			{
+				continue;
+			}
+			else if(c == ' ' || c == '_' || c == '-')
+			{
+				continue;
+			}
+			else
+			{
+				return false;
+			}
+		}
+		return id.length() > 0;
+	}
 
 }