001: package com.xoetrope.validation;
002:
003: import java.text.DateFormat;
004: import java.text.ParseException;
005: import java.util.Date;
006: import net.xoetrope.xui.build.BuildProperties;
007: import net.xoetrope.debug.DebugLogger;
008: import net.xoetrope.xui.XPage;
009: import net.xoetrope.xui.validation.XBaseValidator;
010: import java.text.SimpleDateFormat;
011: import java.text.ParsePosition;
012: import net.xoetrope.xml.XmlElement;
013: import net.xoetrope.xui.XProject;
014:
015: /**
016: * A date validator
017: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
018: * the GNU Public License (GPL), please see license.txt for more details. If
019: * you make commercial use of this software you must purchase a commercial
020: * license from Xoetrope.</p>
021: * <p> $Revision: 1.8 $</p>
022: */
023: public class XDateValidator extends XBaseValidator {
024: private Date min, max;
025: private XPage page;
026: private String dateFormat = "dd/MM/yyyy";
027:
028: /**
029: * Constructors the XMinMaxValidator
030: * @param project the owner project
031: */
032: /** @todo change the API for this validator */
033: public XDateValidator(XProject project) {
034: super (project);
035: }
036:
037: /**
038: * Set the validation parameters
039: * @param element the validator parameters
040: */
041: public void setup(XmlElement element) {
042: super .setup(element);
043: String startDate = element.getAttribute("min");
044: String endDate = element.getAttribute("max");
045: String format = element.getAttribute("format");
046:
047: if ((format != null) && (format.length() > 0))
048: dateFormat = format;
049:
050: // Setup the min and max bounds if needed
051: if (startDate != null) {
052: try {
053: min = DateFormat.getInstance().parse(startDate);
054: } catch (Exception ex) {
055: if (BuildProperties.DEBUG)
056: DebugLogger.logWarning("Invalid start date: "
057: + startDate);
058: }
059: }
060:
061: if (endDate != null) {
062: try {
063: max = DateFormat.getInstance().parse(endDate);
064: } catch (Exception ex) {
065: if (BuildProperties.DEBUG)
066: DebugLogger.logWarning("Invalid end date: "
067: + endDate);
068: }
069: }
070: }
071:
072: /**
073: * Carries out the min-max validation on the value entered. If the Method
074: * validationMethod is not null we have to invoke it in order to get it's
075: * return value. If the Method invocation returns a null we do not validate.
076: * If a problem is found we format the message using the messageHelper.
077: * @param c The component triggering the validation
078: * @throws Exception Contains the details of the message
079: */
080: public void validate(Object c, boolean forceMandatory)
081: throws Exception {
082: errorLevel = LEVEL_IGNORE;
083: formattedMessage = message;
084: String text = getText(c);
085: Object ret = null;
086: Object funcResult = null;
087: boolean failed = false;
088: if (mandatory || forceMandatory) {
089: try {
090: Date dateValue = null;
091:
092: if (text.length() == 0) {
093: failed = true;
094: // errorLevel = LEVEL_WARNING;
095: } else if (validationMethod == null) {
096: if (text.length() > 0) {
097: ParsePosition pp = new ParsePosition(0);
098: SimpleDateFormat formatter = new SimpleDateFormat(
099: dateFormat);
100: formatter.setLenient(false);
101: dateValue = formatter.parse(text, pp);
102: if (dateValue == null) {
103: failed = true;
104: errorLevel = LEVEL_ERROR;
105: }
106: }
107: } else {
108: /* We need to get the value being compared from the function call */
109: funcResult = invokeMethod();
110: if (funcResult != null) {
111: try {
112: dateValue = DateFormat.getInstance().parse(
113: funcResult.toString());
114: } catch (ParseException e) {
115: dateValue = null;
116: }
117: }
118: }
119: /* We are either validating a basic value or the results of the Method
120: invocation if it is not null.
121: */
122: if ((validationMethod == null) || (funcResult != null)) {
123: value = String.valueOf(dateValue);
124: if ((text.length() == 0)
125: && (mandatory || forceMandatory))
126: failed = true;
127: else if ((min != null) && (dateValue.before(min)))
128: failed = true;
129: else if ((max != null) && (dateValue.after(max)))
130: failed = true;
131:
132: if (failed) {
133: if (min != null)
134: replaceToken("{min}", min.toString());
135: if (max != null)
136: replaceToken("{max}", max.toString());
137:
138: if (funcResult != null)
139: replaceToken("{value}", funcResult
140: .toString());
141: else
142: replaceToken("{value}", text);
143: }
144: }
145:
146: } catch (Exception ex) {
147: failed = true;
148: }
149:
150: if (failed) {
151: if (mandatory && forceMandatory)
152: errorLevel = LEVEL_ERROR;
153: else
154: errorLevel = Math.max(LEVEL_WARNING, errorLevel);
155: throwException();
156: }
157: }
158: }
159: }
|