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 |
18 |
private boolean _lenient=true; |
42 |
|
|
43 |
18 |
protected SimpleDateFormat _rfc339Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); |
44 |
|
|
45 |
|
public DateTranslator() |
46 |
16 |
{ |
47 |
16 |
} |
48 |
|
|
49 |
|
|
50 |
|
public DateTranslator(String initializer) |
51 |
2 |
{ |
52 |
2 |
PropertyUtils.configureProperties(this, initializer); |
53 |
2 |
} |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
protected String defaultPattern() |
59 |
|
{ |
60 |
18 |
return "MM/dd/yyyy"; |
61 |
|
} |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
protected Format getFormat(Locale locale) |
67 |
|
{ |
68 |
21 |
return getDateFormat(locale); |
69 |
|
} |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
public String formatRfc3339(Object input) |
79 |
|
{ |
80 |
1 |
return _rfc339Format.format(input); |
81 |
|
} |
82 |
|
|
83 |
|
public SimpleDateFormat getDateFormat(Locale locale) |
84 |
|
{ |
85 |
27 |
SimpleDateFormat ret = new SimpleDateFormat(getPattern(), new DateFormatSymbols(locale)); |
86 |
27 |
ret.setLenient(_lenient); |
87 |
|
|
88 |
27 |
return ret; |
89 |
|
} |
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
protected String getMessageKey() |
95 |
|
{ |
96 |
4 |
return ValidationStrings.INVALID_DATE; |
97 |
|
} |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
protected Object[] getMessageParameters(Locale locale, String label) |
104 |
|
{ |
105 |
4 |
String pattern = getDateFormat(locale).toLocalizedPattern().toUpperCase(locale); |
106 |
|
|
107 |
4 |
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 |
2 |
super.renderContribution(writer, cycle, context, field); |
118 |
|
|
119 |
2 |
String message = buildMessage(context, field, getMessageKey()); |
120 |
|
|
121 |
2 |
JSONObject profile = context.getProfile(); |
122 |
2 |
if (!profile.has(ValidationConstants.CONSTRAINTS)) { |
123 |
2 |
profile.put(ValidationConstants.CONSTRAINTS, new JSONObject()); |
124 |
|
} |
125 |
|
|
126 |
2 |
JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS); |
127 |
|
|
128 |
2 |
context.addInitializationScript(field, "dojo.require(\"tapestry.form.datetime\");"); |
129 |
|
|
130 |
2 |
accumulateProperty(cons, field.getClientId(), |
131 |
|
new JSONLiteral("[tapestry.form.datetime.isValidDate,{" |
132 |
|
+ "datePattern:" |
133 |
|
+ JSONObject.quote(getPattern()) |
134 |
|
+ (isLenient() ? "" : ",strict:true") |
135 |
|
+ "}]")); |
136 |
|
|
137 |
2 |
accumulateProfileProperty(field, profile, ValidationConstants.CONSTRAINTS, message); |
138 |
2 |
} |
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
protected ValidationConstraint getConstraint() |
144 |
|
{ |
145 |
2 |
return ValidationConstraint.DATE_FORMAT; |
146 |
|
} |
147 |
|
|
148 |
|
public void setLenient(boolean value) |
149 |
|
{ |
150 |
1 |
_lenient = value; |
151 |
1 |
} |
152 |
|
|
153 |
|
public boolean isLenient() |
154 |
|
{ |
155 |
7 |
return _lenient; |
156 |
|
} |
157 |
|
} |