1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
package org.apache.tapestry.form.translator; |
16 | |
|
17 | |
import org.apache.hivemind.util.PropertyUtils; |
18 | |
import org.apache.tapestry.IMarkupWriter; |
19 | |
import org.apache.tapestry.IRequestCycle; |
20 | |
import org.apache.tapestry.form.FormComponentContributorContext; |
21 | |
import org.apache.tapestry.form.IFormComponent; |
22 | |
import org.apache.tapestry.json.JSONLiteral; |
23 | |
import org.apache.tapestry.json.JSONObject; |
24 | |
import org.apache.tapestry.valid.ValidationConstants; |
25 | |
import org.apache.tapestry.valid.ValidationConstraint; |
26 | |
import org.apache.tapestry.valid.ValidationStrings; |
27 | |
|
28 | |
import java.text.DateFormatSymbols; |
29 | |
import java.text.Format; |
30 | |
import java.text.SimpleDateFormat; |
31 | |
import java.util.Locale; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public class DateTranslator extends FormatTranslator |
40 | |
{ |
41 | 0 | private boolean _lenient=true; |
42 | |
|
43 | 0 | protected SimpleDateFormat _rfc339Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); |
44 | |
|
45 | |
public DateTranslator() |
46 | 0 | { |
47 | 0 | } |
48 | |
|
49 | |
|
50 | |
public DateTranslator(String initializer) |
51 | 0 | { |
52 | 0 | PropertyUtils.configureProperties(this, initializer); |
53 | 0 | } |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
protected String defaultPattern() |
59 | |
{ |
60 | 0 | return "MM/dd/yyyy"; |
61 | |
} |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
protected Format getFormat(Locale locale) |
67 | |
{ |
68 | 0 | return getDateFormat(locale); |
69 | |
} |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
public String formatRfc3339(Object input) |
79 | |
{ |
80 | 0 | return _rfc339Format.format(input); |
81 | |
} |
82 | |
|
83 | |
public SimpleDateFormat getDateFormat(Locale locale) |
84 | |
{ |
85 | 0 | SimpleDateFormat ret = new SimpleDateFormat(getPattern(), new DateFormatSymbols(locale)); |
86 | 0 | ret.setLenient(_lenient); |
87 | |
|
88 | 0 | return ret; |
89 | |
} |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
protected String getMessageKey() |
95 | |
{ |
96 | 0 | return ValidationStrings.INVALID_DATE; |
97 | |
} |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
protected Object[] getMessageParameters(Locale locale, String label) |
104 | |
{ |
105 | 0 | String pattern = getDateFormat(locale).toLocalizedPattern().toUpperCase(locale); |
106 | |
|
107 | 0 | return new Object[] { label, pattern }; |
108 | |
} |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, |
115 | |
FormComponentContributorContext context, IFormComponent field) |
116 | |
{ |
117 | 0 | super.renderContribution(writer, cycle, context, field); |
118 | |
|
119 | 0 | String message = buildMessage(context, field, getMessageKey()); |
120 | |
|
121 | 0 | JSONObject profile = context.getProfile(); |
122 | 0 | if (!profile.has(ValidationConstants.CONSTRAINTS)) { |
123 | 0 | profile.put(ValidationConstants.CONSTRAINTS, new JSONObject()); |
124 | |
} |
125 | |
|
126 | 0 | JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS); |
127 | |
|
128 | 0 | context.addInitializationScript(field, "dojo.require(\"tapestry.form.datetime\");"); |
129 | |
|
130 | 0 | accumulateProperty(cons, field.getClientId(), |
131 | |
new JSONLiteral("[tapestry.form.datetime.isValidDate,{" |
132 | |
+ "datePattern:" |
133 | |
+ JSONObject.quote(getPattern()) |
134 | |
+ (isLenient() ? "" : ",strict:true") |
135 | |
+ "}]")); |
136 | |
|
137 | 0 | accumulateProfileProperty(field, profile, ValidationConstants.CONSTRAINTS, message); |
138 | 0 | } |
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
protected ValidationConstraint getConstraint() |
144 | |
{ |
145 | 0 | return ValidationConstraint.DATE_FORMAT; |
146 | |
} |
147 | |
|
148 | |
public void setLenient(boolean value) |
149 | |
{ |
150 | 0 | _lenient = value; |
151 | 0 | } |
152 | |
|
153 | |
public boolean isLenient() |
154 | |
{ |
155 | 0 | return _lenient; |
156 | |
} |
157 | |
} |