Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
BaseValidator |
|
| 1.4;1.4 |
1 | // Copyright 2005 The Apache Software Foundation |
|
2 | // |
|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
|
4 | // you may not use this file except in compliance with the License. |
|
5 | // You may obtain a copy of the License at |
|
6 | // |
|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
|
8 | // |
|
9 | // Unless required by applicable law or agreed to in writing, software |
|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
12 | // See the License for the specific language governing permissions and |
|
13 | // limitations under the License. |
|
14 | ||
15 | package org.apache.tapestry.form.validator; |
|
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.form.TranslatedField; |
|
23 | import org.apache.tapestry.form.translator.Translator; |
|
24 | import org.apache.tapestry.json.JSONObject; |
|
25 | ||
26 | /** |
|
27 | * Abstract implementation of {@link org.apache.tapestry.form.validator.Validator}. |
|
28 | * |
|
29 | * @author Howard Lewis Ship |
|
30 | * @since 4.0 |
|
31 | */ |
|
32 | ||
33 | public abstract class BaseValidator implements Validator |
|
34 | { |
|
35 | private String _message; |
|
36 | ||
37 | public BaseValidator() |
|
38 | 24 | { |
39 | 24 | } |
40 | ||
41 | public BaseValidator(String initializer) |
|
42 | 37 | { |
43 | 37 | PropertyUtils.configureProperties(this, initializer); |
44 | 37 | } |
45 | ||
46 | public String getMessage() |
|
47 | { |
|
48 | 43 | return _message; |
49 | } |
|
50 | ||
51 | public void setMessage(String message) |
|
52 | { |
|
53 | 21 | _message = message; |
54 | 21 | } |
55 | ||
56 | /** |
|
57 | * Returns false. |
|
58 | */ |
|
59 | ||
60 | public boolean getAcceptsNull() |
|
61 | { |
|
62 | 0 | return false; |
63 | } |
|
64 | ||
65 | /** |
|
66 | * Does nothing. |
|
67 | */ |
|
68 | ||
69 | public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, |
|
70 | FormComponentContributorContext context, IFormComponent field) |
|
71 | { |
|
72 | 0 | } |
73 | ||
74 | /** |
|
75 | * Returns false. Subclasses may override. |
|
76 | */ |
|
77 | ||
78 | public boolean isRequired() |
|
79 | { |
|
80 | 2 | return false; |
81 | } |
|
82 | ||
83 | /** |
|
84 | * Utility method to store a field specific profile property which can later |
|
85 | * be used by client side validation. |
|
86 | * |
|
87 | * @param field |
|
88 | * The field to store the property for, will key off of {@link IFormComponent#getClientId()}. |
|
89 | * @param profile |
|
90 | * The profile for the form. |
|
91 | * @param key |
|
92 | * The property key to store. |
|
93 | * @param property |
|
94 | * The property to store. |
|
95 | */ |
|
96 | public void accumulateProfileProperty(IFormComponent field, JSONObject profile, |
|
97 | String key, Object property) |
|
98 | { |
|
99 | 17 | if (!profile.has(field.getClientId())) |
100 | 17 | profile.put(field.getClientId(), new JSONObject()); |
101 | ||
102 | 17 | JSONObject fieldProps = profile.getJSONObject(field.getClientId()); |
103 | ||
104 | 17 | accumulateProperty(fieldProps, key, property); |
105 | 17 | } |
106 | ||
107 | /** |
|
108 | * Utility used to append onto an existing property represented as an |
|
109 | * object array. |
|
110 | * @param profile |
|
111 | * @param key |
|
112 | * @param value |
|
113 | */ |
|
114 | public void accumulateProperty(JSONObject profile, String key, Object value) |
|
115 | { |
|
116 | 34 | profile.accumulate(key, value); |
117 | 34 | } |
118 | ||
119 | /** |
|
120 | * Used to grab the corresponding {@link Translator} for |
|
121 | * the field, if one exists. |
|
122 | * @param field |
|
123 | * @return The translator, or null if the required translator type |
|
124 | * doesn't exist. |
|
125 | */ |
|
126 | public Translator getFieldTranslator(IFormComponent field, Class clazz) |
|
127 | { |
|
128 | 26 | if (TranslatedField.class.isAssignableFrom(field.getClass())) { |
129 | 4 | Translator trans = ((TranslatedField)field).getTranslator(); |
130 | 4 | if (clazz.isInstance(trans)) { |
131 | 4 | return trans; |
132 | } |
|
133 | } |
|
134 | ||
135 | 21 | return null; |
136 | } |
|
137 | } |