001: package com.xoetrope.validation;
002:
003: import java.util.Date;
004: import net.xoetrope.xml.XmlElement;
005: import net.xoetrope.xui.XProject;
006:
007: import net.xoetrope.xui.validation.XBaseValidator;
008:
009: /**
010: * Validates names, by ensuring they are properly formed, of adequate length and
011: * containing a valid set of characters
012: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
013: * the GNU Public License (GPL), please see license.txt for more details. If
014: * you make commercial use of this software you must purchase a commercial
015: * license from Xoetrope.</p>
016: * <p> $Revision: 1.8 $</p>
017: */
018: public class XNameValidator extends XBaseValidator {
019: private boolean firstName;
020:
021: /**
022: * Validates a name
023: * @param isFirstName true if this is a first name validator
024: * @param msg The message to be displayed if validation fails
025: */
026: public XNameValidator(XProject project) {
027: super (project);
028: }
029:
030: /**
031: * Set the validation parameters
032: * @param element the validator parameters
033: */
034: public void setup(XmlElement element) {
035: super .setup(element);
036:
037: boolean firstName = false;
038: String fs = element.getAttribute("first");
039: if (fs != null)
040: firstName = Boolean.valueOf(fs).booleanValue();
041: }
042:
043: /**
044: * Validates that a name has been entered. For first names initials are accepted
045: * for part of the name while for a second name words of at least two letters
046: * are required.
047: * @param comp The component triggering the validation
048: * @throws Exception Contains the details of the message
049: */
050: public void validate(Object comp, boolean forceMandatory)
051: throws Exception {
052: errorLevel = LEVEL_IGNORE;
053: String text = getText(comp).trim();
054: boolean failed = false;
055:
056: if ((text.length() > 0) && (mandatory || forceMandatory)) {
057: Date value = null;
058:
059: int numSpaces = 0;
060: int len = text.length();
061: if (len < 2)
062: failed = true;
063: else {
064: for (int i = 0; i < len; i++) {
065: char c = text.charAt(i);
066: Character ch = new Character(c);
067:
068: // Treat an apostrophy as a space
069: if ((ch.isSpaceChar(c)) || (c == '\'')) {
070: if (numSpaces > 0) {
071: failed = true;
072: break;
073: }
074: numSpaces++;
075: } else if (ch.isDigit(c)) {
076: failed = true;
077: break;
078: } else if (!ch.isLetter(c)) {
079: failed = true;
080: break;
081: }
082: }
083:
084: // For a first name one of the words must have at least two letters e.g. John P
085: // and for a double barreled name each word must have at least two letters
086: // e.g. Da Vinci
087: if (len < (2 + (firstName ? 2 : 3) * numSpaces))
088: failed = true;
089: }
090: } else if (mandatory || forceMandatory) { // Text length is zero
091: failed = true;
092: errorLevel = LEVEL_WARNING;
093: }
094:
095: if (failed) {
096: if (errorLevel != LEVEL_WARNING)
097: errorLevel = LEVEL_ERROR;
098: throwException();
099: }
100: }
101: }
|