1
2
3
4 package org.rcfaces.core.internal.validator.impl;
5
6 import java.util.Calendar;
7 import java.util.Date;
8 import java.util.regex.Matcher;
9 import java.util.regex.Pattern;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.rcfaces.core.internal.renderkit.IProcessContext;
14 import org.rcfaces.core.validator.ICheckerTask;
15 import org.rcfaces.core.validator.IClientValidatorContext;
16
17
18
19
20
21
22 public class DateChecker extends AbstractClientValidatorTask implements
23 ICheckerTask {
24 private static final String REVISION = "$Revision: 1.2 $";
25
26 private static final Log LOG = LogFactory.getLog(DateChecker.class);
27
28 private static final Pattern DATE_SEPARATOR_PATTERN = Pattern
29 .compile("^[0-9]*$");
30
31 public String applyChecker(IClientValidatorContext context, String value) {
32 if (value == null || value.length() == 0) {
33 return value;
34 }
35
36 String sep = getParameter(context, "date.sepSign");
37 String set = "[" + buildEscaped(sep) + "]";
38 char sepChar = sep.charAt(0);
39
40
41 String d = null;
42 String m = null;
43 String y = null;
44 boolean invalidFormat = true;
45
46 Matcher matcher = DATE_SEPARATOR_PATTERN.matcher(value);
47
48
49 if (matcher.matches()) {
50 switch (matcher.end()) {
51 case 8:
52 case 6:
53 y = value.substring(4);
54 case 4:
55 m = value.substring(2, 4);
56 case 2:
57 d = value.substring(0, 2);
58 invalidFormat = false;
59 break;
60 case 1:
61 d = value;
62 case 0:
63 invalidFormat = false;
64 break;
65 }
66
67 } else {
68 Pattern pattern = getPattern("^(\\d{1,2})?" + set + "(\\d{1,2})?"
69 + set + "?(\\d{2}|\\d{4})?$");
70
71 matcher = pattern.matcher(value);
72
73 if (matcher.matches()) {
74 invalidFormat = false;
75
76 d = matcher.group(0);
77 m = matcher.group(1);
78 y = matcher.group(2);
79 }
80 }
81
82
83 if (invalidFormat) {
84 String formattedValue = value.replaceAll("(" + set + ")", String
85 .valueOf(sepChar));
86
87 context.setInputValue(formattedValue);
88 context.setOutputValue(formattedValue);
89 return null;
90 }
91
92 int pivot = getIntParameter(context, "date.pivot", 0);
93
94 IProcessContext processContext = context.getComponentRenderContext()
95 .getRenderContext().getProcessContext();
96
97 int iy;
98 int im;
99 int id;
100
101 Calendar calendar = processContext.getUserCalendar();
102 synchronized (calendar) {
103 calendar.setTime(new Date());
104
105 if (y != null && y.length() > 0) {
106 iy = Integer.parseInt(y);
107
108 if (iy < 100) {
109 iy += (iy > pivot) ? 1900 : 2000;
110 }
111 } else {
112 iy = calendar.get(Calendar.YEAR);
113 }
114
115 if (m != null && m.length() > 0) {
116 im = Integer.parseInt(m);
117
118 } else {
119 im = calendar.get(Calendar.MONTH) + 1;
120 }
121
122 if (d != null && d.length() > 0) {
123 id = Integer.parseInt(d);
124
125 } else {
126 id = calendar.get(Calendar.DAY_OF_MONTH);
127 }
128 }
129
130
131 String formattedValue = ((id < 10) ? "0" : "") + id + sepChar
132 + ((im < 10) ? "0" : "") + im + sepChar + iy;
133
134 context.setInputValue(formattedValue);
135 context.setOutputValue(formattedValue);
136
137 int min = getIntParameter(context, "date.min", 1850);
138 int max = getIntParameter(context, "date.max", 2100);
139
140
141 if (iy < min || iy > max) {
142 return null;
143 }
144
145 synchronized (calendar) {
146 calendar.setTime(new Date());
147
148 calendar.set(Calendar.HOUR_OF_DAY, 0);
149 calendar.set(Calendar.MINUTE, 0);
150 calendar.set(Calendar.SECOND, 0);
151 calendar.set(Calendar.MILLISECOND, 0);
152
153 calendar.set(Calendar.YEAR, iy);
154 calendar.set(Calendar.MONTH, im - 1);
155 calendar.set(Calendar.DAY_OF_MONTH, id);
156
157 if (calendar.get(Calendar.YEAR) != iy
158 || calendar.get(Calendar.MONTH) != im - 1
159 || calendar.get(Calendar.DAY_OF_MONTH) != id) {
160 return null;
161 }
162 }
163
164 return formattedValue;
165 }
166 }