|
|||||||||||||||||||
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
AbstractTranslator.java | 100% | 77.8% | 66.7% | 80% |
|
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.translator; | |
16 | ||
17 | import java.text.MessageFormat; | |
18 | import java.util.Locale; | |
19 | ||
20 | import org.apache.hivemind.HiveMind; | |
21 | import org.apache.tapestry.IForm; | |
22 | import org.apache.tapestry.IMarkupWriter; | |
23 | import org.apache.tapestry.IRequestCycle; | |
24 | import org.apache.tapestry.form.AbstractFormComponentContributor; | |
25 | import org.apache.tapestry.form.FormComponentContributorContext; | |
26 | import org.apache.tapestry.form.IFormComponent; | |
27 | import org.apache.tapestry.valid.ValidationStrings; | |
28 | import org.apache.tapestry.valid.ValidatorException; | |
29 | ||
30 | /** | |
31 | * Abstract {@link Translator} implementation that provides default behavior for trimming, null | |
32 | * object, and empty text handling. | |
33 | * | |
34 | * @author Paul Ferraro | |
35 | * @since 4.0 | |
36 | */ | |
37 | public abstract class AbstractTranslator extends AbstractFormComponentContributor implements | |
38 | Translator | |
39 | { | |
40 | private boolean _trim; | |
41 | ||
42 | private String _message; | |
43 | ||
44 | 30 | public AbstractTranslator() |
45 | { | |
46 | } | |
47 | ||
48 | // Needed until HIVEMIND-134 fix is available | |
49 | 0 | public AbstractTranslator(String initializer) |
50 | { | |
51 | 0 | super(initializer); |
52 | } | |
53 | ||
54 | /** | |
55 | * @see org.apache.tapestry.form.translator.Translator#format(org.apache.tapestry.form.IFormComponent, | |
56 | * java.lang.Object) | |
57 | */ | |
58 | 8 | public String format(IFormComponent field, Object object) |
59 | { | |
60 | 8 | return (object != null) ? formatObject(field, object) : ""; |
61 | } | |
62 | ||
63 | /** | |
64 | * @see org.apache.tapestry.form.translator.Translator#parse(org.apache.tapestry.form.IFormComponent, | |
65 | * java.lang.String) | |
66 | */ | |
67 | 15 | public Object parse(IFormComponent field, String text) throws ValidatorException |
68 | { | |
69 | 15 | String value = _trim ? text.trim() : text; |
70 | ||
71 | 15 | return HiveMind.isBlank(value) ? getEmpty() : parseText(field, value); |
72 | } | |
73 | ||
74 | protected abstract String formatObject(IFormComponent field, Object object); | |
75 | ||
76 | protected abstract Object parseText(IFormComponent field, String text) | |
77 | throws ValidatorException; | |
78 | ||
79 | 1 | protected Object getEmpty() |
80 | { | |
81 | 1 | return null; |
82 | } | |
83 | ||
84 | 7 | protected String buildMessage(IFormComponent field, String key) |
85 | { | |
86 | 7 | Locale locale = field.getPage().getLocale(); |
87 | ||
88 | 7 | String pattern = (_message == null) ? ValidationStrings.getMessagePattern(key, locale) |
89 | : _message; | |
90 | ||
91 | 7 | String name = field.getDisplayName(); |
92 | ||
93 | 7 | return MessageFormat.format(pattern, getMessageParameters(locale, name)); |
94 | } | |
95 | ||
96 | 0 | protected Object[] getMessageParameters(Locale locale, String label) |
97 | { | |
98 | 0 | return new Object[] |
99 | { label }; | |
100 | } | |
101 | ||
102 | /** | |
103 | * @see org.apache.tapestry.form.FormComponentContributor#renderContribution(org.apache.tapestry.IRequestCycle, | |
104 | * org.apache.tapestry.form.IFormComponent) | |
105 | */ | |
106 | 7 | public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, FormComponentContributorContext context, IFormComponent field) |
107 | { | |
108 | 7 | super.renderContribution(writer, cycle, context, field); |
109 | ||
110 | 7 | if (_trim) |
111 | { | |
112 | 3 | IForm form = field.getForm(); |
113 | ||
114 | 3 | addSubmitHandler(form, "trim(document." + form.getName() + "." + field.getName() + ")"); |
115 | } | |
116 | } | |
117 | ||
118 | 0 | public boolean isTrim() |
119 | { | |
120 | 0 | return _trim; |
121 | } | |
122 | ||
123 | 6 | public void setTrim(boolean trim) |
124 | { | |
125 | 6 | _trim = trim; |
126 | } | |
127 | ||
128 | 0 | public String getMessage() |
129 | { | |
130 | 0 | return _message; |
131 | } | |
132 | ||
133 | 3 | public void setMessage(String message) |
134 | { | |
135 | 3 | _message = message; |
136 | } | |
137 | } |
|