001: package net.xoetrope.xui.validation;
002:
003: import java.lang.reflect.InvocationTargetException;
004: import java.lang.reflect.Method;
005:
006: import java.awt.Component;
007: import java.awt.Container;
008:
009: import net.xoetrope.xui.XTextHolder;
010: import net.xoetrope.xui.helper.MessageHelper;
011: import net.xoetrope.xml.XmlElement;
012:
013: /**
014: * <p>A basic implementation of the XValidator interface</p>
015: * <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003<br>
016: * License: see license.txt</p>
017: * $Revision: 1.14 $
018: */
019: public abstract class XBaseValidator implements XValidator {
020: protected boolean mandatory;
021: protected String message, value, formattedMessage, validationName;
022: protected Container container;
023: protected Method validationMethod;
024: protected int mask;
025: protected int errorLevel = XValidator.LEVEL_IGNORE;
026: protected boolean start = true;
027:
028: public XBaseValidator() {
029: }
030:
031: /**
032: * Constructor sets up member variables
033: * @param name
034: * @param validatorMask
035: */
036: public XBaseValidator(String name, int validatorMask) {
037: validationName = name;
038: mask = validatorMask;
039: }
040:
041: /**
042: * Get the event mask. The mask helps filter the events for which the
043: * validator is invoked
044: * @return the mask
045: */
046: public int getMask() {
047: return mask;
048: }
049:
050: /**
051: * Set the event mask. The mask helps filter the events for which the
052: * validator is invoked
053: * @return the new mask value
054: */
055: public void setMask(int newMask) {
056: mask = newMask;
057: }
058:
059: /**
060: * Set the Method of the XPage to be invoked when we are doing a funcion
061: * validation
062: * @param m The method to be invoked
063: * @param page The XPage which contains the Method.
064: */
065: public void setValidationMethod(Method m, Container c) {
066: container = c;
067: validationMethod = m;
068: }
069:
070: /**
071: * Get the name of the validation
072: * @return The name
073: */
074: public String getName() {
075: return validationName;
076: }
077:
078: /**
079: * Set the name of the validation
080: * @param name the new name
081: */
082: public void setName(String name) {
083: validationName = name;
084: }
085:
086: /**
087: * Get the validation message which is formatted after the tokens have been
088: * replaced
089: * @return The formatted message.
090: */
091: public String getMessage() {
092: return formattedMessage;
093: }
094:
095: /**
096: * Set the validation parameters
097: * @param element the validator parameters
098: */
099: public void setup(XmlElement element) {
100: message = element.getAttribute("msg");
101: formattedMessage = message;
102: }
103:
104: /**
105: * Get the value as a string object
106: * @return the value as string
107: */
108: public String getValueAsString() {
109: return value;
110: }
111:
112: /**
113: * Get the error level of the validations
114: * @return the error level
115: */
116: public int getLevel() {
117: return errorLevel;
118: }
119:
120: /**
121: * Call the funcion to get the value we are validating.
122: * @return The result of the function call
123: */
124: public Object invokeMethod() {
125: if (validationMethod != null) {
126: try {
127: return validationMethod.invoke(container, null);
128: } catch (IllegalAccessException ex) {
129: return null;
130: } catch (IllegalArgumentException ex) {
131: return null;
132: } catch (InvocationTargetException ex) {
133: return null;
134: }
135: }
136: return "";
137: }
138:
139: /**
140: * Carry out a replacement on the validation message
141: * @param lookup The text to find
142: * @param replacement The text to replace the text with
143: */
144: protected void replaceToken(String lookup, String replacement) {
145: if (start)
146: formattedMessage = message;
147: formattedMessage = MessageHelper.getInstance().replaceToken(
148: formattedMessage, lookup, replacement);
149: start = false;
150: }
151:
152: /**
153: * Replace any further tokens in the message
154: */
155: protected void replaceTokens() {
156: if (start)
157: formattedMessage = message;
158: formattedMessage = MessageHelper.getInstance().replaceTokens(
159: formattedMessage);
160: start = false;
161: }
162:
163: /**
164: * Get the text value of the component.
165: * @param c The component
166: * @return The text components value
167: */
168: protected String getText(Component c) {
169: return ((XTextHolder) c).getText();
170: }
171:
172: /**
173: * Replace the remaining tokens and throw the exception
174: * @throws Exception
175: */
176: protected void throwException() throws Exception {
177: replaceTokens();
178: start = true;
179: throw new Exception(formattedMessage);
180: }
181: }
|