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: *
013: * A password must be at least 6 characters excluding spaces. A strong password
014: * must also include at least one space and one number or non alphanumeric in
015: * addition to the 6 character requirement for a normal password.
016: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
017: * the GNU Public License (GPL), please see license.txt for more details. If
018: * you make commercial use of this software you must purchase a commercial
019: * license from Xoetrope.</p>
020: * <p> $Revision: 1.8 $</p>
021: */
022: public class XPasswordValidator extends XBaseValidator {
023: private boolean strong;
024:
025: /**
026: * Validates a password.
027: * @param isStrong true if this is to be a strong password
028: * @param msg The message to be displayed if validation fails
029: */
030: public XPasswordValidator(XProject project) {
031: super (project);
032: }
033:
034: /**
035: * Set the validation parameters
036: * @param element the validator parameters
037: */
038: public void setup(XmlElement element) {
039: super .setup(element);
040:
041: strong = false;
042: if (element.getAttribute("strong") != null)
043: strong = Boolean.valueOf(element.getAttribute("strong"))
044: .booleanValue();
045: message = element.getAttribute("msg");
046: }
047:
048: /**
049: * Does the checking of the password
050: * @param comp The component triggering the validation
051: * @throws Exception Contains the details of the message
052: */
053: public void validate(Object comp, boolean forceMandatory)
054: throws Exception {
055: errorLevel = LEVEL_IGNORE;
056: String text = getText(comp).trim();
057: boolean failed = false;
058:
059: if ((text.length() > 0) && (mandatory || forceMandatory)) {
060: Date value = null;
061:
062: int numSpaces = 0;
063: int numNumbers = 0;
064: int numNonAlphaNumeric = 0;
065: int numVowels = 0;
066: int len = text.length();
067: if (len < 6)
068: failed = true;
069: else {
070: for (int i = 0; i < len; i++) {
071: char c = text.charAt(i);
072: Character ch = new Character(c);
073:
074: // Treat an apostrophy as a space
075: if (ch.isSpaceChar(c))
076: numSpaces++;
077: else if (ch.isDigit(c))
078: numNumbers++;
079: else if (!ch.isLetter(c))
080: numNonAlphaNumeric++;
081: else {
082: switch (c) {
083: case 'a':
084: case 'e':
085: case 'i':
086: case 'o':
087: case 'u':
088: case 'A':
089: case 'E':
090: case 'I':
091: case 'O':
092: case 'U':
093: numVowels++;
094: break;
095: }
096: }
097: }
098:
099: if ((len - numSpaces) < 6)
100: failed = true;
101:
102: // Check for additional requirements for a strong password
103: if (strong) {
104: if (numSpaces < 1)
105: failed = true;
106: if ((numNumbers < 1) && (numNonAlphaNumeric < 1))
107: failed = true;
108: if ((len - numSpaces - numVowels) < 6)
109: failed = true;
110: }
111: }
112: } else if (mandatory || forceMandatory) // Text length is zero
113: failed = true;
114:
115: if (failed) {
116: errorLevel = LEVEL_WARNING;
117: throwException();
118: }
119: }
120: }
|