001: package net.xoetrope.xui.validation;
002:
003: import java.io.Reader;
004: import java.lang.reflect.Method;
005: import java.util.Enumeration;
006: import java.util.Hashtable;
007:
008: import net.xoetrope.xml.XmlElement;
009: import net.xoetrope.xml.XmlSource;
010: import java.awt.Container;
011: import net.xoetrope.xui.XPage;
012: import net.xoetrope.xui.build.BuildProperties;
013: import net.xoetrope.debug.DebugLogger;
014:
015: /**
016: * <p>Constucts validations be reading rules from a configuration file</p>
017: * <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003<br>
018: * License: see license.txt</p>
019: * $Revision: 1.16 $
020: */
021: public class XValidationFactory {
022: protected Hashtable validations;
023:
024: public XValidationFactory() {
025: }
026:
027: /**
028: * Constructor which reads validations from the reader
029: * @param reader Reader of file containing validations
030: */
031: public XValidationFactory(Reader reader) {
032: read(reader);
033: }
034:
035: /**
036: * Loads XmlElements from the file and puts the element into the validations
037: * Hashtable with the name of the validation as the key.
038: * @param read
039: */
040: public void read(Reader read) {
041: XmlElement element = XmlSource.read(read);
042: Enumeration e = element.getChildren().elements();
043: validations = new Hashtable();
044: while (e.hasMoreElements()) {
045: XmlElement ele = (XmlElement) e.nextElement();
046: String name = ele.getAttribute("name");
047: validations.put(name, ele);
048: }
049:
050: }
051:
052: /**
053: * Creates an XValidation object which validates against a value returned from
054: * a function call
055: * @param validationName The name of the validation
056: * @param m The Method to call in order to get the value to validate against
057: * @param mask
058: * @param page The page object which contains the function
059: * @return The new XValidator
060: */
061: public XValidator getValidation(String validationName, Method m,
062: int mask, Container page) {
063: XValidator validator = getValidation(validationName, mask, page);
064: if (validator != null)
065: validator.setValidationMethod(m, page);
066: return validator;
067: }
068:
069: /**
070: * Gets the validation for the validationName. Checks first to see if the
071: * validation is a predefined type in which case it reads it's required
072: * attributes. If the validation type is custom we just need to create a
073: * validator from the class attribute
074: * @param validationName The name of the validation
075: * @param mask
076: * @return The new XValidator
077: */
078: public XValidator getValidation(String validationName, int mask,
079: Container page) {
080: XmlElement ele = (XmlElement) validations.get(validationName);
081: if (BuildProperties.DEBUG) {
082: if (ele == null) {
083: DebugLogger
084: .logError("Cannot find the validation rule: "
085: + validationName);
086: return null;
087: }
088: }
089:
090: String type = ele.getAttribute("type");
091:
092: if (type.compareTo("minmax") == 0) {
093: XMinMaxValidator mmValidator = new XMinMaxValidator(
094: validationName, mask);
095: mmValidator.setup(ele);
096: return mmValidator;
097: } else if (type.compareTo("mandatory") == 0) {
098: XMandatoryValidator mandatoryValidator = new XMandatoryValidator(
099: validationName, mask);
100: mandatoryValidator.setup(ele);
101: return mandatoryValidator;
102: } else if (type.compareTo("custom") == 0) {
103: String className = ele.getAttribute("class");
104: XBaseValidator validator = null;
105: try {
106: validator = (XBaseValidator) Class.forName(className)
107: .newInstance();
108: validator.setName(validationName);
109: validator.setMask(mask);
110: validator.setup(ele);
111: } catch (Exception ex) {
112: ex.printStackTrace();
113: }
114: return validator;
115: }
116: return null;
117: }
118: }
|