01: package com.xoetrope.validation;
02:
03: import net.xoetrope.xml.XmlElement;
04: import net.xoetrope.xui.XProject;
05:
06: import org.apache.regexp.RE;
07: import org.apache.regexp.RESyntaxException;
08: import net.xoetrope.xui.validation.XBaseValidator;
09:
10: /**
11: * Validates names, by checking them against a regular expression mask
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.9 $</p>
17: */
18: public class XRegularExpressionValidator extends XBaseValidator {
19: private RE regExpression;
20:
21: /**
22: * Validates a name
23: */
24: public XRegularExpressionValidator(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: try {
36: regExpression = new RE(element.getAttribute("pattern"));
37: } catch (RESyntaxException ex) {
38: ex.printStackTrace();
39: }
40: }
41:
42: /**
43: * Does the checking of the value against the data set.
44: * @param comp The component triggering the validation
45: * @throws Exception Contains the details of the message
46: */
47: public void validate(Object comp, boolean forceMandatory)
48: throws Exception {
49: errorLevel = LEVEL_IGNORE;
50: String text = getText(comp).trim();
51: boolean failed = false;
52:
53: if (mandatory || forceMandatory) {
54: if (text.equals(""))
55: failed = true;
56: else if (!regExpression.match(text))
57: failed = true;
58: else {
59: /* We need to check the value being compared from the function call */
60: Object funcResult = invokeMethod();
61: if (funcResult == null)
62: failed = true;
63: }
64: }
65:
66: if (failed) {
67: errorLevel = LEVEL_WARNING;
68: throwException();
69: }
70: }
71: }
|