01: package com.xoetrope.validation;
02:
03: import net.xoetrope.xml.XmlElement;
04: import net.xoetrope.xui.XProject;
05: import net.xoetrope.xui.XProjectManager;
06:
07: import net.xoetrope.xui.data.XModel;
08: import net.xoetrope.xui.validation.XBaseValidator;
09:
10: /**
11: * Validates names, by checking them against a list or set of names
12: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
13: * the GNU Public License (GPL), please see license.txt for more details. If
14: * you make commercial use of this software you must purchase a commercial
15: * license from Xoetrope.</p>
16: * <p> $Revision: 1.8 $</p>
17: */
18: public class XSetValidator extends XBaseValidator {
19: private XModel dataSource;
20:
21: /**
22: * Validates a name
23: */
24: public XSetValidator(XProject project) {
25: super (project);
26: }
27:
28: /**
29: * Set the validation parameters
30: * @param element the validator parameters
31: */
32: public void setup(XmlElement element) {
33: super .setup(element);
34:
35: String source = element.getAttribute("source");
36: dataSource = (XModel) currentProject.getModel().get(source);
37: }
38:
39: /**
40: * Does the checking of the value against the data set.
41: * @param comp The component triggering the validation
42: * @throws Exception Contains the details of the message
43: */
44: public void validate(Object comp, boolean forceMandatory)
45: throws Exception {
46: errorLevel = LEVEL_IGNORE;
47: String text = getText(comp).trim();
48: boolean failed = false;
49:
50: if ((text.length() > 0) && (mandatory || forceMandatory)) {
51: int numValues = dataSource.getNumChildren();
52: int i = 0;
53: for (; i < numValues; i++) {
54: if (dataSource.get(i).get().toString()
55: .compareToIgnoreCase(text) == 0)
56: break;
57: }
58: if (i == numValues)
59: failed = true;
60: } else if (mandatory || forceMandatory) // Text length is zero
61: failed = true;
62:
63: if (failed) {
64: errorLevel = LEVEL_WARNING;
65: throwException();
66: }
67: }
68: }
|